verison 0.01 of Class-C3
[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 => 15;
7
8 BEGIN {
9     use_ok('Class::C3');
10 }
11
12 =pod
13
14                           6
15                          ---
16 Level 3                 | O |                  (more general)
17                       /  ---  \
18                      /    |    \                      |
19                     /     |     \                     |
20                    /      |      \                    |
21                   ---    ---    ---                   |
22 Level 2        3 | D | 4| E |  | F | 5                |
23                   ---    ---    ---                   |
24                    \  \ _ /       |                   |
25                     \    / \ _    |                   |
26                      \  /      \  |                   |
27                       ---      ---                    |
28 Level 1            1 | B |    | C | 2                 |
29                       ---      ---                    |
30                         \      /                      |
31                          \    /                      \ /
32                            ---
33 Level 0                 0 | A |                (more specialized)
34                            ---
35
36 =cut
37
38 {
39     package Test::O;
40     use Class::C3; 
41     
42     package Test::F;   
43     use Class::C3;  
44     use base 'Test::O';        
45     
46     package Test::E;
47     use base 'Test::O';    
48     use Class::C3;     
49     
50     sub C_or_E { 'Test::E' }
51
52     package Test::D;
53     use Class::C3; 
54     use base 'Test::O';     
55     
56     sub C_or_D { 'Test::D' }       
57       
58     package Test::C;
59     use base ('Test::D', 'Test::F');
60     use Class::C3; 
61     
62     sub C_or_D { 'Test::C' }
63     sub C_or_E { 'Test::C' }    
64         
65     package Test::B;    
66     use Class::C3; 
67     use base ('Test::D', 'Test::E');    
68         
69     package Test::A;    
70     use base ('Test::B', 'Test::C');
71     use Class::C3;    
72 }
73
74 is_deeply(
75     [ Class::C3::calculateMRO('Test::F') ],
76     [ qw(Test::F Test::O) ],
77     '... got the right MRO for Test::F');
78
79 is_deeply(
80     [ Class::C3::calculateMRO('Test::E') ],
81     [ qw(Test::E Test::O) ],
82     '... got the right MRO for Test::E');    
83
84 is_deeply(
85     [ Class::C3::calculateMRO('Test::D') ],
86     [ qw(Test::D Test::O) ],
87     '... got the right MRO for Test::D');       
88
89 is_deeply(
90     [ Class::C3::calculateMRO('Test::C') ],
91     [ qw(Test::C Test::D Test::F Test::O) ],
92     '... got the right MRO for Test::C'); 
93
94 is_deeply(
95     [ Class::C3::calculateMRO('Test::B') ],
96     [ qw(Test::B Test::D Test::E Test::O) ],
97     '... got the right MRO for Test::B');     
98
99 is_deeply(
100     [ Class::C3::calculateMRO('Test::A') ],
101     [ qw(Test::A Test::B Test::C Test::D Test::E Test::F Test::O) ],
102     '... got the right MRO for Test::A');  
103     
104 is(Test::A->C_or_D, 'Test::C', '... got the expected method output');
105 is(Test::A->can('C_or_D')->(), 'Test::C', '... can got the expected method output');
106
107 is(Test::B->C_or_D, 'Test::D', '... got the expected method output');
108 is(Test::B->can('C_or_D')->(), 'Test::D', '... can got the expected method output');
109
110 is(Test::A->C_or_E, 'Test::C', '... got the expected method output');
111 is(Test::A->can('C_or_E')->(), 'Test::C', '... can got the expected method output');
112
113 is(Test::B->C_or_E, 'Test::E', '... got the expected method output');
114 is(Test::B->can('C_or_E')->(), 'Test::E', '... can got the expected method output');
115
116