6 use Test::More tests => 72;
8 use Scalar::Util 'reftype';
10 BEGIN {use Class::MOP;
11 require_ok(File::Spec->catfile('examples', 'ArrayBasedStorage.pod'));
20 'instance_metaclass' => 'ArrayBasedStorage::Instance',
23 Foo->meta->add_attribute('foo' => (
25 clearer => 'clear_foo',
26 predicate => 'has_foo',
29 Foo->meta->add_attribute('bar' => (
32 default => 'FOO is BAR'
37 $class->meta->new_object(@_);
42 'instance_metaclass' => 'ArrayBasedStorage::Instance',
50 Bar->meta->add_attribute('baz' => (
52 predicate => 'has_baz',
57 'instance_metaclass' => 'ArrayBasedStorage::Instance',
63 'instance_metaclass' => 'ArrayBasedStorage::Instance',
66 Baz->meta->add_attribute('bling' => (
68 default => 'Baz::bling'
73 'instance_metaclass' => 'ArrayBasedStorage::Instance',
79 use base 'Bar', 'Baz';
85 is(reftype($foo), 'ARRAY', '... Foo is made with ARRAY');
88 can_ok($foo, 'has_foo');
89 can_ok($foo, 'get_bar');
90 can_ok($foo, 'set_bar');
91 can_ok($foo, 'clear_foo');
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');
97 $foo->foo('This is Foo');
99 ok($foo->has_foo, '... Foo::foo is defined now');
100 is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
104 ok(!$foo->has_foo, '... Foo::foo is not defined anymore');
105 is($foo->foo(), undef, '... Foo::foo is not defined anymore');
108 is($foo->get_bar(), 42, '... Foo::bar == 42');
110 my $foo2 = Foo->new();
111 isa_ok($foo2, 'Foo');
113 is(reftype($foo2), 'ARRAY', '... Foo is made with ARRAY');
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');
119 $foo2->set_bar('DONT PANIC');
120 is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
122 is($foo->get_bar(), 42, '... Foo::bar == 42');
126 my $bar = Bar->new();
130 is(reftype($bar), 'ARRAY', '... Bar is made with ARRAY');
133 can_ok($bar, 'has_foo');
134 can_ok($bar, 'get_bar');
135 can_ok($bar, 'set_bar');
137 can_ok($bar, 'has_baz');
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');
145 $bar->foo('This is Bar::foo');
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');
151 $bar->baz('This is Bar::baz');
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');
160 my $baz = Bar::Baz->new();
161 isa_ok($baz, 'Bar::Baz');
166 is(reftype($baz), 'ARRAY', '... Bar::Baz is made with ARRAY');
169 can_ok($baz, 'has_foo');
170 can_ok($baz, 'get_bar');
171 can_ok($baz, 'set_bar');
173 can_ok($baz, 'has_baz');
174 can_ok($baz, 'bling');
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');
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');
184 $baz->foo('This is Bar::Baz::foo');
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');
191 $baz->baz('This is Bar::Baz::baz');
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');
199 Foo->meta->add_attribute( forgotten => is => "rw" );
201 my $new_baz = Bar::Baz->new;
203 cmp_ok( scalar(@$new_baz), ">", scalar(@$baz), "additional slot due to refreshed meta instance" );