Skip to content

Commit

Permalink
[Fix rubocop#321] check for assertion method receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
MatzFan committed Sep 26, 2024
1 parent 1da48c7 commit e6e08a6
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 17 deletions.
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][])
13 changes: 5 additions & 8 deletions lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# frozen_string_literal: true

require 'minitest/assertions'
require 'minitest/mock' # includes Assertions#assert_mock
require 'set'

module RuboCop
Expand All @@ -12,8 +10,7 @@ module MinitestExplorationHelpers
include DefNode
extend NodePattern::Macros

ASSERTION_REGEXP = /\A(assert|refute)/.freeze
MINITEST_ASSERTIONS = ::Minitest::Assertions.instance_methods.grep(ASSERTION_REGEXP)
ASSERTION_PREFIXES = %w[assert refute].freeze

LIFECYCLE_HOOK_METHODS_IN_ORDER = %i[
before_setup
Expand Down Expand Up @@ -102,19 +99,19 @@ 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)
return false if !node.send_type? && !node.block_type? && !node.numblock_type?

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

method_name == assertion_method || 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

def lifecycle_hook_method?(node)
node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def test_do_something
def test_does_not_register_offense_when_using_assertion_methods_which_are_continuous_without_empty_line
assert_no_offenses(<<~RUBY)
def test_do_something
assert foo
assert_not foo
assert bar
end
RUBY
Expand Down
8 changes: 4 additions & 4 deletions test/rubocop/cop/minitest/multiple_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ def setup
configure_max_assertions(1)
end

def test_checks_only_genuine_assertion_methods
def test_checks_only_assertion_methods_with_no_receiver
assert_no_offenses(<<~RUBY)
class FooTest < Minitest::Test
# assertion 'look-alike' method
# assert_something has a receiver
def test_asserts_once
assert_equal(:assert_equal, Bar.assert_but_not_a_minitest_assertion)
assert_equal(foo, Bar.assert_something)
end
end
RUBY
Expand Down Expand Up @@ -48,7 +48,7 @@ def test_registers_offense_when_multiple_expectations_with_numblock
class FooTest < Minitest::Test
def test_asserts_two_times
^^^^^^^^^^^^^^^^^^^^^^^^^^ Test case has too many assertions [2/1].
assert do
assert_something do
assert_equal(_1, bar)
end
end
Expand Down
4 changes: 0 additions & 4 deletions test/rubocop/cop/mixin/minitest_exploration_helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ def test_private; end
end
RUBY

def test_MINITEST_ASSERTIONS # rubocop:disable Naming/MethodName
assert_equal 38, RuboCop::Cop::MinitestExplorationHelpers::MINITEST_ASSERTIONS.size
end

def test_test_case_returns_true_for_test_case
assert Helper.test_case?(method_node(:test_public))
end
Expand Down

0 comments on commit e6e08a6

Please sign in to comment.