-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #120 from cstyles/assert-vs-assert_equal
Add new `Minitest/AssertWithExpectedArgument` cop
- Loading branch information
Showing
7 changed files
with
148 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
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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop tries to detect when a user accidentally used | ||
# `assert` when they meant to use `assert_equal`. | ||
# | ||
# @example | ||
# # bad | ||
# assert(3, my_list.length) | ||
# assert(expected, actual) | ||
# | ||
# # good | ||
# assert_equal(3, my_list.length) | ||
# assert_equal(expected, actual) | ||
# assert(foo, 'message') | ||
# | ||
class AssertWithExpectedArgument < Cop | ||
MSG = 'Did you mean to use `assert_equal(%<arguments>s)`?' | ||
RESTRICT_ON_SEND = %i[assert].freeze | ||
|
||
def_node_matcher :assert_with_two_arguments?, <<~PATTERN | ||
(send nil? :assert $_ $_) | ||
PATTERN | ||
|
||
def on_send(node) | ||
assert_with_two_arguments?(node) do |_expected, message| | ||
return if message.str_type? || message.dstr_type? | ||
|
||
arguments = node.arguments.map(&:source).join(', ') | ||
add_offense(node, message: format(MSG, arguments: arguments)) | ||
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
70 changes: 70 additions & 0 deletions
70
test/rubocop/cop/minitest/assert_with_expected_argument_test.rb
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,70 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertWithExpectedArgumentTest < Minitest::Test | ||
def test_registers_offense_when_second_argument_is_not_a_string | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
my_list = [1, 2, 3] | ||
assert(3, my_list.length) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean to use `assert_equal(3, my_list.length)`? | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_second_argument_is_a_variable | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
my_list_length = 3 | ||
assert(3, my_list_length) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Did you mean to use `assert_equal(3, my_list_length)`? | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_assert_is_called_with_one_argument | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(true) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_second_argument_is_a_literal_string | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert([], "empty array should be truthy") | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_second_argument_is_an_interpolated_string | ||
assert_no_offenses(<<~'RUBY') | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
additional_message = 'hello world' | ||
assert([], "empty array should be truthy #{additional_message}") | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assert_equal_with_two_arguments | ||
assert_no_offenses(<<~'RUBY') | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_equal(3, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |