Small optimisations, by Brandon Black
[p5sagit/p5-mst-13.2.git] / t / mro / basic_02_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 => 10;
13
14=pod
15
16This example is take from: http://www.python.org/2.3/mro.html
17
18"My first example"
19class O: pass
20class F(O): pass
21class E(O): pass
22class D(O): pass
23class C(D,F): pass
24class B(D,E): pass
25class A(B,C): pass
26
27
28 6
29 ---
30Level 3 | O | (more general)
31 / --- \
32 / | \ |
33 / | \ |
34 / | \ |
35 --- --- --- |
36Level 2 3 | D | 4| E | | F | 5 |
37 --- --- --- |
38 \ \ _ / | |
39 \ / \ _ | |
40 \ / \ | |
41 --- --- |
42Level 1 1 | B | | C | 2 |
43 --- --- |
44 \ / |
45 \ / \ /
46 ---
47Level 0 0 | A | (more specialized)
48 ---
49
50=cut
51
52{
53 package Test::O;
54 use mro 'c3';
55
56 package Test::F;
57 use mro 'c3';
58 use base 'Test::O';
59
60 package Test::E;
61 use base 'Test::O';
62 use mro 'c3';
63
64 sub C_or_E { 'Test::E' }
65
66 package Test::D;
67 use mro 'c3';
68 use base 'Test::O';
69
70 sub C_or_D { 'Test::D' }
71
72 package Test::C;
73 use base ('Test::D', 'Test::F');
74 use mro 'c3';
75
76 sub C_or_D { 'Test::C' }
77 sub C_or_E { 'Test::C' }
78
79 package Test::B;
80 use mro 'c3';
81 use base ('Test::D', 'Test::E');
82
83 package Test::A;
84 use base ('Test::B', 'Test::C');
85 use mro 'c3';
86}
87
88is_deeply(
89 mro::get_linear_isa('Test::F'),
90 [ qw(Test::F Test::O) ],
91 '... got the right MRO for Test::F');
92
93is_deeply(
94 mro::get_linear_isa('Test::E'),
95 [ qw(Test::E Test::O) ],
96 '... got the right MRO for Test::E');
97
98is_deeply(
99 mro::get_linear_isa('Test::D'),
100 [ qw(Test::D Test::O) ],
101 '... got the right MRO for Test::D');
102
103is_deeply(
104 mro::get_linear_isa('Test::C'),
105 [ qw(Test::C Test::D Test::F Test::O) ],
106 '... got the right MRO for Test::C');
107
108is_deeply(
109 mro::get_linear_isa('Test::B'),
110 [ qw(Test::B Test::D Test::E Test::O) ],
111 '... got the right MRO for Test::B');
112
113is_deeply(
114 mro::get_linear_isa('Test::A'),
115 [ qw(Test::A Test::B Test::C Test::D Test::E Test::F Test::O) ],
116 '... got the right MRO for Test::A');
117
118is(Test::A->C_or_D, 'Test::C', '... got the expected method output');
119is(Test::A->can('C_or_D')->(), 'Test::C', '... can got the expected method output');
120is(Test::A->C_or_E, 'Test::C', '... got the expected method output');
121is(Test::A->can('C_or_E')->(), 'Test::C', '... can got the expected method output');