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