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