-
-
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 cop to enfore refute_empty over refute(actual.empty?)
- Loading branch information
1 parent
7a42f9f
commit 870b213
Showing
7 changed files
with
183 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Check if your test uses `refute_empty` instead of `refute(actual.empty?)`. | ||
# | ||
# @example | ||
# # bad | ||
# refute(actual.empty?) | ||
# refute(actual.empty?, 'the message') | ||
# | ||
# # good | ||
# refute_empty(actual) | ||
# refute_empty(actual, 'the message') | ||
# | ||
class RefuteEmpty < Cop | ||
MSG = 'Prefer using `refute_empty(%<arguments>s)` over ' \ | ||
'`refute(%<receiver>s)`.' | ||
|
||
def_node_matcher :refute_with_empty, <<~PATTERN | ||
(send nil? :refute $(send $_ :empty?) $...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
refute_with_empty(node) do |first_receiver_arg, actual, rest_receiver_arg| | ||
message = rest_receiver_arg.first | ||
|
||
arguments = [actual.source, message&.source].compact.join(', ') | ||
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_empty(node) do |_first_receiver_arg, actual, rest_receiver_arg| | ||
message = rest_receiver_arg.first | ||
|
||
replacement = [actual.source, message&.source].compact.join(', ') | ||
corrector.replace(node.loc.expression, "refute_empty(#{replacement})") | ||
end | ||
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
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,101 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class RefuteEmptyTest < Minitest::Test | ||
def setup | ||
@cop = RuboCop::Cop::Minitest::RefuteEmpty.new | ||
end | ||
|
||
def test_registers_offense_when_using_refute_with_empty | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute(somestuff.empty?) | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_empty(somestuff)` over `refute(somestuff.empty?)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_empty(somestuff) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_refute_with_empty_and_string_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute(somestuff.empty?, 'the message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_empty(somestuff, 'the message')` over `refute(somestuff.empty?, 'the message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_empty(somestuff, 'the message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_refute_with_empty_and_variable_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
message = 'the message' | ||
refute(somestuff.empty?, message) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_empty(somestuff, message)` over `refute(somestuff.empty?, message)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
message = 'the message' | ||
refute_empty(somestuff, message) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_refute_with_empty_and_constant_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
MESSAGE = 'the message' | ||
def test_do_something | ||
refute(somestuff.empty?, MESSAGE) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_empty(somestuff, MESSAGE)` over `refute(somestuff.empty?, MESSAGE)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
MESSAGE = 'the message' | ||
def test_do_something | ||
refute_empty(somestuff, MESSAGE) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def refute_empty_method | ||
assert_no_offenses(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_empty(somestuff) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |