summaryrefslogtreecommitdiff
path: root/t/moose-ranges.t
blob: 35ff83d342a0b26540f046ef396baeb01cc8ad10 (plain)
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
#!perl 
use strict;
use warnings;
 
package Foo;{ 
use Moose;
use Data::MultiValued::AttributeTrait::Ranges;
 
has stuff => (
    is => 'rw',
    isa => 'Int',
    traits => ['MultiValued::Ranges'],
    default => 3,
    predicate => 'has_stuff',
    clearer => 'clear_stuff',
);
 
has other => (
    is => 'rw',
    isa => 'Str',
    traits => ['MultiValued::Ranges'],
    predicate => 'has_other',
    clearer => 'clear_other',
);
}
package main; 
use Test::Most 'die';
use Data::Printer;
 
subtest 'default' => sub {
    my $obj = Foo->new();
 
    ok(!$obj->has_other,'not has other');
    ok($obj->has_stuff,'has stuff');
 
    is($obj->stuff,3,'default');
 
    cmp_deeply(
        [$obj->meta->get_attribute('stuff')->all_ranges($obj)],
        [[undef,undef]],
        'stuff all_ranges');
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_ranges($obj)],
        [],
        'other all_ranges');
};
 
subtest 'constructor param' => sub {
    my $obj = Foo->new({stuff=>12,other=>'bar'});
 
    ok($obj->has_other,'has other');
    ok($obj->has_stuff,'has stuff');
 
    is($obj->stuff,12,'param');
    is($obj->other,'bar','param');
 
    cmp_deeply(
        [$obj->meta->get_attribute('stuff')->all_ranges($obj)],
        [[undef,undef]],
        'stuff all_ranges');
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_ranges($obj)],
        [[undef,undef]],
        'other all_ranges');
};
 
subtest 'with ranges' => sub {
    my $obj = Foo->new();
 
    my $opts = {from=>10,to=>20,at=>15};
 
    ok($obj->has_stuff,'has stuff');
    ok($obj->has_stuff_multi($opts),'has stuff ranged (forever)');
    ok(!$obj->has_other,'not has other');
    ok(!$obj->has_other_multi($opts),'not has other ranged');
 
    $obj->stuff_multi($opts,7);
    $obj->other_multi($opts,'foo');
 
    is($obj->stuff,3,'default');
    is($obj->stuff_multi($opts),7,'stuff ranged');
    is($obj->other_multi($opts),'foo','other ranged');
 
    cmp_deeply(
        [$obj->meta->get_attribute('stuff')->all_ranges($obj)],
        [[undef,10],[10,20],[20,undef]],
        'stuff all_ranges');
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_ranges($obj)],
        [[10,20]],
        'other all_ranges');
};
 
done_testing();