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