Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for assertion method receiver - fixes #321 #322

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#321](https://github.com/rubocop/rubocop-minitest/issues/321): Fix a false positive for `Minitest/MultipleAssertions` for assert_/refute_ methods with a receiver. ([@MatzFan][])
6 changes: 3 additions & 3 deletions lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def assertions_count(node)
end
end

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
def assertion_method?(node)
return false unless node
return assertion_method?(node.expression) if node.assignment? && node.respond_to?(:expression)
Expand All @@ -108,10 +108,10 @@ def assertion_method?(node)
ASSERTION_PREFIXES.any? do |prefix|
method_name = node.method_name

method_name.start_with?(prefix) || node.method?(:flunk)
(method_name.start_with?(prefix) && !node.receiver) || node.method?(:flunk)
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
Copy link
Member

@koic koic Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor to the following and rebase with the latest master branch?

diff --git a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
index 4bb7382..ceb1971 100644
--- a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
+++ b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
@@ -99,19 +99,20 @@ module RuboCop
         end
       end

-      # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
+      # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
       def assertion_method?(node)
         return false unless node
+        return false if node.receiver
         return assertion_method?(node.expression) if node.assignment? && node.respond_to?(:expression)
         return false if !node.send_type? && !node.block_type? && !node.numblock_type?

         ASSERTION_PREFIXES.any? do |prefix|
           method_name = node.method_name

-          (method_name.start_with?(prefix) && !node.receiver) || node.method?(:flunk)
+          method_name.start_with?(prefix) || node.method?(:flunk)
         end
       end
-      # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/AbcSize
+      # rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

       def lifecycle_hook_method?(node)
         node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name)


def lifecycle_hook_method?(node)
node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name)
Expand Down
11 changes: 11 additions & 0 deletions test/rubocop/cop/minitest/multiple_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ def setup
configure_max_assertions(1)
end

def test_checks_only_assertion_methods_with_no_receiver
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
# assert_something has a receiver
def test_asserts_once
assert_equal(foo, Bar.assert_something)
end
end
RUBY
end

def test_registers_offense_when_multiple_expectations
assert_offense(<<~RUBY)
class FooTest < Minitest::Test
Expand Down