test-unit 2.5.5 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -84,6 +84,17 @@ class TestUnitAttribute < Test::Unit::TestCase
84
84
  changed_attributes)
85
85
  end
86
86
 
87
+ def test_attributes_with_prepended_module
88
+ omit("Module#prepend is needed") unless Module.respond_to?(:prepend, true)
89
+ test_case = Class.new(TestStack) do
90
+ prepend Module.new
91
+ end
92
+ assert_equal({
93
+ "category" => :accessor,
94
+ },
95
+ test_case.attributes("test_peek"))
96
+ end
97
+
87
98
  class TestDescription < self
88
99
  def test_decoration_style
89
100
  test_case = Class.new(TestStack) do
File without changes
@@ -1,5 +1,5 @@
1
1
  # Author:: Nathaniel Talbott.
2
- # Copyright:: Copyright (c) 2008-2012 Kouhei Sutou <kou@clear-code.com>
2
+ # Copyright:: Copyright (c) 2008-2013 Kouhei Sutou <kou@clear-code.com>
3
3
  # Copyright:: Copyright (c) 2011 Haruka Yoshihara <yoshihara@clear-code.com>
4
4
  # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott
5
5
  # License:: Ruby license.
@@ -299,135 +299,6 @@ module Test
299
299
  end
300
300
  end
301
301
 
302
- def test_startup_shutdown
303
- called = []
304
- test_case = Class.new(TestCase) do
305
- class << self
306
- def called
307
- @@called
308
- end
309
-
310
- def called=(called)
311
- @@called = called
312
- end
313
-
314
- def startup
315
- @@called << :startup
316
- end
317
-
318
- def shutdown
319
- @@called << :shutdown
320
- end
321
- end
322
- self.called = called
323
-
324
- def setup
325
- self.class.called << :setup
326
- end
327
-
328
- def teardown
329
- self.class.called << :teardown
330
- end
331
-
332
- def test1
333
- end
334
-
335
- def test2
336
- end
337
- end
338
-
339
- test_suite = test_case.suite
340
- test_suite.run(TestResult.new) {}
341
- check("startup/shutdown should be called once per test case" +
342
- ": #{called.inspect}",
343
- called == [:startup,
344
- :setup, :teardown,
345
- :setup, :teardown,
346
- :shutdown])
347
- end
348
-
349
- def test_error_on_startup
350
- test_case = Class.new(TestCase) do
351
- class << self
352
- def startup
353
- raise "from startup"
354
- end
355
- end
356
-
357
- def test_nothing
358
- end
359
- end
360
-
361
- test_suite = test_case.suite
362
- result = TestResult.new
363
- test_suite.run(result) {}
364
- check("Should record an error on startup: #{result}",
365
- result.error_count == 1)
366
- end
367
-
368
- def test_pass_through_error_on_startup
369
- test_case = Class.new(TestCase) do
370
- class << self
371
- def startup
372
- raise Interrupt, "from startup"
373
- end
374
- end
375
-
376
- def test_nothing
377
- end
378
- end
379
-
380
- test_suite = test_case.suite
381
- begin
382
- test_suite.run(TestResult.new) {}
383
- check("Should not be reached", false)
384
- rescue Exception
385
- check("Interrupt should be passed through: #{$!}",
386
- Interrupt === $!)
387
- end
388
- end
389
-
390
- def test_error_on_shutdown
391
- test_case = Class.new(TestCase) do
392
- class << self
393
- def shutdown
394
- raise "from shutdown"
395
- end
396
- end
397
-
398
- def test_nothing
399
- end
400
- end
401
-
402
- test_suite = test_case.suite
403
- result = TestResult.new
404
- test_suite.run(result) {}
405
- check("Should record an error on shutdown: #{result}",
406
- result.error_count == 1)
407
- end
408
-
409
- def test_pass_through_error_on_shutdown
410
- test_case = Class.new(TestCase) do
411
- class << self
412
- def shutdown
413
- raise Interrupt, "from shutdown"
414
- end
415
- end
416
-
417
- def test_nothing
418
- end
419
- end
420
-
421
- test_suite = test_case.suite
422
- begin
423
- test_suite.run(TestResult.new) {}
424
- check("Should not be reached", false)
425
- rescue Exception
426
- check("Interrupt should be passed through: #{$!}",
427
- Interrupt === $!)
428
- end
429
- end
430
-
431
302
  def test_interrupted
432
303
  test_case = Class.new(TestCase) do
433
304
  def test_fail
@@ -590,7 +461,13 @@ module Test
590
461
 
591
462
  class TestTestDefined < self
592
463
  class TestNoQuery < self
593
- def test_have_test
464
+ def test_no_test
465
+ test_case = Class.new(TestCase) do
466
+ end
467
+ assert_false(test_case.test_defined?({}))
468
+ end
469
+
470
+ def test_have_def_style_test
594
471
  test_case = Class.new(TestCase) do
595
472
  def test_nothing
596
473
  end
@@ -598,133 +475,270 @@ module Test
598
475
  assert_true(test_case.test_defined?({}))
599
476
  end
600
477
 
601
- def test_no_test
478
+ def test_have_method_style_test
602
479
  test_case = Class.new(TestCase) do
480
+ test "nothing" do
481
+ end
603
482
  end
604
- assert_false(test_case.test_defined?({}))
483
+ assert_true(test_case.test_defined?({}))
605
484
  end
606
485
  end
607
486
 
608
487
  class TestPath < self
609
- def test_base_name
610
- test_case = Class.new(TestCase) do
611
- def test_nothing
488
+ class TestDefStyle < self
489
+ def test_base_name
490
+ test_case = Class.new(TestCase) do
491
+ def test_nothing
492
+ end
612
493
  end
494
+ base_name = File.basename(__FILE__)
495
+ assert_true(test_case.test_defined?(:path => base_name))
613
496
  end
614
- base_name = File.basename(__FILE__)
615
- assert_true(test_case.test_defined?(:path => base_name))
616
- end
617
497
 
618
- def test_absolute_path
619
- test_case = Class.new(TestCase) do
620
- def test_nothing
498
+ def test_absolute_path
499
+ test_case = Class.new(TestCase) do
500
+ def test_nothing
501
+ end
621
502
  end
503
+ assert_true(test_case.test_defined?(:path => __FILE__))
504
+ end
505
+
506
+ def test_not_match
507
+ test_case = Class.new(TestCase) do
508
+ def test_nothing
509
+ end
510
+ end
511
+ assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
622
512
  end
623
- assert_true(test_case.test_defined?(:path => __FILE__))
624
513
  end
625
514
 
626
- def test_not_match
627
- test_case = Class.new(TestCase) do
628
- def test_nothing
515
+ class TestMethodStyle < self
516
+ def test_base_name
517
+ test_case = Class.new(TestCase) do
518
+ test "nothing" do
519
+ end
520
+ end
521
+ base_name = File.basename(__FILE__)
522
+ assert_true(test_case.test_defined?(:path => base_name))
523
+ end
524
+
525
+ def test_absolute_path
526
+ test_case = Class.new(TestCase) do
527
+ test "nothing" do
528
+ end
529
+ end
530
+ assert_true(test_case.test_defined?(:path => __FILE__))
531
+ end
532
+
533
+ def test_not_match
534
+ test_case = Class.new(TestCase) do
535
+ test "nothing" do
536
+ end
629
537
  end
538
+ assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
630
539
  end
631
- assert_false(test_case.test_defined?(:path => "nonexistent.rb"))
632
540
  end
633
541
  end
634
542
 
635
543
  class TestLine < self
636
- def test_before
637
- line_before = nil
638
- test_case = Class.new(TestCase) do
639
- line_before = __LINE__
640
- def test_nothing
544
+ class TestDefStyle < self
545
+ def test_before
546
+ line_before = nil
547
+ test_case = Class.new(TestCase) do
548
+ line_before = __LINE__
549
+ def test_nothing
550
+ end
641
551
  end
552
+ assert_false(test_case.test_defined?(:line => line_before))
642
553
  end
643
- assert_false(test_case.test_defined?(:line => line_before))
644
- end
645
554
 
646
- def test_def
647
- line_def = nil
648
- test_case = Class.new(TestCase) do
649
- line_def = __LINE__; def test_nothing
555
+ def test_def
556
+ line_def = nil
557
+ test_case = Class.new(TestCase) do
558
+ line_def = __LINE__; def test_nothing
559
+ end
560
+ end
561
+ assert_true(test_case.test_defined?(:line => line_def))
562
+ end
563
+
564
+ def test_after
565
+ line_after = nil
566
+ test_case = Class.new(TestCase) do
567
+ def test_nothing
568
+ end
569
+ line_after = __LINE__
650
570
  end
571
+ assert_true(test_case.test_defined?(:line => line_after))
651
572
  end
652
- assert_true(test_case.test_defined?(:line => line_def))
653
573
  end
654
574
 
655
- def test_after
656
- line_after = nil
657
- test_case = Class.new(TestCase) do
658
- def test_nothing
575
+ class TestMethodStyle < self
576
+ def test_before
577
+ line_before = nil
578
+ test_case = Class.new(TestCase) do
579
+ line_before = __LINE__
580
+ test "nothing" do
581
+ end
659
582
  end
660
- line_after = __LINE__
583
+ assert_false(test_case.test_defined?(:line => line_before))
584
+ end
585
+
586
+ def test_method
587
+ line_method = nil
588
+ test_case = Class.new(TestCase) do
589
+ line_method = __LINE__; test "nothing" do
590
+ end
591
+ end
592
+ assert_true(test_case.test_defined?(:line => line_method))
593
+ end
594
+
595
+ def test_after
596
+ line_after = nil
597
+ test_case = Class.new(TestCase) do
598
+ test "nothing" do
599
+ end
600
+ line_after = __LINE__
601
+ end
602
+ assert_true(test_case.test_defined?(:line => line_after))
661
603
  end
662
- assert_true(test_case.test_defined?(:line => line_after))
663
604
  end
664
605
  end
665
606
 
666
607
  class TestMethodName < self
667
- def test_match
668
- test_case = Class.new(TestCase) do
669
- def test_nothing
608
+ class TestDefStyle < self
609
+ def test_match
610
+ test_case = Class.new(TestCase) do
611
+ def test_nothing
612
+ end
670
613
  end
614
+ query = {:method_name => "test_nothing"}
615
+ assert_true(test_case.test_defined?(query))
616
+ end
617
+
618
+ def test_not_match
619
+ test_case = Class.new(TestCase) do
620
+ def test_nothing
621
+ end
622
+ end
623
+ query = {:method_name => "test_nonexistent"}
624
+ assert_false(test_case.test_defined?(query))
671
625
  end
672
- assert_true(test_case.test_defined?(:method_name => "test_nothing"))
673
626
  end
674
627
 
675
- def test_not_match
676
- test_case = Class.new(TestCase) do
677
- def test_nothing
628
+ class TestMethodStyle < self
629
+ def test_match
630
+ test_case = Class.new(TestCase) do
631
+ test "nothing" do
632
+ end
633
+ end
634
+ query = {:method_name => "test: nothing"}
635
+ assert_true(test_case.test_defined?(query))
636
+ end
637
+
638
+ def test_not_match
639
+ test_case = Class.new(TestCase) do
640
+ test "nothing" do
641
+ end
678
642
  end
643
+ query = {:method_name => "test: nonexistent"}
644
+ assert_false(test_case.test_defined?(query))
679
645
  end
680
- query = {:method_name => "test_nonexistent"}
681
- assert_false(test_case.test_defined?(query))
682
646
  end
683
647
  end
684
648
 
685
649
  class TestCombine < self
686
- def test_line_middle
687
- line_middle = nil
688
- test_case = Class.new(TestCase) do
689
- def test_before
650
+ class TestDefStyle < self
651
+ def test_line_middle
652
+ line_middle = nil
653
+ test_case = Class.new(TestCase) do
654
+ def test_before
655
+ end
656
+ line_middle = __LINE__
657
+ def test_after
658
+ end
690
659
  end
691
- line_middle = __LINE__
692
- def test_after
660
+ query = {
661
+ :path => __FILE__,
662
+ :line => line_middle,
663
+ :method_name => "test_before",
664
+ }
665
+ assert_true(test_case.test_defined?(query))
666
+ end
667
+
668
+ def test_line_after_def
669
+ line_after_def = nil
670
+ test_case = Class.new(TestCase) do
671
+ def test_before
672
+ end
673
+
674
+ line_after_def = __LINE__; def test_after
675
+ end
693
676
  end
677
+ query = {
678
+ :path => __FILE__,
679
+ :line => line_after_def,
680
+ :method_name => "test_before",
681
+ }
682
+ assert_false(test_case.test_defined?(query))
694
683
  end
695
- query = {
696
- :path => __FILE__,
697
- :line => line_middle,
698
- :method_name => "test_before",
699
- }
700
- assert_true(test_case.test_defined?(query))
701
684
  end
702
685
 
703
- def test_line_after_def
704
- line_after_def = nil
705
- test_case = Class.new(TestCase) do
706
- def test_before
686
+ class TestMethodStyle < self
687
+ def test_line_middle
688
+ line_middle = nil
689
+ test_case = Class.new(TestCase) do
690
+ test "before" do
691
+ end
692
+ line_middle = __LINE__
693
+ test "after" do
694
+ end
707
695
  end
696
+ query = {
697
+ :path => __FILE__,
698
+ :line => line_middle,
699
+ :method_name => "test: before",
700
+ }
701
+ assert_true(test_case.test_defined?(query))
702
+ end
703
+
704
+ def test_line_after_method
705
+ line_after_method = nil
706
+ test_case = Class.new(TestCase) do
707
+ test "before" do
708
+ end
708
709
 
709
- line_after_def = __LINE__; def test_after
710
+ line_after_method = __LINE__; test "after" do
711
+ end
710
712
  end
713
+ query = {
714
+ :path => __FILE__,
715
+ :line => line_after_method,
716
+ :method_name => "test: before",
717
+ }
718
+ assert_false(test_case.test_defined?(query))
711
719
  end
712
- query = {
713
- :path => __FILE__,
714
- :line => line_after_def,
715
- :method_name => "test_before",
716
- }
717
- assert_false(test_case.test_defined?(query))
718
720
  end
719
721
  end
720
722
  end
721
723
 
722
724
  class TestSubTestCase < self
723
- def test_name
724
- test_case = Class.new(TestCase)
725
- sub_test_case = test_case.sub_test_case("sub test case") do
725
+ class TestName < self
726
+ def test_anonymous
727
+ test_case = Class.new(TestCase)
728
+ sub_test_case = test_case.sub_test_case("sub test case") do
729
+ end
730
+ assert_equal("sub test case", sub_test_case.name)
731
+ end
732
+
733
+ def test_named
734
+ test_case = Class.new(TestCase)
735
+ def test_case.name
736
+ "ParentTestCase"
737
+ end
738
+ sub_test_case = test_case.sub_test_case("sub test case") do
739
+ end
740
+ assert_equal("ParentTestCase::sub test case", sub_test_case.name)
726
741
  end
727
- assert_equal("sub test case", sub_test_case.name)
728
742
  end
729
743
 
730
744
  def test_suite
@@ -739,6 +753,229 @@ module Test
739
753
  assert_equal(["test_nothing"], test_method_names)
740
754
  end
741
755
  end
756
+
757
+ class TestStartupShutdown < self
758
+ class TestOrder < self
759
+ module CallLogger
760
+ def called
761
+ @@called ||= []
762
+ end
763
+ end
764
+
765
+ def call_order(test_case)
766
+ test_case.called.clear
767
+ test_suite = test_case.suite
768
+ test_suite.run(TestResult.new) {}
769
+ test_case.called
770
+ end
771
+
772
+ class TestNoInheritance < self
773
+ def setup
774
+ @test_case = Class.new(TestCase) do
775
+ extend CallLogger
776
+
777
+ class << self
778
+ def startup
779
+ called << :startup
780
+ end
781
+
782
+ def shutdown
783
+ called << :shutdown
784
+ end
785
+ end
786
+
787
+ def setup
788
+ self.class.called << :setup
789
+ end
790
+
791
+ def teardown
792
+ self.class.called << :teardown
793
+ end
794
+
795
+ def test1
796
+ end
797
+
798
+ def test2
799
+ end
800
+ end
801
+ end
802
+
803
+ def test_call_order
804
+ assert_equal([
805
+ :startup,
806
+ :setup, :teardown,
807
+ :setup, :teardown,
808
+ :shutdown,
809
+ ],
810
+ call_order(@test_case))
811
+ end
812
+ end
813
+
814
+ class TestInheritance < self
815
+ def setup
816
+ @original_descendants = TestCase::DESCENDANTS.dup
817
+ TestCase::DESCENDANTS.clear
818
+
819
+ @parent_test_case = Class.new(TestCase) do
820
+ extend CallLogger
821
+
822
+ class << self
823
+ def startup
824
+ called << :startup_parent
825
+ end
826
+
827
+ def shutdown
828
+ called << :shutdown_parent
829
+ end
830
+ end
831
+
832
+ def setup
833
+ self.class.called << :setup_parent
834
+ end
835
+
836
+ def teardown
837
+ self.class.called << :teardown_parent
838
+ end
839
+
840
+ def test1_parent
841
+ self.class.called << :test1_parent
842
+ end
843
+
844
+ def test2_parent
845
+ self.class.called << :test2_parent
846
+ end
847
+ end
848
+
849
+ @child_test_case = Class.new(@parent_test_case) do
850
+ class << self
851
+ def startup
852
+ called << :startup_child
853
+ end
854
+
855
+ def shutdown
856
+ called << :shutdown_child
857
+ end
858
+ end
859
+
860
+ def setup
861
+ self.class.called << :setup_child
862
+ end
863
+
864
+ def teardown
865
+ self.class.called << :teardown_child
866
+ end
867
+
868
+ def test1_child
869
+ self.class.called << :test1_child
870
+ end
871
+
872
+ def test2_child
873
+ self.class.called << :test2_child
874
+ end
875
+ end
876
+ end
877
+
878
+ def teardown
879
+ TestCase::DESCENDANTS.replace(@original_descendants)
880
+ end
881
+
882
+ def test_call_order
883
+ collector = Collector::Descendant.new
884
+ test_suite = collector.collect
885
+ test_suite.run(TestResult.new) {}
886
+ called = @parent_test_case.called
887
+ assert_equal([
888
+ :startup_parent,
889
+ :setup_parent, :test1_parent, :teardown_parent,
890
+ :setup_parent, :test2_parent, :teardown_parent,
891
+ :startup_child,
892
+ :setup_child, :test1_child, :teardown_child,
893
+ :setup_child, :test2_child, :teardown_child,
894
+ :shutdown_child,
895
+ :shutdown_parent,
896
+ ],
897
+ called)
898
+ end
899
+ end
900
+ end
901
+
902
+ class TestError < self
903
+ def run_test_case(test_case)
904
+ test_suite = test_case.suite
905
+ result = TestResult.new
906
+ test_suite.run(result) {}
907
+ result
908
+ end
909
+
910
+ def error_count(test_case)
911
+ run_test_case(test_case).error_count
912
+ end
913
+
914
+ def test_on_startup
915
+ test_case = Class.new(TestCase) do
916
+ class << self
917
+ def startup
918
+ raise "from startup"
919
+ end
920
+ end
921
+
922
+ def test_nothing
923
+ end
924
+ end
925
+
926
+ assert_equal(1, error_count(test_case))
927
+ end
928
+
929
+ def test_pass_through_on_startup
930
+ test_case = Class.new(TestCase) do
931
+ class << self
932
+ def startup
933
+ raise Interrupt, "from startup"
934
+ end
935
+ end
936
+
937
+ def test_nothing
938
+ end
939
+ end
940
+
941
+ assert_raise(Interrupt) do
942
+ run_test_case(test_case)
943
+ end
944
+ end
945
+
946
+ def test_on_shutdown
947
+ test_case = Class.new(TestCase) do
948
+ class << self
949
+ def shutdown
950
+ raise "from shutdown"
951
+ end
952
+ end
953
+
954
+ def test_nothing
955
+ end
956
+ end
957
+
958
+ assert_equal(1, error_count(test_case))
959
+ end
960
+
961
+ def test_pass_through_on_shutdown
962
+ test_case = Class.new(TestCase) do
963
+ class << self
964
+ def shutdown
965
+ raise Interrupt, "from shutdown"
966
+ end
967
+ end
968
+
969
+ def test_nothing
970
+ end
971
+ end
972
+
973
+ assert_raise(Interrupt) do
974
+ run_test_case(test_case)
975
+ end
976
+ end
977
+ end
978
+ end
742
979
  end
743
980
  end
744
981
  end