Sha256: e09821260aa1d5400b15600fd4bdc3092f7c72303620b4e73577c787da532ef3

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module RSpec
      # Prefer `contain_exactly` when matching an array literal.
      #
      # @example
      #   # bad
      #   it { is_expected.to match_array([content1, content2]) }
      #
      #   # good
      #   it { is_expected.to contain_exactly(content1, content2) }
      #
      #   # good
      #   it { is_expected.to match_array([content] + array) }
      #
      #   # good
      #   it { is_expected.to match_array(%w(tremble in fear foolish mortals)) }
      class MatchArray < Base
        extend AutoCorrector

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

        def on_send(node)
          return unless node.first_argument.array_type?
          return if node.first_argument.percent_literal?

          add_offense(node) do |corrector|
            array_contents = node.arguments.flat_map(&:to_a)
            corrector.replace(
              node.source_range,
              "contain_exactly(#{array_contents.map(&:source).join(', ')})"
            )
          end
        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/match_array.rb