Commit | Line | Data |
857f87a7 |
1 | use strict; |
2 | use warnings; |
3 | |
86a4d873 |
4 | use Test::More; |
857f87a7 |
5 | use Test::Exception; |
6 | |
efd3d14c |
7 | use Class::MOP; |
857f87a7 |
8 | |
9 | { |
10 | package Foo; |
0ac992ee |
11 | |
857f87a7 |
12 | use strict; |
13 | use warnings; |
14 | use metaclass; |
0ac992ee |
15 | |
857f87a7 |
16 | __PACKAGE__->meta->add_attribute('bar'); |
0ac992ee |
17 | |
857f87a7 |
18 | package Bar; |
0ac992ee |
19 | |
857f87a7 |
20 | use strict; |
21 | use warnings; |
22 | use metaclass; |
0ac992ee |
23 | |
857f87a7 |
24 | __PACKAGE__->meta->superclasses('Foo'); |
25 | |
0ac992ee |
26 | __PACKAGE__->meta->add_attribute('baz'); |
27 | |
857f87a7 |
28 | package Baz; |
0ac992ee |
29 | |
857f87a7 |
30 | use strict; |
31 | use warnings; |
32 | use metaclass; |
0ac992ee |
33 | |
857f87a7 |
34 | __PACKAGE__->meta->superclasses('Bar'); |
35 | |
0ac992ee |
36 | __PACKAGE__->meta->add_attribute('bah'); |
857f87a7 |
37 | } |
38 | |
39 | { |
249727d6 |
40 | my $meta = Foo->meta; |
44d6ea77 |
41 | my $original_metaclass_name = ref $meta; |
42 | |
07a05583 |
43 | is_deeply( |
44 | { $meta->immutable_options }, {}, |
45 | 'immutable_options is empty before a class is made_immutable' |
46 | ); |
47 | |
44d6ea77 |
48 | $meta->make_immutable; |
d9586da2 |
49 | |
e1c7a1b7 |
50 | my $immutable_metaclass = $meta->_immutable_metaclass->meta; |
249727d6 |
51 | |
d7532ce0 |
52 | my $immutable_class_name = $immutable_metaclass->name; |
f5d08022 |
53 | |
d7532ce0 |
54 | ok( !$immutable_class_name->is_mutable, '... immutable_metaclass is not mutable' ); |
55 | ok( $immutable_class_name->is_immutable, '... immutable_metaclass is immutable' ); |
56 | is( $immutable_class_name->meta, $immutable_metaclass, |
249727d6 |
57 | '... immutable_metaclass meta hack works' ); |
58 | |
07a05583 |
59 | is_deeply( |
60 | { $meta->immutable_options }, |
61 | { |
62 | inline_accessors => 1, |
63 | inline_constructor => 1, |
64 | inline_destructor => 0, |
65 | debug => 0, |
66 | immutable_trait => 'Class::MOP::Class::Immutable::Trait', |
67 | constructor_name => 'new', |
68 | constructor_class => 'Class::MOP::Method::Constructor', |
69 | destructor_class => undef, |
70 | }, |
71 | 'immutable_options is empty before a class is made_immutable' |
72 | ); |
73 | |
f5d08022 |
74 | isa_ok( $meta, "Class::MOP::Class" ); |
d9586da2 |
75 | } |
76 | |
77 | { |
857f87a7 |
78 | my $meta = Foo->meta; |
249727d6 |
79 | is( $meta->name, 'Foo', '... checking the Foo metaclass' ); |
0ac992ee |
80 | |
44d6ea77 |
81 | ok( !$meta->is_mutable, '... our class is not mutable' ); |
82 | ok( $meta->is_immutable, '... our class is immutable' ); |
857f87a7 |
83 | |
249727d6 |
84 | isa_ok( $meta, 'Class::MOP::Class' ); |
0ac992ee |
85 | |
249727d6 |
86 | dies_ok { $meta->add_method() } '... exception thrown as expected'; |
87 | dies_ok { $meta->alias_method() } '... exception thrown as expected'; |
857f87a7 |
88 | dies_ok { $meta->remove_method() } '... exception thrown as expected'; |
0ac992ee |
89 | |
249727d6 |
90 | dies_ok { $meta->add_attribute() } '... exception thrown as expected'; |
857f87a7 |
91 | dies_ok { $meta->remove_attribute() } '... exception thrown as expected'; |
0ac992ee |
92 | |
249727d6 |
93 | dies_ok { $meta->add_package_symbol() } |
94 | '... exception thrown as expected'; |
95 | dies_ok { $meta->remove_package_symbol() } |
96 | '... exception thrown as expected'; |
119f3a92 |
97 | |
249727d6 |
98 | lives_ok { $meta->identifier() } |
99 | '... no exception for get_package_symbol special case'; |
857f87a7 |
100 | |
101 | my @supers; |
102 | lives_ok { |
103 | @supers = $meta->superclasses; |
249727d6 |
104 | } |
105 | '... got the superclasses okay'; |
857f87a7 |
106 | |
249727d6 |
107 | dies_ok { $meta->superclasses( ['UNIVERSAL'] ) } |
108 | '... but could not set the superclasses okay'; |
0ac992ee |
109 | |
857f87a7 |
110 | my $meta_instance; |
111 | lives_ok { |
112 | $meta_instance = $meta->get_meta_instance; |
249727d6 |
113 | } |
114 | '... got the meta instance okay'; |
115 | isa_ok( $meta_instance, 'Class::MOP::Instance' ); |
116 | is( $meta_instance, $meta->get_meta_instance, |
117 | '... and we know it is cached' ); |
0ac992ee |
118 | |
857f87a7 |
119 | my @cpl; |
120 | lives_ok { |
121 | @cpl = $meta->class_precedence_list; |
249727d6 |
122 | } |
123 | '... got the class precedence list okay'; |
857f87a7 |
124 | is_deeply( |
249727d6 |
125 | \@cpl, |
126 | ['Foo'], |
127 | '... we just have ourselves in the class precedence list' |
128 | ); |
0ac992ee |
129 | |
857f87a7 |
130 | my @attributes; |
131 | lives_ok { |
2620be77 |
132 | @attributes = $meta->get_all_attributes; |
249727d6 |
133 | } |
134 | '... got the attribute list okay'; |
857f87a7 |
135 | is_deeply( |
249727d6 |
136 | \@attributes, |
137 | [ $meta->get_attribute('bar') ], |
138 | '... got the right list of attributes' |
139 | ); |
857f87a7 |
140 | } |
141 | |
142 | { |
143 | my $meta = Bar->meta; |
249727d6 |
144 | is( $meta->name, 'Bar', '... checking the Bar metaclass' ); |
0ac992ee |
145 | |
249727d6 |
146 | ok( $meta->is_mutable, '... our class is mutable' ); |
147 | ok( !$meta->is_immutable, '... our class is not immutable' ); |
857f87a7 |
148 | |
149 | lives_ok { |
150 | $meta->make_immutable(); |
249727d6 |
151 | } |
152 | '... changed Bar to be immutable'; |
0ac992ee |
153 | |
249727d6 |
154 | ok( !$meta->make_immutable, '... make immutable now returns nothing' ); |
0ac992ee |
155 | |
249727d6 |
156 | ok( !$meta->is_mutable, '... our class is no longer mutable' ); |
157 | ok( $meta->is_immutable, '... our class is now immutable' ); |
857f87a7 |
158 | |
249727d6 |
159 | isa_ok( $meta, 'Class::MOP::Class' ); |
0ac992ee |
160 | |
249727d6 |
161 | dies_ok { $meta->add_method() } '... exception thrown as expected'; |
162 | dies_ok { $meta->alias_method() } '... exception thrown as expected'; |
857f87a7 |
163 | dies_ok { $meta->remove_method() } '... exception thrown as expected'; |
0ac992ee |
164 | |
249727d6 |
165 | dies_ok { $meta->add_attribute() } '... exception thrown as expected'; |
857f87a7 |
166 | dies_ok { $meta->remove_attribute() } '... exception thrown as expected'; |
0ac992ee |
167 | |
249727d6 |
168 | dies_ok { $meta->add_package_symbol() } |
169 | '... exception thrown as expected'; |
170 | dies_ok { $meta->remove_package_symbol() } |
171 | '... exception thrown as expected'; |
857f87a7 |
172 | |
173 | my @supers; |
174 | lives_ok { |
175 | @supers = $meta->superclasses; |
249727d6 |
176 | } |
177 | '... got the superclasses okay'; |
857f87a7 |
178 | |
249727d6 |
179 | dies_ok { $meta->superclasses( ['UNIVERSAL'] ) } |
180 | '... but could not set the superclasses okay'; |
0ac992ee |
181 | |
857f87a7 |
182 | my $meta_instance; |
183 | lives_ok { |
184 | $meta_instance = $meta->get_meta_instance; |
249727d6 |
185 | } |
186 | '... got the meta instance okay'; |
187 | isa_ok( $meta_instance, 'Class::MOP::Instance' ); |
188 | is( $meta_instance, $meta->get_meta_instance, |
189 | '... and we know it is cached' ); |
0ac992ee |
190 | |
857f87a7 |
191 | my @cpl; |
192 | lives_ok { |
193 | @cpl = $meta->class_precedence_list; |
249727d6 |
194 | } |
195 | '... got the class precedence list okay'; |
857f87a7 |
196 | is_deeply( |
249727d6 |
197 | \@cpl, |
198 | [ 'Bar', 'Foo' ], |
199 | '... we just have ourselves in the class precedence list' |
200 | ); |
0ac992ee |
201 | |
857f87a7 |
202 | my @attributes; |
203 | lives_ok { |
2620be77 |
204 | @attributes = $meta->get_all_attributes; |
249727d6 |
205 | } |
206 | '... got the attribute list okay'; |
857f87a7 |
207 | is_deeply( |
249727d6 |
208 | [ sort { $a->name cmp $b->name } @attributes ], |
209 | [ Foo->meta->get_attribute('bar'), $meta->get_attribute('baz') ], |
210 | '... got the right list of attributes' |
211 | ); |
857f87a7 |
212 | } |
213 | |
214 | { |
215 | my $meta = Baz->meta; |
249727d6 |
216 | is( $meta->name, 'Baz', '... checking the Baz metaclass' ); |
0ac992ee |
217 | |
249727d6 |
218 | ok( $meta->is_mutable, '... our class is mutable' ); |
219 | ok( !$meta->is_immutable, '... our class is not immutable' ); |
857f87a7 |
220 | |
221 | lives_ok { |
222 | $meta->make_immutable(); |
249727d6 |
223 | } |
224 | '... changed Baz to be immutable'; |
0ac992ee |
225 | |
249727d6 |
226 | ok( !$meta->make_immutable, '... make immutable now returns nothing' ); |
0ac992ee |
227 | |
249727d6 |
228 | ok( !$meta->is_mutable, '... our class is no longer mutable' ); |
229 | ok( $meta->is_immutable, '... our class is now immutable' ); |
857f87a7 |
230 | |
249727d6 |
231 | isa_ok( $meta, 'Class::MOP::Class' ); |
0ac992ee |
232 | |
249727d6 |
233 | dies_ok { $meta->add_method() } '... exception thrown as expected'; |
234 | dies_ok { $meta->alias_method() } '... exception thrown as expected'; |
857f87a7 |
235 | dies_ok { $meta->remove_method() } '... exception thrown as expected'; |
0ac992ee |
236 | |
249727d6 |
237 | dies_ok { $meta->add_attribute() } '... exception thrown as expected'; |
857f87a7 |
238 | dies_ok { $meta->remove_attribute() } '... exception thrown as expected'; |
0ac992ee |
239 | |
249727d6 |
240 | dies_ok { $meta->add_package_symbol() } |
241 | '... exception thrown as expected'; |
242 | dies_ok { $meta->remove_package_symbol() } |
243 | '... exception thrown as expected'; |
857f87a7 |
244 | |
245 | my @supers; |
246 | lives_ok { |
247 | @supers = $meta->superclasses; |
249727d6 |
248 | } |
249 | '... got the superclasses okay'; |
857f87a7 |
250 | |
249727d6 |
251 | dies_ok { $meta->superclasses( ['UNIVERSAL'] ) } |
252 | '... but could not set the superclasses okay'; |
0ac992ee |
253 | |
857f87a7 |
254 | my $meta_instance; |
255 | lives_ok { |
256 | $meta_instance = $meta->get_meta_instance; |
249727d6 |
257 | } |
258 | '... got the meta instance okay'; |
259 | isa_ok( $meta_instance, 'Class::MOP::Instance' ); |
260 | is( $meta_instance, $meta->get_meta_instance, |
261 | '... and we know it is cached' ); |
0ac992ee |
262 | |
857f87a7 |
263 | my @cpl; |
264 | lives_ok { |
265 | @cpl = $meta->class_precedence_list; |
249727d6 |
266 | } |
267 | '... got the class precedence list okay'; |
857f87a7 |
268 | is_deeply( |
249727d6 |
269 | \@cpl, |
270 | [ 'Baz', 'Bar', 'Foo' ], |
271 | '... we just have ourselves in the class precedence list' |
272 | ); |
0ac992ee |
273 | |
857f87a7 |
274 | my @attributes; |
275 | lives_ok { |
2620be77 |
276 | @attributes = $meta->get_all_attributes; |
249727d6 |
277 | } |
278 | '... got the attribute list okay'; |
857f87a7 |
279 | is_deeply( |
249727d6 |
280 | [ sort { $a->name cmp $b->name } @attributes ], |
281 | [ |
282 | $meta->get_attribute('bah'), Foo->meta->get_attribute('bar'), |
283 | Bar->meta->get_attribute('baz') |
284 | ], |
285 | '... got the right list of attributes' |
286 | ); |
857f87a7 |
287 | } |
dd9dc741 |
288 | |
289 | # This test probably needs to go last since it will muck up the Foo class |
290 | { |
291 | my $meta = Foo->meta; |
292 | |
293 | $meta->make_mutable; |
294 | $meta->make_immutable( |
295 | inline_accessors => 0, |
296 | inline_constructor => 0, |
297 | constructor_name => 'newer', |
298 | ); |
299 | |
300 | is_deeply( |
301 | { $meta->immutable_options }, |
302 | { |
303 | inline_accessors => 0, |
304 | inline_constructor => 0, |
305 | inline_destructor => 0, |
306 | debug => 0, |
307 | immutable_trait => 'Class::MOP::Class::Immutable::Trait', |
308 | constructor_name => 'newer', |
309 | constructor_class => 'Class::MOP::Method::Constructor', |
310 | destructor_class => undef, |
311 | }, |
312 | 'custom immutable_options are returned by immutable_options accessor' |
313 | ); |
314 | } |
86a4d873 |
315 | |
316 | done_testing; |