minitest 5.11.3 → 5.17.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,11 @@ 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"
114
137
  end
115
138
  end
116
139
 
@@ -118,20 +141,22 @@ describe Minitest::Spec do
118
141
  @assertion_count -= 1 # no msg
119
142
  @assertion_count += 2 # assert_output is 2 assertions
120
143
 
121
- proc {}.must_be_silent.must_equal true
144
+ assert_success expect {}.must_be_silent
122
145
 
123
146
  assert_triggered "In stdout.\nExpected: \"\"\n Actual: \"xxx\"" do
124
- proc { print "xxx" }.must_be_silent
147
+ expect { print "xxx" }.must_be_silent
125
148
  end
126
149
  end
127
150
 
128
151
  it "needs to have all methods named well" do
152
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
153
+
129
154
  @assertion_count = 2
130
155
 
131
- methods = Object.public_instance_methods.find_all { |n| n =~ /^must|^wont/ }
156
+ methods = Minitest::Expectations.public_instance_methods.grep(/must|wont/)
132
157
  methods.map!(&:to_s) if Symbol === methods.first
133
158
 
134
- musts, wonts = methods.sort.partition { |m| m =~ /^must/ }
159
+ musts, wonts = methods.sort.partition { |m| m =~ /must/ }
135
160
 
136
161
  expected_musts = %w[must_be
137
162
  must_be_close_to
@@ -149,70 +174,71 @@ describe Minitest::Spec do
149
174
  must_output
150
175
  must_raise
151
176
  must_respond_to
152
- must_throw]
177
+ must_throw
178
+ path_must_exist]
153
179
 
154
180
  bad = %w[not raise throw send output be_silent]
155
181
 
156
- expected_wonts = expected_musts.map { |m| m.sub(/^must/, "wont") }
182
+ expected_wonts = expected_musts.map { |m| m.sub(/must/, "wont") }.sort
157
183
  expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
158
184
 
159
- musts.must_equal expected_musts
160
- wonts.must_equal expected_wonts
185
+ _(musts).must_equal expected_musts
186
+ _(wonts).must_equal expected_wonts
161
187
  end
162
188
 
163
189
  it "needs to raise if an expected exception is not raised" do
164
190
  @assertion_count -= 2 # no positive test
165
191
 
166
192
  assert_triggered "RuntimeError expected but nothing was raised." do
167
- proc { 42 }.must_raise RuntimeError
193
+ expect { 42 }.must_raise RuntimeError
168
194
  end
169
195
 
170
196
  assert_triggered "msg.\nRuntimeError expected but nothing was raised." do
171
- proc { 42 }.must_raise RuntimeError, "msg"
197
+ expect { 42 }.must_raise RuntimeError, "msg"
172
198
  end
173
199
  end
174
200
 
175
201
  it "needs to verify binary messages" do
176
- 42.wont_be(:<, 24).must_equal false
202
+ assert_success _(42).wont_be(:<, 24)
177
203
 
178
204
  assert_triggered "Expected 24 to not be < 42." do
179
- 24.wont_be :<, 42
205
+ _(24).wont_be :<, 42
180
206
  end
181
207
 
182
208
  assert_triggered "msg.\nExpected 24 to not be < 42." do
183
- 24.wont_be :<, 42, "msg"
209
+ _(24).wont_be :<, 42, "msg"
184
210
  end
185
211
  end
186
212
 
187
213
  it "needs to verify emptyness" do
188
214
  @assertion_count += 3 # empty is 2 assertions
189
215
 
190
- [].must_be_empty.must_equal true
216
+ assert_success _([]).must_be_empty
191
217
 
192
218
  assert_triggered "Expected [42] to be empty." do
193
- [42].must_be_empty
219
+ _([42]).must_be_empty
194
220
  end
195
221
 
196
222
  assert_triggered "msg.\nExpected [42] to be empty." do
197
- [42].must_be_empty "msg"
223
+ _([42]).must_be_empty "msg"
198
224
  end
199
225
  end
200
226
 
201
227
  it "needs to verify equality" do
202
228
  @assertion_count += 1
203
229
 
204
- (6 * 7).must_equal(42).must_equal true
230
+ assert_success _(6 * 7).must_equal(42)
205
231
 
206
232
  assert_triggered "Expected: 42\n Actual: 54" do
207
- (6 * 9).must_equal 42
233
+ _(6 * 9).must_equal 42
208
234
  end
209
235
 
210
236
  assert_triggered "msg.\nExpected: 42\n Actual: 54" do
211
- (6 * 9).must_equal 42, "msg"
237
+ _(6 * 9).must_equal 42, "msg"
212
238
  end
213
239
 
214
- assert_triggered(/^-42\n\+#<Proc:0xXXXXXX@PATH>\n/) do
215
- proc { 42 }.must_equal 42 # proc isn't called, so expectation fails
240
+ assert_triggered(/^-42\n\+#<Proc:0xXXXXXX[ @]PATH>\n/) do
241
+ _(proc { 42 }).must_equal 42 # proc isn't called, so expectation fails
216
242
  end
217
243
  end
218
244
 
@@ -220,7 +246,7 @@ describe Minitest::Spec do
220
246
  @assertion_count += 1 # extra test
221
247
 
222
248
  out, err = capture_io do
223
- nil.must_equal(nil).must_equal true
249
+ assert_success _(nil).must_equal(nil)
224
250
  end
225
251
 
226
252
  exp = "DEPRECATED: Use assert_nil if expecting nil from #{__FILE__}:#{__LINE__-3}. " \
@@ -234,259 +260,259 @@ describe Minitest::Spec do
234
260
  it "needs to verify floats outside a delta" do
235
261
  @assertion_count += 1 # extra test
236
262
 
237
- 24.wont_be_close_to(42).must_equal false
263
+ assert_success _(24).wont_be_close_to(42)
238
264
 
239
265
  assert_triggered "Expected |42 - 42.0| (0.0) to not be <= 0.001." do
240
- (6 * 7.0).wont_be_close_to 42
266
+ _(6 * 7.0).wont_be_close_to 42
241
267
  end
242
268
 
243
- x = maglev? ? "1.0000000000000001e-05" : "1.0e-05"
269
+ x = "1.0e-05"
244
270
  assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
245
- (6 * 7.0).wont_be_close_to 42, 0.00001
271
+ _(6 * 7.0).wont_be_close_to 42, 0.00001
246
272
  end
247
273
 
248
274
  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"
275
+ _(6 * 7.0).wont_be_close_to 42, 0.00001, "msg"
250
276
  end
251
277
  end
252
278
 
253
279
  it "needs to verify floats outside an epsilon" do
254
280
  @assertion_count += 1 # extra test
255
281
 
256
- 24.wont_be_within_epsilon(42).must_equal false
282
+ assert_success _(24).wont_be_within_epsilon(42)
257
283
 
258
- x = maglev? ? "0.042000000000000003" : "0.042"
284
+ x = "0.042"
259
285
  assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
260
- (6 * 7.0).wont_be_within_epsilon 42
286
+ _(6 * 7.0).wont_be_within_epsilon 42
261
287
  end
262
288
 
263
- x = maglev? ? "0.00042000000000000002" : "0.00042"
289
+ x = "0.00042"
264
290
  assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
265
- (6 * 7.0).wont_be_within_epsilon 42, 0.00001
291
+ _(6 * 7.0).wont_be_within_epsilon 42, 0.00001
266
292
  end
267
293
 
268
294
  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"
295
+ _(6 * 7.0).wont_be_within_epsilon 42, 0.00001, "msg"
270
296
  end
271
297
  end
272
298
 
273
299
  it "needs to verify floats within a delta" do
274
300
  @assertion_count += 1 # extra test
275
301
 
276
- (6.0 * 7).must_be_close_to(42.0).must_equal true
302
+ assert_success _(6.0 * 7).must_be_close_to(42.0)
277
303
 
278
304
  assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.001." do
279
- (1.0 / 100).must_be_close_to 0.0
305
+ _(1.0 / 100).must_be_close_to 0.0
280
306
  end
281
307
 
282
- x = maglev? ? "9.9999999999999995e-07" : "1.0e-06"
308
+ x = "1.0e-06"
283
309
  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
310
+ _(1.0 / 1000).must_be_close_to 0.0, 0.000001
285
311
  end
286
312
 
287
313
  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"
314
+ _(1.0 / 1000).must_be_close_to 0.0, 0.000001, "msg"
289
315
  end
290
316
  end
291
317
 
292
318
  it "needs to verify floats within an epsilon" do
293
319
  @assertion_count += 1 # extra test
294
320
 
295
- (6.0 * 7).must_be_within_epsilon(42.0).must_equal true
321
+ assert_success _(6.0 * 7).must_be_within_epsilon(42.0)
296
322
 
297
323
  assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.0." do
298
- (1.0 / 100).must_be_within_epsilon 0.0
324
+ _(1.0 / 100).must_be_within_epsilon 0.0
299
325
  end
300
326
 
301
327
  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
328
+ _(1.0 / 1000).must_be_within_epsilon 0.0, 0.000001
303
329
  end
304
330
 
305
331
  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"
332
+ _(1.0 / 1000).must_be_within_epsilon 0.0, 0.000001, "msg"
307
333
  end
308
334
  end
309
335
 
310
336
  it "needs to verify identity" do
311
- 1.must_be_same_as(1).must_equal true
337
+ assert_success _(1).must_be_same_as(1)
312
338
 
313
339
  assert_triggered "Expected 1 (oid=N) to be the same as 2 (oid=N)." do
314
- 1.must_be_same_as 2
340
+ _(1).must_be_same_as 2
315
341
  end
316
342
 
317
343
  assert_triggered "msg.\nExpected 1 (oid=N) to be the same as 2 (oid=N)." do
318
- 1.must_be_same_as 2, "msg"
344
+ _(1).must_be_same_as 2, "msg"
319
345
  end
320
346
  end
321
347
 
322
348
  it "needs to verify inequality" do
323
349
  @assertion_count += 2
324
- 42.wont_equal(6 * 9).must_equal false
325
- proc {}.wont_equal(42).must_equal false
350
+ assert_success _(42).wont_equal(6 * 9)
351
+ assert_success _(proc {}).wont_equal(42)
326
352
 
327
353
  assert_triggered "Expected 1 to not be equal to 1." do
328
- 1.wont_equal 1
354
+ _(1).wont_equal 1
329
355
  end
330
356
 
331
357
  assert_triggered "msg.\nExpected 1 to not be equal to 1." do
332
- 1.wont_equal 1, "msg"
358
+ _(1).wont_equal 1, "msg"
333
359
  end
334
360
  end
335
361
 
336
362
  it "needs to verify instances of a class" do
337
- 42.wont_be_instance_of(String).must_equal false
363
+ assert_success _(42).wont_be_instance_of(String)
338
364
 
339
365
  assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
340
- 42.wont_be_kind_of Int
366
+ _(42).wont_be_kind_of Int
341
367
  end
342
368
 
343
369
  assert_triggered "msg.\nExpected 42 to not be an instance of #{Int.name}." do
344
- 42.wont_be_instance_of Int, "msg"
370
+ _(42).wont_be_instance_of Int, "msg"
345
371
  end
346
372
  end
347
373
 
348
374
  it "needs to verify kinds of a class" do
349
375
  @assertion_count += 2
350
376
 
351
- 42.wont_be_kind_of(String).must_equal false
352
- proc {}.wont_be_kind_of(String).must_equal false
377
+ assert_success _(42).wont_be_kind_of(String)
378
+ assert_success _(proc {}).wont_be_kind_of(String)
353
379
 
354
380
  assert_triggered "Expected 42 to not be a kind of #{Int.name}." do
355
- 42.wont_be_kind_of Int
381
+ _(42).wont_be_kind_of Int
356
382
  end
357
383
 
358
384
  assert_triggered "msg.\nExpected 42 to not be a kind of #{Int.name}." do
359
- 42.wont_be_kind_of Int, "msg"
385
+ _(42).wont_be_kind_of Int, "msg"
360
386
  end
361
387
  end
362
388
 
363
389
  it "needs to verify kinds of objects" do
364
390
  @assertion_count += 3 # extra test
365
391
 
366
- (6 * 7).must_be_kind_of(Int).must_equal true
367
- (6 * 7).must_be_kind_of(Numeric).must_equal true
392
+ assert_success _(6 * 7).must_be_kind_of(Int)
393
+ assert_success _(6 * 7).must_be_kind_of(Numeric)
368
394
 
369
395
  assert_triggered "Expected 42 to be a kind of String, not #{Int.name}." do
370
- (6 * 7).must_be_kind_of String
396
+ _(6 * 7).must_be_kind_of String
371
397
  end
372
398
 
373
399
  assert_triggered "msg.\nExpected 42 to be a kind of String, not #{Int.name}." do
374
- (6 * 7).must_be_kind_of String, "msg"
400
+ _(6 * 7).must_be_kind_of String, "msg"
375
401
  end
376
402
 
377
403
  exp = "Expected #<Proc:0xXXXXXX@PATH> to be a kind of String, not Proc."
378
404
  assert_triggered exp do
379
- proc {}.must_be_kind_of String
405
+ _(proc {}).must_be_kind_of String
380
406
  end
381
407
  end
382
408
 
383
409
  it "needs to verify mismatch" do
384
410
  @assertion_count += 3 # match is 2
385
411
 
386
- "blah".wont_match(/\d+/).must_equal false
412
+ assert_success _("blah").wont_match(/\d+/)
387
413
 
388
414
  assert_triggered "Expected /\\w+/ to not match \"blah\"." do
389
- "blah".wont_match(/\w+/)
415
+ _("blah").wont_match(/\w+/)
390
416
  end
391
417
 
392
418
  assert_triggered "msg.\nExpected /\\w+/ to not match \"blah\"." do
393
- "blah".wont_match(/\w+/, "msg")
419
+ _("blah").wont_match(/\w+/, "msg")
394
420
  end
395
421
  end
396
422
 
397
423
  it "needs to verify nil" do
398
- nil.must_be_nil.must_equal true
424
+ assert_success _(nil).must_be_nil
399
425
 
400
426
  assert_triggered "Expected 42 to be nil." do
401
- 42.must_be_nil
427
+ _(42).must_be_nil
402
428
  end
403
429
 
404
430
  assert_triggered "msg.\nExpected 42 to be nil." do
405
- 42.must_be_nil "msg"
431
+ _(42).must_be_nil "msg"
406
432
  end
407
433
  end
408
434
 
409
435
  it "needs to verify non-emptyness" do
410
436
  @assertion_count += 3 # empty is 2 assertions
411
437
 
412
- ["some item"].wont_be_empty.must_equal false
438
+ assert_success _(["some item"]).wont_be_empty
413
439
 
414
440
  assert_triggered "Expected [] to not be empty." do
415
- [].wont_be_empty
441
+ _([]).wont_be_empty
416
442
  end
417
443
 
418
444
  assert_triggered "msg.\nExpected [] to not be empty." do
419
- [].wont_be_empty "msg"
445
+ _([]).wont_be_empty "msg"
420
446
  end
421
447
  end
422
448
 
423
449
  it "needs to verify non-identity" do
424
- 1.wont_be_same_as(2).must_equal false
450
+ assert_success _(1).wont_be_same_as(2)
425
451
 
426
452
  assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
427
- 1.wont_be_same_as 1
453
+ _(1).wont_be_same_as 1
428
454
  end
429
455
 
430
456
  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"
457
+ _(1).wont_be_same_as 1, "msg"
432
458
  end
433
459
  end
434
460
 
435
461
  it "needs to verify non-nil" do
436
- 42.wont_be_nil.must_equal false
462
+ assert_success _(42).wont_be_nil
437
463
 
438
464
  assert_triggered "Expected nil to not be nil." do
439
- nil.wont_be_nil
465
+ _(nil).wont_be_nil
440
466
  end
441
467
 
442
468
  assert_triggered "msg.\nExpected nil to not be nil." do
443
- nil.wont_be_nil "msg"
469
+ _(nil).wont_be_nil "msg"
444
470
  end
445
471
  end
446
472
 
447
473
  it "needs to verify objects not responding to a message" do
448
- "".wont_respond_to(:woot!).must_equal false
474
+ assert_success _("").wont_respond_to(:woot!)
449
475
 
450
476
  assert_triggered "Expected \"\" to not respond to to_s." do
451
- "".wont_respond_to :to_s
477
+ _("").wont_respond_to :to_s
452
478
  end
453
479
 
454
480
  assert_triggered "msg.\nExpected \"\" to not respond to to_s." do
455
- "".wont_respond_to :to_s, "msg"
481
+ _("").wont_respond_to :to_s, "msg"
456
482
  end
457
483
  end
458
484
 
459
485
  it "needs to verify output in stderr" do
460
486
  @assertion_count -= 1 # no msg
461
487
 
462
- proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true
488
+ assert_success expect { $stderr.print "blah" }.must_output(nil, "blah")
463
489
 
464
490
  assert_triggered "In stderr.\nExpected: \"blah\"\n Actual: \"xxx\"" do
465
- proc { $stderr.print "xxx" }.must_output(nil, "blah")
491
+ expect { $stderr.print "xxx" }.must_output(nil, "blah")
466
492
  end
467
493
  end
468
494
 
469
495
  it "needs to verify output in stdout" do
470
496
  @assertion_count -= 1 # no msg
471
497
 
472
- proc { print "blah" }.must_output("blah").must_equal true
498
+ assert_success expect { print "blah" }.must_output("blah")
473
499
 
474
500
  assert_triggered "In stdout.\nExpected: \"blah\"\n Actual: \"xxx\"" do
475
- proc { print "xxx" }.must_output("blah")
501
+ expect { print "xxx" }.must_output("blah")
476
502
  end
477
503
  end
478
504
 
479
505
  it "needs to verify regexp matches" do
480
506
  @assertion_count += 3 # must_match is 2 assertions
481
507
 
482
- "blah".must_match(/\w+/).must_equal true
508
+ assert_kind_of MatchData, _("blah").must_match(/\w+/)
483
509
 
484
510
  assert_triggered "Expected /\\d+/ to match \"blah\"." do
485
- "blah".must_match(/\d+/)
511
+ _("blah").must_match(/\d+/)
486
512
  end
487
513
 
488
514
  assert_triggered "msg.\nExpected /\\d+/ to match \"blah\"." do
489
- "blah".must_match(/\d+/, "msg")
515
+ _("blah").must_match(/\d+/, "msg")
490
516
  end
491
517
  end
492
518
 
@@ -508,89 +534,137 @@ describe Minitest::Spec do
508
534
  end
509
535
 
510
536
  it "can NOT use must_equal in a thread. It must use expect in a thread" do
511
- assert_raises NoMethodError do
537
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
538
+ assert_raises RuntimeError do
512
539
  capture_io do
513
540
  Thread.new { (1 + 1).must_equal 2 }.join
514
541
  end
515
542
  end
516
543
  end
544
+
545
+ it "fails gracefully when expectation used outside of `it`" do
546
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
547
+
548
+ @assertion_count += 1
549
+
550
+ e = assert_raises RuntimeError do
551
+ capture_io do
552
+ Thread.new { # forces ctx to be nil
553
+ describe("woot") do
554
+ (1 + 1).must_equal 2
555
+ end
556
+ }.join
557
+ end
558
+ end
559
+
560
+ assert_equal "Calling #must_equal outside of test.", e.message
561
+ end
562
+
563
+ it "deprecates expectation used without _" do
564
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
565
+
566
+ @assertion_count += 3
567
+
568
+ exp = /DEPRECATED: global use of must_equal from/
569
+
570
+ assert_output "", exp do
571
+ (1 + 1).must_equal 2
572
+ end
573
+ end
574
+
575
+ # https://github.com/seattlerb/minitest/issues/837
576
+ # https://github.com/rails/rails/pull/39304
577
+ it "deprecates expectation used without _ with empty backtrace_filter" do
578
+ skip "N/A" if ENV["MT_NO_EXPECTATIONS"]
579
+
580
+ @assertion_count += 3
581
+
582
+ exp = /DEPRECATED: global use of must_equal from/
583
+
584
+ with_empty_backtrace_filter do
585
+ assert_output "", exp do
586
+ (1 + 1).must_equal 2
587
+ end
588
+ end
589
+ end
517
590
  end
518
591
 
519
592
  it "needs to verify throw" do
520
- @assertion_count += 2 # 2 extra tests
593
+ @assertion_count += 4 # 2 extra tests
521
594
 
522
- proc { throw :blah }.must_throw(:blah).must_equal true
595
+ assert_nil expect { throw :blah }.must_throw(:blah)
596
+ assert_equal 42, expect { throw :blah, 42 }.must_throw(:blah)
523
597
 
524
598
  assert_triggered "Expected :blah to have been thrown." do
525
- proc {}.must_throw :blah
599
+ expect {}.must_throw :blah
526
600
  end
527
601
 
528
602
  assert_triggered "Expected :blah to have been thrown, not :xxx." do
529
- proc { throw :xxx }.must_throw :blah
603
+ expect { throw :xxx }.must_throw :blah
530
604
  end
531
605
 
532
606
  assert_triggered "msg.\nExpected :blah to have been thrown." do
533
- proc {}.must_throw :blah, "msg"
607
+ expect {}.must_throw :blah, "msg"
534
608
  end
535
609
 
536
610
  assert_triggered "msg.\nExpected :blah to have been thrown, not :xxx." do
537
- proc { throw :xxx }.must_throw :blah, "msg"
611
+ expect { throw :xxx }.must_throw :blah, "msg"
538
612
  end
539
613
  end
540
614
 
541
615
  it "needs to verify types of objects" do
542
- (6 * 7).must_be_instance_of(Int).must_equal true
616
+ assert_success _(6 * 7).must_be_instance_of(Int)
543
617
 
544
618
  exp = "Expected 42 to be an instance of String, not #{Int.name}."
545
619
 
546
620
  assert_triggered exp do
547
- (6 * 7).must_be_instance_of String
621
+ _(6 * 7).must_be_instance_of String
548
622
  end
549
623
 
550
624
  assert_triggered "msg.\n#{exp}" do
551
- (6 * 7).must_be_instance_of String, "msg"
625
+ _(6 * 7).must_be_instance_of String, "msg"
552
626
  end
553
627
  end
554
628
 
555
629
  it "needs to verify using any (negative) predicate" do
556
630
  @assertion_count -= 1 # doesn"t take a message
557
631
 
558
- "blah".wont_be(:empty?).must_equal false
632
+ assert_success _("blah").wont_be(:empty?)
559
633
 
560
634
  assert_triggered "Expected \"\" to not be empty?." do
561
- "".wont_be :empty?
635
+ _("").wont_be :empty?
562
636
  end
563
637
  end
564
638
 
565
639
  it "needs to verify using any binary operator" do
566
640
  @assertion_count -= 1 # no msg
567
641
 
568
- 41.must_be(:<, 42).must_equal true
642
+ assert_success _(41).must_be(:<, 42)
569
643
 
570
644
  assert_triggered "Expected 42 to be < 41." do
571
- 42.must_be(:<, 41)
645
+ _(42).must_be(:<, 41)
572
646
  end
573
647
  end
574
648
 
575
649
  it "needs to verify using any predicate" do
576
650
  @assertion_count -= 1 # no msg
577
651
 
578
- "".must_be(:empty?).must_equal true
652
+ assert_success _("").must_be(:empty?)
579
653
 
580
654
  assert_triggered "Expected \"blah\" to be empty?." do
581
- "blah".must_be :empty?
655
+ _("blah").must_be :empty?
582
656
  end
583
657
  end
584
658
 
585
659
  it "needs to verify using respond_to" do
586
- 42.must_respond_to(:+).must_equal true
660
+ assert_success _(42).must_respond_to(:+)
587
661
 
588
662
  assert_triggered "Expected 42 (#{Int.name}) to respond to #clear." do
589
- 42.must_respond_to :clear
663
+ _(42).must_respond_to :clear
590
664
  end
591
665
 
592
666
  assert_triggered "msg.\nExpected 42 (#{Int.name}) to respond to #clear." do
593
- 42.must_respond_to :clear, "msg"
667
+ _(42).must_respond_to :clear, "msg"
594
668
  end
595
669
  end
596
670
  end
@@ -608,33 +682,33 @@ describe Minitest::Spec, :let do
608
682
  end
609
683
 
610
684
  it "is evaluated once per example" do
611
- _count.must_equal 0
685
+ _(_count).must_equal 0
612
686
 
613
- count.must_equal 1
614
- count.must_equal 1
687
+ _(count).must_equal 1
688
+ _(count).must_equal 1
615
689
 
616
- _count.must_equal 1
690
+ _(_count).must_equal 1
617
691
  end
618
692
 
619
693
  it "is REALLY evaluated once per example" do
620
- _count.must_equal 1
694
+ _(_count).must_equal 1
621
695
 
622
- count.must_equal 2
623
- count.must_equal 2
696
+ _(count).must_equal 2
697
+ _(count).must_equal 2
624
698
 
625
- _count.must_equal 2
699
+ _(_count).must_equal 2
626
700
  end
627
701
 
628
702
  it 'raises an error if the name begins with "test"' do
629
- proc { self.class.let(:test_value) { true } }.must_raise ArgumentError
703
+ expect { self.class.let(:test_value) { true } }.must_raise ArgumentError
630
704
  end
631
705
 
632
706
  it "raises an error if the name shadows a normal instance method" do
633
- proc { self.class.let(:message) { true } }.must_raise ArgumentError
707
+ expect { self.class.let(:message) { true } }.must_raise ArgumentError
634
708
  end
635
709
 
636
710
  it "doesn't raise an error if it is just another let" do
637
- proc do
711
+ v = proc do
638
712
  describe :outer do
639
713
  let(:bar)
640
714
  describe :inner do
@@ -642,13 +716,14 @@ describe Minitest::Spec, :let do
642
716
  end
643
717
  end
644
718
  :good
645
- end.call.must_equal :good
719
+ end.call
720
+ _(v).must_equal :good
646
721
  end
647
722
 
648
723
  it "procs come after dont_flip" do
649
724
  p = proc {}
650
725
  assert_respond_to p, :call
651
- p.must_respond_to :call
726
+ _(p).must_respond_to :call
652
727
  end
653
728
  end
654
729
 
@@ -662,13 +737,17 @@ describe Minitest::Spec, :subject do
662
737
  end
663
738
 
664
739
  it "is evaluated once per example" do
665
- subject.must_equal 1
666
- subject.must_equal 1
667
- subject_evaluation_count.must_equal 1
740
+ _(subject).must_equal 1
741
+ _(subject).must_equal 1
742
+ _(subject_evaluation_count).must_equal 1
668
743
  end
669
744
  end
670
745
 
671
746
  class TestMetaStatic < Minitest::Test
747
+ def assert_method_count expected, klass
748
+ assert_equal expected, klass.public_instance_methods.grep(/^test_/).count
749
+ end
750
+
672
751
  def test_children
673
752
  Minitest::Spec.children.clear # prevents parallel run
674
753
 
@@ -702,8 +781,8 @@ class TestMetaStatic < Minitest::Test
702
781
  end
703
782
  end
704
783
 
705
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
706
- assert_equal 1, inner.public_instance_methods.grep(/^test_/).count
784
+ assert_method_count 1, outer
785
+ assert_method_count 1, inner
707
786
  end
708
787
 
709
788
  def test_it_wont_add_test_methods_to_children
@@ -717,15 +796,17 @@ class TestMetaStatic < Minitest::Test
717
796
  end
718
797
  end
719
798
 
720
- assert_equal 1, outer.public_instance_methods.grep(/^test_/).count
721
- assert_equal 0, inner.public_instance_methods.grep(/^test_/).count
799
+ assert_method_count 1, outer
800
+ assert_method_count 0, inner
722
801
  end
723
802
  end
724
803
 
725
- require "minitest/metametameta"
726
-
727
804
  class TestMeta < MetaMetaMetaTestCase
728
- parallelize_me!
805
+ # do not call parallelize_me! here because specs use register_spec_type globally
806
+
807
+ def assert_defined_methods expected, klass
808
+ assert_equal expected, klass.instance_methods(false).sort.map(&:to_s)
809
+ end
729
810
 
730
811
  def util_structure
731
812
  y = z = nil
@@ -795,11 +876,11 @@ class TestMeta < MetaMetaMetaTestCase
795
876
  def test_bug_dsl_expectations
796
877
  spec_class = Class.new MiniSpecB do
797
878
  it "should work" do
798
- 0.must_equal 0
879
+ _(0).must_equal 0
799
880
  end
800
881
  end
801
882
 
802
- test_name = spec_class.instance_methods.sort.grep(/test/).first
883
+ test_name = spec_class.instance_methods.sort.grep(/test_/).first
803
884
 
804
885
  spec = spec_class.new test_name
805
886
 
@@ -848,9 +929,9 @@ class TestMeta < MetaMetaMetaTestCase
848
929
  inner_methods2 = inner_methods1 +
849
930
  %w[test_0002_anonymous test_0003_anonymous]
850
931
 
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)
932
+ assert_defined_methods top_methods, x
933
+ assert_defined_methods inner_methods1, y
934
+ assert_defined_methods inner_methods2, z
854
935
  end
855
936
 
856
937
  def test_structure_postfix_it
@@ -867,8 +948,8 @@ class TestMeta < MetaMetaMetaTestCase
867
948
  it "inner-it" do end
868
949
  end
869
950
 
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)
951
+ assert_defined_methods %w[test_0001_inner-it], y
952
+ assert_defined_methods %w[test_0001_inner-it], z
872
953
  end
873
954
 
874
955
  def test_setup_teardown_behavior
@@ -899,9 +980,9 @@ class TestMeta < MetaMetaMetaTestCase
899
980
  ].sort
900
981
 
901
982
  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/)
983
+ assert_defined_methods test_methods, x
984
+ assert_defined_methods [], y
985
+ assert_defined_methods [], z
905
986
  end
906
987
 
907
988
  def test_structure_subclasses
@@ -947,18 +1028,20 @@ class TestSpecInTestCase < MetaMetaMetaTestCase
947
1028
  end
948
1029
 
949
1030
  def test_expectation
950
- @tc.assert_equal true, 1.must_equal(1)
1031
+ @tc.assert_equal true, _(1).must_equal(1)
951
1032
  end
952
1033
 
953
1034
  def test_expectation_triggered
954
1035
  assert_triggered "Expected: 2\n Actual: 1" do
955
- 1.must_equal 2
1036
+ _(1).must_equal 2
956
1037
  end
957
1038
  end
958
1039
 
1040
+ include Minitest::Spec::DSL::InstanceMethods
1041
+
959
1042
  def test_expectation_with_a_message
960
1043
  assert_triggered "woot.\nExpected: 2\n Actual: 1" do
961
- 1.must_equal 2, "woot"
1044
+ _(1).must_equal 2, "woot"
962
1045
  end
963
1046
  end
964
1047
  end
@@ -985,3 +1068,38 @@ class ValueMonadTest < Minitest::Test
985
1068
  assert_equal "c", struct.expect
986
1069
  end
987
1070
  end
1071
+
1072
+ describe Minitest::Spec, :infect_an_assertion do
1073
+ class << self
1074
+ attr_accessor :infect_mock
1075
+ end
1076
+
1077
+ def assert_infects exp, act, msg = nil, foo: nil, bar: nil
1078
+ self.class.infect_mock.assert_infects exp, act, msg, foo: foo, bar: bar
1079
+ end
1080
+
1081
+ infect_an_assertion :assert_infects, :must_infect
1082
+ infect_an_assertion :assert_infects, :must_infect_without_flipping, :dont_flip
1083
+
1084
+ it "infects assertions with kwargs" do
1085
+ mock = Minitest::Mock.new
1086
+ mock.expect :assert_infects, true, [:exp, :act, nil], foo: :foo, bar: :bar
1087
+
1088
+ self.class.infect_mock = mock
1089
+
1090
+ _(:act).must_infect :exp, foo: :foo, bar: :bar
1091
+
1092
+ assert_mock mock
1093
+ end
1094
+
1095
+ it "infects assertions with kwargs (dont_flip)" do
1096
+ mock = Minitest::Mock.new
1097
+ mock.expect :assert_infects, true, [:act, :exp, nil], foo: :foo, bar: :bar
1098
+
1099
+ self.class.infect_mock = mock
1100
+
1101
+ _(:act).must_infect_without_flipping :exp, foo: :foo, bar: :bar
1102
+
1103
+ assert_mock mock
1104
+ end
1105
+ end