Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Minitest/RefuteEqual aware of refute(expected == actual) #265

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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