diff --git a/changelog/new_make_minitest_refute_equal_aware_of_assert_operator_and_refute_operator.md b/changelog/new_make_minitest_refute_equal_aware_of_assert_operator_and_refute_operator.md new file mode 100644 index 0000000..1565ab9 --- /dev/null +++ b/changelog/new_make_minitest_refute_equal_aware_of_assert_operator_and_refute_operator.md @@ -0,0 +1 @@ +* [#267](https://github.com/rubocop/rubocop-minitest/pull/267): Make `Minitest/RefuteEqual` aware of `assert_operator` and `refute_operator`. ([@koic][]) diff --git a/lib/rubocop/cop/minitest/refute_equal.rb b/lib/rubocop/cop/minitest/refute_equal.rb index c3079a7..bbacf8f 100644 --- a/lib/rubocop/cop/minitest/refute_equal.rb +++ b/lib/rubocop/cop/minitest/refute_equal.rb @@ -10,6 +10,8 @@ module Minitest # # bad # assert("rubocop-minitest" != actual) # refute("rubocop-minitest" == actual) + # assert_operator("rubocop-minitest", :!=, actual) + # refute_operator("rubocop-minitest", :==, actual) # # # good # refute_equal("rubocop-minitest", actual) @@ -19,48 +21,38 @@ class RefuteEqual < Base extend AutoCorrector MSG = 'Prefer using `refute_equal(%s)`.' - RESTRICT_ON_SEND = %i[assert refute].freeze + RESTRICT_ON_SEND = %i[assert refute assert_operator refute_operator].freeze - def_node_matcher :assert_not_equal_or_refute_equal, <<~PATTERN + def_node_matcher :refute_equal, <<~PATTERN { (send nil? :assert (send $_ :!= $_) $...) (send nil? :refute (send $_ :== $_) $...) + (send nil? :assert_operator $_ (sym :!=) $_ $...) + (send nil? :refute_operator $_ (sym :==) $_ $...) } PATTERN + # rubocop:disable Metrics/AbcSize def on_send(node) - preferred = process_not_equal(node) - return unless preferred - - assert_not_equal_or_refute_equal(node) do |expected, actual| + refute_equal(node) do |expected, actual, rest_args| + basic_arguments = "#{expected.source}, #{actual.source}" + preferred = (message_arg = rest_args.first) ? "#{basic_arguments}, #{message_arg.source}" : basic_arguments message = format(MSG, preferred: preferred) add_offense(node, message: message) do |corrector| corrector.replace(node.loc.selector, 'refute_equal') - replacement = [expected, actual].map(&:source).join(', ') - corrector.replace(node.first_argument, replacement) - end - end - end - - private + range = if node.method?(:assert) || node.method?(:refute) + node.first_argument + else + node.first_argument.source_range.begin.join(node.arguments[2].source_range.end) + end - def preferred_usage(first_arg, second_arg, custom_message = nil) - [first_arg, second_arg, custom_message].compact.map(&:source).join(', ') - end - - def original_usage(first_part, custom_message) - [first_part, custom_message].compact.join(', ') - end - - def process_not_equal(node) - 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) + corrector.replace(range, basic_arguments) + end end end + # rubocop:enable Metrics/AbcSize end end end diff --git a/test/rubocop/cop/minitest/refute_equal_test.rb b/test/rubocop/cop/minitest/refute_equal_test.rb index a340bc3..3201231 100644 --- a/test/rubocop/cop/minitest/refute_equal_test.rb +++ b/test/rubocop/cop/minitest/refute_equal_test.rb @@ -79,12 +79,50 @@ def test_do_something RUBY end - def test_registers_offense_when_using_refute_equal_operator + def test_registers_offense_when_using_assert_operator_with_negated_equal_symbol_argument assert_offense(<<~RUBY) class FooTest < Minitest::Test def test_do_something - refute('rubocop-minitest' == actual) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', actual)`. + assert_operator('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_registers_offense_when_using_assert_operator_with_negated_equal_symbol_and_message_arguments + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_operator('rubocop-minitest', :==, actual, message) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', actual, message)`. + end + end + RUBY + + assert_correction(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_equal('rubocop-minitest', actual, message) + end + end + RUBY + end + + def test_registers_offense_when_using_refute_operator_with_equal_symbol_argument + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_operator('rubocop-minitest', :==, actual) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', actual)`. end end RUBY @@ -98,6 +136,25 @@ def test_do_something RUBY end + def test_registers_offense_when_using_refute_operator_with_equal_symbol_and_message_arguments + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_operator('rubocop-minitest', :==, actual, message) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', actual, message)`. + end + end + RUBY + + assert_correction(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_equal('rubocop-minitest', actual, message) + end + end + RUBY + end + def test_does_not_register_offense_when_using_negate_equals assert_no_offenses(<<~RUBY) class FooTest < Minitest::Test @@ -140,4 +197,24 @@ def test_do_something end RUBY end + + def test_registers_offense_when_using_assert_operator_with_equal_symbol_argument + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + assert_operator('rubocop-minitest', :==, actual) + end + end + RUBY + end + + def test_registers_offense_when_using_refute_operator_with_negated_equal_symbol_argument + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + refute_operator('rubocop-minitest', :!=, actual) + end + end + RUBY + end end