spec 5.0.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.autotest +34 -0
  3. data/.gitignore +3 -0
  4. data/History.txt +911 -0
  5. data/Manifest.txt +26 -0
  6. data/README.txt +497 -0
  7. data/Rakefile +214 -0
  8. data/design_rationale.rb +52 -0
  9. data/lib/hoe/minitest.rb +26 -0
  10. data/lib/minitest/assertions.rb +649 -0
  11. data/lib/minitest/autorun.rb +12 -0
  12. data/lib/minitest/benchmark.rb +423 -0
  13. data/lib/minitest/expectations.rb +268 -0
  14. data/lib/minitest/hell.rb +11 -0
  15. data/lib/minitest/mock.rb +220 -0
  16. data/lib/minitest/parallel_each.rb +120 -0
  17. data/lib/minitest/pride.rb +4 -0
  18. data/lib/minitest/pride_plugin.rb +143 -0
  19. data/lib/minitest/spec.rb +292 -0
  20. data/lib/minitest/test.rb +272 -0
  21. data/lib/minitest/unit.rb +45 -0
  22. data/lib/minitest.rb +839 -0
  23. data/lib/spec.rb +3 -0
  24. data/readme.md +7 -0
  25. data/release_notes.md +49 -0
  26. data/spec.gemspec +36 -0
  27. data/test/manual/appium.rb +14 -0
  28. data/test/manual/appium_after_last.rb +24 -0
  29. data/test/manual/appium_before_first.rb +23 -0
  30. data/test/manual/assert.rb +61 -0
  31. data/test/manual/before_first_0.rb +27 -0
  32. data/test/manual/before_first_1.rb +29 -0
  33. data/test/manual/debug.rb +37 -0
  34. data/test/manual/do_end.rb +31 -0
  35. data/test/manual/raise.rb +61 -0
  36. data/test/manual/run2.rb +74 -0
  37. data/test/manual/run3.rb +91 -0
  38. data/test/manual/setup.rb +13 -0
  39. data/test/manual/simple.rb +19 -0
  40. data/test/manual/simple2.rb +20 -0
  41. data/test/manual/t.rb +11 -0
  42. data/test/manual/trace.rb +19 -0
  43. data/test/manual/trace2.rb +15 -0
  44. data/test/minitest/metametameta.rb +78 -0
  45. data/test/minitest/test_helper.rb +20 -0
  46. data/test/minitest/test_minitest_benchmark.rb +131 -0
  47. data/test/minitest/test_minitest_mock.rb +490 -0
  48. data/test/minitest/test_minitest_reporter.rb +270 -0
  49. data/test/minitest/test_minitest_spec.rb +794 -0
  50. data/test/minitest/test_minitest_unit.rb +1846 -0
  51. metadata +147 -0
@@ -0,0 +1,794 @@
1
+ # encoding: utf-8
2
+ require "minitest/autorun"
3
+ require "stringio"
4
+ require 'minitest/test_helper'
5
+
6
+ class MiniSpecA < Minitest::Spec; end
7
+ class MiniSpecB < Minitest::Test; extend Minitest::Spec::DSL; end
8
+ class MiniSpecC < MiniSpecB; end
9
+ class NamedExampleA < MiniSpecA; end
10
+ class NamedExampleB < MiniSpecB; end
11
+ class NamedExampleC < MiniSpecC; end
12
+ class ExampleA; end
13
+ class ExampleB < ExampleA; end
14
+
15
+ describe Minitest::Spec do
16
+ # do not parallelize this suite... it just can"t handle it.
17
+
18
+ def assert_triggered expected = "blah", klass = Minitest::Assertion
19
+ @assertion_count += 2
20
+
21
+ e = assert_raises(klass) do
22
+ yield
23
+ end
24
+
25
+ msg = e.message.sub(/(---Backtrace---).*/m, '\1')
26
+ msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
27
+
28
+ assert_equal expected, msg
29
+ end
30
+
31
+ before do
32
+ @assertion_count = 4
33
+ end
34
+
35
+ after do
36
+ self.assertions.must_equal @assertion_count if passed? and not skipped?
37
+ end
38
+
39
+ it "needs to be able to catch a Minitest::Assertion exception" do
40
+ @assertion_count = 1
41
+
42
+ assert_triggered "Expected 1 to not be equal to 1." do
43
+ 1.wont_equal 1
44
+ end
45
+ end
46
+
47
+ it "needs to be sensible about must_include order" do
48
+ @assertion_count += 3 # must_include is 2 assertions
49
+
50
+ [1, 2, 3].must_include(2).must_equal true
51
+
52
+ assert_triggered "Expected [1, 2, 3] to include 5." do
53
+ [1, 2, 3].must_include 5
54
+ end
55
+
56
+ assert_triggered "msg.\nExpected [1, 2, 3] to include 5." do
57
+ [1, 2, 3].must_include 5, "msg"
58
+ end
59
+ end
60
+
61
+ it "needs to be sensible about wont_include order" do
62
+ @assertion_count += 3 # wont_include is 2 assertions
63
+
64
+ [1, 2, 3].wont_include(5).must_equal false
65
+
66
+ assert_triggered "Expected [1, 2, 3] to not include 2." do
67
+ [1, 2, 3].wont_include 2
68
+ end
69
+
70
+ assert_triggered "msg.\nExpected [1, 2, 3] to not include 2." do
71
+ [1, 2, 3].wont_include 2, "msg"
72
+ end
73
+ end
74
+
75
+ it "needs to catch an expected exception" do
76
+ @assertion_count = 2
77
+
78
+ proc { raise "blah" }.must_raise RuntimeError
79
+ proc { raise Minitest::Assertion }.must_raise Minitest::Assertion
80
+ end
81
+
82
+ it "needs to catch an unexpected exception" do
83
+ @assertion_count -= 2 # no positive
84
+
85
+ msg = <<-EOM.gsub(/^ {6}/, "").chomp
86
+ [RuntimeError] exception expected, not
87
+ Class: <Minitest::Assertion>
88
+ Message: <"Minitest::Assertion">
89
+ ---Backtrace---
90
+ EOM
91
+
92
+ assert_triggered msg do
93
+ proc { raise Minitest::Assertion }.must_raise RuntimeError
94
+ end
95
+
96
+ assert_triggered "msg.\n#{msg}" do
97
+ proc { raise Minitest::Assertion }.must_raise RuntimeError, "msg"
98
+ end
99
+ end
100
+
101
+ it "needs to ensure silence" do
102
+ @assertion_count -= 1 # no msg
103
+ @assertion_count += 2 # assert_output is 2 assertions
104
+
105
+ proc { }.must_be_silent.must_equal true
106
+
107
+ assert_triggered "In stdout.\nExpected: \"\"\n Actual: \"xxx\"" do
108
+ proc { print "xxx" }.must_be_silent
109
+ end
110
+ end
111
+
112
+ it "needs to have all methods named well" do
113
+ @assertion_count = 2
114
+
115
+ methods = Object.public_instance_methods.find_all { |n| n =~ /^must|^wont/ }
116
+ methods.map! { |m| m.to_s } if Symbol === methods.first
117
+
118
+ musts, wonts = methods.sort.partition { |m| m =~ /^must/ }
119
+
120
+ expected_musts = %w(must_be
121
+ must_be_close_to
122
+ must_be_empty
123
+ must_be_instance_of
124
+ must_be_kind_of
125
+ must_be_nil
126
+ must_be_same_as
127
+ must_be_silent
128
+ must_be_within_delta
129
+ must_be_within_epsilon
130
+ must_equal
131
+ must_include
132
+ must_match
133
+ must_output
134
+ must_raise
135
+ must_respond_to
136
+ must_throw)
137
+
138
+ bad = %w[not raise throw send output be_silent]
139
+
140
+ expected_wonts = expected_musts.map { |m| m.sub(/^must/, "wont") }
141
+ expected_wonts.reject! { |m| m =~ /wont_#{Regexp.union(*bad)}/ }
142
+
143
+ musts.must_equal expected_musts
144
+ wonts.must_equal expected_wonts
145
+ end
146
+
147
+ it "needs to raise if an expected exception is not raised" do
148
+ @assertion_count -= 2 # no positive test
149
+
150
+ assert_triggered "RuntimeError expected but nothing was raised." do
151
+ proc { 42 }.must_raise RuntimeError
152
+ end
153
+
154
+ assert_triggered "msg.\nRuntimeError expected but nothing was raised." do
155
+ proc { 42 }.must_raise RuntimeError, "msg"
156
+ end
157
+ end
158
+
159
+ it "needs to verify binary messages" do
160
+ 42.wont_be(:<, 24).must_equal false
161
+
162
+ assert_triggered "Expected 24 to not be < 42." do
163
+ 24.wont_be :<, 42
164
+ end
165
+
166
+ assert_triggered "msg.\nExpected 24 to not be < 42." do
167
+ 24.wont_be :<, 42, "msg"
168
+ end
169
+ end
170
+
171
+ it "needs to verify emptyness" do
172
+ @assertion_count += 3 # empty is 2 assertions
173
+
174
+ [].must_be_empty.must_equal true
175
+
176
+ assert_triggered "Expected [42] to be empty." do
177
+ [42].must_be_empty
178
+ end
179
+
180
+ assert_triggered "msg.\nExpected [42] to be empty." do
181
+ [42].must_be_empty "msg"
182
+ end
183
+ end
184
+
185
+ it "needs to verify equality" do
186
+ (6 * 7).must_equal(42).must_equal true
187
+
188
+ assert_triggered "Expected: 42\n Actual: 54" do
189
+ (6 * 9).must_equal 42
190
+ end
191
+
192
+ assert_triggered "msg.\nExpected: 42\n Actual: 54" do
193
+ (6 * 9).must_equal 42, "msg"
194
+ end
195
+ end
196
+
197
+ it "needs to verify floats outside a delta" do
198
+ @assertion_count += 1 # extra test
199
+
200
+ 24.wont_be_close_to(42).must_equal false
201
+
202
+ assert_triggered "Expected |42 - 42.0| (0.0) to not be <= 0.001." do
203
+ (6 * 7.0).wont_be_close_to 42
204
+ end
205
+
206
+ x = maglev? ? "1.0000000000000001e-05" : "1.0e-05"
207
+ assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
208
+ (6 * 7.0).wont_be_close_to 42, 0.00001
209
+ end
210
+
211
+ assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be <= #{x}." do
212
+ (6 * 7.0).wont_be_close_to 42, 0.00001, "msg"
213
+ end
214
+ end
215
+
216
+ it "needs to verify floats outside an epsilon" do
217
+ @assertion_count += 1 # extra test
218
+
219
+ 24.wont_be_within_epsilon(42).must_equal false
220
+
221
+ x = maglev? ? "0.042000000000000003" : "0.042"
222
+ assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
223
+ (6 * 7.0).wont_be_within_epsilon 42
224
+ end
225
+
226
+ x = maglev? ? "0.00042000000000000002" : "0.00042"
227
+ assert_triggered "Expected |42 - 42.0| (0.0) to not be <= #{x}." do
228
+ (6 * 7.0).wont_be_within_epsilon 42, 0.00001
229
+ end
230
+
231
+ assert_triggered "msg.\nExpected |42 - 42.0| (0.0) to not be <= #{x}." do
232
+ (6 * 7.0).wont_be_within_epsilon 42, 0.00001, "msg"
233
+ end
234
+ end
235
+
236
+ it "needs to verify floats within a delta" do
237
+ @assertion_count += 1 # extra test
238
+
239
+ (6.0 * 7).must_be_close_to(42.0).must_equal true
240
+
241
+ assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.001." do
242
+ (1.0 / 100).must_be_close_to 0.0
243
+ end
244
+
245
+ x = maglev? ? "9.9999999999999995e-07" : "1.0e-06"
246
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= #{x}." do
247
+ (1.0 / 1000).must_be_close_to 0.0, 0.000001
248
+ end
249
+
250
+ assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be <= #{x}." do
251
+ (1.0 / 1000).must_be_close_to 0.0, 0.000001, "msg"
252
+ end
253
+ end
254
+
255
+ it "needs to verify floats within an epsilon" do
256
+ @assertion_count += 1 # extra test
257
+
258
+ (6.0 * 7).must_be_within_epsilon(42.0).must_equal true
259
+
260
+ assert_triggered "Expected |0.0 - 0.01| (0.01) to be <= 0.0." do
261
+ (1.0 / 100).must_be_within_epsilon 0.0
262
+ end
263
+
264
+ assert_triggered "Expected |0.0 - 0.001| (0.001) to be <= 0.0." do
265
+ (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001
266
+ end
267
+
268
+ assert_triggered "msg.\nExpected |0.0 - 0.001| (0.001) to be <= 0.0." do
269
+ (1.0 / 1000).must_be_within_epsilon 0.0, 0.000001, "msg"
270
+ end
271
+ end
272
+
273
+ it "needs to verify identity" do
274
+ 1.must_be_same_as(1).must_equal true
275
+
276
+ assert_triggered "Expected 1 (oid=N) to be the same as 2 (oid=N)." do
277
+ 1.must_be_same_as 2
278
+ end
279
+
280
+ assert_triggered "msg.\nExpected 1 (oid=N) to be the same as 2 (oid=N)." do
281
+ 1.must_be_same_as 2, "msg"
282
+ end
283
+ end
284
+
285
+ it "needs to verify inequality" do
286
+ 42.wont_equal(6 * 9).must_equal false
287
+
288
+ assert_triggered "Expected 1 to not be equal to 1." do
289
+ 1.wont_equal 1
290
+ end
291
+
292
+ assert_triggered "msg.\nExpected 1 to not be equal to 1." do
293
+ 1.wont_equal 1, "msg"
294
+ end
295
+ end
296
+
297
+ it "needs to verify instances of a class" do
298
+ 42.wont_be_instance_of(String).must_equal false
299
+
300
+ assert_triggered "Expected 42 to not be an instance of Fixnum." do
301
+ 42.wont_be_instance_of Fixnum
302
+ end
303
+
304
+ assert_triggered "msg.\nExpected 42 to not be an instance of Fixnum." do
305
+ 42.wont_be_instance_of Fixnum, "msg"
306
+ end
307
+ end
308
+
309
+ it "needs to verify kinds of a class" do
310
+ 42.wont_be_kind_of(String).must_equal false
311
+
312
+ assert_triggered "Expected 42 to not be a kind of Integer." do
313
+ 42.wont_be_kind_of Integer
314
+ end
315
+
316
+ assert_triggered "msg.\nExpected 42 to not be a kind of Integer." do
317
+ 42.wont_be_kind_of Integer, "msg"
318
+ end
319
+ end
320
+
321
+ it "needs to verify kinds of objects" do
322
+ @assertion_count += 2 # extra test
323
+
324
+ (6 * 7).must_be_kind_of(Fixnum).must_equal true
325
+ (6 * 7).must_be_kind_of(Numeric).must_equal true
326
+
327
+ assert_triggered "Expected 42 to be a kind of String, not Fixnum." do
328
+ (6 * 7).must_be_kind_of String
329
+ end
330
+
331
+ assert_triggered "msg.\nExpected 42 to be a kind of String, not Fixnum." do
332
+ (6 * 7).must_be_kind_of String, "msg"
333
+ end
334
+ end
335
+
336
+ it "needs to verify mismatch" do
337
+ @assertion_count += 3 # match is 2
338
+
339
+ "blah".wont_match(/\d+/).must_equal false
340
+
341
+ assert_triggered "Expected /\\w+/ to not match \"blah\"." do
342
+ "blah".wont_match(/\w+/)
343
+ end
344
+
345
+ assert_triggered "msg.\nExpected /\\w+/ to not match \"blah\"." do
346
+ "blah".wont_match(/\w+/, "msg")
347
+ end
348
+ end
349
+
350
+ it "needs to verify nil" do
351
+ nil.must_be_nil.must_equal true
352
+
353
+ assert_triggered "Expected 42 to be nil." do
354
+ 42.must_be_nil
355
+ end
356
+
357
+ assert_triggered "msg.\nExpected 42 to be nil." do
358
+ 42.must_be_nil "msg"
359
+ end
360
+ end
361
+
362
+ it "needs to verify non-emptyness" do
363
+ @assertion_count += 3 # empty is 2 assertions
364
+
365
+ ["some item"].wont_be_empty.must_equal false
366
+
367
+ assert_triggered "Expected [] to not be empty." do
368
+ [].wont_be_empty
369
+ end
370
+
371
+ assert_triggered "msg.\nExpected [] to not be empty." do
372
+ [].wont_be_empty "msg"
373
+ end
374
+ end
375
+
376
+ it "needs to verify non-identity" do
377
+ 1.wont_be_same_as(2).must_equal false
378
+
379
+ assert_triggered "Expected 1 (oid=N) to not be the same as 1 (oid=N)." do
380
+ 1.wont_be_same_as 1
381
+ end
382
+
383
+ assert_triggered "msg.\nExpected 1 (oid=N) to not be the same as 1 (oid=N)." do
384
+ 1.wont_be_same_as 1, "msg"
385
+ end
386
+ end
387
+
388
+ it "needs to verify non-nil" do
389
+ 42.wont_be_nil.must_equal false
390
+
391
+ assert_triggered "Expected nil to not be nil." do
392
+ nil.wont_be_nil
393
+ end
394
+
395
+ assert_triggered "msg.\nExpected nil to not be nil." do
396
+ nil.wont_be_nil "msg"
397
+ end
398
+ end
399
+
400
+ it "needs to verify objects not responding to a message" do
401
+ "".wont_respond_to(:woot!).must_equal false
402
+
403
+ assert_triggered "Expected \"\" to not respond to to_s." do
404
+ "".wont_respond_to :to_s
405
+ end
406
+
407
+ assert_triggered "msg.\nExpected \"\" to not respond to to_s." do
408
+ "".wont_respond_to :to_s, "msg"
409
+ end
410
+ end
411
+
412
+ it "needs to verify output in stderr" do
413
+ @assertion_count -= 1 # no msg
414
+
415
+ proc { $stderr.print "blah" }.must_output(nil, "blah").must_equal true
416
+
417
+ assert_triggered "In stderr.\nExpected: \"blah\"\n Actual: \"xxx\"" do
418
+ proc { $stderr.print "xxx" }.must_output(nil, "blah")
419
+ end
420
+ end
421
+
422
+ it "needs to verify output in stdout" do
423
+ @assertion_count -= 1 # no msg
424
+
425
+ proc { print "blah" }.must_output("blah").must_equal true
426
+
427
+ assert_triggered "In stdout.\nExpected: \"blah\"\n Actual: \"xxx\"" do
428
+ proc { print "xxx" }.must_output("blah")
429
+ end
430
+ end
431
+
432
+ it "needs to verify regexp matches" do
433
+ @assertion_count += 3 # must_match is 2 assertions
434
+
435
+ "blah".must_match(/\w+/).must_equal true
436
+
437
+ assert_triggered "Expected /\\d+/ to match \"blah\"." do
438
+ "blah".must_match(/\d+/)
439
+ end
440
+
441
+ assert_triggered "msg.\nExpected /\\d+/ to match \"blah\"." do
442
+ "blah".must_match(/\d+/, "msg")
443
+ end
444
+ end
445
+
446
+ it "needs to verify throw" do
447
+ @assertion_count += 2 # 2 extra tests
448
+
449
+ proc { throw :blah }.must_throw(:blah).must_equal true
450
+
451
+ assert_triggered "Expected :blah to have been thrown." do
452
+ proc { }.must_throw :blah
453
+ end
454
+
455
+ assert_triggered "Expected :blah to have been thrown, not :xxx." do
456
+ proc { throw :xxx }.must_throw :blah
457
+ end
458
+
459
+ assert_triggered "msg.\nExpected :blah to have been thrown." do
460
+ proc { }.must_throw :blah, "msg"
461
+ end
462
+
463
+ assert_triggered "msg.\nExpected :blah to have been thrown, not :xxx." do
464
+ proc { throw :xxx }.must_throw :blah, "msg"
465
+ end
466
+ end
467
+
468
+ it "needs to verify types of objects" do
469
+ (6 * 7).must_be_instance_of(Fixnum).must_equal true
470
+
471
+ exp = "Expected 42 to be an instance of String, not Fixnum."
472
+
473
+ assert_triggered exp do
474
+ (6 * 7).must_be_instance_of String
475
+ end
476
+
477
+ assert_triggered "msg.\n#{exp}" do
478
+ (6 * 7).must_be_instance_of String, "msg"
479
+ end
480
+ end
481
+
482
+ it "needs to verify using any (negative) predicate" do
483
+ @assertion_count -= 1 # doesn"t take a message
484
+
485
+ "blah".wont_be(:empty?).must_equal false
486
+
487
+ assert_triggered "Expected \"\" to not be empty?." do
488
+ "".wont_be :empty?
489
+ end
490
+ end
491
+
492
+ it "needs to verify using any binary operator" do
493
+ @assertion_count -= 1 # no msg
494
+
495
+ 41.must_be(:<, 42).must_equal true
496
+
497
+ assert_triggered "Expected 42 to be < 41." do
498
+ 42.must_be(:<, 41)
499
+ end
500
+ end
501
+
502
+ it "needs to verify using any predicate" do
503
+ @assertion_count -= 1 # no msg
504
+
505
+ "".must_be(:empty?).must_equal true
506
+
507
+ assert_triggered "Expected \"blah\" to be empty?." do
508
+ "blah".must_be :empty?
509
+ end
510
+ end
511
+
512
+ it "needs to verify using respond_to" do
513
+ 42.must_respond_to(:+).must_equal true
514
+
515
+ assert_triggered "Expected 42 (Fixnum) to respond to #clear." do
516
+ 42.must_respond_to :clear
517
+ end
518
+
519
+ assert_triggered "msg.\nExpected 42 (Fixnum) to respond to #clear." do
520
+ 42.must_respond_to :clear, "msg"
521
+ end
522
+ end
523
+
524
+ end
525
+
526
+ describe Minitest::Spec, :let do
527
+ i_suck_and_my_tests_are_order_dependent!
528
+
529
+ def _count
530
+ $let_count ||= 0
531
+ end
532
+
533
+ let :count do
534
+ $let_count += 1
535
+ $let_count
536
+ end
537
+
538
+ it "is evaluated once per example" do
539
+ _count.must_equal 0
540
+
541
+ count.must_equal 1
542
+ count.must_equal 1
543
+
544
+ _count.must_equal 1
545
+ end
546
+
547
+ it "is REALLY evaluated once per example" do
548
+ _count.must_equal 1
549
+
550
+ count.must_equal 2
551
+ count.must_equal 2
552
+
553
+ _count.must_equal 2
554
+ end
555
+
556
+ it 'raises an error if the name begins with "test"' do
557
+ describe "let" do
558
+ proc { let(:test_value) { true } }.must_raise ArgumentError
559
+ end
560
+ end
561
+ end
562
+
563
+ describe Minitest::Spec, :subject do
564
+ attr_reader :subject_evaluation_count
565
+
566
+ subject do
567
+ @subject_evaluation_count ||= 0
568
+ @subject_evaluation_count += 1
569
+ @subject_evaluation_count
570
+ end
571
+
572
+ it "is evaluated once per example" do
573
+ subject.must_equal 1
574
+ subject.must_equal 1
575
+ subject_evaluation_count.must_equal 1
576
+ end
577
+ end
578
+
579
+ class TestMetaStatic < Minitest::Test
580
+ def test_children
581
+ Minitest::Spec.children.clear # prevents parallel run
582
+
583
+ x = y = z = nil
584
+ x = describe "top-level thingy" do
585
+ y = describe "first thingy" do end
586
+
587
+ it "top-level-it" do end
588
+
589
+ z = describe "second thingy" do end
590
+ end
591
+
592
+ assert_equal [x], Minitest::Spec.children
593
+ assert_equal [y, z], x.children
594
+ assert_equal [], y.children
595
+ assert_equal [], z.children
596
+ end
597
+ end
598
+
599
+ require "minitest/metametameta"
600
+
601
+ class TestMeta < MetaMetaMetaTestCase
602
+ parallelize_me!
603
+
604
+ def util_structure
605
+ x = y = z = nil
606
+ before_list = []
607
+ after_list = []
608
+ x = describe "top-level thingy" do
609
+ before { before_list << 1 }
610
+ after { after_list << 1 }
611
+
612
+ it "top-level-it" do end
613
+
614
+ y = describe "inner thingy" do
615
+ before { before_list << 2 }
616
+ after { after_list << 2 }
617
+ it "inner-it" do end
618
+
619
+ z = describe "very inner thingy" do
620
+ before { before_list << 3 }
621
+ after { after_list << 3 }
622
+ it "inner-it" do end
623
+
624
+ it {} # ignore me
625
+ specify {} # anonymous it
626
+ end
627
+ end
628
+ end
629
+
630
+ return x, y, z, before_list, after_list
631
+ end
632
+
633
+ def test_register_spec_type
634
+ original_types = Minitest::Spec::TYPES.dup
635
+
636
+ assert_includes Minitest::Spec::TYPES, [//, Minitest::Spec]
637
+
638
+ Minitest::Spec.register_spec_type(/woot/, TestMeta)
639
+
640
+ p = lambda do |x| true end
641
+ Minitest::Spec.register_spec_type TestMeta, &p
642
+
643
+ keys = Minitest::Spec::TYPES.map(&:first)
644
+
645
+ assert_includes keys, /woot/
646
+ assert_includes keys, p
647
+ ensure
648
+ Minitest::Spec::TYPES.replace original_types
649
+ end
650
+
651
+ def test_spec_type
652
+ original_types = Minitest::Spec::TYPES.dup
653
+
654
+ Minitest::Spec.register_spec_type(/A$/, MiniSpecA)
655
+ Minitest::Spec.register_spec_type MiniSpecB do |desc|
656
+ desc.superclass == ExampleA
657
+ end
658
+
659
+ assert_equal MiniSpecA, Minitest::Spec.spec_type(ExampleA)
660
+ assert_equal MiniSpecB, Minitest::Spec.spec_type(ExampleB)
661
+ ensure
662
+ Minitest::Spec::TYPES.replace original_types
663
+ end
664
+
665
+ def test_name
666
+ spec_a = describe ExampleA do; end
667
+ spec_b = describe ExampleB, :random_method do; end
668
+
669
+ assert_equal "ExampleA", spec_a.name
670
+ assert_equal "ExampleB::random_method", spec_b.name
671
+ end
672
+
673
+ def test_name2
674
+ assert_equal "NamedExampleA", NamedExampleA.name
675
+ assert_equal "NamedExampleB", NamedExampleB.name
676
+ assert_equal "NamedExampleC", NamedExampleC.name
677
+
678
+ spec_a = describe ExampleA do; end
679
+ spec_b = describe ExampleB, :random_method do; end
680
+
681
+ assert_equal "ExampleA", spec_a.name
682
+ assert_equal "ExampleB::random_method", spec_b.name
683
+ end
684
+
685
+ def test_structure
686
+ x, y, z, * = util_structure
687
+
688
+ assert_equal "top-level thingy", x.to_s
689
+ assert_equal "top-level thingy::inner thingy", y.to_s
690
+ assert_equal "top-level thingy::inner thingy::very inner thingy", z.to_s
691
+
692
+ assert_equal "top-level thingy", x.desc
693
+ assert_equal "inner thingy", y.desc
694
+ assert_equal "very inner thingy", z.desc
695
+
696
+ top_methods = %w(setup teardown test_0001_top-level-it)
697
+ inner_methods1 = %w(setup teardown test_0001_inner-it)
698
+ inner_methods2 = inner_methods1 +
699
+ %w(test_0002_anonymous test_0003_anonymous)
700
+
701
+ assert_equal top_methods, x.instance_methods(false).sort.map(&:to_s)
702
+ assert_equal inner_methods1, y.instance_methods(false).sort.map(&:to_s)
703
+ assert_equal inner_methods2, z.instance_methods(false).sort.map(&:to_s)
704
+ end
705
+
706
+ def test_setup_teardown_behavior
707
+ _, _, z, before_list, after_list = util_structure
708
+
709
+ @tu = z
710
+
711
+ run_tu_with_fresh_reporter
712
+
713
+ size = z.runnable_methods.size
714
+ assert_equal [1, 2, 3] * size, before_list
715
+ assert_equal [3, 2, 1] * size, after_list
716
+ end
717
+
718
+ def test_describe_first_structure
719
+ x = x1 = x2 = y = z = nil
720
+ x = describe "top-level thingy" do
721
+ y = describe "first thingy" do end
722
+
723
+ x1 = it "top level it" do end
724
+ x2 = it "не латинские буквы-и-спецсимволы&いった α, β, γ, δ, ε hello!!! world" do end
725
+
726
+ z = describe "second thingy" do end
727
+ end
728
+
729
+ test_methods = ["test_0001_top level it", "test_0002_не латинские буквы-и-спецсимволы&いった α, β, γ, δ, ε hello!!! world"].sort
730
+
731
+ assert_equal test_methods, [x1, x2]
732
+ assert_equal test_methods,
733
+ x.instance_methods.grep(/^test/).map {|o| o.to_s}.sort
734
+ assert_equal [], y.instance_methods.grep(/^test/)
735
+ assert_equal [], z.instance_methods.grep(/^test/)
736
+ end
737
+
738
+ def test_structure_subclasses
739
+ z = nil
740
+ x = Class.new Minitest::Spec do
741
+ def xyz; end
742
+ end
743
+ y = Class.new x do
744
+ z = describe("inner") {}
745
+ end
746
+
747
+ assert_respond_to x.new(nil), "xyz"
748
+ assert_respond_to y.new(nil), "xyz"
749
+ assert_respond_to z.new(nil), "xyz"
750
+ end
751
+ end
752
+
753
+ class TestSpecInTestCase < MetaMetaMetaTestCase
754
+ def setup
755
+ super
756
+
757
+ @tc = self
758
+ @assertion_count = 1
759
+ end
760
+
761
+ def assert_triggered expected, klass = Minitest::Assertion
762
+ @assertion_count += 1
763
+
764
+ e = assert_raises klass do
765
+ yield
766
+ end
767
+
768
+ msg = e.message.sub(/(---Backtrace---).*/m, "\1")
769
+ msg.gsub!(/\(oid=[-0-9]+\)/, "(oid=N)")
770
+
771
+ assert_equal expected, msg
772
+ end
773
+
774
+ def teardown
775
+ msg = "expected #{@assertion_count} assertions, not #{@tc.assertions}"
776
+ assert_equal @assertion_count, @tc.assertions, msg
777
+ end
778
+
779
+ def test_expectation
780
+ @tc.assert_equal true, 1.must_equal(1)
781
+ end
782
+
783
+ def test_expectation_triggered
784
+ assert_triggered "Expected: 2\n Actual: 1" do
785
+ 1.must_equal 2
786
+ end
787
+ end
788
+
789
+ def test_expectation_with_a_message
790
+ assert_triggered "Expected: 2\n Actual: 1" do
791
+ 1.must_equal 2, ""
792
+ end
793
+ end
794
+ end