diff --git a/changelog/change_make_minitest_refute_equal_aware_of_refute_equal.md b/changelog/change_make_minitest_refute_equal_aware_of_refute_equal.md new file mode 100644 index 00000000..03cb82c2 --- /dev/null +++ b/changelog/change_make_minitest_refute_equal_aware_of_refute_equal.md @@ -0,0 +1 @@ +* [#265](https://github.com/rubocop/rubocop-minitest/pull/265): Make `Minitest/RefuteEqual` aware of `refute(expected == actual)`. ([@koic][]) diff --git a/lib/rubocop/cop/minitest/refute_equal.rb b/lib/rubocop/cop/minitest/refute_equal.rb index bdf8937b..c3079a7e 100644 --- a/lib/rubocop/cop/minitest/refute_equal.rb +++ b/lib/rubocop/cop/minitest/refute_equal.rb @@ -9,6 +9,7 @@ module Minitest # @example # # bad # assert("rubocop-minitest" != actual) + # refute("rubocop-minitest" == actual) # # # good # refute_equal("rubocop-minitest", actual) @@ -18,17 +19,20 @@ class RefuteEqual < Base extend AutoCorrector MSG = 'Prefer using `refute_equal(%s)`.' - RESTRICT_ON_SEND = %i[assert].freeze + RESTRICT_ON_SEND = %i[assert refute].freeze - def_node_matcher :assert_not_equal, <<~PATTERN - (send nil? :assert (send $_ :!= $_) $... ) + def_node_matcher :assert_not_equal_or_refute_equal, <<~PATTERN + { + (send nil? :assert (send $_ :!= $_) $...) + (send nil? :refute (send $_ :== $_) $...) + } PATTERN def on_send(node) preferred = process_not_equal(node) return unless preferred - assert_not_equal(node) do |expected, actual| + assert_not_equal_or_refute_equal(node) do |expected, actual| message = format(MSG, preferred: preferred) add_offense(node, message: message) do |corrector| @@ -51,7 +55,7 @@ def original_usage(first_part, custom_message) end def process_not_equal(node) - assert_not_equal(node) do |first_arg, second_arg, rest_args| + assert_not_equal_or_refute_equal(node) do |first_arg, second_arg, rest_args| custom_message = rest_args.first preferred_usage(first_arg, second_arg, custom_message) diff --git a/test/rubocop/cop/minitest/refute_equal_test.rb b/test/rubocop/cop/minitest/refute_equal_test.rb index d5613101..a340bc31 100644 --- a/test/rubocop/cop/minitest/refute_equal_test.rb +++ b/test/rubocop/cop/minitest/refute_equal_test.rb @@ -79,6 +79,25 @@ def test_do_something RUBY end + def test_registers_offense_when_using_refute_equal_operator + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute('rubocop-minitest' == actual) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', actual)`. + end + end + RUBY + + assert_correction(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_equal('rubocop-minitest', actual) + end + end + RUBY + end + def test_does_not_register_offense_when_using_negate_equals assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test