summaryrefslogtreecommitdiff
path: root/t/moose-tagged.t
blob: 8e7dd97916b5052533e17e9f98df9656a0887471 (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
#!perl 
use strict;
use warnings;
 
package Foo;{ 
use Moose;
use Data::MultiValued::AttributeTrait::Tags;
 
has stuff => (
    is => 'rw',
    isa => 'Int',
    traits => ['MultiValued::Tags'],
    default => 3,
    predicate => 'has_stuff',
    clearer => 'clear_stuff',
    multi_accessor => 'stuff_tagged',
    multi_predicate => 'has_stuff_tagged',
);
 
has other => (
    is => 'rw',
    isa => 'Str',
    traits => ['MultiValued::Tags'],
    predicate => 'has_other',
    clearer => 'clear_other',
);
 
#__PACKAGE__->meta->make_immutable; 
 
}
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($obj)],
        [undef],
        'stuff all_tags');
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_tags($obj)],
        [],
        'other all_tags');
};
 
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($obj)],
        [undef],
        'stuff all_tags');
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_tags($obj)],
        [undef],
        'other all_tags');
};
 
subtest 'with tags' => sub {
    my $obj = Foo->new();
 
    my $opts = {tag=>'one'};
 
    ok($obj->has_stuff,'has stuff');
    ok(!$obj->has_stuff_tagged($opts),'not has stuff tagged');
    ok(!$obj->has_other,'not has other');
    ok(!$obj->has_other_multi($opts),'not has other tagged');
 
    $obj->stuff_tagged($opts,7);
    $obj->other_multi($opts,'foo');
 
    is($obj->stuff,3,'default');
    is($obj->stuff_tagged($opts),7,'stuff tagged');
    is($obj->other_multi($opts),'foo','other tagged');
 
    cmp_deeply(
        [$obj->meta->get_attribute('stuff')->all_tags($obj)],
        bag(undef,'one'),
       'stuff all_tags');
    cmp_deeply(
        [$obj->meta->get_attribute('other')->all_tags($obj)],
        bag('one'),
       'other all_tags');
 
    my @tags = $obj->meta->get_attribute('stuff')->all_tags($obj);
    my $pred=$obj->meta->get_attribute('stuff')->multi_predicate;
    for my $tag (@tags) {
        ok($obj->$pred({tag=>$tag}),"stuff has tag @{[ $tag || 'undef' ]}");
    }
 
};
 
done_testing();