-
-
Notifications
You must be signed in to change notification settings - Fork 280
Add new RSpec/IdenticalEqualityAssertion
cop
#1132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Checks for equality assertions with identical expressions on both sides. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# expect(foo.bar).to eq(foo.bar) | ||
# expect(foo.bar).to eql(foo.bar) | ||
# | ||
# # good | ||
# expect(foo.bar).to eq(2) | ||
# expect(foo.bar).to eql(2) | ||
# | ||
class IdenticalEqualityAssertion < Base | ||
MSG = 'Identical expressions on both sides of the equality ' \ | ||
'may indicate a flawed test.' | ||
RESTRICT_ON_SEND = %i[to].freeze | ||
|
||
# @!method equality_check?(node) | ||
def_node_matcher :equality_check?, <<~PATTERN | ||
(send (send nil? :expect $_) :to | ||
{(send nil? {:eql :eq :be} $_) | ||
(send (send nil? :be) :== $_)}) | ||
PATTERN | ||
|
||
def on_send(node) | ||
equality_check?(node) do |left, right| | ||
add_offense(node) if left == right | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or 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
57 changes: 57 additions & 0 deletions
57
spec/rubocop/cop/rspec/identical_equality_assertion_spec.rb
This file contains hidden or 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::IdenticalEqualityAssertion do | ||
it 'registers an offense when using identical expressions with `eq`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo.bar).to eq(foo.bar) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Identical expressions on both sides of the equality may indicate a flawed test. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using identical expressions with `eql`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo.bar.baz).to eql(foo.bar.baz) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Identical expressions on both sides of the equality may indicate a flawed test. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for trivial constants' do | ||
expect_offense(<<~RUBY) | ||
expect(42).to eq(42) | ||
^^^^^^^^^^^^^^^^^^^^ Identical expressions on both sides of the equality may indicate a flawed test. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for complex constants' do | ||
expect_offense(<<~RUBY) | ||
expect({a: 1, b: :b}).to eql({a: 1, b: :b}) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Identical expressions on both sides of the equality may indicate a flawed test. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for identical expression with be' do | ||
expect_offense(<<~RUBY) | ||
expect(foo.bar).to be(foo.bar) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Identical expressions on both sides of the equality may indicate a flawed test. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for identical expression with be ==' do | ||
expect_offense(<<~RUBY) | ||
expect(foo.bar).to be == foo.bar | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Identical expressions on both sides of the equality may indicate a flawed test. | ||
RUBY | ||
end | ||
|
||
it 'does not register offense for different expressions' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(foo.bar).to eq(bar.foo) | ||
RUBY | ||
end | ||
|
||
it 'checks for whole expression' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(Foo.new(1).foo).to eql(Foo.new(2).bar) | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.