ff63f2ca30807f7ec7ddfafc0b9e2e14b4ebc65c
[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 no_plan => 1;
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         }
26     );
27 }
28
29 my $page = MyHomePage->new();
30 isa_ok($page, 'MyHomePage');
31
32 can_ok($page, $_) for qw[
33     dec_counter 
34     inc_counter
35     reset_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 # check the meta ..
53
54 my $counter = $page->meta->get_attribute('counter');
55 isa_ok($counter, 'MooseX::AttributeHelpers::Counter');
56
57 is($counter->helper_type, 'Num', '... got the expected helper type');
58
59 is($counter->type_constraint->name, 'Int', '... got the expected type constraint');
60
61 is_deeply($counter->provides, { 
62     inc   => 'inc_counter',
63     dec   => 'dec_counter',
64     reset => 'reset_counter',        
65 }, '... got the right provides methods');
66