| 1 |
# vim: set ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use 5.020; |
| 5 |
use strictures 2; |
| 6 |
use stable 0.031 'postderef'; |
| 7 |
use experimental 'signatures'; |
| 8 |
no autovivification warn => qw(fetch store exists delete); |
| 9 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 10 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 11 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 12 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 13 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 14 |
|
| 15 |
use Test2::API 'intercept'; |
| 16 |
use Test2::V0 qw(!bag !bool), -no_pragmas => 1; |
| 17 |
use if $ENV{AUTHOR_TESTING}, 'Test2::Warnings'; |
| 18 |
use Scalar::Util 'dualvar'; |
| 19 |
use Tie::Hash; |
| 20 |
use Test::JSON::Schema::Acceptance; |
| 21 |
use Test::File::ShareDir -share => { -dist => { 'Test-JSON-Schema-Acceptance' => 'share' } }; |
| 22 |
|
| 23 |
use lib 't/lib'; |
| 24 |
use Helper; |
| 25 |
|
| 26 |
my $key = 'a'; |
| 27 |
|
| 28 |
foreach my $test ( |
| 29 |
[ 'autovivification' => sub ($thing) { |
| 30 |
$thing->{(keys %$thing)[0]}{$key++} = 'this should not be here'; |
| 31 |
} ], |
| 32 |
[ 'string->integer type mutation, explicit numification' => sub ($thing) { |
| 33 |
$thing->{foo}{string} += 0; |
| 34 |
} ], |
| 35 |
[ 'integer->string type mutation, explicit stringification' => sub ($thing) { |
| 36 |
$thing->{foo}{int} .= ''; |
| 37 |
} ], |
| 38 |
[ 'string->integer type mutation, used as a number' => sub ($thing) { |
| 39 |
my $str = sprintf('%d', $thing->{foo}{string}); |
| 40 |
} ], |
| 41 |
[ 'integer->string type mutation, used as a string' => sub ($thing) { |
| 42 |
my $str = sprintf('%s', $thing->{foo}{int}); |
| 43 |
}, |
| 44 |
"$]" >= 5.035009 ? 'on perls >= 5.35.9, reading the string form of an integer value no longer sets the flag SVf_POK' : '', |
| 45 |
], |
| 46 |
[ 'string->dualvar' => sub ($thing) { |
| 47 |
$thing->{foo}{string} = dualvar(1, 'one'); |
| 48 |
} ], |
| 49 |
[ 'integer->dualvar' => sub ($thing) { |
| 50 |
$thing->{foo}{int} = dualvar(1, 'one'); |
| 51 |
} ], |
| 52 |
[ 'blessed hash replacement' => sub ($thing) { |
| 53 |
$thing->{foo} = bless($thing->{foo}, 'MyHash'); |
| 54 |
} ], |
| 55 |
[ 'tied hash replacement' => sub ($thing) { |
| 56 |
my %hash; |
| 57 |
tie(%hash, 'Tie::StdHash'); |
| 58 |
@hash{keys %{$thing->{foo}}} = values %{$thing->{foo}}; |
| 59 |
$thing->{foo} = \%hash; |
| 60 |
} ], |
| 61 |
[ 'bigint' => sub ($thing) { |
| 62 |
$thing->{foo}{bigint} += 1; |
| 63 |
} ], |
| 64 |
[ 'bignum' => sub ($thing) { |
| 65 |
$thing->{foo}{bignum} += 1; |
| 66 |
} ], |
| 67 |
) |
| 68 |
{ |
| 69 |
my ($test_name, $mutator, $skip) = @$test; |
| 70 |
|
| 71 |
foreach my $type (qw(data schema)) { |
| 72 |
if ($skip) { |
| 73 |
SKIP: { skip $test_name.' in '.$type.': '.$skip, 1 } |
| 74 |
next; |
| 75 |
} |
| 76 |
|
| 77 |
my $accepter = Test::JSON::Schema::Acceptance->new(test_dir => 't/tests/mutation'); |
| 78 |
my $events = intercept( |
| 79 |
sub { |
| 80 |
$accepter->acceptance(validate_data => sub ($schema, $data) { |
| 81 |
$mutator->($type eq 'data' ? $data : $type eq 'schema' ? $schema : die "$type?!"); |
| 82 |
|
| 83 |
return 1; |
| 84 |
}); |
| 85 |
} |
| 86 |
); |
| 87 |
|
| 88 |
my $TODO = todo 'JSON::PP may mutate some bignums' |
| 89 |
if Test::JSON::Schema::Acceptance::_JSON_BACKEND eq 'JSON::PP' and $test_name =~ /^big/; |
| 90 |
|
| 91 |
cmp_result( |
| 92 |
[ map exists $_->{parent} |
| 93 |
? { |
| 94 |
details => $_->{assert}{details}, |
| 95 |
pass => $_->{assert}{pass}, |
| 96 |
children => [ map exists $_->{assert} ? $_->{assert} : (), @{$_->{parent}{children}} ], |
| 97 |
} |
| 98 |
: (), |
| 99 |
map $_->facet_data, @$events ], |
| 100 |
[ |
| 101 |
{ |
| 102 |
details => 'hash.json: "mutation of hashes" - "object of integers"', |
| 103 |
pass => 0, |
| 104 |
children => [ |
| 105 |
superhashof({ |
| 106 |
details => 'test passes: data is valid: true', |
| 107 |
pass => 1, |
| 108 |
}), |
| 109 |
superhashof({ |
| 110 |
details => 'evaluator did not mutate '.$type, |
| 111 |
pass => 0, |
| 112 |
}), |
| 113 |
], |
| 114 |
}, |
| 115 |
], |
| 116 |
$test_name.' in '.$type, |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
done_testing; |