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