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