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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
use Test2::Bundle::Extended -target => 'Test2::Compare::Array';
use lib 't/lib';
isa_ok($CLASS, 'Test2::Compare::Base');
is($CLASS->name, '<ARRAY>', "got name");
subtest construction => sub {
my $one = $CLASS->new();
isa_ok($one, $CLASS);
is($one->items, {}, "created items as a hash");
is($one->order, [], "created order as an array");
$one = $CLASS->new(items => { 1 => 'a', 2 => 'b' });
is($one->items, { 1 => 'a', 2 => 'b' }, "used items as specified");
is($one->order, [ 1, 2 ], "generated order");
like(
dies { $CLASS->new(items => { a => 1, b => 2 }) },
qr/All indexes listed in the 'items' hashref must be numeric/,
"Indexes must be numeric"
);
like(
dies { $CLASS->new(items => {}, order => [ 'a' ]) },
qr/All indexes listed in the 'order' arrayref must be numeric/,
"Indexes must be numeric"
);
$one = $CLASS->new(inref => ['a', 'b']);
is($one->items, { 0 => 'a', 1 => 'b' }, "Generated items");
is($one->order, [ 0, 1 ], "generated order");
like(
dies { $CLASS->new(inref => [ 'a' ], items => { 0 => 'a' }) },
qr/Cannot specify both 'inref' and 'items'/,
"Cannot specify inref and items"
);
like(
dies { $CLASS->new(inref => [ 'a' ], order => [ 0 ]) },
qr/Cannot specify both 'inref' and 'order'/,
"Cannot specify inref and order"
);
like(
dies { $CLASS->new(inref => { 1 => 'a' }) },
qr/'inref' must be an array reference, got 'HASH\(.+\)'/,
"inref must be 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 top_index => sub {
my $one = $CLASS->new;
is($one->top_index, undef, "no indexes");
$one = $CLASS->new(inref => [ 'a', 'b', 'c' ]);
is($one->top_index, 2, "got top index");
$one = $CLASS->new(inref => [ 'a' ]);
is($one->top_index, 0, "got top index");
$one = $CLASS->new(inref => [ ]);
is($one->top_index, undef, "no indexes");
$one = $CLASS->new(order => [ 0, 1, 2, sub { 1 }], items => { 0 => 'a', 1 => 'b', 2 => 'c' });
is($one->top_index, 2, "got top index, despite ref");
};
subtest add_item => sub {
my $one = $CLASS->new();
$one->add_item('a');
$one->add_item(1 => 'b');
$one->add_item(3 => 'd');
like(
dies { $one->add_item(2 => 'c') },
qr/elements must be added in order!/,
"Items must be added in order"
);
$one->add_item(8 => 'x');
$one->add_item('y');
is(
$one->items,
{ 0 => 'a', 1 => 'b', 3 => 'd', 8 => 'x', 9 => 'y' },
"Expected items"
);
is($one->order, [0, 1, 3, 8, 9], "got order");
};
subtest add_filter => sub {
my $one = $CLASS->new;
$one->add_item('a');
my $f = sub { grep { m/[a-z]/ } @_ };
$one->add_filter($f);
$one->add_item('b');
like(
dies { $one->add_filter },
qr/A single coderef is required/,
"No filter specified"
);
like(
dies { $one->add_filter(1) },
qr/A single coderef is required/,
"Not a valid filter"
);
like(
dies { $one->add_filter(undef) },
qr/A single coderef is required/,
"Filter must be defined"
);
like(
dies { $one->add_filter(sub { 1 }, sub { 2 }) },
qr/A single coderef is required/,
"Too many filters"
);
like(
dies { $one->add_filter({}) },
qr/A single coderef is required/,
"Not a coderef"
);
is( $one->order, [0, $f, 1], "added filter to order array");
};
subtest deltas => sub {
my $conv = Test2::Compare->can('strict_convert');
my %params = (exists => 1, convert => $conv, seen => {});
my $inref = ['a', 'b'];
my $one = $CLASS->new(inref => $inref);
like(
[$one->deltas(%params, got => ['a', 'b'])],
[],
"No delta, no diff"
);
like(
[$one->deltas(%params, got => ['a'])],
[
{
dne => 'got',
id => [ARRAY => 1],
got => undef,
}
],
"Got the delta for the missing value"
);
like(
[$one->deltas(%params, got => ['a', 'a'])],
[
{
dne => DNE,
id => [ARRAY => 1],
got => 'a',
chk => {input => 'b'},
}
],
"Got the delta for the incorrect value"
);
like(
[$one->deltas(%params, got => ['a', 'b', 'a', 'a'])],
[],
"No delta, not checking ending"
);
$one->set_ending(1);
like(
[$one->deltas(%params, got => ['a', 'b', 'a', 'x'])],
array {
item 0 => {
dne => 'check',
id => [ARRAY => 2],
got => 'a',
check => DNE,
};
item 1 => {
dne => 'check',
id => [ARRAY => 3],
got => 'x',
check => DNE,
};
end(),
},
"Got 2 deltas for extra items"
);
$one = $CLASS->new();
$one->add_item('a');
$one->add_filter(
sub {
grep { m/[a-z]/ } @_;
}
);
$one->add_item('b');
is(
[$one->deltas(%params, got => ['a', 1, 2, 'b'])],
[],
"Filter worked"
);
like(
[$one->deltas(%params, got => ['a', 1, 2, 'a'])],
[
{
dne => DNE,
id => [ARRAY => 1],
got => 'a',
chk => {input => 'b'},
}
],
"Filter worked, but input is still wrong"
);
};
{
package Foo::Array;
use base 'MyTest::Target';
sub new {
my $class = shift;
bless [ @_ ] , $class;
}
}
subtest objects_as_arrays => sub {
my $o1 = Foo::Array->new( 'b' ) ;
my $o2 = Foo::Array->new( 'b' ) ;
is ( $o1, $o2, "same" );
};
subtest add_prop => sub {
my $one = $CLASS->new();
ok(!$one->meta, "no meta yet");
$one->add_prop('size' => 1);
isa_ok($one->meta, 'Test2::Compare::Meta');
is(@{$one->meta->items}, 1, "1 item");
$one->add_prop('reftype' => 'ARRAY');
is(@{$one->meta->items}, 2, "2 items");
};
done_testing;
|