aboutsummaryrefslogtreecommitdiff
path: root/t/tests/compare-bag.t
blob: 7c716396c9ba37b16c1f950602587049b3ee7ef9 (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
137
138
139
140
141
142
143
144
145
146
#!perl 
use strict;
use warnings;
use 5.020;
use Test2::Bundle::Extended -target => 'Test2::Compare::Bag';
 
isa_ok($CLASS'Test2::Compare::Base');
is($CLASS->name, '<BAG>'"got name");
 
subtest construction => sub {
    my $one = $CLASS->new();
    isa_ok($one$CLASS);
    is($one->items, [], "created items as an array");
};
 
subtest verify => sub {
    my $one = $CLASS->new;
 
    is($one->verify(exists => 0), 0, "did not get anything");
    is($one->verify(exists => 1, got => undef), 0, "undef is not an array");
    is($one->verify(exists => 1, got => 0), 0, "0 is not an array");
    is($one->verify(exists => 1, got => 1), 0, "1 is not an array");
    is($one->verify(exists => 1, got => 'string'), 0, "'string' is not an array");
    is($one->verify(exists => 1, got => {}), 0, "a hash is not an array");
    is($one->verify(exists => 1, got => []), 1, "an array is an array");
};
 
subtest add_item => sub {
    my $one = $CLASS->new();
 
    $one->add_item('a');
    $one->add_item(=> 'b');
    $one->add_item(=> 'd');
 
    ok(
        lives { $one->add_item(=> 'c') },
        "Indexes are ignored",
    );
 
    $one->add_item(=> 'x');
    $one->add_item('y');
 
    is(
        $one->items,
        'a''b''d''c''x''y' ],
        "Expected items",
    );
};
 
subtest deltas => sub {
    my $conv = Test2::Compare->can('strict_convert');
 
    my %params = (exists => 1, convert => $convseen => {});
 
    my $items = ['a''b'];
    my $one = $CLASS->new(items => $items);
 
    like(
        [$one->deltas(%paramsgot => ['a''b'])],
        [],
        "No delta, no diff"
    );
 
    like(
        [$one->deltas(%paramsgot => ['b''a'])],
        [],
        "No delta, no diff, order is ignored"
    );
 
    like(
        [$one->deltas(%paramsgot => ['a'])],
        [
            {
                dne => 'got',
                id  => [ARRAY => '*'],
                got => undef,,
                chk => {input => 'b'},
                children => [
                    {
                        dne => DNE,
                        id => [ARRAY => 0],
                        got => 'a',
                        chk => {input => 'b'},
                    },
                ],
            }
        ],
        "Got the delta for the missing value"
    );
 
    like(
        [$one->deltas(%paramsgot => ['a''a'])],
        [
            {
                dne => 'got',
                id  => [ARRAY => '*'],
                got => undef,
                chk => {input => 'b'},
                children => [
                    {
                        dne => DNE,
                        id => [ARRAY => 0],
                        got => 'a',
                        chk => {input => 'b'},
                    },
                    {
                        dne => DNE,
                        id => [ARRAY => 1],
                        got => 'a',
                        chk => {input => 'b'},
                    },
                ],
            }
        ],
        "Got the delta for the incorrect value"
    );
 
    like(
        [$one->deltas(%paramsgot => ['a''b''a''a'])],
        [],
        "No delta, not checking ending"
    );
 
    $one->set_ending(1);
    like(
        [$one->deltas(%paramsgot => ['a''b''a''x'])],
        array {
            item => {
                dne   => 'check',
                id    => [ARRAY => 2],
                got   => 'a',
                check => DNE,
            };
            item => {
                dne   => 'check',
                id    => [ARRAY => 3],
                got   => 'x',
                check => DNE,
            };
            end(),
        },
        "Got 2 deltas for extra items"
    );
};
 
done_testing;