16 isa => 'ArrayRef[Observer]',
18 default => sub { [] },
20 'add_observer' => 'push',
21 'count_observers' => 'count',
27 foreach my $observer ( $self->observers() ) {
28 $observer->update($self);
49 traits => ['Counter'],
59 after qw(inc_counter dec_counter) => sub {
76 my ( $self, $subject ) = @_;
77 like $subject->count, qr{^-?\d+$},
78 'Observed number ' . $subject->count;
84 my $count = Counter->new();
86 ok( $count->can('add_observer'), 'add_observer method added' );
88 ok( $count->can('count_observers'), 'count_observers method added' );
90 ok( $count->can('inc_counter'), 'inc_counter method added' );
92 ok( $count->can('dec_counter'), 'dec_counter method added' );
94 $count->add_observer( Display->new() );
96 is( $count->count_observers, 1, 'Only one observer' );
98 is( $count->count, 0, 'Default to zero' );
102 is( $count->count, 1, 'Increment to one ' );
104 $count->inc_counter for ( 1 .. 6 );
106 is( $count->count, 7, 'Increment up to seven' );
110 is( $count->count, 6, 'Decrement to 6' );
112 $count->dec_counter for ( 1 .. 5 );
114 is( $count->count, 1, 'Decrement to 1' );
116 $count->dec_counter for ( 1 .. 2 );
118 is( $count->count, -1, 'Negative numbers' );
122 is( $count->count, 0, 'Back to zero' );