Class: RuboCop::Cop::RSpec::ExpectActual

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/expect_actual.rb

Overview

Checks for `expect(…)` calls containing literal values.

Examples:

# bad
expect(5).to eq(price)
expect(/foo/).to eq(pattern)
expect("John").to eq(name)

# good
expect(price).to eq(5)
expect(pattern).to eq(/foo/)
expect(name).to eq("John")

Constant Summary collapse

MSG =
'Provide the actual you are testing to `expect(...)`.'
SIMPLE_LITERALS =
%i[
  true
  false
  nil
  int
  float
  str
  sym
  complex
  rational
  regopt
].freeze
COMPLEX_LITERALS =
%i[
  array
  hash
  pair
  irange
  erange
  regexp
].freeze
SUPPORTED_MATCHERS =
%i[eq eql equal be].freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_pattern, #send_pattern

Instance Method Details

#on_send(node) ⇒ Object


59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/cop/rspec/expect_actual.rb', line 59

def on_send(node)
  expect_literal(node) do |actual, matcher, expected|
    add_offense(actual.source_range) do |corrector|
      next unless SUPPORTED_MATCHERS.include?(matcher)

      swap(corrector, actual, expected)
    end
  end
end