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