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