| 1 |
use Test2::Bundle::Extended -target => 'Test2::Compare::Bag'; |
| 2 |
|
| 3 |
isa_ok($CLASS, 'Test2::Compare::Base'); |
| 4 |
is($CLASS->name, '<BAG>', "got name"); |
| 5 |
|
| 6 |
subtest construction => sub { |
| 7 |
my $one = $CLASS->new(); |
| 8 |
isa_ok($one, $CLASS); |
| 9 |
is($one->items, [], "created items as an array"); |
| 10 |
}; |
| 11 |
|
| 12 |
subtest verify => sub { |
| 13 |
my $one = $CLASS->new; |
| 14 |
|
| 15 |
is($one->verify(exists => 0), 0, "did not get anything"); |
| 16 |
is($one->verify(exists => 1, got => undef), 0, "undef is not an array"); |
| 17 |
is($one->verify(exists => 1, got => 0), 0, "0 is not an array"); |
| 18 |
is($one->verify(exists => 1, got => 1), 0, "1 is not an array"); |
| 19 |
is($one->verify(exists => 1, got => 'string'), 0, "'string' is not an array"); |
| 20 |
is($one->verify(exists => 1, got => {}), 0, "a hash is not an array"); |
| 21 |
is($one->verify(exists => 1, got => []), 1, "an array is an array"); |
| 22 |
}; |
| 23 |
|
| 24 |
subtest add_item => sub { |
| 25 |
my $one = $CLASS->new(); |
| 26 |
|
| 27 |
$one->add_item('a'); |
| 28 |
$one->add_item(1 => 'b'); |
| 29 |
$one->add_item(3 => 'd'); |
| 30 |
|
| 31 |
ok( |
| 32 |
lives { $one->add_item(2 => 'c') }, |
| 33 |
"Indexes are ignored", |
| 34 |
); |
| 35 |
|
| 36 |
$one->add_item(8 => 'x'); |
| 37 |
$one->add_item('y'); |
| 38 |
|
| 39 |
is( |
| 40 |
$one->items, |
| 41 |
[ 'a', 'b', 'd', 'c', 'x', 'y' ], |
| 42 |
"Expected items", |
| 43 |
); |
| 44 |
}; |
| 45 |
|
| 46 |
subtest deltas => sub { |
| 47 |
my $conv = Test2::Compare->can('strict_convert'); |
| 48 |
|
| 49 |
my %params = (exists => 1, convert => $conv, seen => {}); |
| 50 |
|
| 51 |
my $items = ['a', 'b']; |
| 52 |
my $one = $CLASS->new(items => $items); |
| 53 |
|
| 54 |
like( |
| 55 |
[$one->deltas(%params, got => ['a', 'b'])], |
| 56 |
[], |
| 57 |
"No delta, no diff" |
| 58 |
); |
| 59 |
|
| 60 |
like( |
| 61 |
[$one->deltas(%params, got => ['b', 'a'])], |
| 62 |
[], |
| 63 |
"No delta, no diff, order is ignored" |
| 64 |
); |
| 65 |
|
| 66 |
like( |
| 67 |
[$one->deltas(%params, got => ['a'])], |
| 68 |
[ |
| 69 |
{ |
| 70 |
dne => 'got', |
| 71 |
id => [ARRAY => '*'], |
| 72 |
got => undef,, |
| 73 |
chk => {input => 'b'}, |
| 74 |
} |
| 75 |
], |
| 76 |
"Got the delta for the missing value" |
| 77 |
); |
| 78 |
|
| 79 |
like( |
| 80 |
[$one->deltas(%params, got => ['a', 'a'])], |
| 81 |
[ |
| 82 |
{ |
| 83 |
dne => 'got', |
| 84 |
id => [ARRAY => '*'], |
| 85 |
got => undef, |
| 86 |
chk => {input => 'b'}, |
| 87 |
} |
| 88 |
], |
| 89 |
"Got the delta for the incorrect value" |
| 90 |
); |
| 91 |
|
| 92 |
like( |
| 93 |
[$one->deltas(%params, got => ['a', 'b', 'a', 'a'])], |
| 94 |
[], |
| 95 |
"No delta, not checking ending" |
| 96 |
); |
| 97 |
|
| 98 |
$one->set_ending(1); |
| 99 |
like( |
| 100 |
[$one->deltas(%params, got => ['a', 'b', 'a', 'x'])], |
| 101 |
array { |
| 102 |
item 0 => { |
| 103 |
dne => 'check', |
| 104 |
id => [ARRAY => 2], |
| 105 |
got => 'a', |
| 106 |
check => DNE, |
| 107 |
}; |
| 108 |
item 1 => { |
| 109 |
dne => 'check', |
| 110 |
id => [ARRAY => 3], |
| 111 |
got => 'x', |
| 112 |
check => DNE, |
| 113 |
}; |
| 114 |
end(), |
| 115 |
}, |
| 116 |
"Got 2 deltas for extra items" |
| 117 |
); |
| 118 |
|
| 119 |
subtest 'duplicate items' => sub { |
| 120 |
my $items = ['a', 'a']; |
| 121 |
my $one = $CLASS->new(items => $items); |
| 122 |
|
| 123 |
like( |
| 124 |
[$one->deltas(%params, got => ['a', 'a'])], |
| 125 |
[], |
| 126 |
"No delta, no diff" |
| 127 |
); |
| 128 |
|
| 129 |
like( |
| 130 |
[$one->deltas(%params, got => ['a', 'a', 'a'])], |
| 131 |
[], |
| 132 |
"No delta, not checking ending" |
| 133 |
); |
| 134 |
|
| 135 |
$one->set_ending(1); |
| 136 |
like( |
| 137 |
[$one->deltas(%params, got => ['a', 'a', 'a'])], |
| 138 |
array { |
| 139 |
item 0 => { |
| 140 |
dne => 'check', |
| 141 |
id => [ARRAY => 2], |
| 142 |
got => 'a', |
| 143 |
check => DNE, |
| 144 |
}; |
| 145 |
end(), |
| 146 |
}, |
| 147 |
"Got the delta for extra item" |
| 148 |
); |
| 149 |
}; |
| 150 |
}; |
| 151 |
|
| 152 |
subtest add_prop => sub { |
| 153 |
my $one = $CLASS->new(); |
| 154 |
|
| 155 |
ok(!$one->meta, "no meta yet"); |
| 156 |
$one->add_prop('size' => 1); |
| 157 |
isa_ok($one->meta, 'Test2::Compare::Meta'); |
| 158 |
is(@{$one->meta->items}, 1, "1 item"); |
| 159 |
|
| 160 |
$one->add_prop('reftype' => 'ARRAY'); |
| 161 |
is(@{$one->meta->items}, 2, "2 items"); |
| 162 |
}; |
| 163 |
|
| 164 |
done_testing; |