File: test-test-case.rb

package info (click to toggle)
ruby-test-unit 3.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,016 kB
  • sloc: ruby: 14,239; makefile: 13
file content (1284 lines) | stat: -rw-r--r-- 37,177 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
# Author:: Nathaniel Talbott.
# Copyright:: Copyright (c) 2008-2014  Kouhei Sutou <kou@clear-code.com>
# Copyright:: Copyright (c) 2011  Haruka Yoshihara <yoshihara@clear-code.com>
# Copyright:: Copyright (c) 2000-2002  Nathaniel Talbott
# License:: Ruby license.

require 'test/unit'

module Test
  module Unit
    class TestTestCase < TestCase
      self.test_order = :random
      def test_creation
        test_case = Class.new(TestCase) do
          def test_with_arguments(arg1, arg2)
          end
        end

        test = test_case.new(:test_with_arguments)
        check("Should have caught an invalid test when there are arguments",
              !test.valid?)

        test = test_case.new(:non_existent_test)
        check("Should have caught an invalid test when the method does not exist",
              !test.valid?)
      end

      def setup
        @tc_failure_error = Class.new(TestCase) do
          def test_failure
            assert_block("failure") do
              false
            end
          end
          def test_error
            1 / 0
          end
          def test_nested_failure
            nested
          end
          def nested
            assert_block("nested") do
              false
            end
          end
          def return_passed?
            return passed?
          end
        end

        def @tc_failure_error.name
          "TC_FailureError"
        end
      end

      def jruby_backtrace_entry?(entry)
        entry.start_with?("org/jruby/")
      end

      def rubinius_backtrace_entry?(entry)
        entry.start_with?("kernel/")
      end

      def internal_backtrace_entry?(entry)
        entry.start_with?("<internal:")
      end

      def normalize_location(location)
        filtered_location = location.reject do |entry|
          jruby_backtrace_entry?(entry) or
            rubinius_backtrace_entry?(entry) or
            internal_backtrace_entry?(entry)
        end
        filtered_location.collect do |entry|
          entry.sub(/:\d+:/, ":0:")
        end
      end

      def test_add_failed_assertion
        test_case = @tc_failure_error.new(:test_failure)
        assert do
          test_case.passed?
        end

        result = TestResult.new
        faults = []
        result.add_listener(TestResult::FAULT) do |fault|
          faults << fault
        end
        progress = []
        test_case.run(result) do |*arguments|
          progress << arguments
        end
        fault_details = faults.collect do |fault|
          {
            :class     => fault.class,
            :message   => fault.message,
            :test_name => fault.test_name,
            :location  => normalize_location(fault.location),
          }
        end
        assert_equal([
                       {
                         :class     => Failure,
                         :message   => "failure",
                         :test_name => "test_failure(TC_FailureError)",
                         :location  => [
                           "#{__FILE__}:0:in `test_failure'",
                           "#{__FILE__}:0:in `#{__method__}'",
                         ],
                       },
                     ],
                     fault_details) unless ENV['AUTOPKGTEST_TMP']

        assert do
          not test_case.passed?
        end
        assert_equal([
                       [TestCase::STARTED,         test_case.name],
                       [TestCase::STARTED_OBJECT,  test_case],
                       [TestCase::FINISHED,        test_case.name],
                       [TestCase::FINISHED_OBJECT, test_case],
                     ],
                     progress)
      end

      def test_add_failure_nested
        test_case = @tc_failure_error.new(:test_nested_failure)
        assert do
          test_case.passed?
        end

        result = TestResult.new
        faults = []
        result.add_listener(TestResult::FAULT) do |fault|
          faults << fault
        end
        test_case.run(result) do
        end
        fault_details = faults.collect do |fault|
          {
            :class     => fault.class,
            :message   => fault.message,
            :test_name => fault.test_name,
            :location  => normalize_location(fault.location),
          }
        end
        assert_equal([
                       {
                         :class     => Failure,
                         :message   => "nested",
                         :test_name => "test_nested_failure(TC_FailureError)",
                         :location  => [
                           "#{__FILE__}:0:in `nested'",
                           "#{__FILE__}:0:in `test_nested_failure'",
                           "#{__FILE__}:0:in `#{__method__}'",
                         ],
                       },
                     ],
                     fault_details) unless ENV['AUTOPKGTEST_TMP']

        assert do
          not test_case.passed?
        end
      end

      def jruby?
        defined?(JRUBY_VERSION)
      end

      def rubinius?
        false # TODO
      end

      def cruby?
        (not jruby?) and (not rubinius?)
      end

      def test_add_error
        test_case = @tc_failure_error.new(:test_error)
        assert do
          test_case.passed?
        end

        result = TestResult.new
        faults = []
        result.add_listener(TestResult::FAULT) do |fault|
          faults << fault
        end
        test_case.run(result) do
        end
        fault_details = faults.collect do |fault|
          {
            :class     => fault.class,
            :message   => fault.message,
            :test_name => fault.test_name,
            :location  => normalize_location(fault.location),
          }
        end
        location = []
        location << "#{__FILE__}:0:in `/'" if cruby?
        location << "#{__FILE__}:0:in `test_error'"
        location << "#{__FILE__}:0:in `#{__method__}'"
        assert_equal([
                       {
                         :class     => Error,
                         :message   => "ZeroDivisionError: divided by 0",
                         :test_name => "test_error(TC_FailureError)",
                         :location  => location,
                       },
                     ],
                     fault_details) unless ENV['AUTOPKGTEST_TMP']

        assert do
          not test_case.passed?
        end
      end

      def test_no_tests
        suite = TestCase.suite
        check("Should have a test suite", suite.instance_of?(TestSuite))
        check("Should have one test", suite.size == 1)
        check("Should have the default test", suite.tests.first.name == "default_test(Test::Unit::TestCase)")

        result = TestResult.new
        suite.run(result) {}
        check("Should have had one test run", result.run_count == 1)
        check("Should have had one test failure", result.failure_count == 1)
        check("Should have had no errors", result.error_count == 0)
      end

      def test_suite
        tc = Class.new(TestCase) do
          def test_succeed
            assert_block {true}
          end
          def test_fail
            assert_block {false}
          end
          def test_error
            1/0
          end
          def dont_run
            assert_block {true}
          end
          def test_dont_run(argument)
            assert_block {true}
          end
          def test
            assert_block {true}
          end
        end

        suite = tc.suite
        check("Should have a test suite", suite.instance_of?(TestSuite))
        check("Should have three tests", suite.size == 3)

        result = TestResult.new
        suite.run(result) {}
        check("Should have had three test runs", result.run_count == 3)
        check("Should have had one test failure", result.failure_count == 1)
        check("Should have had one test error", result.error_count == 1)
      end

      def test_setup_teardown
        tc = Class.new(TestCase) do
          attr_reader(:setup_called, :teardown_called)
          def initialize(test)
            super(test)
            @setup_called = false
            @teardown_called = false
          end
          def setup
            @setup_called = true
          end
          def teardown
            @teardown_called = true
          end
          def test_succeed
            assert_block {true}
          end
          def test_fail
            assert_block {false}
          end
          def test_error
            raise "Error!"
          end
        end
        result = TestResult.new

        test = tc.new(:test_succeed)
        test.run(result) {}
        check("Should have called setup the correct number of times", test.setup_called)
        check("Should have called teardown the correct number of times", test.teardown_called)

        test = tc.new(:test_fail)
        test.run(result) {}
        check("Should have called setup the correct number of times", test.setup_called)
        check("Should have called teardown the correct number of times", test.teardown_called)

        test = tc.new(:test_error)
        test.run(result) {}
        check("Should have called setup the correct number of times", test.setup_called)
        check("Should have called teardown the correct number of times", test.teardown_called)

        check("Should have had two test runs", result.run_count == 3)
        check("Should have had a test failure", result.failure_count == 1)
        check("Should have had a test error", result.error_count == 1)
      end

      def test_assertion_failed_not_called
        tc = Class.new(TestCase) do
          def test_thing
            raise AssertionFailedError.new
          end
        end

        suite = tc.suite
        check("Should have one test", suite.size == 1)
        result = TestResult.new
        suite.run(result) {}
        check("Should have had one test run", result.run_count == 1)
        check("Should have had one assertion failure", result.failure_count == 1)
        check("Should not have any assertion errors but had #{result.error_count}", result.error_count == 0)
      end

      def test_equality
        tc1 = Class.new(TestCase) do
          def test_1
          end
          def test_2
          end
        end

        tc2 = Class.new(TestCase) do
          def test_1
          end
        end

        test1 = tc1.new('test_1')
        test2 = tc1.new('test_1')
        check("Should be equal", test1 == test2)
        check("Should be equal", test2 == test1)

        test1 = tc1.new('test_2')
        check("Should not be equal", test1 != test2)
        check("Should not be equal", test2 != test1)

        test2 = tc1.new('test_2')
        check("Should be equal", test1 == test2)
        check("Should be equal", test2 == test1)

        test1 = tc1.new('test_1')
        test2 = tc2.new('test_1')
        check("Should not be equal", test1 != test2)
        check("Should not be equal", test2 != test1)

        check("Should not be equal", test1 != Object.new)
        check("Should not be equal", Object.new != test1)
      end

      def test_re_raise_exception
        test_case = Class.new(TestCase) do
          def test_raise_interrupt
            raise Interrupt, "from test"
          end
        end

        test = test_case.new("test_raise_interrupt")
        begin
          test.run(TestResult.new) {}
          check("Should not be reached", false)
        rescue Exception
          check("Interrupt exception should be re-raised", $!.class == Interrupt)
        end
      end

      def test_timeout_error
        test_case = Class.new(TestCase) do
          def test_raise_timeout_error
            require "timeout"
            raise Timeout::Error
          end
        end

        test_suite = test_case.suite
        result = TestResult.new
        begin
          test_suite.run(result) {}
          check("Timeout::Error should be handled as error",
                result.error_count == 1)
        rescue Exception
          check("Timeout::Error should not be passed through: #{$!}", false)
        end
      end

      def test_interrupted
        test_case = Class.new(TestCase) do
          def test_fail
            flunk
          end

          def test_nothing
          end
        end

        failed_test = test_case.new(:test_fail)
        failed_test.run(TestResult.new) {}
        check("Should be interrupted", failed_test.interrupted?)

        success_test = test_case.new(:test_nothing)
        success_test.run(TestResult.new) {}
        check("Should not be interrupted", !success_test.interrupted?)
      end

      def test_inherited_test_should_be_ignored
        test_case = Class.new(TestCase) do
          def test_nothing
          end
        end

        sub_test_case = Class.new(test_case) do
          def test_fail
            flunk
          end
        end

        test = test_case.new("test_nothing")
        assert_predicate(test, :valid?)

        test = sub_test_case.new("test_fail")
        assert_predicate(test, :valid?)

        test = sub_test_case.new("test_nothing")
        assert_not_predicate(test, :valid?)
      end

      def test_mixin_test_should_not_be_ignored
        test_module = Module.new do
          def test_nothing
          end
        end

        test_case = Class.new(Test::Unit::TestCase) do
          include test_module

          def test_fail
            flunk
          end
        end

        assert_nothing_thrown do
          test_case.new("test_nothing")
        end

        assert_nothing_thrown do
          test_case.new("test_fail")
        end
      end

      def test_defined_order
        test_case = Class.new(Test::Unit::TestCase) do
          def test_z
          end

          def test_1
          end

          def test_a
          end
        end

        test_case.test_order = :defined
        assert_equal(["test_z", "test_1", "test_a"],
                     test_case.suite.tests.collect {|test| test.method_name})
      end

      def test_declarative_style
        test_case = Class.new(Test::Unit::TestCase) do
          test "declarative style test definition" do
          end

          test "include parenthesis" do
          end

          test "1 + 2 = 3" do
          end
        end

        test_case.test_order = :defined

        assert_equal(["test: declarative style test definition",
                      "test: include parenthesis",
                      "test: 1 + 2 = 3"],
                     test_case.suite.tests.collect {|test| test.method_name})

        assert_equal(["declarative style test definition",
                      "include parenthesis",
                      "1 + 2 = 3"],
                     test_case.suite.tests.collect {|test| test.description})
      end

      def test_test_mark
        test_case = Class.new(Test::Unit::TestCase) do
          test
          def my_test_method
          end
        end

        test_case.test_order = :defined

        assert_equal(["my_test_method"],
                     test_case.suite.tests.collect {|test| test.method_name})
      end

      def test_redefine_method
        test_case = Class.new(Test::Unit::TestCase) do
          self.test_order = :alphabetic

          def test_name
          end
          alias_method :test_name2, :test_name

          def test_name
          end
        end

        suite = test_case.suite
        assert_equal(["test_name", "test_name2"],
                     suite.tests.collect {|test| test.method_name})
        result = TestResult.new
        suite.run(result) {}
        assert_equal("2 tests, 0 assertions, 0 failures, " +
                     "0 errors, 0 pendings, 0 omissions, 1 notifications",
                     result.summary)
      end

      def test_data_driven_test
        test_case = Class.new(TestCase) do
          def test_with_data(data)
          end
        end

        test = test_case.new("test_with_data")
        assert_not_predicate(test, :valid?)
        test.assign_test_data("label1", :test_data1)
        assert_predicate(test, :valid?)
      end

      def test_data_driven_test_without_parameter
        test_case = Class.new(TestCase) do
          data("label" => "value")
          def test_without_parameter
            assert_equal("value", data)
          end
        end

        suite = test_case.suite
        assert_equal(["test_without_parameter"],
                     suite.tests.collect {|test| test.method_name})
        result = TestResult.new
        suite.run(result) {}
        assert_equal("1 tests, 1 assertions, 0 failures, " +
                     "0 errors, 0 pendings, 0 omissions, 0 notifications",
                     result.summary)
      end

      private
      def check(message, passed)
        add_assertion
        raise AssertionFailedError.new(message) unless passed
      end

      class TestTestDefined < self
        class TestNoQuery < self
          def test_no_test
            test_case = Class.new(TestCase) do
            end
            assert_false(test_case.test_defined?({}))
          end

          def test_have_def_style_test
            test_case = Class.new(TestCase) do
              def test_nothing
              end
            end
            assert_true(test_case.test_defined?({}))
          end

          def test_have_method_style_test
            test_case = Class.new(TestCase) do
              test "nothing" do
              end
            end
            assert_true(test_case.test_defined?({}))
          end
        end

        class TestPath < self
          class TestDefStyle < self
            def test_base_name
              test_case = Class.new(TestCase) do
                def test_nothing
                end
              end
              base_name = File.basename(__FILE__)
              assert_true(test_case.test_defined?(:path => base_name))
            end

            def test_absolute_path
              test_case = Class.new(TestCase) do
                def test_nothing
                end
              end
              assert_true(test_case.test_defined?(:path => __FILE__))
            end

            def test_not_match
              test_case = Class.new(TestCase) do
                def test_nothing
                end
              end
              assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
            end
          end

          class TestMethodStyle < self
            def test_base_name
              test_case = Class.new(TestCase) do
                test "nothing" do
                end
              end
              base_name = File.basename(__FILE__)
              assert_true(test_case.test_defined?(:path => base_name))
            end

            def test_absolute_path
              test_case = Class.new(TestCase) do
                test "nothing" do
                end
              end
              assert_true(test_case.test_defined?(:path => __FILE__))
            end

            def test_not_match
              test_case = Class.new(TestCase) do
                test "nothing" do
                end
              end
              assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
            end
          end
        end

        class TestLine < self
          class TestDefStyle < self
            def test_before
              line_before = nil
              test_case = Class.new(TestCase) do
                line_before = __LINE__
                def test_nothing
                end
              end
              assert_false(test_case.test_defined?(:line => line_before))
            end

            def test_def
              line_def = nil
              test_case = Class.new(TestCase) do
                line_def = __LINE__; def test_nothing
                end
              end
              assert_true(test_case.test_defined?(:line => line_def))
            end

            def test_after
              line_after = nil
              test_case = Class.new(TestCase) do
                def test_nothing
                end
                line_after = __LINE__
              end
              assert_true(test_case.test_defined?(:line => line_after))
            end

            def test_child
              child_test_case = nil
              line_child = nil
              parent_test_case = Class.new(TestCase) do
                test "parent" do
                end

                child_test_case = Class.new(self) do
                  line_child = __LINE__; test "child" do
                  end
                end
              end
              assert_equal([
                             false,
                             true,
                           ],
                           [
                             parent_test_case.test_defined?(:line => line_child),
                             child_test_case.test_defined?(:line => line_child),
                           ])
            end
          end

          class TestMethodStyle < self
            def test_before
              line_before = nil
              test_case = Class.new(TestCase) do
                line_before = __LINE__
                test "nothing" do
                end
              end
              assert_false(test_case.test_defined?(:line => line_before))
            end

            def test_method
              line_method = nil
              test_case = Class.new(TestCase) do
                line_method = __LINE__; test "nothing" do
                end
              end
              assert_true(test_case.test_defined?(:line => line_method))
            end

            def test_after
              line_after = nil
              test_case = Class.new(TestCase) do
                test "nothing" do
                end
                line_after = __LINE__
              end
              assert_true(test_case.test_defined?(:line => line_after))
            end

            def test_child
              child_test_case = nil
              line_child = nil
              parent_test_case = Class.new(TestCase) do
                test "parent" do
                end

                child_test_case = Class.new(self) do
                  line_child = __LINE__; test "child" do
                  end
                end
              end
              assert_equal([
                             false,
                             true,
                           ],
                           [
                             parent_test_case.test_defined?(:line => line_child),
                             child_test_case.test_defined?(:line => line_child),
                           ])
            end

            def test_with_setup
              line = nil
              test_case = Class.new(TestCase) do
                setup do
                end

                line = __LINE__; test "with setup" do
                end
              end
              assert do
                test_case.test_defined?(:line => line,
                                        :method_name => "test: with setup")
              end
            end
          end
        end

        class TestMethodName < self
          class TestDefStyle < self
            def test_match
              test_case = Class.new(TestCase) do
                def test_nothing
                end
              end
              query = {:method_name => "test_nothing"}
              assert_true(test_case.test_defined?(query))
            end

            def test_not_match
              test_case = Class.new(TestCase) do
                def test_nothing
                end
              end
              query = {:method_name => "test_nonexistent"}
              assert_false(test_case.test_defined?(query))
            end
          end

          class TestMethodStyle < self
            def test_match
              test_case = Class.new(TestCase) do
                test "nothing" do
                end
              end
              query = {:method_name => "test: nothing"}
              assert_true(test_case.test_defined?(query))
            end

            def test_not_match
              test_case = Class.new(TestCase) do
                test "nothing" do
                end
              end
              query = {:method_name => "test: nonexistent"}
              assert_false(test_case.test_defined?(query))
            end
          end
        end

        class TestCombine < self
          class TestDefStyle < self
            def test_line_middle
              line_middle = nil
              test_case = Class.new(TestCase) do
                def test_before
                end
                line_middle = __LINE__
                def test_after
                end
              end
              query = {
                :path => __FILE__,
                :line => line_middle,
                :method_name => "test_before",
              }
              assert_true(test_case.test_defined?(query))
            end

            def test_line_after_def
              line_after_def = nil
              test_case = Class.new(TestCase) do
                def test_before
                end

                line_after_def = __LINE__; def test_after
                end
              end
              query = {
                :path => __FILE__,
                :line => line_after_def,
                :method_name => "test_before",
              }
              assert_false(test_case.test_defined?(query))
            end
          end

          class TestMethodStyle < self
            def test_line_middle
              line_middle = nil
              test_case = Class.new(TestCase) do
                test "before" do
                end
                line_middle = __LINE__
                test "after" do
                end
              end
              query = {
                :path => __FILE__,
                :line => line_middle,
                :method_name => "test: before",
              }
              assert_true(test_case.test_defined?(query))
            end

            def test_line_after_method
              line_after_method = nil
              test_case = Class.new(TestCase) do
                test "before" do
                end

                line_after_method = __LINE__; test "after" do
                end
              end
              query = {
                :path => __FILE__,
                :line => line_after_method,
                :method_name => "test: before",
              }
              assert_false(test_case.test_defined?(query))
            end
          end
        end
      end

      class TestSubTestCase < self
        class TestName < self
          def test_anonymous
            test_case = Class.new(TestCase)
            sub_test_case = test_case.sub_test_case("sub test case") do
            end
            assert_equal("sub test case", sub_test_case.name)
          end

          def test_named
            test_case = Class.new(TestCase)
            def test_case.name
              "ParentTestCase"
            end
            sub_test_case = test_case.sub_test_case("sub test case") do
            end
            assert_equal("ParentTestCase::sub test case", sub_test_case.name)
          end
        end

        def test_suite
          test_case = Class.new(TestCase)
          sub_test_case = test_case.sub_test_case("sub test case") do
            def test_nothing
            end
          end
          test_method_names = sub_test_case.suite.tests.collect do |test|
            test.method_name
          end
          assert_equal(["test_nothing"], test_method_names)
        end

        def test_duplicated_name
          test_case = Class.new(TestCase) do
            def test_nothing
            end
          end
          sub_test_case = test_case.sub_test_case("sub test case") do
            def test_nothing
            end
          end

          test_method_names = test_case.suite.tests.collect do |test|
            test.method_name
          end
          sub_test_method_names = sub_test_case.suite.tests.collect do |test|
            test.method_name
          end

          assert_equal([
                         ["test_nothing"],
                         ["test_nothing"],
                       ],
                       [
                         test_method_names,
                         sub_test_method_names,
                       ])
        end
      end

      class TestStartupShutdown < self
        class TestOrder < self
          module CallLogger
            def called
              @@called ||= []
            end
          end

          def call_order(test_case)
            test_case.called.clear
            test_suite = test_case.suite
            test_suite.run(TestResult.new) {}
            test_case.called
          end

          class TestNoInheritance < self
            def setup
              @test_case = Class.new(TestCase) do
                extend CallLogger

                class << self
                  def startup
                    called << :startup
                  end

                  def shutdown
                    called << :shutdown
                  end
                end

                def setup
                  self.class.called << :setup
                end

                def teardown
                  self.class.called << :teardown
                end

                def test1
                end

                def test2
                end
              end
            end

            def test_call_order
              assert_equal([
                             :startup,
                             :setup, :teardown,
                             :setup, :teardown,
                             :shutdown,
                           ],
                           call_order(@test_case))
            end
          end

          class TestInheritance < self
            def setup
              @original_descendants = TestCase::DESCENDANTS.dup
              TestCase::DESCENDANTS.clear

              @parent_test_case = Class.new(TestCase) do
                extend CallLogger

                self.test_order = :alphabetic

                class << self
                  def startup
                    called << :startup_parent
                  end

                  def shutdown
                    called << :shutdown_parent
                  end
                end

                def setup
                  self.class.called << :setup_parent
                end

                def teardown
                  self.class.called << :teardown_parent
                end

                def test1_parent
                  self.class.called << :test1_parent
                end

                def test2_parent
                  self.class.called << :test2_parent
                end
              end

              @child_test_case = Class.new(@parent_test_case) do
                class << self
                  def startup
                    called << :startup_child
                  end

                  def shutdown
                    called << :shutdown_child
                  end
                end

                def setup
                  self.class.called << :setup_child
                end

                def teardown
                  self.class.called << :teardown_child
                end

                def test1_child
                  self.class.called << :test1_child
                end

                def test2_child
                  self.class.called << :test2_child
                end
              end
            end

            def teardown
              TestCase::DESCENDANTS.replace(@original_descendants)
            end

            def test_call_order
              collector = Collector::Descendant.new
              test_suite = collector.collect
              test_suite.run(TestResult.new) {}
              called = @parent_test_case.called
              assert_equal([
                             :startup_parent,
                             :setup_parent, :test1_parent, :teardown_parent,
                             :setup_parent, :test2_parent, :teardown_parent,
                             :startup_child,
                             :setup_child, :test1_child, :teardown_child,
                             :setup_child, :test2_child, :teardown_child,
                             :shutdown_child,
                             :shutdown_parent,
                           ],
                           called)
            end
          end
        end

        class TestError < self
          def run_test_case(test_case)
            test_suite = test_case.suite
            result = TestResult.new
            test_suite.run(result) {}
            result
          end

          def error_count(test_case)
            run_test_case(test_case).error_count
          end

          def test_on_startup
            test_case = Class.new(TestCase) do
              class << self
                def startup
                  raise "from startup"
                end
              end

              def test_nothing
              end
            end

            assert_equal(1, error_count(test_case))
          end

          def test_pass_through_on_startup
            test_case = Class.new(TestCase) do
              class << self
                def startup
                  raise Interrupt, "from startup"
                end
              end

              def test_nothing
              end
            end

            assert_raise(Interrupt) do
              run_test_case(test_case)
            end
          end

          def test_on_shutdown
            test_case = Class.new(TestCase) do
              class << self
                def shutdown
                  raise "from shutdown"
                end
              end

              def test_nothing
              end
            end

            assert_equal(1, error_count(test_case))
          end

          def test_pass_through_on_shutdown
            test_case = Class.new(TestCase) do
              class << self
                def shutdown
                  raise Interrupt, "from shutdown"
                end
              end

              def test_nothing
              end
            end

            assert_raise(Interrupt) do
              run_test_case(test_case)
            end
          end

          def test_pass_through_in_test
            test_case = Class.new(TestCase) do
              @called = []
              class << self
                def called
                  @called
                end

                def startup
                  @called << :startup
                end

                def shutdown
                  @called << :shutdown
                end
              end

              def test_error
                raise Interrupt, "from test"
              end
            end

            assert_raise(Interrupt) do
              run_test_case(test_case)
            end
            assert_equal([:startup, :shutdown],
                         test_case.called)
          end

          class TestName < self
            def test_no_data
              test_case = Class.new(TestCase) do
                class << self
                  def name
                    "TestCase"
                  end
                end

                def test_nothing
                end
              end

              test = test_case.new("test_nothing")
              assert_equal("test_nothing(TestCase)",
                           test.name)
            end

            def test_data
              test_case = Class.new(TestCase) do
                class << self
                  def name
                    "TestCase"
                  end
                end

                def test_nothing
                end
              end

              test = test_case.new("test_nothing")
              test.assign_test_data("(nil)", nil)
              assert_equal("test_nothing[(nil)](TestCase)",
                           test.name)
            end
          end

          class TestLocalName < self
            def test_no_data
              test_case = Class.new(TestCase) do
                class << self
                  def name
                    "TestCase"
                  end
                end

                def test_nothing
                end
              end

              test = test_case.new("test_nothing")
              assert_equal("test_nothing",
                           test.local_name)
            end

            def test_data
              test_case = Class.new(TestCase) do
                class << self
                  def name
                    "TestCase"
                  end
                end

                def test_nothing
                end
              end

              test = test_case.new("test_nothing")
              test.assign_test_data("(nil)", nil)
              assert_equal("test_nothing[(nil)]",
                           test.local_name)
            end
          end
        end
      end
    end
  end
end