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