rubocop-minitest 0.6.2 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,14 +9,14 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # assert_equal(false, actual)
12
- # assert_equal(false, actual, 'the message')
12
+ # assert_equal(false, actual, 'message')
13
13
  #
14
14
  # assert(!test)
15
- # assert(!test, 'the message')
15
+ # assert(!test, 'message')
16
16
  #
17
17
  # # good
18
18
  # refute(actual)
19
- # refute(actual, 'the message')
19
+ # refute(actual, 'message')
20
20
  #
21
21
  class RefuteFalse < Cop
22
22
  include ArgumentRangeHelper
@@ -9,16 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # refute(collection.include?(object))
12
- # refute(collection.include?(object), 'the message')
12
+ # refute(collection.include?(object), 'message')
13
13
  #
14
14
  # # good
15
15
  # refute_includes(collection, object)
16
- # refute_includes(collection, object, 'the message')
16
+ # refute_includes(collection, object, 'message')
17
17
  #
18
18
  class RefuteIncludes < Cop
19
- extend IncludesCopRule
19
+ extend MinitestCopRule
20
20
 
21
- rule target_method: :refute, prefer_method: :refute_includes
21
+ define_rule :refute, target_method: :include?, preferred_method: :refute_includes
22
22
  end
23
23
  end
24
24
  end
@@ -9,50 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # refute(object.instance_of?(Class))
12
- # refute(object.instance_of?(Class), 'the message')
12
+ # refute(object.instance_of?(Class), 'message')
13
13
  #
14
14
  # # good
15
15
  # refute_instance_of(Class, object)
16
- # refute_instance_of(Class, object, 'the message')
16
+ # refute_instance_of(Class, object, 'message')
17
17
  #
18
18
  class RefuteInstanceOf < Cop
19
- include ArgumentRangeHelper
19
+ extend MinitestCopRule
20
20
 
21
- MSG = 'Prefer using `refute_instance_of(%<arguments>s)` over ' \
22
- '`refute(%<receiver>s)`.'
23
-
24
- def_node_matcher :refute_with_instance_of, <<~PATTERN
25
- (send nil? :refute $(send $_ :instance_of? $_) $...)
26
- PATTERN
27
-
28
- def on_send(node)
29
- refute_with_instance_of(node) do |first_receiver_arg, object, method, rest_args|
30
- message = rest_args.first
31
- arguments = node_arguments(object, method, message)
32
- receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
33
-
34
- offense_message = format(MSG, arguments: arguments, receiver: receiver)
35
-
36
- add_offense(node, message: offense_message)
37
- end
38
- end
39
-
40
- def autocorrect(node)
41
- lambda do |corrector|
42
- refute_with_instance_of(node) do |_, object, method|
43
- corrector.replace(node.loc.selector, 'refute_instance_of')
44
-
45
- replacement = [method, object].map(&:source).join(', ')
46
- corrector.replace(first_argument_range(node), replacement)
47
- end
48
- end
49
- end
50
-
51
- private
52
-
53
- def node_arguments(object, method, message)
54
- [method, object, message].compact.map(&:source).join(', ')
55
- end
21
+ define_rule :refute, target_method: :instance_of?, inverse: true
56
22
  end
57
23
  end
58
24
  end
@@ -9,51 +9,16 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # refute(matcher.match(string))
12
- # refute(matcher.match(string), 'the message')
12
+ # refute(matcher.match(string), 'message')
13
13
  #
14
14
  # # good
15
15
  # refute_match(matcher, string)
16
- # refute_match(matcher, string, 'the message')
16
+ # refute_match(matcher, string, 'message')
17
17
  #
18
18
  class RefuteMatch < Cop
19
- include ArgumentRangeHelper
19
+ extend MinitestCopRule
20
20
 
21
- MSG = 'Prefer using `refute_match(%<arguments>s)` over ' \
22
- '`refute(%<receiver>s)`.'
23
-
24
- def_node_matcher :refute_with_match, <<~PATTERN
25
- (send nil? :refute $(send $_ :match $_) $...)
26
- PATTERN
27
-
28
- def on_send(node)
29
- refute_with_match(node) do
30
- |first_receiver_arg, matcher, actual, rest_receiver_arg|
31
- message = rest_receiver_arg.first
32
- arguments = node_arguments(matcher, actual, message)
33
- receiver = [first_receiver_arg.source, message&.source].compact.join(', ')
34
-
35
- offense_message = format(MSG, arguments: arguments, receiver: receiver)
36
-
37
- add_offense(node, message: offense_message)
38
- end
39
- end
40
-
41
- def autocorrect(node)
42
- lambda do |corrector|
43
- refute_with_match(node) do |_, matcher, actual|
44
- corrector.replace(node.loc.selector, 'refute_match')
45
-
46
- replacement = [matcher, actual].map(&:source).join(', ')
47
- corrector.replace(first_argument_range(node), replacement)
48
- end
49
- end
50
- end
51
-
52
- private
53
-
54
- def node_arguments(matcher, actual, message)
55
- [matcher.source, actual.source, message&.source].compact.join(', ')
56
- end
21
+ define_rule :refute, target_method: :match
57
22
  end
58
23
  end
59
24
  end
@@ -9,11 +9,11 @@ module RuboCop
9
9
  # @example
10
10
  # # bad
11
11
  # refute_equal(nil, actual)
12
- # refute_equal(nil, actual, 'the message')
12
+ # refute_equal(nil, actual, 'message')
13
13
  #
14
14
  # # good
15
15
  # refute_nil(actual)
16
- # refute_nil(actual, 'the message')
16
+ # refute_nil(actual, 'message')
17
17
  #
18
18
  class RefuteNil < Cop
19
19
  include ArgumentRangeHelper
@@ -3,59 +3,24 @@
3
3
  module RuboCop
4
4
  module Cop
5
5
  module Minitest
6
- # This cop enforces the test to use `refute_respond_to(object, :some_method)`
7
- # over `refute(object.respond_to?(:some_method))`.
6
+ # This cop enforces the test to use `refute_respond_to(object, :do_something)`
7
+ # over `refute(object.respond_to?(:do_something))`.
8
8
  #
9
9
  # @example
10
10
  # # bad
11
- # refute(object.respond_to?(:some_method))
12
- # refute(object.respond_to?(:some_method), 'the message')
13
- # refute(respond_to?(:some_method))
11
+ # refute(object.respond_to?(:do_something))
12
+ # refute(object.respond_to?(:do_something), 'message')
13
+ # refute(respond_to?(:do_something))
14
14
  #
15
15
  # # good
16
- # refute_respond_to(object, :some_method)
17
- # refute_respond_to(object, :some_method, 'the message')
18
- # refute_respond_to(self, :some_method)
16
+ # refute_respond_to(object, :do_something)
17
+ # refute_respond_to(object, :do_something, 'message')
18
+ # refute_respond_to(self, :do_something)
19
19
  #
20
20
  class RefuteRespondTo < Cop
21
- include ArgumentRangeHelper
21
+ extend MinitestCopRule
22
22
 
23
- MSG = 'Prefer using `refute_respond_to(%<preferred>s)` over ' \
24
- '`refute(%<over>s)`.'
25
-
26
- def_node_matcher :refute_with_respond_to, <<~PATTERN
27
- (send nil? :refute $(send $_ :respond_to? $_) $...)
28
- PATTERN
29
-
30
- def on_send(node)
31
- refute_with_respond_to(node) do |over, object, method, rest_args|
32
- custom_message = rest_args.first
33
- preferred = build_preferred_arguments(object, method, custom_message)
34
- over = [over, custom_message].compact.map(&:source).join(', ')
35
- message = format(MSG, preferred: preferred, over: over)
36
- add_offense(node, message: message)
37
- end
38
- end
39
-
40
- def autocorrect(node)
41
- lambda do |corrector|
42
- refute_with_respond_to(node) do |_, object, method|
43
- corrector.replace(node.loc.selector, 'refute_respond_to')
44
-
45
- object = object ? object.source : 'self'
46
- replacement = [object, method.source].join(', ')
47
- corrector.replace(first_argument_range(node), replacement)
48
- end
49
- end
50
- end
51
-
52
- private
53
-
54
- def build_preferred_arguments(receiver, method, message)
55
- receiver = receiver ? receiver.source : 'self'
56
-
57
- [receiver, method.source, message&.source].compact.join(', ')
58
- end
23
+ define_rule :refute, target_method: :respond_to?
59
24
  end
60
25
  end
61
26
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'mixin/argument_range_helper'
4
- require_relative 'mixin/includes_cop_rule'
4
+ require_relative 'mixin/minitest_cop_rule'
5
5
  require_relative 'minitest/assert_empty'
6
6
  require_relative 'minitest/assert_empty_literal'
7
7
  require_relative 'minitest/assert_equal'
@@ -11,6 +11,7 @@ require_relative 'minitest/assert_instance_of'
11
11
  require_relative 'minitest/assert_match'
12
12
  require_relative 'minitest/assert_respond_to'
13
13
  require_relative 'minitest/assert_truthy'
14
+ require_relative 'minitest/global_expectations'
14
15
  require_relative 'minitest/refute_empty'
15
16
  require_relative 'minitest/refute_false'
16
17
  require_relative 'minitest/refute_equal'
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Provide a method to define offense rule for Minitest cops.
6
+ module MinitestCopRule
7
+ #
8
+ # Define offense rule for Minitest cops.
9
+ #
10
+ # @example
11
+ # define_rule :assert, target_method: :match
12
+ # define_rule :refute, target_method: :match
13
+ # define_rule :assert, target_method: :include?, preferred_method: :assert_includes
14
+ # define_rule :assert, target_method: :instance_of?, inverse: true
15
+ #
16
+ # @param assertion_method [Symbol] Assertion method like `assert` or `refute`.
17
+ # @param target_method [Symbol] Method name offensed by assertion method arguments.
18
+ # @param preferred_method [Symbol] An optional param. Custom method name replaced by
19
+ # auto-correction. The preferred method name that connects
20
+ # `assertion_method` and `target_method` with `_` is
21
+ # the default name.
22
+ # @param inverse [Boolean] An optional param. Order of arguments replaced by auto-correction.
23
+ #
24
+ def define_rule(assertion_method, target_method:, preferred_method: nil, inverse: false)
25
+ if preferred_method.nil?
26
+ preferred_method = "#{assertion_method}_#{target_method.to_s.delete('?')}"
27
+ end
28
+
29
+ class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
30
+ include ArgumentRangeHelper
31
+
32
+ MSG = 'Prefer using `#{preferred_method}(%<new_arguments>s)` over ' \
33
+ '`#{assertion_method}(%<original_arguments>s)`.'
34
+
35
+ def on_send(node)
36
+ return unless node.method?(:#{assertion_method})
37
+ return unless (arguments = peel_redundant_parentheses_from(node.arguments))
38
+ return unless arguments.first.respond_to?(:method?) && arguments.first.method?(:#{target_method})
39
+
40
+ add_offense(node, message: offense_message(arguments))
41
+ end
42
+
43
+ def autocorrect(node)
44
+ lambda do |corrector|
45
+ corrector.replace(node.loc.selector, '#{preferred_method}')
46
+
47
+ arguments = peel_redundant_parentheses_from(node.arguments)
48
+
49
+ new_arguments = new_arguments(arguments).join(', ')
50
+
51
+ if enclosed_in_redundant_parentheses?(node)
52
+ new_arguments = '(' + new_arguments + ')'
53
+ end
54
+
55
+ corrector.replace(first_argument_range(node), new_arguments)
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def peel_redundant_parentheses_from(arguments)
62
+ return arguments unless arguments.first.begin_type?
63
+
64
+ peel_redundant_parentheses_from(arguments.first.children)
65
+ end
66
+
67
+ def offense_message(arguments)
68
+ message_argument = arguments.last if arguments.first != arguments.last
69
+
70
+ new_arguments = [
71
+ new_arguments(arguments),
72
+ message_argument&.source
73
+ ].flatten.compact.join(', ')
74
+
75
+ original_arguments = arguments.map(&:source).join(', ')
76
+
77
+ format(
78
+ MSG,
79
+ new_arguments: new_arguments,
80
+ original_arguments: original_arguments
81
+ )
82
+ end
83
+
84
+ def new_arguments(arguments)
85
+ receiver = correct_receiver(arguments.first.receiver)
86
+ method_argument = arguments.first.arguments.first&.source
87
+
88
+ new_arguments = [receiver, method_argument].compact
89
+ new_arguments.reverse! if #{inverse}
90
+ new_arguments
91
+ end
92
+
93
+ def enclosed_in_redundant_parentheses?(node)
94
+ node.arguments.first.begin_type?
95
+ end
96
+
97
+ def correct_receiver(receiver)
98
+ receiver ? receiver.source : 'self'
99
+ end
100
+ RUBY
101
+ end
102
+ end
103
+ end
104
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Minitest
5
- VERSION = '0.6.2'
5
+ VERSION = '0.7.0'
6
6
  end
7
7
  end
data/manual/cops.md CHANGED
@@ -10,6 +10,7 @@
10
10
  * [Minitest/AssertNil](cops_minitest.md#minitestassertnil)
11
11
  * [Minitest/AssertRespondTo](cops_minitest.md#minitestassertrespondto)
12
12
  * [Minitest/AssertTruthy](cops_minitest.md#minitestasserttruthy)
13
+ * [Minitest/GlobalExpectations](cops_minitest.md#minitestglobalexpectations)
13
14
  * [Minitest/RefuteEmpty](cops_minitest.md#minitestrefuteempty)
14
15
  * [Minitest/RefuteEqual](cops_minitest.md#minitestrefuteequal)
15
16
  * [Minitest/RefuteFalse](cops_minitest.md#minitestrefutefalse)
@@ -14,11 +14,11 @@ instead of using `assert(object.empty?)`.
14
14
  ```ruby
15
15
  # bad
16
16
  assert(object.empty?)
17
- assert(object.empty?, 'the message')
17
+ assert(object.empty?, 'message')
18
18
 
19
19
  # good
20
20
  assert_empty(object)
21
- assert_empty(object, 'the message')
21
+ assert_empty(object, 'message')
22
22
  ```
23
23
 
24
24
  ### References
@@ -82,11 +82,11 @@ instead of using `assert(collection.include?(object))`.
82
82
  ```ruby
83
83
  # bad
84
84
  assert(collection.include?(object))
85
- assert(collection.include?(object), 'the message')
85
+ assert(collection.include?(object), 'message')
86
86
 
87
87
  # good
88
88
  assert_includes(collection, object)
89
- assert_includes(collection, object, 'the message')
89
+ assert_includes(collection, object, 'message')
90
90
  ```
91
91
 
92
92
  ### References
@@ -107,11 +107,11 @@ over `assert(object.instance_of?(Class))`.
107
107
  ```ruby
108
108
  # bad
109
109
  assert(object.instance_of?(Class))
110
- assert(object.instance_of?(Class), 'the message')
110
+ assert(object.instance_of?(Class), 'message')
111
111
 
112
112
  # good
113
113
  assert_instance_of(Class, object)
114
- assert_instance_of(Class, object, 'the message')
114
+ assert_instance_of(Class, object, 'message')
115
115
  ```
116
116
 
117
117
  ### References
@@ -132,11 +132,11 @@ instead of using `assert(matcher.match(string))`.
132
132
  ```ruby
133
133
  # bad
134
134
  assert(matcher.match(string))
135
- assert(matcher.match(string), 'the message')
135
+ assert(matcher.match(string), 'message')
136
136
 
137
137
  # good
138
138
  assert_match(regex, string)
139
- assert_match(matcher, string, 'the message')
139
+ assert_match(matcher, string, 'message')
140
140
  ```
141
141
 
142
142
  ### References
@@ -157,11 +157,11 @@ instead of using `assert_equal(nil, something)`.
157
157
  ```ruby
158
158
  # bad
159
159
  assert_equal(nil, actual)
160
- assert_equal(nil, actual, 'the message')
160
+ assert_equal(nil, actual, 'message')
161
161
 
162
162
  # good
163
163
  assert_nil(actual)
164
- assert_nil(actual, 'the message')
164
+ assert_nil(actual, 'message')
165
165
  ```
166
166
 
167
167
  ### References
@@ -174,21 +174,21 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChan
174
174
  --- | --- | --- | --- | ---
175
175
  Enabled | Yes | Yes | 0.3 | -
176
176
 
177
- This cop enforces the use of `assert_respond_to(object, :some_method)`
178
- over `assert(object.respond_to?(:some_method))`.
177
+ This cop enforces the use of `assert_respond_to(object, :do_something)`
178
+ over `assert(object.respond_to?(:do_something))`.
179
179
 
180
180
  ### Examples
181
181
 
182
182
  ```ruby
183
183
  # bad
184
- assert(object.respond_to?(:some_method))
185
- assert(object.respond_to?(:some_method), 'the message')
186
- assert(respond_to?(:some_method))
184
+ assert(object.respond_to?(:do_something))
185
+ assert(object.respond_to?(:do_something), 'message')
186
+ assert(respond_to?(:do_something))
187
187
 
188
188
  # good
189
- assert_respond_to(object, :some_method)
190
- assert_respond_to(object, :some_method, 'the message')
191
- assert_respond_to(self, some_method)
189
+ assert_respond_to(object, :do_something)
190
+ assert_respond_to(object, :do_something, 'message')
191
+ assert_respond_to(self, :do_something)
192
192
  ```
193
193
 
194
194
  ### References
@@ -209,17 +209,38 @@ instead of using `assert_equal(true, actual)`.
209
209
  ```ruby
210
210
  # bad
211
211
  assert_equal(true, actual)
212
- assert_equal(true, actual, 'the message')
212
+ assert_equal(true, actual, 'message')
213
213
 
214
214
  # good
215
215
  assert(actual)
216
- assert(actual, 'the message')
216
+ assert(actual, 'message')
217
217
  ```
218
218
 
219
219
  ### References
220
220
 
221
221
  * [https://github.com/rubocop-hq/minitest-style-guide#assert-truthy](https://github.com/rubocop-hq/minitest-style-guide#assert-truthy)
222
222
 
223
+ ## Minitest/GlobalExpectations
224
+
225
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
226
+ --- | --- | --- | --- | ---
227
+ Enabled | Yes | Yes | 0.7 | -
228
+
229
+ This Cop checks for deprecated global expectations
230
+ and autocorrects them to use expect format.
231
+
232
+ ### Examples
233
+
234
+ ```ruby
235
+ # bad
236
+ n.must_equal 42
237
+ n.wont_match b
238
+
239
+ # good
240
+ _(n).must_equal 42
241
+ _(n).wont_match b
242
+ ```
243
+
223
244
  ## Minitest/RefuteEmpty
224
245
 
225
246
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
@@ -234,11 +255,11 @@ using `refute(object.empty?)`.
234
255
  ```ruby
235
256
  # bad
236
257
  refute(object.empty?)
237
- refute(object.empty?, 'the message')
258
+ refute(object.empty?, 'message')
238
259
 
239
260
  # good
240
261
  refute_empty(object)
241
- refute_empty(object, 'the message')
262
+ refute_empty(object, 'message')
242
263
  ```
243
264
 
244
265
  ### References
@@ -283,14 +304,14 @@ over `assert_equal(false, object)`.
283
304
  ```ruby
284
305
  # bad
285
306
  assert_equal(false, actual)
286
- assert_equal(false, actual, 'the message')
307
+ assert_equal(false, actual, 'message')
287
308
 
288
309
  assert(!test)
289
- assert(!test, 'the message')
310
+ assert(!test, 'message')
290
311
 
291
312
  # good
292
313
  refute(actual)
293
- refute(actual, 'the message')
314
+ refute(actual, 'message')
294
315
  ```
295
316
 
296
317
  ### References
@@ -311,11 +332,11 @@ instead of using `refute(collection.include?(object))`.
311
332
  ```ruby
312
333
  # bad
313
334
  refute(collection.include?(object))
314
- refute(collection.include?(object), 'the message')
335
+ refute(collection.include?(object), 'message')
315
336
 
316
337
  # good
317
338
  refute_includes(collection, object)
318
- refute_includes(collection, object, 'the message')
339
+ refute_includes(collection, object, 'message')
319
340
  ```
320
341
 
321
342
  ### References
@@ -336,11 +357,11 @@ over `refute(object.instance_of?(Class))`.
336
357
  ```ruby
337
358
  # bad
338
359
  refute(object.instance_of?(Class))
339
- refute(object.instance_of?(Class), 'the message')
360
+ refute(object.instance_of?(Class), 'message')
340
361
 
341
362
  # good
342
363
  refute_instance_of(Class, object)
343
- refute_instance_of(Class, object, 'the message')
364
+ refute_instance_of(Class, object, 'message')
344
365
  ```
345
366
 
346
367
  ### References
@@ -361,11 +382,11 @@ instead of using `refute(matcher.match(string))`.
361
382
  ```ruby
362
383
  # bad
363
384
  refute(matcher.match(string))
364
- refute(matcher.match(string), 'the message')
385
+ refute(matcher.match(string), 'message')
365
386
 
366
387
  # good
367
388
  refute_match(matcher, string)
368
- refute_match(matcher, string, 'the message')
389
+ refute_match(matcher, string, 'message')
369
390
  ```
370
391
 
371
392
  ### References
@@ -386,11 +407,11 @@ instead of using `refute_equal(nil, something)`.
386
407
  ```ruby
387
408
  # bad
388
409
  refute_equal(nil, actual)
389
- refute_equal(nil, actual, 'the message')
410
+ refute_equal(nil, actual, 'message')
390
411
 
391
412
  # good
392
413
  refute_nil(actual)
393
- refute_nil(actual, 'the message')
414
+ refute_nil(actual, 'message')
394
415
  ```
395
416
 
396
417
  ### References
@@ -403,21 +424,21 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChan
403
424
  --- | --- | --- | --- | ---
404
425
  Enabled | Yes | Yes | 0.4 | -
405
426
 
406
- This cop enforces the test to use `refute_respond_to(object, :some_method)`
407
- over `refute(object.respond_to?(:some_method))`.
427
+ This cop enforces the test to use `refute_respond_to(object, :do_something)`
428
+ over `refute(object.respond_to?(:do_something))`.
408
429
 
409
430
  ### Examples
410
431
 
411
432
  ```ruby
412
433
  # bad
413
- refute(object.respond_to?(:some_method))
414
- refute(object.respond_to?(:some_method), 'the message')
415
- refute(respond_to?(:some_method))
434
+ refute(object.respond_to?(:do_something))
435
+ refute(object.respond_to?(:do_something), 'message')
436
+ refute(respond_to?(:do_something))
416
437
 
417
438
  # good
418
- refute_respond_to(object, :some_method)
419
- refute_respond_to(object, :some_method, 'the message')
420
- refute_respond_to(self, :some_method)
439
+ refute_respond_to(object, :do_something)
440
+ refute_respond_to(object, :do_something, 'message')
441
+ refute_respond_to(self, :do_something)
421
442
  ```
422
443
 
423
444
  ### References