rubocop-minitest 0.2.1 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +7 -0
  3. data/.rubocop_todo.yml +5 -5
  4. data/CHANGELOG.md +44 -0
  5. data/Gemfile +6 -2
  6. data/README.md +2 -1
  7. data/config/default.yml +70 -10
  8. data/lib/rubocop-minitest.rb +0 -3
  9. data/lib/rubocop/cop/minitest/assert_empty.rb +11 -10
  10. data/lib/rubocop/cop/minitest/assert_empty_literal.rb +36 -0
  11. data/lib/rubocop/cop/minitest/assert_equal.rb +52 -0
  12. data/lib/rubocop/cop/minitest/assert_includes.rb +13 -12
  13. data/lib/rubocop/cop/minitest/assert_instance_of.rb +59 -0
  14. data/lib/rubocop/cop/minitest/assert_nil.rb +10 -4
  15. data/lib/rubocop/cop/minitest/assert_respond_to.rb +62 -0
  16. data/lib/rubocop/cop/minitest/assert_truthy.rb +10 -5
  17. data/lib/rubocop/cop/minitest/refute_empty.rb +51 -0
  18. data/lib/rubocop/cop/minitest/refute_equal.rb +68 -0
  19. data/lib/rubocop/cop/minitest/refute_false.rb +51 -0
  20. data/lib/rubocop/cop/minitest/refute_includes.rb +61 -0
  21. data/lib/rubocop/cop/minitest/refute_instance_of.rb +59 -0
  22. data/lib/rubocop/cop/minitest/refute_nil.rb +10 -4
  23. data/lib/rubocop/cop/minitest/refute_respond_to.rb +62 -0
  24. data/lib/rubocop/cop/minitest_cops.rb +11 -0
  25. data/lib/rubocop/cop/mixin/argument_range_helper.rb +31 -0
  26. data/lib/rubocop/minitest/version.rb +1 -1
  27. data/manual/cops.md +10 -0
  28. data/manual/cops_minitest.md +264 -15
  29. data/relnotes/v0.1.0.md +7 -0
  30. data/relnotes/v0.2.0.md +9 -0
  31. data/relnotes/v0.2.1.md +5 -0
  32. data/relnotes/v0.3.0.md +16 -0
  33. data/relnotes/v0.4.0.md +14 -0
  34. data/relnotes/v0.4.1.md +5 -0
  35. data/relnotes/v0.5.0.md +5 -0
  36. data/relnotes/v0.5.1.md +3 -0
  37. data/tasks/cut_release.rake +60 -0
  38. metadata +24 -3
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
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))`.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # refute(object.respond_to?(:some_method))
12
+ # refute(object.respond_to?(:some_method), 'the message')
13
+ # refute(respond_to?(:some_method))
14
+ #
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)
19
+ #
20
+ class RefuteRespondTo < Cop
21
+ include ArgumentRangeHelper
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
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,7 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'mixin/argument_range_helper'
3
4
  require_relative 'minitest/assert_empty'
5
+ require_relative 'minitest/assert_empty_literal'
6
+ require_relative 'minitest/assert_equal'
4
7
  require_relative 'minitest/assert_nil'
5
8
  require_relative 'minitest/assert_includes'
9
+ require_relative 'minitest/assert_instance_of'
10
+ require_relative 'minitest/assert_respond_to'
6
11
  require_relative 'minitest/assert_truthy'
12
+ require_relative 'minitest/refute_empty'
13
+ require_relative 'minitest/refute_false'
14
+ require_relative 'minitest/refute_equal'
7
15
  require_relative 'minitest/refute_nil'
16
+ require_relative 'minitest/refute_includes'
17
+ require_relative 'minitest/refute_instance_of'
18
+ require_relative 'minitest/refute_respond_to'
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ # Methods that calculate and return `Parser::Source::Ranges`.
6
+ module ArgumentRangeHelper
7
+ include RangeHelp
8
+
9
+ private
10
+
11
+ def first_argument_range(node)
12
+ first_argument = node.first_argument
13
+
14
+ range_between(
15
+ first_argument.source_range.begin_pos,
16
+ first_argument.source_range.end_pos
17
+ )
18
+ end
19
+
20
+ def first_and_second_arguments_range(node)
21
+ first_argument = node.first_argument
22
+ second_argument = node.arguments[1]
23
+
24
+ range_between(
25
+ first_argument.source_range.begin_pos,
26
+ second_argument.source_range.end_pos
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Minitest
5
- VERSION = '0.2.1'
5
+ VERSION = '0.5.1'
6
6
  end
7
7
  end
@@ -2,9 +2,19 @@
2
2
  #### Department [Minitest](cops_minitest.md)
3
3
 
4
4
  * [Minitest/AssertEmpty](cops_minitest.md#minitestassertempty)
5
+ * [Minitest/AssertEmptyLiteral](cops_minitest.md#minitestassertemptyliteral)
6
+ * [Minitest/AssertEqual](cops_minitest.md#minitestassertequal)
5
7
  * [Minitest/AssertIncludes](cops_minitest.md#minitestassertincludes)
8
+ * [Minitest/AssertInstanceOf](cops_minitest.md#minitestassertinstanceof)
6
9
  * [Minitest/AssertNil](cops_minitest.md#minitestassertnil)
10
+ * [Minitest/AssertRespondTo](cops_minitest.md#minitestassertrespondto)
7
11
  * [Minitest/AssertTruthy](cops_minitest.md#minitestasserttruthy)
12
+ * [Minitest/RefuteEmpty](cops_minitest.md#minitestrefuteempty)
13
+ * [Minitest/RefuteEqual](cops_minitest.md#minitestrefuteequal)
14
+ * [Minitest/RefuteFalse](cops_minitest.md#minitestrefutefalse)
15
+ * [Minitest/RefuteIncludes](cops_minitest.md#minitestrefuteincludes)
16
+ * [Minitest/RefuteInstanceOf](cops_minitest.md#minitestrefuteinstanceof)
8
17
  * [Minitest/RefuteNil](cops_minitest.md#minitestrefutenil)
18
+ * [Minitest/RefuteRespondTo](cops_minitest.md#minitestrefuterespondto)
9
19
 
10
20
  <!-- END_COP_LIST -->
@@ -6,56 +6,126 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChan
6
6
  --- | --- | --- | --- | ---
7
7
  Enabled | Yes | Yes | 0.2 | -
8
8
 
9
- Check if your test uses `assert_empty` instead of `assert(actual.empty?)`.
9
+ This cop enforces the test to use `assert_empty`
10
+ instead of using `assert(object.empty?)`.
10
11
 
11
12
  ### Examples
12
13
 
13
14
  ```ruby
14
15
  # bad
15
- assert(actual.empty?)
16
- assert(actual.empty?, 'the message')
16
+ assert(object.empty?)
17
+ assert(object.empty?, 'the message')
17
18
 
18
19
  # good
19
- assert_empty(actual)
20
- assert_empty(actual, 'the message')
20
+ assert_empty(object)
21
+ assert_empty(object, 'the message')
21
22
  ```
22
23
 
23
24
  ### References
24
25
 
25
26
  * [https://github.com/rubocop-hq/minitest-style-guide#assert-empty](https://github.com/rubocop-hq/minitest-style-guide#assert-empty)
26
27
 
28
+ ## Minitest/AssertEmptyLiteral
29
+
30
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
31
+ --- | --- | --- | --- | ---
32
+ Enabled | Yes | No | 0.5 | -
33
+
34
+ This cop enforces the test to use `assert_empty`
35
+ instead of using `assert([], object)`.
36
+
37
+ ### Examples
38
+
39
+ ```ruby
40
+ # bad
41
+ assert([], object)
42
+ assert({}, object)
43
+
44
+ # good
45
+ assert_empty(object)
46
+ ```
47
+
48
+ ## Minitest/AssertEqual
49
+
50
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
51
+ --- | --- | --- | --- | ---
52
+ Enabled | Yes | Yes | 0.4 | -
53
+
54
+ This cop enforces the use of `assert_equal(expected, actual)`
55
+ over `assert(expected == actual)`.
56
+
57
+ ### Examples
58
+
59
+ ```ruby
60
+ # bad
61
+ assert("rubocop-minitest" == actual)
62
+
63
+ # good
64
+ assert_equal("rubocop-minitest", actual)
65
+ ```
66
+
67
+ ### References
68
+
69
+ * [https://github.com/rubocop-hq/minitest-style-guide#assert-equal-arguments-order](https://github.com/rubocop-hq/minitest-style-guide#assert-equal-arguments-order)
70
+
27
71
  ## Minitest/AssertIncludes
28
72
 
29
73
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
30
74
  --- | --- | --- | --- | ---
31
75
  Enabled | Yes | Yes | 0.2 | -
32
76
 
33
- Check if your test uses `assert_includes`
34
- instead of `assert(collection.includes?(actual))`.
77
+ This cop enforces the test to use `assert_includes`
78
+ instead of using `assert(collection.include?(object))`.
35
79
 
36
80
  ### Examples
37
81
 
38
82
  ```ruby
39
83
  # bad
40
- assert(collection.includes?(actual))
41
- assert(collection.includes?(actual), 'the message')
84
+ assert(collection.include?(object))
85
+ assert(collection.include?(object), 'the message')
42
86
 
43
87
  # good
44
- assert_includes(collection, actual)
45
- assert_includes(collection, actual, 'the message')
88
+ assert_includes(collection, object)
89
+ assert_includes(collection, object, 'the message')
46
90
  ```
47
91
 
48
92
  ### References
49
93
 
50
94
  * [https://github.com/rubocop-hq/minitest-style-guide#assert-includes](https://github.com/rubocop-hq/minitest-style-guide#assert-includes)
51
95
 
96
+ ## Minitest/AssertInstanceOf
97
+
98
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
99
+ --- | --- | --- | --- | ---
100
+ Enabled | Yes | Yes | 0.4 | -
101
+
102
+ This cop enforces the test to use `assert_instance_of(Class, object)`
103
+ over `assert(object.instance_of?(Class))`.
104
+
105
+ ### Examples
106
+
107
+ ```ruby
108
+ # bad
109
+ assert(object.instance_of?(Class))
110
+ assert(object.instance_of?(Class), 'the message')
111
+
112
+ # good
113
+ assert_instance_of(Class, object)
114
+ assert_instance_of(Class, object, 'the message')
115
+ ```
116
+
117
+ ### References
118
+
119
+ * [https://github.com/rubocop-hq/minitest-style-guide#assert-instance-of](https://github.com/rubocop-hq/minitest-style-guide#assert-instance-of)
120
+
52
121
  ## Minitest/AssertNil
53
122
 
54
123
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
55
124
  --- | --- | --- | --- | ---
56
125
  Enabled | Yes | Yes | 0.1 | -
57
126
 
58
- Check if your test uses `assert_nil` instead of `assert_equal(nil, something)`.
127
+ This cop enforces the test to use `assert_nil`
128
+ instead of using `assert_equal(nil, something)`.
59
129
 
60
130
  ### Examples
61
131
 
@@ -73,14 +143,41 @@ assert_nil(actual, 'the message')
73
143
 
74
144
  * [https://github.com/rubocop-hq/minitest-style-guide#assert-nil](https://github.com/rubocop-hq/minitest-style-guide#assert-nil)
75
145
 
146
+ ## Minitest/AssertRespondTo
147
+
148
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
149
+ --- | --- | --- | --- | ---
150
+ Enabled | Yes | Yes | 0.3 | -
151
+
152
+ This cop enforces the use of `assert_respond_to(object, :some_method)`
153
+ over `assert(object.respond_to?(:some_method))`.
154
+
155
+ ### Examples
156
+
157
+ ```ruby
158
+ # bad
159
+ assert(object.respond_to?(:some_method))
160
+ assert(object.respond_to?(:some_method), 'the message')
161
+ assert(respond_to?(:some_method))
162
+
163
+ # good
164
+ assert_respond_to(object, :some_method)
165
+ assert_respond_to(object, :some_method, 'the message')
166
+ assert_respond_to(self, some_method)
167
+ ```
168
+
169
+ ### References
170
+
171
+ * [https://github.com/rubocop-hq/minitest-style-guide#assert-responds-to-method](https://github.com/rubocop-hq/minitest-style-guide#assert-responds-to-method)
172
+
76
173
  ## Minitest/AssertTruthy
77
174
 
78
175
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
79
176
  --- | --- | --- | --- | ---
80
177
  Enabled | Yes | Yes | 0.2 | -
81
178
 
82
- Check if your test uses `assert(actual)`
83
- instead of `assert_equal(true, actual)`.
179
+ This cop enforces the test to use `assert(actual)`
180
+ instead of using `assert_equal(true, actual)`.
84
181
 
85
182
  ### Examples
86
183
 
@@ -98,13 +195,138 @@ assert(actual, 'the message')
98
195
 
99
196
  * [https://github.com/rubocop-hq/minitest-style-guide#assert-truthy](https://github.com/rubocop-hq/minitest-style-guide#assert-truthy)
100
197
 
198
+ ## Minitest/RefuteEmpty
199
+
200
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
201
+ --- | --- | --- | --- | ---
202
+ Enabled | Yes | Yes | 0.3 | -
203
+
204
+ This cop enforces to use `refute_empty` instead of
205
+ using `refute(object.empty?)`.
206
+
207
+ ### Examples
208
+
209
+ ```ruby
210
+ # bad
211
+ refute(object.empty?)
212
+ refute(object.empty?, 'the message')
213
+
214
+ # good
215
+ refute_empty(object)
216
+ refute_empty(object, 'the message')
217
+ ```
218
+
219
+ ### References
220
+
221
+ * [https://github.com/rubocop-hq/minitest-style-guide#refute-empty](https://github.com/rubocop-hq/minitest-style-guide#refute-empty)
222
+
223
+ ## Minitest/RefuteEqual
224
+
225
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
226
+ --- | --- | --- | --- | ---
227
+ Enabled | Yes | Yes | 0.3 | -
228
+
229
+ This cop enforces the use of `refute_equal(expected, object)`
230
+ over `assert_equal(expected != actual)` or `assert(! expected == actual)`.
231
+
232
+ ### Examples
233
+
234
+ ```ruby
235
+ # bad
236
+ assert("rubocop-minitest" != actual)
237
+ assert(! "rubocop-minitest" == actual)
238
+
239
+ # good
240
+ refute_equal("rubocop-minitest", actual)
241
+ ```
242
+
243
+ ### References
244
+
245
+ * [https://github.com/rubocop-hq/minitest-style-guide#refute-equal](https://github.com/rubocop-hq/minitest-style-guide#refute-equal)
246
+
247
+ ## Minitest/RefuteFalse
248
+
249
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
250
+ --- | --- | --- | --- | ---
251
+ Enabled | Yes | Yes | 0.3 | -
252
+
253
+ This cop enforces the use of `refute(object)`
254
+ over `assert_equal(false, object)`.
255
+
256
+ ### Examples
257
+
258
+ ```ruby
259
+ # bad
260
+ assert_equal(false, actual)
261
+ assert_equal(false, actual, 'the message')
262
+
263
+ # good
264
+ refute(actual)
265
+ refute(actual, 'the message')
266
+ ```
267
+
268
+ ### References
269
+
270
+ * [https://github.com/rubocop-hq/minitest-style-guide#refute-false](https://github.com/rubocop-hq/minitest-style-guide#refute-false)
271
+
272
+ ## Minitest/RefuteIncludes
273
+
274
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
275
+ --- | --- | --- | --- | ---
276
+ Enabled | Yes | Yes | 0.3 | -
277
+
278
+ This cop enforces the test to use `refute_includes`
279
+ instead of using `refute(collection.include?(object))`.
280
+
281
+ ### Examples
282
+
283
+ ```ruby
284
+ # bad
285
+ refute(collection.include?(object))
286
+ refute(collection.include?(object), 'the message')
287
+
288
+ # good
289
+ refute_includes(collection, object)
290
+ refute_includes(collection, object, 'the message')
291
+ ```
292
+
293
+ ### References
294
+
295
+ * [https://github.com/rubocop-hq/minitest-style-guide#refute-includes](https://github.com/rubocop-hq/minitest-style-guide#refute-includes)
296
+
297
+ ## Minitest/RefuteInstanceOf
298
+
299
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
300
+ --- | --- | --- | --- | ---
301
+ Enabled | Yes | Yes | 0.4 | -
302
+
303
+ This cop enforces the use of `refute_instance_of(Class, object)`
304
+ over `refute(object.instance_of?(Class))`.
305
+
306
+ ### Examples
307
+
308
+ ```ruby
309
+ # bad
310
+ refute(object.instance_of?(Class))
311
+ refute(object.instance_of?(Class), 'the message')
312
+
313
+ # good
314
+ refute_instance_of(Class, object)
315
+ refute_instance_of(Class, object, 'the message')
316
+ ```
317
+
318
+ ### References
319
+
320
+ * [https://github.com/rubocop-hq/minitest-style-guide#refute-instance-of](https://github.com/rubocop-hq/minitest-style-guide#refute-instance-of)
321
+
101
322
  ## Minitest/RefuteNil
102
323
 
103
324
  Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
104
325
  --- | --- | --- | --- | ---
105
326
  Enabled | Yes | Yes | 0.2 | -
106
327
 
107
- Check if your test uses `refute_nil` instead of `refute_equal(nil, something)`.
328
+ This cop enforces the test to use `refute_nil`
329
+ instead of using `refute_equal(nil, something)`.
108
330
 
109
331
  ### Examples
110
332
 
@@ -121,3 +343,30 @@ refute_nil(actual, 'the message')
121
343
  ### References
122
344
 
123
345
  * [https://github.com/rubocop-hq/minitest-style-guide#refute-nil](https://github.com/rubocop-hq/minitest-style-guide#refute-nil)
346
+
347
+ ## Minitest/RefuteRespondTo
348
+
349
+ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
350
+ --- | --- | --- | --- | ---
351
+ Enabled | Yes | Yes | 0.4 | -
352
+
353
+ This cop enforces the test to use `refute_respond_to(object, :some_method)`
354
+ over `refute(object.respond_to?(:some_method))`.
355
+
356
+ ### Examples
357
+
358
+ ```ruby
359
+ # bad
360
+ refute(object.respond_to?(:some_method))
361
+ refute(object.respond_to?(:some_method), 'the message')
362
+ refute(respond_to?(:some_method))
363
+
364
+ # good
365
+ refute_respond_to(object, :some_method)
366
+ refute_respond_to(object, :some_method, 'the message')
367
+ refute_respond_to(self, :some_method)
368
+ ```
369
+
370
+ ### References
371
+
372
+ * [https://github.com/rubocop-hq/minitest-style-guide#refute-respond-to](https://github.com/rubocop-hq/minitest-style-guide#refute-respond-to)