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