cdb064269cf20c5bfe024594b57a7086786f5001
[gitmo/Moose.git] / t / 070_attribute_helpers / 201_trait_counter.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 16;
7 use Test::Moose 'does_ok';
8
9 {
10     package MyHomePage;
11     use Moose;
12     use Moose::AttributeHelpers;
13
14     has 'counter' => (
15         traits  => ['Counter'],
16         is      => 'ro',
17         isa     => 'Int',
18         default => 0,
19         handles => {
20             inc_counter   => 'inc',
21             dec_counter   => 'dec',
22             reset_counter => 'reset',
23             set_counter   => 'set'
24         }
25     );
26 }
27
28 my $page = MyHomePage->new();
29 isa_ok( $page, 'MyHomePage' );
30
31 can_ok( $page, $_ ) for qw[
32     dec_counter
33     inc_counter
34     reset_counter
35     set_counter
36 ];
37
38 is( $page->counter, 0, '... got the default value' );
39
40 $page->inc_counter;
41 is( $page->counter, 1, '... got the incremented value' );
42
43 $page->inc_counter;
44 is( $page->counter, 2, '... got the incremented value (again)' );
45
46 $page->dec_counter;
47 is( $page->counter, 1, '... got the decremented value' );
48
49 $page->reset_counter;
50 is( $page->counter, 0, '... got the original value' );
51
52 $page->set_counter(5);
53 is( $page->counter, 5, '... set the value' );
54
55 $page->inc_counter(2);
56 is( $page->counter, 7, '... increment by arg' );
57
58 $page->dec_counter(5);
59 is( $page->counter, 2, '... decrement by arg' );
60
61 # check the meta ..
62
63 my $counter = $page->meta->get_attribute('counter');
64 does_ok( $counter, 'Moose::AttributeHelpers::Trait::Counter' );
65
66 is( $counter->type_constraint->name, 'Int',
67     '... got the expected type constraint' );
68
69 is_deeply(
70     $counter->handles,
71     {
72         inc_counter   => 'inc',
73         dec_counter   => 'dec',
74         reset_counter => 'reset',
75         set_counter   => 'set'
76     },
77     '... got the right handles methods'
78 );
79