Sha256: f6c0d20d6a4de6fc68ccc09ac1f4aa775d225f30610e0aca24c24da6874791f0

Contents?: true

Size: 1.62 KB

Versions: 20

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Checks for `expect(...)` calls containing literal values.
      #
      # @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")
      #
      class ExpectActual < Cop
        MSG = 'Provide the actual you are testing to `expect(...)`.'.freeze

        SIMPLE_LITERALS = %i[
          true
          false
          nil
          int
          float
          str
          sym
          complex
          rational
          regopt
        ].freeze

        COMPLEX_LITERALS = %i[
          array
          hash
          pair
          irange
          erange
          regexp
        ].freeze

        def_node_matcher :expect_literal, '(send _ :expect $#literal?)'

        def on_send(node)
          expect_literal(node) do |argument|
            add_offense(argument, location: :expression)
          end
        end

        private

        # This is not implement using a NodePattern because it seems
        # to not be able to match against an explicit (nil) sexp
        def literal?(node)
          node && (simple_literal?(node) || complex_literal?(node))
        end

        def simple_literal?(node)
          SIMPLE_LITERALS.include?(node.type)
        end

        def complex_literal?(node)
          COMPLEX_LITERALS.include?(node.type) &&
            node.each_child_node.all?(&method(:literal?))
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
rubocop-rspec-1.32.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.31.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.30.1 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.30.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.29.1 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.29.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.28.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.27.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.26.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.25.1 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.25.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.24.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.23.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.22.2 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.22.1 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.22.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.21.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.20.1 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.20.0 lib/rubocop/cop/rspec/expect_actual.rb
rubocop-rspec-1.19.0 lib/rubocop/cop/rspec/expect_actual.rb