instance-protocol
[gitmo/Class-MOP.git] / t / 010_self_introspection.t
CommitLineData
a5eca695 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
2bab2be6 6use Test::More tests => 140;
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
27 initialize create create_anon_class
552e3d24 28
2bab2be6 29 instance_metaclass
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
50 );
51
52is_deeply([ sort @methods ], [ sort $meta->get_method_list ], '... got the correct method list');
53
54foreach my $method_name (@methods) {
a5eca695 55 ok($meta->has_method($method_name), '... Class::MOP::Class->has_method(' . $method_name . ')');
56 {
57 no strict 'refs';
58 is($meta->get_method($method_name),
59 \&{'Class::MOP::Class::' . $method_name},
60 '... Class::MOP::Class->get_method(' . $method_name . ') == &Class::MOP::Class::' . $method_name);
61 }
62}
63
7b31baf4 64# check for imported functions which are not methods
65
2eb717d5 66foreach my $non_method_name (qw(
67 confess
68 blessed reftype
69 subname
70 svref_2object
71 )) {
72 ok(!$meta->has_method($non_method_name), '... NOT Class::MOP::Class->has_method(' . $non_method_name . ')');
73}
74
7b31baf4 75# check for the right attributes
76
2bab2be6 77my @attributes = (
78 '$:package',
79 '%:attributes',
80 '$:attribute_metaclass',
81 '$:method_metaclass',
82 '$:instance_metaclass'
83);
7b31baf4 84
85is_deeply(
86 [ sort @attributes ],
87 [ sort $meta->get_attribute_list ],
88 '... got the right list of attributes');
89
90is_deeply(
91 [ sort @attributes ],
92 [ sort keys %{$meta->get_attribute_map} ],
93 '... got the right list of attributes');
94
95foreach my $attribute_name (@attributes) {
727919c5 96 ok($meta->has_attribute($attribute_name), '... Class::MOP::Class->has_attribute(' . $attribute_name . ')');
97 isa_ok($meta->get_attribute($attribute_name), 'Class::MOP::Attribute');
98}
99
7b31baf4 100## check the attributes themselves
101
102ok($meta->get_attribute('$:package')->has_reader, '... Class::MOP::Class $:package has a reader');
103is($meta->get_attribute('$:package')->reader, 'name', '... Class::MOP::Class $:package\'s a reader is &name');
104
105ok($meta->get_attribute('$:package')->has_init_arg, '... Class::MOP::Class $:package has a init_arg');
106is($meta->get_attribute('$:package')->init_arg, ':package', '... Class::MOP::Class $:package\'s a init_arg is :package');
107
108ok($meta->get_attribute('%:attributes')->has_reader, '... Class::MOP::Class %:attributes has a reader');
109is($meta->get_attribute('%:attributes')->reader,
110 'get_attribute_map',
111 '... Class::MOP::Class %:attributes\'s a reader is &get_attribute_map');
112
113ok($meta->get_attribute('%:attributes')->has_init_arg, '... Class::MOP::Class %:attributes has a init_arg');
114is($meta->get_attribute('%:attributes')->init_arg,
115 ':attributes',
116 '... Class::MOP::Class %:attributes\'s a init_arg is :attributes');
117
118ok($meta->get_attribute('%:attributes')->has_default, '... Class::MOP::Class %:attributes has a default');
119is_deeply($meta->get_attribute('%:attributes')->default,
120 {},
121 '... Class::MOP::Class %:attributes\'s a default of {}');
122
123ok($meta->get_attribute('$:attribute_metaclass')->has_reader, '... Class::MOP::Class $:attribute_metaclass has a reader');
124is($meta->get_attribute('$:attribute_metaclass')->reader,
125 'attribute_metaclass',
126 '... Class::MOP::Class $:attribute_metaclass\'s a reader is &attribute_metaclass');
127
128ok($meta->get_attribute('$:attribute_metaclass')->has_init_arg, '... Class::MOP::Class $:attribute_metaclass has a init_arg');
129is($meta->get_attribute('$:attribute_metaclass')->init_arg,
130 ':attribute_metaclass',
131 '... Class::MOP::Class $:attribute_metaclass\'s a init_arg is :attribute_metaclass');
132
133ok($meta->get_attribute('$:attribute_metaclass')->has_default, '... Class::MOP::Class $:attribute_metaclass has a default');
134is($meta->get_attribute('$:attribute_metaclass')->default,
135 'Class::MOP::Attribute',
136 '... Class::MOP::Class $:attribute_metaclass\'s a default is Class::MOP:::Attribute');
137
138ok($meta->get_attribute('$:method_metaclass')->has_reader, '... Class::MOP::Class $:method_metaclass has a reader');
139is($meta->get_attribute('$:method_metaclass')->reader,
140 'method_metaclass',
141 '... Class::MOP::Class $:method_metaclass\'s a reader is &method_metaclass');
142
143ok($meta->get_attribute('$:method_metaclass')->has_init_arg, '... Class::MOP::Class $:method_metaclass has a init_arg');
144is($meta->get_attribute('$:method_metaclass')->init_arg,
145 ':method_metaclass',
146 '... Class::MOP::Class $:method_metaclass\'s init_arg is :method_metaclass');
147
148ok($meta->get_attribute('$:method_metaclass')->has_default, '... Class::MOP::Class $:method_metaclass has a default');
149is($meta->get_attribute('$:method_metaclass')->default,
150 'Class::MOP::Method',
151 '... Class::MOP::Class $:method_metaclass\'s a default is Class::MOP:::Method');
152
153# check the values of some of the methods
154
a5eca695 155is($meta->name, 'Class::MOP::Class', '... Class::MOP::Class->name');
156is($meta->version, $Class::MOP::Class::VERSION, '... Class::MOP::Class->version');
157
7b31baf4 158ok($meta->has_package_variable('$VERSION'), '... Class::MOP::Class->has_package_variable($VERSION)');
159is(${$meta->get_package_variable('$VERSION')},
160 $Class::MOP::Class::VERSION,
161 '... Class::MOP::Class->get_package_variable($VERSION)');
162
a5eca695 163is_deeply(
164 [ $meta->superclasses ],
165 [],
166 '... Class::MOP::Class->superclasses == []');
167
168is_deeply(
169 [ $meta->class_precedence_list ],
170 [ 'Class::MOP::Class' ],
171 '... Class::MOP::Class->class_precedence_list == []');
172
7b31baf4 173is($meta->attribute_metaclass, 'Class::MOP::Attribute', '... got the right value for attribute_metaclass');
174is($meta->method_metaclass, 'Class::MOP::Method', '... got the right value for method_metaclass');
175