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' );
20 no_droppings('Person');
21 is_immutable('Person');
27 check_isa( 'Employee', [ 'Person', 'Moose::Object' ] );
29 has_rw_attr( 'Employee', $_ ) for qw( title salary );
30 has_ro_attr( 'Employee', 'ssn' );
32 has_overridden_method( 'Employee', 'full_name' );
40 local $Test::Builder::Level = $Test::Builder::Level + 1;
42 no_droppings($_) for qw( Printable HasAccount );
44 does_role( 'Person', $_ ) for qw( Printable HasAccount );
45 has_method( 'Person', $_ ) for qw( as_string deposit withdraw );
46 has_rw_attr( 'Person', 'balance' );
48 does_role( 'Employee', $_ ) for qw( Printable HasAccount );
56 local $Test::Builder::Level = $Test::Builder::Level + 1;
61 has_rw_attr( 'Person', 'title' );
63 has_rw_attr( 'Employee', 'title', 'overridden' );
64 has_rw_attr( 'Employee', 'salary_level' );
65 has_ro_attr( 'Employee', 'salary' );
67 has_method( 'Employee', '_build_salary' );
70 ok( ! Employee->meta->has_method('full_name'),
71 'Employee no longer implements a full_name method' );
73 my $person_title_attr = Person->meta->get_attribute('title');
74 ok( !$person_title_attr->is_required, 'title is not required in Person' );
75 is( $person_title_attr->predicate, 'has_title',
76 'Person title attr has a has_title predicate' );
77 is( $person_title_attr->clearer, 'clear_title',
78 'Person title attr has a clear_title clearer' );
80 my $balance_attr = Person->meta->get_attribute('balance');
81 is( $balance_attr->default, 100, 'balance defaults to 100' );
83 my $employee_title_attr = Employee->meta->get_attribute('title');
84 is( $employee_title_attr->default, 'Worker',
85 'title defaults to Worker in Employee' );
87 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
88 is( $salary_level_attr->default, 1, 'salary_level defaults to 1' );
90 my $salary_attr = Employee->meta->get_attribute('salary');
91 ok( !$salary_attr->init_arg, 'no init_arg for salary attribute' );
92 ok( $salary_attr->has_builder, 'salary attr has a builder' );
100 local $Test::Builder::Level = $Test::Builder::Level + 1;
102 has_meta('Document');
104 has_meta('TPSReport');
106 no_droppings('Document');
107 no_droppings('Report');
108 no_droppings('TPSReport');
110 has_ro_attr( 'Document', $_ ) for qw( title author );
111 has_ro_attr( 'Report', 'summary' );
112 has_ro_attr( 'TPSReport', $_ ) for qw( t p s );
114 has_method( 'Document', 'output' );
115 has_augmented_method( 'Report', 'output' );
116 has_augmented_method( 'TPSReport', 'output' );
119 my $tps = TPSReport->new(
120 title => 'That TPS Report',
121 author => 'Peter Gibbons (for Bill Lumberg)',
122 summary => 'I celebrate his whole collection!',
123 t => 'PC Load Letter',
128 my $output = $tps->output;
129 $output =~ s/\n\n+/\n/g;
131 is( $output, <<'EOF', 'output returns expected report' );
133 I celebrate his whole collection!
137 Written by Peter Gibbons (for Bill Lumberg)
143 local $Test::Builder::Level = $Test::Builder::Level + 1;
146 has_meta('Employee');
147 no_droppings('Employee');
150 for my $attr_name ( qw( first_name last_name title ) ) {
151 my $attr = Person->meta->get_attribute($attr_name);
153 ok( $attr->has_type_constraint,
154 "Person $attr_name has a type constraint" );
155 is( $attr->type_constraint->name, 'Str',
156 "Person $attr_name type is Str" );
160 my $salary_level_attr = Employee->meta->get_attribute('salary_level');
161 ok( $salary_level_attr->has_type_constraint,
162 'Employee salary_level has a type constraint' );
164 my $tc = $salary_level_attr->type_constraint;
166 for my $invalid ( 0, 11, -14, 'foo', undef ) {
167 my $str = defined $invalid ? $invalid : 'undef';
168 ok( ! $tc->check($invalid),
169 "salary_level type rejects invalid value - $str" );
172 for my $valid ( 1..10 ) {
173 ok( $tc->check($valid),
174 "salary_level type accepts valid value - $valid" );
179 my $salary_attr = Employee->meta->get_attribute('salary');
181 ok( $salary_attr->has_type_constraint,
182 'Employee salary has a type constraint' );
184 my $tc = $salary_attr->type_constraint;
186 for my $invalid ( 0, -14, 'foo', undef ) {
187 my $str = defined $invalid ? $invalid : 'undef';
188 ok( ! $tc->check($invalid),
189 "salary type rejects invalid value - $str" );
192 for my $valid ( 1, 100_000, 10**10 ) {
193 ok( $tc->check($valid),
194 "salary type accepts valid value - $valid" );
199 my $ssn_attr = Employee->meta->get_attribute('ssn');
201 ok( $ssn_attr->has_type_constraint,
202 'Employee ssn has a type constraint' );
204 my $tc = $ssn_attr->type_constraint;
206 for my $invalid ( 0, -14, 'foo', undef, '123-ab-1241', '123456789' ) {
207 my $str = defined $invalid ? $invalid : 'undef';
208 ok( ! $tc->check($invalid),
209 "ssn type rejects invalid value - $str" );
212 for my $valid ( '041-12-1251', '123-45-6789', '926-41-5820' ) {
213 ok( $tc->check($valid),
214 "ssn type accepts valid value - $valid" );
221 local $Test::Builder::Level = $Test::Builder::Level + 1;
223 has_meta('BankAccount');
224 no_droppings('BankAccount');
226 has_rw_attr( 'BankAccount', 'balance' );
227 has_rw_attr( 'BankAccount', 'owner' );
228 has_ro_attr( 'BankAccount', 'history' );
231 my $ba_meta = BankAccount->meta;
232 ok( $ba_meta->has_attribute('balance'),
233 'BankAccount class has a balance attribute' );
235 my $history_attr = $ba_meta->get_attribute('history');
238 $history_attr->meta()
239 ->does_role('Moose::Meta::Attribute::Native::Trait::Array'),
240 'BankAccount history attribute uses native delegation to an array ref'
243 ok( $ba_meta->get_attribute('balance')->has_trigger,
244 'BankAccount balance attribute has a trigger' );
246 my $person_meta = Person->meta;
247 ok( ! $person_meta->has_attribute('balance'),
248 'Person class does not have a balance attribute' );
250 my $deposit_meth = $person_meta->get_method('deposit');
251 isa_ok( $deposit_meth, 'Moose::Meta::Method::Delegation' );
253 my $withdraw_meth = $person_meta->get_method('withdraw');
254 isa_ok( $withdraw_meth, 'Moose::Meta::Method::Delegation' );
256 ok( $ba_meta->get_attribute('owner')->is_weak_ref,
257 'owner attribute is a weak ref' );
266 ok( $class->can('meta'), "$class has a meta() method" )
267 or BAIL_OUT("$class does not have a meta() method (did you forget to 'use Moose'?)");
274 my @isa = $class->meta->linearized_isa;
275 shift @isa; # returns $class as the first entry
277 my $count = scalar @{$parents};
278 my $noun = PL_N( 'parent', $count );
280 is( scalar @isa, $count, "$class has $count $noun" );
282 for ( my $i = 0; $i < @{$parents}; $i++ ) {
283 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
290 my $overridden = shift;
292 my $articled = $overridden ? "an overridden $name" : A($name);
293 ok( $class->meta->has_attribute($name),
294 "$class has $articled attribute" );
296 my $attr = $class->meta->get_attribute($name);
298 is( $attr->get_read_method, $name,
299 "$name attribute has a reader accessor - $name()" );
300 is( $attr->get_write_method, $name,
301 "$name attribute has a writer accessor - $name()" );
308 my $articled = A($name);
309 ok( $class->meta->has_attribute($name),
310 "$class has $articled attribute" );
312 my $attr = $class->meta->get_attribute($name);
314 is( $attr->get_read_method, $name,
315 "$name attribute has a reader accessor - $name()" );
316 is( $attr->get_write_method, undef,
317 "$name attribute does not have a writer" );
324 my $articled = A($name);
325 ok( $class->meta->has_method($name), "$class has $articled method" );
328 sub has_overridden_method {
332 my $articled = A($name);
333 ok( $class->meta->has_method($name), "$class has $articled method" );
335 my $meth = $class->meta->get_method($name);
336 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
339 sub has_augmented_method {
343 my $articled = A($name);
344 ok( $class->meta->has_method($name), "$class has $articled method" );
346 my $meth = $class->meta->get_method($name);
347 isa_ok( $meth, 'Moose::Meta::Method::Augmented' );
353 ok( !$class->can('has'), "no Moose droppings in $class" );
354 ok( !$class->can('subtype'), "no Moose::Util::TypeConstraints droppings in $class" );
360 ok( $class->meta->is_immutable, "$class has been made immutable" );
367 ok( $class->meta->does_role($role), "$class does the $role role" );
371 my $person = Person->new(
372 first_name => 'Bilbo',
373 last_name => 'Baggins',
376 is( $person->full_name, 'Bilbo Baggins',
377 'full_name() is correctly implemented' );
379 $person = eval { Person->new( [ qw( Lisa Smith ) ] ) };
380 ok( !$@, 'Person->new() can accept an array reference as an argument' )
382 'You must implement Person->BUILDARGS in order to continue these tests'
385 is( $person->first_name, 'Lisa', 'set first_name from two-arg arrayref' );
386 is( $person->last_name, 'Smith', 'set last_name from two-arg arrayref' );
388 eval { Person->new( sub { 'foo' } ) };
389 like( $@, qr/\QSingle parameters to new() must be a HASH ref/,
390 'Person constructor still rejects bad parameters' );
394 my $employee = Employee->new(
395 first_name => 'Amanda',
396 last_name => 'Palmer',
401 my $orig_super = \&Employee::super;
402 no warnings 'redefine';
403 local *Employee::super = sub { $called++; goto &$orig_super };
405 is( $employee->full_name, 'Amanda Palmer (Singer)',
406 'full_name() is properly overriden in Employee' );
407 ok( $called, 'Employee->full_name calls super()' );
411 my $person = Person->new(
412 first_name => 'Bilbo',
413 last_name => 'Baggins',
417 is( $person->as_string, 'Bilbo Baggins',
418 'as_string() is correctly implemented' );
420 account_tests($person);
424 my $employee = Employee->new(
425 first_name => 'Amanda',
426 last_name => 'Palmer',
431 is( $employee->as_string, 'Amanda Palmer (Singer)',
432 'as_string() uses overridden full_name method in Employee' );
434 account_tests($employee);
438 my $person = Person->new(
439 first_name => 'Bilbo',
440 last_name => 'Baggins',
443 is( $person->full_name, 'Bilbo Baggins',
444 'full_name() is correctly implemented for a Person without a title' );
445 ok( !$person->has_title,
446 'Person has_title predicate is working correctly (returns false)' );
448 $person->title('Ringbearer');
449 ok( $person->has_title, 'Person has_title predicate is working correctly (returns true)' );
452 my $orig_pred = \&Person::has_title;
453 no warnings 'redefine';
454 local *Person::has_title = sub { $called++; goto &$orig_pred };
456 is( $person->full_name, 'Bilbo Baggins (Ringbearer)',
457 'full_name() is correctly implemented for a Person with a title' );
458 ok( $called, 'full_name in person uses the predicate for the title attribute' );
460 $person->clear_title;
461 ok( !$person->has_title, 'Person clear_title method cleared the title' );
463 account_tests( $person, 100 );
467 my $employee = Employee->new(
468 first_name => 'Jimmy',
474 is( $employee->salary, 30000,
475 'salary is calculated from salary_level, and salary passed to constructor is ignored' );
479 my $person = Person->new(
480 first_name => 'Bilbo',
481 last_name => 'Baggins',
484 isa_ok( $person->account, 'BankAccount' );
485 is( $person->account->owner, $person,
486 'owner of bank account is person that created account' );
488 $person->deposit(10);
489 is_deeply( $person->account->history, [ 100 ],
490 'deposit was recorded in account history' );
492 $person->withdraw(15);
493 is_deeply( $person->account->history, [ 100, 110 ],
494 'withdrawal was recorded in account history' );
496 $person->withdraw(45);
497 is_deeply( $person->account->history, [ 100, 110, 95 ],
498 'withdrawal was recorded in account history' );
502 local $Test::Builder::Level = $Test::Builder::Level + 1;
505 my $base_amount = shift || 0;
507 $person->deposit(50);
508 eval { $person->withdraw( 75 + $base_amount ) };
509 like( $@, qr/\QBalance cannot be negative/,
510 'cannot withdraw more than is in our balance' );
512 $person->withdraw( 23 );
514 is( $person->balance, 27 + $base_amount,
515 'balance is 27 (+ starting balance) after deposit of 50 and withdrawal of 23' );