-
-
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/IdenticalEqualityAssertion
cop
Closes #1130
- Loading branch information
1 parent
956f3b5
commit 3db1f43
Showing
7 changed files
with
142 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,39 @@ | ||
# 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 | ||
|
||
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| | ||
return unless left == right | ||
|
||
add_offense(node) | ||
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
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::IdenticalEqualityAssertion do | ||
let(:msg) do | ||
'Identical expressions on both sides of the equality '\ | ||
'may indicate a flawed test.' | ||
end | ||
|
||
it 'registers an offense when using identical expressions with `eq`' do | ||
expect_offense(<<~RUBY) | ||
expect(foo.bar).to eq(foo.bar) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} | ||
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) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for trivial constants' do | ||
expect_offense(<<~RUBY) | ||
expect(42).to eq(42) | ||
^^^^^^^^^^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for complex constants' do | ||
expect_offense(<<~RUBY) | ||
expect({a: 1, b: :b}).to eql({a: 1, b: :b}) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for identical expression with be' do | ||
expect_offense(<<~RUBY) | ||
expect(foo.bar).to be(foo.bar) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} | ||
RUBY | ||
end | ||
|
||
it 'registers an offense for identical expression with be ==' do | ||
expect_offense(<<~RUBY) | ||
expect(foo.bar).to be == foo.bar | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{msg} | ||
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 |