Commit | Line | Data |
38bf2a25 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Fatal; |
6 | |
7 | use Class::MOP; |
8 | use Class::MOP::Class; |
9 | use Class::MOP::Package; |
10 | use Class::MOP::Module; |
11 | |
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 | |
17 | my $class_mop_class_meta = Class::MOP::Class->meta(); |
18 | isa_ok($class_mop_class_meta, 'Class::MOP::Class'); |
19 | |
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( |
27 | _new |
28 | |
0db1c8dc |
29 | initialize reinitialize create create_anon is_anon |
30 | _free_anon _anon_cache_key _anon_package_prefix |
38bf2a25 |
31 | |
32 | name |
33 | namespace |
34 | |
35 | add_package_symbol get_package_symbol has_package_symbol |
36 | remove_package_symbol get_or_add_package_symbol |
37 | list_all_package_symbols get_all_package_symbols remove_package_glob |
38 | |
39 | _package_stash |
40 | |
0db1c8dc |
41 | DESTROY |
38bf2a25 |
42 | ); |
43 | |
44 | my @class_mop_module_methods = qw( |
45 | _new |
46 | |
47 | _instantiate_module |
48 | |
49 | version authority identifier create |
0db1c8dc |
50 | |
51 | _anon_cache_key _anon_package_prefix |
38bf2a25 |
52 | ); |
53 | |
54 | my @class_mop_class_methods = qw( |
55 | _new |
56 | |
57 | is_pristine |
58 | |
59 | initialize reinitialize create |
60 | |
0db1c8dc |
61 | create_anon_class is_anon_class |
62 | _anon_cache_key _anon_package_prefix |
38bf2a25 |
63 | |
64 | instance_metaclass get_meta_instance |
65 | _inline_create_instance |
66 | _inline_rebless_instance |
67 | _inline_get_mop_slot _inline_set_mop_slot _inline_clear_mop_slot |
680b7c27 |
68 | _create_meta_instance |
38bf2a25 |
69 | new_object clone_object |
70 | _inline_new_object _inline_default_value _inline_preserve_weak_metaclasses |
71 | _inline_slot_initializer _inline_extra_init _inline_fallback_constructor |
72 | _inline_generate_instance _inline_params _inline_slot_initializers |
73 | _inline_init_attr_from_constructor _inline_init_attr_from_default |
74 | _generate_fallback_constructor |
96fec633 |
75 | _eval_environment |
680b7c27 |
76 | _construct_instance |
77 | _construct_class_instance |
78 | _clone_instance |
38bf2a25 |
79 | rebless_instance rebless_instance_back rebless_instance_away |
80 | _force_rebless_instance _fixup_attributes_after_rebless |
680b7c27 |
81 | _check_metaclass_compatibility |
38bf2a25 |
82 | _check_class_metaclass_compatibility _check_single_metaclass_compatibility |
83 | _class_metaclass_is_compatible _single_metaclass_is_compatible |
84 | _fix_metaclass_incompatibility _fix_class_metaclass_incompatibility |
85 | _fix_single_metaclass_incompatibility _base_metaclasses |
86 | _can_fix_metaclass_incompatibility |
87 | _class_metaclass_can_be_made_compatible |
88 | _single_metaclass_can_be_made_compatible |
89 | |
90 | _remove_generated_metaobjects |
91 | _restore_metaobjects_from |
92 | |
93 | add_meta_instance_dependencies remove_meta_instance_dependencies update_meta_instance_dependencies |
94 | add_dependent_meta_instance remove_dependent_meta_instance |
95 | invalidate_meta_instances invalidate_meta_instance |
96 | |
97 | superclasses subclasses direct_subclasses class_precedence_list |
7e3009e7 |
98 | linearized_isa _method_lookup_order _superclasses_updated _superclass_metas |
38bf2a25 |
99 | |
680b7c27 |
100 | get_all_method_names get_all_methods |
38bf2a25 |
101 | find_method_by_name find_all_methods_by_name find_next_method_by_name |
102 | |
103 | add_before_method_modifier add_after_method_modifier add_around_method_modifier |
104 | |
105 | _attach_attribute |
106 | _post_add_attribute |
107 | remove_attribute |
108 | find_attribute_by_name |
109 | get_all_attributes |
110 | |
38bf2a25 |
111 | is_mutable is_immutable make_mutable make_immutable |
112 | _initialize_immutable _install_inlined_code _inlined_methods |
113 | _add_inlined_method _inline_accessors _inline_constructor |
114 | _inline_destructor _immutable_options _real_ref_name |
115 | _rebless_as_immutable _rebless_as_mutable _remove_inlined_code |
116 | |
117 | _immutable_metaclass |
118 | immutable_trait immutable_options |
119 | constructor_name constructor_class destructor_class |
38bf2a25 |
120 | ); |
121 | |
122 | # check the class ... |
123 | |
124 | is_deeply([ sort $class_mop_class_meta->get_method_list ], [ sort @class_mop_class_methods ], '... got the correct method list for class'); |
125 | |
126 | foreach my $method_name (sort @class_mop_class_methods) { |
127 | ok($class_mop_class_meta->has_method($method_name), '... Class::MOP::Class->has_method(' . $method_name . ')'); |
128 | { |
129 | no strict 'refs'; |
130 | is($class_mop_class_meta->get_method($method_name)->body, |
131 | \&{'Class::MOP::Class::' . $method_name}, |
132 | '... Class::MOP::Class->get_method(' . $method_name . ') == &Class::MOP::Class::' . $method_name); |
133 | } |
134 | } |
135 | |
136 | ## check the package .... |
137 | |
138 | is_deeply([ sort $class_mop_package_meta->get_method_list ], [ sort @class_mop_package_methods ], '... got the correct method list for package'); |
139 | |
140 | foreach my $method_name (sort @class_mop_package_methods) { |
141 | ok($class_mop_package_meta->has_method($method_name), '... Class::MOP::Package->has_method(' . $method_name . ')'); |
142 | { |
143 | no strict 'refs'; |
144 | is($class_mop_package_meta->get_method($method_name)->body, |
145 | \&{'Class::MOP::Package::' . $method_name}, |
146 | '... Class::MOP::Package->get_method(' . $method_name . ') == &Class::MOP::Package::' . $method_name); |
147 | } |
148 | } |
149 | |
150 | ## check the module .... |
151 | |
152 | is_deeply([ sort $class_mop_module_meta->get_method_list ], [ sort @class_mop_module_methods ], '... got the correct method list for module'); |
153 | |
154 | foreach my $method_name (sort @class_mop_module_methods) { |
155 | ok($class_mop_module_meta->has_method($method_name), '... Class::MOP::Module->has_method(' . $method_name . ')'); |
156 | { |
157 | no strict 'refs'; |
158 | is($class_mop_module_meta->get_method($method_name)->body, |
159 | \&{'Class::MOP::Module::' . $method_name}, |
160 | '... Class::MOP::Module->get_method(' . $method_name . ') == &Class::MOP::Module::' . $method_name); |
161 | } |
162 | } |
163 | |
164 | |
165 | # check for imported functions which are not methods |
166 | |
167 | foreach my $non_method_name (qw( |
168 | confess |
169 | blessed |
170 | subname |
171 | svref_2object |
172 | )) { |
173 | ok(!$class_mop_class_meta->has_method($non_method_name), '... NOT Class::MOP::Class->has_method(' . $non_method_name . ')'); |
174 | } |
175 | |
176 | # check for the right attributes |
177 | |
178 | my @class_mop_package_attributes = ( |
179 | 'package', |
180 | 'namespace', |
181 | ); |
182 | |
183 | my @class_mop_module_attributes = ( |
184 | 'version', |
185 | 'authority' |
186 | ); |
187 | |
188 | my @class_mop_class_attributes = ( |
189 | 'superclasses', |
190 | 'instance_metaclass', |
191 | 'immutable_trait', |
192 | 'constructor_name', |
193 | 'constructor_class', |
194 | 'destructor_class', |
195 | ); |
196 | |
197 | # check class |
198 | |
199 | is_deeply( |
200 | [ sort $class_mop_class_meta->get_attribute_list ], |
201 | [ sort @class_mop_class_attributes ], |
202 | '... got the right list of attributes' |
203 | ); |
204 | |
205 | is_deeply( |
206 | [ sort keys %{$class_mop_class_meta->_attribute_map} ], |
207 | [ sort @class_mop_class_attributes ], |
208 | '... got the right list of attributes'); |
209 | |
210 | foreach my $attribute_name (sort @class_mop_class_attributes) { |
211 | ok($class_mop_class_meta->has_attribute($attribute_name), '... Class::MOP::Class->has_attribute(' . $attribute_name . ')'); |
212 | isa_ok($class_mop_class_meta->get_attribute($attribute_name), 'Class::MOP::Attribute'); |
213 | } |
214 | |
215 | # check module |
216 | |
217 | is_deeply( |
218 | [ sort $class_mop_package_meta->get_attribute_list ], |
219 | [ sort @class_mop_package_attributes ], |
220 | '... got the right list of attributes'); |
221 | |
222 | is_deeply( |
223 | [ sort keys %{$class_mop_package_meta->_attribute_map} ], |
224 | [ sort @class_mop_package_attributes ], |
225 | '... got the right list of attributes'); |
226 | |
227 | foreach my $attribute_name (sort @class_mop_package_attributes) { |
228 | ok($class_mop_package_meta->has_attribute($attribute_name), '... Class::MOP::Package->has_attribute(' . $attribute_name . ')'); |
229 | isa_ok($class_mop_package_meta->get_attribute($attribute_name), 'Class::MOP::Attribute'); |
230 | } |
231 | |
232 | # check package |
233 | |
234 | is_deeply( |
235 | [ sort $class_mop_module_meta->get_attribute_list ], |
236 | [ sort @class_mop_module_attributes ], |
237 | '... got the right list of attributes'); |
238 | |
239 | is_deeply( |
240 | [ sort keys %{$class_mop_module_meta->_attribute_map} ], |
241 | [ sort @class_mop_module_attributes ], |
242 | '... got the right list of attributes'); |
243 | |
244 | foreach my $attribute_name (sort @class_mop_module_attributes) { |
245 | ok($class_mop_module_meta->has_attribute($attribute_name), '... Class::MOP::Module->has_attribute(' . $attribute_name . ')'); |
246 | isa_ok($class_mop_module_meta->get_attribute($attribute_name), 'Class::MOP::Attribute'); |
247 | } |
248 | |
249 | ## check the attributes themselves |
250 | |
251 | # ... package |
252 | |
253 | ok($class_mop_package_meta->get_attribute('package')->has_reader, '... Class::MOP::Class package has a reader'); |
254 | is(ref($class_mop_package_meta->get_attribute('package')->reader), 'HASH', '... Class::MOP::Class package\'s a reader is { name => sub { ... } }'); |
255 | |
256 | ok($class_mop_package_meta->get_attribute('package')->has_init_arg, '... Class::MOP::Class package has a init_arg'); |
257 | is($class_mop_package_meta->get_attribute('package')->init_arg, 'package', '... Class::MOP::Class package\'s a init_arg is package'); |
258 | |
259 | # ... class, but inherited from HasMethods |
260 | ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_reader, '... Class::MOP::Class method_metaclass has a reader'); |
261 | is_deeply($class_mop_class_meta->find_attribute_by_name('method_metaclass')->reader, |
262 | { 'method_metaclass' => \&Class::MOP::Mixin::HasMethods::method_metaclass }, |
263 | '... Class::MOP::Class method_metaclass\'s a reader is &method_metaclass'); |
264 | |
265 | ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_init_arg, '... Class::MOP::Class method_metaclass has a init_arg'); |
266 | is($class_mop_class_meta->find_attribute_by_name('method_metaclass')->init_arg, |
267 | 'method_metaclass', |
268 | '... Class::MOP::Class method_metaclass\'s init_arg is method_metaclass'); |
269 | |
270 | ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_default, '... Class::MOP::Class method_metaclass has a default'); |
271 | is($class_mop_class_meta->find_attribute_by_name('method_metaclass')->default, |
272 | 'Class::MOP::Method', |
273 | '... Class::MOP::Class method_metaclass\'s a default is Class::MOP:::Method'); |
274 | |
275 | ok($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->has_reader, '... Class::MOP::Class wrapped_method_metaclass has a reader'); |
276 | is_deeply($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->reader, |
277 | { 'wrapped_method_metaclass' => \&Class::MOP::Mixin::HasMethods::wrapped_method_metaclass }, |
278 | '... Class::MOP::Class wrapped_method_metaclass\'s a reader is &wrapped_method_metaclass'); |
279 | |
280 | 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'); |
281 | is($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->init_arg, |
282 | 'wrapped_method_metaclass', |
283 | '... Class::MOP::Class wrapped_method_metaclass\'s init_arg is wrapped_method_metaclass'); |
284 | |
285 | ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_default, '... Class::MOP::Class method_metaclass has a default'); |
286 | is($class_mop_class_meta->find_attribute_by_name('method_metaclass')->default, |
287 | 'Class::MOP::Method', |
288 | '... Class::MOP::Class method_metaclass\'s a default is Class::MOP:::Method'); |
289 | |
290 | |
291 | # ... class, but inherited from HasAttributes |
292 | |
293 | ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_reader, '... Class::MOP::Class attributes has a reader'); |
294 | is_deeply($class_mop_class_meta->find_attribute_by_name('attributes')->reader, |
295 | { '_attribute_map' => \&Class::MOP::Mixin::HasAttributes::_attribute_map }, |
296 | '... Class::MOP::Class attributes\'s a reader is &_attribute_map'); |
297 | |
298 | ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_init_arg, '... Class::MOP::Class attributes has a init_arg'); |
299 | is($class_mop_class_meta->find_attribute_by_name('attributes')->init_arg, |
300 | 'attributes', |
301 | '... Class::MOP::Class attributes\'s a init_arg is attributes'); |
302 | |
303 | ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_default, '... Class::MOP::Class attributes has a default'); |
304 | is_deeply($class_mop_class_meta->find_attribute_by_name('attributes')->default('Foo'), |
305 | {}, |
306 | '... Class::MOP::Class attributes\'s a default of {}'); |
307 | |
308 | ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_reader, '... Class::MOP::Class attribute_metaclass has a reader'); |
309 | is_deeply($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->reader, |
310 | { 'attribute_metaclass' => \&Class::MOP::Mixin::HasAttributes::attribute_metaclass }, |
311 | '... Class::MOP::Class attribute_metaclass\'s a reader is &attribute_metaclass'); |
312 | |
313 | ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_init_arg, '... Class::MOP::Class attribute_metaclass has a init_arg'); |
314 | is($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->init_arg, |
315 | 'attribute_metaclass', |
316 | '... Class::MOP::Class attribute_metaclass\'s a init_arg is attribute_metaclass'); |
317 | |
318 | ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_default, '... Class::MOP::Class attribute_metaclass has a default'); |
319 | is($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->default, |
320 | 'Class::MOP::Attribute', |
321 | '... Class::MOP::Class attribute_metaclass\'s a default is Class::MOP:::Attribute'); |
322 | |
323 | # check the values of some of the methods |
324 | |
325 | is($class_mop_class_meta->name, 'Class::MOP::Class', '... Class::MOP::Class->name'); |
326 | is($class_mop_class_meta->version, $Class::MOP::Class::VERSION, '... Class::MOP::Class->version'); |
327 | |
9b60687d |
328 | if ( defined $Class::MOP::Class::VERSION ) { |
329 | ok($class_mop_class_meta->has_package_symbol('$VERSION'), '... Class::MOP::Class->has_package_symbol($VERSION)'); |
330 | } |
38bf2a25 |
331 | is(${$class_mop_class_meta->get_package_symbol('$VERSION')}, |
332 | $Class::MOP::Class::VERSION, |
333 | '... Class::MOP::Class->get_package_symbol($VERSION)'); |
334 | |
335 | is_deeply( |
336 | [ $class_mop_class_meta->superclasses ], |
337 | [ qw/Class::MOP::Module Class::MOP::Mixin::HasAttributes Class::MOP::Mixin::HasMethods/ ], |
338 | '... Class::MOP::Class->superclasses == [ Class::MOP::Module ]'); |
339 | |
340 | is_deeply( |
341 | [ $class_mop_class_meta->class_precedence_list ], |
342 | [ qw/ |
343 | Class::MOP::Class |
344 | Class::MOP::Module |
345 | Class::MOP::Package |
346 | Class::MOP::Object |
347 | Class::MOP::Mixin::HasAttributes |
348 | Class::MOP::Mixin |
349 | Class::MOP::Mixin::HasMethods |
350 | Class::MOP::Mixin |
351 | / ], |
352 | '... Class::MOP::Class->class_precedence_list == [ Class::MOP::Class Class::MOP::Module Class::MOP::Package ]'); |
353 | |
354 | is($class_mop_class_meta->attribute_metaclass, 'Class::MOP::Attribute', '... got the right value for attribute_metaclass'); |
355 | is($class_mop_class_meta->method_metaclass, 'Class::MOP::Method', '... got the right value for method_metaclass'); |
356 | is($class_mop_class_meta->instance_metaclass, 'Class::MOP::Instance', '... got the right value for instance_metaclass'); |
357 | |
358 | done_testing; |