13 inc_counter_2 => [ inc => 2 ],
15 dec_counter_2 => [ dec => 2 ],
16 reset_counter => 'reset',
18 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};
58 my $obj = $class->new();
60 is( $obj->counter, 0, '... got the default value' );
63 is( $obj->counter, 1, '... got the incremented value' );
66 is( $obj->counter, 2, '... got the incremented value (again)' );
68 throws_ok { $obj->inc_counter( 1, 2 ) }
69 qr/Cannot call inc with more than 1 argument/,
70 'inc throws an error when two arguments are passed';
73 is( $obj->counter, 1, '... got the decremented value' );
75 throws_ok { $obj->dec_counter( 1, 2 ) }
76 qr/Cannot call dec with more than 1 argument/,
77 'dec throws an error when two arguments are passed';
80 is( $obj->counter, 0, '... got the original value' );
82 throws_ok { $obj->reset_counter(2) }
83 qr/Cannot call reset with any arguments/,
84 'reset throws an error when an argument is passed';
87 is( $obj->counter, 5, '... set the value' );
89 throws_ok { $obj->set_counter( 1, 2 ) }
90 qr/Cannot call set with more than 1 argument/,
91 'set throws an error when two arguments are passed';
94 is( $obj->counter, 7, '... increment by arg' );
97 is( $obj->counter, 2, '... decrement by arg' );
100 is( $obj->counter, 4, '... curried increment' );
103 is( $obj->counter, 2, '... curried deccrement' );
105 $obj->set_counter_42;
106 is( $obj->counter, 42, '... curried set' );
108 if ( $class->meta->get_attribute('counter')->is_lazy ) {
109 my $obj = $class->new;
112 is( $obj->counter, 1, 'inc increments - with lazy default' );
114 $obj->_clear_counter;
117 is( $obj->counter, -1, 'dec decrements - with lazy default' );