Commit | Line | Data |
a5eca695 |
1 | use strict; |
2 | use warnings; |
3 | |
86a4d873 |
4 | use Test::More; |
a5eca695 |
5 | use Test::Exception; |
6 | |
efd3d14c |
7 | use Class::MOP; |
8 | use Class::MOP::Class; |
9 | use Class::MOP::Package; |
10 | use Class::MOP::Module; |
a5eca695 |
11 | |
013b1897 |
12 | { |
13 | my $class = Class::MOP::Class->initialize('Foo'); |
14 | is($class->meta, Class::MOP::Class->meta, '... instance and class both lead to the same meta'); |
15 | } |
16 | |
6d5355c3 |
17 | my $class_mop_class_meta = Class::MOP::Class->meta(); |
18 | isa_ok($class_mop_class_meta, 'Class::MOP::Class'); |
a5eca695 |
19 | |
6d5355c3 |
20 | my $class_mop_package_meta = Class::MOP::Package->meta(); |
21 | isa_ok($class_mop_package_meta, 'Class::MOP::Package'); |
22 | |
23 | my $class_mop_module_meta = Class::MOP::Module->meta(); |
24 | isa_ok($class_mop_module_meta, 'Class::MOP::Module'); |
25 | |
26 | my @class_mop_package_methods = qw( |
6c2f6b5c |
27 | _new |
6d5355c3 |
28 | |
a19fcb5b |
29 | initialize reinitialize |
6d5355c3 |
30 | |
31 | name |
a5e51f0b |
32 | namespace |
0ac992ee |
33 | |
34 | add_package_symbol get_package_symbol has_package_symbol remove_package_symbol |
ae234dc6 |
35 | list_all_package_symbols get_all_package_symbols remove_package_glob |
0ac992ee |
36 | |
a5e51f0b |
37 | _deconstruct_variable_name |
b409c969 |
38 | |
39 | get_method_map |
6d5355c3 |
40 | ); |
41 | |
42 | my @class_mop_module_methods = qw( |
6c2f6b5c |
43 | _new |
7f436b8c |
44 | |
89f66023 |
45 | _instantiate_module |
46 | |
4edd0667 |
47 | version authority identifier create |
6d5355c3 |
48 | ); |
49 | |
50 | my @class_mop_class_methods = qw( |
6c2f6b5c |
51 | _new |
0ac992ee |
52 | |
cc67d48a |
53 | is_pristine |
54 | |
a19fcb5b |
55 | initialize create |
86a4d873 |
56 | |
715adbb7 |
57 | update_package_cache_flag |
e0e4674a |
58 | reset_package_cache_flag |
0ac992ee |
59 | |
b9d9fc0b |
60 | create_anon_class is_anon_class |
0ac992ee |
61 | |
27e95ba5 |
62 | instance_metaclass get_meta_instance |
63 | create_meta_instance _create_meta_instance |
7b31baf4 |
64 | new_object clone_object |
d69fb6b3 |
65 | construct_instance _construct_instance |
3c0ad733 |
66 | construct_class_instance _construct_class_instance |
d379f552 |
67 | clone_instance _clone_instance |
0708313d |
68 | rebless_instance rebless_instance_back rebless_instance_away |
11a8a2c1 |
69 | check_metaclass_compatibility _check_metaclass_compatibility |
4920ae3d |
70 | _check_class_metaclass_compatibility _check_single_metaclass_compatibility |
71 | _fix_metaclass_incompatibility _fix_class_metaclass_incompatibility |
72 | _fix_single_metaclass_incompatibility _base_metaclasses |
73 | _can_fix_class_metaclass_incompatibility_by_subclassing |
74 | _can_fix_single_metaclass_incompatibility_by_subclassing |
75 | _can_fix_metaclass_incompatibility_by_subclassing |
76 | _can_fix_metaclass_incompatibility |
0ac992ee |
77 | |
c3f935e1 |
78 | add_meta_instance_dependencies remove_meta_instance_dependencies update_meta_instance_dependencies |
da5680be |
79 | add_dependent_meta_instance remove_dependent_meta_instance |
caab0eaa |
80 | invalidate_meta_instances invalidate_meta_instance |
81 | |
462ed2c0 |
82 | superclasses subclasses direct_subclasses class_precedence_list |
b53f14fa |
83 | linearized_isa _superclasses_updated |
0ac992ee |
84 | |
b1ff395f |
85 | alias_method get_all_method_names get_all_methods compute_all_applicable_methods |
0ac992ee |
86 | find_method_by_name find_all_methods_by_name find_next_method_by_name |
87 | |
88 | add_before_method_modifier add_after_method_modifier add_around_method_modifier |
ee5e71d4 |
89 | |
9b871d79 |
90 | _attach_attribute |
91 | _post_add_attribute |
92 | remove_attribute |
93 | find_attribute_by_name |
94 | get_all_attributes |
b1aaf0dc |
95 | |
9b871d79 |
96 | compute_all_applicable_attributes |
b1aaf0dc |
97 | get_attribute_map |
0ac992ee |
98 | |
44d6ea77 |
99 | is_mutable is_immutable make_mutable make_immutable |
f5d08022 |
100 | _initialize_immutable _install_inlined_code _inlined_methods |
101 | _add_inlined_method _inline_accessors _inline_constructor |
102 | _inline_destructor _immutable_options _rebless_as_immutable |
103 | _rebless_as_mutable _remove_inlined_code |
104 | |
e1c7a1b7 |
105 | _immutable_metaclass |
28443f51 |
106 | immutable_trait immutable_options |
107 | constructor_name constructor_class destructor_class |
f5d08022 |
108 | |
77e5fce4 |
109 | DESTROY |
7f436b8c |
110 | ); |
0ac992ee |
111 | |
112 | # check the class ... |
113 | |
c0ae440b |
114 | is_deeply([ sort $class_mop_class_meta->get_method_list ], [ sort @class_mop_class_methods ], '... got the correct method list for class'); |
7b31baf4 |
115 | |
0586121e |
116 | foreach my $method_name (sort @class_mop_class_methods) { |
6d5355c3 |
117 | ok($class_mop_class_meta->has_method($method_name), '... Class::MOP::Class->has_method(' . $method_name . ')'); |
a5eca695 |
118 | { |
119 | no strict 'refs'; |
0ac992ee |
120 | is($class_mop_class_meta->get_method($method_name)->body, |
a5eca695 |
121 | \&{'Class::MOP::Class::' . $method_name}, |
0ac992ee |
122 | '... Class::MOP::Class->get_method(' . $method_name . ') == &Class::MOP::Class::' . $method_name); |
a5eca695 |
123 | } |
124 | } |
125 | |
6d5355c3 |
126 | ## check the package .... |
127 | |
c0ae440b |
128 | is_deeply([ sort $class_mop_package_meta->get_method_list ], [ sort @class_mop_package_methods ], '... got the correct method list for package'); |
6d5355c3 |
129 | |
0586121e |
130 | foreach my $method_name (sort @class_mop_package_methods) { |
6d5355c3 |
131 | ok($class_mop_package_meta->has_method($method_name), '... Class::MOP::Package->has_method(' . $method_name . ')'); |
132 | { |
133 | no strict 'refs'; |
0ac992ee |
134 | is($class_mop_package_meta->get_method($method_name)->body, |
6d5355c3 |
135 | \&{'Class::MOP::Package::' . $method_name}, |
0ac992ee |
136 | '... Class::MOP::Package->get_method(' . $method_name . ') == &Class::MOP::Package::' . $method_name); |
6d5355c3 |
137 | } |
138 | } |
139 | |
7f436b8c |
140 | ## check the module .... |
141 | |
c0ae440b |
142 | is_deeply([ sort $class_mop_module_meta->get_method_list ], [ sort @class_mop_module_methods ], '... got the correct method list for module'); |
7f436b8c |
143 | |
0586121e |
144 | foreach my $method_name (sort @class_mop_module_methods) { |
7f436b8c |
145 | ok($class_mop_module_meta->has_method($method_name), '... Class::MOP::Module->has_method(' . $method_name . ')'); |
146 | { |
147 | no strict 'refs'; |
0ac992ee |
148 | is($class_mop_module_meta->get_method($method_name)->body, |
7f436b8c |
149 | \&{'Class::MOP::Module::' . $method_name}, |
0ac992ee |
150 | '... Class::MOP::Module->get_method(' . $method_name . ') == &Class::MOP::Module::' . $method_name); |
7f436b8c |
151 | } |
152 | } |
153 | |
154 | |
7b31baf4 |
155 | # check for imported functions which are not methods |
156 | |
2eb717d5 |
157 | foreach my $non_method_name (qw( |
158 | confess |
9b522fc4 |
159 | blessed |
2eb717d5 |
160 | subname |
161 | svref_2object |
162 | )) { |
0ac992ee |
163 | ok(!$class_mop_class_meta->has_method($non_method_name), '... NOT Class::MOP::Class->has_method(' . $non_method_name . ')'); |
2eb717d5 |
164 | } |
165 | |
7b31baf4 |
166 | # check for the right attributes |
167 | |
6d5355c3 |
168 | my @class_mop_package_attributes = ( |
8683db0e |
169 | 'package', |
170 | 'namespace', |
6d5355c3 |
171 | ); |
172 | |
173 | my @class_mop_module_attributes = ( |
8683db0e |
174 | 'version', |
175 | 'authority' |
6d5355c3 |
176 | ); |
177 | |
178 | my @class_mop_class_attributes = ( |
8683db0e |
179 | 'superclasses', |
44d6ea77 |
180 | 'instance_metaclass', |
f5d08022 |
181 | 'immutable_trait', |
182 | 'constructor_name', |
183 | 'constructor_class', |
184 | 'destructor_class', |
2bab2be6 |
185 | ); |
7b31baf4 |
186 | |
6d5355c3 |
187 | # check class |
188 | |
7b31baf4 |
189 | is_deeply( |
6d5355c3 |
190 | [ sort $class_mop_class_meta->get_attribute_list ], |
c0ae440b |
191 | [ sort @class_mop_class_attributes ], |
192 | '... got the right list of attributes' |
193 | ); |
0ac992ee |
194 | |
7b31baf4 |
195 | is_deeply( |
b1aaf0dc |
196 | [ sort keys %{$class_mop_class_meta->_attribute_map} ], |
c0ae440b |
197 | [ sort @class_mop_class_attributes ], |
0ac992ee |
198 | '... got the right list of attributes'); |
7b31baf4 |
199 | |
0586121e |
200 | foreach my $attribute_name (sort @class_mop_class_attributes) { |
0ac992ee |
201 | ok($class_mop_class_meta->has_attribute($attribute_name), '... Class::MOP::Class->has_attribute(' . $attribute_name . ')'); |
202 | isa_ok($class_mop_class_meta->get_attribute($attribute_name), 'Class::MOP::Attribute'); |
6d5355c3 |
203 | } |
204 | |
7f436b8c |
205 | # check module |
6d5355c3 |
206 | |
207 | is_deeply( |
6d5355c3 |
208 | [ sort $class_mop_package_meta->get_attribute_list ], |
c0ae440b |
209 | [ sort @class_mop_package_attributes ], |
6d5355c3 |
210 | '... got the right list of attributes'); |
0ac992ee |
211 | |
6d5355c3 |
212 | is_deeply( |
b1aaf0dc |
213 | [ sort keys %{$class_mop_package_meta->_attribute_map} ], |
c0ae440b |
214 | [ sort @class_mop_package_attributes ], |
0ac992ee |
215 | '... got the right list of attributes'); |
6d5355c3 |
216 | |
0586121e |
217 | foreach my $attribute_name (sort @class_mop_package_attributes) { |
0ac992ee |
218 | ok($class_mop_package_meta->has_attribute($attribute_name), '... Class::MOP::Package->has_attribute(' . $attribute_name . ')'); |
219 | isa_ok($class_mop_package_meta->get_attribute($attribute_name), 'Class::MOP::Attribute'); |
727919c5 |
220 | } |
221 | |
0ac992ee |
222 | # check package |
7f436b8c |
223 | |
224 | is_deeply( |
7f436b8c |
225 | [ sort $class_mop_module_meta->get_attribute_list ], |
c0ae440b |
226 | [ sort @class_mop_module_attributes ], |
7f436b8c |
227 | '... got the right list of attributes'); |
0ac992ee |
228 | |
7f436b8c |
229 | is_deeply( |
b1aaf0dc |
230 | [ sort keys %{$class_mop_module_meta->_attribute_map} ], |
c0ae440b |
231 | [ sort @class_mop_module_attributes ], |
0ac992ee |
232 | '... got the right list of attributes'); |
7f436b8c |
233 | |
0586121e |
234 | foreach my $attribute_name (sort @class_mop_module_attributes) { |
0ac992ee |
235 | ok($class_mop_module_meta->has_attribute($attribute_name), '... Class::MOP::Module->has_attribute(' . $attribute_name . ')'); |
236 | isa_ok($class_mop_module_meta->get_attribute($attribute_name), 'Class::MOP::Attribute'); |
7f436b8c |
237 | } |
238 | |
7b31baf4 |
239 | ## check the attributes themselves |
240 | |
6d5355c3 |
241 | # ... package |
242 | |
8683db0e |
243 | ok($class_mop_package_meta->get_attribute('package')->has_reader, '... Class::MOP::Class package has a reader'); |
244 | is(ref($class_mop_package_meta->get_attribute('package')->reader), 'HASH', '... Class::MOP::Class package\'s a reader is { name => sub { ... } }'); |
6d5355c3 |
245 | |
8683db0e |
246 | ok($class_mop_package_meta->get_attribute('package')->has_init_arg, '... Class::MOP::Class package has a init_arg'); |
247 | is($class_mop_package_meta->get_attribute('package')->init_arg, 'package', '... Class::MOP::Class package\'s a init_arg is package'); |
7b31baf4 |
248 | |
f197afa6 |
249 | # ... class, but inherited from HasMethods |
250 | ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_reader, '... Class::MOP::Class method_metaclass has a reader'); |
251 | is_deeply($class_mop_class_meta->find_attribute_by_name('method_metaclass')->reader, |
9b871d79 |
252 | { 'method_metaclass' => \&Class::MOP::Mixin::HasMethods::method_metaclass }, |
f197afa6 |
253 | '... Class::MOP::Class method_metaclass\'s a reader is &method_metaclass'); |
b1ff395f |
254 | |
f197afa6 |
255 | ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_init_arg, '... Class::MOP::Class method_metaclass has a init_arg'); |
256 | is($class_mop_class_meta->find_attribute_by_name('method_metaclass')->init_arg, |
b1ff395f |
257 | 'method_metaclass', |
f197afa6 |
258 | '... Class::MOP::Class method_metaclass\'s init_arg is method_metaclass'); |
b1ff395f |
259 | |
f197afa6 |
260 | ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_default, '... Class::MOP::Class method_metaclass has a default'); |
261 | is($class_mop_class_meta->find_attribute_by_name('method_metaclass')->default, |
b1ff395f |
262 | 'Class::MOP::Method', |
f197afa6 |
263 | '... Class::MOP::Class method_metaclass\'s a default is Class::MOP:::Method'); |
b1ff395f |
264 | |
f197afa6 |
265 | ok($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->has_reader, '... Class::MOP::Class wrapped_method_metaclass has a reader'); |
266 | is_deeply($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->reader, |
9b871d79 |
267 | { 'wrapped_method_metaclass' => \&Class::MOP::Mixin::HasMethods::wrapped_method_metaclass }, |
f197afa6 |
268 | '... Class::MOP::Class wrapped_method_metaclass\'s a reader is &wrapped_method_metaclass'); |
b1ff395f |
269 | |
f197afa6 |
270 | ok($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->has_init_arg, '... Class::MOP::Class wrapped_method_metaclass has a init_arg'); |
271 | is($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->init_arg, |
b1ff395f |
272 | 'wrapped_method_metaclass', |
f197afa6 |
273 | '... Class::MOP::Class wrapped_method_metaclass\'s init_arg is wrapped_method_metaclass'); |
b1ff395f |
274 | |
f197afa6 |
275 | ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_default, '... Class::MOP::Class method_metaclass has a default'); |
276 | is($class_mop_class_meta->find_attribute_by_name('method_metaclass')->default, |
b1ff395f |
277 | 'Class::MOP::Method', |
f197afa6 |
278 | '... Class::MOP::Class method_metaclass\'s a default is Class::MOP:::Method'); |
b1ff395f |
279 | |
280 | |
9b871d79 |
281 | # ... class, but inherited from HasAttributes |
7b31baf4 |
282 | |
9b871d79 |
283 | ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_reader, '... Class::MOP::Class attributes has a reader'); |
284 | is_deeply($class_mop_class_meta->find_attribute_by_name('attributes')->reader, |
285 | { '_attribute_map' => \&Class::MOP::Mixin::HasAttributes::_attribute_map }, |
b1aaf0dc |
286 | '... Class::MOP::Class attributes\'s a reader is &_attribute_map'); |
0ac992ee |
287 | |
9b871d79 |
288 | ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_init_arg, '... Class::MOP::Class attributes has a init_arg'); |
289 | is($class_mop_class_meta->find_attribute_by_name('attributes')->init_arg, |
0ac992ee |
290 | 'attributes', |
8683db0e |
291 | '... Class::MOP::Class attributes\'s a init_arg is attributes'); |
0ac992ee |
292 | |
9b871d79 |
293 | ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_default, '... Class::MOP::Class attributes has a default'); |
294 | is_deeply($class_mop_class_meta->find_attribute_by_name('attributes')->default('Foo'), |
0ac992ee |
295 | {}, |
8683db0e |
296 | '... Class::MOP::Class attributes\'s a default of {}'); |
7b31baf4 |
297 | |
9b871d79 |
298 | ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_reader, '... Class::MOP::Class attribute_metaclass has a reader'); |
299 | is_deeply($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->reader, |
300 | { 'attribute_metaclass' => \&Class::MOP::Mixin::HasAttributes::attribute_metaclass }, |
8683db0e |
301 | '... Class::MOP::Class attribute_metaclass\'s a reader is &attribute_metaclass'); |
0ac992ee |
302 | |
9b871d79 |
303 | ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_init_arg, '... Class::MOP::Class attribute_metaclass has a init_arg'); |
304 | is($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->init_arg, |
0ac992ee |
305 | 'attribute_metaclass', |
8683db0e |
306 | '... Class::MOP::Class attribute_metaclass\'s a init_arg is attribute_metaclass'); |
0ac992ee |
307 | |
9b871d79 |
308 | ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_default, '... Class::MOP::Class attribute_metaclass has a default'); |
309 | is($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->default, |
0ac992ee |
310 | 'Class::MOP::Attribute', |
8683db0e |
311 | '... Class::MOP::Class attribute_metaclass\'s a default is Class::MOP:::Attribute'); |
0ac992ee |
312 | |
7b31baf4 |
313 | # check the values of some of the methods |
314 | |
6d5355c3 |
315 | is($class_mop_class_meta->name, 'Class::MOP::Class', '... Class::MOP::Class->name'); |
316 | is($class_mop_class_meta->version, $Class::MOP::Class::VERSION, '... Class::MOP::Class->version'); |
a5eca695 |
317 | |
58d75218 |
318 | ok($class_mop_class_meta->has_package_symbol('$VERSION'), '... Class::MOP::Class->has_package_symbol($VERSION)'); |
0ac992ee |
319 | is(${$class_mop_class_meta->get_package_symbol('$VERSION')}, |
320 | $Class::MOP::Class::VERSION, |
58d75218 |
321 | '... Class::MOP::Class->get_package_symbol($VERSION)'); |
7b31baf4 |
322 | |
a5eca695 |
323 | is_deeply( |
0ac992ee |
324 | [ $class_mop_class_meta->superclasses ], |
f197afa6 |
325 | [ qw/Class::MOP::Module Class::MOP::Mixin::HasAttributes Class::MOP::Mixin::HasMethods/ ], |
2243a22b |
326 | '... Class::MOP::Class->superclasses == [ Class::MOP::Module ]'); |
0ac992ee |
327 | |
a5eca695 |
328 | is_deeply( |
0ac992ee |
329 | [ $class_mop_class_meta->class_precedence_list ], |
2243a22b |
330 | [ qw/ |
331 | Class::MOP::Class |
332 | Class::MOP::Module |
0ac992ee |
333 | Class::MOP::Package |
334 | Class::MOP::Object |
9b871d79 |
335 | Class::MOP::Mixin::HasAttributes |
336 | Class::MOP::Mixin |
f197afa6 |
337 | Class::MOP::Mixin::HasMethods |
338 | Class::MOP::Mixin |
0ac992ee |
339 | / ], |
2243a22b |
340 | '... Class::MOP::Class->class_precedence_list == [ Class::MOP::Class Class::MOP::Module Class::MOP::Package ]'); |
a5eca695 |
341 | |
6d5355c3 |
342 | is($class_mop_class_meta->attribute_metaclass, 'Class::MOP::Attribute', '... got the right value for attribute_metaclass'); |
343 | is($class_mop_class_meta->method_metaclass, 'Class::MOP::Method', '... got the right value for method_metaclass'); |
344 | is($class_mop_class_meta->instance_metaclass, 'Class::MOP::Instance', '... got the right value for instance_metaclass'); |
7b31baf4 |
345 | |
86a4d873 |
346 | done_testing; |