test tweaks
[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;
8d2d4c67 42
f892c0f0 43 use strict;
44 use warnings;
8d2d4c67 45
f892c0f0 46 use base 'Foo';
8d2d4c67 47
f892c0f0 48 Bar->meta->add_attribute('baz' => (
49 accessor => 'baz',
50 predicate => 'has_baz',
8d2d4c67 51 ));
52
f892c0f0 53 package Baz;
8d2d4c67 54
f892c0f0 55 use strict;
56 use warnings;
8d2d4c67 57 use metaclass (
c23184fc 58 'instance_metaclass' => 'ArrayBasedStorage::Instance',
f892c0f0 59 );
8d2d4c67 60
f892c0f0 61 Baz->meta->add_attribute('bling' => (
62 accessor => 'bling',
63 default => 'Baz::bling'
8d2d4c67 64 ));
65
f892c0f0 66 package Bar::Baz;
8d2d4c67 67
f892c0f0 68 use strict;
69 use warnings;
8d2d4c67 70
71 use base 'Bar', 'Baz';
f892c0f0 72}
73
74my $foo = Foo->new();
75isa_ok($foo, 'Foo');
76
88dd563c 77is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY');
78
f892c0f0 79can_ok($foo, 'foo');
80can_ok($foo, 'has_foo');
81can_ok($foo, 'get_bar');
82can_ok($foo, 'set_bar');
8d2d4c67 83can_ok($foo, 'clear_foo');
f892c0f0 84
85ok(!$foo->has_foo, '... Foo::foo is not defined yet');
86is($foo->foo(), undef, '... Foo::foo is not defined yet');
87is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
88
89$foo->foo('This is Foo');
90
91ok($foo->has_foo, '... Foo::foo is defined now');
92is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
93
8d2d4c67 94$foo->clear_foo;
95
96ok(!$foo->has_foo, '... Foo::foo is not defined anymore');
97is($foo->foo(), undef, '... Foo::foo is not defined anymore');
98
f892c0f0 99$foo->set_bar(42);
100is($foo->get_bar(), 42, '... Foo::bar == 42');
101
102my $foo2 = Foo->new();
103isa_ok($foo2, 'Foo');
104
88dd563c 105is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY');
106
f892c0f0 107ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
108is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
109is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
110
111$foo2->set_bar('DONT PANIC');
112is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
113
114is($foo->get_bar(), 42, '... Foo::bar == 42');
115
116# now Bar ...
117
118my $bar = Bar->new();
119isa_ok($bar, 'Bar');
120isa_ok($bar, 'Foo');
121
88dd563c 122is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY');
123
f892c0f0 124can_ok($bar, 'foo');
125can_ok($bar, 'has_foo');
126can_ok($bar, 'get_bar');
127can_ok($bar, 'set_bar');
128can_ok($bar, 'baz');
129can_ok($bar, 'has_baz');
130
131ok(!$bar->has_foo, '... Bar::foo is not defined yet');
132is($bar->foo(), undef, '... Bar::foo is not defined yet');
133is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
134ok(!$bar->has_baz, '... Bar::baz is not defined yet');
135is($bar->baz(), undef, '... Bar::baz is not defined yet');
136
137$bar->foo('This is Bar::foo');
138
139ok($bar->has_foo, '... Bar::foo is defined now');
140is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
141is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
142
143$bar->baz('This is Bar::baz');
144
145ok($bar->has_baz, '... Bar::baz is defined now');
146is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
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# now Baz ...
151
152my $baz = Bar::Baz->new();
153isa_ok($baz, 'Bar::Baz');
154isa_ok($baz, 'Bar');
155isa_ok($baz, 'Foo');
156isa_ok($baz, 'Baz');
157
88dd563c 158is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY');
159
f892c0f0 160can_ok($baz, 'foo');
161can_ok($baz, 'has_foo');
162can_ok($baz, 'get_bar');
163can_ok($baz, 'set_bar');
164can_ok($baz, 'baz');
165can_ok($baz, 'has_baz');
166can_ok($baz, 'bling');
167
168is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
169is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
170
171ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
172is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
173ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
174is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');
175
176$baz->foo('This is Bar::Baz::foo');
177
178ok($baz->has_foo, '... Bar::Baz::foo is defined now');
179is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
180is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
181is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
182
183$baz->baz('This is Bar::Baz::baz');
184
185ok($baz->has_baz, '... Bar::Baz::baz is defined now');
186is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
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