forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes rubocop#150. This PR adds `EnforcedStyle: refute` for `Rails/RefuteMethods`. `EnforcedStyle: refute` is an alternative style to `EnforcedStyle: assert_not`. ```ruby # @example EnforcedStyle: refute # # bad # assert_not false # assert_not_empty [1, 2, 3] # assert_not_equal true, false # # # good # refute false # refute_empty [1, 2, 3] # refute_equal true, false ``` The default style keeps `EnforcedStyle: assert_not` because following the Rails coding conventions conventions. > Use `assert_not` methods instead of refute. https://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions
- Loading branch information
Showing
5 changed files
with
183 additions
and
67 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,53 +1,122 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::RefuteMethods do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and correct using `refute` with a single argument' do | ||
expect_offense(<<~RUBY) | ||
refute foo | ||
^^^^^^ Prefer `assert_not` over `refute`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_not foo | ||
RUBY | ||
end | ||
RSpec.describe RuboCop::Cop::Rails::RefuteMethods, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
it 'registers an offense and corrects using `refute` ' \ | ||
'with multiple arguments' do | ||
expect_offense(<<~RUBY) | ||
refute foo, bar, baz | ||
^^^^^^ Prefer `assert_not` over `refute`. | ||
RUBY | ||
let(:config) do | ||
RuboCop::Config.new('Rails/RefuteMethods' => cop_config) | ||
end | ||
|
||
expect_correction(<<~RUBY) | ||
assert_not foo, bar, baz | ||
RUBY | ||
let(:cop_config) do | ||
{ | ||
'EnforcedStyle' => enforced_style, | ||
'SupportedStyles' => %w[assert_not refute] | ||
} | ||
end | ||
|
||
it 'registers an offense when using `refute_empty`' do | ||
expect_offense(<<~RUBY) | ||
refute_empty foo | ||
^^^^^^^^^^^^ Prefer `assert_not_empty` over `refute_empty`. | ||
RUBY | ||
context 'when EnforcedStyle is `assert_not`' do | ||
let(:enforced_style) { 'assert_not' } | ||
|
||
expect_correction(<<~RUBY) | ||
assert_not_empty foo | ||
RUBY | ||
end | ||
it 'registers an offense and correct using `refute` ' \ | ||
'with a single argument' do | ||
expect_offense(<<~RUBY) | ||
refute foo | ||
^^^^^^ Prefer `assert_not` over `refute`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_not foo | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects using `refute` ' \ | ||
'with multiple arguments' do | ||
expect_offense(<<~RUBY) | ||
refute foo, bar, baz | ||
^^^^^^ Prefer `assert_not` over `refute`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_not foo, bar, baz | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `refute_empty`' do | ||
expect_offense(<<~RUBY) | ||
refute_empty foo | ||
^^^^^^^^^^^^ Prefer `assert_not_empty` over `refute_empty`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
assert_not_empty foo | ||
RUBY | ||
end | ||
|
||
it 'does not registers an offense when using `assert_not` ' \ | ||
'with a single argument' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_not foo | ||
RUBY | ||
end | ||
|
||
it 'does not registers an offense when using `assert_not` ' \ | ||
'with a single argument' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_not foo | ||
RUBY | ||
it 'does not registers an offense when using `assert_not` ' \ | ||
'with a multiple arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_not foo, bar, baz | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not registers an offense when using `assert_not` ' \ | ||
'with a multiple arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
assert_not foo, bar, baz | ||
RUBY | ||
context 'when EnforcedStyle is `refute`' do | ||
let(:enforced_style) { 'refute' } | ||
|
||
it 'registers an offense and correct using `assert_not` ' \ | ||
'with a single argument' do | ||
expect_offense(<<~RUBY) | ||
assert_not foo | ||
^^^^^^^^^^ Prefer `refute` over `assert_not`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
refute foo | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects using `assert_not` ' \ | ||
'with multiple arguments' do | ||
expect_offense(<<~RUBY) | ||
assert_not foo, bar, baz | ||
^^^^^^^^^^ Prefer `refute` over `assert_not`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
refute foo, bar, baz | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `assert_not_empty`' do | ||
expect_offense(<<~RUBY) | ||
assert_not_empty foo | ||
^^^^^^^^^^^^^^^^ Prefer `refute_empty` over `assert_not_empty`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
refute_empty foo | ||
RUBY | ||
end | ||
|
||
it 'does not registers an offense when using `refute` ' \ | ||
'with a single argument' do | ||
expect_no_offenses(<<~RUBY) | ||
refute foo | ||
RUBY | ||
end | ||
|
||
it 'does not registers an offense when using `refute` ' \ | ||
'with a multiple arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
refute foo, bar, baz | ||
RUBY | ||
end | ||
end | ||
end |