7 use Moose::Util::TypeConstraints;
18 cut_in_half => [ div => 2 ],
32 my $class = Moose::Meta::Class->create(
34 superclasses => ['Moose::Object'],
37 $class->add_attribute(
44 clearer => '_clear_integer',
49 return ( $class->name, \%handles );
54 run_tests(build_class);
55 run_tests( build_class( lazy => 1 ) );
57 # Will force the inlining code to check the entire hashref when it is modified.
58 subtype 'MyInt', as 'Int', where { 1 };
60 run_tests( build_class( isa => 'MyInt' ) );
62 coerce 'MyInt', from 'Int', via { $_ };
64 run_tests( build_class( isa => 'MyInt', coerce => 1 ) );
68 my ( $class, $handles ) = @_;
70 can_ok( $class, $_ ) for sort keys %{$handles};
73 my $obj = $class->new;
75 is( $obj->integer, 5, 'Default to five' );
79 is( $obj->integer, 15, 'Add ten for fithteen' );
81 throws_ok { $obj->add( 10, 2 ) }
82 qr/Cannot call add with more than 1 argument/,
83 'add throws an error when 2 arguments are passed';
87 is( $obj->integer, 12, 'Subtract three for 12' );
89 throws_ok { $obj->sub( 10, 2 ) }
90 qr/Cannot call sub with more than 1 argument/,
91 'sub throws an error when 2 arguments are passed';
95 is( $obj->integer, 10, 'Set to ten' );
97 throws_ok { $obj->set( 10, 2 ) }
98 qr/Cannot call set with more than 1 argument/,
99 'set throws an error when 2 arguments are passed';
103 is( $obj->integer, 5, 'divide by 2' );
105 throws_ok { $obj->div( 10, 2 ) }
106 qr/Cannot call div with more than 1 argument/,
107 'div throws an error when 2 arguments are passed';
111 is( $obj->integer, 10, 'multiplied by 2' );
113 throws_ok { $obj->mul( 10, 2 ) }
114 qr/Cannot call mul with more than 1 argument/,
115 'mul throws an error when 2 arguments are passed';
119 is( $obj->integer, 0, 'Mod by 2' );
121 throws_ok { $obj->mod( 10, 2 ) }
122 qr/Cannot call mod with more than 1 argument/,
123 'mod throws an error when 2 arguments are passed';
129 is( $obj->integer, 2, 'Mod by 5' );
135 throws_ok { $obj->abs(10) }
136 qr/Cannot call abs with any arguments/,
137 'abs throws an error when an argument is passed';
139 is( $obj->integer, 1, 'abs 1' );
145 is( $obj->integer, 13, 'inc 12' );
149 is( $obj->integer, 12, 'dec 13' );
151 if ( $class->meta->get_attribute('integer')->is_lazy ) {
152 my $obj = $class->new;
156 is( $obj->integer, 7, 'add with lazy default' );
158 $obj->_clear_integer;
162 is( $obj->integer, 1, 'mod with lazy default' );