minitest 5.11.3 → 5.27.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.
@@ -1,5 +1,4 @@
1
- # encoding: utf-8
2
- require "minitest/autorun"
1
+ require_relative "metametameta"
3
2
  require "stringio"
4
3
 
5
4
  class MiniSpecA < Minitest::Spec; end
@@ -12,38 +11,38 @@ class ExampleA; end
12
11
  class ExampleB < ExampleA; end
13
12
 
14
13
  describe Minitest::Spec do
15
- # helps to deal with 2.4 deprecation of Fixnum for Integer
16
- Int = 1.class
17
-
18
14
  # do not parallelize this suite... it just can"t handle it.
19
15
 
20
16
  def assert_triggered expected = "blah", klass = Minitest::Assertion
21
17
  @assertion_count += 1
22
18
 
23
- e = assert_raises(klass) do
19
+ e = assert_raises klass do
24
20
  yield
25
21
  end
26
22
 
27
23
  msg = e.message.sub(/(---Backtrace---).*/m, '\1')
28
24
  msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
29
- msg.gsub!(/@.+>/, "@PATH>")
30
25
  msg.gsub!(/(\d\.\d{6})\d+/, '\1xxx') # normalize: ruby version, impl, platform
31
- msg.gsub!(/:0x[a-fA-F0-9]{4,}/m, ":0xXXXXXX")
26
+ msg.gsub!(/:0x[Xa-fA-F0-9]{4,}[ @].+?>/, ":0xXXXXXX@PATH>")
32
27
 
33
- if expected
28
+ return unless expected
29
+
30
+ @assertion_count += 1
31
+ case expected
32
+ when String then
33
+ assert_equal expected, msg
34
+ when Regexp then
34
35
  @assertion_count += 1
35
- case expected
36
- when String then
37
- assert_equal expected, msg
38
- when Regexp then
39
- @assertion_count += 1
40
- assert_match expected, msg
41
- else
42
- flunk "Unknown: #{expected.inspect}"
43
- end
36
+ assert_match expected, msg
37
+ else
38
+ flunk "Unknown: #{expected.inspect}"
44
39
  end
45
40
  end
46
41
 
42
+ def assert_success spec
43
+ assert_equal true, spec
44
+ end
45
+
47
46
  before do
48
47
  @assertion_count = 4
49
48
  end
@@ -56,43 +55,63 @@ describe Minitest::Spec do
56
55
  @assertion_count = 1
57
56
 
58
57
  assert_triggered "Expected 1 to not be equal to 1." do
59
- 1.wont_equal 1
58
+ _(1).wont_equal 1
59
+ end
60
+ end
61
+
62
+ it "needs to check for file existence" do
63
+ @assertion_count = 3
64
+
65
+ assert_success _(__FILE__).path_must_exist
66
+
67
+ assert_triggered "Expected path 'blah' to exist." do
68
+ _("blah").path_must_exist
69
+ end
70
+ end
71
+
72
+ it "needs to check for file non-existence" do
73
+ @assertion_count = 3
74
+
75
+ assert_success _("blah").path_wont_exist
76
+
77
+ assert_triggered "Expected path '#{__FILE__}' to not exist." do
78
+ _(__FILE__).path_wont_exist
60
79
  end
61
80
  end
62
81
 
63
82
  it "needs to be sensible about must_include order" do
64
83
  @assertion_count += 3 # must_include is 2 assertions
65
84
 
66
- [1, 2, 3].must_include(2).must_equal true
85
+ assert_success _([1, 2, 3]).must_include(2)
67
86
 
68
87
  assert_triggered "Expected [1, 2, 3] to include 5." do
69
- [1, 2, 3].must_include 5
88
+ _([1, 2, 3]).must_include 5
70
89
  end
71
90
 
72
91
  assert_triggered "msg.\nExpected [1, 2, 3] to include 5." do
73
- [1, 2, 3].must_include 5, "msg"
92
+ _([1, 2, 3]).must_include 5, "msg"
74
93
  end
75
94
  end
76
95
 
77
96
  it "needs to be sensible about wont_include order" do
78
97
  @assertion_count += 3 # wont_include is 2 assertions
79
98
 
80
- [1, 2, 3].wont_include(5).must_equal false
99
+ assert_success _([1, 2, 3]).wont_include(5)
81
100
 
82
101
  assert_triggered "Expected [1, 2, 3] to not include 2." do
83
- [1, 2, 3].wont_include 2
102
+ _([1, 2, 3]).wont_include 2
84
103
  end
85
104
 
86
105
  assert_triggered "msg.\nExpected [1, 2, 3] to not include 2." do
87
- [1, 2, 3].wont_include 2, "msg"
106
+ _([1, 2, 3]).wont_include 2, "msg"
88
107
  end
89
108
  end
90
109
 
91
110
  it "needs to catch an expected exception" do
92
- @assertion_count = 2
111
+ @assertion_count -= 2
93
112
 
94
- proc { raise "blah" }.must_raise RuntimeError
95
- proc { raise Minitest::Assertion }.must_raise Minitest::Assertion
113
+ expect { raise "blah" }.must_raise RuntimeError
114
+ expect { raise Minitest::Assertion }.must_raise Minitest::Assertion
96
115
  end
97
116
 
98
117
  it "needs to catch an unexpected exception" do
@@ -106,11 +125,39 @@ describe Minitest::Spec do
106
125
  EOM
107
126
 
108
127
  assert_triggered msg do
109
- proc { raise StandardError, "woot" }.must_raise RuntimeError
128
+ expect { raise StandardError, "woot" }.must_raise RuntimeError
110
129
  end
111
130
 
112
131
  assert_triggered "msg.\n#{msg}" do
113
- proc { raise StandardError, "woot" }.must_raise RuntimeError, "msg"
132
+ expect { raise StandardError, "woot" }.must_raise RuntimeError, "msg"
133
+ end
134
+ end
135
+
136
+ def good_pattern
137
+ capture_io do # 3.0 is noisy
138
+ eval "[1,2,3] => [Integer, Integer, Integer]" # eval to escape parser for ruby<3
139
+ end
140
+ end
141
+
142
+ def bad_pattern
143
+ capture_io do # 3.0 is noisy
144
+ eval "[1,2,3] => [Integer, Integer]" # eval to escape parser for ruby<3
145
+ end
146
+ end
147
+
148
+ it "needs to pattern match" do
149
+ @assertion_count = 1
150
+
151
+ expect { good_pattern }.must_pattern_match
152
+ end
153
+
154
+ it "needs to error on bad pattern match" do
155
+ @assertion_count = 1
156
+
157
+ exp = /length mismatch/
158
+
159
+ assert_triggered exp do
160
+ expect { bad_pattern }.must_pattern_match
114
161
  end
115
162
  end
116
163
 
@@ -118,20 +165,22 @@ describe Minitest::Spec do
118
165
  @assertion_count -= 1 # no msg
119
166
  @assertion_count += 2 # assert_output is 2 assertions
120
167
 
121
- proc {}.must_be_silent.must_equal true
168
+ assert_success expect {}.must_be_silent
122
169
 
123
170
  assert_triggered "In stdout.\nExpected: \"\"\n Actual: \"xxx\"" do
124
- proc { print "xxx" }.must_be_silent
171
+ expect { print "xxx" }.must_be_silent
125
172
  end
126
173
  end
127
174
 
128
175
  it "needs to have all methods named well" do
176
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
177
+
129
178
  @assertion_count = 2
130
179
 
131
- methods = Object.public_instance_methods.find_all { |n| n =~ /^must|^wont/ }
180
+ methods = Minitest::Expectations.public_instance_methods.grep(/must|wont/)
132
181
  methods.map!(&:to_s) if Symbol === methods.first
133
182
 
134
- musts, wonts = methods.sort.partition { |m| m =~ /^must/ }
183
+ musts, wonts = methods.sort.partition { |m| m.include? "must" }
135
184
 
136
185
  expected_musts = %w[must_be
137
186
  must_be_close_to
@@ -147,346 +196,345 @@ describe Minitest::Spec do
147
196
  must_include
148
197
  must_match
149
198
  must_output
199
+ must_pattern_match
150
200
  must_raise
151
201
  must_respond_to
152
- must_throw]
202
+ must_throw
203
+ must_verify
204
+ path_must_exist]
153
205
 
154
- bad = %w[not raise throw send output be_silent]
206
+ bad = %w[not raise throw send output be_silent verify]
155
207
 
156
- expected_wonts = expected_musts.map { |m| m.sub(/^must/, "wont") }
208
+ expected_wonts = expected_musts.map { |m| m.sub("must", "wont") }.sort
157
209
  expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
158
210
 
159
- musts.must_equal expected_musts
160
- wonts.must_equal expected_wonts
211
+ _(musts).must_equal expected_musts
212
+ _(wonts).must_equal expected_wonts
161
213
  end
162
214
 
163
215
  it "needs to raise if an expected exception is not raised" do
164
216
  @assertion_count -= 2 # no positive test
165
217
 
166
218
  assert_triggered "RuntimeError expected but nothing was raised." do
167
- proc { 42 }.must_raise RuntimeError
219
+ expect { 42 }.must_raise RuntimeError
168
220
  end
169
221
 
170
222
  assert_triggered "msg.\nRuntimeError expected but nothing was raised." do
171
- proc { 42 }.must_raise RuntimeError, "msg"
223
+ expect { 42 }.must_raise RuntimeError, "msg"
172
224
  end
173
225
  end
174
226
 
175
227
  it "needs to verify binary messages" do
176
- 42.wont_be(:<, 24).must_equal false
228
+ assert_success _(42).wont_be(:<, 24)
177
229
 
178
230
  assert_triggered "Expected 24 to not be < 42." do
179
- 24.wont_be :<, 42
231
+ _(24).wont_be :<, 42
180
232
  end
181
233
 
182
234
  assert_triggered "msg.\nExpected 24 to not be < 42." do
183
- 24.wont_be :<, 42, "msg"
235
+ _(24).wont_be :<, 42, "msg"
184
236
  end
185
237
  end
186
238
 
187
239
  it "needs to verify emptyness" do
188
240
  @assertion_count += 3 # empty is 2 assertions
189
241
 
190
- [].must_be_empty.must_equal true
242
+ assert_success _([]).must_be_empty
191
243
 
192
244
  assert_triggered "Expected [42] to be empty." do
193
- [42].must_be_empty
245
+ _([42]).must_be_empty
194
246
  end
195
247
 
196
248
  assert_triggered "msg.\nExpected [42] to be empty." do
197
- [42].must_be_empty "msg"
249
+ _([42]).must_be_empty "msg"
198
250
  end
199
251
  end
200
252
 
201
253
  it "needs to verify equality" do
202
254
  @assertion_count += 1
203
255
 
204
- (6 * 7).must_equal(42).must_equal true
256
+ assert_success _(6 * 7).must_equal(42)
205
257
 
206
258
  assert_triggered "Expected: 42\n Actual: 54" do
207
- (6 * 9).must_equal 42
259
+ _(6 * 9).must_equal 42
208
260
  end
209
261
 
210
262
  assert_triggered "msg.\nExpected: 42\n Actual: 54" do
211
- (6 * 9).must_equal 42, "msg"
263
+ _(6 * 9).must_equal 42, "msg"
212
264
  end
213
265
 
214
- assert_triggered(/^-42\n\+#<Proc:0xXXXXXX@PATH>\n/) do
215
- proc { 42 }.must_equal 42 # proc isn't called, so expectation fails
266
+ assert_triggered(/^-42\n\+#<Proc:0xXXXXXX[ @]PATH>\n/) do
267
+ _(proc { 42 }).must_equal 42 # proc isn't called, so expectation fails
216
268
  end
217
269
  end
218
270
 
219
271
  it "needs to warn on equality with nil" do
220
- @assertion_count += 1 # extra test
272
+ @assertion_count = 3
273
+ @assertion_count += 2 unless error_on_warn? # 2 extra assertions
221
274
 
222
- out, err = capture_io do
223
- nil.must_equal(nil).must_equal true
224
- end
275
+ exp = /.*?test_minitest_\w+.rb:\d+: warning: DEPRECATED: Use assert_nil if expecting nil. This will fail in Minitest 6./
225
276
 
226
- exp = "DEPRECATED: Use assert_nil if expecting nil from #{__FILE__}:#{__LINE__-3}. " \
227
- "This will fail in Minitest 6.\n"
228
- exp = "" if $-w.nil?
229
-
230
- assert_empty out
231
- assert_equal exp, err
277
+ assert_deprecation exp do
278
+ assert_success _(nil).must_equal(nil)
279
+ end
232
280
  end
233
281
 
234
282
  it "needs to verify floats outside a delta" do
235
283
  @assertion_count += 1 # extra test
236
284
 
237
- 24.wont_be_close_to(42).must_equal false
285
+ assert_success _(24).wont_be_close_to(42)
238
286
 
239
287
  assert_triggered "Expected |42 - 42.0| (0.0) to not be <= 0.001." do
240
- (6 * 7.0).wont_be_close_to 42
288
+ _(6 * 7.0).wont_be_close_to 42
241
289
  end
242
290
 
243
- x = maglev? ? "1.0000000000000001e-05" : "1.0e-05"
291
+ x = "1.0e-05"
244
292
  assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
245
- (6 * 7.0).wont_be_close_to 42, 0.00001
293
+ _(6 * 7.0).wont_be_close_to 42, 0.00001
246
294
  end
247
295
 
248
296
  assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be <= #{x}." do
249
- (6 * 7.0).wont_be_close_to 42, 0.00001, "msg"
297
+ _(6 * 7.0).wont_be_close_to 42, 0.00001, "msg"
250
298
  end
251
299
  end
252
300
 
253
301
  it "needs to verify floats outside an epsilon" do
254
302
  @assertion_count += 1 # extra test
255
303
 
256
- 24.wont_be_within_epsilon(42).must_equal false
304
+ assert_success _(24).wont_be_within_epsilon(42)
257
305
 
258
- x = maglev? ? "0.042000000000000003" : "0.042"
306
+ x = "0.042"
259
307
  assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
260
- (6 * 7.0).wont_be_within_epsilon 42
308
+ _(6 * 7.0).wont_be_within_epsilon 42
261
309
  end
262
310
 
263
- x = maglev? ? "0.00042000000000000002" : "0.00042"
311
+ x = "0.00042"
264
312
  assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
265
- (6 * 7.0).wont_be_within_epsilon 42, 0.00001
313
+ _(6 * 7.0).wont_be_within_epsilon 42, 0.00001
266
314
  end
267
315
 
268
316
  assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be <= #{x}." do
269
- (6 * 7.0).wont_be_within_epsilon 42, 0.00001, "msg"
317
+ _(6 * 7.0).wont_be_within_epsilon 42, 0.00001, "msg"
270
318
  end
271
319
  end
272
320
 
273
321
  it "needs to verify floats within a delta" do
274
322
  @assertion_count += 1 # extra test
275
323
 
276
- (6.0 * 7).must_be_close_to(42.0).must_equal true
324
+ assert_success _(6.0 * 7).must_be_close_to(42.0)
277
325
 
278
326
  assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.001." do
279
- (1.0 / 100).must_be_close_to 0.0
327
+ _(1.0 / 100).must_be_close_to 0.0
280
328
  end
281
329
 
282
- x = maglev? ? "9.9999999999999995e-07" : "1.0e-06"
330
+ x = "1.0e-06"
283
331
  assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do
284
- (1.0 / 1000).must_be_close_to 0.0, 0.000001
332
+ _(1.0 / 1000).must_be_close_to 0.0, 0.000001
285
333
  end
286
334
 
287
335
  assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be <= #{x}." do
288
- (1.0 / 1000).must_be_close_to 0.0, 0.000001, "msg"
336
+ _(1.0 / 1000).must_be_close_to 0.0, 0.000001, "msg"
289
337
  end
290
338
  end
291
339
 
292
340
  it "needs to verify floats within an epsilon" do
293
341
  @assertion_count += 1 # extra test
294
342
 
295
- (6.0 * 7).must_be_within_epsilon(42.0).must_equal true
343
+ assert_success _(6.0 * 7).must_be_within_epsilon(42.0)
296
344
 
297
345
  assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.0." do
298
- (1.0 / 100).must_be_within_epsilon 0.0
346
+ _(1.0 / 100).must_be_within_epsilon 0.0
299
347
  end
300
348
 
301
349
  assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= 0.0." do
302
- (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001
350
+ _(1.0 / 1000).must_be_within_epsilon 0.0, 0.000001
303
351
  end
304
352
 
305
353
  assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be <= 0.0." do
306
- (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001, "msg"
354
+ _(1.0 / 1000).must_be_within_epsilon 0.0, 0.000001, "msg"
307
355
  end
308
356
  end
309
357
 
310
358
  it "needs to verify identity" do
311
- 1.must_be_same_as(1).must_equal true
359
+ assert_success _(1).must_be_same_as(1)
312
360
 
313
361
  assert_triggered "Expected 1 (oid=N) to be the same as 2 (oid=N)." do
314
- 1.must_be_same_as 2
362
+ _(1).must_be_same_as 2
315
363
  end
316
364
 
317
365
  assert_triggered "msg.\nExpected 1 (oid=N) to be the same as 2 (oid=N)." do
318
- 1.must_be_same_as 2, "msg"
366
+ _(1).must_be_same_as 2, "msg"
319
367
  end
320
368
  end
321
369
 
322
370
  it "needs to verify inequality" do
323
371
  @assertion_count += 2
324
- 42.wont_equal(6 * 9).must_equal false
325
- proc {}.wont_equal(42).must_equal false
372
+ assert_success _(42).wont_equal(6 * 9)
373
+ assert_success _(proc {}).wont_equal(42)
326
374
 
327
375
  assert_triggered "Expected 1 to not be equal to 1." do
328
- 1.wont_equal 1
376
+ _(1).wont_equal 1
329
377
  end
330
378
 
331
379
  assert_triggered "msg.\nExpected 1 to not be equal to 1." do
332
- 1.wont_equal 1, "msg"
380
+ _(1).wont_equal 1, "msg"
333
381
  end
334
382
  end
335
383
 
336
384
  it "needs to verify instances of a class" do
337
- 42.wont_be_instance_of(String).must_equal false
385
+ assert_success _(42).wont_be_instance_of(String)
338
386
 
339
- assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
340
- 42.wont_be_kind_of Int
387
+ assert_triggered "Expected 42 to not be a kind of Integer." do
388
+ _(42).wont_be_kind_of Integer
341
389
  end
342
390
 
343
- assert_triggered "msg.\nExpected 42 to not be an instance of #{Int.name}." do
344
- 42.wont_be_instance_of Int, "msg"
391
+ assert_triggered "msg.\nExpected 42 to not be an instance of Integer." do
392
+ _(42).wont_be_instance_of Integer, "msg"
345
393
  end
346
394
  end
347
395
 
348
396
  it "needs to verify kinds of a class" do
349
397
  @assertion_count += 2
350
398
 
351
- 42.wont_be_kind_of(String).must_equal false
352
- proc {}.wont_be_kind_of(String).must_equal false
399
+ assert_success _(42).wont_be_kind_of(String)
400
+ assert_success _(proc {}).wont_be_kind_of(String)
353
401
 
354
- assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
355
- 42.wont_be_kind_of Int
402
+ assert_triggered "Expected 42 to not be a kind of Integer." do
403
+ _(42).wont_be_kind_of Integer
356
404
  end
357
405
 
358
- assert_triggered "msg.\nExpected 42 to not be a kind of #{Int.name}." do
359
- 42.wont_be_kind_of Int, "msg"
406
+ assert_triggered "msg.\nExpected 42 to not be a kind of Integer." do
407
+ _(42).wont_be_kind_of Integer, "msg"
360
408
  end
361
409
  end
362
410
 
363
411
  it "needs to verify kinds of objects" do
364
412
  @assertion_count += 3 # extra test
365
413
 
366
- (6 * 7).must_be_kind_of(Int).must_equal true
367
- (6 * 7).must_be_kind_of(Numeric).must_equal true
414
+ assert_success _(6 * 7).must_be_kind_of(Integer)
415
+ assert_success _(6 * 7).must_be_kind_of(Numeric)
368
416
 
369
- assert_triggered "Expected 42 to be a kind of String, not #{Int.name}." do
370
- (6 * 7).must_be_kind_of String
417
+ assert_triggered "Expected 42 to be a kind of String, not Integer." do
418
+ _(6 * 7).must_be_kind_of String
371
419
  end
372
420
 
373
- assert_triggered "msg.\nExpected 42 to be a kind of String, not #{Int.name}." do
374
- (6 * 7).must_be_kind_of String, "msg"
421
+ assert_triggered "msg.\nExpected 42 to be a kind of String, not Integer." do
422
+ _(6 * 7).must_be_kind_of String, "msg"
375
423
  end
376
424
 
377
425
  exp = "Expected #<Proc:0xXXXXXX@PATH> to be a kind of String, not Proc."
378
426
  assert_triggered exp do
379
- proc {}.must_be_kind_of String
427
+ _(proc {}).must_be_kind_of String
380
428
  end
381
429
  end
382
430
 
383
431
  it "needs to verify mismatch" do
384
432
  @assertion_count += 3 # match is 2
385
433
 
386
- "blah".wont_match(/\d+/).must_equal false
434
+ assert_success _("blah").wont_match(/\d+/)
387
435
 
388
436
  assert_triggered "Expected /\\w+/ to not match \"blah\"." do
389
- "blah".wont_match(/\w+/)
437
+ _("blah").wont_match(/\w+/)
390
438
  end
391
439
 
392
440
  assert_triggered "msg.\nExpected /\\w+/ to not match \"blah\"." do
393
- "blah".wont_match(/\w+/, "msg")
441
+ _("blah").wont_match(/\w+/, "msg")
394
442
  end
395
443
  end
396
444
 
397
445
  it "needs to verify nil" do
398
- nil.must_be_nil.must_equal true
446
+ assert_success _(nil).must_be_nil
399
447
 
400
448
  assert_triggered "Expected 42 to be nil." do
401
- 42.must_be_nil
449
+ _(42).must_be_nil
402
450
  end
403
451
 
404
452
  assert_triggered "msg.\nExpected 42 to be nil." do
405
- 42.must_be_nil "msg"
453
+ _(42).must_be_nil "msg"
406
454
  end
407
455
  end
408
456
 
409
457
  it "needs to verify non-emptyness" do
410
458
  @assertion_count += 3 # empty is 2 assertions
411
459
 
412
- ["some item"].wont_be_empty.must_equal false
460
+ assert_success _(["some item"]).wont_be_empty
413
461
 
414
462
  assert_triggered "Expected [] to not be empty." do
415
- [].wont_be_empty
463
+ _([]).wont_be_empty
416
464
  end
417
465
 
418
466
  assert_triggered "msg.\nExpected [] to not be empty." do
419
- [].wont_be_empty "msg"
467
+ _([]).wont_be_empty "msg"
420
468
  end
421
469
  end
422
470
 
423
471
  it "needs to verify non-identity" do
424
- 1.wont_be_same_as(2).must_equal false
472
+ assert_success _(1).wont_be_same_as(2)
425
473
 
426
474
  assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
427
- 1.wont_be_same_as 1
475
+ _(1).wont_be_same_as 1
428
476
  end
429
477
 
430
478
  assert_triggered "msg.\nExpected 1 (oid=N) to not be the same as 1 (oid=N)." do
431
- 1.wont_be_same_as 1, "msg"
479
+ _(1).wont_be_same_as 1, "msg"
432
480
  end
433
481
  end
434
482
 
435
483
  it "needs to verify non-nil" do
436
- 42.wont_be_nil.must_equal false
484
+ assert_success _(42).wont_be_nil
437
485
 
438
486
  assert_triggered "Expected nil to not be nil." do
439
- nil.wont_be_nil
487
+ _(nil).wont_be_nil
440
488
  end
441
489
 
442
490
  assert_triggered "msg.\nExpected nil to not be nil." do
443
- nil.wont_be_nil "msg"
491
+ _(nil).wont_be_nil "msg"
444
492
  end
445
493
  end
446
494
 
447
495
  it "needs to verify objects not responding to a message" do
448
- "".wont_respond_to(:woot!).must_equal false
496
+ assert_success _("").wont_respond_to(:woot!)
449
497
 
450
498
  assert_triggered "Expected \"\" to not respond to to_s." do
451
- "".wont_respond_to :to_s
499
+ _("").wont_respond_to :to_s
452
500
  end
453
501
 
454
502
  assert_triggered "msg.\nExpected \"\" to not respond to to_s." do
455
- "".wont_respond_to :to_s, "msg"
503
+ _("").wont_respond_to :to_s, "msg"
456
504
  end
457
505
  end
458
506
 
459
507
  it "needs to verify output in stderr" do
460
508
  @assertion_count -= 1 # no msg
461
509
 
462
- proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true
510
+ assert_success expect { $stderr.print "blah" }.must_output(nil, "blah")
463
511
 
464
512
  assert_triggered "In stderr.\nExpected: \"blah\"\n Actual: \"xxx\"" do
465
- proc { $stderr.print "xxx" }.must_output(nil, "blah")
513
+ expect { $stderr.print "xxx" }.must_output(nil, "blah")
466
514
  end
467
515
  end
468
516
 
469
517
  it "needs to verify output in stdout" do
470
518
  @assertion_count -= 1 # no msg
471
519
 
472
- proc { print "blah" }.must_output("blah").must_equal true
520
+ assert_success expect { print "blah" }.must_output("blah")
473
521
 
474
522
  assert_triggered "In stdout.\nExpected: \"blah\"\n Actual: \"xxx\"" do
475
- proc { print "xxx" }.must_output("blah")
523
+ expect { print "xxx" }.must_output("blah")
476
524
  end
477
525
  end
478
526
 
479
527
  it "needs to verify regexp matches" do
480
528
  @assertion_count += 3 # must_match is 2 assertions
481
529
 
482
- "blah".must_match(/\w+/).must_equal true
530
+ assert_kind_of MatchData, _("blah").must_match(/\w+/)
483
531
 
484
532
  assert_triggered "Expected /\\d+/ to match \"blah\"." do
485
- "blah".must_match(/\d+/)
533
+ _("blah").must_match(/\d+/)
486
534
  end
487
535
 
488
536
  assert_triggered "msg.\nExpected /\\d+/ to match \"blah\"." do
489
- "blah".must_match(/\d+/, "msg")
537
+ _("blah").must_match(/\d+/, "msg")
490
538
  end
491
539
  end
492
540
 
@@ -508,96 +556,148 @@ describe Minitest::Spec do
508
556
  end
509
557
 
510
558
  it "can NOT use must_equal in a thread. It must use expect in a thread" do
511
- assert_raises NoMethodError do
559
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
560
+
561
+ assert_raises RuntimeError, Minitest::UnexpectedWarning do
512
562
  capture_io do
513
563
  Thread.new { (1 + 1).must_equal 2 }.join
514
564
  end
515
565
  end
516
566
  end
567
+
568
+ it "fails gracefully when expectation used outside of `it`" do
569
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
570
+
571
+ @assertion_count += 2 # assert_match is compound
572
+
573
+ e = assert_raises RuntimeError, Minitest::UnexpectedWarning do
574
+ capture_io do
575
+ Thread.new { # forces ctx to be nil
576
+ describe "woot" do
577
+ (1 + 1).must_equal 2
578
+ end
579
+ }.join
580
+ end
581
+ end
582
+
583
+ exp = "Calling #must_equal outside of test."
584
+ exp = "DEPRECATED: global use of must_equal from" if error_on_warn?
585
+
586
+ assert_match exp, e.message
587
+ end
588
+
589
+ it "deprecates expectation used without _" do
590
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
591
+
592
+ @assertion_count += 1
593
+ @assertion_count += 2 unless error_on_warn?
594
+
595
+ exp = /DEPRECATED: global use of must_equal from/
596
+
597
+ assert_deprecation exp do
598
+ (1 + 1).must_equal 2
599
+ end
600
+ end
601
+
602
+ # https://github.com/seattlerb/minitest/issues/837
603
+ # https://github.com/rails/rails/pull/39304
604
+ it "deprecates expectation used without _ with empty backtrace_filter" do
605
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
606
+
607
+ @assertion_count += 1
608
+ @assertion_count += 2 unless error_on_warn?
609
+
610
+ exp = /DEPRECATED: global use of must_equal from/
611
+
612
+ with_empty_backtrace_filter do
613
+ assert_deprecation exp do
614
+ (1 + 1).must_equal 2
615
+ end
616
+ end
617
+ end
517
618
  end
518
619
 
519
620
  it "needs to verify throw" do
520
- @assertion_count += 2 # 2 extra tests
621
+ @assertion_count += 4 # 2 extra tests
521
622
 
522
- proc { throw :blah }.must_throw(:blah).must_equal true
623
+ assert_nil expect { throw :blah }.must_throw(:blah)
624
+ assert_equal 42, expect { throw :blah, 42 }.must_throw(:blah)
523
625
 
524
626
  assert_triggered "Expected :blah to have been thrown." do
525
- proc {}.must_throw :blah
627
+ expect {}.must_throw :blah
526
628
  end
527
629
 
528
630
  assert_triggered "Expected :blah to have been thrown, not :xxx." do
529
- proc { throw :xxx }.must_throw :blah
631
+ expect { throw :xxx }.must_throw :blah
530
632
  end
531
633
 
532
634
  assert_triggered "msg.\nExpected :blah to have been thrown." do
533
- proc {}.must_throw :blah, "msg"
635
+ expect {}.must_throw :blah, "msg"
534
636
  end
535
637
 
536
638
  assert_triggered "msg.\nExpected :blah to have been thrown, not :xxx." do
537
- proc { throw :xxx }.must_throw :blah, "msg"
639
+ expect { throw :xxx }.must_throw :blah, "msg"
538
640
  end
539
641
  end
540
642
 
541
643
  it "needs to verify types of objects" do
542
- (6 * 7).must_be_instance_of(Int).must_equal true
644
+ assert_success _(6 * 7).must_be_instance_of(Integer)
543
645
 
544
- exp = "Expected 42 to be an instance of String, not #{Int.name}."
646
+ exp = "Expected 42 to be an instance of String, not Integer."
545
647
 
546
648
  assert_triggered exp do
547
- (6 * 7).must_be_instance_of String
649
+ _(6 * 7).must_be_instance_of String
548
650
  end
549
651
 
550
652
  assert_triggered "msg.\n#{exp}" do
551
- (6 * 7).must_be_instance_of String, "msg"
653
+ _(6 * 7).must_be_instance_of String, "msg"
552
654
  end
553
655
  end
554
656
 
555
657
  it "needs to verify using any (negative) predicate" do
556
658
  @assertion_count -= 1 # doesn"t take a message
557
659
 
558
- "blah".wont_be(:empty?).must_equal false
660
+ assert_success _("blah").wont_be(:empty?)
559
661
 
560
662
  assert_triggered "Expected \"\" to not be empty?." do
561
- "".wont_be :empty?
663
+ _("").wont_be :empty?
562
664
  end
563
665
  end
564
666
 
565
667
  it "needs to verify using any binary operator" do
566
668
  @assertion_count -= 1 # no msg
567
669
 
568
- 41.must_be(:<, 42).must_equal true
670
+ assert_success _(41).must_be(:<, 42)
569
671
 
570
672
  assert_triggered "Expected 42 to be < 41." do
571
- 42.must_be(:<, 41)
673
+ _(42).must_be :<, 41
572
674
  end
573
675
  end
574
676
 
575
677
  it "needs to verify using any predicate" do
576
678
  @assertion_count -= 1 # no msg
577
679
 
578
- "".must_be(:empty?).must_equal true
680
+ assert_success _("").must_be(:empty?)
579
681
 
580
682
  assert_triggered "Expected \"blah\" to be empty?." do
581
- "blah".must_be :empty?
683
+ _("blah").must_be :empty?
582
684
  end
583
685
  end
584
686
 
585
687
  it "needs to verify using respond_to" do
586
- 42.must_respond_to(:+).must_equal true
688
+ assert_success _(42).must_respond_to(:+)
587
689
 
588
- assert_triggered "Expected 42 (#{Int.name}) to respond to #clear." do
589
- 42.must_respond_to :clear
690
+ assert_triggered "Expected 42 (Integer) to respond to #clear." do
691
+ _(42).must_respond_to :clear
590
692
  end
591
693
 
592
- assert_triggered "msg.\nExpected 42 (#{Int.name}) to respond to #clear." do
593
- 42.must_respond_to :clear, "msg"
694
+ assert_triggered "msg.\nExpected 42 (Integer) to respond to #clear." do
695
+ _(42).must_respond_to :clear, "msg"
594
696
  end
595
697
  end
596
698
  end
597
699
 
598
700
  describe Minitest::Spec, :let do
599
- i_suck_and_my_tests_are_order_dependent!
600
-
601
701
  def _count
602
702
  $let_count ||= 0
603
703
  end
@@ -608,47 +708,48 @@ describe Minitest::Spec, :let do
608
708
  end
609
709
 
610
710
  it "is evaluated once per example" do
611
- _count.must_equal 0
711
+ exp = _count + 1
612
712
 
613
- count.must_equal 1
614
- count.must_equal 1
713
+ _(count).must_equal exp
714
+ _(count).must_equal exp
615
715
 
616
- _count.must_equal 1
716
+ _(_count).must_equal exp
617
717
  end
618
718
 
619
719
  it "is REALLY evaluated once per example" do
620
- _count.must_equal 1
720
+ exp = _count + 1
621
721
 
622
- count.must_equal 2
623
- count.must_equal 2
722
+ _(count).must_equal exp
723
+ _(count).must_equal exp
624
724
 
625
- _count.must_equal 2
725
+ _(_count).must_equal exp
626
726
  end
627
727
 
628
728
  it 'raises an error if the name begins with "test"' do
629
- proc { self.class.let(:test_value) { true } }.must_raise ArgumentError
729
+ expect { self.class.let(:test_value) { true } }.must_raise ArgumentError
630
730
  end
631
731
 
632
732
  it "raises an error if the name shadows a normal instance method" do
633
- proc { self.class.let(:message) { true } }.must_raise ArgumentError
733
+ expect { self.class.let(:message) { true } }.must_raise ArgumentError
634
734
  end
635
735
 
636
736
  it "doesn't raise an error if it is just another let" do
637
- proc do
737
+ v = proc do
638
738
  describe :outer do
639
- let(:bar)
739
+ let :bar
640
740
  describe :inner do
641
- let(:bar)
741
+ let :bar
642
742
  end
643
743
  end
644
744
  :good
645
- end.call.must_equal :good
745
+ end.call
746
+ _(v).must_equal :good
646
747
  end
647
748
 
648
749
  it "procs come after dont_flip" do
649
750
  p = proc {}
650
751
  assert_respond_to p, :call
651
- p.must_respond_to :call
752
+ _(p).must_respond_to :call
652
753
  end
653
754
  end
654
755
 
@@ -662,13 +763,17 @@ describe Minitest::Spec, :subject do
662
763
  end
663
764
 
664
765
  it "is evaluated once per example" do
665
- subject.must_equal 1
666
- subject.must_equal 1
667
- subject_evaluation_count.must_equal 1
766
+ _(subject).must_equal 1
767
+ _(subject).must_equal 1
768
+ _(subject_evaluation_count).must_equal 1
668
769
  end
669
770
  end
670
771
 
671
772
  class TestMetaStatic < Minitest::Test
773
+ def assert_method_count expected, klass
774
+ assert_equal expected, klass.public_instance_methods.grep(/^test_/).count
775
+ end
776
+
672
777
  def test_children
673
778
  Minitest::Spec.children.clear # prevents parallel run
674
779
 
@@ -702,8 +807,8 @@ class TestMetaStatic < Minitest::Test
702
807
  end
703
808
  end
704
809
 
705
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
706
- assert_equal 1, inner.public_instance_methods.grep(/^test_/).count
810
+ assert_method_count 1, outer
811
+ assert_method_count 1, inner
707
812
  end
708
813
 
709
814
  def test_it_wont_add_test_methods_to_children
@@ -717,15 +822,17 @@ class TestMetaStatic < Minitest::Test
717
822
  end
718
823
  end
719
824
 
720
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
721
- assert_equal 0, inner.public_instance_methods.grep(/^test_/).count
825
+ assert_method_count 1, outer
826
+ assert_method_count 0, inner
722
827
  end
723
828
  end
724
829
 
725
- require "minitest/metametameta"
726
-
727
830
  class TestMeta < MetaMetaMetaTestCase
728
- parallelize_me!
831
+ # do not call parallelize_me! here because specs use register_spec_type globally
832
+
833
+ def assert_defined_methods expected, klass
834
+ assert_equal expected, klass.instance_methods(false).sort.map(&:to_s)
835
+ end
729
836
 
730
837
  def util_structure
731
838
  y = z = nil
@@ -795,11 +902,11 @@ class TestMeta < MetaMetaMetaTestCase
795
902
  def test_bug_dsl_expectations
796
903
  spec_class = Class.new MiniSpecB do
797
904
  it "should work" do
798
- 0.must_equal 0
905
+ _(0).must_equal 0
799
906
  end
800
907
  end
801
908
 
802
- test_name = spec_class.instance_methods.sort.grep(/test/).first
909
+ test_name = spec_class.instance_methods.sort.grep(/test_/).first
803
910
 
804
911
  spec = spec_class.new test_name
805
912
 
@@ -820,6 +927,16 @@ class TestMeta < MetaMetaMetaTestCase
820
927
  assert_equal "ExampleB::random_method::addl_context", spec_c.name
821
928
  end
822
929
 
930
+ def test_inspect
931
+ spec_a = describe ExampleA do; end
932
+ spec_b = describe ExampleB, :random_method do; end
933
+ spec_c = describe ExampleB, :random_method, :addl_context do; end
934
+
935
+ assert_equal "ExampleA", spec_a.inspect
936
+ assert_equal "ExampleB::random_method", spec_b.inspect
937
+ assert_equal "ExampleB::random_method::addl_context", spec_c.inspect
938
+ end
939
+
823
940
  def test_name2
824
941
  assert_equal "NamedExampleA", NamedExampleA.name
825
942
  assert_equal "NamedExampleB", NamedExampleB.name
@@ -832,6 +949,23 @@ class TestMeta < MetaMetaMetaTestCase
832
949
  assert_equal "ExampleB::random_method", spec_b.name
833
950
  end
834
951
 
952
+ def test_name_inside_class
953
+ spec_a = nil
954
+ spec_b = nil
955
+ inside_class_example = Class.new Minitest::Spec
956
+ Object.const_set :InsideClassExample, inside_class_example
957
+ inside_class_example.class_eval do
958
+ spec_a = describe "a" do
959
+ spec_b = describe "b" do; end
960
+ end
961
+ end
962
+
963
+ assert_equal "InsideClassExample::a", spec_a.name
964
+ assert_equal "InsideClassExample::a::b", spec_b.name
965
+ ensure
966
+ Object.send :remove_const, :InsideClassExample
967
+ end
968
+
835
969
  def test_structure
836
970
  x, y, z, * = util_structure
837
971
 
@@ -848,9 +982,9 @@ class TestMeta < MetaMetaMetaTestCase
848
982
  inner_methods2 = inner_methods1 +
849
983
  %w[test_0002_anonymous test_0003_anonymous]
850
984
 
851
- assert_equal top_methods, x.instance_methods(false).sort.map(&:to_s)
852
- assert_equal inner_methods1, y.instance_methods(false).sort.map(&:to_s)
853
- assert_equal inner_methods2, z.instance_methods(false).sort.map(&:to_s)
985
+ assert_defined_methods top_methods, x
986
+ assert_defined_methods inner_methods1, y
987
+ assert_defined_methods inner_methods2, z
854
988
  end
855
989
 
856
990
  def test_structure_postfix_it
@@ -867,8 +1001,8 @@ class TestMeta < MetaMetaMetaTestCase
867
1001
  it "inner-it" do end
868
1002
  end
869
1003
 
870
- assert_equal %w[test_0001_inner-it], y.instance_methods(false).map(&:to_s)
871
- assert_equal %w[test_0001_inner-it], z.instance_methods(false).map(&:to_s)
1004
+ assert_defined_methods %w[test_0001_inner-it], y
1005
+ assert_defined_methods %w[test_0001_inner-it], z
872
1006
  end
873
1007
 
874
1008
  def test_setup_teardown_behavior
@@ -894,14 +1028,15 @@ class TestMeta < MetaMetaMetaTestCase
894
1028
  z = describe "second thingy" do end
895
1029
  end
896
1030
 
897
- test_methods = ["test_0001_top level it",
898
- "test_0002_не латинские &いった α, β, γ, δ, ε hello!!! world",
1031
+ test_methods = [
1032
+ "test_0001_top level it",
1033
+ "test_0002_не латинские &いった α, β, γ, δ, ε hello!!! world",
899
1034
  ].sort
900
1035
 
901
1036
  assert_equal test_methods, [x1, x2]
902
- assert_equal test_methods, x.instance_methods.grep(/^test/).map(&:to_s).sort
903
- assert_equal [], y.instance_methods.grep(/^test/)
904
- assert_equal [], z.instance_methods.grep(/^test/)
1037
+ assert_defined_methods test_methods, x
1038
+ assert_defined_methods [], y
1039
+ assert_defined_methods [], z
905
1040
  end
906
1041
 
907
1042
  def test_structure_subclasses
@@ -947,18 +1082,20 @@ class TestSpecInTestCase < MetaMetaMetaTestCase
947
1082
  end
948
1083
 
949
1084
  def test_expectation
950
- @tc.assert_equal true, 1.must_equal(1)
1085
+ @tc.assert_equal true, _(1).must_equal(1)
951
1086
  end
952
1087
 
953
1088
  def test_expectation_triggered
954
1089
  assert_triggered "Expected: 2\n Actual: 1" do
955
- 1.must_equal 2
1090
+ _(1).must_equal 2
956
1091
  end
957
1092
  end
958
1093
 
1094
+ include Minitest::Spec::DSL::InstanceMethods
1095
+
959
1096
  def test_expectation_with_a_message
960
1097
  assert_triggered "woot.\nExpected: 2\n Actual: 1" do
961
- 1.must_equal 2, "woot"
1098
+ _(1).must_equal 2, "woot"
962
1099
  end
963
1100
  end
964
1101
  end
@@ -985,3 +1122,38 @@ class ValueMonadTest < Minitest::Test
985
1122
  assert_equal "c", struct.expect
986
1123
  end
987
1124
  end
1125
+
1126
+ describe Minitest::Spec, :infect_an_assertion do
1127
+ class << self
1128
+ attr_accessor :infect_mock
1129
+ end
1130
+
1131
+ def assert_infects exp, act, msg = nil, foo: nil, bar: nil
1132
+ self.class.infect_mock.assert_infects exp, act, msg, foo: foo, bar: bar
1133
+ end
1134
+
1135
+ infect_an_assertion :assert_infects, :must_infect
1136
+ infect_an_assertion :assert_infects, :must_infect_without_flipping, :dont_flip
1137
+
1138
+ it "infects assertions with kwargs" do
1139
+ mock = Minitest::Mock.new
1140
+ mock.expect :assert_infects, true, [:exp, :act, nil], foo: :foo, bar: :bar
1141
+
1142
+ self.class.infect_mock = mock
1143
+
1144
+ _(:act).must_infect :exp, foo: :foo, bar: :bar
1145
+
1146
+ assert_mock mock
1147
+ end
1148
+
1149
+ it "infects assertions with kwargs (dont_flip)" do
1150
+ mock = Minitest::Mock.new
1151
+ mock.expect :assert_infects, true, [:act, :exp, nil], foo: :foo, bar: :bar
1152
+
1153
+ self.class.infect_mock = mock
1154
+
1155
+ _(:act).must_infect_without_flipping :exp, foo: :foo, bar: :bar
1156
+
1157
+ assert_mock mock
1158
+ end
1159
+ end