-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include a cop to enforce refute_equal over assert of a negation (#21)
This commit adds a Cop that checks some usage of `assert(some negations)` and enforces the usage of `refute_eqaul(expected, actual)` instead.
- Loading branch information
1 parent
68bc547
commit 38729e2
Showing
7 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the usages of `refute_equal(expected, object)` | ||
# over `assert_equal(expected != actual)` or `assert(! expected -= actual)`. | ||
# | ||
# @example | ||
# # bad | ||
# assert("rubocop-minitest" != actual) | ||
# assert(! "rubocop-minitest" == actual) | ||
# | ||
# # good | ||
# refute_equal("rubocop-minitest", actual) | ||
# | ||
class RefuteEqual < Cop | ||
MSG = 'Prefer using `refute_equal(%<preferred>s)` over ' \ | ||
'`assert(%<over>s)`.' | ||
|
||
def_node_matcher :assert_not_equal, <<~PATTERN | ||
(send nil? :assert ${(send $_ :!= $_) (send (send $_ :! ) :== $_) } $... ) | ||
PATTERN | ||
|
||
def on_send(node) | ||
preferred, over = process_not_equal(node) | ||
return unless preferred && over | ||
|
||
message = format(MSG, preferred: preferred, over: over) | ||
add_offense(node, message: message) | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
assert_not_equal(node) do |_, first_arg, second_arg, rest_args| | ||
autocorrect_node(node, corrector, first_arg, second_arg, rest_args) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def autocorrect_node(node, corrector, first_arg, second_arg, rest_args) | ||
custom_message = rest_args.first | ||
replacement = preferred_usage(first_arg, second_arg, custom_message) | ||
corrector.replace(node.loc.expression, "refute_equal(#{replacement})") | ||
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(node) do |over, first_arg, second_arg, rest_args| | ||
custom_message = rest_args.first | ||
preferred = preferred_usage(first_arg, second_arg, custom_message) | ||
over = original_usage(over.source, custom_message&.source) | ||
return [preferred, over] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class RefuteEqualTest < Minitest::Test | ||
def setup | ||
@cop = RuboCop::Cop::Minitest::RefuteEqual.new | ||
end | ||
|
||
def test_registers_offense_when_using_assert_not_equal_operator_with_string | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert('rubocop-minitest' != actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', actual)` over `assert('rubocop-minitest' != actual)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_equal('rubocop-minitest', actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_not_equal_operator_with_object | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(expected != actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal(expected, actual)` over `assert(expected != actual)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_equal(expected, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_not_equal_operator_with_method_call | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(obj.expected != other_obj.actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal(obj.expected, other_obj.actual)` over `assert(obj.expected != other_obj.actual)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_equal(obj.expected, other_obj.actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_not_equal_operator_with_the_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert('rubocop-minitest' != actual, 'the message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', actual, 'the message')` over `assert('rubocop-minitest' != actual, 'the message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_equal('rubocop-minitest', actual, 'the message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_negate_equals | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(! 'rubocop-minitest' == object) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', object)` over `assert(! 'rubocop-minitest' == object)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_equal('rubocop-minitest', object) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_negate_equals_with_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(! 'rubocop-minitest' == object, 'the message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_equal('rubocop-minitest', object, 'the message')` over `assert(! 'rubocop-minitest' == object, 'the message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_equal('rubocop-minitest', object, 'the message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_refute_equal | ||
assert_no_offenses(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_equal('rubocop-minitest', actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |