-
-
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.
Merge pull request #1132 from tejasbubane/fix-1130
Add new `RSpec/IdenticalEqualityAssertion` cop
- Loading branch information
Showing
7 changed files
with
135 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
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 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 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 |