Class: RuboCop::Cop::RSpec::MatchArray

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/rspec/match_array.rb

Overview

Checks where ‘match_array` is used.

This cop checks for the following:

  • Prefer ‘contain_exactly` when matching an array with values.

  • Prefer ‘eq` when using `match_array` with an empty array literal.

Examples:

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

Constant Summary collapse

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

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

Methods included from RSpec::Language::NodePattern

#block_or_numblock_pattern, #block_pattern, #numblock_pattern, #send_pattern

Methods included from RSpec::Language

#example?, #example_group?, #example_group_with_body?, #explicit_rspec?, #hook?, #include?, #let?, #rspec?, #shared_group?, #spec_group?, #subject?

Instance Method Details

#match_array_with_empty_array?(node) ⇒ Object



32
33
34
# File 'lib/rubocop/cop/rspec/match_array.rb', line 32

def_node_matcher :match_array_with_empty_array?, <<~PATTERN
  (send nil? :match_array (array))
PATTERN

#on_send(node) ⇒ Object



36
37
38
39
40
41
# 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