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