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