correct invalidation of meta instance
[gitmo/Class-MOP.git] / t / 108_ArrayBasedStorage_test.t
CommitLineData
f892c0f0 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8d2d4c67 6use Test::More tests => 72;
f892c0f0 7use File::Spec;
88dd563c 8use Scalar::Util 'reftype';
f892c0f0 9
8d2d4c67 10BEGIN {
11 use_ok('Class::MOP');
10dd437b 12 require_ok(File::Spec->catfile('examples', 'ArrayBasedStorage.pod'));
f892c0f0 13}
14
15{
16 package Foo;
8d2d4c67 17
f892c0f0 18 use strict;
8d2d4c67 19 use warnings;
1becdfcc 20 use metaclass (
c23184fc 21 'instance_metaclass' => 'ArrayBasedStorage::Instance',
f892c0f0 22 );
8d2d4c67 23
f892c0f0 24 Foo->meta->add_attribute('foo' => (
25 accessor => 'foo',
8d2d4c67 26 clearer => 'clear_foo',
f892c0f0 27 predicate => 'has_foo',
28 ));
8d2d4c67 29
f892c0f0 30 Foo->meta->add_attribute('bar' => (
31 reader => 'get_bar',
32 writer => 'set_bar',
8d2d4c67 33 default => 'FOO is BAR'
f892c0f0 34 ));
8d2d4c67 35
f892c0f0 36 sub new {
37 my $class = shift;
38 $class->meta->new_object(@_);
39 }
8d2d4c67 40
f892c0f0 41 package Bar;
caab0eaa 42 use metaclass (
43 'instance_metaclass' => 'ArrayBasedStorage::Instance',
44 );
8d2d4c67 45
f892c0f0 46 use strict;
47 use warnings;
8d2d4c67 48
f892c0f0 49 use base 'Foo';
8d2d4c67 50
f892c0f0 51 Bar->meta->add_attribute('baz' => (
52 accessor => 'baz',
53 predicate => 'has_baz',
8d2d4c67 54 ));
55
f892c0f0 56 package Baz;
caab0eaa 57 use metaclass (
58 'instance_metaclass' => 'ArrayBasedStorage::Instance',
59 );
8d2d4c67 60
f892c0f0 61 use strict;
62 use warnings;
8d2d4c67 63 use metaclass (
c23184fc 64 'instance_metaclass' => 'ArrayBasedStorage::Instance',
f892c0f0 65 );
8d2d4c67 66
f892c0f0 67 Baz->meta->add_attribute('bling' => (
68 accessor => 'bling',
69 default => 'Baz::bling'
8d2d4c67 70 ));
71
f892c0f0 72 package Bar::Baz;
caab0eaa 73 use metaclass (
74 'instance_metaclass' => 'ArrayBasedStorage::Instance',
75 );
8d2d4c67 76
f892c0f0 77 use strict;
78 use warnings;
8d2d4c67 79
80 use base 'Bar', 'Baz';
f892c0f0 81}
82
83my $foo = Foo->new();
84isa_ok($foo, 'Foo');
85
88dd563c 86is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY');
87
f892c0f0 88can_ok($foo, 'foo');
89can_ok($foo, 'has_foo');
90can_ok($foo, 'get_bar');
91can_ok($foo, 'set_bar');
8d2d4c67 92can_ok($foo, 'clear_foo');
f892c0f0 93
94ok(!$foo->has_foo, '... Foo::foo is not defined yet');
95is($foo->foo(), undef, '... Foo::foo is not defined yet');
96is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
97
98$foo->foo('This is Foo');
99
100ok($foo->has_foo, '... Foo::foo is defined now');
101is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
102
8d2d4c67 103$foo->clear_foo;
104
105ok(!$foo->has_foo, '... Foo::foo is not defined anymore');
106is($foo->foo(), undef, '... Foo::foo is not defined anymore');
107
f892c0f0 108$foo->set_bar(42);
109is($foo->get_bar(), 42, '... Foo::bar == 42');
110
111my $foo2 = Foo->new();
112isa_ok($foo2, 'Foo');
113
88dd563c 114is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY');
115
f892c0f0 116ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
117is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
118is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
119
120$foo2->set_bar('DONT PANIC');
121is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
122
123is($foo->get_bar(), 42, '... Foo::bar == 42');
124
125# now Bar ...
126
127my $bar = Bar->new();
128isa_ok($bar, 'Bar');
129isa_ok($bar, 'Foo');
130
88dd563c 131is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY');
132
f892c0f0 133can_ok($bar, 'foo');
134can_ok($bar, 'has_foo');
135can_ok($bar, 'get_bar');
136can_ok($bar, 'set_bar');
137can_ok($bar, 'baz');
138can_ok($bar, 'has_baz');
139
140ok(!$bar->has_foo, '... Bar::foo is not defined yet');
141is($bar->foo(), undef, '... Bar::foo is not defined yet');
142is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
143ok(!$bar->has_baz, '... Bar::baz is not defined yet');
144is($bar->baz(), undef, '... Bar::baz is not defined yet');
145
146$bar->foo('This is Bar::foo');
147
148ok($bar->has_foo, '... Bar::foo is defined now');
149is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
150is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
151
152$bar->baz('This is Bar::baz');
153
154ok($bar->has_baz, '... Bar::baz is defined now');
155is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
156is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
157is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
158
159# now Baz ...
160
161my $baz = Bar::Baz->new();
162isa_ok($baz, 'Bar::Baz');
163isa_ok($baz, 'Bar');
164isa_ok($baz, 'Foo');
165isa_ok($baz, 'Baz');
166
88dd563c 167is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY');
168
f892c0f0 169can_ok($baz, 'foo');
170can_ok($baz, 'has_foo');
171can_ok($baz, 'get_bar');
172can_ok($baz, 'set_bar');
173can_ok($baz, 'baz');
174can_ok($baz, 'has_baz');
175can_ok($baz, 'bling');
176
177is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
178is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
179
180ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
181is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
182ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
183is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');
184
185$baz->foo('This is Bar::Baz::foo');
186
187ok($baz->has_foo, '... Bar::Baz::foo is defined now');
188is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
189is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
190is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
191
192$baz->baz('This is Bar::Baz::baz');
193
194ok($baz->has_baz, '... Bar::Baz::baz is defined now');
195is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
196is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
197is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
198is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
199
200