convert all uses of Test::Exception to Test::Fatal.
[gitmo/Class-MOP.git] / t / 070_immutable_metaclass.t
CommitLineData
857f87a7 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
13b8971f 5use Test::Fatal;
857f87a7 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
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
13b8971f 86 ok exception { $meta->add_method() }, '... exception thrown as expected';
87 ok exception { $meta->alias_method() }, '... exception thrown as expected';
88 ok exception { $meta->remove_method() }, '... exception thrown as expected';
0ac992ee 89
13b8971f 90 ok exception { $meta->add_attribute() }, '... exception thrown as expected';
91 ok exception { $meta->remove_attribute() }, '... exception thrown as expected';
0ac992ee 92
13b8971f 93 ok exception { $meta->add_package_symbol() },
249727d6 94 '... exception thrown as expected';
13b8971f 95 ok exception { $meta->remove_package_symbol() },
249727d6 96 '... exception thrown as expected';
119f3a92 97
13b8971f 98 ok ! exception { $meta->identifier() },
249727d6 99 '... no exception for get_package_symbol special case';
857f87a7 100
101 my @supers;
13b8971f 102 ok ! exception {
857f87a7 103 @supers = $meta->superclasses;
13b8971f 104 },
249727d6 105 '... got the superclasses okay';
857f87a7 106
13b8971f 107 ok exception { $meta->superclasses( ['UNIVERSAL'] ) },
249727d6 108 '... but could not set the superclasses okay';
0ac992ee 109
857f87a7 110 my $meta_instance;
13b8971f 111 ok ! exception {
857f87a7 112 $meta_instance = $meta->get_meta_instance;
13b8971f 113 },
249727d6 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;
13b8971f 120 ok ! exception {
857f87a7 121 @cpl = $meta->class_precedence_list;
13b8971f 122 },
249727d6 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;
13b8971f 131 ok ! exception {
2620be77 132 @attributes = $meta->get_all_attributes;
13b8971f 133 },
249727d6 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
13b8971f 149 ok ! exception {
857f87a7 150 $meta->make_immutable();
13b8971f 151 },
249727d6 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
13b8971f 161 ok exception { $meta->add_method() }, '... exception thrown as expected';
162 ok exception { $meta->alias_method() }, '... exception thrown as expected';
163 ok exception { $meta->remove_method() }, '... exception thrown as expected';
0ac992ee 164
13b8971f 165 ok exception { $meta->add_attribute() }, '... exception thrown as expected';
166 ok exception { $meta->remove_attribute() }, '... exception thrown as expected';
0ac992ee 167
13b8971f 168 ok exception { $meta->add_package_symbol() },
249727d6 169 '... exception thrown as expected';
13b8971f 170 ok exception { $meta->remove_package_symbol() },
249727d6 171 '... exception thrown as expected';
857f87a7 172
173 my @supers;
13b8971f 174 ok ! exception {
857f87a7 175 @supers = $meta->superclasses;
13b8971f 176 },
249727d6 177 '... got the superclasses okay';
857f87a7 178
13b8971f 179 ok exception { $meta->superclasses( ['UNIVERSAL'] ) },
249727d6 180 '... but could not set the superclasses okay';
0ac992ee 181
857f87a7 182 my $meta_instance;
13b8971f 183 ok ! exception {
857f87a7 184 $meta_instance = $meta->get_meta_instance;
13b8971f 185 },
249727d6 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;
13b8971f 192 ok ! exception {
857f87a7 193 @cpl = $meta->class_precedence_list;
13b8971f 194 },
249727d6 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;
13b8971f 203 ok ! exception {
2620be77 204 @attributes = $meta->get_all_attributes;
13b8971f 205 },
249727d6 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
13b8971f 221 ok ! exception {
857f87a7 222 $meta->make_immutable();
13b8971f 223 },
249727d6 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
13b8971f 233 ok exception { $meta->add_method() }, '... exception thrown as expected';
234 ok exception { $meta->alias_method() }, '... exception thrown as expected';
235 ok exception { $meta->remove_method() }, '... exception thrown as expected';
0ac992ee 236
13b8971f 237 ok exception { $meta->add_attribute() }, '... exception thrown as expected';
238 ok exception { $meta->remove_attribute() }, '... exception thrown as expected';
0ac992ee 239
13b8971f 240 ok exception { $meta->add_package_symbol() },
249727d6 241 '... exception thrown as expected';
13b8971f 242 ok exception { $meta->remove_package_symbol() },
249727d6 243 '... exception thrown as expected';
857f87a7 244
245 my @supers;
13b8971f 246 ok ! exception {
857f87a7 247 @supers = $meta->superclasses;
13b8971f 248 },
249727d6 249 '... got the superclasses okay';
857f87a7 250
13b8971f 251 ok exception { $meta->superclasses( ['UNIVERSAL'] ) },
249727d6 252 '... but could not set the superclasses okay';
0ac992ee 253
857f87a7 254 my $meta_instance;
13b8971f 255 ok ! exception {
857f87a7 256 $meta_instance = $meta->get_meta_instance;
13b8971f 257 },
249727d6 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;
13b8971f 264 ok ! exception {
857f87a7 265 @cpl = $meta->class_precedence_list;
13b8971f 266 },
249727d6 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;
13b8971f 275 ok ! exception {
2620be77 276 @attributes = $meta->get_all_attributes;
13b8971f 277 },
249727d6 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
316done_testing;