-
-
Notifications
You must be signed in to change notification settings - Fork 276
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/RedundantPredicateMatcher
cop
Fix: #1689
- Loading branch information
Showing
8 changed files
with
252 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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks for redundant predicate matcher. | ||
# | ||
# @example | ||
# # bad | ||
# expect(foo).to be_exist(bar) | ||
# expect(foo).not_to be_include(bar) | ||
# | ||
# # good | ||
# expect(foo).to exist(bar) | ||
# expect(foo).not_to include(bar) | ||
# | ||
class RedundantPredicateMatcher < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `%<good>s` instead of `%<bad>s`.' | ||
RESTRICT_ON_SEND = | ||
%i[be_all be_cover be_end_with be_eql be_equal | ||
be_exist be_exists be_include be_match | ||
be_respond_to be_start_with].freeze | ||
|
||
def on_send(node) | ||
return if node.parent.block_type? || !replacable_arguments?(node) | ||
|
||
method_name = node.method_name.to_s | ||
replaced = replaced_method_name(method_name) | ||
add_offense(node, message: message(method_name, | ||
replaced)) do |corrector| | ||
unless node.method?(:be_all) | ||
corrector.replace(node.loc.selector, replaced) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def message(bad_method, good_method) | ||
format(MSG, bad: bad_method, good: good_method) | ||
end | ||
|
||
def replacable_arguments?(node) | ||
if node.method?(:be_all) | ||
node.first_argument.send_type? | ||
else | ||
true | ||
end | ||
end | ||
|
||
def replaced_method_name(method_name) | ||
name = method_name.to_s.delete_prefix('be_') | ||
if name == 'exists' | ||
'exist' | ||
else | ||
name | ||
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
145 changes: 145 additions & 0 deletions
145
spec/rubocop/cop/rspec/redundant_predicate_matcher_spec.rb
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,145 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::RedundantPredicateMatcher do | ||
it 'registers an offense when using `be_all`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_all be_odd | ||
^^^^^^^^^^^^^ Use `all` instead of `be_all`. | ||
expect(foo).to be_all(expected_foo_element_value) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `all` instead of `be_all`. | ||
RUBY | ||
|
||
expect_no_corrections | ||
end | ||
|
||
it 'does not register an offense when using `be_all` with block' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be_all { |bar| bar == baz } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `be_all` without send' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to be_all(false) | ||
expect(foo).to be_all(1) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_cover`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_cover(bar, baz) | ||
^^^^^^^^^^^^^^^^^^ Use `cover` instead of `be_cover`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to cover(bar, baz) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_end_with`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_end_with('?') | ||
^^^^^^^^^^^^^^^^ Use `end_with` instead of `be_end_with`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to end_with('?') | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_eql`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_eql(bar) | ||
^^^^^^^^^^^ Use `eql` instead of `be_eql`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to eql(bar) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_equal`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_equal(bar) | ||
^^^^^^^^^^^^^ Use `equal` instead of `be_equal`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to equal(bar) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_exist`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_exist("bar.txt") | ||
^^^^^^^^^^^^^^^^^^^ Use `exist` instead of `be_exist`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to exist("bar.txt") | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_exists`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_exists("bar.txt") | ||
^^^^^^^^^^^^^^^^^^^^ Use `exist` instead of `be_exists`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to exist("bar.txt") | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_include`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_include(bar, baz) | ||
^^^^^^^^^^^^^^^^^^^^ Use `include` instead of `be_include`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to include(bar, baz) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_match`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_match(/bar/) | ||
^^^^^^^^^^^^^^^ Use `match` instead of `be_match`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to match(/bar/) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_respond_to`' do | ||
expect_offense(<<~RUBY) | ||
expect("string").to be_respond_to(:length) | ||
^^^^^^^^^^^^^^^^^^^^^^ Use `respond_to` instead of `be_respond_to`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect("string").to respond_to(:length) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `be_start_with`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo).to be_start_with("A") | ||
^^^^^^^^^^^^^^^^^^ Use `start_with` instead of `be_start_with`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(foo).to start_with("A") | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using built-in matcher' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo).to all(bar) | ||
expect(foo).to cover(bar) | ||
expect(foo).to end_with(bar) | ||
RUBY | ||
end | ||
end |