6 use Test::More tests => 28;
12 use Moose::AttributeHelpers;
15 traits => ['Collection::Array'],
17 isa => 'ArrayRef[Observer]',
19 default => sub { [] },
21 'add_observer' => 'push',
22 'count_observers' => 'count',
28 foreach my $observer ( $self->observers() ) {
29 $observer->update($self);
46 use Moose::AttributeHelpers;
51 traits => ['Counter'],
61 after qw(inc_counter dec_counter) => sub {
78 my ( $self, $subject ) = @_;
79 like $subject->count, qr{^-?\d+$},
80 'Observed number ' . $subject->count;
86 my $count = Counter->new();
88 ok( $count->can('add_observer'), 'add_observer method added' );
90 ok( $count->can('count_observers'), 'count_observers method added' );
92 ok( $count->can('inc_counter'), 'inc_counter method added' );
94 ok( $count->can('dec_counter'), 'dec_counter method added' );
96 $count->add_observer( Display->new() );
98 is( $count->count_observers, 1, 'Only one observer' );
100 is( $count->count, 0, 'Default to zero' );
104 is( $count->count, 1, 'Increment to one ' );
106 $count->inc_counter for ( 1 .. 6 );
108 is( $count->count, 7, 'Increment up to seven' );
112 is( $count->count, 6, 'Decrement to 6' );
114 $count->dec_counter for ( 1 .. 5 );
116 is( $count->count, 1, 'Decrement to 1' );
118 $count->dec_counter for ( 1 .. 2 );
120 is( $count->count, -1, 'Negative numbers' );
124 is( $count->count, 0, 'Back to zero' );