Class: RuboCop::Cop::RSpec::BeEmpty

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

Overview

Prefer using ‘be_empty` when checking for an empty array.

Examples:

# bad
expect(array).to contain_exactly
expect(array).to match_array([])

# good
expect(array).to be_empty

Constant Summary collapse

MSG =
'Use `be_empty` matchers for checking an empty array.'
RESTRICT_ON_SEND =
%i[contain_exactly match_array].freeze

Instance Method Summary collapse

Methods inherited from Base

inherited, #on_new_investigation

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

#expect_array_matcher?(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubocop/cop/rspec/be_empty.rb', line 23

def_node_matcher :expect_array_matcher?, <<~PATTERN
  (send
    (send nil? :expect _)
    #Runners.all
    ${
      (send nil? :match_array (array))
      (send nil? :contain_exactly)
    }
    _?
  )
PATTERN

#on_send(node) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rubocop/cop/rspec/be_empty.rb', line 35

def on_send(node)
  expect_array_matcher?(node.parent) do |expect|
    add_offense(expect) do |corrector|
      corrector.replace(expect, 'be_empty')
    end
  end
end