remove extraneous garbage from tests
[gitmo/Class-C3.git] / t / 02_MRO.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 14;
7
8 =pod
9
10 This example is take from: http://www.python.org/2.3/mro.html
11
12 "My first example"
13 class O: pass
14 class F(O): pass
15 class E(O): pass
16 class D(O): pass
17 class C(D,F): pass
18 class B(D,E): pass
19 class A(B,C): pass
20
21
22                           6
23                          ---
24 Level 3                 | O |                  (more general)
25                       /  ---  \
26                      /    |    \                      |
27                     /     |     \                     |
28                    /      |      \                    |
29                   ---    ---    ---                   |
30 Level 2        3 | D | 4| E |  | F | 5                |
31                   ---    ---    ---                   |
32                    \  \ _ /       |                   |
33                     \    / \ _    |                   |
34                      \  /      \  |                   |
35                       ---      ---                    |
36 Level 1            1 | B |    | C | 2                 |
37                       ---      ---                    |
38                         \      /                      |
39                          \    /                      \ /
40                            ---
41 Level 0                 0 | A |                (more specialized)
42                            ---
43
44 =cut
45
46 {
47     package Test::O;
48     use Class::C3; 
49     
50     package Test::F;   
51     use Class::C3;  
52     use base 'Test::O';        
53     
54     package Test::E;
55     use base 'Test::O';    
56     use Class::C3;     
57     
58     sub C_or_E { 'Test::E' }
59
60     package Test::D;
61     use Class::C3; 
62     use base 'Test::O';     
63     
64     sub C_or_D { 'Test::D' }       
65       
66     package Test::C;
67     use base ('Test::D', 'Test::F');
68     use Class::C3; 
69     
70     sub C_or_D { 'Test::C' }
71     sub C_or_E { 'Test::C' }    
72         
73     package Test::B;    
74     use Class::C3; 
75     use base ('Test::D', 'Test::E');    
76         
77     package Test::A;    
78     use base ('Test::B', 'Test::C');
79     use Class::C3;    
80 }
81
82 Class::C3::initialize();
83
84 is_deeply(
85     [ Class::C3::calculateMRO('Test::F') ],
86     [ qw(Test::F Test::O) ],
87     '... got the right MRO for Test::F');
88
89 is_deeply(
90     [ Class::C3::calculateMRO('Test::E') ],
91     [ qw(Test::E Test::O) ],
92     '... got the right MRO for Test::E');    
93
94 is_deeply(
95     [ Class::C3::calculateMRO('Test::D') ],
96     [ qw(Test::D Test::O) ],
97     '... got the right MRO for Test::D');       
98
99 is_deeply(
100     [ Class::C3::calculateMRO('Test::C') ],
101     [ qw(Test::C Test::D Test::F Test::O) ],
102     '... got the right MRO for Test::C'); 
103
104 is_deeply(
105     [ Class::C3::calculateMRO('Test::B') ],
106     [ qw(Test::B Test::D Test::E Test::O) ],
107     '... got the right MRO for Test::B');     
108
109 is_deeply(
110     [ Class::C3::calculateMRO('Test::A') ],
111     [ qw(Test::A Test::B Test::C Test::D Test::E Test::F Test::O) ],
112     '... got the right MRO for Test::A');  
113     
114 is(Test::A->C_or_D, 'Test::C', '... got the expected method output');
115 is(Test::A->can('C_or_D')->(), 'Test::C', '... can got the expected method output');
116
117 is(Test::A->C_or_E, 'Test::C', '... got the expected method output');
118 is(Test::A->can('C_or_E')->(), 'Test::C', '... can got the expected method output');
119
120 # remove the C3
121 Class::C3::uninitialize();
122
123 is(Test::A->C_or_D, 'Test::D', '...  old method resolution has been restored');
124 is(Test::A->can('C_or_D')->(), 'Test::D', '...  old can(method) resolution has been restored');
125
126 is(Test::A->C_or_E, 'Test::E', '...  old method resolution has been restored');
127 is(Test::A->can('C_or_E')->(), 'Test::E', '...  old can(method) resolution has been restored');
128
129