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