-
-
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.
Include a cop to enforce assert_respond_to
This commit adds a cop that checks the usage of `assert(object.respond_to?(:some_method))` and enforces the usage of `assert_respond_to(object, :some_method)` instead.
- Loading branch information
1 parent
38729e2
commit 4f18082
Showing
7 changed files
with
140 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,49 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the use of `assert_respond_to(object, :some_method)` | ||
# over `assert(object.respond_to?(:some_method))`. | ||
# | ||
# @example | ||
# # bad | ||
# assert(object.respond_to?(:some_method)) | ||
# assert(object.respond_to?(:some_method), 'the message') | ||
# | ||
# # good | ||
# assert_respond_to(object, :some_method) | ||
# assert_respond_to(object, :some_method, 'the message') | ||
# | ||
class AssertRespondTo < Cop | ||
MSG = 'Prefer using `assert_respond_to(%<preferred>s)` over ' \ | ||
'`assert(%<over>s)`.' | ||
|
||
def_node_matcher :assert_with_respond_to, <<~PATTERN | ||
(send nil? :assert $(send $_ :respond_to? $_) $...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
assert_with_respond_to(node) do |over, object, method, rest_args| | ||
custom_message = rest_args.first | ||
preferred = [object, method, custom_message].compact.map(&:source).join(', ') | ||
over = [over, custom_message].compact.map(&:source).join(', ') | ||
message = format(MSG, preferred: preferred, over: over) | ||
add_offense(node, message: message) | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
lambda do |corrector| | ||
assert_with_respond_to(node) do |_, object, method, rest_args| | ||
custom_message = rest_args.first | ||
replacement = [object, method, custom_message] | ||
.compact.map(&:source).join(', ') | ||
corrector.replace(node.loc.expression, "assert_respond_to(#{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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertRespondToTest < Minitest::Test | ||
def setup | ||
@cop = RuboCop::Cop::Minitest::AssertRespondTo.new | ||
end | ||
|
||
def test_registers_offense_when_using_assert_calling_respond_to_method | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(object.respond_to?(:some_method)) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_respond_to(object, :some_method)` over `assert(object.respond_to?(:some_method))`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_respond_to(object, :some_method) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_calling_respond_to_method_with_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(object.respond_to?(:some_method), 'the message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_respond_to(object, :some_method, 'the message')` over `assert(object.respond_to?(:some_method), 'the message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_respond_to(object, :some_method, 'the message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assert_respond_to | ||
assert_no_offenses(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_respond_to('rubocop-minitest', :some_method) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |