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 ) );
56 run_tests( build_class( trigger => sub { } ) );
58 # Will force the inlining code to check the entire hashref when it is modified.
59 subtype 'MyInt', as 'Int', where { 1 };
61 run_tests( build_class( isa => 'MyInt' ) );
63 coerce 'MyInt', from 'Int', via { $_ };
65 run_tests( build_class( isa => 'MyInt', coerce => 1 ) );
69 my ( $class, $handles ) = @_;
71 can_ok( $class, $_ ) for sort keys %{$handles};
74 my $obj = $class->new;
76 is( $obj->integer, 5, 'Default to five' );
80 is( $obj->integer, 15, 'Add ten for fithteen' );
82 throws_ok { $obj->add( 10, 2 ) }
83 qr/Cannot call add with more than 1 argument/,
84 'add throws an error when 2 arguments are passed';
88 is( $obj->integer, 12, 'Subtract three for 12' );
90 throws_ok { $obj->sub( 10, 2 ) }
91 qr/Cannot call sub with more than 1 argument/,
92 'sub throws an error when 2 arguments are passed';
96 is( $obj->integer, 10, 'Set to ten' );
98 throws_ok { $obj->set( 10, 2 ) }
99 qr/Cannot call set with more than 1 argument/,
100 'set throws an error when 2 arguments are passed';
104 is( $obj->integer, 5, 'divide by 2' );
106 throws_ok { $obj->div( 10, 2 ) }
107 qr/Cannot call div with more than 1 argument/,
108 'div throws an error when 2 arguments are passed';
112 is( $obj->integer, 10, 'multiplied by 2' );
114 throws_ok { $obj->mul( 10, 2 ) }
115 qr/Cannot call mul with more than 1 argument/,
116 'mul throws an error when 2 arguments are passed';
120 is( $obj->integer, 0, 'Mod by 2' );
122 throws_ok { $obj->mod( 10, 2 ) }
123 qr/Cannot call mod with more than 1 argument/,
124 'mod throws an error when 2 arguments are passed';
130 is( $obj->integer, 2, 'Mod by 5' );
136 throws_ok { $obj->abs(10) }
137 qr/Cannot call abs with any arguments/,
138 'abs throws an error when an argument is passed';
140 is( $obj->integer, 1, 'abs 1' );
146 is( $obj->integer, 13, 'inc 12' );
150 is( $obj->integer, 12, 'dec 13' );
152 if ( $class->meta->get_attribute('integer')->is_lazy ) {
153 my $obj = $class->new;
157 is( $obj->integer, 7, 'add with lazy default' );
159 $obj->_clear_integer;
163 is( $obj->integer, 1, 'mod with lazy default' );