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