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