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');
126 has_meta('TPSReport');
128 no_droppings('Document');
129 no_droppings('Report');
130 no_droppings('TPSReport');
132 has_ro_attr( 'Document', $_ ) for qw( title author );
133 has_ro_attr( 'Report', 'summary' );
134 has_ro_attr( 'TPSReport', $_ ) for qw( t p s );
136 has_method( 'Document', 'output' );
137 has_augmented_method( 'Report', 'output' );
138 has_augmented_method( 'TPSReport', 'output' );
140 my $tps = TPSReport->new(
141 title => 'That TPS Report',
142 author => 'Peter Gibbons (for Bill Lumberg)',
143 summary => 'I celebrate his whole collection!',
144 t => 'PC Load Letter',
149 my $output = $tps->output;
150 $output =~ s/\n\n+/\n/g;
152 is( $output, <<'EOF', 'output returns expected report' );
154 I celebrate his whole collection!
158 Written by Peter Gibbons (for Bill Lumberg)
164 has_meta('Employee');
165 no_droppings('Employee');
167 for my $attr_name (qw( first_name last_name title )) {
168 my $attr = Person->meta->get_attribute($attr_name);
171 $attr->has_type_constraint,
172 "Person $attr_name has a type constraint"
175 $attr->type_constraint->name, 'Str',
176 "Person $attr_name type is Str"
181 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
183 $salary_level_attr->has_type_constraint,
184 'Employee salary_level has a type constraint'
187 my $tc = $salary_level_attr->type_constraint;
189 for my $invalid ( 0, 11, -14, 'foo', undef ) {
190 my $str = defined $invalid ? $invalid : 'undef';
192 !$tc->check($invalid),
193 "salary_level type rejects invalid value - $str"
197 for my $valid ( 1 .. 10 ) {
200 "salary_level type accepts valid value - $valid"
206 my $salary_attr = Employee->meta->get_attribute('salary');
209 $salary_attr->has_type_constraint,
210 'Employee salary has a type constraint'
213 my $tc = $salary_attr->type_constraint;
215 for my $invalid ( 0, -14, 'foo', undef ) {
216 my $str = defined $invalid ? $invalid : 'undef';
218 !$tc->check($invalid),
219 "salary type rejects invalid value - $str"
223 for my $valid ( 1, 100_000, 10**10 ) {
226 "salary type accepts valid value - $valid"
232 my $ssn_attr = Employee->meta->get_attribute('ssn');
235 $ssn_attr->has_type_constraint,
236 'Employee ssn has a type constraint'
239 my $tc = $ssn_attr->type_constraint;
241 for my $invalid ( 0, -14, 'foo', undef, '123-ab-1241', '123456789' ) {
242 my $str = defined $invalid ? $invalid : 'undef';
244 !$tc->check($invalid),
245 "ssn type rejects invalid value - $str"
249 for my $valid ( '041-12-1251', '123-45-6789', '926-41-5820' ) {
252 "ssn type accepts valid value - $valid"
260 has_meta('Employee');
261 has_meta('BankAccount');
262 no_droppings('BankAccount');
264 has_rw_attr( 'BankAccount', 'balance' );
265 has_rw_attr( 'BankAccount', 'owner' );
266 has_ro_attr( 'BankAccount', 'history' );
268 my $ba_meta = BankAccount->meta;
270 $ba_meta->has_attribute('balance'),
271 'BankAccount class has a balance attribute'
274 my $history_attr = $ba_meta->get_attribute('history');
277 $history_attr->meta()
278 ->does_role('Moose::Meta::Attribute::Native::Trait::Array'),
279 'BankAccount history attribute uses native delegation to an array ref'
283 $ba_meta->get_attribute('balance')->has_trigger,
284 'BankAccount balance attribute has a trigger'
287 my $person_meta = Person->meta;
289 !$person_meta->has_attribute('balance'),
290 'Person class does not have a balance attribute'
293 my $deposit_meth = $person_meta->get_method('deposit');
294 isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' );
296 my $withdraw_meth = $person_meta->get_method('withdraw');
297 isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' );
300 $ba_meta->get_attribute('owner')->is_weak_ref,
301 'owner attribute is a weak ref'
310 local $Test::Builder::Level = $Test::Builder::Level + 1;
313 or BAIL_OUT("$package cannot be loaded");
315 ok( $package->can('meta'), "$package has a meta() method" )
317 "$package does not have a meta() method (did you forget to 'use Moose'?)"
325 local $Test::Builder::Level = $Test::Builder::Level + 1;
327 my @isa = $class->meta->linearized_isa;
328 shift @isa; # returns $class as the first entry
330 my $count = scalar @{$parents};
331 my $noun = PL_N( 'parent', $count );
333 is( scalar @isa, $count, "$class has $count $noun" );
335 for ( my $i = 0; $i < @{$parents}; $i++ ) {
336 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
343 my $overridden = shift;
345 local $Test::Builder::Level = $Test::Builder::Level + 1;
347 my $articled = $overridden ? "an overridden $name" : A($name);
349 $class->meta->has_attribute($name),
350 "$class has $articled attribute"
353 my $attr = $class->meta->get_attribute($name);
356 $attr->get_read_method, $name,
357 "$name attribute has a reader accessor - $name()"
360 $attr->get_write_method, $name,
361 "$name attribute has a writer accessor - $name()"
369 local $Test::Builder::Level = $Test::Builder::Level + 1;
371 my $articled = A($name);
373 $class->meta->has_attribute($name),
374 "$class has $articled attribute"
377 my $attr = $class->meta->get_attribute($name);
380 $attr->get_read_method, $name,
381 "$name attribute has a reader accessor - $name()"
384 $attr->get_write_method, undef,
385 "$name attribute does not have a writer"
393 local $Test::Builder::Level = $Test::Builder::Level + 1;
395 my $articled = A($name);
397 $role->meta->get_attribute($name),
398 "$role has $articled attribute"
406 local $Test::Builder::Level = $Test::Builder::Level + 1;
408 my $articled = A($name);
409 ok( $package->meta->has_method($name), "$package has $articled method" );
412 sub has_overridden_method {
416 local $Test::Builder::Level = $Test::Builder::Level + 1;
418 my $articled = A($name);
419 ok( $package->meta->has_method($name), "$package has $articled method" );
421 my $meth = $package->meta->get_method($name);
422 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
425 sub has_augmented_method {
429 local $Test::Builder::Level = $Test::Builder::Level + 1;
431 my $articled = A($name);
432 ok( $class->meta->has_method($name), "$class has $articled method" );
434 my $meth = $class->meta->get_method($name);
435 isa_ok( $meth, 'Moose::Meta::Method::Augmented' );
438 sub requires_method {
442 local $Test::Builder::Level = $Test::Builder::Level + 1;
445 $package->meta->requires_method($method),
446 "$package requires the method $method"
453 local $Test::Builder::Level = $Test::Builder::Level + 1;
455 ok( !$package->can('has'), "no Moose droppings in $package" );
456 ok( !$package->can('subtype'),
457 "no Moose::Util::TypeConstraints droppings in $package" );
463 local $Test::Builder::Level = $Test::Builder::Level + 1;
465 ok( $class->meta->is_immutable, "$class has been made immutable" );
472 local $Test::Builder::Level = $Test::Builder::Level + 1;
474 ok( $package->meta->does_role($role), "$package does the $role role" );
478 my $person = Person->new(
479 first_name => 'Bilbo',
480 last_name => 'Baggins',
484 $person->full_name, 'Bilbo Baggins',
485 'full_name() is correctly implemented'
488 $person = eval { Person->new( [ qw( Lisa Smith ) ] ) };
489 ok( !$@, 'Person->new() can accept an array reference as an argument' )
491 'You must implement Person->BUILDARGS in order to continue these tests'
494 is( $person->first_name, 'Lisa', 'set first_name from two-arg arrayref' );
495 is( $person->last_name, 'Smith', 'set last_name from two-arg arrayref' );
498 Person->new( sub {'foo'} );
501 $@, qr/\QSingle parameters to new() must be a HASH ref/,
502 'Person constructor still rejects bad parameters'
507 my $employee = Employee->new(
508 first_name => 'Amanda',
509 last_name => 'Palmer',
514 my $orig_super = \&Employee::super;
515 no warnings 'redefine';
516 local *Employee::super = sub { $called++; goto &$orig_super };
519 $employee->full_name, 'Amanda Palmer (Singer)',
520 'full_name() is properly overriden in Employee'
522 ok( $called, 'Employee->full_name calls super()' );
526 my $person = Person->new(
527 first_name => 'Bilbo',
528 last_name => 'Baggins',
533 $person->as_string, 'Bilbo Baggins',
534 'as_string() is correctly implemented'
537 account_tests($person);
541 my $employee = Employee->new(
542 first_name => 'Amanda',
543 last_name => 'Palmer',
549 $employee->as_string, 'Amanda Palmer (Singer)',
550 'as_string() uses overridden full_name method in Employee'
553 account_tests($employee);
557 my $person = Person->new(
558 first_name => 'Bilbo',
559 last_name => 'Baggins',
563 $person->full_name, 'Bilbo Baggins',
564 'full_name() is correctly implemented for a Person without a title'
568 'Person has_title predicate is working correctly (returns false)'
571 $person->title('Ringbearer');
572 ok( $person->has_title,
573 'Person has_title predicate is working correctly (returns true)' );
576 my $orig_pred = \&Person::has_title;
577 no warnings 'redefine';
578 local *Person::has_title = sub { $called++; goto &$orig_pred };
581 $person->full_name, 'Bilbo Baggins (Ringbearer)',
582 'full_name() is correctly implemented for a Person with a title'
585 'full_name in person uses the predicate for the title attribute' );
587 $person->clear_title;
588 ok( !$person->has_title, 'Person clear_title method cleared the title' );
590 account_tests( $person, 100 );
594 my $employee = Employee->new(
595 first_name => 'Jimmy',
602 $employee->salary, 30000,
603 'salary is calculated from salary_level, and salary passed to constructor is ignored'
608 my $person = Person->new(
609 first_name => 'Bilbo',
610 last_name => 'Baggins',
613 isa_ok( $person->account, 'BankAccount' );
615 $person->account->owner, $person,
616 'owner of bank account is person that created account'
619 $person->deposit(10);
621 $person->account->history, [100],
622 'deposit was recorded in account history'
625 $person->withdraw(15);
627 $person->account->history, [ 100, 110 ],
628 'withdrawal was recorded in account history'
631 $person->withdraw(45);
633 $person->account->history, [ 100, 110, 95 ],
634 'withdrawal was recorded in account history'
639 local $Test::Builder::Level = $Test::Builder::Level + 1;
642 my $base_amount = shift || 0;
644 $person->deposit(50);
645 eval { $person->withdraw( 75 + $base_amount ) };
647 $@, qr/\QBalance cannot be negative/,
648 'cannot withdraw more than is in our balance'
651 $person->withdraw(23);
654 $person->balance, 27 + $base_amount,
655 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23'