9 use Moose::Util::TypeConstraints;
10 use NoInlineAttribute;
18 inc_counter_2 => [ inc => 2 ],
20 dec_counter_2 => [ dec => 2 ],
21 reset_counter => 'reset',
23 set_counter_42 => [ set => 42 ],
31 my $class = Moose::Meta::Class->create(
33 superclasses => ['Moose::Object'],
36 my @traits = 'Counter';
37 push @traits, 'NoInlineAttribute'
38 if delete $attr{no_inline};
40 $class->add_attribute(
47 clearer => '_clear_counter',
52 return ( $class->name, \%handles );
57 run_tests(build_class);
58 run_tests( build_class( lazy => 1 ) );
59 run_tests( build_class( trigger => sub { } ) );
60 run_tests( build_class( no_inline => 1 ) );
62 # Will force the inlining code to check the entire hashref when it is modified.
63 subtype 'MyInt', as 'Int', where { 1 };
65 run_tests( build_class( isa => 'MyInt' ) );
67 coerce 'MyInt', from 'Int', via { $_ };
69 run_tests( build_class( isa => 'MyInt', coerce => 1 ) );
73 my ( $class, $handles ) = @_;
75 can_ok( $class, $_ ) for sort keys %{$handles};
78 my $obj = $class->new();
80 is( $obj->counter, 0, '... got the default value' );
82 is( $obj->inc_counter, 1, 'inc returns new value' );
83 is( $obj->counter, 1, '... got the incremented value' );
85 is( $obj->inc_counter, 2, 'inc returns new value' );
86 is( $obj->counter, 2, '... got the incremented value (again)' );
88 throws_ok { $obj->inc_counter( 1, 2 ) }
89 qr/Cannot call inc with more than 1 argument/,
90 'inc throws an error when two arguments are passed';
92 is( $obj->dec_counter, 1, 'dec returns new value' );
93 is( $obj->counter, 1, '... got the decremented value' );
95 throws_ok { $obj->dec_counter( 1, 2 ) }
96 qr/Cannot call dec with more than 1 argument/,
97 'dec throws an error when two arguments are passed';
99 is( $obj->reset_counter, 0, 'reset returns new value' );
100 is( $obj->counter, 0, '... got the original value' );
102 throws_ok { $obj->reset_counter(2) }
103 qr/Cannot call reset with any arguments/,
104 'reset throws an error when an argument is passed';
106 is( $obj->set_counter(5), 5, 'set returns new value' );
107 is( $obj->counter, 5, '... set the value' );
109 throws_ok { $obj->set_counter( 1, 2 ) }
110 qr/Cannot call set with more than 1 argument/,
111 'set throws an error when two arguments are passed';
113 $obj->inc_counter(2);
114 is( $obj->counter, 7, '... increment by arg' );
116 $obj->dec_counter(5);
117 is( $obj->counter, 2, '... decrement by arg' );
120 is( $obj->counter, 4, '... curried increment' );
123 is( $obj->counter, 2, '... curried deccrement' );
125 $obj->set_counter_42;
126 is( $obj->counter, 42, '... curried set' );
128 if ( $class->meta->get_attribute('counter')->is_lazy ) {
129 my $obj = $class->new;
132 is( $obj->counter, 1, 'inc increments - with lazy default' );
134 $obj->_clear_counter;
137 is( $obj->counter, -1, 'dec decrements - with lazy default' );