Sha256: 47af60c665bdac2f600effd37f73a8fe984591e1ef437f403f89e3df0482598b

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

module RuboCop
  module Cop
    module Ezcater
      # Enforce use of `match_ordered_array` matcher instead of using `eq` matcher
      #
      # @example
      #
      #   # good
      #   expect(foo).to match_ordered_array([1, 2, 3])
      #   expect(foo).to match_ordered_array [1, 2, 3]
      #
      #   # bad
      #   expect(foo).to eq([1, 2, 3])
      #   expect(foo).to eq [1, 2, 3]
      class RspecMatchOrderedArray < Cop
        MATCH_ORDERED_ARRAY = "match_ordered_array".freeze
        MSG = "Use the `match_ordered_array` matcher from ezcater_matchers gem "\
          "instead of `eq` when comparing collections".freeze

        def_node_matcher "eq_array", <<~PATTERN
          (send nil? :eq (array ...))
        PATTERN

        def on_send(node)
          eq_array(node) do
            add_offense(node, location: :expression, message: MSG)
          end
        end

        def autocorrect(node)
          lambda do |corrector|
            corrector.replace(
              Parser::Source::Range.new(
                node.source_range.source_buffer,
                node.source_range.begin_pos,
                node.source_range.begin_pos + 2
              ),
              MATCH_ORDERED_ARRAY
            )
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ezcater_rubocop-0.52.6 lib/rubocop/cop/ezcater/rspec_match_ordered_array.rb
ezcater_rubocop-0.52.5 lib/rubocop/cop/ezcater/rspec_match_ordered_array.rb