0_25
[gitmo/Class-MOP.git] / t / 010_self_introspection.t
CommitLineData
a5eca695 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
587aca23 6use Test::More tests => 134;
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
7b31baf4 29 new_object clone_object
30 construct_instance construct_class_instance clone_instance
550d56db 31 check_metaclass_compatability
7b31baf4 32
a5eca695 33 name version
552e3d24 34
7b31baf4 35 attribute_metaclass method_metaclass
36
a5eca695 37 superclasses class_precedence_list
552e3d24 38
aa448b16 39 has_method get_method add_method remove_method alias_method
96ceced8 40 get_method_list compute_all_applicable_methods
41 find_all_methods_by_name find_next_method_by_name
552e3d24 42
a4258ffd 43 add_before_method_modifier add_after_method_modifier add_around_method_modifier
ee5e71d4 44
552e3d24 45 has_attribute get_attribute add_attribute remove_attribute
7b31baf4 46 get_attribute_list get_attribute_map compute_all_applicable_attributes
47
48 add_package_variable get_package_variable has_package_variable remove_package_variable
49 );
50
51is_deeply([ sort @methods ], [ sort $meta->get_method_list ], '... got the correct method list');
52
53foreach my $method_name (@methods) {
a5eca695 54 ok($meta->has_method($method_name), '... Class::MOP::Class->has_method(' . $method_name . ')');
55 {
56 no strict 'refs';
57 is($meta->get_method($method_name),
58 \&{'Class::MOP::Class::' . $method_name},
59 '... Class::MOP::Class->get_method(' . $method_name . ') == &Class::MOP::Class::' . $method_name);
60 }
61}
62
7b31baf4 63# check for imported functions which are not methods
64
2eb717d5 65foreach my $non_method_name (qw(
66 confess
67 blessed reftype
68 subname
69 svref_2object
70 )) {
71 ok(!$meta->has_method($non_method_name), '... NOT Class::MOP::Class->has_method(' . $non_method_name . ')');
72}
73
7b31baf4 74# check for the right attributes
75
76my @attributes = ('$:package', '%:attributes', '$:attribute_metaclass', '$:method_metaclass');
77
78is_deeply(
79 [ sort @attributes ],
80 [ sort $meta->get_attribute_list ],
81 '... got the right list of attributes');
82
83is_deeply(
84 [ sort @attributes ],
85 [ sort keys %{$meta->get_attribute_map} ],
86 '... got the right list of attributes');
87
88foreach my $attribute_name (@attributes) {
727919c5 89 ok($meta->has_attribute($attribute_name), '... Class::MOP::Class->has_attribute(' . $attribute_name . ')');
90 isa_ok($meta->get_attribute($attribute_name), 'Class::MOP::Attribute');
91}
92
7b31baf4 93## check the attributes themselves
94
95ok($meta->get_attribute('$:package')->has_reader, '... Class::MOP::Class $:package has a reader');
96is($meta->get_attribute('$:package')->reader, 'name', '... Class::MOP::Class $:package\'s a reader is &name');
97
98ok($meta->get_attribute('$:package')->has_init_arg, '... Class::MOP::Class $:package has a init_arg');
99is($meta->get_attribute('$:package')->init_arg, ':package', '... Class::MOP::Class $:package\'s a init_arg is :package');
100
101ok($meta->get_attribute('%:attributes')->has_reader, '... Class::MOP::Class %:attributes has a reader');
102is($meta->get_attribute('%:attributes')->reader,
103 'get_attribute_map',
104 '... Class::MOP::Class %:attributes\'s a reader is &get_attribute_map');
105
106ok($meta->get_attribute('%:attributes')->has_init_arg, '... Class::MOP::Class %:attributes has a init_arg');
107is($meta->get_attribute('%:attributes')->init_arg,
108 ':attributes',
109 '... Class::MOP::Class %:attributes\'s a init_arg is :attributes');
110
111ok($meta->get_attribute('%:attributes')->has_default, '... Class::MOP::Class %:attributes has a default');
112is_deeply($meta->get_attribute('%:attributes')->default,
113 {},
114 '... Class::MOP::Class %:attributes\'s a default of {}');
115
116ok($meta->get_attribute('$:attribute_metaclass')->has_reader, '... Class::MOP::Class $:attribute_metaclass has a reader');
117is($meta->get_attribute('$:attribute_metaclass')->reader,
118 'attribute_metaclass',
119 '... Class::MOP::Class $:attribute_metaclass\'s a reader is &attribute_metaclass');
120
121ok($meta->get_attribute('$:attribute_metaclass')->has_init_arg, '... Class::MOP::Class $:attribute_metaclass has a init_arg');
122is($meta->get_attribute('$:attribute_metaclass')->init_arg,
123 ':attribute_metaclass',
124 '... Class::MOP::Class $:attribute_metaclass\'s a init_arg is :attribute_metaclass');
125
126ok($meta->get_attribute('$:attribute_metaclass')->has_default, '... Class::MOP::Class $:attribute_metaclass has a default');
127is($meta->get_attribute('$:attribute_metaclass')->default,
128 'Class::MOP::Attribute',
129 '... Class::MOP::Class $:attribute_metaclass\'s a default is Class::MOP:::Attribute');
130
131ok($meta->get_attribute('$:method_metaclass')->has_reader, '... Class::MOP::Class $:method_metaclass has a reader');
132is($meta->get_attribute('$:method_metaclass')->reader,
133 'method_metaclass',
134 '... Class::MOP::Class $:method_metaclass\'s a reader is &method_metaclass');
135
136ok($meta->get_attribute('$:method_metaclass')->has_init_arg, '... Class::MOP::Class $:method_metaclass has a init_arg');
137is($meta->get_attribute('$:method_metaclass')->init_arg,
138 ':method_metaclass',
139 '... Class::MOP::Class $:method_metaclass\'s init_arg is :method_metaclass');
140
141ok($meta->get_attribute('$:method_metaclass')->has_default, '... Class::MOP::Class $:method_metaclass has a default');
142is($meta->get_attribute('$:method_metaclass')->default,
143 'Class::MOP::Method',
144 '... Class::MOP::Class $:method_metaclass\'s a default is Class::MOP:::Method');
145
146# check the values of some of the methods
147
a5eca695 148is($meta->name, 'Class::MOP::Class', '... Class::MOP::Class->name');
149is($meta->version, $Class::MOP::Class::VERSION, '... Class::MOP::Class->version');
150
7b31baf4 151ok($meta->has_package_variable('$VERSION'), '... Class::MOP::Class->has_package_variable($VERSION)');
152is(${$meta->get_package_variable('$VERSION')},
153 $Class::MOP::Class::VERSION,
154 '... Class::MOP::Class->get_package_variable($VERSION)');
155
a5eca695 156is_deeply(
157 [ $meta->superclasses ],
158 [],
159 '... Class::MOP::Class->superclasses == []');
160
161is_deeply(
162 [ $meta->class_precedence_list ],
163 [ 'Class::MOP::Class' ],
164 '... Class::MOP::Class->class_precedence_list == []');
165
7b31baf4 166is($meta->attribute_metaclass, 'Class::MOP::Attribute', '... got the right value for attribute_metaclass');
167is($meta->method_metaclass, 'Class::MOP::Method', '... got the right value for method_metaclass');
168