Add warnings when calling CMOP::Class->{compute_all_applicable_method,alias_method}.
[gitmo/Class-MOP.git] / t / 073_make_mutable.t
CommitLineData
0ac992ee 1use strict;
2use warnings;
3
070cb0d6 4use Test::More tests => 101;
0ac992ee 5use Test::Exception;
6
7use Scalar::Util;
8
efd3d14c 9use Class::MOP;
0ac992ee 10
11{
12 package Foo;
13
14 use strict;
15 use warnings;
16 use metaclass;
17
18 __PACKAGE__->meta->add_attribute('bar');
19
20 package Bar;
21
22 use strict;
23 use warnings;
24 use metaclass;
25
26 __PACKAGE__->meta->superclasses('Foo');
27
28 __PACKAGE__->meta->add_attribute('baz');
29
30 package Baz;
31
32 use strict;
33 use warnings;
34 use metaclass;
35
36 __PACKAGE__->meta->superclasses('Bar');
37
38 __PACKAGE__->meta->add_attribute('bah');
39}
40
41{
42 my $meta = Baz->meta;
43 is($meta->name, 'Baz', '... checking the Baz metaclass');
f3938c21 44 my @orig_keys = sort grep { !/^_/ } keys %$meta;
0ac992ee 45
04dd7510 46 lives_ok {$meta->make_immutable; } '... changed Baz to be immutable';
0ac992ee 47 ok(!$meta->is_mutable, '... our class is no longer mutable');
48 ok($meta->is_immutable, '... our class is now immutable');
49 ok(!$meta->make_immutable, '... make immutable now returns nothing');
229910b5 50 ok($meta->get_method_map->{new}, '... inlined constructor created');
46b23b44 51 ok($meta->has_method('new'), '... inlined constructor created for sure');
c1809cb1 52 ok($meta->get_immutable_transformer->inlined_constructor,
53 '... transformer says it did inline the constructor');
0ac992ee 54
04dd7510 55 lives_ok { $meta->make_mutable; } '... changed Baz to be mutable';
0ac992ee 56 ok($meta->is_mutable, '... our class is mutable');
57 ok(!$meta->is_immutable, '... our class is not immutable');
58 ok(!$meta->make_mutable, '... make mutable now returns nothing');
229910b5 59 ok(!$meta->get_method_map->{new}, '... inlined constructor removed');
46b23b44 60 ok(!$meta->has_method('new'), '... inlined constructor removed for sure');
c1809cb1 61 ok(!$meta->get_immutable_transformer->inlined_constructor,
62 '... transformer says it did not inline the constructor');
0ac992ee 63
f3938c21 64 my @new_keys = sort grep { !/^_/ } keys %$meta;
0ac992ee 65 is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
66
67 isa_ok($meta, 'Class::MOP::Class', '... Baz->meta isa Class::MOP::Class');
68
69 ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
70 is( Baz->xyz, 'xxx', '... method xyz works');
46b23b44 71
0ac992ee 72 ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute');
73 ok(Baz->can('fickle'), '... Baz can fickle');
74 ok($meta->remove_attribute('fickle'), '... removed attribute');
75
76 my $reef = \ 'reef';
77 ok($meta->add_package_symbol('$ref', $reef), '... added package symbol');
78 is($meta->get_package_symbol('$ref'), $reef, '... values match');
79 lives_ok { $meta->remove_package_symbol('$ref') } '... removed it';
80 isnt($meta->get_package_symbol('$ref'), $reef, '... values match');
81
82 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
83 ok( $meta->superclasses('Foo'), '... set the superclasses');
84 is_deeply(['Foo'], [$meta->superclasses], '... set the superclasses okay');
85 ok( $meta->superclasses( @supers ), '... reset superclasses');
86 is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay');
87
88 ok( $meta->$_ , "... ${_} works")
89 for qw(get_meta_instance compute_all_applicable_attributes
90 class_precedence_list get_method_map );
229910b5 91
92 lives_ok {$meta->make_immutable; } '... changed Baz to be immutable again';
93 ok($meta->get_method_map->{new}, '... inlined constructor recreated');
0ac992ee 94}
95
96{
97 my $meta = Baz->meta;
98
99 lives_ok { $meta->make_immutable() } 'Changed Baz to be immutable';
100 lives_ok { $meta->make_mutable() } '... changed Baz to be mutable';
101 lives_ok { $meta->make_immutable() } '... changed Baz to be immutable';
102
103 dies_ok{ $meta->add_method('xyz', sub{'xxx'}) } '... exception thrown as expected';
0ac992ee 104
105 dies_ok {
106 $meta->add_attribute('fickle', accessor => 'fickle')
107 } '... exception thrown as expected';
108 dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
109
110 my $reef = \ 'reef';
111 dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected';
112 dies_ok { $meta->remove_package_symbol('$ref') } '... exception thrown as expected';
113
114 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
115 dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
116
117 ok( $meta->$_ , "... ${_} works")
118 for qw(get_meta_instance compute_all_applicable_attributes
119 class_precedence_list get_method_map );
120}
121
0ac992ee 122{
123
d9586da2 124 ok(Baz->meta->is_immutable, 'Superclass is immutable');
0ac992ee 125 my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
f3938c21 126 my @orig_keys = sort grep { !/^_/ } keys %$meta;
070cb0d6 127 my @orig_meths = sort { $a->name cmp $b->name }
128 $meta->get_all_methods;
0ac992ee 129 ok($meta->is_anon_class, 'We have an anon metaclass');
d9586da2 130 ok($meta->is_mutable, '... our anon class is mutable');
131 ok(!$meta->is_immutable, '... our anon class is not immutable');
132
0ac992ee 133 lives_ok {$meta->make_immutable(
134 inline_accessor => 1,
135 inline_destructor => 0,
136 inline_constructor => 1,
137 )
138 } '... changed class to be immutable';
139 ok(!$meta->is_mutable, '... our class is no longer mutable');
140 ok($meta->is_immutable, '... our class is now immutable');
141 ok(!$meta->make_immutable, '... make immutable now returns nothing');
142
143 lives_ok { $meta->make_mutable } '... changed Baz to be mutable';
144 ok($meta->is_mutable, '... our class is mutable');
145 ok(!$meta->is_immutable, '... our class is not immutable');
146 ok(!$meta->make_mutable, '... make mutable now returns nothing');
147 ok($meta->is_anon_class, '... still marked as an anon class');
148 my $instance = $meta->new_object;
149
f3938c21 150 my @new_keys = sort grep { !/^_/ } keys %$meta;
070cb0d6 151 my @new_meths = sort { $a->name cmp $b->name }
152 $meta->get_all_methods;
0ac992ee 153 is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
154 is_deeply(\@orig_meths, \@new_meths, '... no straneous methods');
155
156 isa_ok($meta, 'Class::MOP::Class', '... Anon class isa Class::MOP::Class');
157
158 ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
159 is( $instance->xyz , 'xxx', '... method xyz works');
0ac992ee 160 ok( $meta->remove_method('xyz'), '... removed method');
0ac992ee 161
162 ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute');
163 ok($instance->can('fickle'), '... instance can fickle');
164 ok($meta->remove_attribute('fickle'), '... removed attribute');
165
166 my $reef = \ 'reef';
167 ok($meta->add_package_symbol('$ref', $reef), '... added package symbol');
168 is($meta->get_package_symbol('$ref'), $reef, '... values match');
169 lives_ok { $meta->remove_package_symbol('$ref') } '... removed it';
170 isnt($meta->get_package_symbol('$ref'), $reef, '... values match');
171
172 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
173 ok( $meta->superclasses('Foo'), '... set the superclasses');
174 is_deeply(['Foo'], [$meta->superclasses], '... set the superclasses okay');
175 ok( $meta->superclasses( @supers ), '... reset superclasses');
176 is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay');
177
178 ok( $meta->$_ , "... ${_} works")
179 for qw(get_meta_instance compute_all_applicable_attributes
180 class_precedence_list get_method_map );
181};
182
183
184#rerun the same tests on an anon class.. just cause we can.
185{
186 my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
187
188 lives_ok {$meta->make_immutable(
189 inline_accessor => 1,
190 inline_destructor => 0,
191 inline_constructor => 1,
192 )
193 } '... changed class to be immutable';
194 lives_ok { $meta->make_mutable() } '... changed class to be mutable';
195 lives_ok {$meta->make_immutable } '... changed class to be immutable';
196
197 dies_ok{ $meta->add_method('xyz', sub{'xxx'}) } '... exception thrown as expected';
0ac992ee 198
199 dies_ok {
200 $meta->add_attribute('fickle', accessor => 'fickle')
201 } '... exception thrown as expected';
202 dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
203
204 my $reef = \ 'reef';
205 dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected';
206 dies_ok { $meta->remove_package_symbol('$ref') } '... exception thrown as expected';
207
208 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
209 dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
210
211 ok( $meta->$_ , "... ${_} works")
212 for qw(get_meta_instance compute_all_applicable_attributes
213 class_precedence_list get_method_map );
214}
d89108ab 215
216{
217 Foo->meta->make_immutable;
218 Bar->meta->make_immutable;
219 Bar->meta->make_mutable;
220
221 isnt( Foo->meta->get_immutable_transformer, Bar->meta->get_immutable_transformer,
222 'Foo and Bar should have different immutable transformer objects' );
223}