forked from rubocop/rubocop-minitest
-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# 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 AssertWithTwoArguments < Cop | ||
MSG = 'Did you mean to use `assert_equal(%<arguments>s)`?' | ||
|
||
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? | ||
|
||
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
37 changes: 37 additions & 0 deletions
37
test/rubocop/cop/minitest/assert_with_two_arguments_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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertWithTwoArgumentsTest < 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_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_string | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert([], "empty array should be truthy") | ||
end | ||
end | ||
RUBY | ||
end | ||
end |