-
-
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.
Add new
Minitest/UselessAssertion
cop
- Loading branch information
Showing
6 changed files
with
243 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#205](https://github.com/rubocop/rubocop-minitest/issues/205): Add new `Minitest/UselessAssertion` cop. ([@fatkodima][]) |
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,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Detects useless assertions (assertions that either always pass or always fail). | ||
# | ||
# @example | ||
# # bad | ||
# assert true | ||
# assert_equal @foo, @foo | ||
# assert_nil [foo, bar] | ||
# | ||
# # good | ||
# assert something | ||
# assert_equal foo, bar | ||
# assert_nil foo | ||
# assert false, "My message" | ||
# | ||
class UselessAssertion < Base | ||
MSG = 'Useless assertion detected.' | ||
|
||
SINGLE_ASSERTION_ARGUMENT_METHODS = %i[ | ||
assert refute assert_nil refute_nil assert_not assert_empty refute_empty | ||
].freeze | ||
TWO_ASSERTION_ARGUMENTS_METHODS = %i[ | ||
assert_equal refute_equal assert_in_delta refute_in_delta | ||
assert_in_epsilon refute_in_epsilon assert_same refute_same | ||
].freeze | ||
|
||
RESTRICT_ON_SEND = SINGLE_ASSERTION_ARGUMENT_METHODS + | ||
TWO_ASSERTION_ARGUMENTS_METHODS + | ||
%i[assert_includes refute_includes assert_silent] | ||
|
||
def on_send(node) | ||
return if node.receiver | ||
|
||
add_offense(node) if offense?(node) | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Metrics | ||
def offense?(node) | ||
expected, actual, = node.arguments | ||
|
||
case node.method_name | ||
when *SINGLE_ASSERTION_ARGUMENT_METHODS | ||
actual.nil? && expected&.literal? | ||
when *TWO_ASSERTION_ARGUMENTS_METHODS | ||
return false unless expected || actual | ||
return false if expected.source != actual.source | ||
|
||
(expected.variable? && actual.variable?) || | ||
(empty_composite?(expected) && empty_composite?(actual)) | ||
when :assert_includes, :refute_includes | ||
expected && empty_composite?(expected) | ||
when :assert_silent | ||
block_node = node.parent | ||
block_node&.body.nil? | ||
else | ||
false | ||
end | ||
end | ||
# rubocop:enable Metrics | ||
|
||
def empty_composite?(node) | ||
return true if node.str_type? && node.value.empty? | ||
|
||
(node.array_type? || node.hash_type?) && node.children.empty? | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class UselessAssertionTest < Minitest::Test | ||
%i[assert refute assert_nil refute_nil assert_not assert_empty refute_empty].each do |matcher| | ||
define_method("test_#{matcher}_registers_offense_when_using_literals") do | ||
assert_offense(<<~RUBY, matcher: matcher) | ||
#{matcher} [] | ||
^{matcher}^^^ Useless assertion detected. | ||
#{matcher} [foo] | ||
^{matcher}^^^^^^ Useless assertion detected. | ||
#{matcher}({}) | ||
^{matcher}^^^^ Useless assertion detected. | ||
#{matcher}({ key: value }) | ||
^{matcher}^^^^^^^^^^^^^^^^ Useless assertion detected. | ||
#{matcher} nil | ||
^{matcher}^^^^ Useless assertion detected. | ||
#{matcher} true | ||
^{matcher}^^^^^ Useless assertion detected. | ||
#{matcher} false | ||
^{matcher}^^^^^^ Useless assertion detected. | ||
#{matcher} "" | ||
^{matcher}^^^ Useless assertion detected. | ||
#{matcher} "foo" | ||
^{matcher}^^^^^^ Useless assertion detected. | ||
#{matcher} 1 | ||
^{matcher}^^ Useless assertion detected. | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_no_offenses_when_using_method_call") do | ||
assert_no_offenses(<<~RUBY) | ||
#{matcher} foo | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_no_offenses_when_using_explicit_message") do | ||
assert_no_offenses(<<~RUBY) | ||
#{matcher} false, "My message" | ||
RUBY | ||
end | ||
end | ||
|
||
%i[ | ||
assert_equal refute_equal | ||
assert_in_delta refute_in_delta | ||
assert_in_epsilon refute_in_epsilon | ||
assert_same refute_same | ||
].each do |matcher| | ||
define_method("test_#{matcher}_registers_offense_when_using_same_expected_and_actual") do | ||
assert_offense(<<~RUBY, matcher: matcher) | ||
#{matcher} [], [] | ||
^{matcher}^^^^^^^ Useless assertion detected. | ||
#{matcher} $foo, $foo | ||
^{matcher}^^^^^^^^^^^ Useless assertion detected. | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_registers_offense_when_using_same_local_variable") do | ||
assert_offense(<<~RUBY, matcher: matcher) | ||
foo = get_foo | ||
#{matcher} foo, foo | ||
^{matcher}^^^^^^^^^ Useless assertion detected. | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_no_offenses_when_different_expected_and_actual") do | ||
assert_no_offenses(<<~RUBY) | ||
#{matcher} foo, bar | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_no_offenses_when_expected_and_actual_are_same_method_call") do | ||
assert_no_offenses(<<~RUBY) | ||
#{matcher} foo, foo | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_no_offenses_when_expected_and_actual_are_same_expression") do | ||
assert_no_offenses(<<~RUBY) | ||
#{matcher} x.foo, x.foo | ||
RUBY | ||
end | ||
end | ||
|
||
%i[assert_includes refute_includes].each do |matcher| | ||
define_method("test_#{matcher}_registers_offense_when_using_empty_hash") do | ||
assert_offense(<<~RUBY, matcher: matcher) | ||
#{matcher}({}, foo) | ||
^{matcher}^^^^^^^^^ Useless assertion detected. | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_registers_offense_when_using_empty_array") do | ||
assert_offense(<<~RUBY, matcher: matcher) | ||
#{matcher} [], foo | ||
^{matcher}^^^^^^^^ Useless assertion detected. | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_registers_offense_when_using_empty_string") do | ||
assert_offense(<<~RUBY, matcher: matcher) | ||
#{matcher} "", foo | ||
^{matcher}^^^^^^^^ Useless assertion detected. | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_no_offenses_when_using_non_empty_composite_literal") do | ||
assert_no_offenses(<<~RUBY) | ||
#{matcher} foo, bar | ||
RUBY | ||
end | ||
|
||
define_method("test_#{matcher}_no_offenses_when_using_method_call") do | ||
assert_no_offenses(<<~RUBY) | ||
#{matcher} foo, bar | ||
RUBY | ||
end | ||
end | ||
|
||
def test_assert_silent_registers_offense_when_block_is_empty | ||
assert_offense(<<~RUBY) | ||
assert_silent {} | ||
^^^^^^^^^^^^^ Useless assertion detected. | ||
RUBY | ||
end | ||
|
||
def test_assert_silent_no_offenses_when_block_has_a_body | ||
assert_no_offenses(<<~RUBY) | ||
assert_silent do | ||
foo | ||
end | ||
RUBY | ||
end | ||
|
||
def test_ignores_useless_assertion_called_on_receiver | ||
assert_no_offenses(<<~RUBY) | ||
foo.assert(true) | ||
RUBY | ||
end | ||
end |