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