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