| 1 |
# vim: set ft=perl ts=8 sts=2 sw=2 tw=100 et : |
| 2 |
# no package, so things defined here appear in the namespace of the parent. |
| 3 |
use strictures 2; |
| 4 |
use 5.020; |
| 5 |
use stable 0.031 'postderef'; |
| 6 |
use experimental 'signatures'; |
| 7 |
no autovivification warn => qw(fetch store exists delete); |
| 8 |
use if "$]" >= 5.022, experimental => 're_strict'; |
| 9 |
no if "$]" >= 5.031009, feature => 'indirect'; |
| 10 |
no if "$]" >= 5.033001, feature => 'multidimensional'; |
| 11 |
no if "$]" >= 5.033006, feature => 'bareword_filehandles'; |
| 12 |
no if "$]" >= 5.041009, feature => 'smartmatch'; |
| 13 |
no feature 'switch'; |
| 14 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 15 |
|
| 16 |
use if $ENV{NO_OPTIONAL_MODULES}, 'Devel::Hide', qw( |
| 17 |
Data::Validate::Domain |
| 18 |
Email::Address::XS |
| 19 |
Net::IDN::Encode |
| 20 |
Data::Validate::URI |
| 21 |
Sereal |
| 22 |
Cpanel::JSON::XS |
| 23 |
); |
| 24 |
|
| 25 |
if ($ENV{NO_OPTIONAL_MODULES}) { |
| 26 |
$ENV{AUTHOR_TESTING} = 0; # disable official test suite tests: they require these modules |
| 27 |
$ENV{RELEASE_TESTING} = 0; # "" |
| 28 |
} |
| 29 |
|
| 30 |
use Test2::V0 qw(!bag !bool !warnings !subtest), -no_pragmas => 1; # prefer Test::Deep and Test2::Warnings versions of these exports |
| 31 |
use if $ENV{AUTHOR_TESTING}, 'Test2::Warnings', ':report_warnings'; |
| 32 |
sub subtest { Test2::V0::subtest(@_); bail_if_not_passing() if $ENV{AUTHOR_TESTING}; } |
| 33 |
use if $ENV{AUTHOR_TESTING} || -d '.git', 'Test2::Plugin::SubtestFilter'; |
| 34 |
use Test::Deep qw(!array !hash !blessed); # import symbols: ignore, re etc |
| 35 |
use Test2::API 'context_do'; |
| 36 |
use Test::File::ShareDir -share => { -dist => { 'JSON-Schema-Modern' => 'share' } }; |
| 37 |
use JSON::Schema::Modern; |
| 38 |
use JSON::Schema::Modern::Utilities qw(jsonp true false); |
| 39 |
|
| 40 |
my $encoder = JSON::Schema::Modern::_JSON_BACKEND()->new |
| 41 |
->allow_nonref(1) |
| 42 |
->utf8(0) |
| 43 |
->allow_bignum(1) |
| 44 |
->allow_blessed(1) |
| 45 |
->convert_blessed(1) |
| 46 |
->canonical(1) |
| 47 |
->pretty(1) |
| 48 |
->space_before(0) |
| 49 |
->indent_length(2); |
| 50 |
|
| 51 |
# like sprintf, but all list items are JSON-encoded. assumes placeholders are %s! |
| 52 |
sub json_sprintf { |
| 53 |
sprintf(shift, map +(ref($_) =~ /^Math::Big(?:Int|Float)\z/ ? ref($_).'->new(\''.$_.'\')' : $encoder->indent(0)->encode($_)), @_); |
| 54 |
} |
| 55 |
|
| 56 |
sub encode ($x ) { |
| 57 |
(ref $x eq 'REF' || ref $x eq 'SCALAR' ? '\\' : '') |
| 58 |
.$encoder->encode(ref $x eq 'REF' || ref $x eq 'SCALAR' ? $x->$* : $x); |
| 59 |
} |
| 60 |
|
| 61 |
# deep comparison, with strict typing |
| 62 |
sub is_equal ($got, $expected, $test_name = undef) { |
| 63 |
context_do { |
| 64 |
my $ctx = shift; |
| 65 |
my ($got, $expected, $test_name) = @_; |
| 66 |
my $equal = JSON::Schema::Modern::Utilities::is_equal($got, $expected, my $state = {}); |
| 67 |
if ($equal) { |
| 68 |
$ctx->pass($test_name); |
| 69 |
} |
| 70 |
else { |
| 71 |
$ctx->fail($test_name); |
| 72 |
my $method = |
| 73 |
# be less noisy for expected failures |
| 74 |
(grep $_->{todo}, Test2::API::test2_stack->top->{_pre_filters}->@*) ? 'note' |
| 75 |
: $ENV{AUTHOR_TESTING} || $ENV{AUTOMATED_TESTING} ? 'diag' : 'note'; |
| 76 |
|
| 77 |
$ctx->$method('structures differ'.($state->{path} ? ' starting at '.$state->{path} : '') |
| 78 |
.': '.$state->{error}); |
| 79 |
my ($equal, $stack) = Test::Deep::cmp_details($got, $expected); |
| 80 |
$ctx->$method(Test::Deep::deep_diag($stack)) if not $equal; |
| 81 |
$ctx->$method("got result:\n".$encoder->encode($got)); |
| 82 |
} |
| 83 |
return $equal; |
| 84 |
} $got, $expected, $test_name; |
| 85 |
} |
| 86 |
|
| 87 |
# deep comparison, with Test::Deep syntax sugar |
| 88 |
sub cmp_result ($got, $expected, $test_name) { |
| 89 |
context_do { |
| 90 |
my $ctx = shift; |
| 91 |
my ($got, $expected, $test_name) = @_; |
| 92 |
my ($equal, $stack) = Test::Deep::cmp_details($got, $expected); |
| 93 |
if ($equal) { |
| 94 |
$ctx->pass($test_name); |
| 95 |
} |
| 96 |
else { |
| 97 |
$ctx->fail($test_name); |
| 98 |
my $method = |
| 99 |
# be less noisy for expected failures |
| 100 |
(grep $_->{todo}, Test2::API::test2_stack->top->{_pre_filters}->@*) ? 'note' |
| 101 |
: $ENV{AUTHOR_TESTING} || $ENV{AUTOMATED_TESTING} ? 'diag' : 'note'; |
| 102 |
$ctx->$method(Test::Deep::deep_diag($stack)); |
| 103 |
$ctx->$method("got result:\n".encode($got)); |
| 104 |
} |
| 105 |
return $equal; |
| 106 |
} $got, $expected, $test_name; |
| 107 |
} |
| 108 |
|
| 109 |
sub lives_result ($sub, $test_name) { |
| 110 |
context_do { |
| 111 |
my ($ctx, $result) = @_; |
| 112 |
if (not $ctx->ok(lives(\&$sub), $test_name)) { |
| 113 |
my $method = |
| 114 |
# be less noisy for expected failures |
| 115 |
(grep $_->{todo}, Test2::API::test2_stack->top->{_pre_filters}->@*) ? 'note' |
| 116 |
: $ENV{AUTHOR_TESTING} || $ENV{AUTOMATED_TESTING} ? 'diag' : 'note'; |
| 117 |
$ctx->$method("got result:\n".encode($result)); |
| 118 |
} |
| 119 |
} $@; |
| 120 |
} |
| 121 |
|
| 122 |
sub die_result ($sub, $pattern, $test_name) { |
| 123 |
# we don't use Test2::Tools::Exception::dies because it tickles JSM::Result's boolean overload |
| 124 |
eval { $sub->() }; |
| 125 |
my $result = $@; |
| 126 |
return fail($test_name) if not defined $result; |
| 127 |
context_do { |
| 128 |
my ($ctx, $result) = @_; |
| 129 |
if (not like($result, $pattern, $test_name)) { |
| 130 |
my $method = |
| 131 |
# be less noisy for expected failures |
| 132 |
(grep $_->{todo}, Test2::API::test2_stack->top->{_pre_filters}->@*) ? 'note' |
| 133 |
: $ENV{AUTHOR_TESTING} || $ENV{AUTOMATED_TESTING} ? 'diag' : 'note'; |
| 134 |
$ctx->$method("got result:\n".encode($result)); |
| 135 |
} |
| 136 |
} $result; |
| 137 |
} |
| 138 |
|
| 139 |
sub is_passing () { |
| 140 |
context_do { shift->hub->is_passing }; |
| 141 |
} |
| 142 |
|
| 143 |
sub bail_if_not_passing { |
| 144 |
context_do { |
| 145 |
my $ctx = shift; |
| 146 |
$ctx->bail if not $ctx->hub->is_passing; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
1; |