-
-
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.
Adds cop to enforce assert_instance_of over assert(object.instance_of…
…?(Class))
- Loading branch information
1 parent
6088c9d
commit ddc7ae4
Showing
7 changed files
with
151 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,60 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the test to use `assert_instance_of(Class, object)` | ||
# over `assert(object.instance_of?(Class))`. | ||
# | ||
# @example | ||
# # bad | ||
# assert(object.instance_of?(Class)) | ||
# assert(object.instance_of?(Class), 'the message') | ||
# | ||
# # good | ||
# assert_instance_of(Class, object) | ||
# assert_instance_of(Class, object, 'the message') | ||
# | ||
class AssertInstanceOf < Cop | ||
MSG = 'Prefer using `assert_instance_of(%<arguments>s)` over ' \ | ||
'`assert(%<receiver>s)`.' | ||
|
||
def_node_matcher :assert_with_instance_of, <<~PATTERN | ||
(send nil? :assert $(send $_ :instance_of? $_) $...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
assert_with_instance_of(node) do | ||
|first_receiver_arg, object, method, rest_args| | ||
|
||
message = rest_args.first | ||
arguments = node_arguments(object, method, 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_instance_of(node) do |_, object, method, rest_args| | ||
message = rest_args.first | ||
arguments = node_arguments(object, method, message) | ||
|
||
replacement = "assert_instance_of(#{arguments})" | ||
corrector.replace(node.loc.expression, replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def node_arguments(object, method, message) | ||
[method, object, message].compact.map(&:source).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,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertInstanceOfTest < Minitest::Test | ||
def setup | ||
@cop = RuboCop::Cop::Minitest::AssertInstanceOf.new | ||
end | ||
|
||
def test_registers_offense_when_using_assert_with_instance_of | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(object.instance_of?(SomeClass)) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_instance_of(SomeClass, object)` over `assert(object.instance_of?(SomeClass))`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_instance_of(SomeClass, object) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_with_instance_of_and_message | ||
assert_offense(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(object.instance_of?(SomeClass), 'the message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_instance_of(SomeClass, object, 'the message')` over `assert(object.instance_of?(SomeClass), 'the message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_instance_of(SomeClass, object, 'the message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assert_instance_of_method | ||
assert_no_offenses(<<~RUBY, @cop) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_instance_of(SomeClass, object) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |