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