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