-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
RSpec/NegatedExpectation
cop
This cop checks for expectations with `!`. The autocorrection is marked as unsafe, because it may change the expectation from a positive to a negative one, or vice versa. ```ruby !expect(foo).to be_valid expect(foo).not_to be_valid ```
- Loading branch information
Showing
8 changed files
with
140 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks for expectations with `!`. | ||
# | ||
# @safety | ||
# The autocorrection is marked as unsafe, because it may change the | ||
# expectation from a positive to a negative one, or vice versa. | ||
# | ||
# @example | ||
# # bad | ||
# !expect(foo).to be_valid | ||
# | ||
# # good | ||
# expect(foo).not_to be_valid | ||
# | ||
class NegatedExpectation < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `expect(...).%<replaced>s` instead of ' \ | ||
'`!expect(...)`.%<runner>s' | ||
RESTRICT_ON_SEND = Runners.all | ||
|
||
def on_send(node) | ||
return unless node.parent&.send_type? | ||
return unless node.parent.method?(:!) | ||
|
||
replaced = replaced(node) | ||
add_offense(node.parent, | ||
message: message(node, replaced)) do |corrector| | ||
corrector.remove(node.parent.loc.selector) | ||
corrector.replace(node.loc.selector, replaced) | ||
end | ||
end | ||
|
||
private | ||
|
||
def message(node, replaced) | ||
format(MSG, runner: node.loc.selector.source, replaced: replaced) | ||
end | ||
|
||
def replaced(node) | ||
runner = node.loc.selector.source | ||
runner == 'to' ? 'not_to' : 'to' | ||
end | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::NegatedExpectation, :config do | ||
it 'registers an offense when using redundant negation with `.to`' do | ||
expect_offense(<<~RUBY) | ||
!expect(foo).to be_valid | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(...).not_to` instead of `!expect(...)`.to | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).not_to be_valid | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using redundant negation with `.not_to`' do | ||
expect_offense(<<~RUBY) | ||
!expect(foo).not_to be_valid | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(...).to` instead of `!expect(...)`.not_to | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to be_valid | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using redundant negation with `.to_not`' do | ||
expect_offense(<<~RUBY) | ||
!expect(foo).to_not be_valid | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(...).to` instead of `!expect(...)`.to_not | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to be_valid | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when without redundant negation' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).not_to be_valid | ||
RUBY | ||
end | ||
end |