Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
59de9de4 |
6 | use Test::More tests => 18; |
e3c07b19 |
7 | use Test::Moose 'does_ok'; |
8 | |
9 | BEGIN { |
59de9de4 |
10 | use_ok('MooseX::AttributeHelpers'); |
e3c07b19 |
11 | } |
12 | |
13 | { |
14 | package MyHomePage; |
15 | use Moose; |
16 | |
17 | has 'counter' => ( |
59de9de4 |
18 | traits => [qw/MooseX::AttributeHelpers::Trait::Counter/], |
e3c07b19 |
19 | is => 'ro', |
20 | isa => 'Int', |
21 | default => sub { 0 }, |
0d103ac9 |
22 | handles => { |
23 | inc_counter => 'inc', |
24 | dec_counter => 'dec', |
25 | reset_counter => 'reset', |
26 | set_counter => 'set' |
e3c07b19 |
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 |
59de9de4 |
38 | set_counter |
e3c07b19 |
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 | |
59de9de4 |
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 | |
e3c07b19 |
64 | # check the meta .. |
65 | |
66 | my $counter = $page->meta->get_attribute('counter'); |
59de9de4 |
67 | does_ok($counter, 'MooseX::AttributeHelpers::Trait::Counter'); |
e3c07b19 |
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 | |
0d103ac9 |
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'); |
e3c07b19 |
79 | |