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