| 1 |
use Test2::Bundle::Extended -target => 'Test2::Compare::Set'; |
| 2 |
|
| 3 |
subtest construction => sub { |
| 4 |
my $one = $CLASS->new(); |
| 5 |
isa_ok($one, 'Test2::Compare::Base', $CLASS); |
| 6 |
is($one->reduction, 'any', "default to 'any'"); |
| 7 |
is($one->checks, [], "default to empty list of checks"); |
| 8 |
is($one->name, '<CHECK-SET>', "got name"); |
| 9 |
is($one->operator, 'any', "got op"); |
| 10 |
|
| 11 |
$one = $CLASS->new(checks => [ 'a', 'b' ], reduction => 'all'); |
| 12 |
isa_ok($one, 'Test2::Compare::Base', $CLASS); |
| 13 |
is($one->reduction, 'all', "specified reduction"); |
| 14 |
is($one->checks, ['a', 'b'], "specified checks"); |
| 15 |
is($one->name, '<CHECK-SET>', "got name"); |
| 16 |
is($one->operator, 'all', "got op"); |
| 17 |
|
| 18 |
like( |
| 19 |
dies { $CLASS->new(reduction => 'fake') }, |
| 20 |
qr/'fake' is not a valid set reduction/, |
| 21 |
"Need a valid reduction", |
| 22 |
); |
| 23 |
}; |
| 24 |
|
| 25 |
subtest set_reduction => sub { |
| 26 |
my $one = $CLASS->new(); |
| 27 |
is($one->reduction, 'any', "default"); |
| 28 |
$one->set_reduction('all'); |
| 29 |
is($one->reduction, 'all', "changed"); |
| 30 |
|
| 31 |
like( |
| 32 |
dies { $one->set_reduction('fake') }, |
| 33 |
qr/'fake' is not a valid set reduction/, |
| 34 |
"Need a valid reduction", |
| 35 |
); |
| 36 |
}; |
| 37 |
|
| 38 |
subtest verify => sub { |
| 39 |
my $one = $CLASS->new(); |
| 40 |
|
| 41 |
is($one->verify(exists => 1), 1, "valid"); |
| 42 |
|
| 43 |
# in_set(DNE) is a valid construct, so we cannot reject non-existing values. |
| 44 |
is($one->verify(exists => 0), 1, "valid"); |
| 45 |
}; |
| 46 |
|
| 47 |
subtest add_check => sub { |
| 48 |
my $one = $CLASS->new(checks => ['a']); |
| 49 |
$one->add_check('b'); |
| 50 |
$one->add_check(match qr/xxx/); |
| 51 |
|
| 52 |
is( |
| 53 |
$one->checks, |
| 54 |
[ 'a', 'b', meta { prop blessed => 'Test2::Compare::Pattern' } ], |
| 55 |
"Added the checks" |
| 56 |
); |
| 57 |
}; |
| 58 |
|
| 59 |
subtest deltas => sub { |
| 60 |
my $one; |
| 61 |
|
| 62 |
my $after_each = sub { |
| 63 |
$one->set_checks(undef); |
| 64 |
is( |
| 65 |
dies { $one->deltas() }, |
| 66 |
"No checks defined for set\n", |
| 67 |
"Need checks list" |
| 68 |
); |
| 69 |
|
| 70 |
$one->set_checks([]); |
| 71 |
$one->set_file(__FILE__); my $file = __FILE__; |
| 72 |
is( |
| 73 |
dies { $one->deltas() }, |
| 74 |
"No checks defined for set\n", |
| 75 |
"Need checks in list" |
| 76 |
); |
| 77 |
|
| 78 |
$one->set_checks(undef); |
| 79 |
$one->set_lines([__LINE__]); my $line1 = __LINE__; |
| 80 |
is( |
| 81 |
dies { $one->deltas() }, |
| 82 |
"No checks defined for set (Set defined in $file line $line1)\n", |
| 83 |
"Need checks list, have file+line" |
| 84 |
); |
| 85 |
|
| 86 |
$one->set_checks([]); |
| 87 |
push @{$one->lines} => __LINE__; my $line2 = __LINE__; |
| 88 |
is( |
| 89 |
dies { $one->deltas() }, |
| 90 |
"No checks defined for set (Set defined in $file lines $line1, $line2)\n", |
| 91 |
"Need checks in list, have file + 2 lines" |
| 92 |
); |
| 93 |
}; |
| 94 |
|
| 95 |
subtest any => sub { |
| 96 |
$one = $CLASS->new(reduction => 'any'); |
| 97 |
$one->add_check(match qr/a/); |
| 98 |
$one->add_check(match qr/b/); |
| 99 |
$one->add_check(match qr/c/); |
| 100 |
|
| 101 |
is('xax', $one, "matches 'a'"); |
| 102 |
is('xbx', $one, "matches 'b'"); |
| 103 |
is('xcx', $one, "matches 'c'"); |
| 104 |
|
| 105 |
is([$one->deltas(got => 'a', exists => 1, seen => {}, convert => sub { $_[0] })], [], "no deltas with 'a'"); |
| 106 |
is([$one->deltas(got => 'b', exists => 1, seen => {}, convert => sub { $_[0] })], [], "no deltas with 'b'"); |
| 107 |
is([$one->deltas(got => 'c', exists => 1, seen => {}, convert => sub { $_[0] })], [], "no deltas with 'c'"); |
| 108 |
|
| 109 |
like( |
| 110 |
[$one->deltas(got => 'x', exists => 1, seen => {}, convert => sub { $_[0] })], |
| 111 |
[{ got => 'x' }, { got => 'x' }, { got => 'x' }, DNE], |
| 112 |
"no matches, 3 deltas, one per check" |
| 113 |
); |
| 114 |
|
| 115 |
$after_each->(); |
| 116 |
}; |
| 117 |
|
| 118 |
subtest all => sub { |
| 119 |
$one = $CLASS->new(reduction => 'all'); |
| 120 |
$one->add_check(mismatch qr/x/); |
| 121 |
$one->add_check(match qr/fo/); |
| 122 |
$one->add_check(match qr/oo/); |
| 123 |
|
| 124 |
is('foo', $one, "matches all 3"); |
| 125 |
|
| 126 |
is([$one->deltas(got => 'foo', exists => 1, seen => {}, convert => sub { $_[0] })], [], "no deltas with 'foo'"); |
| 127 |
|
| 128 |
like( |
| 129 |
[$one->deltas(got => 'oo', exists => 1, seen => {}, convert => sub { $_[0] })], |
| 130 |
[{ got => 'oo' }, DNE], |
| 131 |
"1 delta, one failed check" |
| 132 |
); |
| 133 |
|
| 134 |
like( |
| 135 |
[$one->deltas(got => 'fox', exists => 1, seen => {}, convert => sub { $_[0] })], |
| 136 |
[{ got => 'fox' }, { got => 'fox' }, DNE], |
| 137 |
"2 deltas, one per failed check" |
| 138 |
); |
| 139 |
|
| 140 |
$after_each->(); |
| 141 |
}; |
| 142 |
}; |
| 143 |
|
| 144 |
done_testing; |