forked from rubocop/rubocop-rspec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
RSpec/MinitestAssertions
cop
Resolve: #1485
- Loading branch information
Showing
8 changed files
with
174 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
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 | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Check if using Minitest matchers. | ||
# | ||
# @example | ||
# # bad | ||
# assert_equal(a, b) | ||
# assert_equal a, b, "must be equal" | ||
# refute_equal(a, b) | ||
# | ||
# # good | ||
# expect(a).to eq(b) | ||
# expect(a).to(eq(b), "must be equal") | ||
# expect(a).not_to eq(b) | ||
# | ||
class MinitestAssertions < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Use `%<prefer>s`.' | ||
RESTRICT_ON_SEND = %i[assert_equal refute_equal].freeze | ||
|
||
# @!method minitest_assertion(node) | ||
def_node_matcher :minitest_assertion, <<-PATTERN | ||
(send nil? {:assert_equal :refute_equal} $_ $_ $_?) | ||
PATTERN | ||
|
||
def on_send(node) | ||
minitest_assertion(node) do |expected, actual, failure_message| | ||
prefer = replacement(node, expected, actual, failure_message.first) | ||
add_offense(node, message: message(prefer)) do |corrector| | ||
corrector.replace(node, prefer) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def replacement(node, expected, actual, failure_message) | ||
runner = node.method?(:assert_equal) ? 'to' : 'not_to' | ||
if failure_message.nil? | ||
"expect(#{expected.source}).#{runner} eq(#{actual.source})" | ||
else | ||
"expect(#{expected.source}).#{runner}(eq(#{actual.source}), " \ | ||
"#{failure_message.source})" | ||
end | ||
end | ||
|
||
def message(prefer) | ||
format(MSG, prefer: prefer) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::RSpec::MinitestAssertions, :config do | ||
it 'registers an offense when using `assert_equal`' do | ||
expect_offense(<<~RUBY) | ||
assert_equal(a, b) | ||
^^^^^^^^^^^^^^^^^^ Use `expect(a).to eq(b)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).to eq(b) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `assert_equal` with no parentheses' do | ||
expect_offense(<<~RUBY) | ||
assert_equal a, b | ||
^^^^^^^^^^^^^^^^^ Use `expect(a).to eq(b)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).to eq(b) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `assert_equal` with failure message' do | ||
expect_offense(<<~RUBY) | ||
assert_equal a, b, "must be equal" | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Use `expect(a).to(eq(b), "must be equal")`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).to(eq(b), "must be equal") | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `assert_equal` with ' \ | ||
'multi-line arguments' do | ||
expect_offense(<<~RUBY) | ||
assert_equal(a, | ||
^^^^^^^^^^^^^^^ Use `expect(a).to(eq(b), "must be equal")`. | ||
b, | ||
"must be equal") | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).to(eq(b), "must be equal") | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when using `refute_equal`' do | ||
expect_offense(<<~RUBY) | ||
refute_equal a, b | ||
^^^^^^^^^^^^^^^^^ Use `expect(a).not_to eq(b)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect(a).not_to eq(b) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `expect(a).to eq(b)`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(a).to eq(b) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using `expect(a).not_to eq(b)`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(a).not_to eq(b) | ||
RUBY | ||
end | ||
end |