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