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