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