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' );
126 ok( Person->can('full_name'), 'Person has a full_name() method' )
128 'Person does not have a full_name() method. Cannot continue testing.'
131 my $meth = Person->meta()->get_method('full_name');
133 $meth && $meth->isa('Class::MOP::Method::Wrapped'),
134 'method modifiers have been applied to the Person->full_name method'
138 scalar $meth->before_modifiers,
140 'Person->full_name has a single before modifier'
144 scalar $meth->after_modifiers,
146 'Person->full_name has a single after modifier'
149 my $person = Person->new(
150 first_name => 'Bilbo',
151 last_name => 'Baggins',
157 'Person::CALL global is empty before calling full_name'
160 $person->full_name();
164 [ 'calling full_name', 'called full_name' ],
165 'Person::CALL global contains before and after strings'
169 scalar $meth->around_modifiers,
171 'Person->full_name has a single around modifier'
174 my $larry = Person->new(
175 first_name => 'Larry',
182 'full_name is wrapped by asterisks when last name is Wall'
189 for my $attr_name (qw( first_name last_name title )) {
190 my $attr = Person->meta->get_attribute($attr_name);
193 $attr->has_type_constraint,
194 "Person $attr_name has a type constraint"
197 $attr->type_constraint->name, 'Str',
198 "Person $attr_name type is Str"
202 has_meta('Employee');
205 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
207 $salary_level_attr->has_type_constraint,
208 'Employee salary_level has a type constraint'
211 my $tc = $salary_level_attr->type_constraint;
213 for my $invalid ( 0, 11, -14, 'foo', undef ) {
214 my $str = defined $invalid ? $invalid : 'undef';
216 !$tc->check($invalid),
217 "salary_level type rejects invalid value - $str"
221 for my $valid ( 1 .. 10 ) {
224 "salary_level type accepts valid value - $valid"
230 my $salary_attr = Employee->meta->get_attribute('salary');
233 $salary_attr->has_type_constraint,
234 'Employee salary has a type constraint'
237 my $tc = $salary_attr->type_constraint;
239 for my $invalid ( 0, -14, 'foo', undef ) {
240 my $str = defined $invalid ? $invalid : 'undef';
242 !$tc->check($invalid),
243 "salary type rejects invalid value - $str"
247 for my $valid ( 1, 100_000, 10**10 ) {
250 "salary type accepts valid value - $valid"
256 my $ssn_attr = Employee->meta->get_attribute('ssn');
259 $ssn_attr->has_type_constraint,
260 'Employee ssn has a type constraint'
263 my $tc = $ssn_attr->type_constraint;
265 for my $invalid ( 0, -14, 'foo', undef, '123-ab-1241', '123456789' ) {
266 my $str = defined $invalid ? $invalid : 'undef';
268 !$tc->check($invalid),
269 "ssn type rejects invalid value - $str"
273 for my $valid ( '041-12-1251', '123-45-6789', '926-41-5820' ) {
276 "ssn type accepts valid value - $valid"
281 no_droppings('Employee');
285 has_meta('BankAccount');
287 has_rw_attr( 'BankAccount', $_ ) for qw( balance owner );
289 my $ba_meta = BankAccount->meta;
292 $ba_meta->get_attribute('owner')->is_weak_ref,
293 'owner attribute is a weak ref'
296 has_method( 'BankAccount', $_ ) for qw( deposit withdraw );
298 has_ro_attr( 'BankAccount', 'history' );
300 my $history_attr = $ba_meta->get_attribute('history');
303 $history_attr->default->(),
305 'BankAccount history attribute defaults to []'
309 my $tc = $history_attr->type_constraint;
311 for my $invalid ( 0, 42, undef, {}, [ 'foo', 'bar' ] ) {
312 my $str = defined $invalid ? $invalid : 'undef';
314 !$tc->check($invalid),
315 "salary_level type rejects invalid value - $str"
319 for my $valid ( [], [1], [ 1, 2, 3 ], [ 1, -10, 9999 ] ) {
322 "salary_level type accepts valid value"
328 $history_attr->meta()
329 ->does_role('Moose::Meta::Attribute::Native::Trait::Array'),
330 'BankAccount history attribute uses native delegation to an array ref'
334 $ba_meta->get_attribute('balance')->has_trigger,
335 'BankAccount balance attribute has a trigger'
340 my $person_meta = Person->meta;
342 ok( !$person_meta->does_role('HasAccount'),
343 'Person class does not do the HasAccount role' );
346 !$person_meta->has_attribute('balance'),
347 'Person class does not have a balance attribute'
350 my $deposit_meth = $person_meta->get_method('deposit');
351 isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' );
353 my $withdraw_meth = $person_meta->get_method('withdraw');
354 isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' );
358 has_meta('Employee');
360 no_droppings('BankAccount');
366 local $Test::Builder::Level = $Test::Builder::Level + 1;
369 or BAIL_OUT("$package cannot be loaded");
371 ok( $package->can('meta'), "$package has a meta() method" )
373 "$package does not have a meta() method (did you forget to 'use Moose'?)"
381 local $Test::Builder::Level = $Test::Builder::Level + 1;
383 my @isa = $class->meta->linearized_isa;
384 shift @isa; # returns $class as the first entry
386 my $count = scalar @{$parents};
387 my $noun = PL_N( 'parent', $count );
389 is( scalar @isa, $count, "$class has $count $noun" );
391 for ( my $i = 0; $i < @{$parents}; $i++ ) {
392 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
399 my $overridden = shift;
401 local $Test::Builder::Level = $Test::Builder::Level + 1;
403 my $articled = $overridden ? "an overridden $name" : A($name);
405 $class->meta->has_attribute($name),
406 "$class has $articled attribute"
409 my $attr = $class->meta->get_attribute($name);
412 $attr->get_read_method, $name,
413 "$name attribute has a reader accessor - $name()"
416 $attr->get_write_method, $name,
417 "$name attribute has a writer accessor - $name()"
425 local $Test::Builder::Level = $Test::Builder::Level + 1;
427 my $articled = A($name);
429 $class->meta->has_attribute($name),
430 "$class has $articled attribute"
433 my $attr = $class->meta->get_attribute($name);
436 $attr->get_read_method, $name,
437 "$name attribute has a reader accessor - $name()"
440 $attr->get_write_method, undef,
441 "$name attribute does not have a writer"
449 local $Test::Builder::Level = $Test::Builder::Level + 1;
451 my $articled = A($name);
453 $role->meta->get_attribute($name),
454 "$role has $articled attribute"
462 local $Test::Builder::Level = $Test::Builder::Level + 1;
464 my $articled = A($name);
465 ok( $package->meta->has_method($name), "$package has $articled method" );
468 sub has_overridden_method {
472 local $Test::Builder::Level = $Test::Builder::Level + 1;
474 my $articled = A($name);
475 ok( $package->meta->has_method($name), "$package has $articled method" );
477 my $meth = $package->meta->get_method($name);
478 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
481 sub has_augmented_method {
485 local $Test::Builder::Level = $Test::Builder::Level + 1;
487 my $articled = A($name);
488 ok( $class->meta->has_method($name), "$class has $articled method" );
490 my $meth = $class->meta->get_method($name);
491 isa_ok( $meth, 'Moose::Meta::Method::Augmented' );
494 sub requires_method {
498 local $Test::Builder::Level = $Test::Builder::Level + 1;
501 $package->meta->requires_method($method),
502 "$package requires the method $method"
509 local $Test::Builder::Level = $Test::Builder::Level + 1;
511 ok( !$package->can('has'), "no Moose droppings in $package" );
512 ok( !$package->can('subtype'),
513 "no Moose::Util::TypeConstraints droppings in $package" );
519 local $Test::Builder::Level = $Test::Builder::Level + 1;
521 ok( $class->meta->is_immutable, "$class has been made immutable" );
528 local $Test::Builder::Level = $Test::Builder::Level + 1;
530 ok( $package->meta->does_role($role), "$package does the $role role" );
534 my $person = Person->new(
535 first_name => 'Bilbo',
536 last_name => 'Baggins',
540 $person->full_name, 'Bilbo Baggins',
541 'full_name() is correctly implemented'
544 $person = eval { Person->new( [ qw( Lisa Smith ) ] ) };
548 "Calling Person->new() with an array reference threw an error:\n$e"
551 'You must implement Person->BUILDARGS correctly in order to continue these tests'
555 ok( 1, 'Person->new() can accept an array reference as an argument' );
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' );
562 Person->new( sub {'foo'} );
565 $@, qr/\QSingle parameters to new() must be a HASH ref/,
566 'Person constructor still rejects bad parameters'
571 my $employee = Employee->new(
572 first_name => 'Amanda',
573 last_name => 'Palmer',
578 my $orig_super = \&Employee::super;
579 no warnings 'redefine';
580 local *Employee::super = sub { $called++; goto &$orig_super };
583 $employee->full_name, 'Amanda Palmer (Singer)',
584 'full_name() is properly overriden in Employee'
586 ok( $called, 'Employee->full_name calls super()' );
590 my $person = Person->new(
591 first_name => 'Bilbo',
592 last_name => 'Baggins',
597 $person->as_string, 'Bilbo Baggins',
598 'as_string() is correctly implemented'
601 account_tests($person);
605 my $employee = Employee->new(
606 first_name => 'Amanda',
607 last_name => 'Palmer',
613 $employee->as_string, 'Amanda Palmer (Singer)',
614 'as_string() uses overridden full_name method in Employee'
617 account_tests($employee);
621 my $person = Person->new(
622 first_name => 'Bilbo',
623 last_name => 'Baggins',
627 $person->full_name, 'Bilbo Baggins',
628 'full_name() is correctly implemented for a Person without a title'
632 'Person has_title predicate is working correctly (returns false)'
635 $person->title('Ringbearer');
636 ok( $person->has_title,
637 'Person has_title predicate is working correctly (returns true)' );
640 my $orig_pred = \&Person::has_title;
641 no warnings 'redefine';
642 local *Person::has_title = sub { $called++; goto &$orig_pred };
645 $person->full_name, 'Bilbo Baggins (Ringbearer)',
646 'full_name() is correctly implemented for a Person with a title'
649 'full_name in person uses the predicate for the title attribute' );
651 $person->clear_title;
652 ok( !$person->has_title, 'Person clear_title method cleared the title' );
654 account_tests( $person, 100 );
658 my $employee = Employee->new(
659 first_name => 'Jimmy',
666 $employee->salary, 30000,
667 'salary is calculated from salary_level, and salary passed to constructor is ignored'
672 my $person = Person->new(
673 first_name => 'Bilbo',
674 last_name => 'Baggins',
677 isa_ok( $person->account, 'BankAccount' );
679 $person->account->owner, $person,
680 'owner of bank account is person that created account'
683 $person->deposit(10);
685 $person->account->history, [100],
686 'deposit was recorded in account history'
689 $person->withdraw(15);
691 $person->account->history, [ 100, 110 ],
692 'withdrawal was recorded in account history'
695 $person->withdraw(45);
697 $person->account->history, [ 100, 110, 95 ],
698 'withdrawal was recorded in account history'
703 local $Test::Builder::Level = $Test::Builder::Level + 1;
706 my $base_amount = shift || 0;
708 $person->deposit(50);
711 $person->balance, 50 + $base_amount,
712 "balance is 50 + $base_amount",
715 eval { $person->withdraw( 75 + $base_amount ) };
717 $@, qr/\QBalance cannot be negative/,
718 'cannot withdraw more than is in our balance'
721 $person->withdraw(23);
724 $person->balance, 27 + $base_amount,
725 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23'