-
-
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.
It is a common mistake to use `assert` instead of `assert_equal`. This is dangerous because minitest will treat the second argument to `assert` as the `msg` parameter and, as long as the first argument is truthy, the test will always pass. This false negative gives the user a sense of security even though they're not actually testing what they think they are. This commit introduces a new cop which will register a violation if `assert` is called with two arguments and the second argument isn't a `String`. If the second argument *is* a `String`, then the user is likely using `assert` as intended. If it's not, most of the time it means a mistake has been made.
- 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 |