summaryrefslogtreecommitdiff
path: root/t/moose-tags-ranges.t
blob: bb8fb0651e55505379d8a229a082131624ac017b (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!perl 
use strict;
use warnings;
 
package Foo;{ 
use Moose;
use Data::MultiValued::AttributeTrait::TagsAndRanges;
 
has stuff => (
    is => 'rw',
    isa => 'Int',
    traits => ['MultiValued::TagsAndRanges'],
    default => 3,
    predicate => 'has_stuff',
    clearer => 'clear_stuff',
);
 
has other => (
    is => 'rw',
    isa => 'Str',
    traits => ['MultiValued::TagsAndRanges'],
    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_tags_and_ranges($obj)],
        [[undef, [[undef,undef]]]],
        'stuff all_ranges');
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_tags_and_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_tags_and_ranges($obj)],
        [[undef, [[undef,undef]]]],
        'stuff all_ranges');
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_tags_and_ranges($obj)],
        [[undef, [[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_tags($obj)],
        [undef],
        'stuff all_tags');
 
    cmp_deeply(
        [$obj->meta->get_attribute('stuff')->all_tags_and_ranges($obj)],
        [[undef, [[undef,10],[10,20],[20,undef]]]],
        'stuff all_tags_and_ranges');
 
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_tags_and_ranges($obj)],
        [[undef, [[10,20]]]],
        'other all_tags_and_ranges');
};
 
subtest 'with tags and ranges' => sub {
    my $obj = Foo->new();
 
    my $opts = {from=>10,to=>20,at=>15,tag=>'x'};
 
    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_tags($obj)],
        bag(undef,'x'),
        'stuff all_tags');
 
    cmp_deeply(
        [$obj->meta->get_attribute('stuff')->all_tags_and_ranges($obj)],
        bag(
            ['x', [[10,20]]],
            [undef,[[undef,undef]]],
        ),
        'stuff all_tags_and_ranges');
 
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_tags_and_ranges($obj)],
        [['x', [[10,20]]]],
        'other all_tags_and_ranges');
};
 
done_testing();