Merge branch 'stable'
[gitmo/Class-MOP.git] / t / 002_class_precedence_list.t
CommitLineData
0882828e 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
0882828e 5
efd3d14c 6use Class::MOP;
7use Class::MOP::Class;
0882828e 8
9=pod
10
11 A
12 / \
13B C
14 \ /
15 D
16
17=cut
18
19{
20 package My::A;
aa448b16 21 use metaclass;
0882828e 22 package My::B;
23 our @ISA = ('My::A');
24 package My::C;
86a4d873 25 our @ISA = ('My::A');
26 package My::D;
27 our @ISA = ('My::B', 'My::C');
0882828e 28}
29
30is_deeply(
86a4d873 31 [ My::D->meta->class_precedence_list ],
32 [ 'My::D', 'My::B', 'My::A', 'My::C', 'My::A' ],
0882828e 33 '... My::D->meta->class_precedence_list == (D B A C A)');
34
b7bdffc3 35is_deeply(
86a4d873 36 [ My::D->meta->linearized_isa ],
37 [ 'My::D', 'My::B', 'My::A', 'My::C' ],
b7bdffc3 38 '... My::D->meta->linearized_isa == (D B A C)');
39
0882828e 40=pod
41
bfe4d0fc 42 A <-+
43 | |
44 B |
45 | |
46 C --+
0882828e 47
48=cut
49
21af8dc9 50# 5.9.5+ dies at the moment of
51# recursive @ISA definition, not later when
52# you try to use the @ISAs.
53eval {
54 {
55 package My::2::A;
86a4d873 56 use metaclass;
21af8dc9 57 our @ISA = ('My::2::C');
86a4d873 58
21af8dc9 59 package My::2::B;
60 our @ISA = ('My::2::A');
86a4d873 61
21af8dc9 62 package My::2::C;
86a4d873 63 our @ISA = ('My::2::B');
21af8dc9 64 }
0882828e 65
21af8dc9 66 My::2::B->meta->class_precedence_list
67};
0882828e 68ok($@, '... recursive inheritance breaks correctly :)');
69
70=pod
71
72 +--------+
73 | A |
74 | / \ |
75 +->B C-+
76 \ /
77 D
78
79=cut
80
81{
82 package My::3::A;
86a4d873 83 use metaclass;
0882828e 84 package My::3::B;
85 our @ISA = ('My::3::A');
86 package My::3::C;
86a4d873 87 our @ISA = ('My::3::A', 'My::3::B');
88 package My::3::D;
89 our @ISA = ('My::3::B', 'My::3::C');
0882828e 90}
91
92is_deeply(
86a4d873 93 [ My::3::D->meta->class_precedence_list ],
94 [ 'My::3::D', 'My::3::B', 'My::3::A', 'My::3::C', 'My::3::A', 'My::3::B', 'My::3::A' ],
0882828e 95 '... My::3::D->meta->class_precedence_list == (D B A C A B A)');
fe122940 96
b7bdffc3 97is_deeply(
86a4d873 98 [ My::3::D->meta->linearized_isa ],
99 [ 'My::3::D', 'My::3::B', 'My::3::A', 'My::3::C' ],
b7bdffc3 100 '... My::3::D->meta->linearized_isa == (D B A C B)');
101
fe122940 102=pod
103
104Test all the class_precedence_lists
105using Perl's own dispatcher to check
106against.
107
108=cut
109
110my @CLASS_PRECEDENCE_LIST;
111
112{
113 package Foo;
86a4d873 114 use metaclass;
115
116 sub CPL { push @CLASS_PRECEDENCE_LIST => 'Foo' }
117
fe122940 118 package Bar;
119 our @ISA = ('Foo');
86a4d873 120
121 sub CPL {
fe122940 122 push @CLASS_PRECEDENCE_LIST => 'Bar';
123 $_[0]->SUPER::CPL();
86a4d873 124 }
125
fe122940 126 package Baz;
86a4d873 127 use metaclass;
fe122940 128 our @ISA = ('Bar');
86a4d873 129
130 sub CPL {
fe122940 131 push @CLASS_PRECEDENCE_LIST => 'Baz';
132 $_[0]->SUPER::CPL();
86a4d873 133 }
134
fe122940 135 package Foo::Bar;
136 our @ISA = ('Baz');
86a4d873 137
138 sub CPL {
fe122940 139 push @CLASS_PRECEDENCE_LIST => 'Foo::Bar';
140 $_[0]->SUPER::CPL();
86a4d873 141 }
142
fe122940 143 package Foo::Bar::Baz;
144 our @ISA = ('Foo::Bar');
86a4d873 145
146 sub CPL {
fe122940 147 push @CLASS_PRECEDENCE_LIST => 'Foo::Bar::Baz';
148 $_[0]->SUPER::CPL();
86a4d873 149 }
fe122940 150
151}
152
153Foo::Bar::Baz->CPL();
154
155is_deeply(
86a4d873 156 [ Foo::Bar::Baz->meta->class_precedence_list ],
157 [ @CLASS_PRECEDENCE_LIST ],
fe122940 158 '... Foo::Bar::Baz->meta->class_precedence_list == @CLASS_PRECEDENCE_LIST');
159
86a4d873 160done_testing;