64cc641abc56abaca1628f867820e851b3d229a7
[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         }
25     );
26 }
27
28 my $page = MyHomePage->new();
29 isa_ok($page, 'MyHomePage');
30
31 can_ok($page, 'inc_counter');
32 can_ok($page, 'dec_counter');
33
34 is($page->counter, 0, '... got the default value');
35
36 $page->inc_counter; 
37 is($page->counter, 1, '... got the incremented value');
38
39 $page->inc_counter; 
40 is($page->counter, 2, '... got the incremented value (again)');
41
42 $page->dec_counter; 
43 is($page->counter, 1, '... got the decremented value');
44
45 # check the meta ..
46
47 my $counter = $page->meta->get_attribute('counter');
48 isa_ok($counter, 'MooseX::AttributeHelpers::Counter');
49
50 is($counter->helper_type, 'Num', '... got the expected helper type');
51
52 is($counter->type_constraint->name, 'Int', '... got the expected type constraint');
53
54 is_deeply($counter->provides, { 
55     inc => 'inc_counter',
56     dec => 'dec_counter',    
57 }, '... got the right provides methods');
58