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