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