Class: RuboCop::Cop::Minitest::AssertOutput

Inherits:
Base
  • Object
show all
Includes:
RuboCop::Cop::MinitestExplorationHelpers
Defined in:
lib/rubocop/cop/minitest/assert_output.rb

Overview

Checks for opportunities to use ‘assert_output`.

Examples:

# bad
$stdout = StringIO.new
puts object.method
$stdout.rewind
assert_match expected, $stdout.read

# good
assert_output(expected) { puts object.method }

Constant Summary collapse

MSG =
'Use `assert_output` instead of mutating %<name>s.'
OUTPUT_GLOBAL_VARIABLES =
%i[$stdout $stderr].freeze

Constants included from RuboCop::Cop::MinitestExplorationHelpers

RuboCop::Cop::MinitestExplorationHelpers::ASSERTION_PREFIXES, RuboCop::Cop::MinitestExplorationHelpers::LIFECYCLE_HOOK_METHODS, RuboCop::Cop::MinitestExplorationHelpers::LIFECYCLE_HOOK_METHODS_IN_ORDER

Instance Method Summary collapse

Instance Method Details

#on_gvasgn(node) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/minitest/assert_output.rb', line 24

def on_gvasgn(node)
  test_case_node = find_test_case(node)
  return unless test_case_node

  gvar_name = node.children.first
  return unless OUTPUT_GLOBAL_VARIABLES.include?(gvar_name)

  assertions(test_case_node).each do |assertion|
    add_offense(assertion, message: format(MSG, name: gvar_name)) if references_gvar?(assertion, gvar_name)
  end
end