test tweaks
[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 {
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
43     use strict;
44     use warnings;
45
46     use base 'Foo';
47
48     Bar->meta->add_attribute('baz' => (
49         accessor  => 'baz',
50         predicate => 'has_baz',
51     ));
52
53     package Baz;
54
55     use strict;
56     use warnings;
57     use metaclass (
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
74 my $foo = Foo->new();
75 isa_ok($foo, 'Foo');
76
77 is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY');
78
79 can_ok($foo, 'foo');
80 can_ok($foo, 'has_foo');
81 can_ok($foo, 'get_bar');
82 can_ok($foo, 'set_bar');
83 can_ok($foo, 'clear_foo');
84
85 ok(!$foo->has_foo, '... Foo::foo is not defined yet');
86 is($foo->foo(), undef, '... Foo::foo is not defined yet');
87 is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
88
89 $foo->foo('This is Foo');
90
91 ok($foo->has_foo, '... Foo::foo is defined now');
92 is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
93
94 $foo->clear_foo;
95
96 ok(!$foo->has_foo, '... Foo::foo is not defined anymore');
97 is($foo->foo(), undef, '... Foo::foo is not defined anymore');
98
99 $foo->set_bar(42);
100 is($foo->get_bar(), 42, '... Foo::bar == 42');
101
102 my $foo2 = Foo->new();
103 isa_ok($foo2, 'Foo');
104
105 is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY');
106
107 ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
108 is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
109 is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
110
111 $foo2->set_bar('DONT PANIC');
112 is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
113
114 is($foo->get_bar(), 42, '... Foo::bar == 42');
115
116 # now Bar ...
117
118 my $bar = Bar->new();
119 isa_ok($bar, 'Bar');
120 isa_ok($bar, 'Foo');
121
122 is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY');
123
124 can_ok($bar, 'foo');
125 can_ok($bar, 'has_foo');
126 can_ok($bar, 'get_bar');
127 can_ok($bar, 'set_bar');
128 can_ok($bar, 'baz');
129 can_ok($bar, 'has_baz');
130
131 ok(!$bar->has_foo, '... Bar::foo is not defined yet');
132 is($bar->foo(), undef, '... Bar::foo is not defined yet');
133 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
134 ok(!$bar->has_baz, '... Bar::baz is not defined yet');
135 is($bar->baz(), undef, '... Bar::baz is not defined yet');
136
137 $bar->foo('This is Bar::foo');
138
139 ok($bar->has_foo, '... Bar::foo is defined now');
140 is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
141 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
142
143 $bar->baz('This is Bar::baz');
144
145 ok($bar->has_baz, '... Bar::baz is defined now');
146 is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
147 is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
148 is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
149
150 # now Baz ...
151
152 my $baz = Bar::Baz->new();
153 isa_ok($baz, 'Bar::Baz');
154 isa_ok($baz, 'Bar');
155 isa_ok($baz, 'Foo');
156 isa_ok($baz, 'Baz');
157
158 is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY');
159
160 can_ok($baz, 'foo');
161 can_ok($baz, 'has_foo');
162 can_ok($baz, 'get_bar');
163 can_ok($baz, 'set_bar');
164 can_ok($baz, 'baz');
165 can_ok($baz, 'has_baz');
166 can_ok($baz, 'bling');
167
168 is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
169 is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
170
171 ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
172 is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
173 ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
174 is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');
175
176 $baz->foo('This is Bar::Baz::foo');
177
178 ok($baz->has_foo, '... Bar::Baz::foo is defined now');
179 is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
180 is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
181 is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
182
183 $baz->baz('This is Bar::Baz::baz');
184
185 ok($baz->has_baz, '... Bar::Baz::baz is defined now');
186 is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
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