1 package MooseClass::Tests;
6 use Lingua::EN::Inflect qw( A PL_N );
7 use Test::More 'no_plan';
10 local $Test::Builder::Level = $Test::Builder::Level + 1;
14 check_isa( 'Person', ['Moose::Object'] );
16 has_rw_attr( 'Person', $_ ) for qw( first_name last_name );
18 has_method( 'Person', 'full_name' );
24 check_isa( 'Employee', [ 'Person', 'Moose::Object' ] );
26 has_rw_attr( 'Employee', $_ ) for qw( title salary );
27 has_ro_attr( 'Employee', 'ssn' );
29 has_overridden_method( 'Employee', 'full_name' );
33 no_droppings('Person');
34 is_immutable('Person');
38 local $Test::Builder::Level = $Test::Builder::Level + 1;
40 has_meta('Printable');
41 requires_method( 'Printable', 'as_string' );
44 does_role( 'Person', 'Printable' );
45 has_method( 'Person', 'as_string' );
47 has_meta('HasAccount');
48 has_method( 'HasAccount', $_ ) for qw( deposit withdraw );
49 has_role_attr( 'HasAccount', 'balance' );
51 does_role( 'Person', 'HasAccount' );
52 has_method( 'Person', $_ ) for qw( deposit withdraw );
53 has_rw_attr( 'Person', 'balance' );
56 does_role( 'Employee', $_ ) for qw( Printable HasAccount );
61 no_droppings($_) for qw( Printable HasAccount );
68 local $Test::Builder::Level = $Test::Builder::Level + 1;
73 has_rw_attr( 'Person', 'title' );
75 has_rw_attr( 'Employee', 'title', 'overridden' );
76 has_rw_attr( 'Employee', 'salary_level' );
77 has_ro_attr( 'Employee', 'salary' );
79 has_method( 'Employee', '_build_salary' );
83 !Employee->meta->has_method('full_name'),
84 'Employee no longer implements a full_name method'
87 my $person_title_attr = Person->meta->get_attribute('title');
88 ok( !$person_title_attr->is_required, 'title is not required in Person' );
90 $person_title_attr->predicate, 'has_title',
91 'Person title attr has a has_title predicate'
94 $person_title_attr->clearer, 'clear_title',
95 'Person title attr has a clear_title clearer'
98 my $balance_attr = Person->meta->get_attribute('balance');
99 is( $balance_attr->default, 100, 'balance defaults to 100' );
101 my $employee_title_attr = Employee->meta->get_attribute('title');
103 $employee_title_attr->default, 'Worker',
104 'title defaults to Worker in Employee'
107 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
108 is( $salary_level_attr->default, 1, 'salary_level defaults to 1' );
110 my $salary_attr = Employee->meta->get_attribute('salary');
111 ok( !$salary_attr->init_arg, 'no init_arg for salary attribute' );
112 ok( $salary_attr->has_builder, 'salary attr has a builder' );
120 local $Test::Builder::Level = $Test::Builder::Level + 1;
122 has_meta('Document');
124 has_meta('TPSReport');
126 no_droppings('Document');
127 no_droppings('Report');
128 no_droppings('TPSReport');
130 has_ro_attr( 'Document', $_ ) for qw( title author );
131 has_ro_attr( 'Report', 'summary' );
132 has_ro_attr( 'TPSReport', $_ ) for qw( t p s );
134 has_method( 'Document', 'output' );
135 has_augmented_method( 'Report', 'output' );
136 has_augmented_method( 'TPSReport', 'output' );
139 my $tps = TPSReport->new(
140 title => 'That TPS Report',
141 author => 'Peter Gibbons (for Bill Lumberg)',
142 summary => 'I celebrate his whole collection!',
143 t => 'PC Load Letter',
148 my $output = $tps->output;
149 $output =~ s/\n\n+/\n/g;
151 is( $output, <<'EOF', 'output returns expected report' );
153 I celebrate his whole collection!
157 Written by Peter Gibbons (for Bill Lumberg)
163 local $Test::Builder::Level = $Test::Builder::Level + 1;
166 has_meta('Employee');
167 no_droppings('Employee');
170 for my $attr_name (qw( first_name last_name title )) {
171 my $attr = Person->meta->get_attribute($attr_name);
174 $attr->has_type_constraint,
175 "Person $attr_name has a type constraint"
178 $attr->type_constraint->name, 'Str',
179 "Person $attr_name type is Str"
184 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
186 $salary_level_attr->has_type_constraint,
187 'Employee salary_level has a type constraint'
190 my $tc = $salary_level_attr->type_constraint;
192 for my $invalid ( 0, 11, -14, 'foo', undef ) {
193 my $str = defined $invalid ? $invalid : 'undef';
195 !$tc->check($invalid),
196 "salary_level type rejects invalid value - $str"
200 for my $valid ( 1 .. 10 ) {
203 "salary_level type accepts valid value - $valid"
209 my $salary_attr = Employee->meta->get_attribute('salary');
212 $salary_attr->has_type_constraint,
213 'Employee salary has a type constraint'
216 my $tc = $salary_attr->type_constraint;
218 for my $invalid ( 0, -14, 'foo', undef ) {
219 my $str = defined $invalid ? $invalid : 'undef';
221 !$tc->check($invalid),
222 "salary type rejects invalid value - $str"
226 for my $valid ( 1, 100_000, 10**10 ) {
229 "salary type accepts valid value - $valid"
235 my $ssn_attr = Employee->meta->get_attribute('ssn');
238 $ssn_attr->has_type_constraint,
239 'Employee ssn has a type constraint'
242 my $tc = $ssn_attr->type_constraint;
244 for my $invalid ( 0, -14, 'foo', undef, '123-ab-1241', '123456789' ) {
245 my $str = defined $invalid ? $invalid : 'undef';
247 !$tc->check($invalid),
248 "ssn type rejects invalid value - $str"
252 for my $valid ( '041-12-1251', '123-45-6789', '926-41-5820' ) {
255 "ssn type accepts valid value - $valid"
263 local $Test::Builder::Level = $Test::Builder::Level + 1;
266 has_meta('Employee');
267 has_meta('BankAccount');
268 no_droppings('BankAccount');
270 has_rw_attr( 'BankAccount', 'balance' );
271 has_rw_attr( 'BankAccount', 'owner' );
272 has_ro_attr( 'BankAccount', 'history' );
275 my $ba_meta = BankAccount->meta;
277 $ba_meta->has_attribute('balance'),
278 'BankAccount class has a balance attribute'
281 my $history_attr = $ba_meta->get_attribute('history');
284 $history_attr->meta()
285 ->does_role('Moose::Meta::Attribute::Native::Trait::Array'),
286 'BankAccount history attribute uses native delegation to an array ref'
290 $ba_meta->get_attribute('balance')->has_trigger,
291 'BankAccount balance attribute has a trigger'
294 my $person_meta = Person->meta;
296 !$person_meta->has_attribute('balance'),
297 'Person class does not have a balance attribute'
300 my $deposit_meth = $person_meta->get_method('deposit');
301 isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' );
303 my $withdraw_meth = $person_meta->get_method('withdraw');
304 isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' );
307 $ba_meta->get_attribute('owner')->is_weak_ref,
308 'owner attribute is a weak ref'
318 or BAIL_OUT("$package cannot be loaded");
320 ok( $package->can('meta'), "$package has a meta() method" )
322 "$package does not have a meta() method (did you forget to 'use Moose'?)"
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 my $articled = $overridden ? "an overridden $name" : A($name);
350 $class->meta->has_attribute($name),
351 "$class has $articled attribute"
354 my $attr = $class->meta->get_attribute($name);
357 $attr->get_read_method, $name,
358 "$name attribute has a reader accessor - $name()"
361 $attr->get_write_method, $name,
362 "$name attribute has a writer accessor - $name()"
370 my $articled = A($name);
372 $class->meta->has_attribute($name),
373 "$class has $articled attribute"
376 my $attr = $class->meta->get_attribute($name);
379 $attr->get_read_method, $name,
380 "$name attribute has a reader accessor - $name()"
383 $attr->get_write_method, undef,
384 "$name attribute does not have a writer"
392 my $articled = A($name);
394 $role->meta->get_attribute($name),
395 "$role has $articled attribute"
403 my $articled = A($name);
404 ok( $package->meta->has_method($name), "$package has $articled method" );
407 sub has_overridden_method {
411 my $articled = A($name);
412 ok( $package->meta->has_method($name), "$package has $articled method" );
414 my $meth = $package->meta->get_method($name);
415 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
418 sub has_augmented_method {
422 my $articled = A($name);
423 ok( $class->meta->has_method($name), "$class has $articled method" );
425 my $meth = $class->meta->get_method($name);
426 isa_ok( $meth, 'Moose::Meta::Method::Augmented' );
429 sub requires_method {
434 $package->meta->requires_method($method),
435 "$package requires the method $method"
442 ok( !$package->can('has'), "no Moose droppings in $package" );
443 ok( !$package->can('subtype'),
444 "no Moose::Util::TypeConstraints droppings in $package" );
450 ok( $class->meta->is_immutable, "$class has been made immutable" );
457 ok( $package->meta->does_role($role), "$package does the $role role" );
461 my $person = Person->new(
462 first_name => 'Bilbo',
463 last_name => 'Baggins',
467 $person->full_name, 'Bilbo Baggins',
468 'full_name() is correctly implemented'
471 $person = eval { Person->new( [ qw( Lisa Smith ) ] ) };
472 ok( !$@, 'Person->new() can accept an array reference as an argument' )
474 'You must implement Person->BUILDARGS in order to continue these tests'
477 is( $person->first_name, 'Lisa', 'set first_name from two-arg arrayref' );
478 is( $person->last_name, 'Smith', 'set last_name from two-arg arrayref' );
481 Person->new( sub {'foo'} );
484 $@, qr/\QSingle parameters to new() must be a HASH ref/,
485 'Person constructor still rejects bad parameters'
490 my $employee = Employee->new(
491 first_name => 'Amanda',
492 last_name => 'Palmer',
497 my $orig_super = \&Employee::super;
498 no warnings 'redefine';
499 local *Employee::super = sub { $called++; goto &$orig_super };
502 $employee->full_name, 'Amanda Palmer (Singer)',
503 'full_name() is properly overriden in Employee'
505 ok( $called, 'Employee->full_name calls super()' );
509 my $person = Person->new(
510 first_name => 'Bilbo',
511 last_name => 'Baggins',
516 $person->as_string, 'Bilbo Baggins',
517 'as_string() is correctly implemented'
520 account_tests($person);
524 my $employee = Employee->new(
525 first_name => 'Amanda',
526 last_name => 'Palmer',
532 $employee->as_string, 'Amanda Palmer (Singer)',
533 'as_string() uses overridden full_name method in Employee'
536 account_tests($employee);
540 my $person = Person->new(
541 first_name => 'Bilbo',
542 last_name => 'Baggins',
546 $person->full_name, 'Bilbo Baggins',
547 'full_name() is correctly implemented for a Person without a title'
551 'Person has_title predicate is working correctly (returns false)'
554 $person->title('Ringbearer');
555 ok( $person->has_title,
556 'Person has_title predicate is working correctly (returns true)' );
559 my $orig_pred = \&Person::has_title;
560 no warnings 'redefine';
561 local *Person::has_title = sub { $called++; goto &$orig_pred };
564 $person->full_name, 'Bilbo Baggins (Ringbearer)',
565 'full_name() is correctly implemented for a Person with a title'
568 'full_name in person uses the predicate for the title attribute' );
570 $person->clear_title;
571 ok( !$person->has_title, 'Person clear_title method cleared the title' );
573 account_tests( $person, 100 );
577 my $employee = Employee->new(
578 first_name => 'Jimmy',
585 $employee->salary, 30000,
586 'salary is calculated from salary_level, and salary passed to constructor is ignored'
591 my $person = Person->new(
592 first_name => 'Bilbo',
593 last_name => 'Baggins',
596 isa_ok( $person->account, 'BankAccount' );
598 $person->account->owner, $person,
599 'owner of bank account is person that created account'
602 $person->deposit(10);
604 $person->account->history, [100],
605 'deposit was recorded in account history'
608 $person->withdraw(15);
610 $person->account->history, [ 100, 110 ],
611 'withdrawal was recorded in account history'
614 $person->withdraw(45);
616 $person->account->history, [ 100, 110, 95 ],
617 'withdrawal was recorded in account history'
622 local $Test::Builder::Level = $Test::Builder::Level + 1;
625 my $base_amount = shift || 0;
627 $person->deposit(50);
628 eval { $person->withdraw( 75 + $base_amount ) };
630 $@, qr/\QBalance cannot be negative/,
631 'cannot withdraw more than is in our balance'
634 $person->withdraw(23);
637 $person->balance, 27 + $base_amount,
638 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23'