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