Skip to content

Commit

Permalink
Make Minitest/RefuteEqual aware of assert_operator and `refute_op…
Browse files Browse the repository at this point in the history
…erator`

This PR makes `Minitest/RefuteEqual` aware of `assert_operator` and `refute_operator`.
  • Loading branch information
koic committed Oct 2, 2023
1 parent 213d03c commit d9dcb8c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#267](https://github.com/rubocop/rubocop-minitest/pull/267): Make `Minitest/RefuteEqual` aware of `assert_operator` and `refute_operator`. ([@koic][])
44 changes: 18 additions & 26 deletions lib/rubocop/cop/minitest/refute_equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -19,48 +21,38 @@ class RefuteEqual < Base
extend AutoCorrector

MSG = 'Prefer using `refute_equal(%<preferred>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
Expand Down
83 changes: 80 additions & 3 deletions test/rubocop/cop/minitest/refute_equal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

0 comments on commit d9dcb8c

Please sign in to comment.