-
-
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.
Merge pull request #27 from duduribeiro/add_cop_to_enforce_assert_res…
…pond_to Include a cop to enforce assert_respond_to
- Loading branch information
Showing
7 changed files
with
142 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,51 @@ | ||
# 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 | ||
preferred = [object, method, custom_message] | ||
.compact.map(&:source).join(', ') | ||
replacement = "assert_respond_to(#{preferred})" | ||
corrector.replace(node.loc.expression, 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 |