1 package MooseClass::Tests;
6 use Lingua::EN::Inflect qw( A PL_N );
7 use Test::More 'no_plan';
12 check_isa( 'Person', ['Moose::Object'] );
14 has_rw_attr( 'Person', $_ ) for qw( first_name last_name );
16 has_method( 'Person', 'full_name' );
22 check_isa( 'Employee', [ 'Person', 'Moose::Object' ] );
24 has_rw_attr( 'Employee', $_ ) for qw( title salary );
25 has_ro_attr( 'Employee', 'ssn' );
27 has_overridden_method( 'Employee', 'full_name' );
31 no_droppings('Person');
32 is_immutable('Person');
36 has_meta('Printable');
37 requires_method( 'Printable', 'as_string' );
40 does_role( 'Person', 'Printable' );
41 has_method( 'Person', 'as_string' );
43 has_meta('HasAccount');
44 has_method( 'HasAccount', $_ ) for qw( deposit withdraw );
45 has_role_attr( 'HasAccount', 'balance' );
47 does_role( 'Person', 'HasAccount' );
48 has_method( 'Person', $_ ) for qw( deposit withdraw );
49 has_rw_attr( 'Person', 'balance' );
52 does_role( 'Employee', $_ ) for qw( Printable HasAccount );
57 no_droppings($_) for qw( Printable HasAccount );
65 for my $name ( qw( first_name last_name ) ) {
66 has_rw_attr( 'Person', $name );
68 my $attr = Person->meta->get_attribute($name);
69 ok( $attr && $attr->is_required,
70 "$name is required in Person" );
73 has_rw_attr( 'Person', 'title' );
75 my $person_title_attr = Person->meta->get_attribute('title');
76 ok( !$person_title_attr->is_required, 'title is not required in Person' );
78 $person_title_attr->predicate, 'has_title',
79 'Person title attr has a has_title predicate'
82 $person_title_attr->clearer, 'clear_title',
83 'Person title attr has a clear_title clearer'
90 has_rw_attr( 'Employee', 'title', 'overridden' );
92 my $employee_title_attr = Employee->meta->get_attribute('title');
94 $employee_title_attr->default, 'Worker',
95 'title defaults to Worker in Employee'
99 !Employee->meta->has_method('full_name'),
100 'Employee no longer implements a full_name method'
103 has_ro_attr( 'Employee', 'salary' );
105 my $salary_attr = Employee->meta->get_attribute('salary');
106 ok( $salary_attr->is_lazy, 'salary is lazy' );
107 ok( !$salary_attr->init_arg, 'no init_arg for salary attribute' );
108 ok( $salary_attr->has_builder, 'salary attr has a builder' );
110 has_method( 'Employee', '_build_salary' );
112 has_rw_attr( 'Employee', 'salary_level' );
114 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
115 is( $salary_level_attr->default, 1, 'salary_level defaults to 1' );
119 my $balance_attr = Person->meta->get_attribute('balance');
120 is( $balance_attr->default, 100, 'balance defaults to 100' );
124 has_meta('Document');
125 has_ro_attr( 'Document', $_ ) for qw( title author );
128 has_ro_attr( 'Report', 'summary' );
130 has_meta('TPSReport');
131 has_ro_attr( 'TPSReport', $_ ) for qw( t p s );
133 has_method( 'Document', 'output' );
134 has_augmented_method( 'Report', 'output' );
135 has_augmented_method( 'TPSReport', 'output' );
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',
146 my $output = $tps->output;
147 $output =~ s/\n\n+/\n/g;
149 is( $output, <<'EOF', 'output returns expected report' );
151 I celebrate his whole collection!
155 Written by Peter Gibbons (for Bill Lumberg)
158 no_droppings('Document');
159 no_droppings('Report');
160 no_droppings('TPSReport');
166 for my $attr_name (qw( first_name last_name title )) {
167 my $attr = Person->meta->get_attribute($attr_name);
170 $attr->has_type_constraint,
171 "Person $attr_name has a type constraint"
174 $attr->type_constraint->name, 'Str',
175 "Person $attr_name type is Str"
179 has_meta('Employee');
182 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
184 $salary_level_attr->has_type_constraint,
185 'Employee salary_level has a type constraint'
188 my $tc = $salary_level_attr->type_constraint;
190 for my $invalid ( 0, 11, -14, 'foo', undef ) {
191 my $str = defined $invalid ? $invalid : 'undef';
193 !$tc->check($invalid),
194 "salary_level type rejects invalid value - $str"
198 for my $valid ( 1 .. 10 ) {
201 "salary_level type accepts valid value - $valid"
207 my $salary_attr = Employee->meta->get_attribute('salary');
210 $salary_attr->has_type_constraint,
211 'Employee salary has a type constraint'
214 my $tc = $salary_attr->type_constraint;
216 for my $invalid ( 0, -14, 'foo', undef ) {
217 my $str = defined $invalid ? $invalid : 'undef';
219 !$tc->check($invalid),
220 "salary type rejects invalid value - $str"
224 for my $valid ( 1, 100_000, 10**10 ) {
227 "salary type accepts valid value - $valid"
233 my $ssn_attr = Employee->meta->get_attribute('ssn');
236 $ssn_attr->has_type_constraint,
237 'Employee ssn has a type constraint'
240 my $tc = $ssn_attr->type_constraint;
242 for my $invalid ( 0, -14, 'foo', undef, '123-ab-1241', '123456789' ) {
243 my $str = defined $invalid ? $invalid : 'undef';
245 !$tc->check($invalid),
246 "ssn type rejects invalid value - $str"
250 for my $valid ( '041-12-1251', '123-45-6789', '926-41-5820' ) {
253 "ssn type accepts valid value - $valid"
258 no_droppings('Employee');
262 has_meta('BankAccount');
264 has_rw_attr( 'BankAccount', $_ ) for qw( balance owner );
266 my $ba_meta = BankAccount->meta;
269 $ba_meta->get_attribute('owner')->is_weak_ref,
270 'owner attribute is a weak ref'
273 has_method( 'BankAccount', $_ ) for qw( deposit withdraw );
275 has_ro_attr( 'BankAccount', 'history' );
277 my $history_attr = $ba_meta->get_attribute('history');
280 $history_attr->default->(),
282 'BankAccount history attribute defaults to []'
286 my $tc = $history_attr->type_constraint;
288 for my $invalid ( 0, 42, undef, {}, [ 'foo', 'bar' ] ) {
289 my $str = defined $invalid ? $invalid : 'undef';
291 !$tc->check($invalid),
292 "salary_level type rejects invalid value - $str"
296 for my $valid ( [], [1], [ 1, 2, 3 ], [ 1, -10, 9999 ] ) {
299 "salary_level type accepts valid value"
305 $history_attr->meta()
306 ->does_role('Moose::Meta::Attribute::Native::Trait::Array'),
307 'BankAccount history attribute uses native delegation to an array ref'
311 $ba_meta->get_attribute('balance')->has_trigger,
312 'BankAccount balance attribute has a trigger'
317 my $person_meta = Person->meta;
319 ok( !$person_meta->does_role('HasAccount'),
320 'Person class does not do the HasAccount role' );
323 !$person_meta->has_attribute('balance'),
324 'Person class does not have a balance attribute'
327 my $deposit_meth = $person_meta->get_method('deposit');
328 isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' );
330 my $withdraw_meth = $person_meta->get_method('withdraw');
331 isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' );
335 has_meta('Employee');
337 no_droppings('BankAccount');
343 local $Test::Builder::Level = $Test::Builder::Level + 1;
346 or BAIL_OUT("$package cannot be loaded");
348 ok( $package->can('meta'), "$package has a meta() method" )
350 "$package does not have a meta() method (did you forget to 'use Moose'?)"
358 local $Test::Builder::Level = $Test::Builder::Level + 1;
360 my @isa = $class->meta->linearized_isa;
361 shift @isa; # returns $class as the first entry
363 my $count = scalar @{$parents};
364 my $noun = PL_N( 'parent', $count );
366 is( scalar @isa, $count, "$class has $count $noun" );
368 for ( my $i = 0; $i < @{$parents}; $i++ ) {
369 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
376 my $overridden = shift;
378 local $Test::Builder::Level = $Test::Builder::Level + 1;
380 my $articled = $overridden ? "an overridden $name" : A($name);
382 $class->meta->has_attribute($name),
383 "$class has $articled attribute"
386 my $attr = $class->meta->get_attribute($name);
389 $attr->get_read_method, $name,
390 "$name attribute has a reader accessor - $name()"
393 $attr->get_write_method, $name,
394 "$name attribute has a writer accessor - $name()"
402 local $Test::Builder::Level = $Test::Builder::Level + 1;
404 my $articled = A($name);
406 $class->meta->has_attribute($name),
407 "$class has $articled attribute"
410 my $attr = $class->meta->get_attribute($name);
413 $attr->get_read_method, $name,
414 "$name attribute has a reader accessor - $name()"
417 $attr->get_write_method, undef,
418 "$name attribute does not have a writer"
426 local $Test::Builder::Level = $Test::Builder::Level + 1;
428 my $articled = A($name);
430 $role->meta->get_attribute($name),
431 "$role has $articled attribute"
439 local $Test::Builder::Level = $Test::Builder::Level + 1;
441 my $articled = A($name);
442 ok( $package->meta->has_method($name), "$package has $articled method" );
445 sub has_overridden_method {
449 local $Test::Builder::Level = $Test::Builder::Level + 1;
451 my $articled = A($name);
452 ok( $package->meta->has_method($name), "$package has $articled method" );
454 my $meth = $package->meta->get_method($name);
455 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
458 sub has_augmented_method {
462 local $Test::Builder::Level = $Test::Builder::Level + 1;
464 my $articled = A($name);
465 ok( $class->meta->has_method($name), "$class has $articled method" );
467 my $meth = $class->meta->get_method($name);
468 isa_ok( $meth, 'Moose::Meta::Method::Augmented' );
471 sub requires_method {
475 local $Test::Builder::Level = $Test::Builder::Level + 1;
478 $package->meta->requires_method($method),
479 "$package requires the method $method"
486 local $Test::Builder::Level = $Test::Builder::Level + 1;
488 ok( !$package->can('has'), "no Moose droppings in $package" );
489 ok( !$package->can('subtype'),
490 "no Moose::Util::TypeConstraints droppings in $package" );
496 local $Test::Builder::Level = $Test::Builder::Level + 1;
498 ok( $class->meta->is_immutable, "$class has been made immutable" );
505 local $Test::Builder::Level = $Test::Builder::Level + 1;
507 ok( $package->meta->does_role($role), "$package does the $role role" );
511 my $person = Person->new(
512 first_name => 'Bilbo',
513 last_name => 'Baggins',
517 $person->full_name, 'Bilbo Baggins',
518 'full_name() is correctly implemented'
521 $person = eval { Person->new( [ qw( Lisa Smith ) ] ) };
525 "Calling Person->new() with an array reference threw an error:\n$e"
528 'You must implement Person->BUILDARGS correctly in order to continue these tests'
532 ok( 1, 'Person->new() can accept an array reference as an argument' );
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' );
539 Person->new( sub {'foo'} );
542 $@, qr/\QSingle parameters to new() must be a HASH ref/,
543 'Person constructor still rejects bad parameters'
548 my $employee = Employee->new(
549 first_name => 'Amanda',
550 last_name => 'Palmer',
555 my $orig_super = \&Employee::super;
556 no warnings 'redefine';
557 local *Employee::super = sub { $called++; goto &$orig_super };
560 $employee->full_name, 'Amanda Palmer (Singer)',
561 'full_name() is properly overriden in Employee'
563 ok( $called, 'Employee->full_name calls super()' );
567 my $person = Person->new(
568 first_name => 'Bilbo',
569 last_name => 'Baggins',
574 $person->as_string, 'Bilbo Baggins',
575 'as_string() is correctly implemented'
578 account_tests($person);
582 my $employee = Employee->new(
583 first_name => 'Amanda',
584 last_name => 'Palmer',
590 $employee->as_string, 'Amanda Palmer (Singer)',
591 'as_string() uses overridden full_name method in Employee'
594 account_tests($employee);
598 my $person = Person->new(
599 first_name => 'Bilbo',
600 last_name => 'Baggins',
604 $person->full_name, 'Bilbo Baggins',
605 'full_name() is correctly implemented for a Person without a title'
609 'Person has_title predicate is working correctly (returns false)'
612 $person->title('Ringbearer');
613 ok( $person->has_title,
614 'Person has_title predicate is working correctly (returns true)' );
617 my $orig_pred = \&Person::has_title;
618 no warnings 'redefine';
619 local *Person::has_title = sub { $called++; goto &$orig_pred };
622 $person->full_name, 'Bilbo Baggins (Ringbearer)',
623 'full_name() is correctly implemented for a Person with a title'
626 'full_name in person uses the predicate for the title attribute' );
628 $person->clear_title;
629 ok( !$person->has_title, 'Person clear_title method cleared the title' );
631 account_tests( $person, 100 );
635 my $employee = Employee->new(
636 first_name => 'Jimmy',
643 $employee->salary, 30000,
644 'salary is calculated from salary_level, and salary passed to constructor is ignored'
649 my $person = Person->new(
650 first_name => 'Bilbo',
651 last_name => 'Baggins',
654 isa_ok( $person->account, 'BankAccount' );
656 $person->account->owner, $person,
657 'owner of bank account is person that created account'
660 $person->deposit(10);
662 $person->account->history, [100],
663 'deposit was recorded in account history'
666 $person->withdraw(15);
668 $person->account->history, [ 100, 110 ],
669 'withdrawal was recorded in account history'
672 $person->withdraw(45);
674 $person->account->history, [ 100, 110, 95 ],
675 'withdrawal was recorded in account history'
680 local $Test::Builder::Level = $Test::Builder::Level + 1;
683 my $base_amount = shift || 0;
685 $person->deposit(50);
686 eval { $person->withdraw( 75 + $base_amount ) };
688 $@, qr/\QBalance cannot be negative/,
689 'cannot withdraw more than is in our balance'
692 $person->withdraw(23);
695 $person->balance, 27 + $base_amount,
696 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23'