Commit | Line | Data |
0ac992ee |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
d89108ab |
6 | use Test::More tests => 114; |
0ac992ee |
7 | use Test::Exception; |
8 | |
9 | use Scalar::Util; |
10 | |
efd3d14c |
11 | use 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'); |
c1809cb1 |
54 | ok($meta->get_immutable_transformer->inlined_constructor, |
55 | '... transformer says it did inline the constructor'); |
0ac992ee |
56 | |
04dd7510 |
57 | lives_ok { $meta->make_mutable; } '... changed Baz to be mutable'; |
0ac992ee |
58 | ok($meta->is_mutable, '... our class is mutable'); |
59 | ok(!$meta->is_immutable, '... our class is not immutable'); |
60 | ok(!$meta->make_mutable, '... make mutable now returns nothing'); |
229910b5 |
61 | ok(!$meta->get_method_map->{new}, '... inlined constructor removed'); |
46b23b44 |
62 | ok(!$meta->has_method('new'), '... inlined constructor removed for sure'); |
c1809cb1 |
63 | ok(!$meta->get_immutable_transformer->inlined_constructor, |
64 | '... transformer says it did not inline the constructor'); |
0ac992ee |
65 | |
f3938c21 |
66 | my @new_keys = sort grep { !/^_/ } keys %$meta; |
0ac992ee |
67 | is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys'); |
68 | |
69 | isa_ok($meta, 'Class::MOP::Class', '... Baz->meta isa Class::MOP::Class'); |
70 | |
71 | ok( $meta->add_method('xyz', sub{'xxx'}), '... added method'); |
72 | is( Baz->xyz, 'xxx', '... method xyz works'); |
46b23b44 |
73 | |
74 | ok(! $meta->has_method('zxy') ,'... we dont have the aliased method yet'); |
0ac992ee |
75 | ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method'); |
91b73829 |
76 | ok( $meta->has_method('zxy') ,'... the aliased method does register'); |
0ac992ee |
77 | is( Baz->zxy, 'xxx', '... method zxy works'); |
78 | ok( $meta->remove_method('xyz'), '... removed method'); |
91b73829 |
79 | ok( $meta->remove_method('zxy'), '... removed aliased method'); |
0ac992ee |
80 | |
81 | ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute'); |
82 | ok(Baz->can('fickle'), '... Baz can fickle'); |
83 | ok($meta->remove_attribute('fickle'), '... removed attribute'); |
84 | |
85 | my $reef = \ 'reef'; |
86 | ok($meta->add_package_symbol('$ref', $reef), '... added package symbol'); |
87 | is($meta->get_package_symbol('$ref'), $reef, '... values match'); |
88 | lives_ok { $meta->remove_package_symbol('$ref') } '... removed it'; |
89 | isnt($meta->get_package_symbol('$ref'), $reef, '... values match'); |
90 | |
91 | ok( my @supers = $meta->superclasses, '... got the superclasses okay'); |
92 | ok( $meta->superclasses('Foo'), '... set the superclasses'); |
93 | is_deeply(['Foo'], [$meta->superclasses], '... set the superclasses okay'); |
94 | ok( $meta->superclasses( @supers ), '... reset superclasses'); |
95 | is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay'); |
96 | |
97 | ok( $meta->$_ , "... ${_} works") |
98 | for qw(get_meta_instance compute_all_applicable_attributes |
99 | class_precedence_list get_method_map ); |
229910b5 |
100 | |
101 | lives_ok {$meta->make_immutable; } '... changed Baz to be immutable again'; |
102 | ok($meta->get_method_map->{new}, '... inlined constructor recreated'); |
0ac992ee |
103 | } |
104 | |
105 | { |
106 | my $meta = Baz->meta; |
107 | |
108 | lives_ok { $meta->make_immutable() } 'Changed Baz to be immutable'; |
109 | lives_ok { $meta->make_mutable() } '... changed Baz to be mutable'; |
110 | lives_ok { $meta->make_immutable() } '... changed Baz to be immutable'; |
111 | |
112 | dies_ok{ $meta->add_method('xyz', sub{'xxx'}) } '... exception thrown as expected'; |
113 | dies_ok{ $meta->alias_method('zxy',sub{'xxx'}) } '... exception thrown as expected'; |
114 | dies_ok{ $meta->remove_method('zxy') } '... exception thrown as expected'; |
115 | |
116 | dies_ok { |
117 | $meta->add_attribute('fickle', accessor => 'fickle') |
118 | } '... exception thrown as expected'; |
119 | dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected'; |
120 | |
121 | my $reef = \ 'reef'; |
122 | dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected'; |
123 | dies_ok { $meta->remove_package_symbol('$ref') } '... exception thrown as expected'; |
124 | |
125 | ok( my @supers = $meta->superclasses, '... got the superclasses okay'); |
126 | dies_ok { $meta->superclasses('Foo') } '... set the superclasses'; |
127 | |
128 | ok( $meta->$_ , "... ${_} works") |
129 | for qw(get_meta_instance compute_all_applicable_attributes |
130 | class_precedence_list get_method_map ); |
131 | } |
132 | |
0ac992ee |
133 | { |
134 | |
d9586da2 |
135 | ok(Baz->meta->is_immutable, 'Superclass is immutable'); |
0ac992ee |
136 | my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']); |
f3938c21 |
137 | my @orig_keys = sort grep { !/^_/ } keys %$meta; |
04dd7510 |
138 | my @orig_meths = sort { $a->{name} cmp $b->{name} } |
139 | $meta->compute_all_applicable_methods; |
0ac992ee |
140 | ok($meta->is_anon_class, 'We have an anon metaclass'); |
d9586da2 |
141 | ok($meta->is_mutable, '... our anon class is mutable'); |
142 | ok(!$meta->is_immutable, '... our anon class is not immutable'); |
143 | |
0ac992ee |
144 | lives_ok {$meta->make_immutable( |
145 | inline_accessor => 1, |
146 | inline_destructor => 0, |
147 | inline_constructor => 1, |
148 | ) |
149 | } '... changed class to be immutable'; |
150 | ok(!$meta->is_mutable, '... our class is no longer mutable'); |
151 | ok($meta->is_immutable, '... our class is now immutable'); |
152 | ok(!$meta->make_immutable, '... make immutable now returns nothing'); |
153 | |
154 | lives_ok { $meta->make_mutable } '... changed Baz to be mutable'; |
155 | ok($meta->is_mutable, '... our class is mutable'); |
156 | ok(!$meta->is_immutable, '... our class is not immutable'); |
157 | ok(!$meta->make_mutable, '... make mutable now returns nothing'); |
158 | ok($meta->is_anon_class, '... still marked as an anon class'); |
159 | my $instance = $meta->new_object; |
160 | |
f3938c21 |
161 | my @new_keys = sort grep { !/^_/ } keys %$meta; |
04dd7510 |
162 | my @new_meths = sort { $a->{name} cmp $b->{name} } |
163 | $meta->compute_all_applicable_methods; |
0ac992ee |
164 | is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys'); |
165 | is_deeply(\@orig_meths, \@new_meths, '... no straneous methods'); |
166 | |
167 | isa_ok($meta, 'Class::MOP::Class', '... Anon class isa Class::MOP::Class'); |
168 | |
169 | ok( $meta->add_method('xyz', sub{'xxx'}), '... added method'); |
170 | is( $instance->xyz , 'xxx', '... method xyz works'); |
171 | ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method'); |
172 | is( $instance->zxy, 'xxx', '... method zxy works'); |
173 | ok( $meta->remove_method('xyz'), '... removed method'); |
91b73829 |
174 | ok( $meta->remove_method('zxy'), '... removed aliased method'); |
0ac992ee |
175 | |
176 | ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute'); |
177 | ok($instance->can('fickle'), '... instance can fickle'); |
178 | ok($meta->remove_attribute('fickle'), '... removed attribute'); |
179 | |
180 | my $reef = \ 'reef'; |
181 | ok($meta->add_package_symbol('$ref', $reef), '... added package symbol'); |
182 | is($meta->get_package_symbol('$ref'), $reef, '... values match'); |
183 | lives_ok { $meta->remove_package_symbol('$ref') } '... removed it'; |
184 | isnt($meta->get_package_symbol('$ref'), $reef, '... values match'); |
185 | |
186 | ok( my @supers = $meta->superclasses, '... got the superclasses okay'); |
187 | ok( $meta->superclasses('Foo'), '... set the superclasses'); |
188 | is_deeply(['Foo'], [$meta->superclasses], '... set the superclasses okay'); |
189 | ok( $meta->superclasses( @supers ), '... reset superclasses'); |
190 | is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay'); |
191 | |
192 | ok( $meta->$_ , "... ${_} works") |
193 | for qw(get_meta_instance compute_all_applicable_attributes |
194 | class_precedence_list get_method_map ); |
195 | }; |
196 | |
197 | |
198 | #rerun the same tests on an anon class.. just cause we can. |
199 | { |
200 | my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']); |
201 | |
202 | lives_ok {$meta->make_immutable( |
203 | inline_accessor => 1, |
204 | inline_destructor => 0, |
205 | inline_constructor => 1, |
206 | ) |
207 | } '... changed class to be immutable'; |
208 | lives_ok { $meta->make_mutable() } '... changed class to be mutable'; |
209 | lives_ok {$meta->make_immutable } '... changed class to be immutable'; |
210 | |
211 | dies_ok{ $meta->add_method('xyz', sub{'xxx'}) } '... exception thrown as expected'; |
212 | dies_ok{ $meta->alias_method('zxy',sub{'xxx'}) } '... exception thrown as expected'; |
213 | dies_ok{ $meta->remove_method('zxy') } '... exception thrown as expected'; |
214 | |
215 | dies_ok { |
216 | $meta->add_attribute('fickle', accessor => 'fickle') |
217 | } '... exception thrown as expected'; |
218 | dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected'; |
219 | |
220 | my $reef = \ 'reef'; |
221 | dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected'; |
222 | dies_ok { $meta->remove_package_symbol('$ref') } '... exception thrown as expected'; |
223 | |
224 | ok( my @supers = $meta->superclasses, '... got the superclasses okay'); |
225 | dies_ok { $meta->superclasses('Foo') } '... set the superclasses'; |
226 | |
227 | ok( $meta->$_ , "... ${_} works") |
228 | for qw(get_meta_instance compute_all_applicable_attributes |
229 | class_precedence_list get_method_map ); |
230 | } |
d89108ab |
231 | |
232 | { |
233 | Foo->meta->make_immutable; |
234 | Bar->meta->make_immutable; |
235 | Bar->meta->make_mutable; |
236 | |
237 | isnt( Foo->meta->get_immutable_transformer, Bar->meta->get_immutable_transformer, |
238 | 'Foo and Bar should have different immutable transformer objects' ); |
239 | } |