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