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