-
-
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/Rails/NegationBeValid
cop
Fix: #1660
- Loading branch information
Showing
8 changed files
with
215 additions
and
1 deletion.
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,92 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
module Rails | ||
# Enforces use of `be_invalid` or `not_to` for negated be_valid. | ||
# | ||
# @example EnforcedStyle: not_to (default) | ||
# # bad | ||
# expect(foo).to be_invalid | ||
# | ||
# # good | ||
# expect(foo).not_to be_valid | ||
# | ||
# @example EnforcedStyle: be_invalid | ||
# # bad | ||
# expect(foo).not_to be_valid | ||
# | ||
# # good | ||
# expect(foo).to be_invalid | ||
# | ||
class NegationBeValid < Base | ||
extend AutoCorrector | ||
include ConfigurableEnforcedStyle | ||
|
||
MSG = 'Use `expect(...).%<runner>s %<matcher>s`.' | ||
RESTRICT_ON_SEND = %i[be_valid be_invalid].freeze | ||
|
||
# @!method not_to?(node) | ||
def_node_matcher :not_to?, <<~PATTERN | ||
(send ... :not_to (send nil? :be_valid ...)) | ||
PATTERN | ||
|
||
# @!method be_invalid?(node) | ||
def_node_matcher :be_invalid?, <<~PATTERN | ||
(send ... :to (send nil? :be_invalid ...)) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless offense?(node.parent) | ||
|
||
add_offense(offense_range(node), | ||
message: message(node.method_name)) do |corrector| | ||
corrector.replace(node.parent.loc.selector, replaced_runner) | ||
corrector.replace(node.loc.selector, replaced_matcher) | ||
end | ||
end | ||
|
||
private | ||
|
||
def offense?(node) | ||
case style | ||
when :not_to | ||
be_invalid?(node) | ||
when :be_invalid | ||
not_to?(node) | ||
end | ||
end | ||
|
||
def offense_range(node) | ||
node.parent.loc.selector.with(end_pos: node.loc.selector.end_pos) | ||
end | ||
|
||
def message(_matcher) | ||
format(MSG, | ||
runner: replaced_runner, | ||
matcher: replaced_matcher) | ||
end | ||
|
||
def replaced_runner | ||
case style | ||
when :not_to | ||
'not_to' | ||
when :be_invalid | ||
'to' | ||
end | ||
end | ||
|
||
def replaced_matcher | ||
case style | ||
when :not_to | ||
'be_valid' | ||
when :be_invalid | ||
'be_invalid' | ||
end | ||
end | ||
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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::Rails::NegationBeValid do | ||
let(:cop_config) { { 'EnforcedStyle' => enforced_style } } | ||
|
||
context 'with EnforcedStyle `not_to`' do | ||
let(:enforced_style) { 'not_to' } | ||
|
||
it 'registers an offense when using ' \ | ||
'`expect(...).to be_invalid`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_invalid | ||
^^^^^^^^^^^^^ Use `expect(...).not_to be_valid`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using ' \ | ||
'`expect(...).not_to be_valid`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).not_to be_valid | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using ' \ | ||
'`expect(...).to be_valid`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be_valid | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with EnforcedStyle `be_invalid`' do | ||
let(:enforced_style) { 'be_invalid' } | ||
|
||
it 'registers an offense when using ' \ | ||
'`expect(...).not_to be_valid`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).not_to be_valid | ||
^^^^^^^^^^^^^^^ Use `expect(...).to be_invalid`. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using ' \ | ||
'`expect(...).to be_invalid`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be_invalid | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using ' \ | ||
'`expect(...).to be_valid`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be_valid | ||
RUBY | ||
end | ||
end | ||
end |