| 1 |
use Test2::Bundle::Extended -target => 'Test2::Compare::Array'; |
| 2 |
|
| 3 |
use lib 't/lib'; |
| 4 |
|
| 5 |
isa_ok($CLASS, 'Test2::Compare::Base'); |
| 6 |
is($CLASS->name, '<ARRAY>', "got name"); |
| 7 |
|
| 8 |
subtest construction => sub { |
| 9 |
my $one = $CLASS->new(); |
| 10 |
isa_ok($one, $CLASS); |
| 11 |
is($one->items, {}, "created items as a hash"); |
| 12 |
is($one->order, [], "created order as an array"); |
| 13 |
|
| 14 |
$one = $CLASS->new(items => { 1 => 'a', 2 => 'b' }); |
| 15 |
is($one->items, { 1 => 'a', 2 => 'b' }, "used items as specified"); |
| 16 |
is($one->order, [ 1, 2 ], "generated order"); |
| 17 |
|
| 18 |
like( |
| 19 |
dies { $CLASS->new(items => { a => 1, b => 2 }) }, |
| 20 |
qr/All indexes listed in the 'items' hashref must be numeric/, |
| 21 |
"Indexes must be numeric" |
| 22 |
); |
| 23 |
like( |
| 24 |
dies { $CLASS->new(items => {}, order => [ 'a' ]) }, |
| 25 |
qr/All indexes listed in the 'order' arrayref must be numeric/, |
| 26 |
"Indexes must be numeric" |
| 27 |
); |
| 28 |
|
| 29 |
$one = $CLASS->new(inref => ['a', 'b']); |
| 30 |
is($one->items, { 0 => 'a', 1 => 'b' }, "Generated items"); |
| 31 |
is($one->order, [ 0, 1 ], "generated order"); |
| 32 |
|
| 33 |
like( |
| 34 |
dies { $CLASS->new(inref => [ 'a' ], items => { 0 => 'a' }) }, |
| 35 |
qr/Cannot specify both 'inref' and 'items'/, |
| 36 |
"Cannot specify inref and items" |
| 37 |
); |
| 38 |
like( |
| 39 |
dies { $CLASS->new(inref => [ 'a' ], order => [ 0 ]) }, |
| 40 |
qr/Cannot specify both 'inref' and 'order'/, |
| 41 |
"Cannot specify inref and order" |
| 42 |
); |
| 43 |
like( |
| 44 |
dies { $CLASS->new(inref => { 1 => 'a' }) }, |
| 45 |
qr/'inref' must be an array reference, got 'HASH\(.+\)'/, |
| 46 |
"inref must be an array" |
| 47 |
); |
| 48 |
}; |
| 49 |
|
| 50 |
subtest verify => sub { |
| 51 |
my $one = $CLASS->new; |
| 52 |
|
| 53 |
is($one->verify(exists => 0), 0, "did not get anything"); |
| 54 |
is($one->verify(exists => 1, got => undef), 0, "undef is not an array"); |
| 55 |
is($one->verify(exists => 1, got => 0), 0, "0 is not an array"); |
| 56 |
is($one->verify(exists => 1, got => 1), 0, "1 is not an array"); |
| 57 |
is($one->verify(exists => 1, got => 'string'), 0, "'string' is not an array"); |
| 58 |
is($one->verify(exists => 1, got => {}), 0, "a hash is not an array"); |
| 59 |
is($one->verify(exists => 1, got => []), 1, "an array is an array"); |
| 60 |
}; |
| 61 |
|
| 62 |
subtest top_index => sub { |
| 63 |
my $one = $CLASS->new; |
| 64 |
is($one->top_index, undef, "no indexes"); |
| 65 |
|
| 66 |
$one = $CLASS->new(inref => [ 'a', 'b', 'c' ]); |
| 67 |
is($one->top_index, 2, "got top index"); |
| 68 |
|
| 69 |
$one = $CLASS->new(inref => [ 'a' ]); |
| 70 |
is($one->top_index, 0, "got top index"); |
| 71 |
|
| 72 |
$one = $CLASS->new(inref => [ ]); |
| 73 |
is($one->top_index, undef, "no indexes"); |
| 74 |
|
| 75 |
$one = $CLASS->new(order => [ 0, 1, 2, sub { 1 }], items => { 0 => 'a', 1 => 'b', 2 => 'c' }); |
| 76 |
is($one->top_index, 2, "got top index, despite ref"); |
| 77 |
}; |
| 78 |
|
| 79 |
subtest add_item => sub { |
| 80 |
my $one = $CLASS->new(); |
| 81 |
|
| 82 |
$one->add_item('a'); |
| 83 |
$one->add_item(1 => 'b'); |
| 84 |
$one->add_item(3 => 'd'); |
| 85 |
|
| 86 |
like( |
| 87 |
dies { $one->add_item(2 => 'c') }, |
| 88 |
qr/elements must be added in order!/, |
| 89 |
"Items must be added in order" |
| 90 |
); |
| 91 |
|
| 92 |
$one->add_item(8 => 'x'); |
| 93 |
$one->add_item('y'); |
| 94 |
|
| 95 |
is( |
| 96 |
$one->items, |
| 97 |
{ 0 => 'a', 1 => 'b', 3 => 'd', 8 => 'x', 9 => 'y' }, |
| 98 |
"Expected items" |
| 99 |
); |
| 100 |
|
| 101 |
is($one->order, [0, 1, 3, 8, 9], "got order"); |
| 102 |
}; |
| 103 |
|
| 104 |
subtest add_filter => sub { |
| 105 |
my $one = $CLASS->new; |
| 106 |
|
| 107 |
$one->add_item('a'); |
| 108 |
my $f = sub { grep { m/[a-z]/ } @_ }; |
| 109 |
$one->add_filter($f); |
| 110 |
$one->add_item('b'); |
| 111 |
|
| 112 |
like( |
| 113 |
dies { $one->add_filter }, |
| 114 |
qr/A single coderef is required/, |
| 115 |
"No filter specified" |
| 116 |
); |
| 117 |
like( |
| 118 |
dies { $one->add_filter(1) }, |
| 119 |
qr/A single coderef is required/, |
| 120 |
"Not a valid filter" |
| 121 |
); |
| 122 |
like( |
| 123 |
dies { $one->add_filter(undef) }, |
| 124 |
qr/A single coderef is required/, |
| 125 |
"Filter must be defined" |
| 126 |
); |
| 127 |
like( |
| 128 |
dies { $one->add_filter(sub { 1 }, sub { 2 }) }, |
| 129 |
qr/A single coderef is required/, |
| 130 |
"Too many filters" |
| 131 |
); |
| 132 |
like( |
| 133 |
dies { $one->add_filter({}) }, |
| 134 |
qr/A single coderef is required/, |
| 135 |
"Not a coderef" |
| 136 |
); |
| 137 |
|
| 138 |
is( $one->order, [0, $f, 1], "added filter to order array"); |
| 139 |
}; |
| 140 |
|
| 141 |
subtest deltas => sub { |
| 142 |
my $conv = Test2::Compare->can('strict_convert'); |
| 143 |
|
| 144 |
my %params = (exists => 1, convert => $conv, seen => {}); |
| 145 |
|
| 146 |
my $inref = ['a', 'b']; |
| 147 |
my $one = $CLASS->new(inref => $inref); |
| 148 |
|
| 149 |
like( |
| 150 |
[$one->deltas(%params, got => ['a', 'b'])], |
| 151 |
[], |
| 152 |
"No delta, no diff" |
| 153 |
); |
| 154 |
|
| 155 |
like( |
| 156 |
[$one->deltas(%params, got => ['a'])], |
| 157 |
[ |
| 158 |
{ |
| 159 |
dne => 'got', |
| 160 |
id => [ARRAY => 1], |
| 161 |
got => undef, |
| 162 |
} |
| 163 |
], |
| 164 |
"Got the delta for the missing value" |
| 165 |
); |
| 166 |
|
| 167 |
like( |
| 168 |
[$one->deltas(%params, got => ['a', 'a'])], |
| 169 |
[ |
| 170 |
{ |
| 171 |
dne => DNE, |
| 172 |
id => [ARRAY => 1], |
| 173 |
got => 'a', |
| 174 |
chk => {input => 'b'}, |
| 175 |
} |
| 176 |
], |
| 177 |
"Got the delta for the incorrect value" |
| 178 |
); |
| 179 |
|
| 180 |
like( |
| 181 |
[$one->deltas(%params, got => ['a', 'b', 'a', 'a'])], |
| 182 |
[], |
| 183 |
"No delta, not checking ending" |
| 184 |
); |
| 185 |
|
| 186 |
$one->set_ending(1); |
| 187 |
like( |
| 188 |
[$one->deltas(%params, got => ['a', 'b', 'a', 'x'])], |
| 189 |
array { |
| 190 |
item 0 => { |
| 191 |
dne => 'check', |
| 192 |
id => [ARRAY => 2], |
| 193 |
got => 'a', |
| 194 |
check => DNE, |
| 195 |
}; |
| 196 |
item 1 => { |
| 197 |
dne => 'check', |
| 198 |
id => [ARRAY => 3], |
| 199 |
got => 'x', |
| 200 |
check => DNE, |
| 201 |
}; |
| 202 |
end(), |
| 203 |
}, |
| 204 |
"Got 2 deltas for extra items" |
| 205 |
); |
| 206 |
|
| 207 |
$one = $CLASS->new(); |
| 208 |
$one->add_item('a'); |
| 209 |
$one->add_filter( |
| 210 |
sub { |
| 211 |
grep { m/[a-z]/ } @_; |
| 212 |
} |
| 213 |
); |
| 214 |
$one->add_item('b'); |
| 215 |
|
| 216 |
is( |
| 217 |
[$one->deltas(%params, got => ['a', 1, 2, 'b'])], |
| 218 |
[], |
| 219 |
"Filter worked" |
| 220 |
); |
| 221 |
|
| 222 |
like( |
| 223 |
[$one->deltas(%params, got => ['a', 1, 2, 'a'])], |
| 224 |
[ |
| 225 |
{ |
| 226 |
dne => DNE, |
| 227 |
id => [ARRAY => 1], |
| 228 |
got => 'a', |
| 229 |
chk => {input => 'b'}, |
| 230 |
} |
| 231 |
], |
| 232 |
"Filter worked, but input is still wrong" |
| 233 |
); |
| 234 |
}; |
| 235 |
|
| 236 |
{ |
| 237 |
package Foo::Array; |
| 238 |
use base 'MyTest::Target'; |
| 239 |
|
| 240 |
sub new { |
| 241 |
my $class = shift; |
| 242 |
bless [ @_ ] , $class; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
subtest objects_as_arrays => sub { |
| 247 |
|
| 248 |
my $o1 = Foo::Array->new( 'b' ) ; |
| 249 |
my $o2 = Foo::Array->new( 'b' ) ; |
| 250 |
|
| 251 |
is ( $o1, $o2, "same" ); |
| 252 |
}; |
| 253 |
|
| 254 |
subtest add_prop => sub { |
| 255 |
my $one = $CLASS->new(); |
| 256 |
|
| 257 |
ok(!$one->meta, "no meta yet"); |
| 258 |
$one->add_prop('size' => 1); |
| 259 |
isa_ok($one->meta, 'Test2::Compare::Meta'); |
| 260 |
is(@{$one->meta->items}, 1, "1 item"); |
| 261 |
|
| 262 |
$one->add_prop('reftype' => 'ARRAY'); |
| 263 |
is(@{$one->meta->items}, 2, "2 items"); |
| 264 |
}; |
| 265 |
|
| 266 |
done_testing; |