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 );
66 has_rw_attr( 'Person', 'title' );
68 has_rw_attr( 'Employee', 'title', 'overridden' );
69 has_rw_attr( 'Employee', 'salary_level' );
70 has_ro_attr( 'Employee', 'salary' );
72 has_method( 'Employee', '_build_salary' );
75 !Employee->meta->has_method('full_name'),
76 'Employee no longer implements a full_name method'
79 my $person_title_attr = Person->meta->get_attribute('title');
80 ok( !$person_title_attr->is_required, 'title is not required in Person' );
82 $person_title_attr->predicate, 'has_title',
83 'Person title attr has a has_title predicate'
86 $person_title_attr->clearer, 'clear_title',
87 'Person title attr has a clear_title clearer'
90 my $balance_attr = Person->meta->get_attribute('balance');
91 is( $balance_attr->default, 100, 'balance defaults to 100' );
93 my $employee_title_attr = Employee->meta->get_attribute('title');
95 $employee_title_attr->default, 'Worker',
96 'title defaults to Worker in Employee'
99 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
100 is( $salary_level_attr->default, 1, 'salary_level defaults to 1' );
102 my $salary_attr = Employee->meta->get_attribute('salary');
103 ok( !$salary_attr->init_arg, 'no init_arg for salary attribute' );
104 ok( $salary_attr->has_builder, 'salary attr has a builder' );
111 has_meta('Document');
113 has_meta('TPSReport');
115 no_droppings('Document');
116 no_droppings('Report');
117 no_droppings('TPSReport');
119 has_ro_attr( 'Document', $_ ) for qw( title author );
120 has_ro_attr( 'Report', 'summary' );
121 has_ro_attr( 'TPSReport', $_ ) for qw( t p s );
123 has_method( 'Document', 'output' );
124 has_augmented_method( 'Report', 'output' );
125 has_augmented_method( 'TPSReport', 'output' );
127 my $tps = TPSReport->new(
128 title => 'That TPS Report',
129 author => 'Peter Gibbons (for Bill Lumberg)',
130 summary => 'I celebrate his whole collection!',
131 t => 'PC Load Letter',
136 my $output = $tps->output;
137 $output =~ s/\n\n+/\n/g;
139 is( $output, <<'EOF', 'output returns expected report' );
141 I celebrate his whole collection!
145 Written by Peter Gibbons (for Bill Lumberg)
151 has_meta('Employee');
152 no_droppings('Employee');
154 for my $attr_name (qw( first_name last_name title )) {
155 my $attr = Person->meta->get_attribute($attr_name);
158 $attr->has_type_constraint,
159 "Person $attr_name has a type constraint"
162 $attr->type_constraint->name, 'Str',
163 "Person $attr_name type is Str"
168 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
170 $salary_level_attr->has_type_constraint,
171 'Employee salary_level has a type constraint'
174 my $tc = $salary_level_attr->type_constraint;
176 for my $invalid ( 0, 11, -14, 'foo', undef ) {
177 my $str = defined $invalid ? $invalid : 'undef';
179 !$tc->check($invalid),
180 "salary_level type rejects invalid value - $str"
184 for my $valid ( 1 .. 10 ) {
187 "salary_level type accepts valid value - $valid"
193 my $salary_attr = Employee->meta->get_attribute('salary');
196 $salary_attr->has_type_constraint,
197 'Employee salary has a type constraint'
200 my $tc = $salary_attr->type_constraint;
202 for my $invalid ( 0, -14, 'foo', undef ) {
203 my $str = defined $invalid ? $invalid : 'undef';
205 !$tc->check($invalid),
206 "salary type rejects invalid value - $str"
210 for my $valid ( 1, 100_000, 10**10 ) {
213 "salary type accepts valid value - $valid"
219 my $ssn_attr = Employee->meta->get_attribute('ssn');
222 $ssn_attr->has_type_constraint,
223 'Employee ssn has a type constraint'
226 my $tc = $ssn_attr->type_constraint;
228 for my $invalid ( 0, -14, 'foo', undef, '123-ab-1241', '123456789' ) {
229 my $str = defined $invalid ? $invalid : 'undef';
231 !$tc->check($invalid),
232 "ssn type rejects invalid value - $str"
236 for my $valid ( '041-12-1251', '123-45-6789', '926-41-5820' ) {
239 "ssn type accepts valid value - $valid"
247 has_meta('Employee');
248 has_meta('BankAccount');
249 no_droppings('BankAccount');
251 has_rw_attr( 'BankAccount', 'balance' );
252 has_rw_attr( 'BankAccount', 'owner' );
253 has_ro_attr( 'BankAccount', 'history' );
255 my $ba_meta = BankAccount->meta;
257 $ba_meta->has_attribute('balance'),
258 'BankAccount class has a balance attribute'
261 my $history_attr = $ba_meta->get_attribute('history');
264 $history_attr->meta()
265 ->does_role('Moose::Meta::Attribute::Native::Trait::Array'),
266 'BankAccount history attribute uses native delegation to an array ref'
270 $ba_meta->get_attribute('balance')->has_trigger,
271 'BankAccount balance attribute has a trigger'
274 my $person_meta = Person->meta;
276 !$person_meta->has_attribute('balance'),
277 'Person class does not have a balance attribute'
280 my $deposit_meth = $person_meta->get_method('deposit');
281 isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' );
283 my $withdraw_meth = $person_meta->get_method('withdraw');
284 isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' );
287 $ba_meta->get_attribute('owner')->is_weak_ref,
288 'owner attribute is a weak ref'
297 local $Test::Builder::Level = $Test::Builder::Level + 1;
300 or BAIL_OUT("$package cannot be loaded");
302 ok( $package->can('meta'), "$package has a meta() method" )
304 "$package does not have a meta() method (did you forget to 'use Moose'?)"
312 local $Test::Builder::Level = $Test::Builder::Level + 1;
314 my @isa = $class->meta->linearized_isa;
315 shift @isa; # returns $class as the first entry
317 my $count = scalar @{$parents};
318 my $noun = PL_N( 'parent', $count );
320 is( scalar @isa, $count, "$class has $count $noun" );
322 for ( my $i = 0; $i < @{$parents}; $i++ ) {
323 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
330 my $overridden = shift;
332 local $Test::Builder::Level = $Test::Builder::Level + 1;
334 my $articled = $overridden ? "an overridden $name" : A($name);
336 $class->meta->has_attribute($name),
337 "$class has $articled attribute"
340 my $attr = $class->meta->get_attribute($name);
343 $attr->get_read_method, $name,
344 "$name attribute has a reader accessor - $name()"
347 $attr->get_write_method, $name,
348 "$name attribute has a writer accessor - $name()"
356 local $Test::Builder::Level = $Test::Builder::Level + 1;
358 my $articled = A($name);
360 $class->meta->has_attribute($name),
361 "$class has $articled attribute"
364 my $attr = $class->meta->get_attribute($name);
367 $attr->get_read_method, $name,
368 "$name attribute has a reader accessor - $name()"
371 $attr->get_write_method, undef,
372 "$name attribute does not have a writer"
380 local $Test::Builder::Level = $Test::Builder::Level + 1;
382 my $articled = A($name);
384 $role->meta->get_attribute($name),
385 "$role has $articled attribute"
393 local $Test::Builder::Level = $Test::Builder::Level + 1;
395 my $articled = A($name);
396 ok( $package->meta->has_method($name), "$package has $articled method" );
399 sub has_overridden_method {
403 local $Test::Builder::Level = $Test::Builder::Level + 1;
405 my $articled = A($name);
406 ok( $package->meta->has_method($name), "$package has $articled method" );
408 my $meth = $package->meta->get_method($name);
409 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
412 sub has_augmented_method {
416 local $Test::Builder::Level = $Test::Builder::Level + 1;
418 my $articled = A($name);
419 ok( $class->meta->has_method($name), "$class has $articled method" );
421 my $meth = $class->meta->get_method($name);
422 isa_ok( $meth, 'Moose::Meta::Method::Augmented' );
425 sub requires_method {
429 local $Test::Builder::Level = $Test::Builder::Level + 1;
432 $package->meta->requires_method($method),
433 "$package requires the method $method"
440 local $Test::Builder::Level = $Test::Builder::Level + 1;
442 ok( !$package->can('has'), "no Moose droppings in $package" );
443 ok( !$package->can('subtype'),
444 "no Moose::Util::TypeConstraints droppings in $package" );
450 local $Test::Builder::Level = $Test::Builder::Level + 1;
452 ok( $class->meta->is_immutable, "$class has been made immutable" );
459 local $Test::Builder::Level = $Test::Builder::Level + 1;
461 ok( $package->meta->does_role($role), "$package does the $role role" );
465 my $person = Person->new(
466 first_name => 'Bilbo',
467 last_name => 'Baggins',
471 $person->full_name, 'Bilbo Baggins',
472 'full_name() is correctly implemented'
475 $person = eval { Person->new( [ qw( Lisa Smith ) ] ) };
476 ok( !$@, 'Person->new() can accept an array reference as an argument' )
478 'You must implement Person->BUILDARGS in order to continue these tests'
481 is( $person->first_name, 'Lisa', 'set first_name from two-arg arrayref' );
482 is( $person->last_name, 'Smith', 'set last_name from two-arg arrayref' );
485 Person->new( sub {'foo'} );
488 $@, qr/\QSingle parameters to new() must be a HASH ref/,
489 'Person constructor still rejects bad parameters'
494 my $employee = Employee->new(
495 first_name => 'Amanda',
496 last_name => 'Palmer',
501 my $orig_super = \&Employee::super;
502 no warnings 'redefine';
503 local *Employee::super = sub { $called++; goto &$orig_super };
506 $employee->full_name, 'Amanda Palmer (Singer)',
507 'full_name() is properly overriden in Employee'
509 ok( $called, 'Employee->full_name calls super()' );
513 my $person = Person->new(
514 first_name => 'Bilbo',
515 last_name => 'Baggins',
520 $person->as_string, 'Bilbo Baggins',
521 'as_string() is correctly implemented'
524 account_tests($person);
528 my $employee = Employee->new(
529 first_name => 'Amanda',
530 last_name => 'Palmer',
536 $employee->as_string, 'Amanda Palmer (Singer)',
537 'as_string() uses overridden full_name method in Employee'
540 account_tests($employee);
544 my $person = Person->new(
545 first_name => 'Bilbo',
546 last_name => 'Baggins',
550 $person->full_name, 'Bilbo Baggins',
551 'full_name() is correctly implemented for a Person without a title'
555 'Person has_title predicate is working correctly (returns false)'
558 $person->title('Ringbearer');
559 ok( $person->has_title,
560 'Person has_title predicate is working correctly (returns true)' );
563 my $orig_pred = \&Person::has_title;
564 no warnings 'redefine';
565 local *Person::has_title = sub { $called++; goto &$orig_pred };
568 $person->full_name, 'Bilbo Baggins (Ringbearer)',
569 'full_name() is correctly implemented for a Person with a title'
572 'full_name in person uses the predicate for the title attribute' );
574 $person->clear_title;
575 ok( !$person->has_title, 'Person clear_title method cleared the title' );
577 account_tests( $person, 100 );
581 my $employee = Employee->new(
582 first_name => 'Jimmy',
589 $employee->salary, 30000,
590 'salary is calculated from salary_level, and salary passed to constructor is ignored'
595 my $person = Person->new(
596 first_name => 'Bilbo',
597 last_name => 'Baggins',
600 isa_ok( $person->account, 'BankAccount' );
602 $person->account->owner, $person,
603 'owner of bank account is person that created account'
606 $person->deposit(10);
608 $person->account->history, [100],
609 'deposit was recorded in account history'
612 $person->withdraw(15);
614 $person->account->history, [ 100, 110 ],
615 'withdrawal was recorded in account history'
618 $person->withdraw(45);
620 $person->account->history, [ 100, 110, 95 ],
621 'withdrawal was recorded in account history'
626 local $Test::Builder::Level = $Test::Builder::Level + 1;
629 my $base_amount = shift || 0;
631 $person->deposit(50);
632 eval { $person->withdraw( 75 + $base_amount ) };
634 $@, qr/\QBalance cannot be negative/,
635 'cannot withdraw more than is in our balance'
638 $person->withdraw(23);
641 $person->balance, 27 + $base_amount,
642 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23'