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