Commit | Line | Data |
ddd87d75 |
1 | package MooseClass::Tests; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
5cab7e05 |
6 | use Lingua::EN::Inflect qw( A PL_N ); |
ddd87d75 |
7 | use Test::More 'no_plan'; |
8 | |
9 | sub tests01 { |
ddd87d75 |
10 | has_meta('Person'); |
11 | |
12 | check_isa( 'Person', ['Moose::Object'] ); |
13 | |
ddd87d75 |
14 | has_rw_attr( 'Person', $_ ) for qw( first_name last_name ); |
15 | |
16 | has_method( 'Person', 'full_name' ); |
17 | |
ddd87d75 |
18 | person01(); |
19 | |
20 | has_meta('Employee'); |
21 | |
22 | check_isa( 'Employee', [ 'Person', 'Moose::Object' ] ); |
23 | |
8d1ce1d7 |
24 | has_rw_attr( 'Employee', $_ ) for qw( title salary ); |
ddd87d75 |
25 | has_ro_attr( 'Employee', 'ssn' ); |
26 | |
27 | has_overridden_method( 'Employee', 'full_name' ); |
28 | |
29 | employee01(); |
b071f963 |
30 | |
31 | no_droppings('Person'); |
32 | is_immutable('Person'); |
ddd87d75 |
33 | } |
34 | |
5cab7e05 |
35 | sub tests02 { |
70eec86e |
36 | has_meta('Printable'); |
37 | requires_method( 'Printable', 'as_string' ); |
38 | |
39 | has_meta('Person'); |
40 | does_role( 'Person', 'Printable' ); |
41 | has_method( 'Person', 'as_string' ); |
5cab7e05 |
42 | |
70eec86e |
43 | has_meta('HasAccount'); |
44 | has_method( 'HasAccount', $_ ) for qw( deposit withdraw ); |
45 | has_role_attr( 'HasAccount', 'balance' ); |
46 | |
47 | does_role( 'Person', 'HasAccount' ); |
48 | has_method( 'Person', $_ ) for qw( deposit withdraw ); |
5cab7e05 |
49 | has_rw_attr( 'Person', 'balance' ); |
50 | |
70eec86e |
51 | has_meta('Employee'); |
5cab7e05 |
52 | does_role( 'Employee', $_ ) for qw( Printable HasAccount ); |
53 | |
54 | person02(); |
55 | employee02(); |
70eec86e |
56 | |
57 | no_droppings($_) for qw( Printable HasAccount ); |
58 | |
59 | tests01(); |
5cab7e05 |
60 | } |
61 | |
8d1ce1d7 |
62 | sub tests03 { |
00c47fc4 |
63 | has_meta('Person'); |
8d1ce1d7 |
64 | |
39182c07 |
65 | for my $name ( qw( first_name last_name ) ) { |
66 | has_rw_attr( 'Person', $name ); |
8d1ce1d7 |
67 | |
39182c07 |
68 | my $attr = Person->meta->get_attribute($name); |
69 | ok( $attr && $attr->is_required, |
70 | "$name is required in Person" ); |
71 | } |
8d1ce1d7 |
72 | |
39182c07 |
73 | has_rw_attr( 'Person', 'title' ); |
8d1ce1d7 |
74 | |
75 | my $person_title_attr = Person->meta->get_attribute('title'); |
76 | ok( !$person_title_attr->is_required, 'title is not required in Person' ); |
70eec86e |
77 | is( |
78 | $person_title_attr->predicate, 'has_title', |
79 | 'Person title attr has a has_title predicate' |
80 | ); |
81 | is( |
82 | $person_title_attr->clearer, 'clear_title', |
83 | 'Person title attr has a clear_title clearer' |
84 | ); |
8d1ce1d7 |
85 | |
39182c07 |
86 | person03(); |
87 | |
88 | has_meta('Employee'); |
89 | |
90 | has_rw_attr( 'Employee', 'title', 'overridden' ); |
8d1ce1d7 |
91 | |
92 | my $employee_title_attr = Employee->meta->get_attribute('title'); |
70eec86e |
93 | is( |
94 | $employee_title_attr->default, 'Worker', |
95 | 'title defaults to Worker in Employee' |
96 | ); |
8d1ce1d7 |
97 | |
39182c07 |
98 | ok( |
99 | !Employee->meta->has_method('full_name'), |
100 | 'Employee no longer implements a full_name method' |
101 | ); |
102 | |
103 | has_ro_attr( 'Employee', 'salary' ); |
8d1ce1d7 |
104 | |
105 | my $salary_attr = Employee->meta->get_attribute('salary'); |
39182c07 |
106 | ok( $salary_attr->is_lazy, 'salary is lazy' ); |
8d1ce1d7 |
107 | ok( !$salary_attr->init_arg, 'no init_arg for salary attribute' ); |
108 | ok( $salary_attr->has_builder, 'salary attr has a builder' ); |
109 | |
39182c07 |
110 | has_method( 'Employee', '_build_salary' ); |
111 | |
112 | has_rw_attr( 'Employee', 'salary_level' ); |
113 | |
114 | my $salary_level_attr = Employee->meta->get_attribute('salary_level'); |
115 | is( $salary_level_attr->default, 1, 'salary_level defaults to 1' ); |
116 | |
8d1ce1d7 |
117 | employee03(); |
39182c07 |
118 | |
119 | my $balance_attr = Person->meta->get_attribute('balance'); |
120 | is( $balance_attr->default, 100, 'balance defaults to 100' ); |
8d1ce1d7 |
121 | } |
122 | |
26164c8d |
123 | sub tests04 { |
00c47fc4 |
124 | has_meta('Document'); |
00c47fc4 |
125 | has_ro_attr( 'Document', $_ ) for qw( title author ); |
efc3139a |
126 | |
127 | has_meta('Report'); |
00c47fc4 |
128 | has_ro_attr( 'Report', 'summary' ); |
efc3139a |
129 | |
130 | has_meta('TPSReport'); |
00c47fc4 |
131 | has_ro_attr( 'TPSReport', $_ ) for qw( t p s ); |
538499df |
132 | |
00c47fc4 |
133 | has_method( 'Document', 'output' ); |
134 | has_augmented_method( 'Report', 'output' ); |
135 | has_augmented_method( 'TPSReport', 'output' ); |
26164c8d |
136 | |
538499df |
137 | my $tps = TPSReport->new( |
138 | title => 'That TPS Report', |
139 | author => 'Peter Gibbons (for Bill Lumberg)', |
140 | summary => 'I celebrate his whole collection!', |
141 | t => 'PC Load Letter', |
142 | p => 'Swingline', |
143 | s => 'flair!', |
144 | ); |
26164c8d |
145 | |
538499df |
146 | my $output = $tps->output; |
147 | $output =~ s/\n\n+/\n/g; |
26164c8d |
148 | |
538499df |
149 | is( $output, <<'EOF', 'output returns expected report' ); |
150 | That TPS Report |
151 | I celebrate his whole collection! |
152 | t: PC Load Letter |
153 | p: Swingline |
154 | s: flair! |
155 | Written by Peter Gibbons (for Bill Lumberg) |
156 | EOF |
efc3139a |
157 | |
158 | no_droppings('Document'); |
159 | no_droppings('Report'); |
160 | no_droppings('TPSReport'); |
26164c8d |
161 | } |
162 | |
ad648c43 |
163 | sub tests05 { |
00c47fc4 |
164 | has_meta('Person'); |
ad648c43 |
165 | |
70eec86e |
166 | for my $attr_name (qw( first_name last_name title )) { |
ad648c43 |
167 | my $attr = Person->meta->get_attribute($attr_name); |
168 | |
70eec86e |
169 | ok( |
170 | $attr->has_type_constraint, |
171 | "Person $attr_name has a type constraint" |
172 | ); |
173 | is( |
174 | $attr->type_constraint->name, 'Str', |
175 | "Person $attr_name type is Str" |
176 | ); |
ad648c43 |
177 | } |
178 | |
21c6ab1c |
179 | has_meta('Employee'); |
180 | |
ad648c43 |
181 | { |
182 | my $salary_level_attr = Employee->meta->get_attribute('salary_level'); |
70eec86e |
183 | ok( |
184 | $salary_level_attr->has_type_constraint, |
185 | 'Employee salary_level has a type constraint' |
186 | ); |
ad648c43 |
187 | |
188 | my $tc = $salary_level_attr->type_constraint; |
189 | |
190 | for my $invalid ( 0, 11, -14, 'foo', undef ) { |
191 | my $str = defined $invalid ? $invalid : 'undef'; |
70eec86e |
192 | ok( |
193 | !$tc->check($invalid), |
194 | "salary_level type rejects invalid value - $str" |
195 | ); |
ad648c43 |
196 | } |
197 | |
70eec86e |
198 | for my $valid ( 1 .. 10 ) { |
199 | ok( |
200 | $tc->check($valid), |
201 | "salary_level type accepts valid value - $valid" |
202 | ); |
ad648c43 |
203 | } |
204 | } |
205 | |
206 | { |
207 | my $salary_attr = Employee->meta->get_attribute('salary'); |
208 | |
70eec86e |
209 | ok( |
210 | $salary_attr->has_type_constraint, |
211 | 'Employee salary has a type constraint' |
212 | ); |
ad648c43 |
213 | |
214 | my $tc = $salary_attr->type_constraint; |
215 | |
216 | for my $invalid ( 0, -14, 'foo', undef ) { |
217 | my $str = defined $invalid ? $invalid : 'undef'; |
70eec86e |
218 | ok( |
219 | !$tc->check($invalid), |
220 | "salary type rejects invalid value - $str" |
221 | ); |
ad648c43 |
222 | } |
223 | |
224 | for my $valid ( 1, 100_000, 10**10 ) { |
70eec86e |
225 | ok( |
226 | $tc->check($valid), |
227 | "salary type accepts valid value - $valid" |
228 | ); |
ad648c43 |
229 | } |
230 | } |
231 | |
232 | { |
233 | my $ssn_attr = Employee->meta->get_attribute('ssn'); |
234 | |
70eec86e |
235 | ok( |
236 | $ssn_attr->has_type_constraint, |
237 | 'Employee ssn has a type constraint' |
238 | ); |
ad648c43 |
239 | |
240 | my $tc = $ssn_attr->type_constraint; |
241 | |
242 | for my $invalid ( 0, -14, 'foo', undef, '123-ab-1241', '123456789' ) { |
243 | my $str = defined $invalid ? $invalid : 'undef'; |
70eec86e |
244 | ok( |
245 | !$tc->check($invalid), |
246 | "ssn type rejects invalid value - $str" |
247 | ); |
ad648c43 |
248 | } |
249 | |
250 | for my $valid ( '041-12-1251', '123-45-6789', '926-41-5820' ) { |
70eec86e |
251 | ok( |
252 | $tc->check($valid), |
253 | "ssn type accepts valid value - $valid" |
254 | ); |
ad648c43 |
255 | } |
256 | } |
21c6ab1c |
257 | |
258 | no_droppings('Employee'); |
ad648c43 |
259 | } |
260 | |
66b226e5 |
261 | sub tests06 { |
00c47fc4 |
262 | has_meta('BankAccount'); |
66b226e5 |
263 | |
5a6ef0a4 |
264 | has_rw_attr( 'BankAccount', $_ ) for qw( balance owner ); |
66b226e5 |
265 | |
36e1e336 |
266 | my $ba_meta = BankAccount->meta; |
5a6ef0a4 |
267 | |
70eec86e |
268 | ok( |
5a6ef0a4 |
269 | $ba_meta->get_attribute('owner')->is_weak_ref, |
270 | 'owner attribute is a weak ref' |
70eec86e |
271 | ); |
36e1e336 |
272 | |
5a6ef0a4 |
273 | has_method( 'BankAccount', $_ ) for qw( deposit withdraw ); |
274 | |
275 | has_ro_attr( 'BankAccount', 'history' ); |
276 | |
ed84c5c6 |
277 | my $history_attr = $ba_meta->get_attribute('history'); |
278 | |
5a6ef0a4 |
279 | is_deeply( |
280 | $history_attr->default->(), |
281 | [], |
282 | 'BankAccount history attribute defaults to []' |
283 | ); |
284 | |
285 | { |
286 | my $tc = $history_attr->type_constraint; |
287 | |
288 | for my $invalid ( 0, 42, undef, {}, [ 'foo', 'bar' ] ) { |
289 | my $str = defined $invalid ? $invalid : 'undef'; |
290 | ok( |
291 | !$tc->check($invalid), |
292 | "salary_level type rejects invalid value - $str" |
293 | ); |
294 | } |
295 | |
296 | for my $valid ( [], [1], [ 1, 2, 3 ], [ 1, -10, 9999 ] ) { |
297 | ok( |
298 | $tc->check($valid), |
299 | "salary_level type accepts valid value" |
300 | ); |
301 | } |
302 | } |
303 | |
ed84c5c6 |
304 | ok( |
305 | $history_attr->meta() |
306 | ->does_role('Moose::Meta::Attribute::Native::Trait::Array'), |
307 | 'BankAccount history attribute uses native delegation to an array ref' |
308 | ); |
309 | |
70eec86e |
310 | ok( |
311 | $ba_meta->get_attribute('balance')->has_trigger, |
312 | 'BankAccount balance attribute has a trigger' |
313 | ); |
36e1e336 |
314 | |
5a6ef0a4 |
315 | has_meta('Person'); |
316 | |
66b226e5 |
317 | my $person_meta = Person->meta; |
5a6ef0a4 |
318 | |
319 | ok( !$person_meta->does_role('HasAccount'), |
320 | 'Person class does not do the HasAccount role' ); |
321 | |
70eec86e |
322 | ok( |
323 | !$person_meta->has_attribute('balance'), |
324 | 'Person class does not have a balance attribute' |
325 | ); |
66b226e5 |
326 | |
327 | my $deposit_meth = $person_meta->get_method('deposit'); |
328 | isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' ); |
329 | |
330 | my $withdraw_meth = $person_meta->get_method('withdraw'); |
331 | isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' ); |
332 | |
66b226e5 |
333 | person06(); |
5a6ef0a4 |
334 | |
335 | has_meta('Employee'); |
336 | |
337 | no_droppings('BankAccount'); |
66b226e5 |
338 | } |
339 | |
ddd87d75 |
340 | sub has_meta { |
70eec86e |
341 | my $package = shift; |
ddd87d75 |
342 | |
00c47fc4 |
343 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
344 | |
70eec86e |
345 | use_ok($package) |
346 | or BAIL_OUT("$package cannot be loaded"); |
5c7cd208 |
347 | |
70eec86e |
348 | ok( $package->can('meta'), "$package has a meta() method" ) |
349 | or BAIL_OUT( |
350 | "$package does not have a meta() method (did you forget to 'use Moose'?)" |
351 | ); |
ddd87d75 |
352 | } |
353 | |
354 | sub check_isa { |
355 | my $class = shift; |
356 | my $parents = shift; |
357 | |
00c47fc4 |
358 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
359 | |
ddd87d75 |
360 | my @isa = $class->meta->linearized_isa; |
361 | shift @isa; # returns $class as the first entry |
362 | |
363 | my $count = scalar @{$parents}; |
364 | my $noun = PL_N( 'parent', $count ); |
365 | |
366 | is( scalar @isa, $count, "$class has $count $noun" ); |
367 | |
368 | for ( my $i = 0; $i < @{$parents}; $i++ ) { |
369 | is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" ); |
370 | } |
371 | } |
372 | |
ddd87d75 |
373 | sub has_rw_attr { |
605c1144 |
374 | my $class = shift; |
375 | my $name = shift; |
376 | my $overridden = shift; |
ddd87d75 |
377 | |
00c47fc4 |
378 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
379 | |
605c1144 |
380 | my $articled = $overridden ? "an overridden $name" : A($name); |
70eec86e |
381 | ok( |
382 | $class->meta->has_attribute($name), |
383 | "$class has $articled attribute" |
384 | ); |
ddd87d75 |
385 | |
386 | my $attr = $class->meta->get_attribute($name); |
387 | |
70eec86e |
388 | is( |
389 | $attr->get_read_method, $name, |
390 | "$name attribute has a reader accessor - $name()" |
391 | ); |
392 | is( |
393 | $attr->get_write_method, $name, |
394 | "$name attribute has a writer accessor - $name()" |
395 | ); |
ddd87d75 |
396 | } |
397 | |
398 | sub has_ro_attr { |
399 | my $class = shift; |
400 | my $name = shift; |
401 | |
00c47fc4 |
402 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
403 | |
8d1ce1d7 |
404 | my $articled = A($name); |
70eec86e |
405 | ok( |
406 | $class->meta->has_attribute($name), |
407 | "$class has $articled attribute" |
408 | ); |
ddd87d75 |
409 | |
410 | my $attr = $class->meta->get_attribute($name); |
411 | |
70eec86e |
412 | is( |
413 | $attr->get_read_method, $name, |
414 | "$name attribute has a reader accessor - $name()" |
415 | ); |
416 | is( |
417 | $attr->get_write_method, undef, |
418 | "$name attribute does not have a writer" |
419 | ); |
420 | } |
421 | |
422 | sub has_role_attr { |
423 | my $role = shift; |
424 | my $name = shift; |
425 | |
00c47fc4 |
426 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
427 | |
70eec86e |
428 | my $articled = A($name); |
429 | ok( |
430 | $role->meta->get_attribute($name), |
431 | "$role has $articled attribute" |
432 | ); |
ddd87d75 |
433 | } |
434 | |
435 | sub has_method { |
70eec86e |
436 | my $package = shift; |
437 | my $name = shift; |
ddd87d75 |
438 | |
00c47fc4 |
439 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
440 | |
8d1ce1d7 |
441 | my $articled = A($name); |
70eec86e |
442 | ok( $package->meta->has_method($name), "$package has $articled method" ); |
ddd87d75 |
443 | } |
444 | |
445 | sub has_overridden_method { |
70eec86e |
446 | my $package = shift; |
447 | my $name = shift; |
ddd87d75 |
448 | |
00c47fc4 |
449 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
450 | |
8d1ce1d7 |
451 | my $articled = A($name); |
70eec86e |
452 | ok( $package->meta->has_method($name), "$package has $articled method" ); |
ddd87d75 |
453 | |
70eec86e |
454 | my $meth = $package->meta->get_method($name); |
ddd87d75 |
455 | isa_ok( $meth, 'Moose::Meta::Method::Overridden' ); |
456 | } |
457 | |
538499df |
458 | sub has_augmented_method { |
459 | my $class = shift; |
460 | my $name = shift; |
461 | |
00c47fc4 |
462 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
463 | |
538499df |
464 | my $articled = A($name); |
465 | ok( $class->meta->has_method($name), "$class has $articled method" ); |
466 | |
467 | my $meth = $class->meta->get_method($name); |
468 | isa_ok( $meth, 'Moose::Meta::Method::Augmented' ); |
469 | } |
470 | |
70eec86e |
471 | sub requires_method { |
472 | my $package = shift; |
473 | my $method = shift; |
474 | |
00c47fc4 |
475 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
476 | |
70eec86e |
477 | ok( |
478 | $package->meta->requires_method($method), |
479 | "$package requires the method $method" |
480 | ); |
481 | } |
482 | |
ddd87d75 |
483 | sub no_droppings { |
70eec86e |
484 | my $package = shift; |
ddd87d75 |
485 | |
00c47fc4 |
486 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
487 | |
70eec86e |
488 | ok( !$package->can('has'), "no Moose droppings in $package" ); |
489 | ok( !$package->can('subtype'), |
490 | "no Moose::Util::TypeConstraints droppings in $package" ); |
ddd87d75 |
491 | } |
492 | |
493 | sub is_immutable { |
494 | my $class = shift; |
495 | |
00c47fc4 |
496 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
497 | |
ddd87d75 |
498 | ok( $class->meta->is_immutable, "$class has been made immutable" ); |
499 | } |
500 | |
5cab7e05 |
501 | sub does_role { |
70eec86e |
502 | my $package = shift; |
503 | my $role = shift; |
5cab7e05 |
504 | |
00c47fc4 |
505 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
506 | |
70eec86e |
507 | ok( $package->meta->does_role($role), "$package does the $role role" ); |
5cab7e05 |
508 | } |
509 | |
ddd87d75 |
510 | sub person01 { |
511 | my $person = Person->new( |
512 | first_name => 'Bilbo', |
513 | last_name => 'Baggins', |
514 | ); |
515 | |
70eec86e |
516 | is( |
517 | $person->full_name, 'Bilbo Baggins', |
518 | 'full_name() is correctly implemented' |
519 | ); |
f7da468c |
520 | |
d047d1d4 |
521 | $person = eval { Person->new( [ qw( Lisa Smith ) ] ) }; |
eb959c49 |
522 | |
523 | if ( my $e = $@ ) { |
524 | diag( |
525 | "Calling Person->new() with an array reference threw an error:\n$e" |
526 | ); |
527 | BAIL_OUT( |
528 | 'You must implement Person->BUILDARGS correctly in order to continue these tests' |
d047d1d4 |
529 | ); |
eb959c49 |
530 | } |
531 | else { |
532 | ok( 1, 'Person->new() can accept an array reference as an argument' ); |
533 | } |
d047d1d4 |
534 | |
f7da468c |
535 | is( $person->first_name, 'Lisa', 'set first_name from two-arg arrayref' ); |
536 | is( $person->last_name, 'Smith', 'set last_name from two-arg arrayref' ); |
537 | |
70eec86e |
538 | eval { |
539 | Person->new( sub {'foo'} ); |
540 | }; |
541 | like( |
542 | $@, qr/\QSingle parameters to new() must be a HASH ref/, |
543 | 'Person constructor still rejects bad parameters' |
544 | ); |
ddd87d75 |
545 | } |
546 | |
547 | sub employee01 { |
548 | my $employee = Employee->new( |
549 | first_name => 'Amanda', |
550 | last_name => 'Palmer', |
8d1ce1d7 |
551 | title => 'Singer', |
ddd87d75 |
552 | ); |
553 | |
70eec86e |
554 | my $called = 0; |
54b470f5 |
555 | my $orig_super = \&Employee::super; |
556 | no warnings 'redefine'; |
557 | local *Employee::super = sub { $called++; goto &$orig_super }; |
558 | |
70eec86e |
559 | is( |
560 | $employee->full_name, 'Amanda Palmer (Singer)', |
561 | 'full_name() is properly overriden in Employee' |
562 | ); |
54b470f5 |
563 | ok( $called, 'Employee->full_name calls super()' ); |
ddd87d75 |
564 | } |
565 | |
5cab7e05 |
566 | sub person02 { |
567 | my $person = Person->new( |
568 | first_name => 'Bilbo', |
569 | last_name => 'Baggins', |
570 | balance => 0, |
571 | ); |
572 | |
70eec86e |
573 | is( |
574 | $person->as_string, 'Bilbo Baggins', |
575 | 'as_string() is correctly implemented' |
576 | ); |
5cab7e05 |
577 | |
578 | account_tests($person); |
579 | } |
580 | |
581 | sub employee02 { |
582 | my $employee = Employee->new( |
583 | first_name => 'Amanda', |
584 | last_name => 'Palmer', |
8d1ce1d7 |
585 | title => 'Singer', |
5cab7e05 |
586 | balance => 0, |
587 | ); |
588 | |
70eec86e |
589 | is( |
590 | $employee->as_string, 'Amanda Palmer (Singer)', |
591 | 'as_string() uses overridden full_name method in Employee' |
592 | ); |
5cab7e05 |
593 | |
594 | account_tests($employee); |
595 | } |
596 | |
8d1ce1d7 |
597 | sub person03 { |
598 | my $person = Person->new( |
599 | first_name => 'Bilbo', |
600 | last_name => 'Baggins', |
601 | ); |
602 | |
70eec86e |
603 | is( |
604 | $person->full_name, 'Bilbo Baggins', |
605 | 'full_name() is correctly implemented for a Person without a title' |
606 | ); |
607 | ok( |
608 | !$person->has_title, |
609 | 'Person has_title predicate is working correctly (returns false)' |
610 | ); |
8d1ce1d7 |
611 | |
612 | $person->title('Ringbearer'); |
70eec86e |
613 | ok( $person->has_title, |
614 | 'Person has_title predicate is working correctly (returns true)' ); |
3647da1b |
615 | |
70eec86e |
616 | my $called = 0; |
54b470f5 |
617 | my $orig_pred = \&Person::has_title; |
618 | no warnings 'redefine'; |
619 | local *Person::has_title = sub { $called++; goto &$orig_pred }; |
620 | |
70eec86e |
621 | is( |
622 | $person->full_name, 'Bilbo Baggins (Ringbearer)', |
623 | 'full_name() is correctly implemented for a Person with a title' |
624 | ); |
625 | ok( $called, |
626 | 'full_name in person uses the predicate for the title attribute' ); |
8d1ce1d7 |
627 | |
628 | $person->clear_title; |
629 | ok( !$person->has_title, 'Person clear_title method cleared the title' ); |
630 | |
631 | account_tests( $person, 100 ); |
632 | } |
633 | |
634 | sub employee03 { |
635 | my $employee = Employee->new( |
636 | first_name => 'Jimmy', |
637 | last_name => 'Foo', |
638 | salary_level => 3, |
639 | salary => 42, |
640 | ); |
641 | |
70eec86e |
642 | is( |
643 | $employee->salary, 30000, |
644 | 'salary is calculated from salary_level, and salary passed to constructor is ignored' |
645 | ); |
8d1ce1d7 |
646 | } |
647 | |
66b226e5 |
648 | sub person06 { |
649 | my $person = Person->new( |
650 | first_name => 'Bilbo', |
651 | last_name => 'Baggins', |
652 | ); |
653 | |
654 | isa_ok( $person->account, 'BankAccount' ); |
70eec86e |
655 | is( |
656 | $person->account->owner, $person, |
657 | 'owner of bank account is person that created account' |
658 | ); |
66b226e5 |
659 | |
660 | $person->deposit(10); |
70eec86e |
661 | is_deeply( |
662 | $person->account->history, [100], |
663 | 'deposit was recorded in account history' |
664 | ); |
66b226e5 |
665 | |
666 | $person->withdraw(15); |
70eec86e |
667 | is_deeply( |
668 | $person->account->history, [ 100, 110 ], |
669 | 'withdrawal was recorded in account history' |
670 | ); |
648519ab |
671 | |
672 | $person->withdraw(45); |
70eec86e |
673 | is_deeply( |
674 | $person->account->history, [ 100, 110, 95 ], |
675 | 'withdrawal was recorded in account history' |
676 | ); |
66b226e5 |
677 | } |
678 | |
5cab7e05 |
679 | sub account_tests { |
680 | local $Test::Builder::Level = $Test::Builder::Level + 1; |
681 | |
682 | my $person = shift; |
8d1ce1d7 |
683 | my $base_amount = shift || 0; |
5cab7e05 |
684 | |
685 | $person->deposit(50); |
8d1ce1d7 |
686 | eval { $person->withdraw( 75 + $base_amount ) }; |
70eec86e |
687 | like( |
688 | $@, qr/\QBalance cannot be negative/, |
689 | 'cannot withdraw more than is in our balance' |
690 | ); |
5cab7e05 |
691 | |
70eec86e |
692 | $person->withdraw(23); |
5cab7e05 |
693 | |
70eec86e |
694 | is( |
695 | $person->balance, 27 + $base_amount, |
696 | 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23' |
697 | ); |
5cab7e05 |
698 | } |
ddd87d75 |
699 | |
700 | 1; |