-
-
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 #248 from koic/make_instance_of_cops_aware_of_equal
Make `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` aware of `assert_equal` and `refute_equal`
- Loading branch information
Showing
7 changed files
with
183 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,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Common functionality for `Minitest/AssertInstanceOf` and `Minitest/RefuteInstanceOf` cops. | ||
# @api private | ||
module InstanceOfAssertionHandleable | ||
include ArgumentRangeHelper | ||
|
||
MSG = 'Prefer using `%<prefer>s`.' | ||
|
||
private | ||
|
||
def investigate(node, assertion_type) | ||
return unless (first_capture, second_capture, message = instance_of_assertion?(node)) | ||
|
||
required_arguments = build_required_arguments(node, assertion_type, first_capture, second_capture) | ||
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 build_required_arguments(node, method_name, first_capture, second_capture) | ||
if node.method?(method_name) | ||
[second_capture, first_capture] | ||
else | ||
[first_capture, second_capture] | ||
end.map(&:source).join(', ') | ||
end | ||
|
||
def replacement_range(node, method_name) | ||
if node.method?(method_name) | ||
node.first_argument | ||
else | ||
first_and_second_arguments_range(node) | ||
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