This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
regcomp.c: Make all warnings and error messages UTF-8 clean
[perl5.git] / t / mro / basic_03_c3.t
1 #!./perl
2
3 use strict;
4 use warnings;
5
6 require q(./test.pl); plan(tests => 4);
7
8 =pod
9
10 This example is take from: http://www.python.org/2.3/mro.html
11
12 "My second example"
13 class O: pass
14 class F(O): pass
15 class E(O): pass
16 class D(O): pass
17 class C(D,F): pass
18 class B(E,D): pass
19 class A(B,C): pass
20
21                            6
22                           ---
23 Level 3                  | O |
24                        /  ---  \
25                       /    |    \
26                      /     |     \
27                     /      |      \
28                   ---     ---    ---
29 Level 2        2 | E | 4 | D |  | F | 5
30                   ---     ---    ---
31                    \      / \     /
32                     \    /   \   /
33                      \  /     \ /
34                       ---     ---
35 Level 1            1 | B |   | C | 3
36                       ---     ---
37                        \       /
38                         \     /
39                           ---
40 Level 0                0 | A |
41                           ---
42
43 >>> A.mro()
44 (<class '__main__.A'>, <class '__main__.B'>, <class '__main__.E'>,
45 <class '__main__.C'>, <class '__main__.D'>, <class '__main__.F'>,
46 <type 'object'>)
47
48 =cut
49
50 {
51     package Test::O;
52     use mro 'c3';
53     
54     sub O_or_D { 'Test::O' }
55     sub O_or_F { 'Test::O' }    
56     
57     package Test::F;
58     use base 'Test::O';
59     use mro 'c3';
60     
61     sub O_or_F { 'Test::F' }    
62     
63     package Test::E;
64     use base 'Test::O';
65     use mro 'c3';
66         
67     package Test::D;
68     use base 'Test::O';    
69     use mro 'c3';
70     
71     sub O_or_D { 'Test::D' }
72     sub C_or_D { 'Test::D' }
73         
74     package Test::C;
75     use base ('Test::D', 'Test::F');
76     use mro 'c3';    
77
78     sub C_or_D { 'Test::C' }
79     
80     package Test::B;
81     use base ('Test::E', 'Test::D');
82     use mro 'c3';
83         
84     package Test::A;
85     use base ('Test::B', 'Test::C');
86     use mro 'c3';
87 }
88
89 ok(eq_array(
90     mro::get_linear_isa('Test::A'),
91     [ qw(Test::A Test::B Test::E Test::C Test::D Test::F Test::O) ]
92 ), '... got the right MRO for Test::A');      
93     
94 is(Test::A->O_or_D, 'Test::D', '... got the right method dispatch');    
95 is(Test::A->O_or_F, 'Test::F', '... got the right method dispatch');   
96
97 # NOTE: 
98 # this test is particularly interesting because the p5 dispatch
99 # would actually call Test::D before Test::C and Test::D is a
100 # subclass of Test::C 
101 is(Test::A->C_or_D, 'Test::C', '... got the right method dispatch');