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