test-unit 3.2.5 → 3.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +5 -5
  2. data/BSDL +24 -0
  3. data/COPYING +41 -44
  4. data/README.md +8 -11
  5. data/Rakefile +0 -23
  6. data/doc/text/getting-started.md +1 -1
  7. data/doc/text/news.md +366 -0
  8. data/lib/test/unit/assertion-failed-error.rb +35 -0
  9. data/lib/test/unit/assertions.rb +312 -112
  10. data/lib/test/unit/attribute.rb +7 -2
  11. data/lib/test/unit/autorunner.rb +79 -31
  12. data/lib/test/unit/code-snippet-fetcher.rb +7 -7
  13. data/lib/test/unit/collector/descendant.rb +1 -0
  14. data/lib/test/unit/collector/dir.rb +4 -2
  15. data/lib/test/unit/collector/load.rb +10 -13
  16. data/lib/test/unit/collector/objectspace.rb +1 -0
  17. data/lib/test/unit/collector.rb +31 -0
  18. data/lib/test/unit/color-scheme.rb +20 -2
  19. data/lib/test/unit/data-sets.rb +116 -0
  20. data/lib/test/unit/data.rb +121 -12
  21. data/lib/test/unit/diff.rb +2 -3
  22. data/lib/test/unit/fixture.rb +6 -0
  23. data/lib/test/unit/notification.rb +9 -7
  24. data/lib/test/unit/omission.rb +34 -31
  25. data/lib/test/unit/pending.rb +12 -11
  26. data/lib/test/unit/priority.rb +7 -3
  27. data/lib/test/unit/runner/console.rb +8 -0
  28. data/lib/test/unit/test-suite-creator.rb +22 -8
  29. data/lib/test/unit/testcase.rb +216 -146
  30. data/lib/test/unit/testsuite.rb +1 -1
  31. data/lib/test/unit/ui/console/testrunner.rb +92 -32
  32. data/lib/test/unit/util/memory-usage.rb +47 -0
  33. data/lib/test/unit/util/observable.rb +2 -2
  34. data/lib/test/unit/util/output.rb +5 -4
  35. data/lib/test/unit/util/procwrapper.rb +4 -4
  36. data/lib/test/unit/version.rb +1 -1
  37. data/lib/test/unit/warning.rb +3 -0
  38. data/lib/test/unit.rb +177 -161
  39. data/lib/test-unit.rb +2 -17
  40. metadata +13 -88
  41. data/GPL +0 -339
  42. data/LGPL +0 -502
  43. data/test/collector/test-descendant.rb +0 -182
  44. data/test/collector/test-load.rb +0 -442
  45. data/test/collector/test_dir.rb +0 -407
  46. data/test/collector/test_objectspace.rb +0 -102
  47. data/test/fixtures/header-label.csv +0 -3
  48. data/test/fixtures/header-label.tsv +0 -3
  49. data/test/fixtures/header.csv +0 -3
  50. data/test/fixtures/header.tsv +0 -3
  51. data/test/fixtures/no-header.csv +0 -2
  52. data/test/fixtures/no-header.tsv +0 -2
  53. data/test/fixtures/plus.csv +0 -3
  54. data/test/run-test.rb +0 -22
  55. data/test/test-assertions.rb +0 -2180
  56. data/test/test-attribute-matcher.rb +0 -38
  57. data/test/test-attribute.rb +0 -123
  58. data/test/test-code-snippet.rb +0 -37
  59. data/test/test-color-scheme.rb +0 -82
  60. data/test/test-color.rb +0 -47
  61. data/test/test-data.rb +0 -303
  62. data/test/test-diff.rb +0 -518
  63. data/test/test-emacs-runner.rb +0 -60
  64. data/test/test-error.rb +0 -26
  65. data/test/test-failure.rb +0 -33
  66. data/test/test-fault-location-detector.rb +0 -163
  67. data/test/test-fixture.rb +0 -713
  68. data/test/test-notification.rb +0 -33
  69. data/test/test-omission.rb +0 -81
  70. data/test/test-pending.rb +0 -70
  71. data/test/test-priority.rb +0 -173
  72. data/test/test-test-case.rb +0 -1278
  73. data/test/test-test-result.rb +0 -113
  74. data/test/test-test-suite-creator.rb +0 -97
  75. data/test/test-test-suite.rb +0 -151
  76. data/test/testunit-test-util.rb +0 -31
  77. data/test/ui/test_testrunmediator.rb +0 -20
  78. data/test/util/test-method-owner-finder.rb +0 -38
  79. data/test/util/test-output.rb +0 -11
  80. data/test/util/test_backtracefilter.rb +0 -52
  81. data/test/util/test_observable.rb +0 -102
  82. data/test/util/test_procwrapper.rb +0 -36
@@ -1,1278 +0,0 @@
1
- # Author:: Nathaniel Talbott.
2
- # Copyright:: Copyright (c) 2008-2014 Kouhei Sutou <kou@clear-code.com>
3
- # Copyright:: Copyright (c) 2011 Haruka Yoshihara <yoshihara@clear-code.com>
4
- # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott
5
- # License:: Ruby license.
6
-
7
- require 'test/unit'
8
-
9
- module Test
10
- module Unit
11
- class TestTestCase < TestCase
12
- self.test_order = :random
13
- def test_creation
14
- test_case = Class.new(TestCase) do
15
- def test_with_arguments(arg1, arg2)
16
- end
17
- end
18
-
19
- test = test_case.new(:test_with_arguments)
20
- check("Should have caught an invalid test when there are arguments",
21
- !test.valid?)
22
-
23
- test = test_case.new(:non_existent_test)
24
- check("Should have caught an invalid test when the method does not exist",
25
- !test.valid?)
26
- end
27
-
28
- def setup
29
- @tc_failure_error = Class.new(TestCase) do
30
- def test_failure
31
- assert_block("failure") do
32
- false
33
- end
34
- end
35
- def test_error
36
- 1 / 0
37
- end
38
- def test_nested_failure
39
- nested
40
- end
41
- def nested
42
- assert_block("nested") do
43
- false
44
- end
45
- end
46
- def return_passed?
47
- return passed?
48
- end
49
- end
50
-
51
- def @tc_failure_error.name
52
- "TC_FailureError"
53
- end
54
- end
55
-
56
- def jruby_backtrace_entry?(entry)
57
- entry.start_with?("org/jruby/")
58
- end
59
-
60
- def rubinius_backtrace_entry?(entry)
61
- entry.start_with?("kernel/")
62
- end
63
-
64
- def normalize_location(location)
65
- filtered_location = location.reject do |entry|
66
- jruby_backtrace_entry?(entry) or
67
- rubinius_backtrace_entry?(entry)
68
- end
69
- filtered_location.collect do |entry|
70
- entry.sub(/:\d+:/, ":0:")
71
- end
72
- end
73
-
74
- def test_add_failed_assertion
75
- test_case = @tc_failure_error.new(:test_failure)
76
- assert do
77
- test_case.passed?
78
- end
79
-
80
- result = TestResult.new
81
- faults = []
82
- result.add_listener(TestResult::FAULT) do |fault|
83
- faults << fault
84
- end
85
- progress = []
86
- test_case.run(result) do |*arguments|
87
- progress << arguments
88
- end
89
- fault_details = faults.collect do |fault|
90
- {
91
- :class => fault.class,
92
- :message => fault.message,
93
- :test_name => fault.test_name,
94
- :location => normalize_location(fault.location),
95
- }
96
- end
97
- assert_equal([
98
- {
99
- :class => Failure,
100
- :message => "failure",
101
- :test_name => "test_failure(TC_FailureError)",
102
- :location => [
103
- "#{__FILE__}:0:in `test_failure'",
104
- "#{__FILE__}:0:in `#{__method__}'",
105
- ],
106
- },
107
- ],
108
- fault_details)
109
-
110
- assert do
111
- not test_case.passed?
112
- end
113
- assert_equal([
114
- [TestCase::STARTED, test_case.name],
115
- [TestCase::STARTED_OBJECT, test_case],
116
- [TestCase::FINISHED, test_case.name],
117
- [TestCase::FINISHED_OBJECT, test_case],
118
- ],
119
- progress)
120
- end
121
-
122
- def test_add_failure_nested
123
- test_case = @tc_failure_error.new(:test_nested_failure)
124
- assert do
125
- test_case.passed?
126
- end
127
-
128
- result = TestResult.new
129
- faults = []
130
- result.add_listener(TestResult::FAULT) do |fault|
131
- faults << fault
132
- end
133
- test_case.run(result) do
134
- end
135
- fault_details = faults.collect do |fault|
136
- {
137
- :class => fault.class,
138
- :message => fault.message,
139
- :test_name => fault.test_name,
140
- :location => normalize_location(fault.location),
141
- }
142
- end
143
- assert_equal([
144
- {
145
- :class => Failure,
146
- :message => "nested",
147
- :test_name => "test_nested_failure(TC_FailureError)",
148
- :location => [
149
- "#{__FILE__}:0:in `nested'",
150
- "#{__FILE__}:0:in `test_nested_failure'",
151
- "#{__FILE__}:0:in `#{__method__}'",
152
- ],
153
- },
154
- ],
155
- fault_details)
156
-
157
- assert do
158
- not test_case.passed?
159
- end
160
- end
161
-
162
- def jruby?
163
- defined?(JRUBY_VERSION)
164
- end
165
-
166
- def rubinius?
167
- false # TODO
168
- end
169
-
170
- def cruby?
171
- (not jruby?) and (not rubinius?)
172
- end
173
-
174
- def test_add_error
175
- test_case = @tc_failure_error.new(:test_error)
176
- assert do
177
- test_case.passed?
178
- end
179
-
180
- result = TestResult.new
181
- faults = []
182
- result.add_listener(TestResult::FAULT) do |fault|
183
- faults << fault
184
- end
185
- test_case.run(result) do
186
- end
187
- fault_details = faults.collect do |fault|
188
- {
189
- :class => fault.class,
190
- :message => fault.message,
191
- :test_name => fault.test_name,
192
- :location => normalize_location(fault.location),
193
- }
194
- end
195
- location = []
196
- location << "#{__FILE__}:0:in `/'" if cruby?
197
- location << "#{__FILE__}:0:in `test_error'"
198
- location << "#{__FILE__}:0:in `#{__method__}'"
199
- assert_equal([
200
- {
201
- :class => Error,
202
- :message => "ZeroDivisionError: divided by 0",
203
- :test_name => "test_error(TC_FailureError)",
204
- :location => location,
205
- },
206
- ],
207
- fault_details)
208
-
209
- assert do
210
- not test_case.passed?
211
- end
212
- end
213
-
214
- def test_no_tests
215
- suite = TestCase.suite
216
- check("Should have a test suite", suite.instance_of?(TestSuite))
217
- check("Should have one test", suite.size == 1)
218
- check("Should have the default test", suite.tests.first.name == "default_test(Test::Unit::TestCase)")
219
-
220
- result = TestResult.new
221
- suite.run(result) {}
222
- check("Should have had one test run", result.run_count == 1)
223
- check("Should have had one test failure", result.failure_count == 1)
224
- check("Should have had no errors", result.error_count == 0)
225
- end
226
-
227
- def test_suite
228
- tc = Class.new(TestCase) do
229
- def test_succeed
230
- assert_block {true}
231
- end
232
- def test_fail
233
- assert_block {false}
234
- end
235
- def test_error
236
- 1/0
237
- end
238
- def dont_run
239
- assert_block {true}
240
- end
241
- def test_dont_run(argument)
242
- assert_block {true}
243
- end
244
- def test
245
- assert_block {true}
246
- end
247
- end
248
-
249
- suite = tc.suite
250
- check("Should have a test suite", suite.instance_of?(TestSuite))
251
- check("Should have three tests", suite.size == 3)
252
-
253
- result = TestResult.new
254
- suite.run(result) {}
255
- check("Should have had three test runs", result.run_count == 3)
256
- check("Should have had one test failure", result.failure_count == 1)
257
- check("Should have had one test error", result.error_count == 1)
258
- end
259
-
260
- def test_setup_teardown
261
- tc = Class.new(TestCase) do
262
- attr_reader(:setup_called, :teardown_called)
263
- def initialize(test)
264
- super(test)
265
- @setup_called = false
266
- @teardown_called = false
267
- end
268
- def setup
269
- @setup_called = true
270
- end
271
- def teardown
272
- @teardown_called = true
273
- end
274
- def test_succeed
275
- assert_block {true}
276
- end
277
- def test_fail
278
- assert_block {false}
279
- end
280
- def test_error
281
- raise "Error!"
282
- end
283
- end
284
- result = TestResult.new
285
-
286
- test = tc.new(:test_succeed)
287
- test.run(result) {}
288
- check("Should have called setup the correct number of times", test.setup_called)
289
- check("Should have called teardown the correct number of times", test.teardown_called)
290
-
291
- test = tc.new(:test_fail)
292
- test.run(result) {}
293
- check("Should have called setup the correct number of times", test.setup_called)
294
- check("Should have called teardown the correct number of times", test.teardown_called)
295
-
296
- test = tc.new(:test_error)
297
- test.run(result) {}
298
- check("Should have called setup the correct number of times", test.setup_called)
299
- check("Should have called teardown the correct number of times", test.teardown_called)
300
-
301
- check("Should have had two test runs", result.run_count == 3)
302
- check("Should have had a test failure", result.failure_count == 1)
303
- check("Should have had a test error", result.error_count == 1)
304
- end
305
-
306
- def test_assertion_failed_not_called
307
- tc = Class.new(TestCase) do
308
- def test_thing
309
- raise AssertionFailedError.new
310
- end
311
- end
312
-
313
- suite = tc.suite
314
- check("Should have one test", suite.size == 1)
315
- result = TestResult.new
316
- suite.run(result) {}
317
- check("Should have had one test run", result.run_count == 1)
318
- check("Should have had one assertion failure", result.failure_count == 1)
319
- check("Should not have any assertion errors but had #{result.error_count}", result.error_count == 0)
320
- end
321
-
322
- def test_equality
323
- tc1 = Class.new(TestCase) do
324
- def test_1
325
- end
326
- def test_2
327
- end
328
- end
329
-
330
- tc2 = Class.new(TestCase) do
331
- def test_1
332
- end
333
- end
334
-
335
- test1 = tc1.new('test_1')
336
- test2 = tc1.new('test_1')
337
- check("Should be equal", test1 == test2)
338
- check("Should be equal", test2 == test1)
339
-
340
- test1 = tc1.new('test_2')
341
- check("Should not be equal", test1 != test2)
342
- check("Should not be equal", test2 != test1)
343
-
344
- test2 = tc1.new('test_2')
345
- check("Should be equal", test1 == test2)
346
- check("Should be equal", test2 == test1)
347
-
348
- test1 = tc1.new('test_1')
349
- test2 = tc2.new('test_1')
350
- check("Should not be equal", test1 != test2)
351
- check("Should not be equal", test2 != test1)
352
-
353
- check("Should not be equal", test1 != Object.new)
354
- check("Should not be equal", Object.new != test1)
355
- end
356
-
357
- def test_re_raise_exception
358
- test_case = Class.new(TestCase) do
359
- def test_raise_interrupt
360
- raise Interrupt, "from test"
361
- end
362
- end
363
-
364
- test = test_case.new("test_raise_interrupt")
365
- begin
366
- test.run(TestResult.new) {}
367
- check("Should not be reached", false)
368
- rescue Exception
369
- check("Interrupt exception should be re-raised", $!.class == Interrupt)
370
- end
371
- end
372
-
373
- def test_timeout_error
374
- test_case = Class.new(TestCase) do
375
- def test_raise_timeout_error
376
- require "timeout"
377
- raise Timeout::Error
378
- end
379
- end
380
-
381
- test_suite = test_case.suite
382
- result = TestResult.new
383
- begin
384
- test_suite.run(result) {}
385
- check("Timeout::Error should be handled as error",
386
- result.error_count == 1)
387
- rescue Exception
388
- check("Timeout::Error should not be passed through: #{$!}", false)
389
- end
390
- end
391
-
392
- def test_interrupted
393
- test_case = Class.new(TestCase) do
394
- def test_fail
395
- flunk
396
- end
397
-
398
- def test_nothing
399
- end
400
- end
401
-
402
- failed_test = test_case.new(:test_fail)
403
- failed_test.run(TestResult.new) {}
404
- check("Should be interrupted", failed_test.interrupted?)
405
-
406
- success_test = test_case.new(:test_nothing)
407
- success_test.run(TestResult.new) {}
408
- check("Should not be interrupted", !success_test.interrupted?)
409
- end
410
-
411
- def test_inherited_test_should_be_ignored
412
- test_case = Class.new(TestCase) do
413
- def test_nothing
414
- end
415
- end
416
-
417
- sub_test_case = Class.new(test_case) do
418
- def test_fail
419
- flunk
420
- end
421
- end
422
-
423
- test = test_case.new("test_nothing")
424
- assert_predicate(test, :valid?)
425
-
426
- test = sub_test_case.new("test_fail")
427
- assert_predicate(test, :valid?)
428
-
429
- test = sub_test_case.new("test_nothing")
430
- assert_not_predicate(test, :valid?)
431
- end
432
-
433
- def test_mixin_test_should_not_be_ignored
434
- test_module = Module.new do
435
- def test_nothing
436
- end
437
- end
438
-
439
- test_case = Class.new(Test::Unit::TestCase) do
440
- include test_module
441
-
442
- def test_fail
443
- flunk
444
- end
445
- end
446
-
447
- assert_nothing_thrown do
448
- test_case.new("test_nothing")
449
- end
450
-
451
- assert_nothing_thrown do
452
- test_case.new("test_fail")
453
- end
454
- end
455
-
456
- def test_defined_order
457
- test_case = Class.new(Test::Unit::TestCase) do
458
- def test_z
459
- end
460
-
461
- def test_1
462
- end
463
-
464
- def test_a
465
- end
466
- end
467
-
468
- test_case.test_order = :defined
469
- assert_equal(["test_z", "test_1", "test_a"],
470
- test_case.suite.tests.collect {|test| test.method_name})
471
- end
472
-
473
- def test_declarative_style
474
- test_case = Class.new(Test::Unit::TestCase) do
475
- test "declarative style test definition" do
476
- end
477
-
478
- test "include parenthesis" do
479
- end
480
-
481
- test "1 + 2 = 3" do
482
- end
483
- end
484
-
485
- test_case.test_order = :defined
486
-
487
- assert_equal(["test: declarative style test definition",
488
- "test: include parenthesis",
489
- "test: 1 + 2 = 3"],
490
- test_case.suite.tests.collect {|test| test.method_name})
491
-
492
- assert_equal(["declarative style test definition",
493
- "include parenthesis",
494
- "1 + 2 = 3"],
495
- test_case.suite.tests.collect {|test| test.description})
496
- end
497
-
498
- def test_test_mark
499
- test_case = Class.new(Test::Unit::TestCase) do
500
- test
501
- def my_test_method
502
- end
503
- end
504
-
505
- test_case.test_order = :defined
506
-
507
- assert_equal(["my_test_method"],
508
- test_case.suite.tests.collect {|test| test.method_name})
509
- end
510
-
511
- def test_redefine_method
512
- test_case = Class.new(Test::Unit::TestCase) do
513
- self.test_order = :alphabetic
514
-
515
- def test_name
516
- end
517
- alias_method :test_name2, :test_name
518
-
519
- def test_name
520
- end
521
- end
522
-
523
- suite = test_case.suite
524
- assert_equal(["test_name", "test_name2"],
525
- suite.tests.collect {|test| test.method_name})
526
- result = TestResult.new
527
- suite.run(result) {}
528
- assert_equal("2 tests, 0 assertions, 0 failures, " +
529
- "0 errors, 0 pendings, 0 omissions, 1 notifications",
530
- result.summary)
531
- end
532
-
533
- def test_data_driven_test
534
- test_case = Class.new(TestCase) do
535
- def test_with_data(data)
536
- end
537
- end
538
-
539
- test = test_case.new("test_with_data")
540
- assert_not_predicate(test, :valid?)
541
- test.assign_test_data("label1", :test_data1)
542
- assert_predicate(test, :valid?)
543
- end
544
-
545
- def test_data_driven_test_without_parameter
546
- test_case = Class.new(TestCase) do
547
- data("label" => "value")
548
- def test_without_parameter
549
- end
550
- end
551
-
552
- suite = test_case.suite
553
- assert_equal(["test_without_parameter"],
554
- suite.tests.collect {|test| test.method_name})
555
- result = TestResult.new
556
- suite.run(result) {}
557
- assert_equal("1 tests, 0 assertions, 0 failures, " +
558
- "0 errors, 0 pendings, 0 omissions, 1 notifications",
559
- result.summary)
560
- end
561
-
562
- private
563
- def check(message, passed)
564
- add_assertion
565
- raise AssertionFailedError.new(message) unless passed
566
- end
567
-
568
- class TestTestDefined < self
569
- class TestNoQuery < self
570
- def test_no_test
571
- test_case = Class.new(TestCase) do
572
- end
573
- assert_false(test_case.test_defined?({}))
574
- end
575
-
576
- def test_have_def_style_test
577
- test_case = Class.new(TestCase) do
578
- def test_nothing
579
- end
580
- end
581
- assert_true(test_case.test_defined?({}))
582
- end
583
-
584
- def test_have_method_style_test
585
- test_case = Class.new(TestCase) do
586
- test "nothing" do
587
- end
588
- end
589
- assert_true(test_case.test_defined?({}))
590
- end
591
- end
592
-
593
- class TestPath < self
594
- class TestDefStyle < self
595
- def test_base_name
596
- test_case = Class.new(TestCase) do
597
- def test_nothing
598
- end
599
- end
600
- base_name = File.basename(__FILE__)
601
- assert_true(test_case.test_defined?(:path => base_name))
602
- end
603
-
604
- def test_absolute_path
605
- test_case = Class.new(TestCase) do
606
- def test_nothing
607
- end
608
- end
609
- assert_true(test_case.test_defined?(:path => __FILE__))
610
- end
611
-
612
- def test_not_match
613
- test_case = Class.new(TestCase) do
614
- def test_nothing
615
- end
616
- end
617
- assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
618
- end
619
- end
620
-
621
- class TestMethodStyle < self
622
- def test_base_name
623
- test_case = Class.new(TestCase) do
624
- test "nothing" do
625
- end
626
- end
627
- base_name = File.basename(__FILE__)
628
- assert_true(test_case.test_defined?(:path => base_name))
629
- end
630
-
631
- def test_absolute_path
632
- test_case = Class.new(TestCase) do
633
- test "nothing" do
634
- end
635
- end
636
- assert_true(test_case.test_defined?(:path => __FILE__))
637
- end
638
-
639
- def test_not_match
640
- test_case = Class.new(TestCase) do
641
- test "nothing" do
642
- end
643
- end
644
- assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
645
- end
646
- end
647
- end
648
-
649
- class TestLine < self
650
- class TestDefStyle < self
651
- def test_before
652
- line_before = nil
653
- test_case = Class.new(TestCase) do
654
- line_before = __LINE__
655
- def test_nothing
656
- end
657
- end
658
- assert_false(test_case.test_defined?(:line => line_before))
659
- end
660
-
661
- def test_def
662
- line_def = nil
663
- test_case = Class.new(TestCase) do
664
- line_def = __LINE__; def test_nothing
665
- end
666
- end
667
- assert_true(test_case.test_defined?(:line => line_def))
668
- end
669
-
670
- def test_after
671
- line_after = nil
672
- test_case = Class.new(TestCase) do
673
- def test_nothing
674
- end
675
- line_after = __LINE__
676
- end
677
- assert_true(test_case.test_defined?(:line => line_after))
678
- end
679
-
680
- def test_child
681
- child_test_case = nil
682
- line_child = nil
683
- parent_test_case = Class.new(TestCase) do
684
- test "parent" do
685
- end
686
-
687
- child_test_case = Class.new(self) do
688
- line_child = __LINE__; test "child" do
689
- end
690
- end
691
- end
692
- assert_equal([
693
- false,
694
- true,
695
- ],
696
- [
697
- parent_test_case.test_defined?(:line => line_child),
698
- child_test_case.test_defined?(:line => line_child),
699
- ])
700
- end
701
- end
702
-
703
- class TestMethodStyle < self
704
- def test_before
705
- line_before = nil
706
- test_case = Class.new(TestCase) do
707
- line_before = __LINE__
708
- test "nothing" do
709
- end
710
- end
711
- assert_false(test_case.test_defined?(:line => line_before))
712
- end
713
-
714
- def test_method
715
- line_method = nil
716
- test_case = Class.new(TestCase) do
717
- line_method = __LINE__; test "nothing" do
718
- end
719
- end
720
- assert_true(test_case.test_defined?(:line => line_method))
721
- end
722
-
723
- def test_after
724
- line_after = nil
725
- test_case = Class.new(TestCase) do
726
- test "nothing" do
727
- end
728
- line_after = __LINE__
729
- end
730
- assert_true(test_case.test_defined?(:line => line_after))
731
- end
732
-
733
- def test_child
734
- child_test_case = nil
735
- line_child = nil
736
- parent_test_case = Class.new(TestCase) do
737
- test "parent" do
738
- end
739
-
740
- child_test_case = Class.new(self) do
741
- line_child = __LINE__; test "child" do
742
- end
743
- end
744
- end
745
- assert_equal([
746
- false,
747
- true,
748
- ],
749
- [
750
- parent_test_case.test_defined?(:line => line_child),
751
- child_test_case.test_defined?(:line => line_child),
752
- ])
753
- end
754
-
755
- def test_with_setup
756
- line = nil
757
- test_case = Class.new(TestCase) do
758
- setup do
759
- end
760
-
761
- line = __LINE__; test "with setup" do
762
- end
763
- end
764
- assert do
765
- test_case.test_defined?(:line => line,
766
- :method_name => "test: with setup")
767
- end
768
- end
769
- end
770
- end
771
-
772
- class TestMethodName < self
773
- class TestDefStyle < self
774
- def test_match
775
- test_case = Class.new(TestCase) do
776
- def test_nothing
777
- end
778
- end
779
- query = {:method_name => "test_nothing"}
780
- assert_true(test_case.test_defined?(query))
781
- end
782
-
783
- def test_not_match
784
- test_case = Class.new(TestCase) do
785
- def test_nothing
786
- end
787
- end
788
- query = {:method_name => "test_nonexistent"}
789
- assert_false(test_case.test_defined?(query))
790
- end
791
- end
792
-
793
- class TestMethodStyle < self
794
- def test_match
795
- test_case = Class.new(TestCase) do
796
- test "nothing" do
797
- end
798
- end
799
- query = {:method_name => "test: nothing"}
800
- assert_true(test_case.test_defined?(query))
801
- end
802
-
803
- def test_not_match
804
- test_case = Class.new(TestCase) do
805
- test "nothing" do
806
- end
807
- end
808
- query = {:method_name => "test: nonexistent"}
809
- assert_false(test_case.test_defined?(query))
810
- end
811
- end
812
- end
813
-
814
- class TestCombine < self
815
- class TestDefStyle < self
816
- def test_line_middle
817
- line_middle = nil
818
- test_case = Class.new(TestCase) do
819
- def test_before
820
- end
821
- line_middle = __LINE__
822
- def test_after
823
- end
824
- end
825
- query = {
826
- :path => __FILE__,
827
- :line => line_middle,
828
- :method_name => "test_before",
829
- }
830
- assert_true(test_case.test_defined?(query))
831
- end
832
-
833
- def test_line_after_def
834
- line_after_def = nil
835
- test_case = Class.new(TestCase) do
836
- def test_before
837
- end
838
-
839
- line_after_def = __LINE__; def test_after
840
- end
841
- end
842
- query = {
843
- :path => __FILE__,
844
- :line => line_after_def,
845
- :method_name => "test_before",
846
- }
847
- assert_false(test_case.test_defined?(query))
848
- end
849
- end
850
-
851
- class TestMethodStyle < self
852
- def test_line_middle
853
- line_middle = nil
854
- test_case = Class.new(TestCase) do
855
- test "before" do
856
- end
857
- line_middle = __LINE__
858
- test "after" do
859
- end
860
- end
861
- query = {
862
- :path => __FILE__,
863
- :line => line_middle,
864
- :method_name => "test: before",
865
- }
866
- assert_true(test_case.test_defined?(query))
867
- end
868
-
869
- def test_line_after_method
870
- line_after_method = nil
871
- test_case = Class.new(TestCase) do
872
- test "before" do
873
- end
874
-
875
- line_after_method = __LINE__; test "after" do
876
- end
877
- end
878
- query = {
879
- :path => __FILE__,
880
- :line => line_after_method,
881
- :method_name => "test: before",
882
- }
883
- assert_false(test_case.test_defined?(query))
884
- end
885
- end
886
- end
887
- end
888
-
889
- class TestSubTestCase < self
890
- class TestName < self
891
- def test_anonymous
892
- test_case = Class.new(TestCase)
893
- sub_test_case = test_case.sub_test_case("sub test case") do
894
- end
895
- assert_equal("sub test case", sub_test_case.name)
896
- end
897
-
898
- def test_named
899
- test_case = Class.new(TestCase)
900
- def test_case.name
901
- "ParentTestCase"
902
- end
903
- sub_test_case = test_case.sub_test_case("sub test case") do
904
- end
905
- assert_equal("ParentTestCase::sub test case", sub_test_case.name)
906
- end
907
- end
908
-
909
- def test_suite
910
- test_case = Class.new(TestCase)
911
- sub_test_case = test_case.sub_test_case("sub test case") do
912
- def test_nothing
913
- end
914
- end
915
- test_method_names = sub_test_case.suite.tests.collect do |test|
916
- test.method_name
917
- end
918
- assert_equal(["test_nothing"], test_method_names)
919
- end
920
-
921
- def test_duplicated_name
922
- test_case = Class.new(TestCase) do
923
- def test_nothing
924
- end
925
- end
926
- sub_test_case = test_case.sub_test_case("sub test case") do
927
- def test_nothing
928
- end
929
- end
930
-
931
- test_method_names = test_case.suite.tests.collect do |test|
932
- test.method_name
933
- end
934
- sub_test_method_names = sub_test_case.suite.tests.collect do |test|
935
- test.method_name
936
- end
937
-
938
- assert_equal([
939
- ["test_nothing"],
940
- ["test_nothing"],
941
- ],
942
- [
943
- test_method_names,
944
- sub_test_method_names,
945
- ])
946
- end
947
- end
948
-
949
- class TestStartupShutdown < self
950
- class TestOrder < self
951
- module CallLogger
952
- def called
953
- @@called ||= []
954
- end
955
- end
956
-
957
- def call_order(test_case)
958
- test_case.called.clear
959
- test_suite = test_case.suite
960
- test_suite.run(TestResult.new) {}
961
- test_case.called
962
- end
963
-
964
- class TestNoInheritance < self
965
- def setup
966
- @test_case = Class.new(TestCase) do
967
- extend CallLogger
968
-
969
- class << self
970
- def startup
971
- called << :startup
972
- end
973
-
974
- def shutdown
975
- called << :shutdown
976
- end
977
- end
978
-
979
- def setup
980
- self.class.called << :setup
981
- end
982
-
983
- def teardown
984
- self.class.called << :teardown
985
- end
986
-
987
- def test1
988
- end
989
-
990
- def test2
991
- end
992
- end
993
- end
994
-
995
- def test_call_order
996
- assert_equal([
997
- :startup,
998
- :setup, :teardown,
999
- :setup, :teardown,
1000
- :shutdown,
1001
- ],
1002
- call_order(@test_case))
1003
- end
1004
- end
1005
-
1006
- class TestInheritance < self
1007
- def setup
1008
- @original_descendants = TestCase::DESCENDANTS.dup
1009
- TestCase::DESCENDANTS.clear
1010
-
1011
- @parent_test_case = Class.new(TestCase) do
1012
- extend CallLogger
1013
-
1014
- self.test_order = :alphabetic
1015
-
1016
- class << self
1017
- def startup
1018
- called << :startup_parent
1019
- end
1020
-
1021
- def shutdown
1022
- called << :shutdown_parent
1023
- end
1024
- end
1025
-
1026
- def setup
1027
- self.class.called << :setup_parent
1028
- end
1029
-
1030
- def teardown
1031
- self.class.called << :teardown_parent
1032
- end
1033
-
1034
- def test1_parent
1035
- self.class.called << :test1_parent
1036
- end
1037
-
1038
- def test2_parent
1039
- self.class.called << :test2_parent
1040
- end
1041
- end
1042
-
1043
- @child_test_case = Class.new(@parent_test_case) do
1044
- class << self
1045
- def startup
1046
- called << :startup_child
1047
- end
1048
-
1049
- def shutdown
1050
- called << :shutdown_child
1051
- end
1052
- end
1053
-
1054
- def setup
1055
- self.class.called << :setup_child
1056
- end
1057
-
1058
- def teardown
1059
- self.class.called << :teardown_child
1060
- end
1061
-
1062
- def test1_child
1063
- self.class.called << :test1_child
1064
- end
1065
-
1066
- def test2_child
1067
- self.class.called << :test2_child
1068
- end
1069
- end
1070
- end
1071
-
1072
- def teardown
1073
- TestCase::DESCENDANTS.replace(@original_descendants)
1074
- end
1075
-
1076
- def test_call_order
1077
- collector = Collector::Descendant.new
1078
- test_suite = collector.collect
1079
- test_suite.run(TestResult.new) {}
1080
- called = @parent_test_case.called
1081
- assert_equal([
1082
- :startup_parent,
1083
- :setup_parent, :test1_parent, :teardown_parent,
1084
- :setup_parent, :test2_parent, :teardown_parent,
1085
- :startup_child,
1086
- :setup_child, :test1_child, :teardown_child,
1087
- :setup_child, :test2_child, :teardown_child,
1088
- :shutdown_child,
1089
- :shutdown_parent,
1090
- ],
1091
- called)
1092
- end
1093
- end
1094
- end
1095
-
1096
- class TestError < self
1097
- def run_test_case(test_case)
1098
- test_suite = test_case.suite
1099
- result = TestResult.new
1100
- test_suite.run(result) {}
1101
- result
1102
- end
1103
-
1104
- def error_count(test_case)
1105
- run_test_case(test_case).error_count
1106
- end
1107
-
1108
- def test_on_startup
1109
- test_case = Class.new(TestCase) do
1110
- class << self
1111
- def startup
1112
- raise "from startup"
1113
- end
1114
- end
1115
-
1116
- def test_nothing
1117
- end
1118
- end
1119
-
1120
- assert_equal(1, error_count(test_case))
1121
- end
1122
-
1123
- def test_pass_through_on_startup
1124
- test_case = Class.new(TestCase) do
1125
- class << self
1126
- def startup
1127
- raise Interrupt, "from startup"
1128
- end
1129
- end
1130
-
1131
- def test_nothing
1132
- end
1133
- end
1134
-
1135
- assert_raise(Interrupt) do
1136
- run_test_case(test_case)
1137
- end
1138
- end
1139
-
1140
- def test_on_shutdown
1141
- test_case = Class.new(TestCase) do
1142
- class << self
1143
- def shutdown
1144
- raise "from shutdown"
1145
- end
1146
- end
1147
-
1148
- def test_nothing
1149
- end
1150
- end
1151
-
1152
- assert_equal(1, error_count(test_case))
1153
- end
1154
-
1155
- def test_pass_through_on_shutdown
1156
- test_case = Class.new(TestCase) do
1157
- class << self
1158
- def shutdown
1159
- raise Interrupt, "from shutdown"
1160
- end
1161
- end
1162
-
1163
- def test_nothing
1164
- end
1165
- end
1166
-
1167
- assert_raise(Interrupt) do
1168
- run_test_case(test_case)
1169
- end
1170
- end
1171
-
1172
- def test_pass_through_in_test
1173
- test_case = Class.new(TestCase) do
1174
- @called = []
1175
- class << self
1176
- def called
1177
- @called
1178
- end
1179
-
1180
- def startup
1181
- @called << :startup
1182
- end
1183
-
1184
- def shutdown
1185
- @called << :shutdown
1186
- end
1187
- end
1188
-
1189
- def test_error
1190
- raise Interrupt, "from test"
1191
- end
1192
- end
1193
-
1194
- assert_raise(Interrupt) do
1195
- run_test_case(test_case)
1196
- end
1197
- assert_equal([:startup, :shutdown],
1198
- test_case.called)
1199
- end
1200
-
1201
- class TestName < self
1202
- def test_no_data
1203
- test_case = Class.new(TestCase) do
1204
- class << self
1205
- def name
1206
- "TestCase"
1207
- end
1208
- end
1209
-
1210
- def test_nothing
1211
- end
1212
- end
1213
-
1214
- test = test_case.new("test_nothing")
1215
- assert_equal("test_nothing(TestCase)",
1216
- test.name)
1217
- end
1218
-
1219
- def test_data
1220
- test_case = Class.new(TestCase) do
1221
- class << self
1222
- def name
1223
- "TestCase"
1224
- end
1225
- end
1226
-
1227
- def test_nothing
1228
- end
1229
- end
1230
-
1231
- test = test_case.new("test_nothing")
1232
- test.assign_test_data("(nil)", nil)
1233
- assert_equal("test_nothing[(nil)](TestCase)",
1234
- test.name)
1235
- end
1236
- end
1237
-
1238
- class TestLocalName < self
1239
- def test_no_data
1240
- test_case = Class.new(TestCase) do
1241
- class << self
1242
- def name
1243
- "TestCase"
1244
- end
1245
- end
1246
-
1247
- def test_nothing
1248
- end
1249
- end
1250
-
1251
- test = test_case.new("test_nothing")
1252
- assert_equal("test_nothing",
1253
- test.local_name)
1254
- end
1255
-
1256
- def test_data
1257
- test_case = Class.new(TestCase) do
1258
- class << self
1259
- def name
1260
- "TestCase"
1261
- end
1262
- end
1263
-
1264
- def test_nothing
1265
- end
1266
- end
1267
-
1268
- test = test_case.new("test_nothing")
1269
- test.assign_test_data("(nil)", nil)
1270
- assert_equal("test_nothing[(nil)]",
1271
- test.local_name)
1272
- end
1273
- end
1274
- end
1275
- end
1276
- end
1277
- end
1278
- end