update trait tests with basic test diffs
[gitmo/MooseX-AttributeHelpers.git] / t / 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('MooseX::AttributeHelpers');   
11 }
12
13 {
14     package MyHomePage;
15     use Moose;
16
17     has 'counter' => (
18         traits    => [qw/MooseX::AttributeHelpers::Trait::Counter/],
19         is        => 'ro',
20         isa       => 'Int',
21         default   => sub { 0 },
22         provides  => {
23             inc   => 'inc_counter',
24             dec   => 'dec_counter',
25             reset => 'reset_counter',
26             set   => 'set_counter'
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, 'MooseX::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->provides, { 
74     inc   => 'inc_counter',
75     dec   => 'dec_counter',
76     reset => 'reset_counter',
77     set   => 'set_counter'
78 }, '... got the right provides methods');
79