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