forked from rubocop/rubocop-minitest
-
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.
To check against deprecated global expectations. These will be removed in Minitest 6. Closes rubocop#60
- Loading branch information
1 parent
362f517
commit 03fe8e1
Showing
7 changed files
with
170 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Minitest | ||
# This Cop checks for deprecated global expectations | ||
# and autocorrects them to use expect format | ||
# | ||
# @example | ||
# # bad | ||
# n.must_equal 42 | ||
# n.wont_match b | ||
# | ||
# # good | ||
# _(n).must_equal 42 | ||
# _(n).wont_match b | ||
class GlobalExpectations < Cop | ||
MSG = 'Prefer using `%<corrected>s`.' | ||
|
||
VALUE_MATCHERS = %i[ | ||
be be_close_to be_empty be_instance_of be_kind_of | ||
be_nil be_same_as be_silent be_within_epsilon equal | ||
include match respond_to must_exist | ||
].map do |matcher| | ||
[:"must_#{matcher}", :"wont_#{matcher}"] | ||
end.flatten.freeze | ||
|
||
BLOCK_MATCHERS = %i[must_output must_raise must_throw].freeze | ||
|
||
MATCHERS_STR = (VALUE_MATCHERS + BLOCK_MATCHERS).map do |m| | ||
":#{m}" | ||
end.join(' ').freeze | ||
|
||
def_node_matcher :global_matcher?, <<~PATTERN | ||
(send (send _ _) {#{MATCHERS_STR}} ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return unless global_matcher?(node) | ||
|
||
message = format(MSG, corrected: correct_suggestion(node)) | ||
add_offense(node, message: message) | ||
end | ||
|
||
def autocorrect(node) | ||
return unless global_matcher?(node) | ||
|
||
lambda do |corrector| | ||
receiver = node.receiver.loc.selector | ||
|
||
if BLOCK_MATCHERS.include?(node.method_name) | ||
corrector.insert_before(receiver, '_ { ') | ||
corrector.insert_after(receiver, ' }') | ||
else | ||
corrector.insert_before(receiver, '_(') | ||
corrector.insert_after(receiver, ')') | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def correct_suggestion(node) | ||
source = node.receiver.source | ||
if BLOCK_MATCHERS.include?(node.method_name) | ||
node.source.sub(source, "_ { #{source} }") | ||
else | ||
node.source.sub(source, "_(#{source})") | ||
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
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,64 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class GlobalExpectationsTest < Minitest::Test | ||
RuboCop::Cop::Minitest::GlobalExpectations::VALUE_MATCHERS.each do |matcher| | ||
define_method(:"test_registers_offense_when_using_global_#{matcher}") do | ||
assert_offense(<<~RUBY) | ||
it 'does something' do | ||
n.#{matcher} 42 | ||
#{'^' * (matcher.length + 5)} Prefer using `_(n).#{matcher} 42`. | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
it 'does something' do | ||
_(n).#{matcher} 42 | ||
end | ||
RUBY | ||
end | ||
|
||
define_method(:"test_no_offense_when_using_expect_form_of_#{matcher}") do | ||
assert_no_offenses(<<~RUBY) | ||
it 'does something' do | ||
_(n).#{matcher} 42 | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
def test_works_with_chained_method_calls | ||
assert_offense(<<~RUBY) | ||
it 'does something' do | ||
A.foo.bar.must_equal 42 | ||
^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `_(A.foo.bar).must_equal 42`. | ||
end | ||
RUBY | ||
end | ||
|
||
RuboCop::Cop::Minitest::GlobalExpectations::BLOCK_MATCHERS.each do |matcher| | ||
define_method(:"test_registers_offense_when_using_global_#{matcher}") do | ||
assert_offense(<<~RUBY) | ||
it 'does something' do | ||
n.#{matcher} 42 | ||
#{'^' * (matcher.length + 5)} Prefer using `_ { n }.#{matcher} 42`. | ||
end | ||
RUBY | ||
|
||
assert_correction(<<~RUBY) | ||
it 'does something' do | ||
_ { n }.#{matcher} 42 | ||
end | ||
RUBY | ||
end | ||
|
||
define_method(:"test_no_offense_when_using_expect_form_of_#{matcher}") do | ||
assert_no_offenses(<<~RUBY) | ||
it 'does something' do | ||
_ { n }.#{matcher} 42 | ||
end | ||
RUBY | ||
end | ||
end | ||
end |