Commit | Line | Data |
e3c07b19 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a5209c26 |
6 | use Test::More tests => 25; |
e3c07b19 |
7 | use Test::Moose; |
8 | |
e3c07b19 |
9 | { |
10 | package Real; |
11 | use Moose; |
12 | |
13 | has 'integer' => ( |
0f93f5d2 |
14 | traits => ['Number'], |
d50fc84a |
15 | is => 'ro', |
16 | isa => 'Int', |
17 | default => 5, |
18 | handles => { |
19 | set => 'set', |
20 | add => 'add', |
21 | sub => 'sub', |
22 | mul => 'mul', |
23 | div => 'div', |
24 | mod => 'mod', |
25 | abs => 'abs', |
3c573ca4 |
26 | inc => [ add => 1 ], |
27 | dec => [ sub => 1 ], |
28 | odd => [ mod => 2 ], |
29 | cut_in_half => [ div => 2 ], |
0d103ac9 |
30 | |
59de9de4 |
31 | }, |
e3c07b19 |
32 | ); |
33 | } |
34 | |
35 | my $real = Real->new; |
d50fc84a |
36 | isa_ok( $real, 'Real' ); |
e3c07b19 |
37 | |
d50fc84a |
38 | can_ok( $real, $_ ) for qw[ |
59de9de4 |
39 | set add sub mul div mod abs inc dec odd cut_in_half |
e3c07b19 |
40 | ]; |
41 | |
42 | is $real->integer, 5, 'Default to five'; |
43 | |
44 | $real->add(10); |
45 | |
46 | is $real->integer, 15, 'Add ten for fithteen'; |
47 | |
48 | $real->sub(3); |
49 | |
50 | is $real->integer, 12, 'Subtract three for 12'; |
51 | |
52 | $real->set(10); |
53 | |
54 | is $real->integer, 10, 'Set to ten'; |
55 | |
56 | $real->div(2); |
57 | |
58 | is $real->integer, 5, 'divide by 2'; |
59 | |
60 | $real->mul(2); |
61 | |
62 | is $real->integer, 10, 'multiplied by 2'; |
63 | |
64 | $real->mod(2); |
65 | |
66 | is $real->integer, 0, 'Mod by 2'; |
67 | |
68 | $real->set(7); |
69 | |
70 | $real->mod(5); |
71 | |
72 | is $real->integer, 2, 'Mod by 5'; |
73 | |
74 | $real->set(-1); |
75 | |
76 | $real->abs; |
77 | |
78 | is $real->integer, 1, 'abs 1'; |
79 | |
59de9de4 |
80 | $real->set(12); |
81 | |
82 | $real->inc; |
83 | |
84 | is $real->integer, 13, 'inc 12'; |
85 | |
86 | $real->dec; |
87 | |
88 | is $real->integer, 12, 'dec 13'; |
89 | |
e3c07b19 |
90 | ## test the meta |
91 | |
92 | my $attr = $real->meta->get_attribute('integer'); |
c466e58f |
93 | does_ok( $attr, 'Moose::Meta::Attribute::Native::Trait::Number' ); |
d50fc84a |
94 | |
95 | is_deeply( |
96 | $attr->handles, |
97 | { |
98 | set => 'set', |
99 | add => 'add', |
100 | sub => 'sub', |
101 | mul => 'mul', |
102 | div => 'div', |
103 | mod => 'mod', |
104 | abs => 'abs', |
3c573ca4 |
105 | inc => [ add => 1 ], |
106 | dec => [ sub => 1 ], |
107 | odd => [ mod => 2 ], |
108 | cut_in_half => [ div => 2 ], |
d50fc84a |
109 | }, |
110 | '... got the right handles mapping' |
111 | ); |
e3c07b19 |
112 | |