Commit | Line | Data |
a5eca695 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
1a7ebbb3 |
6 | use Test::More tests => 9; |
a5eca695 |
7 | use Test::Exception; |
8 | |
9 | BEGIN { |
10 | use_ok('Class::MOP'); |
11 | use_ok('Class::MOP::Class'); |
12 | } |
13 | |
14 | =pod |
15 | |
16 | The following class hierarhcy is very contrived |
17 | and totally horrid (it won't work under C3 even), |
18 | but it tests a number of aspect of this module. |
19 | |
20 | A more real-world example would be a nice addition :) |
21 | |
22 | =cut |
23 | |
24 | { |
25 | package Foo; |
26 | |
27 | sub BUILD { 'Foo::BUILD' } |
28 | sub foo { 'Foo::foo' } |
29 | |
30 | package Bar; |
31 | our @ISA = ('Foo'); |
32 | |
33 | sub BUILD { 'Bar::BUILD' } |
34 | sub bar { 'Bar::bar' } |
35 | |
36 | package Baz; |
37 | our @ISA = ('Bar'); |
38 | |
a5eca695 |
39 | sub baz { 'Baz::baz' } |
40 | sub foo { 'Baz::foo' } |
41 | |
42 | package Foo::Bar; |
43 | our @ISA = ('Foo', 'Bar'); |
44 | |
45 | sub BUILD { 'Foo::Bar::BUILD' } |
46 | sub foobar { 'Foo::Bar::foobar' } |
47 | |
48 | package Foo::Bar::Baz; |
49 | our @ISA = ('Foo', 'Bar', 'Baz'); |
50 | |
51 | sub BUILD { 'Foo::Bar::Baz::BUILD' } |
52 | sub bar { 'Foo::Bar::Baz::bar' } |
53 | sub foobarbaz { 'Foo::Bar::Baz::foobarbaz' } |
54 | } |
55 | |
56 | is_deeply( |
57 | [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo')->compute_all_applicable_methods() ], |
58 | [ |
59 | { |
60 | name => 'BUILD', |
61 | class => 'Foo', |
62 | code => \&Foo::BUILD |
63 | }, |
64 | { |
65 | name => 'foo', |
66 | class => 'Foo', |
67 | code => \&Foo::foo |
68 | }, |
69 | ], |
70 | '... got the right list of applicable methods for Foo'); |
71 | |
72 | is_deeply( |
73 | [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Bar')->compute_all_applicable_methods() ], |
74 | [ |
75 | { |
76 | name => 'BUILD', |
77 | class => 'Bar', |
78 | code => \&Bar::BUILD |
79 | }, |
80 | { |
81 | name => 'bar', |
82 | class => 'Bar', |
83 | code => \&Bar::bar |
84 | }, |
85 | { |
86 | name => 'foo', |
87 | class => 'Foo', |
88 | code => \&Foo::foo |
89 | }, |
90 | ], |
91 | '... got the right list of applicable methods for Bar'); |
92 | |
93 | |
94 | is_deeply( |
95 | [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Baz')->compute_all_applicable_methods() ], |
22286063 |
96 | [ |
a5eca695 |
97 | { |
98 | name => 'BUILD', |
22286063 |
99 | class => 'Bar', |
100 | code => \&Bar::BUILD |
a5eca695 |
101 | }, |
102 | { |
103 | name => 'bar', |
104 | class => 'Bar', |
105 | code => \&Bar::bar |
106 | }, |
107 | { |
108 | name => 'baz', |
109 | class => 'Baz', |
110 | code => \&Baz::baz |
111 | }, |
112 | { |
113 | name => 'foo', |
114 | class => 'Baz', |
115 | code => \&Baz::foo |
116 | }, |
117 | ], |
118 | '... got the right list of applicable methods for Baz'); |
119 | |
120 | is_deeply( |
121 | [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo::Bar')->compute_all_applicable_methods() ], |
122 | [ |
123 | { |
124 | name => 'BUILD', |
125 | class => 'Foo::Bar', |
126 | code => \&Foo::Bar::BUILD |
127 | }, |
128 | { |
129 | name => 'bar', |
130 | class => 'Bar', |
131 | code => \&Bar::bar |
132 | }, |
133 | { |
134 | name => 'foo', |
135 | class => 'Foo', |
136 | code => \&Foo::foo |
137 | }, |
138 | { |
139 | name => 'foobar', |
140 | class => 'Foo::Bar', |
141 | code => \&Foo::Bar::foobar |
142 | }, |
143 | ], |
144 | '... got the right list of applicable methods for Foo::Bar'); |
145 | |
146 | is_deeply( |
147 | [ sort { $a->{name} cmp $b->{name} } Class::MOP::Class->initialize('Foo::Bar::Baz')->compute_all_applicable_methods() ], |
148 | [ |
149 | { |
150 | name => 'BUILD', |
151 | class => 'Foo::Bar::Baz', |
152 | code => \&Foo::Bar::Baz::BUILD |
153 | }, |
154 | { |
155 | name => 'bar', |
156 | class => 'Foo::Bar::Baz', |
157 | code => \&Foo::Bar::Baz::bar |
158 | }, |
159 | { |
160 | name => 'baz', |
161 | class => 'Baz', |
162 | code => \&Baz::baz |
163 | }, |
164 | { |
165 | name => 'foo', |
166 | class => 'Foo', |
167 | code => \&Foo::foo |
168 | }, |
169 | { |
170 | name => 'foobarbaz', |
171 | class => 'Foo::Bar::Baz', |
172 | code => \&Foo::Bar::Baz::foobarbaz |
173 | }, |
174 | ], |
175 | '... got the right list of applicable methods for Foo::Bar::Baz'); |
176 | |
177 | ## find_all_methods_by_name |
178 | |
179 | is_deeply( |
180 | [ Class::MOP::Class->initialize('Foo::Bar')->find_all_methods_by_name('BUILD') ], |
181 | [ |
182 | { |
183 | name => 'BUILD', |
184 | class => 'Foo::Bar', |
185 | code => \&Foo::Bar::BUILD |
186 | }, |
187 | { |
188 | name => 'BUILD', |
189 | class => 'Foo', |
190 | code => \&Foo::BUILD |
191 | }, |
192 | { |
193 | name => 'BUILD', |
194 | class => 'Bar', |
195 | code => \&Bar::BUILD |
196 | } |
197 | ], |
198 | '... got the right list of BUILD methods for Foo::Bar'); |
199 | |
200 | is_deeply( |
201 | [ Class::MOP::Class->initialize('Foo::Bar::Baz')->find_all_methods_by_name('BUILD') ], |
202 | [ |
203 | { |
204 | name => 'BUILD', |
205 | class => 'Foo::Bar::Baz', |
206 | code => \&Foo::Bar::Baz::BUILD |
207 | }, |
208 | { |
209 | name => 'BUILD', |
210 | class => 'Foo', |
211 | code => \&Foo::BUILD |
212 | }, |
213 | { |
214 | name => 'BUILD', |
215 | class => 'Bar', |
216 | code => \&Bar::BUILD |
22286063 |
217 | }, |
a5eca695 |
218 | ], |
219 | '... got the right list of BUILD methods for Foo::Bar::Baz'); |