make github the primary repository
[gitmo/Moose.git] / t / cmop / self_introspection.t
CommitLineData
38bf2a25 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Fatal;
6
7use Class::MOP;
8use Class::MOP::Class;
9use Class::MOP::Package;
10use 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
17my $class_mop_class_meta = Class::MOP::Class->meta();
18isa_ok($class_mop_class_meta, 'Class::MOP::Class');
19
20my $class_mop_package_meta = Class::MOP::Package->meta();
21isa_ok($class_mop_package_meta, 'Class::MOP::Package');
22
23my $class_mop_module_meta = Class::MOP::Module->meta();
24isa_ok($class_mop_module_meta, 'Class::MOP::Module');
25
26my @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
44my @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
54my @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
124is_deeply([ sort $class_mop_class_meta->get_method_list ], [ sort @class_mop_class_methods ], '... got the correct method list for class');
125
126foreach 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
138is_deeply([ sort $class_mop_package_meta->get_method_list ], [ sort @class_mop_package_methods ], '... got the correct method list for package');
139
140foreach 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
152is_deeply([ sort $class_mop_module_meta->get_method_list ], [ sort @class_mop_module_methods ], '... got the correct method list for module');
153
154foreach 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
167foreach 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
178my @class_mop_package_attributes = (
179 'package',
180 'namespace',
181);
182
183my @class_mop_module_attributes = (
184 'version',
185 'authority'
186);
187
188my @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
199is_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
205is_deeply(
206 [ sort keys %{$class_mop_class_meta->_attribute_map} ],
207 [ sort @class_mop_class_attributes ],
208 '... got the right list of attributes');
209
210foreach 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
217is_deeply(
218 [ sort $class_mop_package_meta->get_attribute_list ],
219 [ sort @class_mop_package_attributes ],
220 '... got the right list of attributes');
221
222is_deeply(
223 [ sort keys %{$class_mop_package_meta->_attribute_map} ],
224 [ sort @class_mop_package_attributes ],
225 '... got the right list of attributes');
226
227foreach 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
234is_deeply(
235 [ sort $class_mop_module_meta->get_attribute_list ],
236 [ sort @class_mop_module_attributes ],
237 '... got the right list of attributes');
238
239is_deeply(
240 [ sort keys %{$class_mop_module_meta->_attribute_map} ],
241 [ sort @class_mop_module_attributes ],
242 '... got the right list of attributes');
243
244foreach 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
253ok($class_mop_package_meta->get_attribute('package')->has_reader, '... Class::MOP::Class package has a reader');
254is(ref($class_mop_package_meta->get_attribute('package')->reader), 'HASH', '... Class::MOP::Class package\'s a reader is { name => sub { ... } }');
255
256ok($class_mop_package_meta->get_attribute('package')->has_init_arg, '... Class::MOP::Class package has a init_arg');
257is($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
260ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_reader, '... Class::MOP::Class method_metaclass has a reader');
261is_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
265ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_init_arg, '... Class::MOP::Class method_metaclass has a init_arg');
266is($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
270ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_default, '... Class::MOP::Class method_metaclass has a default');
271is($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
275ok($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->has_reader, '... Class::MOP::Class wrapped_method_metaclass has a reader');
276is_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
280ok($class_mop_class_meta->find_attribute_by_name('wrapped_method_metaclass')->has_init_arg, '... Class::MOP::Class wrapped_method_metaclass has a init_arg');
281is($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
285ok($class_mop_class_meta->find_attribute_by_name('method_metaclass')->has_default, '... Class::MOP::Class method_metaclass has a default');
286is($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
293ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_reader, '... Class::MOP::Class attributes has a reader');
294is_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
298ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_init_arg, '... Class::MOP::Class attributes has a init_arg');
299is($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
303ok($class_mop_class_meta->find_attribute_by_name('attributes')->has_default, '... Class::MOP::Class attributes has a default');
304is_deeply($class_mop_class_meta->find_attribute_by_name('attributes')->default('Foo'),
305 {},
306 '... Class::MOP::Class attributes\'s a default of {}');
307
308ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_reader, '... Class::MOP::Class attribute_metaclass has a reader');
309is_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
313ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_init_arg, '... Class::MOP::Class attribute_metaclass has a init_arg');
314is($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
318ok($class_mop_class_meta->find_attribute_by_name('attribute_metaclass')->has_default, '... Class::MOP::Class attribute_metaclass has a default');
319is($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
325is($class_mop_class_meta->name, 'Class::MOP::Class', '... Class::MOP::Class->name');
326is($class_mop_class_meta->version, $Class::MOP::Class::VERSION, '... Class::MOP::Class->version');
327
9b60687d 328if ( defined $Class::MOP::Class::VERSION ) {
329 ok($class_mop_class_meta->has_package_symbol('$VERSION'), '... Class::MOP::Class->has_package_symbol($VERSION)');
330}
38bf2a25 331is(${$class_mop_class_meta->get_package_symbol('$VERSION')},
332 $Class::MOP::Class::VERSION,
333 '... Class::MOP::Class->get_package_symbol($VERSION)');
334
335is_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
340is_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
354is($class_mop_class_meta->attribute_metaclass, 'Class::MOP::Attribute', '... got the right value for attribute_metaclass');
355is($class_mop_class_meta->method_metaclass, 'Class::MOP::Method', '... got the right value for method_metaclass');
356is($class_mop_class_meta->instance_metaclass, 'Class::MOP::Instance', '... got the right value for instance_metaclass');
357
358done_testing;