-
-
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 #260 from koic/add_new_minitest_assert_operator_an…
…d_refute_operator_cops [Fix #255] Add new `Minitest/AssertOperator` and `Minitest/RefuteOperator` cops
- Loading branch information
Showing
8 changed files
with
269 additions
and
4 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
changelog/new_add_new_minitest_assert_operator_and_refute_operator_cops.md
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 @@ | ||
* [#255](https://github.com/rubocop/rubocop-minitest/issues/255): Add new `Minitest/AssertOperator` and `Minitest/RefuteOperator` cops. ([@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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Enforces the use of `assert_operator(expected, :<, actual)` over `assert(expected < actual)`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# assert(expected < actual) | ||
# | ||
# # good | ||
# assert_operator(expected, :<, actual) | ||
# | ||
class AssertOperator < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Prefer using `assert_operator(%<new_arguments>s)`.' | ||
RESTRICT_ON_SEND = %i[assert].freeze | ||
|
||
def on_send(node) | ||
return unless node.first_argument.operator_method? | ||
|
||
new_arguments = build_new_arguments(node) | ||
|
||
add_offense(node, message: format(MSG, new_arguments: new_arguments)) do |corrector| | ||
corrector.replace(node.loc.selector, 'assert_operator') | ||
|
||
corrector.replace(range_of_arguments(node), new_arguments) | ||
end | ||
end | ||
|
||
private | ||
|
||
def build_new_arguments(node) | ||
lhs, op, rhs = *node.first_argument | ||
new_arguments = "#{lhs.source}, :#{op}, #{rhs.source}" | ||
|
||
if node.arguments.count == 2 | ||
new_arguments << ", #{node.last_argument.source}" | ||
else | ||
new_arguments | ||
end | ||
end | ||
|
||
def range_of_arguments(node) | ||
node.first_argument.source_range.begin.join(node.last_argument.source_range.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# Enforces the use of `refute_operator(expected, :<, actual)` over `refute(expected < actual)`. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# refute(expected < actual) | ||
# | ||
# # good | ||
# refute_operator(expected, :<, actual) | ||
# | ||
class RefuteOperator < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Prefer using `refute_operator(%<new_arguments>s)`.' | ||
RESTRICT_ON_SEND = %i[refute].freeze | ||
|
||
def on_send(node) | ||
return unless node.first_argument.operator_method? | ||
|
||
new_arguments = build_new_arguments(node) | ||
|
||
add_offense(node, message: format(MSG, new_arguments: new_arguments)) do |corrector| | ||
corrector.replace(node.loc.selector, 'refute_operator') | ||
|
||
corrector.replace(range_of_arguments(node), new_arguments) | ||
end | ||
end | ||
|
||
private | ||
|
||
def build_new_arguments(node) | ||
lhs, op, rhs = *node.first_argument | ||
new_arguments = "#{lhs.source}, :#{op}, #{rhs.source}" | ||
|
||
if node.arguments.count == 2 | ||
new_arguments << ", #{node.last_argument.source}" | ||
else | ||
new_arguments | ||
end | ||
end | ||
|
||
def range_of_arguments(node) | ||
node.first_argument.source_range.begin.join(node.last_argument.source_range.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../../test_helper' | ||
|
||
class AssertOperatorTest < Minitest::Test | ||
def test_registers_offense_when_using_assert_with_operator_method | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(expected < actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_operator(expected, :<, actual)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_operator(expected, :<, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_with_operator_method_and_message | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert(expected < actual, 'message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_operator(expected, :<, actual, 'message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_operator(expected, :<, actual, 'message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_with_operator_method_without_parentheses | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert expected < actual | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_operator(expected, :<, actual)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_operator expected, :<, actual | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_not_assert_with_operator_method | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_operator(expected, :<, actual) | ||
end | ||
end | ||
RUBY | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../../test_helper' | ||
|
||
class RefuteOperatorTest < Minitest::Test | ||
def test_registers_offense_when_using_refute_with_operator_method | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute(expected < actual) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_operator(expected, :<, actual)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_operator(expected, :<, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_refute_with_operator_method_and_message | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute(expected < actual, 'message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_operator(expected, :<, actual, 'message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_operator(expected, :<, actual, 'message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_refute_with_operator_method_without_parentheses | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute expected < actual | ||
^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `refute_operator(expected, :<, actual)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_operator expected, :<, actual | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_not_refute_with_operator_method | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
refute_operator(expected, :<, actual) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |