Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support autocorrect for
Style/CaseEquality
cop
- Loading branch information
Showing
5 changed files
with
84 additions
and
28 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,43 +1,75 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::CaseEquality do | ||
subject(:cop) { described_class.new(config) } | ||
RSpec.describe RuboCop::Cop::Style::CaseEquality, :config do | ||
shared_examples 'offenses' do | ||
it 'does not fail when the receiver is implicit' do | ||
expect_no_offenses(<<~RUBY) | ||
puts "No offense" | ||
RUBY | ||
end | ||
|
||
let(:config) { RuboCop::Config.new } | ||
it 'registers an offense and corrects for === when the receiver is a regexp' do | ||
expect_offense(<<~RUBY) | ||
/OMG/ === var | ||
^^^ Avoid the use of the case equality operator `===`. | ||
RUBY | ||
|
||
it 'registers an offense for ===' do | ||
expect_offense(<<~RUBY) | ||
Array === var | ||
expect_correction(<<~RUBY) | ||
var =~ /OMG/ | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects for === when the receiver is a range' do | ||
expect_offense(<<~RUBY) | ||
(1..10) === var | ||
^^^ Avoid the use of the case equality operator `===`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
(1..10).include?(var) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and does not correct for === when receiver is of some other type' do | ||
expect_offense(<<~RUBY) | ||
foo === var | ||
^^^ Avoid the use of the case equality operator `===`. | ||
RUBY | ||
RUBY | ||
|
||
expect_no_corrections | ||
end | ||
end | ||
|
||
context 'when constant checks are allowed' do | ||
let(:config) do | ||
RuboCop::Config.new( | ||
'Style/CaseEquality' => { | ||
'AllowOnConstant' => true | ||
} | ||
) | ||
context 'when AllowOnConstant is false' do | ||
let(:cop_config) do | ||
{ 'AllowOnConstant' => false } | ||
end | ||
|
||
it 'does not fail when the receiver is implicit' do | ||
expect_no_offenses(<<~RUBY) | ||
puts "No offense" | ||
it 'registers an offense and corrects for === when the receiver is a constant' do | ||
expect_offense(<<~RUBY) | ||
Array === var | ||
^^^ Avoid the use of the case equality operator `===`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
var.is_a?(Array) | ||
RUBY | ||
end | ||
|
||
include_examples 'offenses' | ||
end | ||
|
||
context 'when AllowOnConstant is true' do | ||
let(:cop_config) do | ||
{ '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 | ||
include_examples 'offenses' | ||
end | ||
end |