do all the renaming that was discussed
[gitmo/Moose.git] / t / 070_attribute_helpers / 204_trait_number.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 25;
7 use Test::Moose;
8
9 {
10     package Real;
11     use Moose;
12     use Moose::AttributeHelpers;
13
14     has 'integer' => (
15         traits  => ['Number'],
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] ],
31
32         },
33     );
34 }
35
36 my $real = Real->new;
37 isa_ok( $real, 'Real' );
38
39 can_ok( $real, $_ ) for qw[
40     set add sub mul div mod abs inc dec odd cut_in_half
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
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
91 ## test the meta
92
93 my $attr = $real->meta->get_attribute('integer');
94 does_ok( $attr, 'Moose::Meta::Attribute::Trait::Native::Number' );
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 );
113