From d2de47efe95c9a4db912e545fba632741dd2ff5e Mon Sep 17 00:00:00 2001 From: fatkodima Date: Sun, 22 Jan 2023 12:30:38 +0200 Subject: [PATCH] Extract `#assertions_count` --- lib/rubocop/cop/minitest/multiple_assertions.rb | 6 ------ lib/rubocop/cop/minitest/no_assertions.rb | 7 ------- lib/rubocop/cop/mixin/minitest_exploration_helpers.rb | 6 ++++++ 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/rubocop/cop/minitest/multiple_assertions.rb b/lib/rubocop/cop/minitest/multiple_assertions.rb index c0224abd..c75f2b6b 100644 --- a/lib/rubocop/cop/minitest/multiple_assertions.rb +++ b/lib/rubocop/cop/minitest/multiple_assertions.rb @@ -49,12 +49,6 @@ def on_class(class_node) private - def assertions_count(node) - node.each_descendant(:send).count do |send_node| - assertion_method?(send_node) - end - end - def max_assertions Integer(cop_config.fetch('Max', 3)) end diff --git a/lib/rubocop/cop/minitest/no_assertions.rb b/lib/rubocop/cop/minitest/no_assertions.rb index b63d719b..b0eea3f7 100644 --- a/lib/rubocop/cop/minitest/no_assertions.rb +++ b/lib/rubocop/cop/minitest/no_assertions.rb @@ -35,13 +35,6 @@ def on_class(class_node) add_offense(node.block_type? ? node.loc.expression : node.loc.name) end end - - private - - def assertions_count(node) - base = assertion_method?(node) ? 1 : 0 - base + node.each_child_node.sum { |c| assertions_count(c) } - end end end end diff --git a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb index d0a16054..53d1c1a0 100644 --- a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +++ b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb @@ -82,6 +82,12 @@ def assertions(def_node) 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?