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