Commit | Line | Data |
52e8a34c |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
efd3d14c |
6 | use Test::More tests => 88; |
9ec169fe |
7 | use File::Spec; |
88dd563c |
8 | use Scalar::Util 'reftype'; |
52e8a34c |
9 | |
efd3d14c |
10 | BEGIN {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 | |
83 | my $foo = Foo->new(); |
84 | isa_ok($foo, 'Foo'); |
85 | |
88dd563c |
86 | is(reftype($foo), 'SCALAR', '... Foo is made with SCALAR'); |
87 | |
52e8a34c |
88 | can_ok($foo, 'foo'); |
89 | can_ok($foo, 'has_foo'); |
90 | can_ok($foo, 'get_bar'); |
91 | can_ok($foo, 'set_bar'); |
92 | |
93 | ok(!$foo->has_foo, '... Foo::foo is not defined yet'); |
94 | is($foo->foo(), undef, '... Foo::foo is not defined yet'); |
95 | is($foo->get_bar(), 'FOO is BAR', '... Foo::bar has been initialized'); |
96 | |
97 | $foo->foo('This is Foo'); |
98 | |
99 | ok($foo->has_foo, '... Foo::foo is defined now'); |
100 | is($foo->foo(), 'This is Foo', '... Foo::foo == "This is Foo"'); |
101 | |
102 | $foo->set_bar(42); |
103 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
104 | |
105 | my $foo2 = Foo->new(); |
106 | isa_ok($foo2, 'Foo'); |
107 | |
88dd563c |
108 | is(reftype($foo2), 'SCALAR', '... Foo is made with SCALAR'); |
109 | |
52e8a34c |
110 | ok(!$foo2->has_foo, '... Foo2::foo is not defined yet'); |
111 | is($foo2->foo(), undef, '... Foo2::foo is not defined yet'); |
112 | is($foo2->get_bar(), 'FOO is BAR', '... Foo2::bar has been initialized'); |
113 | |
114 | $foo2->set_bar('DONT PANIC'); |
115 | is($foo2->get_bar(), 'DONT PANIC', '... Foo2::bar == DONT PANIC'); |
116 | |
117 | is($foo->get_bar(), 42, '... Foo::bar == 42'); |
f892c0f0 |
118 | |
119 | # now Bar ... |
120 | |
121 | my $bar = Bar->new(); |
122 | isa_ok($bar, 'Bar'); |
123 | isa_ok($bar, 'Foo'); |
124 | |
88dd563c |
125 | is(reftype($bar), 'SCALAR', '... Bar is made with SCALAR'); |
126 | |
f892c0f0 |
127 | can_ok($bar, 'foo'); |
128 | can_ok($bar, 'has_foo'); |
129 | can_ok($bar, 'get_bar'); |
130 | can_ok($bar, 'set_bar'); |
131 | can_ok($bar, 'baz'); |
132 | can_ok($bar, 'has_baz'); |
133 | |
134 | ok(!$bar->has_foo, '... Bar::foo is not defined yet'); |
135 | is($bar->foo(), undef, '... Bar::foo is not defined yet'); |
136 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
137 | ok(!$bar->has_baz, '... Bar::baz is not defined yet'); |
138 | is($bar->baz(), undef, '... Bar::baz is not defined yet'); |
139 | |
140 | $bar->foo('This is Bar::foo'); |
141 | |
142 | ok($bar->has_foo, '... Bar::foo is defined now'); |
143 | is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"'); |
144 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
145 | |
146 | $bar->baz('This is Bar::baz'); |
147 | |
148 | ok($bar->has_baz, '... Bar::baz is defined now'); |
149 | is($bar->baz(), 'This is Bar::baz', '... Bar::foo == "This is Bar"'); |
150 | is($bar->foo(), 'This is Bar::foo', '... Bar::foo == "This is Bar"'); |
151 | is($bar->get_bar(), 'FOO is BAR', '... Bar::bar has been initialized'); |
152 | |
153 | # now Baz ... |
154 | |
155 | my $baz = Bar::Baz->new(); |
156 | isa_ok($baz, 'Bar::Baz'); |
157 | isa_ok($baz, 'Bar'); |
158 | isa_ok($baz, 'Foo'); |
159 | isa_ok($baz, 'Baz'); |
160 | |
88dd563c |
161 | is(reftype($baz), 'SCALAR', '... Bar::Baz is made with SCALAR'); |
162 | |
f892c0f0 |
163 | can_ok($baz, 'foo'); |
164 | can_ok($baz, 'has_foo'); |
165 | can_ok($baz, 'get_bar'); |
166 | can_ok($baz, 'set_bar'); |
167 | can_ok($baz, 'baz'); |
168 | can_ok($baz, 'has_baz'); |
169 | can_ok($baz, 'bling'); |
170 | |
171 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
172 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
173 | |
174 | ok(!$baz->has_foo, '... Bar::Baz::foo is not defined yet'); |
175 | is($baz->foo(), undef, '... Bar::Baz::foo is not defined yet'); |
176 | ok(!$baz->has_baz, '... Bar::Baz::baz is not defined yet'); |
177 | is($baz->baz(), undef, '... Bar::Baz::baz is not defined yet'); |
178 | |
179 | $baz->foo('This is Bar::Baz::foo'); |
180 | |
181 | ok($baz->has_foo, '... Bar::Baz::foo is defined now'); |
182 | is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"'); |
183 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
184 | is($baz->bling(), 'Baz::bling', '... Bar::Baz::bling has been initialized'); |
185 | |
186 | $baz->baz('This is Bar::Baz::baz'); |
187 | |
188 | ok($baz->has_baz, '... Bar::Baz::baz is defined now'); |
189 | is($baz->baz(), 'This is Bar::Baz::baz', '... Bar::Baz::foo == "This is Bar"'); |
190 | is($baz->foo(), 'This is Bar::Baz::foo', '... Bar::Baz::foo == "This is Bar"'); |
191 | is($baz->get_bar(), 'FOO is BAR', '... Bar::Baz::bar has been initialized'); |
192 | is($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 | } |