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