pre-0.02 checkin
[gitmo/Class-C3.git] / t / 02_MRO.t
CommitLineData
95bebf8c 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
d401eda1 6use Test::More tests => 11;
95bebf8c 7
8BEGIN {
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 17This example is take from: http://www.python.org/2.3/mro.html
18
19"My first example"
20class O: pass
21class F(O): pass
22class E(O): pass
23class D(O): pass
24class C(D,F): pass
25class B(D,E): pass
26class A(B,C): pass
27
28
95bebf8c 29 6
30 ---
31Level 3 | O | (more general)
32 / --- \
33 / | \ |
34 / | \ |
35 / | \ |
36 --- --- --- |
37Level 2 3 | D | 4| E | | F | 5 |
38 --- --- --- |
39 \ \ _ / | |
40 \ / \ _ | |
41 \ / \ | |
42 --- --- |
43Level 1 1 | B | | C | 2 |
44 --- --- |
45 \ / |
46 \ / \ /
47 ---
48Level 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
89is_deeply(
90 [ Class::C3::calculateMRO('Test::F') ],
91 [ qw(Test::F Test::O) ],
92 '... got the right MRO for Test::F');
93
94is_deeply(
95 [ Class::C3::calculateMRO('Test::E') ],
96 [ qw(Test::E Test::O) ],
97 '... got the right MRO for Test::E');
98
99is_deeply(
100 [ Class::C3::calculateMRO('Test::D') ],
101 [ qw(Test::D Test::O) ],
102 '... got the right MRO for Test::D');
103
104is_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
109is_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
114is_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
119is(Test::A->C_or_D, 'Test::C', '... got the expected method output');
120is(Test::A->can('C_or_D')->(), 'Test::C', '... can got the expected method output');
121
95bebf8c 122is(Test::A->C_or_E, 'Test::C', '... got the expected method output');
123is(Test::A->can('C_or_E')->(), 'Test::C', '... can got the expected method output');
124
95bebf8c 125