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