6 use Test::More tests => 112;
22 __PACKAGE__->meta->add_attribute('bar');
30 __PACKAGE__->meta->superclasses('Foo');
32 __PACKAGE__->meta->add_attribute('baz');
40 __PACKAGE__->meta->superclasses('Bar');
42 __PACKAGE__->meta->add_attribute('bah');
47 is($meta->name, 'Baz', '... checking the Baz metaclass');
48 my @orig_keys = sort grep { !/^_/ } keys %$meta;
50 lives_ok {$meta->make_immutable; } '... changed Baz to be immutable';
51 ok(!$meta->is_mutable, '... our class is no longer mutable');
52 ok($meta->is_immutable, '... our class is now immutable');
53 ok(!$meta->make_immutable, '... make immutable now returns nothing');
54 ok($meta->get_method_map->{new}, '... inlined constructor created');
55 ok($meta->has_method('new'), '... inlined constructor created for sure');
57 lives_ok { $meta->make_mutable; } '... changed Baz to be mutable';
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');
61 ok(!$meta->get_method_map->{new}, '... inlined constructor removed');
62 ok(!$meta->has_method('new'), '... inlined constructor removed for sure');
64 my @new_keys = sort grep { !/^_/ } keys %$meta;
65 is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
67 isa_ok($meta, 'Class::MOP::Class', '... Baz->meta isa Class::MOP::Class');
69 ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
70 is( Baz->xyz, 'xxx', '... method xyz works');
72 ok(! $meta->has_method('zxy') ,'... we dont have the aliased method yet');
73 ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method');
74 ok(! $meta->has_method('zxy') ,'... the aliased method does not register (correctly)');
75 is( Baz->zxy, 'xxx', '... method zxy works');
76 ok( $meta->remove_method('xyz'), '... removed method');
77 ok(! $meta->remove_method('zxy'), '... removed aliased method');
79 ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute');
80 ok(Baz->can('fickle'), '... Baz can fickle');
81 ok($meta->remove_attribute('fickle'), '... removed attribute');
84 ok($meta->add_package_symbol('$ref', $reef), '... added package symbol');
85 is($meta->get_package_symbol('$ref'), $reef, '... values match');
86 lives_ok { $meta->remove_package_symbol('$ref') } '... removed it';
87 isnt($meta->get_package_symbol('$ref'), $reef, '... values match');
89 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
90 ok( $meta->superclasses('Foo'), '... set the superclasses');
91 is_deeply(['Foo'], [$meta->superclasses], '... set the superclasses okay');
92 ok( $meta->superclasses( @supers ), '... reset superclasses');
93 is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay');
95 ok( $meta->$_ , "... ${_} works")
96 for qw(get_meta_instance compute_all_applicable_attributes
97 class_precedence_list get_method_map );
99 lives_ok {$meta->make_immutable; } '... changed Baz to be immutable again';
100 ok($meta->get_method_map->{new}, '... inlined constructor recreated');
104 my $meta = Baz->meta;
106 lives_ok { $meta->make_immutable() } 'Changed Baz to be immutable';
107 lives_ok { $meta->make_mutable() } '... changed Baz to be mutable';
108 lives_ok { $meta->make_immutable() } '... changed Baz to be immutable';
110 dies_ok{ $meta->add_method('xyz', sub{'xxx'}) } '... exception thrown as expected';
111 dies_ok{ $meta->alias_method('zxy',sub{'xxx'}) } '... exception thrown as expected';
112 dies_ok{ $meta->remove_method('zxy') } '... exception thrown as expected';
115 $meta->add_attribute('fickle', accessor => 'fickle')
116 } '... exception thrown as expected';
117 dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
120 dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected';
121 dies_ok { $meta->remove_package_symbol('$ref') } '... exception thrown as expected';
123 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
124 dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
126 ok( $meta->$_ , "... ${_} works")
127 for qw(get_meta_instance compute_all_applicable_attributes
128 class_precedence_list get_method_map );
133 ok(Baz->meta->is_immutable, 'Superclass is immutable');
134 my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
135 my @orig_keys = sort grep { !/^_/ } keys %$meta;
136 my @orig_meths = sort { $a->{name} cmp $b->{name} }
137 $meta->compute_all_applicable_methods;
138 ok($meta->is_anon_class, 'We have an anon metaclass');
139 ok($meta->is_mutable, '... our anon class is mutable');
140 ok(!$meta->is_immutable, '... our anon class is not immutable');
142 lives_ok {$meta->make_immutable(
143 inline_accessor => 1,
144 inline_destructor => 0,
145 inline_constructor => 1,
147 } '... changed class to be immutable';
148 ok(!$meta->is_mutable, '... our class is no longer mutable');
149 ok($meta->is_immutable, '... our class is now immutable');
150 ok(!$meta->make_immutable, '... make immutable now returns nothing');
152 lives_ok { $meta->make_mutable } '... changed Baz to be mutable';
153 ok($meta->is_mutable, '... our class is mutable');
154 ok(!$meta->is_immutable, '... our class is not immutable');
155 ok(!$meta->make_mutable, '... make mutable now returns nothing');
156 ok($meta->is_anon_class, '... still marked as an anon class');
157 my $instance = $meta->new_object;
159 my @new_keys = sort grep { !/^_/ } keys %$meta;
160 my @new_meths = sort { $a->{name} cmp $b->{name} }
161 $meta->compute_all_applicable_methods;
162 is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
163 is_deeply(\@orig_meths, \@new_meths, '... no straneous methods');
165 isa_ok($meta, 'Class::MOP::Class', '... Anon class isa Class::MOP::Class');
167 ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
168 is( $instance->xyz , 'xxx', '... method xyz works');
169 ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method');
170 is( $instance->zxy, 'xxx', '... method zxy works');
171 ok( $meta->remove_method('xyz'), '... removed method');
172 ok( !$meta->remove_method('zxy'), '... removed aliased method');
174 ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute');
175 ok($instance->can('fickle'), '... instance can fickle');
176 ok($meta->remove_attribute('fickle'), '... removed attribute');
179 ok($meta->add_package_symbol('$ref', $reef), '... added package symbol');
180 is($meta->get_package_symbol('$ref'), $reef, '... values match');
181 lives_ok { $meta->remove_package_symbol('$ref') } '... removed it';
182 isnt($meta->get_package_symbol('$ref'), $reef, '... values match');
184 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
185 ok( $meta->superclasses('Foo'), '... set the superclasses');
186 is_deeply(['Foo'], [$meta->superclasses], '... set the superclasses okay');
187 ok( $meta->superclasses( @supers ), '... reset superclasses');
188 is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay');
190 ok( $meta->$_ , "... ${_} works")
191 for qw(get_meta_instance compute_all_applicable_attributes
192 class_precedence_list get_method_map );
196 #rerun the same tests on an anon class.. just cause we can.
198 my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
200 lives_ok {$meta->make_immutable(
201 inline_accessor => 1,
202 inline_destructor => 0,
203 inline_constructor => 1,
205 } '... changed class to be immutable';
206 lives_ok { $meta->make_mutable() } '... changed class to be mutable';
207 lives_ok {$meta->make_immutable } '... changed class to be immutable';
209 dies_ok{ $meta->add_method('xyz', sub{'xxx'}) } '... exception thrown as expected';
210 dies_ok{ $meta->alias_method('zxy',sub{'xxx'}) } '... exception thrown as expected';
211 dies_ok{ $meta->remove_method('zxy') } '... exception thrown as expected';
214 $meta->add_attribute('fickle', accessor => 'fickle')
215 } '... exception thrown as expected';
216 dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
219 dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected';
220 dies_ok { $meta->remove_package_symbol('$ref') } '... exception thrown as expected';
222 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
223 dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
225 ok( $meta->$_ , "... ${_} works")
226 for qw(get_meta_instance compute_all_applicable_attributes
227 class_precedence_list get_method_map );