merging the immutable branch into trunk
[gitmo/Class-MOP.git] / t / 108_ArrayBasedStorage_test.t
CommitLineData
f892c0f0 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
88dd563c 6use Test::More tests => 69;
f892c0f0 7use File::Spec;
88dd563c 8use Scalar::Util 'reftype';
f892c0f0 9
10BEGIN {
11 use_ok('Class::MOP');
12 require_ok(File::Spec->catdir('examples', 'ArrayBasedStorage.pod'));
13}
14
15{
16 package Foo;
17
18 use strict;
19 use warnings;
1becdfcc 20 use metaclass (
c23184fc 21 'instance_metaclass' => 'ArrayBasedStorage::Instance',
f892c0f0 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;
43715282 56 use metaclass (
c23184fc 57 'instance_metaclass' => 'ArrayBasedStorage::Instance',
f892c0f0 58 );
59
60 Baz->meta->add_attribute('bling' => (
61 accessor => 'bling',
62 default => 'Baz::bling'
63 ));
64
65 package Bar::Baz;
66
67 use strict;
68 use warnings;
69
70 use base 'Bar', 'Baz';
71}
72
73my $foo = Foo->new();
74isa_ok($foo, 'Foo');
75
88dd563c 76is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY');
77
f892c0f0 78can_ok($foo, 'foo');
79can_ok($foo, 'has_foo');
80can_ok($foo, 'get_bar');
81can_ok($foo, 'set_bar');
82
83ok(!$foo->has_foo, '... Foo::foo is not defined yet');
84is($foo->foo(), undef, '... Foo::foo is not defined yet');
85is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
86
87$foo->foo('This is Foo');
88
89ok($foo->has_foo, '... Foo::foo is defined now');
90is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
91
92$foo->set_bar(42);
93is($foo->get_bar(), 42, '... Foo::bar == 42');
94
95my $foo2 = Foo->new();
96isa_ok($foo2, 'Foo');
97
88dd563c 98is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY');
99
f892c0f0 100ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
101is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
102is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
103
104$foo2->set_bar('DONT PANIC');
105is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
106
107is($foo->get_bar(), 42, '... Foo::bar == 42');
108
109# now Bar ...
110
111my $bar = Bar->new();
112isa_ok($bar, 'Bar');
113isa_ok($bar, 'Foo');
114
88dd563c 115is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY');
116
f892c0f0 117can_ok($bar, 'foo');
118can_ok($bar, 'has_foo');
119can_ok($bar, 'get_bar');
120can_ok($bar, 'set_bar');
121can_ok($bar, 'baz');
122can_ok($bar, 'has_baz');
123
124ok(!$bar->has_foo, '... Bar::foo is not defined yet');
125is($bar->foo(), undef, '... Bar::foo is not defined yet');
126is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
127ok(!$bar->has_baz, '... Bar::baz is not defined yet');
128is($bar->baz(), undef, '... Bar::baz is not defined yet');
129
130$bar->foo('This is Bar::foo');
131
132ok($bar->has_foo, '... Bar::foo is defined now');
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$bar->baz('This is Bar::baz');
137
138ok($bar->has_baz, '... Bar::baz is defined now');
139is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
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# now Baz ...
144
145my $baz = Bar::Baz->new();
146isa_ok($baz, 'Bar::Baz');
147isa_ok($baz, 'Bar');
148isa_ok($baz, 'Foo');
149isa_ok($baz, 'Baz');
150
88dd563c 151is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY');
152
f892c0f0 153can_ok($baz, 'foo');
154can_ok($baz, 'has_foo');
155can_ok($baz, 'get_bar');
156can_ok($baz, 'set_bar');
157can_ok($baz, 'baz');
158can_ok($baz, 'has_baz');
159can_ok($baz, 'bling');
160
161is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
162is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
163
164ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
165is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
166ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
167is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');
168
169$baz->foo('This is Bar::Baz::foo');
170
171ok($baz->has_foo, '... Bar::Baz::foo is defined now');
172is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
173is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
174is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
175
176$baz->baz('This is Bar::Baz::baz');
177
178ok($baz->has_baz, '... Bar::Baz::baz is defined now');
179is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
180is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
181is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
182is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
183
184