6 use Test::More tests => 29;
9 use_ok('Moose::AttributeHelpers');
15 use Moose::AttributeHelpers;
18 metaclass => 'Collection::Array',
20 isa => 'ArrayRef[Observer]',
22 default => sub { [] },
23 provides => { 'push' => 'add_observer', count => 'count_observers' }
28 foreach my $observer ( $self->observers() ) {
29 $observer->update($self);
33 ###############################################################################
41 ###############################################################################
46 use Moose::AttributeHelpers;
51 metaclass => 'Counter',
61 after 'inc_counter','dec_counter' => sub {
66 ###############################################################################
77 my ( $self, $subject ) = @_;
78 like $subject->count, qr{^-?\d+$}, 'Observed number ' . $subject->count;
81 ###############################################################################
85 my $count = Counter->new();
87 ok($count->can('add_observer'), 'add_observer method added');
89 ok($count->can('count_observers'), 'count_observers method added');
91 ok($count->can('inc_counter'), 'inc_counter method added');
93 ok($count->can('dec_counter'), 'dec_counter method added');
95 $count->add_observer( Display->new() );
97 is($count->count_observers, 1, 'Only one observer');
99 is($count->count, 0, 'Default to zero');
103 is($count->count, 1, 'Increment to one ');
105 $count->inc_counter for (1 .. 6);
107 is($count->count, 7, 'Increment up to seven');
111 is($count->count, 6, 'Decrement to 6');
113 $count->dec_counter for (1 .. 5);
115 is($count->count, 1, 'Decrement to 1');
117 $count->dec_counter for (1 .. 2);
119 is($count->count, -1, 'Negative numbers');
123 is($count->count, 0, 'Back to zero');