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