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