minitest 5.11.3 → 5.25.4

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