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