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