Class: RuboCop::Cop::RSpec::EmptyOutput

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

Overview

Check that the ‘output` matcher is not called with an empty string.

Examples:

# bad
expect { foo }.to output('').to_stdout
expect { bar }.not_to output('').to_stderr

# good
expect { foo }.not_to output.to_stdout
expect { bar }.to output.to_stderr

Constant Summary collapse

MSG =
'Use `%<runner>s` instead of matching on an empty output.'
RESTRICT_ON_SEND =
Runners.all

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

#matching_empty_output(node) ⇒ Object



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

def_node_matcher :matching_empty_output, <<~PATTERN
  (send
    (block
      (send nil? :expect) ...
    )
    #Runners.all
    (send $(send nil? :output (str empty?)) ...)
  )
PATTERN

#on_send(send_node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/rspec/empty_output.rb', line 34

def on_send(send_node)
  matching_empty_output(send_node) do |node|
    runner = send_node.method?(:to) ? 'not_to' : 'to'
    message = format(MSG, runner: runner)
    add_offense(node, message: message) do |corrector|
      corrector.replace(send_node.loc.selector, runner)
      corrector.replace(node, 'output')
    end
  end
end