class RuboCop::Cop::RSpec::ContainExactly

Checks where ‘contain_exactly` is used.

This cop checks for the following:

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

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rspec/contain_exactly.rb, line 28
def on_send(node)
  return if node.arguments.empty?

  check_populated_collection(node)
end

Private Instance Methods

autocorrect_for_populated_array(node, corrector) click to toggle source
# File lib/rubocop/cop/rspec/contain_exactly.rb, line 44
def autocorrect_for_populated_array(node, corrector)
  arrays = node.arguments.map do |splat_node|
    splat_node.children.first
  end
  corrector.replace(
    node,
    "match_array(#{arrays.map(&:source).join(' + ')})"
  )
end
check_populated_collection(node) click to toggle source
# File lib/rubocop/cop/rspec/contain_exactly.rb, line 36
def check_populated_collection(node)
  return unless node.each_child_node.all?(&:splat_type?)

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