microoptimize Class::MOP::Class::initialize since it's called so often
[gitmo/Class-MOP.git] / t / 102_InsideOutClass_test.t
CommitLineData
52e8a34c 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
88dd563c 6use Test::More tests => 89;
9ec169fe 7use File::Spec;
88dd563c 8use Scalar::Util 'reftype';
52e8a34c 9
10BEGIN {
11 use_ok('Class::MOP');
10dd437b 12 require_ok(File::Spec->catfile('examples', 'InsideOutClass.pod'));
52e8a34c 13}
14
15{
16 package Foo;
17
f892c0f0 18 use strict;
19 use warnings;
20
1becdfcc 21 use metaclass (
c23184fc 22 'attribute_metaclass' => 'InsideOutClass::Attribute',
23 'instance_metaclass' => 'InsideOutClass::Instance'
677eb158 24 );
52e8a34c 25
2e41896e 26 Foo->meta->add_attribute('foo' => (
27 accessor => 'foo',
28 predicate => 'has_foo',
29 ));
52e8a34c 30
2e41896e 31 Foo->meta->add_attribute('bar' => (
32 reader => 'get_bar',
33 writer => 'set_bar',
34 default => 'FOO is BAR'
35 ));
52e8a34c 36
37 sub new {
38 my $class = shift;
5659d76e 39 $class->meta->new_object(@_);
52e8a34c 40 }
f892c0f0 41
42 package Bar;
caab0eaa 43 use metaclass (
44 'attribute_metaclass' => 'InsideOutClass::Attribute',
45 'instance_metaclass' => 'InsideOutClass::Instance'
46 );
f892c0f0 47
48 use strict;
49 use warnings;
50
51 use base 'Foo';
52
53 Bar->meta->add_attribute('baz' => (
54 accessor => 'baz',
55 predicate => 'has_baz',
56 ));
57
58 package Baz;
59
60 use strict;
61 use warnings;
1becdfcc 62 use metaclass (
c23184fc 63 'attribute_metaclass' => 'InsideOutClass::Attribute',
64 'instance_metaclass' => 'InsideOutClass::Instance'
f892c0f0 65 );
66
67 Baz->meta->add_attribute('bling' => (
68 accessor => 'bling',
69 default => 'Baz::bling'
70 ));
71
72 package Bar::Baz;
caab0eaa 73 use metaclass (
74 'attribute_metaclass' => 'InsideOutClass::Attribute',
75 'instance_metaclass' => 'InsideOutClass::Instance'
76 );
f892c0f0 77
78 use strict;
79 use warnings;
80
81 use base 'Bar', 'Baz';
52e8a34c 82}
83
84my $foo = Foo->new();
85isa_ok($foo, 'Foo');
86
88dd563c 87is(reftype($foo), 'SCALAR', '... Foo is made with SCALAR');
88
52e8a34c 89can_ok($foo, 'foo');
90can_ok($foo, 'has_foo');
91can_ok($foo, 'get_bar');
92can_ok($foo, 'set_bar');
93
94ok(!$foo->has_foo, '... Foo::foo is not defined yet');
95is($foo->foo(), undef, '... Foo::foo is not defined yet');
96is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
97
98$foo->foo('This is Foo');
99
100ok($foo->has_foo, '... Foo::foo is defined now');
101is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
102
103$foo->set_bar(42);
104is($foo->get_bar(), 42, '... Foo::bar == 42');
105
106my $foo2 = Foo->new();
107isa_ok($foo2, 'Foo');
108
88dd563c 109is(reftype($foo2), 'SCALAR', '... Foo is made with SCALAR');
110
52e8a34c 111ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
112is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
113is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
114
115$foo2->set_bar('DONT PANIC');
116is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
117
118is($foo->get_bar(), 42, '... Foo::bar == 42');
f892c0f0 119
120# now Bar ...
121
122my $bar = Bar->new();
123isa_ok($bar, 'Bar');
124isa_ok($bar, 'Foo');
125
88dd563c 126is(reftype($bar), 'SCALAR', '... Bar is made with SCALAR');
127
f892c0f0 128can_ok($bar, 'foo');
129can_ok($bar, 'has_foo');
130can_ok($bar, 'get_bar');
131can_ok($bar, 'set_bar');
132can_ok($bar, 'baz');
133can_ok($bar, 'has_baz');
134
135ok(!$bar->has_foo, '... Bar::foo is not defined yet');
136is($bar->foo(), undef, '... Bar::foo is not defined yet');
137is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
138ok(!$bar->has_baz, '... Bar::baz is not defined yet');
139is($bar->baz(), undef, '... Bar::baz is not defined yet');
140
141$bar->foo('This is Bar::foo');
142
143ok($bar->has_foo, '... Bar::foo is defined now');
144is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
145is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
146
147$bar->baz('This is Bar::baz');
148
149ok($bar->has_baz, '... Bar::baz is defined now');
150is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
151is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
152is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
153
154# now Baz ...
155
156my $baz = Bar::Baz->new();
157isa_ok($baz, 'Bar::Baz');
158isa_ok($baz, 'Bar');
159isa_ok($baz, 'Foo');
160isa_ok($baz, 'Baz');
161
88dd563c 162is(reftype($baz), 'SCALAR', '... Bar::Baz is made with SCALAR');
163
f892c0f0 164can_ok($baz, 'foo');
165can_ok($baz, 'has_foo');
166can_ok($baz, 'get_bar');
167can_ok($baz, 'set_bar');
168can_ok($baz, 'baz');
169can_ok($baz, 'has_baz');
170can_ok($baz, 'bling');
171
172is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
173is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
174
175ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
176is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
177ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
178is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');
179
180$baz->foo('This is Bar::Baz::foo');
181
182ok($baz->has_foo, '... Bar::Baz::foo is defined now');
183is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
184is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
185is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
186
187$baz->baz('This is Bar::Baz::baz');
188
189ok($baz->has_baz, '... Bar::Baz::baz is defined now');
190is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
191is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
192is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
193is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
194
62189f84 195{
196 no strict 'refs';
197
198 ok(*{'Foo::foo'}{HASH}, '... there is a foo package variable in Foo');
199 ok(*{'Foo::bar'}{HASH}, '... there is a bar package variable in Foo');
200
201 is(scalar(keys(%{'Foo::foo'})), 4, '... got the right number of entries for Foo::foo');
202 is(scalar(keys(%{'Foo::bar'})), 4, '... got the right number of entries for Foo::bar');
203
204 ok(!*{'Bar::foo'}{HASH}, '... no foo package variable in Bar');
205 ok(!*{'Bar::bar'}{HASH}, '... no bar package variable in Bar');
206 ok(*{'Bar::baz'}{HASH}, '... there is a baz package variable in Bar');
207
208 is(scalar(keys(%{'Bar::foo'})), 0, '... got the right number of entries for Bar::foo');
209 is(scalar(keys(%{'Bar::bar'})), 0, '... got the right number of entries for Bar::bar');
210 is(scalar(keys(%{'Bar::baz'})), 2, '... got the right number of entries for Bar::baz');
211
212 ok(*{'Baz::bling'}{HASH}, '... there is a bar package variable in Baz');
213
214 is(scalar(keys(%{'Baz::bling'})), 1, '... got the right number of entries for Baz::bling');
215
216 ok(!*{'Bar::Baz::foo'}{HASH}, '... no foo package variable in Bar::Baz');
217 ok(!*{'Bar::Baz::bar'}{HASH}, '... no bar package variable in Bar::Baz');
218 ok(!*{'Bar::Baz::baz'}{HASH}, '... no baz package variable in Bar::Baz');
219 ok(!*{'Bar::Baz::bling'}{HASH}, '... no bar package variable in Baz::Baz');
220
221 is(scalar(keys(%{'Bar::Baz::foo'})), 0, '... got the right number of entries for Bar::Baz::foo');
222 is(scalar(keys(%{'Bar::Baz::bar'})), 0, '... got the right number of entries for Bar::Baz::bar');
223 is(scalar(keys(%{'Bar::Baz::baz'})), 0, '... got the right number of entries for Bar::Baz::baz');
224 is(scalar(keys(%{'Bar::Baz::bling'})), 0, '... got the right number of entries for Bar::Baz::bling');
caab0eaa 225}