| 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 utf8; |
| 14 |
use open ':std', ':encoding(UTF-8)'; # force stdin, stdout, stderr into utf8 |
| 15 |
|
| 16 |
use lib 't/lib'; |
| 17 |
use Helper; |
| 18 |
|
| 19 |
my @tests = ( |
| 20 |
# data (dividend), schema value (divisor), expected result |
| 21 |
[ 4, 2, true ], |
| 22 |
[ 4, 1, true ], |
| 23 |
[ 4, 3, false ], |
| 24 |
[ 4.5, 1.5, true ], |
| 25 |
[ 4.5, 1, false ], |
| 26 |
[ 4.5, 3, false ], |
| 27 |
[ 4, 2, true ], |
| 28 |
[ 4, 2.5, false ], |
| 29 |
[ 5, 2.5, true ], |
| 30 |
[ 4.5, 2.25, true ], |
| 31 |
[ 4.5, 2.5, false ], |
| 32 |
[ 4.5, 2, false ], |
| 33 |
[ -(~0 >> 1) -1, 0.5, true ], # min signed int |
| 34 |
[ ~0 >> 1, 0.5, true ], # max signed int |
| 35 |
[ ~0, 0.5, true ], # max unsigned int |
| 36 |
); |
| 37 |
|
| 38 |
|
| 39 |
my $js = JSON::Schema::Modern->new; |
| 40 |
my $note = $ENV{AUTHOR_TESTING} || $ENV{AUTOMATED_TESTING} ? \&diag : \¬e; |
| 41 |
|
| 42 |
sub run_test ($data, $schema_value, $expected) { |
| 43 |
local $Test::Builder::Level = $Test::Builder::Level + 1; |
| 44 |
my $result = $js->evaluate($data, { multipleOf => $schema_value }); |
| 45 |
my $pass = ok(!($result->valid xor $expected), "$data is ".($expected ? '' : 'not ')."a multiple of $schema_value"); |
| 46 |
$note->('got result: '.$result->dump) if not $pass; |
| 47 |
} |
| 48 |
|
| 49 |
subtest 'multipleOf, native types' => sub { |
| 50 |
foreach my $test (@tests) { |
| 51 |
my ($data, $schema_value, $expected) = @$test; |
| 52 |
run_test($data, $schema_value, $expected); |
| 53 |
}; |
| 54 |
}; |
| 55 |
|
| 56 |
subtest 'multipleOf, data is a bignum' => sub { |
| 57 |
foreach my $test (@tests) { |
| 58 |
my ($data, $schema_value, $expected) = @$test; |
| 59 |
run_test(Math::BigFloat->new($data), $schema_value, $expected); |
| 60 |
}; |
| 61 |
}; |
| 62 |
|
| 63 |
subtest 'multipleOf, multipleOf is a bignum' => sub { |
| 64 |
foreach my $test (@tests) { |
| 65 |
my ($data, $schema_value, $expected) = @$test; |
| 66 |
run_test($data, Math::BigFloat->new($schema_value), $expected); |
| 67 |
}; |
| 68 |
}; |
| 69 |
|
| 70 |
subtest 'multipleOf, data and multipleOf are bignums' => sub { |
| 71 |
foreach my $test (@tests) { |
| 72 |
my ($data, $schema_value, $expected) = @$test; |
| 73 |
run_test(Math::BigFloat->new($data), Math::BigFloat->new($schema_value), $expected); |
| 74 |
}; |
| 75 |
}; |
| 76 |
|
| 77 |
done_testing; |