merging the immutable branch into trunk
[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');
9ec169fe 12 require_ok(File::Spec->catdir('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;
43
44 use strict;
45 use warnings;
46
47 use base 'Foo';
48
49 Bar->meta->add_attribute('baz' => (
50 accessor => 'baz',
51 predicate => 'has_baz',
52 ));
53
54 package Baz;
55
56 use strict;
57 use warnings;
1becdfcc 58 use metaclass (
c23184fc 59 'attribute_metaclass' => 'InsideOutClass::Attribute',
60 'instance_metaclass' => 'InsideOutClass::Instance'
f892c0f0 61 );
62
63 Baz->meta->add_attribute('bling' => (
64 accessor => 'bling',
65 default => 'Baz::bling'
66 ));
67
68 package Bar::Baz;
69
70 use strict;
71 use warnings;
72
73 use base 'Bar', 'Baz';
52e8a34c 74}
75
76my $foo = Foo->new();
77isa_ok($foo, 'Foo');
78
88dd563c 79is(reftype($foo), 'SCALAR', '... Foo is made with SCALAR');
80
52e8a34c 81can_ok($foo, 'foo');
82can_ok($foo, 'has_foo');
83can_ok($foo, 'get_bar');
84can_ok($foo, 'set_bar');
85
86ok(!$foo->has_foo, '... Foo::foo is not defined yet');
87is($foo->foo(), undef, '... Foo::foo is not defined yet');
88is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized');
89
90$foo->foo('This is Foo');
91
92ok($foo->has_foo, '... Foo::foo is defined now');
93is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"');
94
95$foo->set_bar(42);
96is($foo->get_bar(), 42, '... Foo::bar == 42');
97
98my $foo2 = Foo->new();
99isa_ok($foo2, 'Foo');
100
88dd563c 101is(reftype($foo2), 'SCALAR', '... Foo is made with SCALAR');
102
52e8a34c 103ok(!$foo2->has_foo, '... Foo2::foo is not defined yet');
104is($foo2->foo(), undef, '... Foo2::foo is not defined yet');
105is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized');
106
107$foo2->set_bar('DONT PANIC');
108is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC');
109
110is($foo->get_bar(), 42, '... Foo::bar == 42');
f892c0f0 111
112# now Bar ...
113
114my $bar = Bar->new();
115isa_ok($bar, 'Bar');
116isa_ok($bar, 'Foo');
117
88dd563c 118is(reftype($bar), 'SCALAR', '... Bar is made with SCALAR');
119
f892c0f0 120can_ok($bar, 'foo');
121can_ok($bar, 'has_foo');
122can_ok($bar, 'get_bar');
123can_ok($bar, 'set_bar');
124can_ok($bar, 'baz');
125can_ok($bar, 'has_baz');
126
127ok(!$bar->has_foo, '... Bar::foo is not defined yet');
128is($bar->foo(), undef, '... Bar::foo is not defined yet');
129is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
130ok(!$bar->has_baz, '... Bar::baz is not defined yet');
131is($bar->baz(), undef, '... Bar::baz is not defined yet');
132
133$bar->foo('This is Bar::foo');
134
135ok($bar->has_foo, '... Bar::foo is defined now');
136is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
137is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
138
139$bar->baz('This is Bar::baz');
140
141ok($bar->has_baz, '... Bar::baz is defined now');
142is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"');
143is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"');
144is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized');
145
146# now Baz ...
147
148my $baz = Bar::Baz->new();
149isa_ok($baz, 'Bar::Baz');
150isa_ok($baz, 'Bar');
151isa_ok($baz, 'Foo');
152isa_ok($baz, 'Baz');
153
88dd563c 154is(reftype($baz), 'SCALAR', '... Bar::Baz is made with SCALAR');
155
f892c0f0 156can_ok($baz, 'foo');
157can_ok($baz, 'has_foo');
158can_ok($baz, 'get_bar');
159can_ok($baz, 'set_bar');
160can_ok($baz, 'baz');
161can_ok($baz, 'has_baz');
162can_ok($baz, 'bling');
163
164is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
165is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
166
167ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet');
168is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet');
169ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet');
170is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet');
171
172$baz->foo('This is Bar::Baz::foo');
173
174ok($baz->has_foo, '... Bar::Baz::foo is defined now');
175is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"');
176is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized');
177is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized');
178
179$baz->baz('This is Bar::Baz::baz');
180
181ok($baz->has_baz, '... Bar::Baz::baz is defined now');
182is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"');
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
62189f84 187{
188 no strict 'refs';
189
190 ok(*{'Foo::foo'}{HASH}, '... there is a foo package variable in Foo');
191 ok(*{'Foo::bar'}{HASH}, '... there is a bar package variable in Foo');
192
193 is(scalar(keys(%{'Foo::foo'})), 4, '... got the right number of entries for Foo::foo');
194 is(scalar(keys(%{'Foo::bar'})), 4, '... got the right number of entries for Foo::bar');
195
196 ok(!*{'Bar::foo'}{HASH}, '... no foo package variable in Bar');
197 ok(!*{'Bar::bar'}{HASH}, '... no bar package variable in Bar');
198 ok(*{'Bar::baz'}{HASH}, '... there is a baz package variable in Bar');
199
200 is(scalar(keys(%{'Bar::foo'})), 0, '... got the right number of entries for Bar::foo');
201 is(scalar(keys(%{'Bar::bar'})), 0, '... got the right number of entries for Bar::bar');
202 is(scalar(keys(%{'Bar::baz'})), 2, '... got the right number of entries for Bar::baz');
203
204 ok(*{'Baz::bling'}{HASH}, '... there is a bar package variable in Baz');
205
206 is(scalar(keys(%{'Baz::bling'})), 1, '... got the right number of entries for Baz::bling');
207
208 ok(!*{'Bar::Baz::foo'}{HASH}, '... no foo package variable in Bar::Baz');
209 ok(!*{'Bar::Baz::bar'}{HASH}, '... no bar package variable in Bar::Baz');
210 ok(!*{'Bar::Baz::baz'}{HASH}, '... no baz package variable in Bar::Baz');
211 ok(!*{'Bar::Baz::bling'}{HASH}, '... no bar package variable in Baz::Baz');
212
213 is(scalar(keys(%{'Bar::Baz::foo'})), 0, '... got the right number of entries for Bar::Baz::foo');
214 is(scalar(keys(%{'Bar::Baz::bar'})), 0, '... got the right number of entries for Bar::Baz::bar');
215 is(scalar(keys(%{'Bar::Baz::baz'})), 0, '... got the right number of entries for Bar::Baz::baz');
216 is(scalar(keys(%{'Bar::Baz::bling'})), 0, '... got the right number of entries for Bar::Baz::bling');
217}