stop using excludes within moose, since it's no longer necessary
[gitmo/Moose.git] / t / cmop / InsideOutClass_test.t
CommitLineData
38bf2a25 1use strict;
2use warnings;
3
4use Test::More;
5use File::Spec;
6use Scalar::Util 'reftype';
7
8BEGIN {use Class::MOP;
9 require_ok(File::Spec->catfile('examples', 'InsideOutClass.pod'));
10}
11
12{
13 package Foo;
14
15 use strict;
16 use warnings;
17
18 use metaclass (
19 'attribute_metaclass' => 'InsideOutClass::Attribute',
20 'instance_metaclass' => 'InsideOutClass::Instance'
21 );
22
23 Foo->meta->add_attribute('foo' => (
24 accessor => 'foo',
25 predicate => 'has_foo',
26 ));
27
28 Foo->meta->add_attribute('bar' => (
29 reader => 'get_bar',
30 writer => 'set_bar',
31 default => 'FOO is BAR'
32 ));
33
34 sub new {
35 my $class = shift;
36 $class->meta->new_object(@_);
37 }
38
39 package Bar;
40 use metaclass (
41 'attribute_metaclass' => 'InsideOutClass::Attribute',
42 'instance_metaclass' => 'InsideOutClass::Instance'
43 );
44
45 use strict;
46 use warnings;
47
48 use base 'Foo';
49
50 Bar->meta->add_attribute('baz' => (
51 accessor => 'baz',
52 predicate => 'has_baz',
53 ));
54
55 package Baz;
56
57 use strict;
58 use warnings;
59 use metaclass (
60 'attribute_metaclass' => 'InsideOutClass::Attribute',
61 'instance_metaclass' => 'InsideOutClass::Instance'
62 );
63
64 Baz->meta->add_attribute('bling' => (
65 accessor => 'bling',
66 default => 'Baz::bling'
67 ));
68
69 package Bar::Baz;
70 use metaclass (
71 'attribute_metaclass' => 'InsideOutClass::Attribute',
72 'instance_metaclass' => 'InsideOutClass::Instance'
73 );
74
75 use strict;
76 use warnings;
77
78 use base 'Bar', 'Baz';
79}
80
81my $foo = Foo->new();
82isa_ok($foo, 'Foo');
83
84is(reftype($foo), 'SCALAR', '... Foo is made with SCALAR');
85
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
106is(reftype($foo2), 'SCALAR', '... Foo is made with SCALAR');
107
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');
116
117# now Bar ...
118
119my $bar = Bar->new();
120isa_ok($bar, 'Bar');
121isa_ok($bar, 'Foo');
122
123is(reftype($bar), 'SCALAR', '... Bar is made with SCALAR');
124
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
159is(reftype($baz), 'SCALAR', '... Bar::Baz is made with SCALAR');
160
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
192{
193 no strict 'refs';
194
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');
199 is(scalar(keys(%{'Foo::bar'})), 4, '... got the right number of entries for Foo::bar');
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');
208
209 ok(*{'Baz::bling'}{HASH}, '... there is a bar package variable in Baz');
210
211 is(scalar(keys(%{'Baz::bling'})), 1, '... got the right number of entries for Baz::bling');
212
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');
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');
222}
223
224done_testing;