-
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new AssertMatch and RefuteMatch matchers #49
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the test to use `assert_match` | ||
# instead of using `assert(matcher.match(string))`. | ||
# | ||
# @example | ||
# # bad | ||
# assert(matcher.match(string)) | ||
# assert(matcher.match(string), 'the message') | ||
# | ||
# # good | ||
# assert_match(regex, string) | ||
# assert_match(matcher, string, 'the message') | ||
# | ||
class AssertMatch < Cop | ||
include ArgumentRangeHelper | ||
|
||
MSG = 'Prefer using `assert_match(%<arguments>s)` over ' \ | ||
'`assert(%<receiver>s)`.' | ||
|
||
def_node_matcher :assert_with_match, <<~PATTERN | ||
(send nil? :assert $(send $_ :match $_) $...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
assert_with_match(node) do | ||
|first_receiver_arg, matcher, actual, rest_receiver_arg| | ||
message = rest_receiver_arg.first | ||
arguments = node_arguments(matcher, 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_match(node) do |_, matcher, actual| | ||
corrector.replace(node.loc.selector, 'assert_match') | ||
|
||
replacement = [matcher, actual].map(&:source).join(', ') | ||
corrector.replace(first_argument_range(node), replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def node_arguments(matcher, actual, message) | ||
[matcher.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the test to use `refute_match` | ||
# instead of using `refute(matcher.match(string))`. | ||
# | ||
# @example | ||
# # bad | ||
# refute(matcher.match(string)) | ||
# refute(matcher.match(string), 'the message') | ||
# | ||
# # good | ||
# refute_match(matcher, string) | ||
# refute_match(matcher, string, 'the message') | ||
# | ||
class RefuteMatch < Cop | ||
include ArgumentRangeHelper | ||
|
||
MSG = 'Prefer using `refute_match(%<arguments>s)` over ' \ | ||
'`refute(%<receiver>s)`.' | ||
|
||
def_node_matcher :refute_with_match, <<~PATTERN | ||
(send nil? :refute $(send $_ :match $_) $...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
refute_with_match(node) do | ||
|first_receiver_arg, matcher, actual, rest_receiver_arg| | ||
message = rest_receiver_arg.first | ||
arguments = node_arguments(matcher, 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| | ||
refute_with_match(node) do |_, matcher, actual| | ||
corrector.replace(node.loc.selector, 'refute_match') | ||
|
||
replacement = [matcher, actual].map(&:source).join(', ') | ||
corrector.replace(first_argument_range(node), replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def node_arguments(matcher, actual, message) | ||
[matcher.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
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,82 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertMatchTest < Minitest::Test | ||
def setup | ||
@cop = RuboCop::Cop::Minitest::AssertMatch.new | ||
end | ||
|
||
def test_registers_offense_when_using_assert_with_match | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(matcher.match(object)) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_match(matcher, object)` over `assert(matcher.match(object))`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_match(matcher, object) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_with_match_and_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(matcher.match(object), 'the message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_match(matcher, object, 'the message')` over `assert(matcher.match(object), 'the message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_match(matcher, object, 'the message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_with_match_and_heredoc_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(matcher.match(object), <<~MESSAGE | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_match(matcher, object, <<~MESSAGE)` over `assert(matcher.match(object), <<~MESSAGE)`. | ||
the message | ||
MESSAGE | ||
) | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_match(matcher, object, <<~MESSAGE | ||
the message | ||
MESSAGE | ||
) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assert_match | ||
assert_no_offenses(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_match(matcher, object) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you apply the following changelog entry format?
https://github.com/rubocop-hq/rubocop-minitest/blob/v0.5.1/CONTRIBUTING.md#changelog-entry-format