| 1 |
use Test2::Bundle::Extended -target => 'Test2::Compare::OrderedSubset'; |
| 2 |
|
| 3 |
use lib 't/lib'; |
| 4 |
|
| 5 |
isa_ok($CLASS, 'Test2::Compare::Base'); |
| 6 |
is($CLASS->name, '<ORDERED SUBSET>', "got name"); |
| 7 |
|
| 8 |
subtest construction => sub { |
| 9 |
my $one = $CLASS->new(); |
| 10 |
isa_ok($one, $CLASS); |
| 11 |
is($one->items, [], "created items as an array"); |
| 12 |
|
| 13 |
$one = $CLASS->new(items => [qw/a b/]); |
| 14 |
is($one->items, [qw/a b/], "used items as specified"); |
| 15 |
|
| 16 |
$one = $CLASS->new(inref => ['a', 'b']); |
| 17 |
is($one->items, [qw/a b/], "Generated items"); |
| 18 |
|
| 19 |
like( |
| 20 |
dies { $CLASS->new(inref => { 1 => 'a' }) }, |
| 21 |
qr/'inref' must be an array reference, got 'HASH\(.+\)'/, |
| 22 |
"inref must be an array" |
| 23 |
); |
| 24 |
}; |
| 25 |
|
| 26 |
subtest verify => sub { |
| 27 |
my $one = $CLASS->new; |
| 28 |
|
| 29 |
is($one->verify(exists => 0), 0, "did not get anything"); |
| 30 |
is($one->verify(exists => 1, got => undef), 0, "undef is not an array"); |
| 31 |
is($one->verify(exists => 1, got => 0), 0, "0 is not an array"); |
| 32 |
is($one->verify(exists => 1, got => 1), 0, "1 is not an array"); |
| 33 |
is($one->verify(exists => 1, got => 'string'), 0, "'string' is not an array"); |
| 34 |
is($one->verify(exists => 1, got => {}), 0, "a hash is not an array"); |
| 35 |
is($one->verify(exists => 1, got => []), 1, "an array is an array"); |
| 36 |
}; |
| 37 |
|
| 38 |
subtest add_item => sub { |
| 39 |
my $one = $CLASS->new(); |
| 40 |
|
| 41 |
$one->add_item('a'); |
| 42 |
$one->add_item(1 => 'b'); |
| 43 |
$one->add_item(3 => 'd'); |
| 44 |
|
| 45 |
$one->add_item(8 => 'x'); |
| 46 |
$one->add_item('y'); |
| 47 |
|
| 48 |
is( |
| 49 |
$one->items, |
| 50 |
[ 'a', 'b', 'd', 'x', 'y' ], |
| 51 |
"Expected items" |
| 52 |
); |
| 53 |
}; |
| 54 |
|
| 55 |
subtest deltas => sub { |
| 56 |
my $conv = Test2::Compare->can('strict_convert'); |
| 57 |
|
| 58 |
my %params = (exists => 1, convert => $conv, seen => {}); |
| 59 |
|
| 60 |
my $inref = ['a', 'b']; |
| 61 |
my $one = $CLASS->new(inref => $inref); |
| 62 |
|
| 63 |
like( |
| 64 |
[$one->deltas(%params, got => ['a', 'b'])], |
| 65 |
[], |
| 66 |
"No delta, no diff" |
| 67 |
); |
| 68 |
|
| 69 |
like( |
| 70 |
[$one->deltas(%params, got => ['a'])], |
| 71 |
[ |
| 72 |
{ |
| 73 |
dne => 'got', |
| 74 |
id => [ARRAY => '?'], |
| 75 |
} |
| 76 |
], |
| 77 |
"Got the delta for the missing value" |
| 78 |
); |
| 79 |
|
| 80 |
like( |
| 81 |
[$one->deltas(%params, got => ['a', 'a'])], |
| 82 |
[ |
| 83 |
{ |
| 84 |
dne => 'got', |
| 85 |
id => [ARRAY => '?'], |
| 86 |
} |
| 87 |
], |
| 88 |
"Got the delta for the incorrect value" |
| 89 |
); |
| 90 |
|
| 91 |
like( |
| 92 |
[$one->deltas(%params, got => ['a', 'b', 'a', 'a'])], |
| 93 |
[], |
| 94 |
"No delta, not checking ending" |
| 95 |
); |
| 96 |
}; |
| 97 |
|
| 98 |
{ |
| 99 |
package Foo::OO; |
| 100 |
|
| 101 |
use base 'MyTest::Target'; |
| 102 |
|
| 103 |
sub new { |
| 104 |
my $class = shift; |
| 105 |
bless [ @_ ] , $class; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
subtest object_as_arrays => sub { |
| 110 |
my $o1 = Foo::OO->new( 'b') ; |
| 111 |
|
| 112 |
is ( $o1 , subset{ item 'b' }, "same" ); |
| 113 |
}; |
| 114 |
|
| 115 |
done_testing; |