Sha256: 9dc40394d44fd3ddc0b4f37a3a15afe5a81d7fcbadaa46a7ce3c4f417ccfa5db

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Prefer `match_array` when matching array values.
      #
      # @example
      #   # bad
      #   it { is_expected.to contain_exactly(*array1, *array2) }
      #
      #   # good
      #   it { is_expected.to match_array(array1 + array2) }
      #
      #   # good
      #   it { is_expected.to contain_exactly(content, *array) }
      class ContainExactly < Base
        extend AutoCorrector

        MSG = 'Prefer `match_array` when matching array values.'
        RESTRICT_ON_SEND = %i[contain_exactly].freeze

        def on_send(node)
          return unless node.each_child_node.all?(&:splat_type?)

          add_offense(node) do |corrector|
            autocorrect(node, corrector)
          end
        end

        private

        def autocorrect(node, corrector)
          arrays = node.arguments.map do |splat_node|
            splat_node.children.first
          end
          corrector.replace(
            node.source_range,
            "match_array(#{arrays.map(&:source).join(' + ')})"
          )
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-rspec-2.19.0 lib/rubocop/cop/rspec/contain_exactly.rb