lib/rubocop/cop/mixin/minitest_exploration_helpers.rb in rubocop-minitest-0.26.1 vs lib/rubocop/cop/mixin/minitest_exploration_helpers.rb in rubocop-minitest-0.27.0

- old
+ new

@@ -5,10 +5,11 @@ module RuboCop module Cop # Helper methods for different explorations against test files and test cases. # @api private module MinitestExplorationHelpers + include DefNode extend NodePattern::Macros ASSERTION_PREFIXES = %w[assert refute].freeze LIFECYCLE_HOOK_METHODS = %i[ @@ -25,26 +26,34 @@ def test_class?(class_node) class_node.parent_class && class_node.identifier.source.end_with?('Test') end def test_case?(node) - return false unless node&.def_type? && test_case_name?(node.method_name) + return false unless node&.def_type? && test_method?(node) class_ancestor = node.each_ancestor(:class).first test_class?(class_ancestor) end - def test_cases(class_node) - test_cases = class_def_nodes(class_node).select { |def_node| test_case_name?(def_node.method_name) } + def test_cases(class_node, visibility_check: true) + test_cases = class_def_nodes(class_node).select do |def_node| + test_method?(def_node, visibility_check: visibility_check) + end # Support Active Support's `test 'example' { ... }` method. # https://api.rubyonrails.org/classes/ActiveSupport/Testing/Declarative.html test_blocks = class_node.each_descendant(:block).select { |block| block.method?(:test) || block.method?(:it) } test_cases + test_blocks end + def test_method?(def_node, visibility_check: true) + return false if visibility_check && non_public?(def_node) + + test_case_name?(def_node.method_name) && !def_node.arguments? + end + def lifecycle_hooks(class_node) class_def_nodes(class_node) .select { |def_node| lifecycle_hook_method?(def_node) } end @@ -73,9 +82,15 @@ else method_def.each_child_node(:send) end send_nodes.select { |send_node| assertion_method?(send_node) } + end + + def assertions_count(node) + node.each_descendant(:send).count do |send_node| + assertion_method?(send_node) + end end def assertion_method?(node) return false if !node.send_type? && !node.block_type?