Skip to content

Commit

Permalink
Add new RSpec/IdenticalEqualityAssertion cop
Browse files Browse the repository at this point in the history
Closes #1130
  • Loading branch information
tejasbubane committed Feb 21, 2021
1 parent 956f3b5 commit 3db1f43
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Master (Unreleased)

* Add new `RSpec/IdenticalEqualityAssertion` cop. ([@tejasbubane][])

## 2.2.0 (2021-02-02)

* Fix `HooksBeforeExamples`, `LeadingSubject`, `LetBeforeExamples` and `ScatteredLet` autocorrection to take into account inline comments and comments immediately before the moved node. ([@Darhazer][])
Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,12 @@ RSpec/HooksBeforeExamples:
VersionAdded: '1.29'
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/HooksBeforeExamples

RSpec/IdenticalEqualityAssertion:
Description: Checks for equality assertions with identical expressions on both sides.
Enabled: pending
VersionAdded: '2.3'
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IdenticalEqualityAssertion

RSpec/ImplicitBlockExpectation:
Description: Check that implicit block expectation syntax is not used.
Enabled: true
Expand Down
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/cops.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
* xref:cops_rspec.adoc#rspecfocus[RSpec/Focus]
* xref:cops_rspec.adoc#rspechookargument[RSpec/HookArgument]
* xref:cops_rspec.adoc#rspechooksbeforeexamples[RSpec/HooksBeforeExamples]
* xref:cops_rspec.adoc#rspecidenticalequalityassertion[RSpec/IdenticalEqualityAssertion]
* xref:cops_rspec.adoc#rspecimplicitblockexpectation[RSpec/ImplicitBlockExpectation]
* xref:cops_rspec.adoc#rspecimplicitexpect[RSpec/ImplicitExpect]
* xref:cops_rspec.adoc#rspecimplicitsubject[RSpec/ImplicitSubject]
Expand Down
31 changes: 31 additions & 0 deletions docs/modules/ROOT/pages/cops_rspec.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,37 @@ end

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/HooksBeforeExamples

== RSpec/IdenticalEqualityAssertion

|===
| Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged

| Pending
| Yes
| No
| 2.3
| -
|===

Checks for equality assertions with identical expressions on both sides.

=== Examples

[source,ruby]
----
# 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)
----

=== References

* https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/IdenticalEqualityAssertion

== RSpec/ImplicitBlockExpectation

|===
Expand Down
39 changes: 39 additions & 0 deletions lib/rubocop/cop/rspec/identical_equality_assertion.rb
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
1 change: 1 addition & 0 deletions lib/rubocop/cop/rspec_cops.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
require_relative 'rspec/focus'
require_relative 'rspec/hook_argument'
require_relative 'rspec/hooks_before_examples'
require_relative 'rspec/identical_equality_assertion'
require_relative 'rspec/implicit_block_expectation'
require_relative 'rspec/implicit_expect'
require_relative 'rspec/implicit_subject'
Expand Down
62 changes: 62 additions & 0 deletions spec/rubocop/cop/rspec/identical_equality_assertion_spec.rb
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

0 comments on commit 3db1f43

Please sign in to comment.