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