-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a cop to enforce assert_includes over assert(collection.include…
…s?(actual))
- Loading branch information
1 parent
bea0298
commit ea42ea1
Showing
6 changed files
with
194 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Check if your test uses `assert_includes` | ||
# instead of `assert(collection.includes?(actual))`. | ||
# | ||
# @example | ||
# # bad | ||
# assert(collection.includes?(actual)) | ||
# assert(collection.includes?(actual), 'the message') | ||
# | ||
# # good | ||
# assert_includes(collection, actual) | ||
# assert_includes(collection, actual, 'the message') | ||
# | ||
class AssertIncludes < Cop | ||
MSG = 'Prefer using `assert_includes(%<arguments>s)` over ' \ | ||
'`assert(%<receiver>s)`.' | ||
|
||
def_node_matcher :assert_with_includes, <<~PATTERN | ||
(send nil? :assert $(send $_ :includes? $_) $...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
assert_with_includes(node) do | ||
|first_receiver_arg, collection, actual, rest_receiver_arg| | ||
|
||
message = rest_receiver_arg.first | ||
arguments = node_arguments(collection, actual, message) | ||
receiver = [first_receiver_arg.source, message&.source].compact.join(', ') | ||
|
||
offense_message = format(MSG, arguments: arguments, receiver: receiver) | ||
|
||
add_offense(node, message: offense_message) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
assert_with_includes(node) do | ||
|_receiver, collection, actual, rest_receiver_arg| | ||
|
||
message = rest_receiver_arg.first | ||
replacement = node_arguments(collection, actual, message) | ||
corrector.replace(node.loc.expression, "assert_includes(#{replacement})") | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def node_arguments(collection, actual, message) | ||
[collection.source, actual.source, message&.source].compact.join(', ') | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'minitest/assert_nil' | ||
require_relative 'minitest/assert_includes' |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<!-- START_COP_LIST --> | ||
#### Department [Minitest](cops_minitest.md) | ||
|
||
* [Minitest/AssertIncludes](cops_minitest.md#minitestassertincludes) | ||
* [Minitest/AssertNil](cops_minitest.md#minitestassertnil) | ||
|
||
<!-- END_COP_LIST --> |
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,101 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertIncludesTest < Minitest::Test | ||
def setup | ||
@cop = RuboCop::Cop::Minitest::AssertIncludes.new | ||
end | ||
|
||
def test_adds_offense_for_use_of_assert_collection_includes | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(collection.includes?(actual)) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_includes(collection, actual)` over `assert(collection.includes?(actual))`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_includes(collection, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_adds_offense_for_use_of_assert_collection_includes_with_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(collection.includes?(actual), 'the message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_includes(collection, actual, 'the message')` over `assert(collection.includes?(actual), 'the message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_includes(collection, actual, 'the message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_adds_offense_for_use_of_assert_collection_includes_with_variable_in_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
message = 'the message' | ||
assert(collection.includes?(actual), message) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_includes(collection, actual, message)` over `assert(collection.includes?(actual), message)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
message = 'the message' | ||
assert_includes(collection, actual, message) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_adds_offense_for_use_of_assert_collection_includes_with_constant_in_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
MESSAGE = 'the message' | ||
def test_do_something | ||
assert(collection.includes?(actual), MESSAGE) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_includes(collection, actual, MESSAGE)` over `assert(collection.includes?(actual), MESSAGE)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
MESSAGE = 'the message' | ||
def test_do_something | ||
assert_includes(collection, actual, MESSAGE) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_offend_if_using_assert_nil | ||
assert_no_offenses(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_includes(collection, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |