Class::MOP::Class::Immutable
[gitmo/Class-MOP.git] / t / 010_self_introspection.t
CommitLineData
a5eca695 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
857f87a7 6use Test::More tests => 152;
a5eca695 7use Test::Exception;
8
9BEGIN {
727919c5 10 use_ok('Class::MOP');
a5eca695 11 use_ok('Class::MOP::Class');
12}
13
013b1897 14{
15 my $class = Class::MOP::Class->initialize('Foo');
16 is($class->meta, Class::MOP::Class->meta, '... instance and class both lead to the same meta');
17}
18
2eb717d5 19my $meta = Class::MOP::Class->meta();
a5eca695 20isa_ok($meta, 'Class::MOP::Class');
21
7b31baf4 22my @methods = qw(
2eb717d5 23 meta
24
587aca23 25 get_all_metaclasses get_all_metaclass_names get_all_metaclass_instances
26
66b3dded 27 initialize reinitialize create create_anon_class
552e3d24 28
2d711cc8 29 instance_metaclass get_meta_instance
7b31baf4 30 new_object clone_object
31 construct_instance construct_class_instance clone_instance
550d56db 32 check_metaclass_compatability
7b31baf4 33
a5eca695 34 name version
552e3d24 35
7b31baf4 36 attribute_metaclass method_metaclass
37
a5eca695 38 superclasses class_precedence_list
552e3d24 39
aa448b16 40 has_method get_method add_method remove_method alias_method
96ceced8 41 get_method_list compute_all_applicable_methods
42 find_all_methods_by_name find_next_method_by_name
552e3d24 43
a4258ffd 44 add_before_method_modifier add_after_method_modifier add_around_method_modifier
ee5e71d4 45
552e3d24 46 has_attribute get_attribute add_attribute remove_attribute
058c1cf5 47 get_attribute_list get_attribute_map compute_all_applicable_attributes find_attribute_by_name
7b31baf4 48
49 add_package_variable get_package_variable has_package_variable remove_package_variable
77e5fce4 50
857f87a7 51 is_mutable is_immutable make_immutable
52
77e5fce4 53 DESTROY
7b31baf4 54 );
55
56is_deeply([ sort @methods ], [ sort $meta->get_method_list ], '... got the correct method list');
57
58foreach my $method_name (@methods) {
a5eca695 59 ok($meta->has_method($method_name), '... Class::MOP::Class->has_method(' . $method_name . ')');
60 {
61 no strict 'refs';
62 is($meta->get_method($method_name),
63 \&{'Class::MOP::Class::' . $method_name},
64 '... Class::MOP::Class->get_method(' . $method_name . ') == &Class::MOP::Class::' . $method_name);
65 }
66}
67
7b31baf4 68# check for imported functions which are not methods
69
2eb717d5 70foreach my $non_method_name (qw(
71 confess
72 blessed reftype
73 subname
74 svref_2object
75 )) {
76 ok(!$meta->has_method($non_method_name), '... NOT Class::MOP::Class->has_method(' . $non_method_name . ')');
77}
78
7b31baf4 79# check for the right attributes
80
2bab2be6 81my @attributes = (
82 '$:package',
83 '%:attributes',
84 '$:attribute_metaclass',
85 '$:method_metaclass',
86 '$:instance_metaclass'
87);
7b31baf4 88
89is_deeply(
90 [ sort @attributes ],
91 [ sort $meta->get_attribute_list ],
92 '... got the right list of attributes');
93
94is_deeply(
95 [ sort @attributes ],
96 [ sort keys %{$meta->get_attribute_map} ],
97 '... got the right list of attributes');
98
99foreach my $attribute_name (@attributes) {
727919c5 100 ok($meta->has_attribute($attribute_name), '... Class::MOP::Class->has_attribute(' . $attribute_name . ')');
101 isa_ok($meta->get_attribute($attribute_name), 'Class::MOP::Attribute');
102}
103
7b31baf4 104## check the attributes themselves
105
106ok($meta->get_attribute('$:package')->has_reader, '... Class::MOP::Class $:package has a reader');
b880e0de 107is(ref($meta->get_attribute('$:package')->reader), 'HASH', '... Class::MOP::Class $:package\'s a reader is { name => sub { ... } }');
7b31baf4 108
109ok($meta->get_attribute('$:package')->has_init_arg, '... Class::MOP::Class $:package has a init_arg');
110is($meta->get_attribute('$:package')->init_arg, ':package', '... Class::MOP::Class $:package\'s a init_arg is :package');
111
112ok($meta->get_attribute('%:attributes')->has_reader, '... Class::MOP::Class %:attributes has a reader');
f7259199 113is(ref($meta->get_attribute('%:attributes')->reader),
114 'HASH',
7b31baf4 115 '... Class::MOP::Class %:attributes\'s a reader is &get_attribute_map');
116
117ok($meta->get_attribute('%:attributes')->has_init_arg, '... Class::MOP::Class %:attributes has a init_arg');
118is($meta->get_attribute('%:attributes')->init_arg,
119 ':attributes',
120 '... Class::MOP::Class %:attributes\'s a init_arg is :attributes');
121
122ok($meta->get_attribute('%:attributes')->has_default, '... Class::MOP::Class %:attributes has a default');
123is_deeply($meta->get_attribute('%:attributes')->default,
124 {},
125 '... Class::MOP::Class %:attributes\'s a default of {}');
126
127ok($meta->get_attribute('$:attribute_metaclass')->has_reader, '... Class::MOP::Class $:attribute_metaclass has a reader');
128is($meta->get_attribute('$:attribute_metaclass')->reader,
129 'attribute_metaclass',
130 '... Class::MOP::Class $:attribute_metaclass\'s a reader is &attribute_metaclass');
131
132ok($meta->get_attribute('$:attribute_metaclass')->has_init_arg, '... Class::MOP::Class $:attribute_metaclass has a init_arg');
133is($meta->get_attribute('$:attribute_metaclass')->init_arg,
134 ':attribute_metaclass',
135 '... Class::MOP::Class $:attribute_metaclass\'s a init_arg is :attribute_metaclass');
136
137ok($meta->get_attribute('$:attribute_metaclass')->has_default, '... Class::MOP::Class $:attribute_metaclass has a default');
138is($meta->get_attribute('$:attribute_metaclass')->default,
139 'Class::MOP::Attribute',
140 '... Class::MOP::Class $:attribute_metaclass\'s a default is Class::MOP:::Attribute');
141
142ok($meta->get_attribute('$:method_metaclass')->has_reader, '... Class::MOP::Class $:method_metaclass has a reader');
143is($meta->get_attribute('$:method_metaclass')->reader,
144 'method_metaclass',
145 '... Class::MOP::Class $:method_metaclass\'s a reader is &method_metaclass');
146
147ok($meta->get_attribute('$:method_metaclass')->has_init_arg, '... Class::MOP::Class $:method_metaclass has a init_arg');
148is($meta->get_attribute('$:method_metaclass')->init_arg,
149 ':method_metaclass',
150 '... Class::MOP::Class $:method_metaclass\'s init_arg is :method_metaclass');
151
152ok($meta->get_attribute('$:method_metaclass')->has_default, '... Class::MOP::Class $:method_metaclass has a default');
153is($meta->get_attribute('$:method_metaclass')->default,
154 'Class::MOP::Method',
155 '... Class::MOP::Class $:method_metaclass\'s a default is Class::MOP:::Method');
156
157# check the values of some of the methods
158
a5eca695 159is($meta->name, 'Class::MOP::Class', '... Class::MOP::Class->name');
160is($meta->version, $Class::MOP::Class::VERSION, '... Class::MOP::Class->version');
161
7b31baf4 162ok($meta->has_package_variable('$VERSION'), '... Class::MOP::Class->has_package_variable($VERSION)');
163is(${$meta->get_package_variable('$VERSION')},
164 $Class::MOP::Class::VERSION,
165 '... Class::MOP::Class->get_package_variable($VERSION)');
166
a5eca695 167is_deeply(
168 [ $meta->superclasses ],
169 [],
170 '... Class::MOP::Class->superclasses == []');
171
172is_deeply(
173 [ $meta->class_precedence_list ],
174 [ 'Class::MOP::Class' ],
175 '... Class::MOP::Class->class_precedence_list == []');
176
7b31baf4 177is($meta->attribute_metaclass, 'Class::MOP::Attribute', '... got the right value for attribute_metaclass');
178is($meta->method_metaclass, 'Class::MOP::Method', '... got the right value for method_metaclass');
179