simplify more stuff
[gitmo/Class-MOP.git] / t / 070_immutable_metaclass.t
CommitLineData
857f87a7 1use strict;
2use warnings;
3
86a4d873 4use Test::More;
871e9eb5 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
871e9eb5 86 isnt( exception { $meta->add_method() }, undef, '... exception thrown as expected' );
87 isnt( exception { $meta->alias_method() }, undef, '... exception thrown as expected' );
88 isnt( exception { $meta->remove_method() }, undef, '... exception thrown as expected' );
0ac992ee 89
871e9eb5 90 isnt( exception { $meta->add_attribute() }, undef, '... exception thrown as expected' );
91 isnt( exception { $meta->remove_attribute() }, undef, '... exception thrown as expected' );
0ac992ee 92
871e9eb5 93 isnt( exception { $meta->add_package_symbol() }, undef, '... exception thrown as expected' );
94 isnt( exception { $meta->remove_package_symbol() }, undef, '... exception thrown as expected' );
119f3a92 95
871e9eb5 96 is( exception { $meta->identifier() }, undef, '... no exception for get_package_symbol special case' );
857f87a7 97
98 my @supers;
871e9eb5 99 is( exception {
857f87a7 100 @supers = $meta->superclasses;
871e9eb5 101 }, undef, '... got the superclasses okay' );
857f87a7 102
871e9eb5 103 isnt( exception { $meta->superclasses( ['UNIVERSAL'] ) }, undef, '... but could not set the superclasses okay' );
0ac992ee 104
857f87a7 105 my $meta_instance;
871e9eb5 106 is( exception {
857f87a7 107 $meta_instance = $meta->get_meta_instance;
871e9eb5 108 }, undef, '... got the meta instance okay' );
249727d6 109 isa_ok( $meta_instance, 'Class::MOP::Instance' );
110 is( $meta_instance, $meta->get_meta_instance,
111 '... and we know it is cached' );
0ac992ee 112
857f87a7 113 my @cpl;
871e9eb5 114 is( exception {
857f87a7 115 @cpl = $meta->class_precedence_list;
871e9eb5 116 }, undef, '... got the class precedence list okay' );
857f87a7 117 is_deeply(
249727d6 118 \@cpl,
119 ['Foo'],
120 '... we just have ourselves in the class precedence list'
121 );
0ac992ee 122
857f87a7 123 my @attributes;
871e9eb5 124 is( exception {
2620be77 125 @attributes = $meta->get_all_attributes;
871e9eb5 126 }, undef, '... got the attribute list okay' );
857f87a7 127 is_deeply(
249727d6 128 \@attributes,
129 [ $meta->get_attribute('bar') ],
130 '... got the right list of attributes'
131 );
857f87a7 132}
133
134{
135 my $meta = Bar->meta;
249727d6 136 is( $meta->name, 'Bar', '... checking the Bar metaclass' );
0ac992ee 137
249727d6 138 ok( $meta->is_mutable, '... our class is mutable' );
139 ok( !$meta->is_immutable, '... our class is not immutable' );
857f87a7 140
871e9eb5 141 is( exception {
857f87a7 142 $meta->make_immutable();
871e9eb5 143 }, undef, '... changed Bar to be immutable' );
0ac992ee 144
249727d6 145 ok( !$meta->make_immutable, '... make immutable now returns nothing' );
0ac992ee 146
249727d6 147 ok( !$meta->is_mutable, '... our class is no longer mutable' );
148 ok( $meta->is_immutable, '... our class is now immutable' );
857f87a7 149
249727d6 150 isa_ok( $meta, 'Class::MOP::Class' );
0ac992ee 151
871e9eb5 152 isnt( exception { $meta->add_method() }, undef, '... exception thrown as expected' );
153 isnt( exception { $meta->alias_method() }, undef, '... exception thrown as expected' );
154 isnt( exception { $meta->remove_method() }, undef, '... exception thrown as expected' );
0ac992ee 155
871e9eb5 156 isnt( exception { $meta->add_attribute() }, undef, '... exception thrown as expected' );
157 isnt( exception { $meta->remove_attribute() }, undef, '... exception thrown as expected' );
0ac992ee 158
871e9eb5 159 isnt( exception { $meta->add_package_symbol() }, undef, '... exception thrown as expected' );
160 isnt( exception { $meta->remove_package_symbol() }, undef, '... exception thrown as expected' );
857f87a7 161
162 my @supers;
871e9eb5 163 is( exception {
857f87a7 164 @supers = $meta->superclasses;
871e9eb5 165 }, undef, '... got the superclasses okay' );
857f87a7 166
871e9eb5 167 isnt( exception { $meta->superclasses( ['UNIVERSAL'] ) }, undef, '... but could not set the superclasses okay' );
0ac992ee 168
857f87a7 169 my $meta_instance;
871e9eb5 170 is( exception {
857f87a7 171 $meta_instance = $meta->get_meta_instance;
871e9eb5 172 }, undef, '... got the meta instance okay' );
249727d6 173 isa_ok( $meta_instance, 'Class::MOP::Instance' );
174 is( $meta_instance, $meta->get_meta_instance,
175 '... and we know it is cached' );
0ac992ee 176
857f87a7 177 my @cpl;
871e9eb5 178 is( exception {
857f87a7 179 @cpl = $meta->class_precedence_list;
871e9eb5 180 }, undef, '... 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;
871e9eb5 188 is( exception {
2620be77 189 @attributes = $meta->get_all_attributes;
871e9eb5 190 }, undef, '... 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
871e9eb5 205 is( exception {
857f87a7 206 $meta->make_immutable();
871e9eb5 207 }, undef, '... changed Baz to be immutable' );
0ac992ee 208
249727d6 209 ok( !$meta->make_immutable, '... make immutable now returns nothing' );
0ac992ee 210
249727d6 211 ok( !$meta->is_mutable, '... our class is no longer mutable' );
212 ok( $meta->is_immutable, '... our class is now immutable' );
857f87a7 213
249727d6 214 isa_ok( $meta, 'Class::MOP::Class' );
0ac992ee 215
871e9eb5 216 isnt( exception { $meta->add_method() }, undef, '... exception thrown as expected' );
217 isnt( exception { $meta->alias_method() }, undef, '... exception thrown as expected' );
218 isnt( exception { $meta->remove_method() }, undef, '... exception thrown as expected' );
0ac992ee 219
871e9eb5 220 isnt( exception { $meta->add_attribute() }, undef, '... exception thrown as expected' );
221 isnt( exception { $meta->remove_attribute() }, undef, '... exception thrown as expected' );
0ac992ee 222
871e9eb5 223 isnt( exception { $meta->add_package_symbol() }, undef, '... exception thrown as expected' );
224 isnt( exception { $meta->remove_package_symbol() }, undef, '... exception thrown as expected' );
857f87a7 225
226 my @supers;
871e9eb5 227 is( exception {
857f87a7 228 @supers = $meta->superclasses;
871e9eb5 229 }, undef, '... got the superclasses okay' );
857f87a7 230
871e9eb5 231 isnt( exception { $meta->superclasses( ['UNIVERSAL'] ) }, undef, '... but could not set the superclasses okay' );
0ac992ee 232
857f87a7 233 my $meta_instance;
871e9eb5 234 is( exception {
857f87a7 235 $meta_instance = $meta->get_meta_instance;
871e9eb5 236 }, undef, '... got the meta instance okay' );
249727d6 237 isa_ok( $meta_instance, 'Class::MOP::Instance' );
238 is( $meta_instance, $meta->get_meta_instance,
239 '... and we know it is cached' );
0ac992ee 240
857f87a7 241 my @cpl;
871e9eb5 242 is( exception {
857f87a7 243 @cpl = $meta->class_precedence_list;
871e9eb5 244 }, undef, '... got the class precedence list okay' );
857f87a7 245 is_deeply(
249727d6 246 \@cpl,
247 [ 'Baz', 'Bar', 'Foo' ],
248 '... we just have ourselves in the class precedence list'
249 );
0ac992ee 250
857f87a7 251 my @attributes;
871e9eb5 252 is( exception {
2620be77 253 @attributes = $meta->get_all_attributes;
871e9eb5 254 }, undef, '... got the attribute list okay' );
857f87a7 255 is_deeply(
249727d6 256 [ sort { $a->name cmp $b->name } @attributes ],
257 [
258 $meta->get_attribute('bah'), Foo->meta->get_attribute('bar'),
259 Bar->meta->get_attribute('baz')
260 ],
261 '... got the right list of attributes'
262 );
857f87a7 263}
dd9dc741 264
265# This test probably needs to go last since it will muck up the Foo class
266{
267 my $meta = Foo->meta;
268
269 $meta->make_mutable;
270 $meta->make_immutable(
271 inline_accessors => 0,
272 inline_constructor => 0,
273 constructor_name => 'newer',
274 );
275
276 is_deeply(
277 { $meta->immutable_options },
278 {
279 inline_accessors => 0,
280 inline_constructor => 0,
281 inline_destructor => 0,
282 debug => 0,
283 immutable_trait => 'Class::MOP::Class::Immutable::Trait',
284 constructor_name => 'newer',
285 constructor_class => 'Class::MOP::Method::Constructor',
286 destructor_class => undef,
287 },
288 'custom immutable_options are returned by immutable_options accessor'
289 );
290}
86a4d873 291
292done_testing;