class RuboCop::Cop::RSpec::MatchArray
it { is_expected.to match_array(%w(tremble in fear foolish mortals)) }
# good
it { is_expected.to match_array( + array) }
# good
it { is_expected.to contain_exactly(content1, content2) }
# good
it { is_expected.to match_array([content1, content2]) }
# bad
@example
- Prefer ‘eq` when using `match_array` with an empty array literal.
- Prefer `contain_exactly` when matching an array with values.
This cop checks for the following:
Checks where `match_array` is used.
def check_populated_array(node)
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
def on_send(node)
def on_send(node) return unless node.first_argument&.array_type? return if match_array_with_empty_array?(node) check_populated_array(node) end