From fba823a733198a882d580133f637ed18adb1e5f4 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 20 Dec 2023 15:59:22 +0900 Subject: [PATCH] [Fix #283] Fix an error for `Minitest/MultipleAssertions` Fixes #283. This PR fixes an error for `Minitest/MultipleAssertions` when using `||` assigning a value to a variable. --- .../fix_an_error_for_minitest_multiple_assertions.md | 1 + .../cop/mixin/minitest_exploration_helpers.rb | 5 +++-- .../rubocop/cop/minitest/multiple_assertions_test.rb | 12 ++++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_an_error_for_minitest_multiple_assertions.md diff --git a/changelog/fix_an_error_for_minitest_multiple_assertions.md b/changelog/fix_an_error_for_minitest_multiple_assertions.md new file mode 100644 index 00000000..a36a4a7f --- /dev/null +++ b/changelog/fix_an_error_for_minitest_multiple_assertions.md @@ -0,0 +1 @@ +* [#283](https://github.com/rubocop/rubocop-minitest/issues/283): Fix an error for `Minitest/MultipleAssertions` when using `||` assigning a value to a variable. ([@koic][]) diff --git a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb index 2449c870..ffa39ce2 100644 --- a/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb +++ b/lib/rubocop/cop/mixin/minitest_exploration_helpers.rb @@ -99,8 +99,9 @@ def assertions_count(node) end end - # rubocop:disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity 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? @@ -110,7 +111,7 @@ def assertion_method?(node) method_name.start_with?(prefix) || node.method?(:flunk) end end - # rubocop:enable Metrics/CyclomaticComplexity + # rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity def lifecycle_hook_method?(node) node.def_type? && LIFECYCLE_HOOK_METHODS.include?(node.method_name) diff --git a/test/rubocop/cop/minitest/multiple_assertions_test.rb b/test/rubocop/cop/minitest/multiple_assertions_test.rb index c0d8bf10..12bd192a 100644 --- a/test/rubocop/cop/minitest/multiple_assertions_test.rb +++ b/test/rubocop/cop/minitest/multiple_assertions_test.rb @@ -113,6 +113,18 @@ def test_asserts_once RUBY end + def test_does_not_register_offense_when_using_or_assigning_a_value_to_an_object_attribute + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_asserts_once + var ||= :value + + assert_equal(foo, bar) + end + end + RUBY + end + def test_generates_a_todo_based_on_the_worst_violation inspect_source(<<-RUBY, @cop, 'test/foo_test.rb') class FooTest < Minitest::Test