class RuboCop::Cop::RSpec::ExpectActual
expect(false).to eq(true)
# bad (not supported autocorrection)
expect(name).to eq(“John”)
expect(pattern).to eq(/foo/)
expect(price).to eq(5)
# good
expect(“John”).to eq(name)
expect(/foo/).to eq(pattern)
expect(5).to eq(price)
# bad
@example
Autocorrection is performed when the expected is not a literal.
Checks for ‘expect(…)` calls containing literal values.
def complex_literal?(node)
def complex_literal?(node) COMPLEX_LITERALS.include?(node.type) && node.each_child_node.all? { |child_node| literal?(child_node) } end
def literal?(node)
This is not implemented using a NodePattern because it seems
def literal?(node) node && (simple_literal?(node) || complex_literal?(node)) end
def on_send(node)
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
def simple_literal?(node)
def simple_literal?(node) SIMPLE_LITERALS.include?(node.type) end