rubocop-minitest 0.17.2 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b59e5fd9ae4f6ad15bcc96b2e615eb8aa132b653cfe99c62eb8874186b88d38
4
- data.tar.gz: 710f664f0704ffd649d4741729d6c6c5cab86222c8b79540c553160ff30a0d68
3
+ metadata.gz: add241b07281db4494e25119722057177e5eb15056360b0907d4b58819b6f371
4
+ data.tar.gz: 0dbe3916ea796caa3c59e04df1b13ade8a11c25b7613187308b3ea90348380fa
5
5
  SHA512:
6
- metadata.gz: 727cc013e57b69f43d5796e37d62521ed7880dfc961ef59f5df0cd1a48d2a2bb8a7f6be5698efa0b9021f01097065f4d09ada983c0e36e3455ed3f36af94954b
7
- data.tar.gz: f3b1f390ff8a682c5866871ad83887c7cd891eb307261bfeb46aee8d44ff1ed081945d7daa279035cebb2730c0d797ce0514758907a81b08a74a712766ec293d
6
+ metadata.gz: 4ab3dd3b82e55154731f600b942f2ef3dd48e85bf55d8d5d09c94bcb5227e409ea201c45be221cfde00cdb434d4877508bfad26cc7d3d3427c531c0babf146c1
7
+ data.tar.gz: 261d27488e8ee453f9307bb4367a7072dddb544c0dd73c5ac4e70e033875be7916519cf346ede3c5956023d81e5cf0f22b6ab284c85650a8987b0fa95c77f18d
@@ -0,0 +1,15 @@
1
+ name: Linting
2
+ on:
3
+ - pull_request
4
+ jobs:
5
+ yamllint:
6
+ name: Yamllint
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Yamllint
11
+ uses: karancode/yamllint-github-action@master
12
+ with:
13
+ yamllint_comment: true
14
+ env:
15
+ GITHUB_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
data/.rubocop.yml CHANGED
@@ -55,13 +55,13 @@ Layout/ClassStructure:
55
55
  - prepend
56
56
  - extend
57
57
  ExpectedOrder:
58
- - module_inclusion
59
- - constants
60
- - public_class_methods
61
- - initializer
62
- - instance_methods
63
- - protected_methods
64
- - private_methods
58
+ - module_inclusion
59
+ - constants
60
+ - public_class_methods
61
+ - initializer
62
+ - instance_methods
63
+ - protected_methods
64
+ - private_methods
65
65
 
66
66
  # Trailing white space is meaningful in code examples
67
67
  Layout/TrailingWhitespace:
data/.yamllint.yml ADDED
@@ -0,0 +1,9 @@
1
+ extends: default
2
+
3
+ rules:
4
+ comments:
5
+ min-spaces-from-content: 1
6
+ document-start: disable
7
+ line-length: disable
8
+ truthy:
9
+ check-keys: false
data/CHANGELOG.md CHANGED
@@ -2,6 +2,16 @@
2
2
 
3
3
  ## master (unreleased)
4
4
 
5
+ ## 0.18.0 (2022-03-13)
6
+
7
+ ### New features
8
+
9
+ * [#161](https://github.com/rubocop/rubocop-minitest/pull/161): Add new `Minitest/AssertPredicate` and `Minitest/RefutePredicate` cops. ([@koic][])
10
+
11
+ ### Changes
12
+
13
+ * [#162](https://github.com/rubocop/rubocop-minitest/pull/162): Make `Minitest/AssertNil` (`Minitest/RefuteNil`) aware of `assert_predicate(obj, :nil?)` (`refute_predicate(obj, :nil?)`). ([@koic][])
14
+
5
15
  ## 0.17.2 (2022-02-12)
6
16
 
7
17
  ### Bug fixes
data/config/default.yml CHANGED
@@ -81,6 +81,12 @@ Minitest/AssertRespondTo:
81
81
  Enabled: true
82
82
  VersionAdded: '0.3'
83
83
 
84
+ Minitest/AssertPredicate:
85
+ Description: 'This cop enforces the test to use `assert_predicate` instead of using `assert(obj.a_predicate_method?)`.'
86
+ StyleGuide: 'https://minitest.rubystyle.guide/#assert-predicate'
87
+ Enabled: pending
88
+ VersionAdded: '0.18'
89
+
84
90
  Minitest/AssertSilent:
85
91
  Description: "This cop enforces the test to use `assert_silent { ... }` instead of using `assert_output('', '') { ... }`."
86
92
  StyleGuide: 'https://github.com/rubocop/minitest-style-guide#assert-silent'
@@ -194,6 +200,12 @@ Minitest/RefutePathExists:
194
200
  Enabled: 'pending'
195
201
  VersionAdded: '0.10'
196
202
 
203
+ Minitest/RefutePredicate:
204
+ Description: 'This cop enforces the test to use `refute_predicate` instead of using `refute(obj.a_predicate_method?)`.'
205
+ StyleGuide: 'https://minitest.rubystyle.guide/#refute-predicate'
206
+ Enabled: pending
207
+ VersionAdded: '0.18'
208
+
197
209
  Minitest/RefuteRespondTo:
198
210
  Description: 'This cop enforces the test to use `refute_respond_to(object, :do_something)` over `refute(object.respond_to?(:do_something))`.'
199
211
  StyleGuide: 'https://minitest.rubystyle.guide#refute-respond-to'
data/docs/antora.yml CHANGED
@@ -2,6 +2,6 @@ name: rubocop-minitest
2
2
  title: RuboCop Minitest
3
3
  # We always provide version without patch here (e.g. 1.1),
4
4
  # as patch versions should not appear in the docs.
5
- version: '0.17'
5
+ version: '0.18'
6
6
  nav:
7
- - modules/ROOT/nav.adoc
7
+ - modules/ROOT/nav.adoc
@@ -24,6 +24,7 @@ based on the https://minitest.rubystyle.guide/[Minitest Style Guide].
24
24
  * xref:cops_minitest.adoc#minitestassertnil[Minitest/AssertNil]
25
25
  * xref:cops_minitest.adoc#minitestassertoutput[Minitest/AssertOutput]
26
26
  * xref:cops_minitest.adoc#minitestassertpathexists[Minitest/AssertPathExists]
27
+ * xref:cops_minitest.adoc#minitestassertpredicate[Minitest/AssertPredicate]
27
28
  * xref:cops_minitest.adoc#minitestassertrespondto[Minitest/AssertRespondTo]
28
29
  * xref:cops_minitest.adoc#minitestassertsilent[Minitest/AssertSilent]
29
30
  * xref:cops_minitest.adoc#minitestasserttruthy[Minitest/AssertTruthy]
@@ -43,6 +44,7 @@ based on the https://minitest.rubystyle.guide/[Minitest Style Guide].
43
44
  * xref:cops_minitest.adoc#minitestrefutematch[Minitest/RefuteMatch]
44
45
  * xref:cops_minitest.adoc#minitestrefutenil[Minitest/RefuteNil]
45
46
  * xref:cops_minitest.adoc#minitestrefutepathexists[Minitest/RefutePathExists]
47
+ * xref:cops_minitest.adoc#minitestrefutepredicate[Minitest/RefutePredicate]
46
48
  * xref:cops_minitest.adoc#minitestrefuterespondto[Minitest/RefuteRespondTo]
47
49
  * xref:cops_minitest.adoc#minitesttestmethodname[Minitest/TestMethodName]
48
50
  * xref:cops_minitest.adoc#minitestunreachableassertion[Minitest/UnreachableAssertion]
@@ -262,7 +262,7 @@ assert_match(matcher, string, 'message')
262
262
  |===
263
263
 
264
264
  This cop enforces the test to use `assert_nil` instead of using
265
- `assert_equal(nil, something)` or `assert(something.nil?)`.
265
+ `assert_equal(nil, something)`, `assert(something.nil?)`, or `assert_predicate(something, :nil?)`.
266
266
 
267
267
  === Examples
268
268
 
@@ -273,6 +273,8 @@ assert_equal(nil, actual)
273
273
  assert_equal(nil, actual, 'message')
274
274
  assert(object.nil?)
275
275
  assert(object.nil?, 'message')
276
+ assert_predicate(object, :nil?)
277
+ assert_predicate(object, :nil?, 'message')
276
278
 
277
279
  # good
278
280
  assert_nil(actual)
@@ -347,6 +349,38 @@ assert_path_exists(path, 'message')
347
349
 
348
350
  * https://minitest.rubystyle.guide/#assert-path-exists
349
351
 
352
+ == Minitest/AssertPredicate
353
+
354
+ |===
355
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
356
+
357
+ | Pending
358
+ | Yes
359
+ | Yes
360
+ | 0.18
361
+ | -
362
+ |===
363
+
364
+ This cop enforces the test to use `assert_predicate`
365
+ instead of using `assert(obj.a_predicate_method?)`.
366
+
367
+ === Examples
368
+
369
+ [source,ruby]
370
+ ----
371
+ # bad
372
+ assert(obj.one?)
373
+ assert(obj.one?, 'message')
374
+
375
+ # good
376
+ assert_predicate(obj, :one?)
377
+ assert_predicate(obj, :one?, 'message')
378
+ ----
379
+
380
+ === References
381
+
382
+ * https://minitest.rubystyle.guide/#assert-predicate
383
+
350
384
  == Minitest/AssertRespondTo
351
385
 
352
386
  |===
@@ -1025,7 +1059,7 @@ refute_match(matcher, string, 'message')
1025
1059
  |===
1026
1060
 
1027
1061
  This cop enforces the test to use `refute_nil` instead of using
1028
- `refute_equal(nil, something)` or `refute(something.nil?)`.
1062
+ `refute_equal(nil, something)`, `refute(something.nil?)`, or `refute_predicate(something, :nil?)`.
1029
1063
 
1030
1064
  === Examples
1031
1065
 
@@ -1036,6 +1070,8 @@ refute_equal(nil, actual)
1036
1070
  refute_equal(nil, actual, 'message')
1037
1071
  refute(actual.nil?)
1038
1072
  refute(actual.nil?, 'message')
1073
+ refute_predicate(object, :nil?)
1074
+ refute_predicate(object, :nil?, 'message')
1039
1075
 
1040
1076
  # good
1041
1077
  refute_nil(actual)
@@ -1078,6 +1114,38 @@ refute_path_exists(path, 'message')
1078
1114
 
1079
1115
  * https://minitest.rubystyle.guide/#refute-path-exists
1080
1116
 
1117
+ == Minitest/RefutePredicate
1118
+
1119
+ |===
1120
+ | Enabled by default | Safe | Supports autocorrection | Version Added | Version Changed
1121
+
1122
+ | Pending
1123
+ | Yes
1124
+ | Yes
1125
+ | 0.18
1126
+ | -
1127
+ |===
1128
+
1129
+ This cop enforces the test to use `refute_predicate`
1130
+ instead of using `refute(obj.a_predicate_method?)`.
1131
+
1132
+ === Examples
1133
+
1134
+ [source,ruby]
1135
+ ----
1136
+ # bad
1137
+ refute(obj.one?)
1138
+ refute(obj.one?, 'message')
1139
+
1140
+ # good
1141
+ refute_predicate(obj, :one?)
1142
+ refute_predicate(obj, :one?, 'message')
1143
+ ----
1144
+
1145
+ === References
1146
+
1147
+ * https://minitest.rubystyle.guide/#refute-predicate
1148
+
1081
1149
  == Minitest/RefuteRespondTo
1082
1150
 
1083
1151
  |===
@@ -18,8 +18,7 @@ module RuboCop
18
18
  include ArgumentRangeHelper
19
19
  extend AutoCorrector
20
20
 
21
- MSG = 'Prefer using `assert_empty(%<arguments>s)` over ' \
22
- '`assert_equal(%<literal>s, %<arguments>s)`.'
21
+ MSG = 'Prefer using `assert_empty(%<arguments>s)`.'
23
22
  RESTRICT_ON_SEND = %i[assert_equal].freeze
24
23
 
25
24
  def_node_matcher :assert_equal_with_empty_literal, <<~PATTERN
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
6
  # This cop enforces the test to use `assert_nil` instead of using
7
- # `assert_equal(nil, something)` or `assert(something.nil?)`.
7
+ # `assert_equal(nil, something)`, `assert(something.nil?)`, or `assert_predicate(something, :nil?)`.
8
8
  #
9
9
  # @example
10
10
  # # bad
@@ -12,6 +12,8 @@ module RuboCop
12
12
  # assert_equal(nil, actual, 'message')
13
13
  # assert(object.nil?)
14
14
  # assert(object.nil?, 'message')
15
+ # assert_predicate(object, :nil?)
16
+ # assert_predicate(object, :nil?, 'message')
15
17
  #
16
18
  # # good
17
19
  # assert_nil(actual)
@@ -23,12 +25,13 @@ module RuboCop
23
25
  extend AutoCorrector
24
26
 
25
27
  ASSERTION_TYPE = 'assert'
26
- RESTRICT_ON_SEND = %i[assert_equal assert].freeze
28
+ RESTRICT_ON_SEND = %i[assert assert_equal assert_predicate].freeze
27
29
 
28
30
  def_node_matcher :nil_assertion, <<~PATTERN
29
31
  {
30
32
  (send nil? :assert_equal nil $_ $...)
31
33
  (send nil? :assert (send $_ :nil?) $...)
34
+ (send nil? :assert_predicate $_ (sym :nil?) $...)
32
35
  }
33
36
  PATTERN
34
37
 
@@ -18,7 +18,7 @@ module RuboCop
18
18
  class AssertPathExists < Base
19
19
  extend AutoCorrector
20
20
 
21
- MSG = 'Prefer using `%<good_method>s` over `%<bad_method>s`.'
21
+ MSG = 'Prefer using `%<good_method>s`.'
22
22
  RESTRICT_ON_SEND = %i[assert].freeze
23
23
 
24
24
  def_node_matcher :assert_file_exists, <<~PATTERN
@@ -32,7 +32,7 @@ module RuboCop
32
32
  assert_file_exists(node) do |path, failure_message|
33
33
  failure_message = failure_message.first
34
34
  good_method = build_good_method(path, failure_message)
35
- message = format(MSG, good_method: good_method, bad_method: node.source)
35
+ message = format(MSG, good_method: good_method)
36
36
 
37
37
  add_offense(node, message: message) do |corrector|
38
38
  corrector.replace(node, good_method)
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces the test to use `assert_predicate`
7
+ # instead of using `assert(obj.a_predicate_method?)`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # assert(obj.one?)
12
+ # assert(obj.one?, 'message')
13
+ #
14
+ # # good
15
+ # assert_predicate(obj, :one?)
16
+ # assert_predicate(obj, :one?, 'message')
17
+ #
18
+ class AssertPredicate < Base
19
+ include ArgumentRangeHelper
20
+ include PredicateAssertionHandleable
21
+ extend AutoCorrector
22
+
23
+ MSG = 'Prefer using `assert_predicate(%<new_arguments>s)`.'
24
+ RESTRICT_ON_SEND = %i[assert].freeze
25
+
26
+ private
27
+
28
+ def assertion_type
29
+ 'assert'
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -16,7 +16,7 @@ module RuboCop
16
16
  class AssertSilent < Base
17
17
  extend AutoCorrector
18
18
 
19
- MSG = 'Prefer using `assert_silent` over `assert_output("", "")`.'
19
+ MSG = 'Prefer using `assert_silent`.'
20
20
 
21
21
  def_node_matcher :assert_silent_candidate?, <<~PATTERN
22
22
  (block
@@ -19,8 +19,7 @@ module RuboCop
19
19
  include ArgumentRangeHelper
20
20
  extend AutoCorrector
21
21
 
22
- MSG = 'Prefer using `assert(%<arguments>s)` over ' \
23
- '`assert_equal(true, %<arguments>s)`.'
22
+ MSG = 'Prefer using `assert(%<arguments>s)`.'
24
23
  RESTRICT_ON_SEND = %i[assert_equal].freeze
25
24
 
26
25
  def_node_matcher :assert_equal_with_truthy, <<~PATTERN
@@ -18,20 +18,19 @@ module RuboCop
18
18
  include ArgumentRangeHelper
19
19
  extend AutoCorrector
20
20
 
21
- MSG = 'Prefer using `refute_equal(%<preferred>s)` over ' \
22
- '`assert(%<over>s)`.'
21
+ MSG = 'Prefer using `refute_equal(%<preferred>s)`.'
23
22
  RESTRICT_ON_SEND = %i[assert].freeze
24
23
 
25
24
  def_node_matcher :assert_not_equal, <<~PATTERN
26
- (send nil? :assert ${(send $_ :!= $_) (send (send $_ :! ) :== $_) } $... )
25
+ (send nil? :assert {(send $_ :!= $_) (send (send $_ :! ) :== $_) } $... )
27
26
  PATTERN
28
27
 
29
28
  def on_send(node)
30
- preferred, over = process_not_equal(node)
31
- return unless preferred && over
29
+ preferred = process_not_equal(node)
30
+ return unless preferred
32
31
 
33
- assert_not_equal(node) do |_, expected, actual|
34
- message = format(MSG, preferred: preferred, over: over)
32
+ assert_not_equal(node) do |expected, actual|
33
+ message = format(MSG, preferred: preferred)
35
34
 
36
35
  add_offense(node, message: message) do |corrector|
37
36
  corrector.replace(node.loc.selector, 'refute_equal')
@@ -54,11 +53,10 @@ module RuboCop
54
53
  end
55
54
 
56
55
  def process_not_equal(node)
57
- assert_not_equal(node) do |over, first_arg, second_arg, rest_args|
56
+ assert_not_equal(node) do |first_arg, second_arg, rest_args|
58
57
  custom_message = rest_args.first
59
- preferred = preferred_usage(first_arg, second_arg, custom_message)
60
- over = original_usage(over.source, custom_message&.source)
61
- return [preferred, over]
58
+
59
+ preferred_usage(first_arg, second_arg, custom_message)
62
60
  end
63
61
  end
64
62
  end
@@ -22,8 +22,8 @@ module RuboCop
22
22
  include ArgumentRangeHelper
23
23
  extend AutoCorrector
24
24
 
25
- MSG_FOR_ASSERT_EQUAL = 'Prefer using `refute(%<arguments>s)` over `assert_equal(false, %<arguments>s)`.'
26
- MSG_FOR_ASSERT = 'Prefer using `refute(%<arguments>s)` over `assert(!%<arguments>s)`.'
25
+ MSG_FOR_ASSERT_EQUAL = 'Prefer using `refute(%<arguments>s)`.'
26
+ MSG_FOR_ASSERT = 'Prefer using `refute(%<arguments>s)`.'
27
27
  RESTRICT_ON_SEND = %i[assert_equal assert].freeze
28
28
 
29
29
  def_node_matcher :assert_equal_with_false, <<~PATTERN
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
6
  # This cop enforces the test to use `refute_nil` instead of using
7
- # `refute_equal(nil, something)` or `refute(something.nil?)`.
7
+ # `refute_equal(nil, something)`, `refute(something.nil?)`, or `refute_predicate(something, :nil?)`.
8
8
  #
9
9
  # @example
10
10
  # # bad
@@ -12,6 +12,8 @@ module RuboCop
12
12
  # refute_equal(nil, actual, 'message')
13
13
  # refute(actual.nil?)
14
14
  # refute(actual.nil?, 'message')
15
+ # refute_predicate(object, :nil?)
16
+ # refute_predicate(object, :nil?, 'message')
15
17
  #
16
18
  # # good
17
19
  # refute_nil(actual)
@@ -23,12 +25,13 @@ module RuboCop
23
25
  extend AutoCorrector
24
26
 
25
27
  ASSERTION_TYPE = 'refute'
26
- RESTRICT_ON_SEND = %i[refute_equal refute].freeze
28
+ RESTRICT_ON_SEND = %i[refute refute_equal refute_predicate].freeze
27
29
 
28
30
  def_node_matcher :nil_refutation, <<~PATTERN
29
31
  {
30
32
  (send nil? :refute_equal nil $_ $...)
31
33
  (send nil? :refute (send $_ :nil?) $...)
34
+ (send nil? :refute_predicate $_ (sym :nil?) $...)
32
35
  }
33
36
  PATTERN
34
37
 
@@ -18,7 +18,7 @@ module RuboCop
18
18
  class RefutePathExists < Base
19
19
  extend AutoCorrector
20
20
 
21
- MSG = 'Prefer using `%<good_method>s` over `%<bad_method>s`.'
21
+ MSG = 'Prefer using `%<good_method>s`.'
22
22
  RESTRICT_ON_SEND = %i[refute].freeze
23
23
 
24
24
  def_node_matcher :refute_file_exists, <<~PATTERN
@@ -32,7 +32,7 @@ module RuboCop
32
32
  refute_file_exists(node) do |path, failure_message|
33
33
  failure_message = failure_message.first
34
34
  good_method = build_good_method(path, failure_message)
35
- message = format(MSG, good_method: good_method, bad_method: node.source)
35
+ message = format(MSG, good_method: good_method)
36
36
 
37
37
  add_offense(node, message: message) do |corrector|
38
38
  corrector.replace(node, good_method)
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # This cop enforces the test to use `refute_predicate`
7
+ # instead of using `refute(obj.a_predicate_method?)`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # refute(obj.one?)
12
+ # refute(obj.one?, 'message')
13
+ #
14
+ # # good
15
+ # refute_predicate(obj, :one?)
16
+ # refute_predicate(obj, :one?, 'message')
17
+ #
18
+ class RefutePredicate < Base
19
+ include ArgumentRangeHelper
20
+ include PredicateAssertionHandleable
21
+ extend AutoCorrector
22
+
23
+ MSG = 'Prefer using `refute_predicate(%<new_arguments>s)`.'
24
+ RESTRICT_ON_SEND = %i[refute].freeze
25
+
26
+ private
27
+
28
+ def assertion_type
29
+ 'refute'
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -5,10 +5,12 @@ require_relative 'mixin/in_delta_mixin'
5
5
  require_relative 'mixin/minitest_cop_rule'
6
6
  require_relative 'mixin/minitest_exploration_helpers'
7
7
  require_relative 'mixin/nil_assertion_handleable'
8
+ require_relative 'mixin/predicate_assertion_handleable'
8
9
  require_relative 'minitest/assert_empty'
9
10
  require_relative 'minitest/assert_empty_literal'
10
11
  require_relative 'minitest/assert_equal'
11
12
  require_relative 'minitest/assert_in_delta'
13
+ require_relative 'minitest/assert_predicate'
12
14
  require_relative 'minitest/assert_with_expected_argument'
13
15
  require_relative 'minitest/assertion_in_lifecycle_hook'
14
16
  require_relative 'minitest/assert_kind_of'
@@ -35,6 +37,7 @@ require_relative 'minitest/refute_includes'
35
37
  require_relative 'minitest/refute_match'
36
38
  require_relative 'minitest/refute_instance_of'
37
39
  require_relative 'minitest/refute_path_exists'
40
+ require_relative 'minitest/refute_predicate'
38
41
  require_relative 'minitest/refute_respond_to'
39
42
  require_relative 'minitest/test_method_name'
40
43
  require_relative 'minitest/unreachable_assertion'
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Cop
5
5
  # Common functionality for `AssertInDelta` and `RefuteInDelta` cops.
6
6
  module InDeltaMixin
7
- MSG = 'Prefer using `%<good_method>s` over `%<bad_method>s`.'
7
+ MSG = 'Prefer using `%<good_method>s`.'
8
8
 
9
9
  def on_send(node)
10
10
  equal_floats_call(node) do |expected, actual, message|
@@ -12,7 +12,7 @@ module RuboCop
12
12
  good_method = build_good_method(expected, actual, message)
13
13
 
14
14
  if expected.float_type? || actual.float_type?
15
- message = format(MSG, good_method: good_method, bad_method: node.source)
15
+ message = format(MSG, good_method: good_method)
16
16
 
17
17
  add_offense(node, message: message) do |corrector|
18
18
  corrector.replace(node, good_method)
@@ -28,8 +28,7 @@ module RuboCop
28
28
  include ArgumentRangeHelper
29
29
  extend AutoCorrector
30
30
 
31
- MSG = 'Prefer using `#{preferred_method}(%<new_arguments>s)` over ' \
32
- '`#{assertion_method}(%<original_arguments>s)`.'
31
+ MSG = 'Prefer using `#{preferred_method}(%<new_arguments>s)`.'
33
32
  RESTRICT_ON_SEND = %i[#{assertion_method}].freeze
34
33
 
35
34
  def on_send(node)
@@ -70,12 +69,9 @@ module RuboCop
70
69
  message_argument&.source
71
70
  ].flatten.compact.join(', ')
72
71
 
73
- original_arguments = arguments.map(&:source).join(', ')
74
-
75
72
  format(
76
73
  MSG,
77
- new_arguments: new_arguments,
78
- original_arguments: original_arguments
74
+ new_arguments: new_arguments
79
75
  )
80
76
  end
81
77
 
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Minitest
6
6
  # Common functionality for `AssertNil` and `RefuteNil` cops.
7
7
  module NilAssertionHandleable
8
- MSG = 'Prefer using `%<assertion_type>s_nil(%<preferred_args>s)` over `%<method>s(%<current_args>s)`.'
8
+ MSG = 'Prefer using `%<assertion_type>s_nil(%<preferred_args>s)`.'
9
9
 
10
10
  private
11
11
 
@@ -22,23 +22,18 @@ module RuboCop
22
22
  message_source = message&.source
23
23
 
24
24
  preferred_args = [actual.source, message_source].compact
25
- current_args = if comparison_assertion_method?(node)
26
- ['nil', preferred_args].join(', ')
27
- else
28
- ["#{actual.source}.nil?", message_source].compact.join(', ')
29
- end
30
25
 
31
26
  format(
32
27
  MSG,
33
28
  assertion_type: assertion_type,
34
29
  preferred_args: preferred_args.join(', '),
35
- method: node.method_name, current_args: current_args
30
+ method: node.method_name
36
31
  )
37
32
  end
38
33
 
39
34
  def autocorrect(corrector, node, actual)
40
35
  corrector.replace(node.loc.selector, :"#{assertion_type}_nil")
41
- if comparison_assertion_method?(node)
36
+ if comparison_or_predicate_assertion_method?(node)
42
37
  corrector.replace(first_and_second_arguments_range(node), actual.source)
43
38
  else
44
39
  corrector.remove(node.first_argument.loc.dot)
@@ -46,8 +41,8 @@ module RuboCop
46
41
  end
47
42
  end
48
43
 
49
- def comparison_assertion_method?(node)
50
- node.method?(:"#{assertion_type}_equal")
44
+ def comparison_or_predicate_assertion_method?(node)
45
+ node.method?(:"#{assertion_type}_equal") || node.method?(:"#{assertion_type}_predicate")
51
46
  end
52
47
  end
53
48
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Minitest
6
+ # Common functionality for `Minitest/AssertPredicate` and `Minitest/RefutePredicate` cops.
7
+ module PredicateAssertionHandleable
8
+ MSG = 'Prefer using `%<assertion_type>s_predicate(%<new_arguments>s)`.'
9
+ RESTRICT_ON_SEND = %i[assert].freeze
10
+
11
+ def on_send(node)
12
+ return unless (arguments = peel_redundant_parentheses_from(node.arguments))
13
+ return unless arguments.first.respond_to?(:predicate_method?) && arguments.first.predicate_method?
14
+ return unless arguments.first.arguments.count.zero?
15
+
16
+ add_offense(node, message: offense_message(arguments)) do |corrector|
17
+ autocorrect(corrector, node, arguments)
18
+ end
19
+ end
20
+
21
+ def autocorrect(corrector, node, arguments)
22
+ corrector.replace(node.loc.selector, "#{assertion_type}_predicate")
23
+
24
+ new_arguments = new_arguments(arguments).join(', ')
25
+
26
+ corrector.replace(first_argument_range(node), new_arguments)
27
+ end
28
+
29
+ private
30
+
31
+ def peel_redundant_parentheses_from(arguments)
32
+ return arguments unless arguments.first&.begin_type?
33
+
34
+ peel_redundant_parentheses_from(arguments.first.children)
35
+ end
36
+
37
+ def offense_message(arguments)
38
+ message_argument = arguments.last if arguments.first != arguments.last
39
+
40
+ new_arguments = [
41
+ new_arguments(arguments),
42
+ message_argument&.source
43
+ ].flatten.compact.join(', ')
44
+
45
+ format(MSG, assertion_type: assertion_type, new_arguments: new_arguments)
46
+ end
47
+
48
+ def new_arguments(arguments)
49
+ receiver = correct_receiver(arguments.first.receiver)
50
+ method_name = arguments.first.method_name
51
+
52
+ [receiver, ":#{method_name}"]
53
+ end
54
+
55
+ def correct_receiver(receiver)
56
+ receiver ? receiver.source : 'self'
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -22,7 +22,7 @@ module RuboCop
22
22
  # class FooTest < Minitest::Test
23
23
  # def test_do_something
24
24
  # assert_equal(nil, somestuff)
25
- # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)` over `assert_equal(nil, somestuff)`.
25
+ # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
26
26
  # end
27
27
  # end
28
28
  # RUBY
@@ -36,7 +36,7 @@ module RuboCop
36
36
  # class FooTest < Minitest::Test
37
37
  # def test_do_something
38
38
  # assert_equal(nil, somestuff)
39
- # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)` over `assert_equal(nil, somestuff)`.
39
+ # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
40
40
  # end
41
41
  # end
42
42
  # RUBY
@@ -64,7 +64,7 @@ module RuboCop
64
64
  # class FooTest < Minitest::Test
65
65
  # def test_do_something
66
66
  # assert_equal(nil, somestuff)
67
- # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)` over `assert_equal(nil, somestuff)`.
67
+ # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer using `assert_nil(somestuff)`.
68
68
  # end
69
69
  # end
70
70
  # RUBY
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Minitest
5
5
  # This module holds the RuboCop Minitest version information.
6
6
  module Version
7
- STRING = '0.17.2'
7
+ STRING = '0.18.0'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
data/mkdocs.yml CHANGED
@@ -4,10 +4,10 @@ edit_uri: edit/master/legacy-docs/
4
4
  copyright: "Copyright &copy; 2022 Bozhidar Batsov, Jonas Arvidsson, Koichi ITO, and RuboCop contributors"
5
5
  docs_dir: legacy-docs
6
6
  pages:
7
- - Home: index.md
8
- - Installation: installation.md
9
- - Usage: usage.md
10
- - Cops: cops.md
11
- - Cops Documentation:
12
- - Minitest Cops: cops_minitest.md
7
+ - Home: index.md
8
+ - Installation: installation.md
9
+ - Usage: usage.md
10
+ - Cops: cops.md
11
+ - Cops Documentation:
12
+ - Minitest Cops: cops_minitest.md
13
13
  theme: readthedocs
@@ -0,0 +1,9 @@
1
+ ### New features
2
+
3
+ * [#161](https://github.com/rubocop/rubocop-minitest/pull/161): Add new `Minitest/AssertPredicate` and `Minitest/RefutePredicate` cops. ([@koic][])
4
+
5
+ ### Changes
6
+
7
+ * [#162](https://github.com/rubocop/rubocop-minitest/pull/162): Make `Minitest/AssertNil` (`Minitest/RefuteNil`) aware of `assert_predicate(obj, :nil?)` (`refute_predicate(obj, :nil?)`). ([@koic][])
8
+
9
+ [@koic]: https://github.com/koic
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-minitest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.2
4
+ version: 0.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2022-02-12 00:00:00.000000000 Z
13
+ date: 2022-03-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rubocop
@@ -60,10 +60,12 @@ files:
60
60
  - ".github/ISSUE_TEMPLATE/bug_report.md"
61
61
  - ".github/ISSUE_TEMPLATE/feature_request.md"
62
62
  - ".github/PULL_REQUEST_TEMPLATE.md"
63
+ - ".github/workflows/linting.yml"
63
64
  - ".github/workflows/spell_checking.yml"
64
65
  - ".gitignore"
65
66
  - ".rubocop.yml"
66
67
  - ".rubocop_todo.yml"
68
+ - ".yamllint.yml"
67
69
  - ".yardopts"
68
70
  - CHANGELOG.md
69
71
  - CONTRIBUTING.md
@@ -100,6 +102,7 @@ files:
100
102
  - lib/rubocop/cop/minitest/assert_nil.rb
101
103
  - lib/rubocop/cop/minitest/assert_output.rb
102
104
  - lib/rubocop/cop/minitest/assert_path_exists.rb
105
+ - lib/rubocop/cop/minitest/assert_predicate.rb
103
106
  - lib/rubocop/cop/minitest/assert_respond_to.rb
104
107
  - lib/rubocop/cop/minitest/assert_silent.rb
105
108
  - lib/rubocop/cop/minitest/assert_truthy.rb
@@ -119,6 +122,7 @@ files:
119
122
  - lib/rubocop/cop/minitest/refute_match.rb
120
123
  - lib/rubocop/cop/minitest/refute_nil.rb
121
124
  - lib/rubocop/cop/minitest/refute_path_exists.rb
125
+ - lib/rubocop/cop/minitest/refute_predicate.rb
122
126
  - lib/rubocop/cop/minitest/refute_respond_to.rb
123
127
  - lib/rubocop/cop/minitest/test_method_name.rb
124
128
  - lib/rubocop/cop/minitest/unreachable_assertion.rb
@@ -129,6 +133,7 @@ files:
129
133
  - lib/rubocop/cop/mixin/minitest_cop_rule.rb
130
134
  - lib/rubocop/cop/mixin/minitest_exploration_helpers.rb
131
135
  - lib/rubocop/cop/mixin/nil_assertion_handleable.rb
136
+ - lib/rubocop/cop/mixin/predicate_assertion_handleable.rb
132
137
  - lib/rubocop/minitest.rb
133
138
  - lib/rubocop/minitest/assert_offense.rb
134
139
  - lib/rubocop/minitest/inject.rb
@@ -154,6 +159,7 @@ files:
154
159
  - relnotes/v0.17.0.md
155
160
  - relnotes/v0.17.1.md
156
161
  - relnotes/v0.17.2.md
162
+ - relnotes/v0.18.0.md
157
163
  - relnotes/v0.2.0.md
158
164
  - relnotes/v0.2.1.md
159
165
  - relnotes/v0.3.0.md
@@ -180,7 +186,7 @@ metadata:
180
186
  homepage_uri: https://docs.rubocop.org/rubocop-minitest/
181
187
  changelog_uri: https://github.com/rubocop/rubocop-minitest/blob/master/CHANGELOG.md
182
188
  source_code_uri: https://github.com/rubocop/rubocop-minitest
183
- documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.17
189
+ documentation_uri: https://docs.rubocop.org/rubocop-minitest/0.18
184
190
  bug_tracker_uri: https://github.com/rubocop/rubocop-minitest/issues
185
191
  rubygems_mfa_required: 'true'
186
192
  post_install_message: