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