massive updates to the way immutable works to fix a big ish bug, please see new comme...
[gitmo/Class-MOP.git] / t / 073_make_mutable.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 101;
7 use Test::Exception;
8
9 use Scalar::Util;
10
11 BEGIN {
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
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
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
60     my @new_keys = sort keys %$meta;
61     is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
62
63     isa_ok($meta, 'Class::MOP::Class', '... Baz->meta isa Class::MOP::Class');
64
65     ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
66     is( Baz->xyz, 'xxx',                      '... method xyz works');
67     ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method');
68     is( Baz->zxy, 'xxx',                      '... method zxy works');
69     ok( $meta->remove_method('xyz'),          '... removed method');
70     ok( $meta->remove_method('zxy'),          '... removed aliased method');
71
72     ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute');
73     ok(Baz->can('fickle'),                '... Baz can fickle');
74     ok($meta->remove_attribute('fickle'), '... removed attribute');
75
76     my $reef = \ 'reef';
77     ok($meta->add_package_symbol('$ref', $reef),      '... added package symbol');
78     is($meta->get_package_symbol('$ref'), $reef,      '... values match');
79     lives_ok { $meta->remove_package_symbol('$ref') } '... removed it';
80     isnt($meta->get_package_symbol('$ref'), $reef,    '... values match');
81
82     ok( my @supers = $meta->superclasses,       '... got the superclasses okay');
83     ok( $meta->superclasses('Foo'),             '... set the superclasses');
84     is_deeply(['Foo'], [$meta->superclasses],   '... set the superclasses okay');
85     ok( $meta->superclasses( @supers ),         '... reset superclasses');
86     is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay');
87
88     ok( $meta->$_  , "... ${_} works")
89       for qw(get_meta_instance       compute_all_applicable_attributes
90              class_precedence_list  get_method_map );
91 }
92
93 {
94     my $meta = Baz->meta;
95
96     lives_ok { $meta->make_immutable() } 'Changed Baz to be immutable';
97     lives_ok { $meta->make_mutable() }   '... changed Baz to be mutable';
98     lives_ok { $meta->make_immutable() } '... changed Baz to be immutable';
99
100     dies_ok{ $meta->add_method('xyz', sub{'xxx'})  } '... exception thrown as expected';
101     dies_ok{ $meta->alias_method('zxy',sub{'xxx'}) } '... exception thrown as expected';
102     dies_ok{ $meta->remove_method('zxy')           } '... exception thrown as expected';
103
104     dies_ok {
105       $meta->add_attribute('fickle', accessor => 'fickle')
106     }  '... exception thrown as expected';
107     dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
108
109     my $reef = \ 'reef';
110     dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected';
111     dies_ok { $meta->remove_package_symbol('$ref')     } '... exception thrown as expected';
112
113     ok( my @supers = $meta->superclasses,  '... got the superclasses okay');
114     dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
115
116     ok( $meta->$_  , "... ${_} works")
117       for qw(get_meta_instance       compute_all_applicable_attributes
118              class_precedence_list  get_method_map );
119 }
120
121 {
122
123     my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
124     my @orig_keys  = sort keys %$meta;
125     my @orig_meths = sort { $a->{name} cmp $b->{name} }
126       $meta->compute_all_applicable_methods;
127     ok($meta->is_anon_class,                  'We have an anon metaclass');
128     lives_ok {$meta->make_immutable(
129                                     inline_accessor    => 1,
130                                     inline_destructor  => 0,
131                                     inline_constructor => 1,
132                                    )
133             } '... changed class to be immutable';
134     ok(!$meta->is_mutable,                    '... our class is no longer mutable');
135     ok($meta->is_immutable,                   '... our class is now immutable');
136     ok(!$meta->make_immutable,                '... make immutable now returns nothing');
137
138     lives_ok { $meta->make_mutable }  '... changed Baz to be mutable';
139     ok($meta->is_mutable,             '... our class is mutable');
140     ok(!$meta->is_immutable,          '... our class is not immutable');
141     ok(!$meta->make_mutable,          '... make mutable now returns nothing');
142     ok($meta->is_anon_class,          '... still marked as an anon class');
143     my $instance = $meta->new_object;
144
145     my @new_keys  = sort keys %$meta;
146     my @new_meths = sort { $a->{name} cmp $b->{name} }
147       $meta->compute_all_applicable_methods;
148     is_deeply(\@orig_keys, \@new_keys, '... no straneous hashkeys');
149     is_deeply(\@orig_meths, \@new_meths, '... no straneous methods');
150
151     isa_ok($meta, 'Class::MOP::Class', '... Anon class isa Class::MOP::Class');
152
153     ok( $meta->add_method('xyz', sub{'xxx'}), '... added method');
154     is( $instance->xyz , 'xxx',               '... method xyz works');
155     ok( $meta->alias_method('zxy',sub{'xxx'}),'... aliased method');
156     is( $instance->zxy, 'xxx',                '... method zxy works');
157     ok( $meta->remove_method('xyz'),          '... removed method');
158     ok( $meta->remove_method('zxy'),          '... removed aliased method');
159
160     ok($meta->add_attribute('fickle', accessor => 'fickle'), '... added attribute');
161     ok($instance->can('fickle'),          '... instance can fickle');
162     ok($meta->remove_attribute('fickle'), '... removed attribute');
163
164     my $reef = \ 'reef';
165     ok($meta->add_package_symbol('$ref', $reef),      '... added package symbol');
166     is($meta->get_package_symbol('$ref'), $reef,      '... values match');
167     lives_ok { $meta->remove_package_symbol('$ref') } '... removed it';
168     isnt($meta->get_package_symbol('$ref'), $reef,    '... values match');
169
170     ok( my @supers = $meta->superclasses,       '... got the superclasses okay');
171     ok( $meta->superclasses('Foo'),             '... set the superclasses');
172     is_deeply(['Foo'], [$meta->superclasses],   '... set the superclasses okay');
173     ok( $meta->superclasses( @supers ),         '... reset superclasses');
174     is_deeply([@supers], [$meta->superclasses], '... reset the superclasses okay');
175
176     ok( $meta->$_  , "... ${_} works")
177       for qw(get_meta_instance       compute_all_applicable_attributes
178              class_precedence_list  get_method_map );
179 };
180
181
182 #rerun the same tests on an anon class.. just cause we can.
183 {
184     my $meta = Baz->meta->create_anon_class(superclasses => ['Baz']);
185
186     lives_ok {$meta->make_immutable(
187                                     inline_accessor    => 1,
188                                     inline_destructor  => 0,
189                                     inline_constructor => 1,
190                                    )
191             } '... changed class to be immutable';
192     lives_ok { $meta->make_mutable() }   '... changed class to be mutable';
193     lives_ok {$meta->make_immutable  } '... changed class to be immutable';
194
195     dies_ok{ $meta->add_method('xyz', sub{'xxx'})  } '... exception thrown as expected';
196     dies_ok{ $meta->alias_method('zxy',sub{'xxx'}) } '... exception thrown as expected';
197     dies_ok{ $meta->remove_method('zxy')           } '... exception thrown as expected';
198
199     dies_ok {
200       $meta->add_attribute('fickle', accessor => 'fickle')
201     }  '... exception thrown as expected';
202     dies_ok { $meta->remove_attribute('fickle') } '... exception thrown as expected';
203
204     my $reef = \ 'reef';
205     dies_ok { $meta->add_package_symbol('$ref', $reef) } '... exception thrown as expected';
206     dies_ok { $meta->remove_package_symbol('$ref')     } '... exception thrown as expected';
207
208     ok( my @supers = $meta->superclasses,  '... got the superclasses okay');
209     dies_ok { $meta->superclasses('Foo') } '... set the superclasses';
210
211     ok( $meta->$_  , "... ${_} works")
212       for qw(get_meta_instance       compute_all_applicable_attributes
213              class_precedence_list  get_method_map );
214 }