| 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 Scalar::Util qw(isdual dualvar); |
| 17 |
use Math::BigInt; |
| 18 |
use Math::BigFloat; |
| 19 |
use JSON::PP (); |
| 20 |
use lib 't/lib'; |
| 21 |
use Helper; |
| 22 |
use JSON::Schema::Modern::Utilities qw(is_type get_type); |
| 23 |
|
| 24 |
use constant HAVE_BUILTIN => "$]" >= 5.035010; |
| 25 |
use if HAVE_BUILTIN, experimental => 'builtin'; |
| 26 |
|
| 27 |
my %inflated_data = ( |
| 28 |
null => [ undef ], |
| 29 |
boolean => [ false, true, JSON::PP::false, JSON::PP::true, HAVE_BUILTIN ? (builtin::true, builtin::false) : () ], |
| 30 |
object => [ {}, { a => 1 } ], |
| 31 |
array => [ [], [ 1 ] ], |
| 32 |
number => [ 3.1, 1.23456789012e10, Math::BigFloat->new('0.123'), Math::BigFloat->new('12345123451234512345.2') ], |
| 33 |
integer => [ 0, -1, 2, 2.0, 2**31-1, 2**31, 2**63-1, 2**63, 2**64, 2**65, 1000000000000000, |
| 34 |
Math::BigInt->new('1e100'), Math::BigInt->new('1'), Math::BigInt->new('1.0'), |
| 35 |
Math::BigInt->new('12345123451234512345.0'), Math::BigFloat->new('12345123451234512345.0'), |
| 36 |
Math::BigFloat->new('1e100'), Math::BigFloat->new('20000000000000.0'), |
| 37 |
Math::BigFloat->new('2e1'), Math::BigFloat->new('1'), Math::BigFloat->new('1.0') ], |
| 38 |
string => [ '', '0', '-1', '2', '2.0', '3.1', 'école', 'ಠ_ಠ' ], |
| 39 |
); |
| 40 |
|
| 41 |
my %json_data = ( |
| 42 |
null => [ 'null' ], |
| 43 |
boolean => [ 'false', 'true' ], |
| 44 |
object => [ '{}', '{"a":1}' ], |
| 45 |
array => [ '[]', '[1]' ], |
| 46 |
number => [ '3.1', '1.23456789012e10', '0.123', '12345123451234512345.2' ], |
| 47 |
integer => [ '0', '-1', '2.0', (map $_.'', 2**31-1, 2**31, 2**63-1, 2**63, 2**64, 2**65), '1000000000000000', '2e1', '1e100', '12345123451234512345.0' ], |
| 48 |
string => [ '""', '"0"', '"-1"', '"2.0"', '"3.1"', |
| 49 |
qq{"\x{c3}\x{a9}cole"}, qq{"\x{e0}\x{b2}\x{a0}_\x{e0}\x{b2}\x{a0}"} ], |
| 50 |
); |
| 51 |
|
| 52 |
foreach my $type (sort keys %inflated_data) { |
| 53 |
subtest 'inflated data, type: '.$type => sub { |
| 54 |
foreach my $value ($inflated_data{$type}->@*) { |
| 55 |
my $value_copy = $value; |
| 56 |
ok(is_type($type, $value), json_sprintf(('is_type("'.$type.'", %s) is true'), $value_copy )); |
| 57 |
ok(is_type('number', $value), json_sprintf(('is_type("number", %s) is true'), $value_copy )) |
| 58 |
if $type eq 'integer'; |
| 59 |
is(get_type($value), $type, json_sprintf(('get_type(%s) = '.$type), $value_copy)); |
| 60 |
|
| 61 |
foreach my $other_type (sort keys %inflated_data) { |
| 62 |
next if $other_type eq $type; |
| 63 |
next if $type eq 'integer' and $other_type eq 'number'; |
| 64 |
|
| 65 |
ok(!is_type($other_type, $value), |
| 66 |
json_sprintf('is_type("'.$other_type.'", %s) is false', $value)); |
| 67 |
} |
| 68 |
|
| 69 |
ok(!isdual($value), 'data is not tampered with while it is tested (not dualvar)') |
| 70 |
if not (HAVE_BUILTIN and builtin::is_bool($value)); |
| 71 |
} |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
my $decoder = JSON::Schema::Modern::_JSON_BACKEND()->new |
| 76 |
->allow_nonref(1) |
| 77 |
->canonical(1) |
| 78 |
->utf8(1) |
| 79 |
->allow_bignum(1); |
| 80 |
|
| 81 |
foreach my $type (sort keys %json_data) { |
| 82 |
subtest 'JSON-encoded data, type: '.$type => sub { |
| 83 |
foreach my $value ($json_data{$type}->@*) { |
| 84 |
$value = $decoder->decode($value); |
| 85 |
my $value_copy = $value; |
| 86 |
ok(is_type($type, $value), json_sprintf(('is_type("'.$type.'", %s) is true'), $value_copy )); |
| 87 |
ok(is_type('number', $value), json_sprintf(('is_type("number", %s) is true'), $value_copy )) |
| 88 |
if $type eq 'integer'; |
| 89 |
is(get_type($value), $type, json_sprintf(('get_type(%s) = '.$type), $value_copy)); |
| 90 |
|
| 91 |
foreach my $other_type (sort keys %json_data) { |
| 92 |
next if $other_type eq $type; |
| 93 |
next if $type eq 'integer' and $other_type eq 'number'; |
| 94 |
|
| 95 |
ok(!is_type($other_type, $value), |
| 96 |
json_sprintf('is_type("'.$other_type.'", %s) is false', $value)); |
| 97 |
} |
| 98 |
|
| 99 |
ok(!isdual($value), 'data is not tampered with while it is tested (not dualvar)') |
| 100 |
if not (HAVE_BUILTIN and builtin::is_bool($value)); |
| 101 |
} |
| 102 |
}; |
| 103 |
} |
| 104 |
|
| 105 |
subtest 'integers and numbers in draft4' => sub { |
| 106 |
subtest 'pre-inflated data' => sub { |
| 107 |
my %draft4_inflated_data = ( |
| 108 |
number => [ 3.1, 2.0, 1.23456789012e10, Math::BigFloat->new('0.123'), Math::BigFloat->new('2.0') ], |
| 109 |
integer => [ 0, -1, 2, Math::BigInt->new('2'), Math::BigInt->new('1.0') ], |
| 110 |
); |
| 111 |
|
| 112 |
foreach my $type (sort keys %draft4_inflated_data) { |
| 113 |
foreach my $value ($draft4_inflated_data{$type}->@*) { |
| 114 |
my $value_copy = $value; |
| 115 |
ok(is_type($type, $value, { legacy_ints => 1 }), json_sprintf(('is_type("'.$type.'", %s) is true'), $value_copy )); |
| 116 |
ok(is_type('number', $value, { legacy_ints => 1 }), json_sprintf(('is_type("number", %s) is true'), $value_copy )) |
| 117 |
if $type eq 'integer'; |
| 118 |
is(get_type($value, { legacy_ints => 1 }), $type, json_sprintf(('get_type(%s) = '.$type), $value_copy)); |
| 119 |
|
| 120 |
foreach my $other_type (qw(null boolean object array string), sort keys %draft4_inflated_data) { |
| 121 |
next if $other_type eq $type; |
| 122 |
next if $type eq 'integer' and $other_type eq 'number'; |
| 123 |
|
| 124 |
ok(!is_type($other_type, $value, { legacy_ints => 1 }), |
| 125 |
json_sprintf('is_type("'.$other_type.'", %s) is false', $value)); |
| 126 |
} |
| 127 |
|
| 128 |
ok(!isdual($value), 'data is not tampered with while it is tested (not dualvar)') |
| 129 |
if not (HAVE_BUILTIN and builtin::is_bool($value)); |
| 130 |
} |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
subtest 'data from encoded json' => sub { |
| 135 |
my %draft4_json_data = ( |
| 136 |
number => [ '3.1', '1.23456789012e10', '0.123', '2.0' ], |
| 137 |
integer => [ '0', '-1', '100000' ], |
| 138 |
# these are actually integers, but we are unable to verify that, as they inflate to |
| 139 |
# Math::BigFloat objects where is_int is true: |
| 140 |
# (map $_.'', 2**31-1, 2**31, 2**63-1, 2**63, 2**64, 2**65), '2e1', '1e100' |
| 141 |
); |
| 142 |
|
| 143 |
foreach my $type (sort keys %draft4_json_data) { |
| 144 |
foreach my $value ($draft4_json_data{$type}->@*) { |
| 145 |
$value = $decoder->decode($value); |
| 146 |
my $value_copy = $value; |
| 147 |
ok(is_type($type, $value, { legacy_ints => 1 }), json_sprintf(('is_type("'.$type.'", %s) is true'), $value_copy )); |
| 148 |
ok(is_type('number', $value, { legacy_ints => 1 }), json_sprintf(('is_type("number", %s) is true'), $value_copy )) |
| 149 |
if $type eq 'integer'; |
| 150 |
is(get_type($value, { legacy_ints => 1 }), $type, json_sprintf(('get_type(%s) = '.$type), $value_copy)); |
| 151 |
|
| 152 |
foreach my $other_type (qw(null boolean object array string), sort keys %draft4_json_data) { |
| 153 |
next if $other_type eq $type; |
| 154 |
next if $type eq 'integer' and $other_type eq 'number'; |
| 155 |
|
| 156 |
ok(!is_type($other_type, $value, { legacy_ints => 1 }), |
| 157 |
json_sprintf('is_type("'.$other_type.'", %s) is false', $value)); |
| 158 |
} |
| 159 |
|
| 160 |
ok(!isdual($value), 'data is not tampered with while it is tested (not dualvar)') |
| 161 |
if not (HAVE_BUILTIN and builtin::is_bool($value)); |
| 162 |
} |
| 163 |
} |
| 164 |
}; |
| 165 |
}; |
| 166 |
|
| 167 |
ok(!is_type('foo', 'wharbarbl'), 'non-existent type does not result in exception'); |
| 168 |
|
| 169 |
subtest 'ambiguous types' => sub { |
| 170 |
subtest 'integers' => sub { |
| 171 |
my $integer = dualvar(5, 'five'); |
| 172 |
is(get_type($integer), 'ambiguous type', 'dualvar integers with different values are ambiguous'); |
| 173 |
ok(!is_type($_, $integer), "dualvar integers with different values are not ${_}s") foreach qw(integer number string); |
| 174 |
|
| 175 |
$integer = 5; |
| 176 |
()= sprintf('%s', $integer); |
| 177 |
|
| 178 |
# legacy behaviour (this only happens for IVs, not NVs) |
| 179 |
SKIP: { |
| 180 |
skip 'on perls < 5.35.9, reading the string form of an integer value sets the flag SVf_POK', 1 |
| 181 |
if "$]" >= 5.035009; |
| 182 |
|
| 183 |
is(get_type($integer), 'string', 'older perls only: integer that is later used as a string is now identified as a string'); |
| 184 |
ok(is_type('integer', $integer), 'integer that is later used as a string is still an integer'); |
| 185 |
ok(is_type('number', $integer), 'integer that is later used as a string is still a number'); |
| 186 |
ok(is_type('string', $integer), 'older perls only: integer that is later used as a string is now a string'); |
| 187 |
} |
| 188 |
|
| 189 |
# modern behaviour |
| 190 |
SKIP: { |
| 191 |
skip 'on perls < 5.35.9, reading the string form of an integer value sets the flag SVf_POK', 1 |
| 192 |
if "$]" < 5.035009; |
| 193 |
|
| 194 |
is(get_type($integer), 'integer', 'integer that is later used as a string is still identified as a integer'); |
| 195 |
ok(is_type('integer', $integer), 'integer that is later used as a string is still an integer'); |
| 196 |
ok(is_type('number', $integer), 'integer that is later used as a string is still a number'); |
| 197 |
ok(!is_type('string', $integer), 'integer that is later used as a string is not a string'); |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
subtest 'numbers' => sub { |
| 202 |
my $number = dualvar(5.1, 'five'); |
| 203 |
is(get_type($number), 'ambiguous type', 'dualvar numbers are ambiguous in get_type'); |
| 204 |
ok(!is_type($_, $number), "dualvar numbers are not ${_}s") foreach qw(integer number string); |
| 205 |
|
| 206 |
$number = 5.1; |
| 207 |
()= sprintf('%s', $number); |
| 208 |
|
| 209 |
is(get_type($number), 'number', 'number that is later used as a string is still identified as a number'); |
| 210 |
ok(!is_type('integer', $number), 'number that is later used as a string is not an integer'); |
| 211 |
ok(is_type('number', $number), 'number that is later used as a string is still a number'); |
| 212 |
ok(!is_type('string', $number), 'number that is later used as a string is not a string'); |
| 213 |
}; |
| 214 |
|
| 215 |
subtest 'strings' => sub { |
| 216 |
my $string = dualvar(5.1, 'five'); |
| 217 |
is(get_type($string), 'ambiguous type', 'dualvar strings are ambiguous in get_type'); |
| 218 |
ok(!is_type($_, $string), "dualvar strings are not ${_}s") foreach qw(integer number string); |
| 219 |
|
| 220 |
$string = '5'; |
| 221 |
()= 0+$string; |
| 222 |
|
| 223 |
is(get_type($string), 'string', 'string that is later used as an integer is still identified as a string'); |
| 224 |
|
| 225 |
# legacy behaviour |
| 226 |
SKIP: { |
| 227 |
skip 'on perls < 5.35.9, reading the string form of an integer value sets the flag SVf_POK', 1 |
| 228 |
if "$]" >= 5.035009; |
| 229 |
ok(is_type('integer', $string), 'older perls only: string that is later used as an integer becomes an integer'); |
| 230 |
ok(is_type('number', $string), 'older perls only: string that is later used as an integer becomes a number'); |
| 231 |
} |
| 232 |
|
| 233 |
# modern behaviour |
| 234 |
SKIP: { |
| 235 |
skip 'on perls < 5.35.9, reading the integer form of a string value sets the flag SVf_IOK', 1 |
| 236 |
if "$]" < 5.035009; |
| 237 |
ok(!is_type('integer', $string), 'string that is later used as an integer is not an integer'); |
| 238 |
ok(!is_type('number', $string), 'string that is later used as an integer is not a number'); |
| 239 |
} |
| 240 |
|
| 241 |
ok(is_type('string', $string), 'string that is later used as an integer is still a string'); |
| 242 |
|
| 243 |
$string = '5.1'; |
| 244 |
()= 0+$string; |
| 245 |
|
| 246 |
is(get_type($string), 'string', 'string that is later used as a number is still identified as a string'); |
| 247 |
ok(!is_type('integer', $string), 'string that is later used as a number is not an integer'); |
| 248 |
|
| 249 |
# legacy behaviour |
| 250 |
SKIP: { |
| 251 |
skip 'on perls < 5.35.9, reading the string form of an integer value sets the flag SVf_POK', 1 |
| 252 |
if "$]" >= 5.035009; |
| 253 |
ok(is_type('number', $string), 'older perls only: string that is later used as a number becomes a number'); |
| 254 |
} |
| 255 |
|
| 256 |
# modern behaviour |
| 257 |
SKIP: { |
| 258 |
skip 'on perls < 5.35.9, reading the numeric form of a string value sets the flag SVf_NOK', 1 |
| 259 |
if "$]" < 5.035009; |
| 260 |
ok(!is_type('number', $string), 'string that is later used as a number is not a number'); |
| 261 |
} |
| 262 |
|
| 263 |
ok(is_type('string', $string), 'string that is later used as a number is still a string'); |
| 264 |
}; |
| 265 |
}; |
| 266 |
|
| 267 |
subtest 'is_type and get_type for references' => sub { |
| 268 |
foreach my $test ( |
| 269 |
[ \1, 'reference to SCALAR' ], |
| 270 |
[ \\2, 'reference to REF' ], |
| 271 |
[ sub { 1 }, 'reference to CODE' ], |
| 272 |
[ \*stdout, 'reference to GLOB' ], |
| 273 |
[ \substr('a', '1'), 'reference to LVALUE' ], |
| 274 |
[ \v1.2.3, 'reference to VSTRING' ], |
| 275 |
[ qr/foo/, 'Regexp' ], |
| 276 |
[ *STDIN{IO}, 'IO::File' ], |
| 277 |
[ bless({}, 'Foo'), 'Foo' ], |
| 278 |
[ bless({}, '0'), '0' ], |
| 279 |
) { |
| 280 |
is(get_type($test->[0]), $test->[1], $test->[1].' type is reported without exception'); |
| 281 |
ok(is_type($test->[1], $test->[0]), 'value is a '.$test->[1]); |
| 282 |
foreach my $type (qw(null object array boolean string number integer)) { |
| 283 |
ok(!is_type($type, $test->[0]), 'value is not a '.$type); |
| 284 |
} |
| 285 |
} |
| 286 |
}; |
| 287 |
|
| 288 |
done_testing; |