class RuboCop::Cop::RSpec::MatchArray

Checks where ‘match_array` is used.

This cop checks for the following:

@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)) }

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/match_array.rb, line 36
def on_send(node)
  return unless node.first_argument&.array_type?
  return if match_array_with_empty_array?(node)

  check_populated_array(node)
end

Private Instance Methods

check_populated_array(node) click to toggle source
# File lib/rubocop/cop/rspec/match_array.rb, line 45
def check_populated_array(node)
  return if node.first_argument.percent_literal?

  add_offense(node) do |corrector|
    array_contents = node.arguments.flat_map(&:to_a)
    corrector.replace(
      node,
      "contain_exactly(#{array_contents.map(&:source).join(', ')})"
    )
  end
end