Skip to content

Commit

Permalink
[Fix rubocop#147] Minitest/GlobalExpectations: add PreferredMethod co…
Browse files Browse the repository at this point in the history
…nfig value

Add a new configuration value for Minitest/GlobalExpectations: PreferredMethod.
This value sets the preferred method with which to correct errors.

Note: This does not alter the set of correct methods, only the replacement for
offenses.

Fixes rubocop#147
  • Loading branch information
gi committed Oct 18, 2021
1 parent d0101f6 commit c1699b9
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#148](https://github.com/rubocop/rubocop-minitest/pull/148): `Minitest/GlobalExpectations`: add `PreferredMethod` config value. ([@gi][])

## 0.15.2 (2021-10-11)

### Bug fixes
Expand Down
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Minitest:
Enabled: true
Include:
- '**/spec/**/*'
- '**/test/**/*'
- '**/*_spec.rb'
- '**/*_test.rb'

Minitest/AssertEmpty:
Expand Down Expand Up @@ -103,6 +105,7 @@ Minitest/GlobalExpectations:
Description: 'This cop checks for deprecated global expectations.'
StyleGuide: 'https://minitest.rubystyle.guide#global-expectations'
Enabled: true
PreferredMethod: _
VersionAdded: '0.7'

Minitest/LiteralAsActualArgument:
Expand Down
42 changes: 42 additions & 0 deletions docs/modules/ROOT/pages/cops_minitest.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ and autocorrects them to use expect format.

=== Examples

==== PreferredMethod: _ (default)

[source,ruby]
----
# bad
Expand All @@ -540,6 +542,46 @@ _(wonts).wont_match expected_wonts
_ { musts }.must_raise TypeError
----

==== PreferredMethod: expect

[source,ruby]
----
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError
# good
expect(musts).must_equal expected_musts
expect(wonts).wont_match expected_wonts
expect { musts }.must_raise TypeError
----

==== PreferredMethod: value

[source,ruby]
----
# bad
musts.must_equal expected_musts
wonts.wont_match expected_wonts
musts.must_raise TypeError
# good
value(musts).must_equal expected_musts
value(wonts).wont_match expected_wonts
value { musts }.must_raise TypeError
----

=== Configurable attributes

|===
| Name | Default value | Configurable values

| PreferredMethod
| `_`
| String
|===

=== References

* https://minitest.rubystyle.guide#global-expectations
Expand Down
36 changes: 31 additions & 5 deletions lib/rubocop/cop/minitest/global_expectations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Minitest
# This cop checks for deprecated global expectations
# and autocorrects them to use expect format.
#
# @example
# @example PreferredMethod: _ (default)
# # bad
# musts.must_equal expected_musts
# wonts.wont_match expected_wonts
Expand All @@ -16,6 +16,28 @@ module Minitest
# _(musts).must_equal expected_musts
# _(wonts).wont_match expected_wonts
# _ { musts }.must_raise TypeError
#
# @example PreferredMethod: expect
# # bad
# musts.must_equal expected_musts
# wonts.wont_match expected_wonts
# musts.must_raise TypeError
#
# # good
# expect(musts).must_equal expected_musts
# expect(wonts).wont_match expected_wonts
# expect { musts }.must_raise TypeError
#
# @example PreferredMethod: value
# # bad
# musts.must_equal expected_musts
# wonts.wont_match expected_wonts
# musts.must_raise TypeError
#
# # good
# value(musts).must_equal expected_musts
# value(wonts).wont_match expected_wonts
# value { musts }.must_raise TypeError
class GlobalExpectations < Base
extend AutoCorrector

Expand Down Expand Up @@ -71,21 +93,25 @@ def on_send(node)
receiver = node.receiver.source_range

if BLOCK_MATCHERS.include?(node.method_name)
corrector.wrap(receiver, '_ { ', ' }')
corrector.wrap(receiver, "#{preferred_method} { ", ' }')
else
corrector.wrap(receiver, '_(', ')')
corrector.wrap(receiver, "#{preferred_method}(", ')')
end
end
end

private

def preferred_method
cop_config['PreferredMethod'] || '_'
end

def preferred_receiver(node)
source = node.receiver.source
if BLOCK_MATCHERS.include?(node.method_name)
"_ { #{source} }"
"#{preferred_method} { #{source} }"
else
"_(#{source})"
"#{preferred_method}(#{source})"
end
end
end
Expand Down

0 comments on commit c1699b9

Please sign in to comment.