14 inc_counter_2 => [ inc => 2 ],
16 dec_counter_2 => [ dec => 2 ],
17 reset_counter => 'reset',
19 set_counter_42 => [ set => 42 ],
27 my $class = Moose::Meta::Class->create(
29 superclasses => ['Moose::Object'],
32 $class->add_attribute(
34 traits => ['Counter'],
39 clearer => '_clear_counter',
44 return ( $class->name, \%handles );
49 run_tests(build_class);
50 run_tests( build_class( lazy => 1 ) );
54 my ( $class, $handles ) = @_;
56 can_ok( $class, $_ ) for sort keys %{$handles};
59 my $obj = $class->new();
61 is( $obj->counter, 0, '... got the default value' );
64 is( $obj->counter, 1, '... got the incremented value' );
67 is( $obj->counter, 2, '... got the incremented value (again)' );
69 throws_ok { $obj->inc_counter( 1, 2 ) }
70 qr/Cannot call inc with more than 1 argument/,
71 'inc throws an error when two arguments are passed';
74 is( $obj->counter, 1, '... got the decremented value' );
76 throws_ok { $obj->dec_counter( 1, 2 ) }
77 qr/Cannot call dec with more than 1 argument/,
78 'dec throws an error when two arguments are passed';
81 is( $obj->counter, 0, '... got the original value' );
83 throws_ok { $obj->reset_counter(2) }
84 qr/Cannot call reset with any arguments/,
85 'reset throws an error when an argument is passed';
88 is( $obj->counter, 5, '... set the value' );
90 throws_ok { $obj->set_counter( 1, 2 ) }
91 qr/Cannot call set with more than 1 argument/,
92 'set throws an error when two arguments are passed';
95 is( $obj->counter, 7, '... increment by arg' );
98 is( $obj->counter, 2, '... decrement by arg' );
101 is( $obj->counter, 4, '... curried increment' );
104 is( $obj->counter, 2, '... curried deccrement' );
106 $obj->set_counter_42;
107 is( $obj->counter, 42, '... curried set' );
109 if ( $class->meta->get_attribute('counter')->is_lazy ) {
110 my $obj = $class->new;
113 is( $obj->counter, 1, 'inc increments - with lazy default' );
115 $obj->_clear_counter;
118 is( $obj->counter, -1, 'dec decrements - with lazy default' );