adding the some preliminary junk
[gitmo/Class-C3-XS.git] / t / 03_MRO.t
CommitLineData
8995e827 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More tests => 8;
7
8BEGIN {
9 use_ok('Class::C3');
10 # uncomment this line, and re-run the
11 # test to see the normal p5 dispatch order
12 #$Class::C3::TURN_OFF_C3 = 1;
13}
14
15=pod
16
17
18This example is take from: http://www.python.org/2.3/mro.html
19
20"My second example"
21class O: pass
22class F(O): pass
23class E(O): pass
24class D(O): pass
25class C(D,F): pass
26class B(E,D): pass
27class A(B,C): pass
28
29 6
30 ---
31Level 3 | O |
32 / --- \
33 / | \
34 / | \
35 / | \
36 --- --- ---
37Level 2 2 | E | 4 | D | | F | 5
38 --- --- ---
39 \ / \ /
40 \ / \ /
41 \ / \ /
42 --- ---
43Level 1 1 | B | | C | 3
44 --- ---
45 \ /
46 \ /
47 ---
48Level 0 0 | A |
49 ---
50
51>>> A.mro()
52(<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>,
53<class '__main__.C'>, <class '__main__.D'>, <class '__main__.F'>,
54<type 'object'>)
55
56=cut
57
58{
59 package Test::O;
60 use Class::C3;
61
62 sub O_or_D { 'Test::O' }
63 sub O_or_F { 'Test::O' }
64
65 package Test::F;
66 use base 'Test::O';
67 use Class::C3;
68
69 sub O_or_F { 'Test::F' }
70
71 package Test::E;
72 use base 'Test::O';
73 use Class::C3;
74
75 package Test::D;
76 use base 'Test::O';
77 use Class::C3;
78
79 sub O_or_D { 'Test::D' }
80 sub C_or_D { 'Test::D' }
81
82 package Test::C;
83 use base ('Test::D', 'Test::F');
84 use Class::C3;
85
86 sub C_or_D { 'Test::C' }
87
88 package Test::B;
89 use base ('Test::E', 'Test::D');
90 use Class::C3;
91
92 package Test::A;
93 use base ('Test::B', 'Test::C');
94 use Class::C3;
95}
96
97Class::C3::initialize();
98
99is_deeply(
100 [ Class::C3::calculateMRO('Test::A') ],
101 [ qw(Test::A Test::B Test::E Test::C Test::D Test::F Test::O) ],
102 '... got the right MRO for Test::A');
103
104is(Test::A->O_or_D, 'Test::D', '... got the right method dispatch');
105is(Test::A->O_or_F, 'Test::F', '... got the right method dispatch');
106
107# NOTE:
108# this test is particularly interesting because the p5 dispatch
109# would actually call Test::D before Test::C and Test::D is a
110# subclass of Test::C
111is(Test::A->C_or_D, 'Test::C', '... got the right method dispatch');
112
113Class::C3::uninitialize();
114
115is(Test::A->O_or_D, 'Test::O', '... old dispatch order is restored');
116is(Test::A->O_or_F, 'Test::O', '... old dispatch order is restored');
117is(Test::A->C_or_D, 'Test::D', '... old dispatch order is restored');