Skip to content

Commit

Permalink
Make Minitest/RefuteEqual aware of refute(expected == actual)
Browse files Browse the repository at this point in the history
Follow up rubocop#260 (comment)

This PR makes `Minitest/RefuteEqual` aware of `refute(expected == actual)`.
  • Loading branch information
koic committed Nov 27, 2023
1 parent 7da2ab3 commit ca85011
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#265](https://github.com/rubocop/rubocop-minitest/pull/265): Make `Minitest/RefuteEqual` aware of `refute(expected == actual)`. ([@koic][])
14 changes: 9 additions & 5 deletions lib/rubocop/cop/minitest/refute_equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Minitest
# @example
# # bad
# assert("rubocop-minitest" != actual)
# refute("rubocop-minitest" == actual)
#
# # good
# refute_equal("rubocop-minitest", actual)
Expand All @@ -18,17 +19,20 @@ class RefuteEqual < Base
extend AutoCorrector

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

0 comments on commit ca85011

Please sign in to comment.