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/AssertEqual aware of assert_operator #266

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 @@
* [#266](https://github.com/rubocop/rubocop-minitest/pull/266): Make `Minitest/AssertEqual` aware of `assert_operator`. ([@koic][])
36 changes: 34 additions & 2 deletions lib/rubocop/cop/minitest/assert_equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(%<preferred>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
Expand Down
48 changes: 44 additions & 4 deletions test/rubocop/cop/minitest/assert_equal_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,39 @@ 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

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
Expand All @@ -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.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: This case will be solved by rubocop/rubocop#12247.

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)
Expand Down