class RuboCop::Cop::RSpec::ExpectActual
Checks for ‘expect(…)` calls containing literal values.
Autocorrection is performed when the expected is not a literal.
@example
# 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") # bad (not supported autocorrection) expect(false).to eq(true)
Constants
- COMPLEX_LITERALS
- CORRECTABLE_MATCHERS
- MSG
- RESTRICT_ON_SEND
- SIMPLE_LITERALS
- SKIPPED_MATCHERS
Public Instance Methods
Source
# File lib/rubocop/cop/rspec/expect_actual.rb, line 68 def on_send(node) expect_literal(node) do |actual, send_node, matcher, expected| next if SKIPPED_MATCHERS.include?(matcher) add_offense(actual) do |corrector| next unless CORRECTABLE_MATCHERS.include?(matcher) next if literal?(expected) corrector.replace(actual, expected.source) if matcher == :be corrector.replace(expected, actual.source) else corrector.replace(send_node, "#{matcher}(#{actual.source})") end end end end
Private Instance Methods
Source
# File lib/rubocop/cop/rspec/expect_actual.rb, line 98 def complex_literal?(node) COMPLEX_LITERALS.include?(node.type) && node.each_child_node.all? { |child_node| literal?(child_node) } end
Source
# File lib/rubocop/cop/rspec/expect_actual.rb, line 90 def literal?(node) node && (simple_literal?(node) || complex_literal?(node)) end
This is not implemented using a NodePattern because it seems to not be able to match against an explicit (nil) sexp
Source
# File lib/rubocop/cop/rspec/expect_actual.rb, line 94 def simple_literal?(node) SIMPLE_LITERALS.include?(node.type) end