4 use Test::More tests => 114;
18 __PACKAGE__->meta->add_attribute('bar');
26 __PACKAGE__->meta->superclasses('Foo');
28 __PACKAGE__->meta->add_attribute('baz');
36 __PACKAGE__->meta->superclasses('Bar');
38 __PACKAGE__->meta->add_attribute('bah');
43 is($meta->name, 'Baz', '... checking the Baz metaclass');
44 my %orig_keys = map { $_ => 1 } grep { !/^_/ } keys %$meta;
45 # Since this has no default it won't be present yet, but it will
46 # be after the class is made immutable.
47 $orig_keys{immutable_transformer} = 1;
49 lives_ok {$meta->make_immutable; } '... changed Baz to be immutable';
50 ok(!$meta->is_mutable, '... our class is no longer mutable');
51 ok($meta->is_immutable, '... our class is now immutable');
52 ok(!$meta->make_immutable, '... make immutable now returns nothing');
53 ok($meta->get_method_map->{new}, '... inlined constructor created');
54 ok($meta->has_method('new'), '... inlined constructor created for sure');
55 ok($meta->immutable_transformer->inlined_constructor,
56 '... transformer says it did inline the constructor');
58 lives_ok { $meta->make_mutable; } '... changed Baz to be mutable';
59 ok($meta->is_mutable, '... our class is mutable');
60 ok(!$meta->is_immutable, '... our class is not immutable');
61 ok(!$meta->make_mutable, '... make mutable now returns nothing');
62 ok(!$meta->get_method_map->{new}, '... inlined constructor removed');
63 ok(!$meta->has_method('new'), '... inlined constructor removed for sure');
64 ok(!$meta->immutable_transformer->inlined_constructor,
65 '... transformer says it did not inline the constructor');
67 my %new_keys = map { $_ => 1 } grep { !/^_/ } keys %$meta;
68 is_deeply(\%orig_keys, \%new_keys, '... no extraneous hashkeys');
70 isa_ok($meta, 'Class::MOP::Class', '... Baz->meta isa Class::MOP::Class');
72 ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
73 is( Baz->xyz, 'xxx', '... method xyz works');
75 ok(! $meta->has_method('zxy') ,'... we dont have the aliased method yet');
76 ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method');
77 ok( $meta->has_method('zxy') ,'... the aliased method does register');
78 is( Baz->zxy, 'xxx', '... method zxy works');
79 ok( $meta->remove_method('xyz'), '... removed method');
80 ok( $meta->remove_method('zxy'), '... removed aliased method');
82 ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute');
83 ok(Baz->can('fickle'), '... Baz can fickle');
84 ok($meta->remove_attribute('fickle'), '... removed attribute');
87 ok($meta->add_package_symbol('$ref', $reef), '... added package symbol');
88 is($meta->get_package_symbol('$ref'), $reef, '... values match');
89 lives_ok { $meta->remove_package_symbol('$ref') } '... removed it';
90 isnt($meta->get_package_symbol('$ref'), $reef, '... values match');
92 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
93 ok( $meta->superclasses('Foo'), '... set the superclasses');
94 is_deeply(['Foo'], [$meta->superclasses], '... set the superclasses okay');
95 ok( $meta->superclasses( @supers ), '... reset superclasses');
96 is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay');
98 ok( $meta->$_ , "... ${_} works")
99 for qw(get_meta_instance compute_all_applicable_attributes
100 class_precedence_list get_method_map );
102 lives_ok {$meta->make_immutable; } '... changed Baz to be immutable again';
103 ok($meta->get_method_map->{new}, '... inlined constructor recreated');
107 my $meta = Baz->meta;
109 lives_ok { $meta->make_immutable() } 'Changed Baz to be immutable';
110 lives_ok { $meta->make_mutable() } '... changed Baz to be mutable';
111 lives_ok { $meta->make_immutable() } '... changed Baz to be immutable';
113 dies_ok{ $meta->add_method('xyz', sub{'xxx'}) } '... exception thrown as expected';
114 dies_ok{ $meta->alias_method('zxy',sub{'xxx'}) } '... exception thrown as expected';
115 dies_ok{ $meta->remove_method('zxy') } '... exception thrown as expected';
118 $meta->add_attribute('fickle', accessor => 'fickle')
119 } '... exception thrown as expected';
120 dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
123 dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected';
124 dies_ok { $meta->remove_package_symbol('$ref') } '... exception thrown as expected';
126 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
127 dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
129 ok( $meta->$_ , "... ${_} works")
130 for qw(get_meta_instance compute_all_applicable_attributes
131 class_precedence_list get_method_map );
136 ok(Baz->meta->is_immutable, 'Superclass is immutable');
137 my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
138 my %orig_keys = map { $_ => 1 } grep { !/^_/ } keys %$meta;
139 $orig_keys{immutable_transformer} = 1;
140 my @orig_meths = sort { $a->{name} cmp $b->{name} }
141 $meta->compute_all_applicable_methods;
142 ok($meta->is_anon_class, 'We have an anon metaclass');
143 ok($meta->is_mutable, '... our anon class is mutable');
144 ok(!$meta->is_immutable, '... our anon class is not immutable');
146 lives_ok {$meta->make_immutable(
147 inline_accessor => 1,
148 inline_destructor => 0,
149 inline_constructor => 1,
151 } '... changed class to be immutable';
152 ok(!$meta->is_mutable, '... our class is no longer mutable');
153 ok($meta->is_immutable, '... our class is now immutable');
154 ok(!$meta->make_immutable, '... make immutable now returns nothing');
156 lives_ok { $meta->make_mutable } '... changed Baz to be mutable';
157 ok($meta->is_mutable, '... our class is mutable');
158 ok(!$meta->is_immutable, '... our class is not immutable');
159 ok(!$meta->make_mutable, '... make mutable now returns nothing');
160 ok($meta->is_anon_class, '... still marked as an anon class');
161 my $instance = $meta->new_object;
163 my %new_keys = map { $_ => 1 } grep { !/^_/ } keys %$meta;
164 my @new_meths = sort { $a->{name} cmp $b->{name} }
165 $meta->compute_all_applicable_methods;
166 is_deeply(\%orig_keys, \%new_keys, '... no extraneous hashkeys');
167 is_deeply(\@orig_meths, \@new_meths, '... no extraneous methods');
169 isa_ok($meta, 'Class::MOP::Class', '... Anon class isa Class::MOP::Class');
171 ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
172 is( $instance->xyz , 'xxx', '... method xyz works');
173 ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method');
174 is( $instance->zxy, 'xxx', '... method zxy works');
175 ok( $meta->remove_method('xyz'), '... removed method');
176 ok( $meta->remove_method('zxy'), '... removed aliased method');
178 ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute');
179 ok($instance->can('fickle'), '... instance can fickle');
180 ok($meta->remove_attribute('fickle'), '... removed attribute');
183 ok($meta->add_package_symbol('$ref', $reef), '... added package symbol');
184 is($meta->get_package_symbol('$ref'), $reef, '... values match');
185 lives_ok { $meta->remove_package_symbol('$ref') } '... removed it';
186 isnt($meta->get_package_symbol('$ref'), $reef, '... values match');
188 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
189 ok( $meta->superclasses('Foo'), '... set the superclasses');
190 is_deeply(['Foo'], [$meta->superclasses], '... set the superclasses okay');
191 ok( $meta->superclasses( @supers ), '... reset superclasses');
192 is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay');
194 ok( $meta->$_ , "... ${_} works")
195 for qw(get_meta_instance compute_all_applicable_attributes
196 class_precedence_list get_method_map );
200 #rerun the same tests on an anon class.. just cause we can.
202 my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
204 lives_ok {$meta->make_immutable(
205 inline_accessor => 1,
206 inline_destructor => 0,
207 inline_constructor => 1,
209 } '... changed class to be immutable';
210 lives_ok { $meta->make_mutable() } '... changed class to be mutable';
211 lives_ok {$meta->make_immutable } '... changed class to be immutable';
213 dies_ok{ $meta->add_method('xyz', sub{'xxx'}) } '... exception thrown as expected';
214 dies_ok{ $meta->alias_method('zxy',sub{'xxx'}) } '... exception thrown as expected';
215 dies_ok{ $meta->remove_method('zxy') } '... exception thrown as expected';
218 $meta->add_attribute('fickle', accessor => 'fickle')
219 } '... exception thrown as expected';
220 dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
223 dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected';
224 dies_ok { $meta->remove_package_symbol('$ref') } '... exception thrown as expected';
226 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
227 dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
229 ok( $meta->$_ , "... ${_} works")
230 for qw(get_meta_instance compute_all_applicable_attributes
231 class_precedence_list get_method_map );
235 Foo->meta->make_immutable;
236 Bar->meta->make_immutable;
237 Bar->meta->make_mutable;
239 isnt( Foo->meta->immutable_transformer, Bar->meta->immutable_transformer,
240 'Foo and Bar should have different immutable transformer objects' );