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