48736f823169c15bd8933526168570a694c7870a
[gitmo/Moose.git] / t / 070_native_traits / 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
13     has 'integer' => (
14         traits  => ['Number'],
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',
26             inc         => [ add => 1 ],
27             dec         => [ sub => 1 ],
28             odd         => [ mod => 2 ],
29             cut_in_half => [ div => 2 ],
30
31         },
32     );
33 }
34
35 my $real = Real->new;
36 isa_ok( $real, 'Real' );
37
38 can_ok( $real, $_ ) for qw[
39     set add sub mul div mod abs inc dec odd cut_in_half
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
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
90 ## test the meta
91
92 my $attr = $real->meta->get_attribute('integer');
93 does_ok( $attr, 'Moose::Meta::Attribute::Native::Trait::Number' );
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',
105         inc         => [ add => 1 ],
106         dec         => [ sub => 1 ],
107         odd         => [ mod => 2 ],
108         cut_in_half => [ div => 2 ],
109     },
110     '... got the right handles mapping'
111 );
112