remove extraneous garbage from tests
[gitmo/Class-C3.git] / t / 03_MRO.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 7;
7
8 =pod
9
10
11 This example is take from: http://www.python.org/2.3/mro.html
12
13 "My second example"
14 class O: pass
15 class F(O): pass
16 class E(O): pass
17 class D(O): pass
18 class C(D,F): pass
19 class B(E,D): pass
20 class A(B,C): pass
21
22                            6
23                           ---
24 Level 3                  | O |
25                        /  ---  \
26                       /    |    \
27                      /     |     \
28                     /      |      \
29                   ---     ---    ---
30 Level 2        2 | E | 4 | D |  | F | 5
31                   ---     ---    ---
32                    \      / \     /
33                     \    /   \   /
34                      \  /     \ /
35                       ---     ---
36 Level 1            1 | B |   | C | 3
37                       ---     ---
38                        \       /
39                         \     /
40                           ---
41 Level 0                0 | A |
42                           ---
43
44 >>> A.mro()
45 (<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>,
46 <class '__main__.C'>, <class '__main__.D'>, <class '__main__.F'>,
47 <type 'object'>)
48
49 =cut
50
51 {
52     package Test::O;
53     use Class::C3;
54     
55     sub O_or_D { 'Test::O' }
56     sub O_or_F { 'Test::O' }    
57     
58     package Test::F;
59     use base 'Test::O';
60     use Class::C3;
61     
62     sub O_or_F { 'Test::F' }    
63     
64     package Test::E;
65     use base 'Test::O';
66     use Class::C3;
67         
68     package Test::D;
69     use base 'Test::O';    
70     use Class::C3;
71     
72     sub O_or_D { 'Test::D' }
73     sub C_or_D { 'Test::D' }
74         
75     package Test::C;
76     use base ('Test::D', 'Test::F');
77     use Class::C3;    
78
79     sub C_or_D { 'Test::C' }
80     
81     package Test::B;
82     use base ('Test::E', 'Test::D');
83     use Class::C3;
84         
85     package Test::A;
86     use base ('Test::B', 'Test::C');
87     use Class::C3;
88 }
89
90 Class::C3::initialize();
91
92 is_deeply(
93     [ Class::C3::calculateMRO('Test::A') ],
94     [ qw(Test::A Test::B Test::E Test::C Test::D Test::F Test::O) ],
95     '... got the right MRO for Test::A');      
96     
97 is(Test::A->O_or_D, 'Test::D', '... got the right method dispatch');    
98 is(Test::A->O_or_F, 'Test::F', '... got the right method dispatch');   
99
100 # NOTE: 
101 # this test is particularly interesting because the p5 dispatch
102 # would actually call Test::D before Test::C and Test::D is a
103 # subclass of Test::C 
104 is(Test::A->C_or_D, 'Test::C', '... got the right method dispatch');    
105
106 Class::C3::uninitialize();
107
108 is(Test::A->O_or_D, 'Test::O', '... old dispatch order is restored');    
109 is(Test::A->O_or_F, 'Test::O', '... old dispatch order is restored');   
110 is(Test::A->C_or_D, 'Test::D', '... old dispatch order is restored');