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 19, 2021
1 parent 62fef12 commit cefaaa3
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Bug fixes

* [#142](https://github.com/rubocop/rubocop-minitest/issues/142): Fix `Minitest/GlobalExpectations` autocorrect when receiver is lambda. ([@gi][])
### New features

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

## 0.15.2 (2021-10-11)

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
24 changes: 23 additions & 1 deletion 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
Loading

0 comments on commit cefaaa3

Please sign in to comment.