From 060e7a07e98dd4836e3fa7eff0736c85996d7a57 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Sun, 1 Oct 2023 19:10:33 +0900 Subject: [PATCH] Make `Minitest/AssertEqual` aware of `assert_operator` This PR makes `Minitest/AssertEqual` aware of `assert_operator`. --- ...t_assert_equal_aware_of_assert_operator.md | 1 + lib/rubocop/cop/minitest/assert_equal.rb | 36 +++++++++++++- .../rubocop/cop/minitest/assert_equal_test.rb | 48 +++++++++++++++++-- 3 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 changelog/new_make_minitest_assert_equal_aware_of_assert_operator.md diff --git a/changelog/new_make_minitest_assert_equal_aware_of_assert_operator.md b/changelog/new_make_minitest_assert_equal_aware_of_assert_operator.md new file mode 100644 index 00000000..5abf30db --- /dev/null +++ b/changelog/new_make_minitest_assert_equal_aware_of_assert_operator.md @@ -0,0 +1 @@ +* [#266](/~https://github.com/rubocop/rubocop-minitest/pull/266): Make `Minitest/AssertEqual` aware of `assert_operator`. ([@koic][]) diff --git a/lib/rubocop/cop/minitest/assert_equal.rb b/lib/rubocop/cop/minitest/assert_equal.rb index 05abc62a..8fc54329 100644 --- a/lib/rubocop/cop/minitest/assert_equal.rb +++ b/lib/rubocop/cop/minitest/assert_equal.rb @@ -9,14 +9,46 @@ module Minitest # @example # # bad # assert("rubocop-minitest" == actual) + # assert_operator("rubocop-minitest", :==, actual) # # # good # assert_equal("rubocop-minitest", actual) # class AssertEqual < Base - extend MinitestCopRule + include ArgumentRangeHelper + extend AutoCorrector - define_rule :assert, target_method: :==, preferred_method: :assert_equal + MSG = 'Prefer using `assert_equal(%s)`.' + RESTRICT_ON_SEND = %i[assert assert_operator].freeze + + def_node_matcher :assert_equal, <<~PATTERN + { + (send nil? :assert (send $_ :== $_) $...) + (send nil? :assert_operator $_ (sym :==) $_ $...) + } + PATTERN + + # rubocop:disable Metrics/AbcSize + def on_send(node) + assert_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, 'assert_equal') + + range = if node.method?(:assert) + node.first_argument + else + node.first_argument.source_range.begin.join(node.arguments[2].source_range.end) + end + + corrector.replace(range, basic_arguments) + end + end + end + # rubocop:enable Metrics/AbcSize end end end diff --git a/test/rubocop/cop/minitest/assert_equal_test.rb b/test/rubocop/cop/minitest/assert_equal_test.rb index cd263613..26762617 100644 --- a/test/rubocop/cop/minitest/assert_equal_test.rb +++ b/test/rubocop/cop/minitest/assert_equal_test.rb @@ -104,12 +104,31 @@ def test_do_something RUBY end - def test_registers_offense_when_using_assert_with_equal_in_redundant_parentheses + def test_registers_offense_when_using_assert_operator_with_equal_symbol_argument assert_offense(<<~RUBY) class FooTest < Minitest::Test def test_do_something - assert(('rubocop-minitest' == actual)) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_equal('rubocop-minitest', actual)`. + assert_operator('rubocop-minitest', :==, actual) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_equal('rubocop-minitest', actual)`. + end + end + RUBY + + assert_correction(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + assert_equal('rubocop-minitest', actual) + end + end + RUBY + end + + def test_registers_offense_when_using_assert_operator_with_equal_symbol_and_message_argument + assert_offense(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + assert_operator('rubocop-minitest', :==, actual, message) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_equal('rubocop-minitest', actual, message)`. end end RUBY @@ -117,7 +136,7 @@ def test_do_something assert_correction(<<~RUBY) class FooTest < Minitest::Test def test_do_something - assert_equal(('rubocop-minitest', actual)) + assert_equal('rubocop-minitest', actual, message) end end RUBY @@ -133,6 +152,27 @@ def test_do_something RUBY end + def test_registers_offense_when_using_assert_operator_with_negated_equal_symbol_argument + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + assert_operator('rubocop-minitest', :!=, actual) + end + end + RUBY + end + + # Redundant parentheses should be removed by `Style/RedundantParentheses` cop. + def test_does_not_register_offense_when_using_assert_with_equal_in_redundant_parentheses + assert_no_offenses(<<~RUBY) + class FooTest < Minitest::Test + def test_do_something + assert(('rubocop-minitest' == actual)) + end + end + RUBY + end + # See: /~https://github.com/rubocop/rubocop-minitest/issues/113 def test_does_not_register_offense_when_assert_with_block_argument assert_no_offenses(<<~RUBY)