4 use Test::More tests => 72;
6 use Scalar::Util 'reftype';
9 require_ok(File::Spec->catfile('examples', 'ArrayBasedStorage.pod'));
18 'instance_metaclass' => 'ArrayBasedStorage::Instance',
21 Foo->meta->add_attribute('foo' => (
23 clearer => 'clear_foo',
24 predicate => 'has_foo',
27 Foo->meta->add_attribute('bar' => (
30 default => 'FOO is BAR'
35 $class->meta->new_object(@_);
40 'instance_metaclass' => 'ArrayBasedStorage::Instance',
48 Bar->meta->add_attribute('baz' => (
50 predicate => 'has_baz',
55 'instance_metaclass' => 'ArrayBasedStorage::Instance',
61 'instance_metaclass' => 'ArrayBasedStorage::Instance',
64 Baz->meta->add_attribute('bling' => (
66 default => 'Baz::bling'
71 'instance_metaclass' => 'ArrayBasedStorage::Instance',
77 use base 'Bar', 'Baz';
83 is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY');
86 can_ok($foo, 'has_foo');
87 can_ok($foo, 'get_bar');
88 can_ok($foo, 'set_bar');
89 can_ok($foo, 'clear_foo');
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');
95 $foo->foo('This is Foo');
97 ok($foo->has_foo, '... Foo::foo is defined now');
98 is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
102 ok(!$foo->has_foo, '... Foo::foo is not defined anymore');
103 is($foo->foo(), undef, '... Foo::foo is not defined anymore');
106 is($foo->get_bar(), 42, '... Foo::bar == 42');
108 my $foo2 = Foo->new();
109 isa_ok($foo2, 'Foo');
111 is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY');
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');
117 $foo2->set_bar('DONT PANIC');
118 is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
120 is($foo->get_bar(), 42, '... Foo::bar == 42');
124 my $bar = Bar->new();
128 is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY');
131 can_ok($bar, 'has_foo');
132 can_ok($bar, 'get_bar');
133 can_ok($bar, 'set_bar');
135 can_ok($bar, 'has_baz');
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');
143 $bar->foo('This is Bar::foo');
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');
149 $bar->baz('This is Bar::baz');
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');
158 my $baz = Bar::Baz->new();
159 isa_ok($baz, 'Bar::Baz');
164 is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY');
167 can_ok($baz, 'has_foo');
168 can_ok($baz, 'get_bar');
169 can_ok($baz, 'set_bar');
171 can_ok($baz, 'has_baz');
172 can_ok($baz, 'bling');
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');
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');
182 $baz->foo('This is Bar::Baz::foo');
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');
189 $baz->baz('This is Bar::Baz::baz');
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');
197 Foo->meta->add_attribute( forgotten => is => "rw" );
199 my $new_baz = Bar::Baz->new;
201 cmp_ok( scalar(@$new_baz), ">", scalar(@$baz), "additional slot due to refreshed meta instance" );