6 use Test::More tests => 73;
8 use Scalar::Util 'reftype';
12 require_ok(File::Spec->catfile('examples', 'ArrayBasedStorage.pod'));
21 'instance_metaclass' => 'ArrayBasedStorage::Instance',
24 Foo->meta->add_attribute('foo' => (
26 clearer => 'clear_foo',
27 predicate => 'has_foo',
30 Foo->meta->add_attribute('bar' => (
33 default => 'FOO is BAR'
38 $class->meta->new_object(@_);
43 'instance_metaclass' => 'ArrayBasedStorage::Instance',
51 Bar->meta->add_attribute('baz' => (
53 predicate => 'has_baz',
58 'instance_metaclass' => 'ArrayBasedStorage::Instance',
64 'instance_metaclass' => 'ArrayBasedStorage::Instance',
67 Baz->meta->add_attribute('bling' => (
69 default => 'Baz::bling'
74 'instance_metaclass' => 'ArrayBasedStorage::Instance',
80 use base 'Bar', 'Baz';
86 is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY');
89 can_ok($foo, 'has_foo');
90 can_ok($foo, 'get_bar');
91 can_ok($foo, 'set_bar');
92 can_ok($foo, 'clear_foo');
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');
98 $foo->foo('This is Foo');
100 ok($foo->has_foo, '... Foo::foo is defined now');
101 is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
105 ok(!$foo->has_foo, '... Foo::foo is not defined anymore');
106 is($foo->foo(), undef, '... Foo::foo is not defined anymore');
109 is($foo->get_bar(), 42, '... Foo::bar == 42');
111 my $foo2 = Foo->new();
112 isa_ok($foo2, 'Foo');
114 is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY');
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');
120 $foo2->set_bar('DONT PANIC');
121 is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
123 is($foo->get_bar(), 42, '... Foo::bar == 42');
127 my $bar = Bar->new();
131 is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY');
134 can_ok($bar, 'has_foo');
135 can_ok($bar, 'get_bar');
136 can_ok($bar, 'set_bar');
138 can_ok($bar, 'has_baz');
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');
146 $bar->foo('This is Bar::foo');
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');
152 $bar->baz('This is Bar::baz');
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');
161 my $baz = Bar::Baz->new();
162 isa_ok($baz, 'Bar::Baz');
167 is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY');
170 can_ok($baz, 'has_foo');
171 can_ok($baz, 'get_bar');
172 can_ok($baz, 'set_bar');
174 can_ok($baz, 'has_baz');
175 can_ok($baz, 'bling');
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');
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');
185 $baz->foo('This is Bar::Baz::foo');
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');
192 $baz->baz('This is Bar::Baz::baz');
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');
200 Foo->meta->add_attribute( forgotten => is => "rw" );
202 my $new_baz = Bar::Baz->new;
204 cmp_ok( scalar(@$new_baz), ">", scalar(@$baz), "additional slot due to refreshed meta instance" );