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