Class: RuboCop::Cop::RSpec::IdenticalEqualityAssertion

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/rspec/identical_equality_assertion.rb

Overview

Checks for equality assertions with identical expressions on both sides.

Examples:

# 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)

Constant Summary collapse

MSG =
'Identical expressions on both sides of the equality ' \
'may indicate a flawed test.'
RESTRICT_ON_SEND =
%i[to].freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#equality_check?(node) ⇒ Object



23
24
25
26
27
# File 'lib/rubocop/cop/rspec/identical_equality_assertion.rb', line 23

def_node_matcher :equality_check?, <<~PATTERN
  (send (send nil? :expect $_) :to
    {(send nil? {:eql :eq :be} $_)
     (send (send nil? :be) :== $_)})
PATTERN

#on_send(node) ⇒ Object



29
30
31
32
33
# File 'lib/rubocop/cop/rspec/identical_equality_assertion.rb', line 29

def on_send(node)
  equality_check?(node) do |left, right|
    add_offense(node) if left == right
  end
end