class RuboCop::Cop::Minitest::AssertOutput
This cop checks for opportunities to use `assert_output`.
@example
# bad $stdout = StringIO.new puts object.method $stdout.rewind assert_match expected, $stdout.read # good assert_output(expected) { puts object.method }
Constants
- MSG
- OUTPUT_GLOBAL_VARIABLES
Public Instance Methods
on_gvasgn(node)
click to toggle source
# 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
Private Instance Methods
find_test_case(node)
click to toggle source
# File lib/rubocop/cop/minitest/assert_output.rb, line 38 def find_test_case(node) def_ancestor = node.each_ancestor(:def).first def_ancestor if test_case?(def_ancestor) end
references_gvar?(assertion, gvar_name)
click to toggle source
# File lib/rubocop/cop/minitest/assert_output.rb, line 43 def references_gvar?(assertion, gvar_name) assertion.each_descendant(:gvar).any? { |d| d.children.first == gvar_name } end