File: 204_trait_number.t

package info (click to toggle)
libmoose-perl 1.09-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 3,004 kB
  • ctags: 1,472
  • sloc: perl: 25,387; makefile: 2
file content (113 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Test::Moose;

{
    package Real;
    use Moose;

    has 'integer' => (
        traits  => ['Number'],
        is      => 'ro',
        isa     => 'Int',
        default => 5,
        handles => {
            set         => 'set',
            add         => 'add',
            sub         => 'sub',
            mul         => 'mul',
            div         => 'div',
            mod         => 'mod',
            abs         => 'abs',
            inc         => [ add => 1 ],
            dec         => [ sub => 1 ],
            odd         => [ mod => 2 ],
            cut_in_half => [ div => 2 ],

        },
    );
}

my $real = Real->new;
isa_ok( $real, 'Real' );

can_ok( $real, $_ ) for qw[
    set add sub mul div mod abs inc dec odd cut_in_half
];

is $real->integer, 5, 'Default to five';

$real->add(10);

is $real->integer, 15, 'Add ten for fithteen';

$real->sub(3);

is $real->integer, 12, 'Subtract three for 12';

$real->set(10);

is $real->integer, 10, 'Set to ten';

$real->div(2);

is $real->integer, 5, 'divide by 2';

$real->mul(2);

is $real->integer, 10, 'multiplied by 2';

$real->mod(2);

is $real->integer, 0, 'Mod by 2';

$real->set(7);

$real->mod(5);

is $real->integer, 2, 'Mod by 5';

$real->set(-1);

$real->abs;

is $real->integer, 1, 'abs 1';

$real->set(12);

$real->inc;

is $real->integer, 13, 'inc 12';

$real->dec;

is $real->integer, 12, 'dec 13';

## test the meta

my $attr = $real->meta->get_attribute('integer');
does_ok( $attr, 'Moose::Meta::Attribute::Native::Trait::Number' );

is_deeply(
    $attr->handles,
    {
        set         => 'set',
        add         => 'add',
        sub         => 'sub',
        mul         => 'mul',
        div         => 'div',
        mod         => 'mod',
        abs         => 'abs',
        inc         => [ add => 1 ],
        dec         => [ sub => 1 ],
        odd         => [ mod => 2 ],
        cut_in_half => [ div => 2 ],
    },
    '... got the right handles mapping'
);

done_testing;