Sha256: 80e5b13cac1ff9b8fe5cfbcb14ed2c40591e1a67b71e3211dfc64fd6858457a3
Contents?: true
Size: 1.35 KB
Versions: 6
Compression:
Stored size: 1.35 KB
Contents
# frozen_string_literal: true module RuboCop module Cop module Minitest # 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 } # class AssertOutput < Cop include MinitestExplorationHelpers MSG = 'Use `assert_output` instead of mutating %<name>s.' OUTPUT_GLOBAL_VARIABLES = %i[$stdout $stderr].freeze 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 def find_test_case(node) def_ancestor = node.each_ancestor(:def).first def_ancestor if test_case?(def_ancestor) end def references_gvar?(assertion, gvar_name) assertion.each_descendant(:gvar).any? { |d| d.children.first == gvar_name } end end end end end
Version data entries
6 entries across 6 versions & 1 rubygems