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