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