6 use Test::More tests => 29;
9 use_ok('Moose::AttributeHelpers');
15 use Moose::AttributeHelpers;
18 traits => [ 'Collection::Array' ],
20 isa => 'ArrayRef[Observer]',
22 default => sub { [] },
24 'add_observer' => 'push',
25 'count_observers' => 'count',
31 foreach my $observer ( $self->observers() ) {
32 $observer->update($self);
36 ###############################################################################
44 ###############################################################################
49 use Moose::AttributeHelpers;
54 traits => [ 'Counter' ],
64 after qw(inc_counter dec_counter) => sub {
69 ###############################################################################
80 my ( $self, $subject ) = @_;
81 like $subject->count, qr{^-?\d+$}, 'Observed number ' . $subject->count;
84 ###############################################################################
88 my $count = Counter->new();
90 ok($count->can('add_observer'), 'add_observer method added');
92 ok($count->can('count_observers'), 'count_observers method added');
94 ok($count->can('inc_counter'), 'inc_counter method added');
96 ok($count->can('dec_counter'), 'dec_counter method added');
98 $count->add_observer( Display->new() );
100 is($count->count_observers, 1, 'Only one observer');
102 is($count->count, 0, 'Default to zero');
106 is($count->count, 1, 'Increment to one ');
108 $count->inc_counter for (1 .. 6);
110 is($count->count, 7, 'Increment up to seven');
114 is($count->count, 6, 'Decrement to 6');
116 $count->dec_counter for (1 .. 5);
118 is($count->count, 1, 'Decrement to 1');
120 $count->dec_counter for (1 .. 2);
122 is($count->count, -1, 'Negative numbers');
126 is($count->count, 0, 'Back to zero');