6 use Test::More tests => 111;
20 __PACKAGE__->meta->add_attribute('bar');
28 __PACKAGE__->meta->superclasses('Foo');
30 __PACKAGE__->meta->add_attribute('baz');
38 __PACKAGE__->meta->superclasses('Bar');
40 __PACKAGE__->meta->add_attribute('bah');
45 is($meta->name, 'Baz', '... checking the Baz metaclass');
46 my @orig_keys = sort grep { !/^_/ } keys %$meta;
48 lives_ok {$meta->make_immutable; } '... changed Baz to be immutable';
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');
52 ok($meta->get_method_map->{new}, '... inlined constructor created');
53 ok($meta->has_method('new'), '... inlined constructor created for sure');
55 lives_ok { $meta->make_mutable; } '... changed Baz to be mutable';
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');
59 ok(!$meta->get_method_map->{new}, '... inlined constructor removed');
60 ok(!$meta->has_method('new'), '... inlined constructor removed for sure');
62 my @new_keys = sort grep { !/^_/ } keys %$meta;
63 is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
65 isa_ok($meta, 'Class::MOP::Class', '... Baz->meta isa Class::MOP::Class');
67 ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
68 is( Baz->xyz, 'xxx', '... method xyz works');
70 ok(! $meta->has_method('zxy') ,'... we dont have the aliased method yet');
71 ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method');
72 ok( $meta->has_method('zxy') ,'... the aliased method does register');
73 is( Baz->zxy, 'xxx', '... method zxy works');
74 ok( $meta->remove_method('xyz'), '... removed method');
75 ok( $meta->remove_method('zxy'), '... removed aliased method');
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');
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');
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');
93 ok( $meta->$_ , "... ${_} works")
94 for qw(get_meta_instance compute_all_applicable_attributes
95 class_precedence_list get_method_map );
97 lives_ok {$meta->make_immutable; } '... changed Baz to be immutable again';
98 ok($meta->get_method_map->{new}, '... inlined constructor recreated');
102 my $meta = Baz->meta;
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';
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';
113 $meta->add_attribute('fickle', accessor => 'fickle')
114 } '... exception thrown as expected';
115 dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
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';
121 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
122 dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
124 ok( $meta->$_ , "... ${_} works")
125 for qw(get_meta_instance compute_all_applicable_attributes
126 class_precedence_list get_method_map );
131 ok(Baz->meta->is_immutable, 'Superclass is immutable');
132 my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
133 my @orig_keys = sort grep { !/^_/ } keys %$meta;
134 my @orig_meths = sort { $a->{name} cmp $b->{name} }
135 $meta->compute_all_applicable_methods;
136 ok($meta->is_anon_class, 'We have an anon metaclass');
137 ok($meta->is_mutable, '... our anon class is mutable');
138 ok(!$meta->is_immutable, '... our anon class is not immutable');
140 lives_ok {$meta->make_immutable(
141 inline_accessor => 1,
142 inline_destructor => 0,
143 inline_constructor => 1,
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');
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;
157 my @new_keys = sort grep { !/^_/ } keys %$meta;
158 my @new_meths = sort { $a->{name} cmp $b->{name} }
159 $meta->compute_all_applicable_methods;
160 is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
161 is_deeply(\@orig_meths, \@new_meths, '... no straneous methods');
163 isa_ok($meta, 'Class::MOP::Class', '... Anon class isa Class::MOP::Class');
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');
170 ok( $meta->remove_method('zxy'), '... removed aliased method');
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');
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');
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');
188 ok( $meta->$_ , "... ${_} works")
189 for qw(get_meta_instance compute_all_applicable_attributes
190 class_precedence_list get_method_map );
194 #rerun the same tests on an anon class.. just cause we can.
196 my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
198 lives_ok {$meta->make_immutable(
199 inline_accessor => 1,
200 inline_destructor => 0,
201 inline_constructor => 1,
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';
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';
212 $meta->add_attribute('fickle', accessor => 'fickle')
213 } '... exception thrown as expected';
214 dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
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';
220 ok( my @supers = $meta->superclasses, '... got the superclasses okay');
221 dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
223 ok( $meta->$_ , "... ${_} works")
224 for qw(get_meta_instance compute_all_applicable_attributes
225 class_precedence_list get_method_map );