Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to allow case equality when the receiver is a constant
For some constant, specially direct subclasses of `BasicObject`, using `is_a?` is not an option since `BasicObject` doesn't respond to that method. In those cases it would be interesting to allow this config so apps can opt-out that cop only when the receiver is a constant.
- Loading branch information
1 parent
a065802
commit ae73ae7
Showing
5 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::CaseEquality do | ||
subject(:cop) { described_class.new } | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
|
||
it 'registers an offense for ===' do | ||
expect_offense(<<~RUBY) | ||
Array === var | ||
^^^ Avoid the use of the case equality operator `===`. | ||
RUBY | ||
end | ||
|
||
context 'when constant checks are allowed' do | ||
let(:config) do | ||
RuboCop::Config.new( | ||
'Style/CaseEquality' => { | ||
'AllowOnConstant' => true | ||
} | ||
) | ||
end | ||
|
||
it 'does not register an offense for === when the receiver is a constant' do | ||
expect_no_offenses(<<~RUBY) | ||
Array === var | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for === when the receiver is not a constant' do | ||
expect_offense(<<~RUBY) | ||
/OMG/ === "OMG" | ||
^^^ Avoid the use of the case equality operator `===`. | ||
RUBY | ||
end | ||
end | ||
end |