Commit | Line | Data |
857f87a7 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
efd3d14c |
6 | use Test::More tests => 84; |
857f87a7 |
7 | use Test::Exception; |
8 | |
efd3d14c |
9 | use Class::MOP; |
857f87a7 |
10 | |
11 | { |
12 | package Foo; |
0ac992ee |
13 | |
857f87a7 |
14 | use strict; |
15 | use warnings; |
16 | use metaclass; |
0ac992ee |
17 | |
857f87a7 |
18 | __PACKAGE__->meta->add_attribute('bar'); |
0ac992ee |
19 | |
857f87a7 |
20 | package Bar; |
0ac992ee |
21 | |
857f87a7 |
22 | use strict; |
23 | use warnings; |
24 | use metaclass; |
0ac992ee |
25 | |
857f87a7 |
26 | __PACKAGE__->meta->superclasses('Foo'); |
27 | |
0ac992ee |
28 | __PACKAGE__->meta->add_attribute('baz'); |
29 | |
857f87a7 |
30 | package Baz; |
0ac992ee |
31 | |
857f87a7 |
32 | use strict; |
33 | use warnings; |
34 | use metaclass; |
0ac992ee |
35 | |
857f87a7 |
36 | __PACKAGE__->meta->superclasses('Bar'); |
37 | |
0ac992ee |
38 | __PACKAGE__->meta->add_attribute('bah'); |
857f87a7 |
39 | } |
40 | |
41 | { |
d9586da2 |
42 | my $meta = Foo->meta; |
43 | |
44 | my $transformer; |
45 | lives_ok{ $transformer = $meta->create_immutable_transformer } |
46 | "Created immutable transformer"; |
47 | isa_ok($transformer, 'Class::MOP::Immutable', '... transformer isa Class::MOP::Immutable'); |
48 | my $methods = $transformer->create_methods_for_immutable_metaclass; |
49 | |
50 | my $immutable_metaclass = $transformer->immutable_metaclass; |
51 | is($transformer->metaclass, $meta, '... transformer has correct metaclass'); |
52 | ok($immutable_metaclass->is_anon_class, '... immutable_metaclass is an anonymous class'); |
53 | |
54 | #I don't understand why i need to ->meta here... |
55 | my $obj = $immutable_metaclass->name; |
56 | ok(!$obj->is_mutable, '... immutable_metaclass is not mutable'); |
57 | ok($obj->is_immutable, '... immutable_metaclass is immutable'); |
58 | ok(!$obj->make_immutable, '... immutable_metaclass make_mutable is noop'); |
59 | is($obj->meta, $immutable_metaclass, '... immutable_metaclass meta hack works'); |
60 | |
61 | is_deeply( |
62 | [ $immutable_metaclass->superclasses ], |
11b56828 |
63 | [ Scalar::Util::blessed($meta) ], |
d9586da2 |
64 | '... immutable_metaclass superclasses are correct' |
65 | ); |
66 | ok($immutable_metaclass->has_method('get_mutable_metaclass_name')); |
67 | |
68 | } |
69 | |
70 | { |
857f87a7 |
71 | my $meta = Foo->meta; |
72 | is($meta->name, 'Foo', '... checking the Foo metaclass'); |
0ac992ee |
73 | |
857f87a7 |
74 | ok($meta->is_mutable, '... our class is mutable'); |
0ac992ee |
75 | ok(!$meta->is_immutable, '... our class is not immutable'); |
857f87a7 |
76 | |
119f3a92 |
77 | my $transformer = $meta->get_immutable_transformer; |
78 | |
857f87a7 |
79 | lives_ok { |
80 | $meta->make_immutable(); |
81 | } '... changed Foo to be immutable'; |
0ac992ee |
82 | |
119f3a92 |
83 | is($transformer, $meta->get_immutable_transformer, '... immutable transformer cache works'); |
857f87a7 |
84 | ok(!$meta->make_immutable, '... make immutable now returns nothing'); |
0ac992ee |
85 | |
857f87a7 |
86 | ok(!$meta->is_mutable, '... our class is no longer mutable'); |
0ac992ee |
87 | ok($meta->is_immutable, '... our class is now immutable'); |
857f87a7 |
88 | |
857f87a7 |
89 | isa_ok($meta, 'Class::MOP::Class'); |
0ac992ee |
90 | |
857f87a7 |
91 | dies_ok { $meta->add_method() } '... exception thrown as expected'; |
92 | dies_ok { $meta->alias_method() } '... exception thrown as expected'; |
93 | dies_ok { $meta->remove_method() } '... exception thrown as expected'; |
0ac992ee |
94 | |
857f87a7 |
95 | dies_ok { $meta->add_attribute() } '... exception thrown as expected'; |
96 | dies_ok { $meta->remove_attribute() } '... exception thrown as expected'; |
0ac992ee |
97 | |
58d75218 |
98 | dies_ok { $meta->add_package_symbol() } '... exception thrown as expected'; |
99 | dies_ok { $meta->remove_package_symbol() } '... exception thrown as expected'; |
119f3a92 |
100 | |
5f3efd66 |
101 | lives_ok { $meta->identifier() } '... no exception for get_package_symbol special case'; |
857f87a7 |
102 | |
103 | my @supers; |
104 | lives_ok { |
105 | @supers = $meta->superclasses; |
106 | } '... got the superclasses okay'; |
107 | |
108 | dies_ok { $meta->superclasses([ 'UNIVERSAL' ]) } '... but could not set the superclasses okay'; |
0ac992ee |
109 | |
857f87a7 |
110 | my $meta_instance; |
111 | lives_ok { |
112 | $meta_instance = $meta->get_meta_instance; |
113 | } '... got the meta instance okay'; |
114 | isa_ok($meta_instance, 'Class::MOP::Instance'); |
115 | is($meta_instance, $meta->get_meta_instance, '... and we know it is cached'); |
0ac992ee |
116 | |
857f87a7 |
117 | my @cpl; |
118 | lives_ok { |
119 | @cpl = $meta->class_precedence_list; |
0ac992ee |
120 | } '... got the class precedence list okay'; |
857f87a7 |
121 | is_deeply( |
122 | \@cpl, |
123 | [ 'Foo' ], |
124 | '... we just have ourselves in the class precedence list'); |
0ac992ee |
125 | |
857f87a7 |
126 | my @attributes; |
127 | lives_ok { |
128 | @attributes = $meta->compute_all_applicable_attributes; |
129 | } '... got the attribute list okay'; |
130 | is_deeply( |
131 | \@attributes, |
132 | [ $meta->get_attribute('bar') ], |
133 | '... got the right list of attributes'); |
134 | } |
135 | |
136 | { |
137 | my $meta = Bar->meta; |
0ac992ee |
138 | is($meta->name, 'Bar', '... checking the Bar metaclass'); |
139 | |
857f87a7 |
140 | ok($meta->is_mutable, '... our class is mutable'); |
0ac992ee |
141 | ok(!$meta->is_immutable, '... our class is not immutable'); |
857f87a7 |
142 | |
143 | lives_ok { |
144 | $meta->make_immutable(); |
145 | } '... changed Bar to be immutable'; |
0ac992ee |
146 | |
857f87a7 |
147 | ok(!$meta->make_immutable, '... make immutable now returns nothing'); |
0ac992ee |
148 | |
857f87a7 |
149 | ok(!$meta->is_mutable, '... our class is no longer mutable'); |
0ac992ee |
150 | ok($meta->is_immutable, '... our class is now immutable'); |
857f87a7 |
151 | |
857f87a7 |
152 | isa_ok($meta, 'Class::MOP::Class'); |
0ac992ee |
153 | |
857f87a7 |
154 | dies_ok { $meta->add_method() } '... exception thrown as expected'; |
155 | dies_ok { $meta->alias_method() } '... exception thrown as expected'; |
156 | dies_ok { $meta->remove_method() } '... exception thrown as expected'; |
0ac992ee |
157 | |
857f87a7 |
158 | dies_ok { $meta->add_attribute() } '... exception thrown as expected'; |
159 | dies_ok { $meta->remove_attribute() } '... exception thrown as expected'; |
0ac992ee |
160 | |
58d75218 |
161 | dies_ok { $meta->add_package_symbol() } '... exception thrown as expected'; |
162 | dies_ok { $meta->remove_package_symbol() } '... exception thrown as expected'; |
857f87a7 |
163 | |
164 | my @supers; |
165 | lives_ok { |
166 | @supers = $meta->superclasses; |
167 | } '... got the superclasses okay'; |
168 | |
169 | dies_ok { $meta->superclasses([ 'UNIVERSAL' ]) } '... but could not set the superclasses okay'; |
0ac992ee |
170 | |
857f87a7 |
171 | my $meta_instance; |
172 | lives_ok { |
173 | $meta_instance = $meta->get_meta_instance; |
174 | } '... got the meta instance okay'; |
175 | isa_ok($meta_instance, 'Class::MOP::Instance'); |
0ac992ee |
176 | is($meta_instance, $meta->get_meta_instance, '... and we know it is cached'); |
177 | |
857f87a7 |
178 | my @cpl; |
179 | lives_ok { |
180 | @cpl = $meta->class_precedence_list; |
0ac992ee |
181 | } '... got the class precedence list okay'; |
857f87a7 |
182 | is_deeply( |
183 | \@cpl, |
184 | [ 'Bar', 'Foo'], |
185 | '... we just have ourselves in the class precedence list'); |
0ac992ee |
186 | |
857f87a7 |
187 | my @attributes; |
188 | lives_ok { |
189 | @attributes = $meta->compute_all_applicable_attributes; |
190 | } '... got the attribute list okay'; |
191 | is_deeply( |
192 | [ sort { $a->name cmp $b->name } @attributes ], |
193 | [ Foo->meta->get_attribute('bar'), $meta->get_attribute('baz') ], |
194 | '... got the right list of attributes'); |
195 | } |
196 | |
197 | { |
198 | my $meta = Baz->meta; |
0ac992ee |
199 | is($meta->name, 'Baz', '... checking the Baz metaclass'); |
200 | |
857f87a7 |
201 | ok($meta->is_mutable, '... our class is mutable'); |
0ac992ee |
202 | ok(!$meta->is_immutable, '... our class is not immutable'); |
857f87a7 |
203 | |
204 | lives_ok { |
205 | $meta->make_immutable(); |
206 | } '... changed Baz to be immutable'; |
0ac992ee |
207 | |
857f87a7 |
208 | ok(!$meta->make_immutable, '... make immutable now returns nothing'); |
0ac992ee |
209 | |
857f87a7 |
210 | ok(!$meta->is_mutable, '... our class is no longer mutable'); |
0ac992ee |
211 | ok($meta->is_immutable, '... our class is now immutable'); |
857f87a7 |
212 | |
857f87a7 |
213 | isa_ok($meta, 'Class::MOP::Class'); |
0ac992ee |
214 | |
857f87a7 |
215 | dies_ok { $meta->add_method() } '... exception thrown as expected'; |
216 | dies_ok { $meta->alias_method() } '... exception thrown as expected'; |
217 | dies_ok { $meta->remove_method() } '... exception thrown as expected'; |
0ac992ee |
218 | |
857f87a7 |
219 | dies_ok { $meta->add_attribute() } '... exception thrown as expected'; |
220 | dies_ok { $meta->remove_attribute() } '... exception thrown as expected'; |
0ac992ee |
221 | |
58d75218 |
222 | dies_ok { $meta->add_package_symbol() } '... exception thrown as expected'; |
223 | dies_ok { $meta->remove_package_symbol() } '... exception thrown as expected'; |
857f87a7 |
224 | |
225 | my @supers; |
226 | lives_ok { |
227 | @supers = $meta->superclasses; |
228 | } '... got the superclasses okay'; |
229 | |
230 | dies_ok { $meta->superclasses([ 'UNIVERSAL' ]) } '... but could not set the superclasses okay'; |
0ac992ee |
231 | |
857f87a7 |
232 | my $meta_instance; |
233 | lives_ok { |
234 | $meta_instance = $meta->get_meta_instance; |
235 | } '... got the meta instance okay'; |
236 | isa_ok($meta_instance, 'Class::MOP::Instance'); |
0ac992ee |
237 | is($meta_instance, $meta->get_meta_instance, '... and we know it is cached'); |
238 | |
857f87a7 |
239 | my @cpl; |
240 | lives_ok { |
241 | @cpl = $meta->class_precedence_list; |
0ac992ee |
242 | } '... got the class precedence list okay'; |
857f87a7 |
243 | is_deeply( |
244 | \@cpl, |
245 | [ 'Baz', 'Bar', 'Foo'], |
246 | '... we just have ourselves in the class precedence list'); |
0ac992ee |
247 | |
857f87a7 |
248 | my @attributes; |
249 | lives_ok { |
250 | @attributes = $meta->compute_all_applicable_attributes; |
251 | } '... got the attribute list okay'; |
252 | is_deeply( |
253 | [ sort { $a->name cmp $b->name } @attributes ], |
254 | [ $meta->get_attribute('bah'), Foo->meta->get_attribute('bar'), Bar->meta->get_attribute('baz') ], |
255 | '... got the right list of attributes'); |
256 | } |
257 | |
258 | |