1 package MooseClass::Tests;
6 use Lingua::EN::Inflect qw( PL_N );
7 use Test::More 'no_plan';
10 local $Test::Builder::Level = $Test::Builder::Level + 1;
14 check_isa( 'Person', ['Moose::Object'] );
16 count_attrs( 'Person', 2 );
18 has_rw_attr( 'Person', $_ ) for qw( first_name last_name );
20 has_method( 'Person', 'full_name' );
22 no_droppings('Person');
23 is_immutable('Person');
29 check_isa( 'Employee', [ 'Person', 'Moose::Object' ] );
31 count_attrs( 'Employee', 3 );
33 has_rw_attr( 'Employee', $_ ) for qw( position salary );
34 has_ro_attr( 'Employee', 'ssn' );
36 has_overridden_method( 'Employee', 'full_name' );
44 ok( $class->can('meta'), "$class has a meta() method" )
45 or BAIL_OUT("Cannot run tests against a class without a meta!");
52 my @isa = $class->meta->linearized_isa;
53 shift @isa; # returns $class as the first entry
55 my $count = scalar @{$parents};
56 my $noun = PL_N( 'parent', $count );
58 is( scalar @isa, $count, "$class has $count $noun" );
60 for ( my $i = 0; $i < @{$parents}; $i++ ) {
61 is( $isa[$i], $parents->[$i], "parent[$i] is $parents->[$i]" );
69 my $noun = PL_N( 'attribute', $count );
70 is( scalar $class->meta->get_attribute_list, $count,
71 "$class defines $count $noun" );
78 ok( $class->meta->has_attribute($name), "$class has attribute - $name" );
80 my $attr = $class->meta->get_attribute($name);
82 is( $attr->get_read_method, $name,
83 "$name attribute has a reader accessor - $name()" );
84 is( $attr->get_write_method, $name,
85 "$name attribute has a writer accessor - $name()" );
92 ok( $class->meta->has_attribute($name), "$class has attribute - $name" );
94 my $attr = $class->meta->get_attribute($name);
96 is( $attr->get_read_method, $name,
97 "$name attribute has a reader accessor - $name()" );
98 is( $attr->get_write_method, undef,
99 "$name attribute does not have a writer" );
106 ok( $class->meta->has_method($name), "$class has a $name method" );
109 sub has_overridden_method {
113 ok( $class->meta->has_method($name), "$class has a $name method" );
115 my $meth = $class->meta->get_method($name);
116 isa_ok( $meth, 'Moose::Meta::Method::Overridden' );
122 ok( !$class->can('has'), "no Moose droppings in $class" );
128 ok( $class->meta->is_immutable, "$class has been made immutable" );
132 my $person = Person->new(
133 first_name => 'Bilbo',
134 last_name => 'Baggins',
137 is( $person->full_name, 'Bilbo Baggins',
138 'full_name() is correctly implemented' );
142 my $employee = Employee->new(
143 first_name => 'Amanda',
144 last_name => 'Palmer',
145 position => 'Singer',
148 is( $employee->full_name, 'Amanda Palmer (Singer)',
149 'full_name() is properly overriden in Employee' );