-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
Minitest/AssertInstanceOf
and Minitest/RefuteInstanceOf
awar…
…e of `assert_equal` and `refute_equal` This PR makes `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` aware of `assert_equal(Class, object.class)` and `refute_equal(Class, object.class)`.
- Loading branch information
Showing
7 changed files
with
147 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#248](https://github.com/rubocop/rubocop-minitest/pull/248): Make `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` aware of `assert_equal(Class, object.class)` and `refute_equal(Class, object.class)`. ([@koic][]) |
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,50 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Common functionality for `AssertInstanceOf` and `RefuteInstanceOf` cops. | ||
# @api private | ||
module InstanceOfAssertionHandleable | ||
include RangeHelp | ||
|
||
MSG = 'Prefer using `%<prefer>s`.' | ||
|
||
private | ||
|
||
def investigate(node, assertion_type) | ||
return unless (instance, klass, message = instance_of_assertion?(node)) | ||
|
||
required_arguments = required_arguments(node, assertion_type, instance, klass) | ||
|
||
full_arguments = [required_arguments, message.first&.source].compact.join(', ') | ||
|
||
prefer = "#{assertion_type}_instance_of(#{full_arguments})" | ||
|
||
add_offense(node, message: format(MSG, prefer: prefer)) do |corrector| | ||
range = replacement_range(node, assertion_type) | ||
|
||
corrector.replace(node.loc.selector, "#{assertion_type}_instance_of") | ||
corrector.replace(range, required_arguments) | ||
end | ||
end | ||
|
||
def required_arguments(node, assertion_type, instance, klass) | ||
if node.method?(assertion_type) | ||
[klass.source, instance.source] | ||
else | ||
[instance.source, klass.source] | ||
end.join(', ') | ||
end | ||
|
||
def replacement_range(node, assertion_type) | ||
if node.method?(assertion_type) | ||
node.first_argument | ||
else | ||
range_between(node.first_argument.source_range.begin_pos, node.arguments[1].source_range.end_pos) | ||
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