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