Commit | Line | Data |
85ec0d1f |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
88aaf2bd |
6 | use Test::More no_plan => 1; |
7 | |
85ec0d1f |
8 | BEGIN { |
9 | use_ok('MooseX::AttributeHelpers'); |
10 | } |
11 | |
85ec0d1f |
12 | package Subject; |
13 | |
14 | use Moose::Role; |
15 | use MooseX::AttributeHelpers; |
16 | |
17 | has observers => ( |
18 | metaclass => 'Collection::Array', |
19 | is => 'ro', |
20 | isa => 'ArrayRef', |
21 | auto_deref => 1, |
22 | default => sub { [] }, |
92d18d6e |
23 | provides => { 'push' => 'add_observer', count => 'count_observers' } |
85ec0d1f |
24 | ); |
25 | |
26 | sub notify { |
27 | my ($self) = @_; |
28 | foreach my $observer ( $self->observers() ) { |
29 | $observer->update($self); |
30 | } |
31 | } |
32 | |
33 | ############################################################################### |
34 | |
35 | package Observer; |
36 | |
37 | use Moose::Role; |
38 | |
39 | sub update { |
40 | die 'Forgot to implement' . "\n"; |
41 | } |
42 | |
43 | ############################################################################### |
44 | |
45 | package Counter; |
46 | |
47 | use Moose; |
48 | use MooseX::AttributeHelpers; |
49 | |
50 | with 'Subject'; |
51 | |
52 | has count => ( |
53 | metaclass => 'Counter', |
54 | is => 'ro', |
55 | isa => 'Int', |
56 | default => 0, |
57 | provides => { |
58 | inc => 'inc_counter', |
59 | dec => 'dec_counter', |
60 | } |
61 | ); |
62 | |
63 | after 'inc_counter','dec_counter' => sub { |
64 | my ($self) = @_; |
65 | $self->notify(); |
66 | }; |
67 | |
68 | ############################################################################### |
69 | |
70 | package Display; |
71 | |
92d18d6e |
72 | use Test::More; |
73 | |
85ec0d1f |
74 | use Moose; |
75 | |
76 | with 'Observer'; |
77 | |
78 | sub update { |
79 | my ( $self, $subject ) = @_; |
92d18d6e |
80 | like $subject->count, qr{^-?\d+$}, 'Observed number ' . $subject->count; |
85ec0d1f |
81 | } |
82 | |
83 | ############################################################################### |
84 | |
85 | package main; |
86 | |
87 | my $count = Counter->new(); |
92d18d6e |
88 | |
89 | ok($count->can('add_observer'), 'add_observer method added'); |
90 | |
91 | ok($count->can('count_observers'), 'count_observers method added'); |
92 | |
93 | ok($count->can('inc_counter'), 'inc_counter method added'); |
94 | |
95 | ok($count->can('dec_counter'), 'dec_counter method added'); |
96 | |
85ec0d1f |
97 | $count->add_observer( Display->new() ); |
98 | |
92d18d6e |
99 | is($count->count_observers, 1, 'Only one observer'); |
100 | |
101 | is($count->count, 0, 'Default to zero'); |
102 | |
103 | $count->inc_counter; |
104 | |
105 | is($count->count, 1, 'Increment to one '); |
106 | |
107 | $count->inc_counter for (1 .. 6); |
108 | |
109 | is($count->count, 7, 'Increment up to seven'); |
110 | |
111 | $count->dec_counter; |
112 | |
113 | is($count->count, 6, 'Decrement to 6'); |
114 | |
115 | $count->dec_counter for (1 .. 5); |
116 | |
117 | is($count->count, 1, 'Decrement to 1'); |
118 | |
119 | $count->dec_counter for (1 .. 2); |
120 | |
121 | is($count->count, -1, 'Negative numbers'); |
122 | |
123 | $count->inc_counter; |
85ec0d1f |
124 | |
92d18d6e |
125 | is($count->count, 0, 'Back to zero'); |