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