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