Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a28e50e4 |
6 | use Test::More; |
e3c07b19 |
7 | use Test::Moose 'does_ok'; |
8 | |
e3c07b19 |
9 | { |
10 | package MyHomePage; |
11 | use Moose; |
12 | |
13 | has 'counter' => ( |
0f93f5d2 |
14 | traits => ['Counter'], |
d50fc84a |
15 | is => 'ro', |
16 | isa => 'Int', |
17 | default => 0, |
18 | handles => { |
0d103ac9 |
19 | inc_counter => 'inc', |
20 | dec_counter => 'dec', |
21 | reset_counter => 'reset', |
22 | set_counter => 'set' |
e3c07b19 |
23 | } |
24 | ); |
25 | } |
26 | |
27 | my $page = MyHomePage->new(); |
d50fc84a |
28 | isa_ok( $page, 'MyHomePage' ); |
e3c07b19 |
29 | |
d50fc84a |
30 | can_ok( $page, $_ ) for qw[ |
e3c07b19 |
31 | dec_counter |
32 | inc_counter |
33 | reset_counter |
59de9de4 |
34 | set_counter |
e3c07b19 |
35 | ]; |
36 | |
d50fc84a |
37 | is( $page->counter, 0, '... got the default value' ); |
e3c07b19 |
38 | |
39 | $page->inc_counter; |
d50fc84a |
40 | is( $page->counter, 1, '... got the incremented value' ); |
e3c07b19 |
41 | |
42 | $page->inc_counter; |
d50fc84a |
43 | is( $page->counter, 2, '... got the incremented value (again)' ); |
e3c07b19 |
44 | |
45 | $page->dec_counter; |
d50fc84a |
46 | is( $page->counter, 1, '... got the decremented value' ); |
e3c07b19 |
47 | |
48 | $page->reset_counter; |
d50fc84a |
49 | is( $page->counter, 0, '... got the original value' ); |
e3c07b19 |
50 | |
59de9de4 |
51 | $page->set_counter(5); |
d50fc84a |
52 | is( $page->counter, 5, '... set the value' ); |
59de9de4 |
53 | |
54 | $page->inc_counter(2); |
d50fc84a |
55 | is( $page->counter, 7, '... increment by arg' ); |
59de9de4 |
56 | |
57 | $page->dec_counter(5); |
d50fc84a |
58 | is( $page->counter, 2, '... decrement by arg' ); |
59de9de4 |
59 | |
e3c07b19 |
60 | # check the meta .. |
61 | |
62 | my $counter = $page->meta->get_attribute('counter'); |
c466e58f |
63 | does_ok( $counter, 'Moose::Meta::Attribute::Native::Trait::Counter' ); |
d50fc84a |
64 | |
65 | is( $counter->type_constraint->name, 'Int', |
66 | '... got the expected type constraint' ); |
67 | |
68 | is_deeply( |
69 | $counter->handles, |
70 | { |
71 | inc_counter => 'inc', |
72 | dec_counter => 'dec', |
73 | reset_counter => 'reset', |
74 | set_counter => 'set' |
75 | }, |
76 | '... got the right handles methods' |
77 | ); |
e3c07b19 |
78 | |
a28e50e4 |
79 | done_testing; |