From 01dd517887d4dd04b8be61ce176c4866768b1823 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 26 Sep 2023 02:13:08 +0900 Subject: [PATCH] Fix an error for `Minitest/AssertOperator` and `Minitest/RefuteOperator` This PR fixes an error for `Minitest/AssertOperator` and `Minitest/RefuteOperator` when using unary operation argument. --- ...an_error_for_assert_operator_and_refute_operator.md | 1 + lib/rubocop/cop/minitest/assert_operator.rb | 2 +- lib/rubocop/cop/minitest/refute_operator.rb | 2 +- test/rubocop/cop/minitest/assert_operator_test.rb | 10 ++++++++++ test/rubocop/cop/minitest/refute_operator_test.rb | 10 ++++++++++ 5 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_an_error_for_assert_operator_and_refute_operator.md diff --git a/changelog/fix_an_error_for_assert_operator_and_refute_operator.md b/changelog/fix_an_error_for_assert_operator_and_refute_operator.md new file mode 100644 index 00000000..ef14d921 --- /dev/null +++ b/changelog/fix_an_error_for_assert_operator_and_refute_operator.md @@ -0,0 +1 @@ +* [#263](/~https://github.com/rubocop/rubocop-minitest/pull/263): Fix an error for `Minitest/AssertOperator` and `Minitest/RefuteOperator` when using unary operation argument. ([@koic][]) diff --git a/lib/rubocop/cop/minitest/assert_operator.rb b/lib/rubocop/cop/minitest/assert_operator.rb index e2f78536..241adca2 100644 --- a/lib/rubocop/cop/minitest/assert_operator.rb +++ b/lib/rubocop/cop/minitest/assert_operator.rb @@ -22,7 +22,7 @@ class AssertOperator < Base def on_send(node) first_argument = node.first_argument - return unless first_argument.respond_to?(:operator_method?) && first_argument.operator_method? + return unless first_argument.respond_to?(:binary_operation?) && first_argument.binary_operation? operator = first_argument.to_a[1] return if ALLOWED_OPERATORS.include?(operator) diff --git a/lib/rubocop/cop/minitest/refute_operator.rb b/lib/rubocop/cop/minitest/refute_operator.rb index 719d63d1..213ebeeb 100644 --- a/lib/rubocop/cop/minitest/refute_operator.rb +++ b/lib/rubocop/cop/minitest/refute_operator.rb @@ -22,7 +22,7 @@ class RefuteOperator < Base def on_send(node) first_argument = node.first_argument - return unless first_argument.respond_to?(:operator_method?) && first_argument.operator_method? + return unless first_argument.respond_to?(:binary_operation?) && first_argument.binary_operation? operator = first_argument.to_a[1] return if ALLOWED_OPERATORS.include?(operator) diff --git a/test/rubocop/cop/minitest/assert_operator_test.rb b/test/rubocop/cop/minitest/assert_operator_test.rb index 28f75246..31d2cc53 100644 --- a/test/rubocop/cop/minitest/assert_operator_test.rb +++ b/test/rubocop/cop/minitest/assert_operator_test.rb @@ -91,4 +91,14 @@ def test_do_something end RUBY end + + def test_does_not_register_offense_when_using_assert_with_unary_operation + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + assert(-x) + end + end + RUBY + end end diff --git a/test/rubocop/cop/minitest/refute_operator_test.rb b/test/rubocop/cop/minitest/refute_operator_test.rb index a8a558d8..58a81ef8 100644 --- a/test/rubocop/cop/minitest/refute_operator_test.rb +++ b/test/rubocop/cop/minitest/refute_operator_test.rb @@ -91,4 +91,14 @@ def test_do_something end RUBY end + + def test_does_not_register_offense_when_using_refute_with_unary_operation + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute(-x) + end + end + RUBY + end end