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');
263 has_meta('Employee');
264 has_meta('BankAccount');
265 no_droppings('BankAccount');
267 has_rw_attr( 'BankAccount', 'balance' );
268 has_rw_attr( 'BankAccount', 'owner' );
269 has_ro_attr( 'BankAccount', 'history' );
271 my $ba_meta = BankAccount->meta;
273 $ba_meta->has_attribute('balance'),
274 'BankAccount class has a balance attribute'
277 my $history_attr = $ba_meta->get_attribute('history');
280 $history_attr->meta()
281 ->does_role('Moose::Meta::Attribute::Native::Trait::Array'),
282 'BankAccount history attribute uses native delegation to an array ref'
286 $ba_meta->get_attribute('balance')->has_trigger,
287 'BankAccount balance attribute has a trigger'
290 my $person_meta = Person->meta;
292 !$person_meta->has_attribute('balance'),
293 'Person class does not have a balance attribute'
296 my $deposit_meth = $person_meta->get_method('deposit');
297 isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' );
299 my $withdraw_meth = $person_meta->get_method('withdraw');
300 isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' );
303 $ba_meta->get_attribute('owner')->is_weak_ref,
304 'owner attribute is a weak ref'
313 local $Test::Builder::Level = $Test::Builder::Level + 1;
316 or BAIL_OUT("$package cannot be loaded");
318 ok( $package->can('meta'), "$package has a meta() method" )
320 "$package does not have a meta() method (did you forget to 'use Moose'?)"
328 local $Test::Builder::Level = $Test::Builder::Level + 1;
330 my @isa = $class->meta->linearized_isa;
331 shift @isa; # returns $class as the first entry
333 my $count = scalar @{$parents};
334 my $noun = PL_N( 'parent', $count );
336 is( scalar @isa, $count, "$class has $count $noun" );
338 for ( my $i = 0; $i < @{$parents}; $i++ ) {
339 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
346 my $overridden = shift;
348 local $Test::Builder::Level = $Test::Builder::Level + 1;
350 my $articled = $overridden ? "an overridden $name" : A($name);
352 $class->meta->has_attribute($name),
353 "$class has $articled attribute"
356 my $attr = $class->meta->get_attribute($name);
359 $attr->get_read_method, $name,
360 "$name attribute has a reader accessor - $name()"
363 $attr->get_write_method, $name,
364 "$name attribute has a writer accessor - $name()"
372 local $Test::Builder::Level = $Test::Builder::Level + 1;
374 my $articled = A($name);
376 $class->meta->has_attribute($name),
377 "$class has $articled attribute"
380 my $attr = $class->meta->get_attribute($name);
383 $attr->get_read_method, $name,
384 "$name attribute has a reader accessor - $name()"
387 $attr->get_write_method, undef,
388 "$name attribute does not have a writer"
396 local $Test::Builder::Level = $Test::Builder::Level + 1;
398 my $articled = A($name);
400 $role->meta->get_attribute($name),
401 "$role has $articled attribute"
409 local $Test::Builder::Level = $Test::Builder::Level + 1;
411 my $articled = A($name);
412 ok( $package->meta->has_method($name), "$package has $articled method" );
415 sub has_overridden_method {
419 local $Test::Builder::Level = $Test::Builder::Level + 1;
421 my $articled = A($name);
422 ok( $package->meta->has_method($name), "$package has $articled method" );
424 my $meth = $package->meta->get_method($name);
425 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
428 sub has_augmented_method {
432 local $Test::Builder::Level = $Test::Builder::Level + 1;
434 my $articled = A($name);
435 ok( $class->meta->has_method($name), "$class has $articled method" );
437 my $meth = $class->meta->get_method($name);
438 isa_ok( $meth, 'Moose::Meta::Method::Augmented' );
441 sub requires_method {
445 local $Test::Builder::Level = $Test::Builder::Level + 1;
448 $package->meta->requires_method($method),
449 "$package requires the method $method"
456 local $Test::Builder::Level = $Test::Builder::Level + 1;
458 ok( !$package->can('has'), "no Moose droppings in $package" );
459 ok( !$package->can('subtype'),
460 "no Moose::Util::TypeConstraints droppings in $package" );
466 local $Test::Builder::Level = $Test::Builder::Level + 1;
468 ok( $class->meta->is_immutable, "$class has been made immutable" );
475 local $Test::Builder::Level = $Test::Builder::Level + 1;
477 ok( $package->meta->does_role($role), "$package does the $role role" );
481 my $person = Person->new(
482 first_name => 'Bilbo',
483 last_name => 'Baggins',
487 $person->full_name, 'Bilbo Baggins',
488 'full_name() is correctly implemented'
491 $person = eval { Person->new( [ qw( Lisa Smith ) ] ) };
492 ok( !$@, 'Person->new() can accept an array reference as an argument' )
494 'You must implement Person->BUILDARGS in order to continue these tests'
497 is( $person->first_name, 'Lisa', 'set first_name from two-arg arrayref' );
498 is( $person->last_name, 'Smith', 'set last_name from two-arg arrayref' );
501 Person->new( sub {'foo'} );
504 $@, qr/\QSingle parameters to new() must be a HASH ref/,
505 'Person constructor still rejects bad parameters'
510 my $employee = Employee->new(
511 first_name => 'Amanda',
512 last_name => 'Palmer',
517 my $orig_super = \&Employee::super;
518 no warnings 'redefine';
519 local *Employee::super = sub { $called++; goto &$orig_super };
522 $employee->full_name, 'Amanda Palmer (Singer)',
523 'full_name() is properly overriden in Employee'
525 ok( $called, 'Employee->full_name calls super()' );
529 my $person = Person->new(
530 first_name => 'Bilbo',
531 last_name => 'Baggins',
536 $person->as_string, 'Bilbo Baggins',
537 'as_string() is correctly implemented'
540 account_tests($person);
544 my $employee = Employee->new(
545 first_name => 'Amanda',
546 last_name => 'Palmer',
552 $employee->as_string, 'Amanda Palmer (Singer)',
553 'as_string() uses overridden full_name method in Employee'
556 account_tests($employee);
560 my $person = Person->new(
561 first_name => 'Bilbo',
562 last_name => 'Baggins',
566 $person->full_name, 'Bilbo Baggins',
567 'full_name() is correctly implemented for a Person without a title'
571 'Person has_title predicate is working correctly (returns false)'
574 $person->title('Ringbearer');
575 ok( $person->has_title,
576 'Person has_title predicate is working correctly (returns true)' );
579 my $orig_pred = \&Person::has_title;
580 no warnings 'redefine';
581 local *Person::has_title = sub { $called++; goto &$orig_pred };
584 $person->full_name, 'Bilbo Baggins (Ringbearer)',
585 'full_name() is correctly implemented for a Person with a title'
588 'full_name in person uses the predicate for the title attribute' );
590 $person->clear_title;
591 ok( !$person->has_title, 'Person clear_title method cleared the title' );
593 account_tests( $person, 100 );
597 my $employee = Employee->new(
598 first_name => 'Jimmy',
605 $employee->salary, 30000,
606 'salary is calculated from salary_level, and salary passed to constructor is ignored'
611 my $person = Person->new(
612 first_name => 'Bilbo',
613 last_name => 'Baggins',
616 isa_ok( $person->account, 'BankAccount' );
618 $person->account->owner, $person,
619 'owner of bank account is person that created account'
622 $person->deposit(10);
624 $person->account->history, [100],
625 'deposit was recorded in account history'
628 $person->withdraw(15);
630 $person->account->history, [ 100, 110 ],
631 'withdrawal was recorded in account history'
634 $person->withdraw(45);
636 $person->account->history, [ 100, 110, 95 ],
637 'withdrawal was recorded in account history'
642 local $Test::Builder::Level = $Test::Builder::Level + 1;
645 my $base_amount = shift || 0;
647 $person->deposit(50);
648 eval { $person->withdraw( 75 + $base_amount ) };
650 $@, qr/\QBalance cannot be negative/,
651 'cannot withdraw more than is in our balance'
654 $person->withdraw(23);
657 $person->balance, 27 + $base_amount,
658 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23'