-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Minitest/AssertInDelta
and Minitest/RefuteInDelta
cops
- Loading branch information
Showing
10 changed files
with
355 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
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the test to use `assert_in_delta` | ||
# instead of using `assert_equal` to compare floats. | ||
# | ||
# @example | ||
# # bad | ||
# assert_equal(0.2, actual) | ||
# assert_equal(0.2, actual, 'message') | ||
# | ||
# # good | ||
# assert_in_delta(0.2, actual) | ||
# assert_in_delta(0.2, actual, 0.001, 'message') | ||
# | ||
class AssertInDelta < Cop | ||
include InDeltaMixin | ||
|
||
def_node_matcher :equal_floats_call, <<~PATTERN | ||
(send nil? :assert_equal $_ $_ $...) | ||
PATTERN | ||
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This cop enforces the test to use `refute_in_delta` | ||
# instead of using `refute_equal` to compare floats. | ||
# | ||
# @example | ||
# # bad | ||
# refute_equal(0.2, actual) | ||
# refute_equal(0.2, actual, 'message') | ||
# | ||
# # good | ||
# refute_in_delta(0.2, actual) | ||
# refute_in_delta(0.2, actual, 0.001, 'message') | ||
# | ||
class RefuteInDelta < Cop | ||
include InDeltaMixin | ||
|
||
def_node_matcher :equal_floats_call, <<~PATTERN | ||
(send nil? :refute_equal $_ $_ $...) | ||
PATTERN | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
# Common functionality for `AssertInDelta` and `RefuteInDelta` cops. | ||
module InDeltaMixin | ||
MSG = 'Prefer using `%<good_method>s` over `%<bad_method>s`.' | ||
|
||
def on_send(node) | ||
equal_floats_call(node) do |expected, actual, message| | ||
message = message.first | ||
|
||
if expected.float_type? || actual.float_type? | ||
message = format(MSG, | ||
good_method: build_good_method(expected, actual, message), | ||
bad_method: build_bad_method(expected, actual, message)) | ||
|
||
add_offense(node, message: message) | ||
end | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
equal_floats_call(node) do |expected, actual, message| | ||
message = message.first | ||
replacement = build_good_method(expected, actual, message) | ||
|
||
lambda do |corrector| | ||
corrector.replace(node, replacement) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def build_good_method(expected, actual, message) | ||
if message | ||
"#{assertion_method}_in_delta(#{expected.source}, #{actual.source}, 0.001, #{message.source})" | ||
else | ||
"#{assertion_method}_in_delta(#{expected.source}, #{actual.source})" | ||
end | ||
end | ||
|
||
def build_bad_method(expected, actual, message) | ||
arguments = [expected.source, actual.source, message&.source].compact.join(', ') | ||
"#{assertion_method}_equal(#{arguments})" | ||
end | ||
|
||
def assertion_method | ||
class_name = self.class.name.split('::').last | ||
class_name[/\A[[:upper:]][[:lower:]]+/].downcase | ||
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,82 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AssertInDeltaTest < Minitest::Test | ||
def test_registers_offense_when_using_assert_equal_with_float_as_expected_value | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_equal(0.2, foo) | ||
^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_in_delta(0.2, foo)` over `assert_equal(0.2, foo)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_in_delta(0.2, foo) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_equal_with_float_as_actual_value | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_equal(foo, 0.2) | ||
^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_in_delta(foo, 0.2)` over `assert_equal(foo, 0.2)`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_in_delta(foo, 0.2) | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_registers_offense_when_using_assert_equal_with_float_and_message | ||
assert_offense(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_equal(0.2, foo, 'message') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_in_delta(0.2, foo, 0.001, 'message')` over `assert_equal(0.2, foo, 'message')`. | ||
end | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_in_delta(0.2, foo, 0.001, 'message') | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assert_equal_with_floats | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_equal foo, bar | ||
end | ||
end | ||
RUBY | ||
end | ||
|
||
def test_does_not_register_offense_when_using_assert_in_delta_method | ||
assert_no_offenses(<<~RUBY) | ||
class FooTest < Minitest::Test | ||
def test_do_something | ||
assert_in_delta(foo, 0.2) | ||
end | ||
end | ||
RUBY | ||
end | ||
end |
Oops, something went wrong.