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