lib/rubocop/cop/minitest/multiple_assertions.rb in rubocop-minitest-0.30.0 vs lib/rubocop/cop/minitest/multiple_assertions.rb in rubocop-minitest-0.31.0
- old
+ new
@@ -1,11 +1,12 @@
# frozen_string_literal: true
module RuboCop
module Cop
module Minitest
- # Checks if test cases contain too many assertion calls.
+ # Checks if test cases contain too many assertion calls. If conditional code with assertions
+ # is used, the branch with maximum assertions is counted.
# The maximum allowed assertion calls is configurable.
#
# @example Max: 1
# # bad
# class FooTest < Minitest::Test
@@ -34,11 +35,11 @@
def on_class(class_node)
return unless test_class?(class_node)
test_cases(class_node).each do |node|
- assertions_count = assertions_count(node)
+ assertions_count = assertions_count(node.body)
next unless assertions_count > max_assertions
self.max = assertions_count
@@ -46,9 +47,32 @@
add_offense(node, message: message)
end
end
private
+
+ def assertions_count(node)
+ return 0 unless node.is_a?(RuboCop::AST::Node)
+
+ assertions =
+ case node.type
+ when :if, :case, :case_match
+ assertions_count_in_branches(node.branches)
+ when :rescue
+ assertions_count(node.body) + assertions_count_in_branches(node.branches)
+ when :block, :numblock
+ assertions_count(node.body)
+ else
+ node.each_child_node.sum { |child| assertions_count(child) }
+ end
+
+ assertions += 1 if assertion_method?(node)
+ assertions
+ end
+
+ def assertions_count_in_branches(branches)
+ branches.map { |branch| assertions_count(branch) }.max
+ end
def max_assertions
Integer(cop_config.fetch('Max', 3))
end
end