| 1 |
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
use strictures 2; |
| 3 |
use 5.020; |
| 4 |
use stable 0.031 'postderef'; |
| 5 |
use experimental 'signatures'; |
| 6 |
no autovivification warn => qw(fetch store exists delete); |
| 7 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 8 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 9 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 10 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 11 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 12 |
no feature 'switch'; |
| 13 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 14 |
|
| 15 |
use Feature::Compat::Try; |
| 16 |
use Mojo::File 'path'; |
| 17 |
use builtin::compat 'blessed'; |
| 18 |
|
| 19 |
use if $ENV{AUTHOR_TESTING}, 'Test2::Warnings' => ':fail_on_warning'; # hooks into done_testing unless overridden |
| 20 |
use Test::JSON::Schema::Acceptance 1.039; |
| 21 |
use Test::Memory::Cycle; |
| 22 |
use JSON::Schema::Modern; |
| 23 |
|
| 24 |
# supports options: |
| 25 |
# - acceptance: options passed to Test::JSON::Schema::Acceptance constructor |
| 26 |
# - evaluator: options passed to JSON::Schema::Modern constructor |
| 27 |
# - tests: options passed to Test::JSON::Schema::Acceptance::acceptance method |
| 28 |
# - output_file: filename to print results to (default: none) |
| 29 |
sub acceptance_tests (%options) { |
| 30 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 31 |
|
| 32 |
my $note = $ENV{AUTHOR_TESTING} || $ENV{AUTOMATED_TESTING} ? \&diag : \¬e; |
| 33 |
$note->(''); |
| 34 |
foreach my $env (qw(AUTHOR_TESTING AUTOMATED_TESTING EXTENDED_TESTING NO_TODO TEST_DIR NO_SHORT_CIRCUIT)) { |
| 35 |
$note->($env.': '.($ENV{$env} // '<undef>')); |
| 36 |
} |
| 37 |
$note->(''); |
| 38 |
|
| 39 |
my $accepter = Test::JSON::Schema::Acceptance->new( |
| 40 |
include_optional => 1, |
| 41 |
verbose => $ENV{AUTOMATED_TESTING}, |
| 42 |
test_schemas => $ENV{AUTHOR_TESTING}, |
| 43 |
$options{acceptance}->%*, |
| 44 |
$ENV{TEST_DIR} ? (test_dir => $ENV{TEST_DIR}) |
| 45 |
: $ENV{TEST_PREFIXDIR} ? (test_dir => path($ENV{TEST_PREFIXDIR}, 'tests', $options{acceptance}{specification})) : (), |
| 46 |
supported_specifications => [ qw(draft4 draft6 draft7 draft2019-09 draft2020-12) ], |
| 47 |
); |
| 48 |
$accepter = $accepter->new(%$accepter, |
| 49 |
test_dir => $accepter->test_dir->child($options{acceptance}{test_subdir})) |
| 50 |
if not $ENV{TEST_DIR} and $options{acceptance}{test_subdir}; |
| 51 |
|
| 52 |
$note->('Using JSON decoder: ', blessed($accepter->_json_serializer), ' ', $accepter->_json_serializer->VERSION); |
| 53 |
$note->(''); |
| 54 |
|
| 55 |
my $js = JSON::Schema::Modern->new($options{evaluator}->%*); |
| 56 |
my $js_short_circuit = $ENV{NO_SHORT_CIRCUIT} || JSON::Schema::Modern->new($options{evaluator}->%*, short_circuit => 1); |
| 57 |
|
| 58 |
my $add_resource = sub ($uri, $schema, %resource_options) { |
| 59 |
return if $uri =~ m{/draft-next/}; |
| 60 |
return if $uri =~ m{/v1/}; |
| 61 |
try { |
| 62 |
my $doc = my $document = JSON::Schema::Modern::Document->new( |
| 63 |
schema => $schema, |
| 64 |
evaluator => $js, |
| 65 |
%resource_options, |
| 66 |
); |
| 67 |
|
| 68 |
$js->add_document($uri => $doc); |
| 69 |
$js_short_circuit->add_document($uri => $doc) if not $ENV{NO_SHORT_CIRCUIT}; |
| 70 |
} |
| 71 |
catch ($e) { |
| 72 |
die blessed($e) && $e->isa('JSON::Schema::Modern::Result') ? $e->dump : $e; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
$accepter->acceptance( |
| 77 |
validate_data => sub ($schema, $instance_data) { |
| 78 |
my $result = $js->evaluate($instance_data, $schema); |
| 79 |
my $result_short = $ENV{NO_SHORT_CIRCUIT} || $js_short_circuit->evaluate($instance_data, $schema); |
| 80 |
die 'result is not a JSON::Schema::Modern::Result object' |
| 81 |
if not $result->isa('JSON::Schema::Modern::Result'); |
| 82 |
|
| 83 |
note 'result: ', $result->dump if ref $schema; |
| 84 |
|
| 85 |
if (not $ENV{NO_SHORT_CIRCUIT}) { |
| 86 |
die 'short-circuited result is not a JSON::Schema::Modern::Result object' |
| 87 |
if not $result_short->isa('JSON::Schema::Modern::Result'); |
| 88 |
note 'short-circuited result: ', $result_short->dump; |
| 89 |
die 'results inconsistent between short_circuit = false and true' |
| 90 |
if ($result->valid xor $result_short->valid); |
| 91 |
} |
| 92 |
|
| 93 |
my $in_todo; |
| 94 |
|
| 95 |
# if any errors contain an exception, generate a warning so we can be sure |
| 96 |
# to count that as a failure (an exception would be caught and perhaps TODO'd). |
| 97 |
# (This might change if tests are added that are expected to produce exceptions.) |
| 98 |
foreach my $r ($result, ($ENV{NO_SHORT_CIRCUIT} ? () : $result_short)) { |
| 99 |
diag 'evaluation generated an exception: '.$_->dump |
| 100 |
foreach |
| 101 |
grep +($_->{error} =~ /^EXCEPTION/ |
| 102 |
&& $_->{error} !~ /(max|min)imum value is not a number\z/) # optional/bignum.json |
| 103 |
&& !($in_todo //= grep $_->{todo}, Test2::API::test2_stack->top->{_pre_filters}->@*), |
| 104 |
$r->errors; |
| 105 |
} |
| 106 |
|
| 107 |
($result->valid, $result->TO_JSON); |
| 108 |
}, |
| 109 |
add_resource => $add_resource, |
| 110 |
@ARGV ? (tests => { file => \@ARGV }) : (), |
| 111 |
($options{test} // {})->%*, |
| 112 |
); |
| 113 |
|
| 114 |
memory_cycle_ok($js, 'no leaks in the main evaluator object'); |
| 115 |
memory_cycle_ok($js_short_circuit, 'no leaks in the short-circuiting evaluator object') |
| 116 |
if not $ENV{NO_SHORT_CIRCUIT}; |
| 117 |
|
| 118 |
path('t/results/'.$options{output_file})->spew($accepter->results_text, 'UTF-8') |
| 119 |
if $ENV{AUTHOR_TESTING}; |
| 120 |
} |
| 121 |
|
| 122 |
1; |