| 1 |
# copied over from JSON::XS and modified to use JSON |
| 2 |
|
| 3 |
use strict; |
| 4 |
no warnings; |
| 5 |
use Test::More; |
| 6 |
BEGIN { plan tests => 745 }; |
| 7 |
|
| 8 |
BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; } |
| 9 |
|
| 10 |
use JSON; |
| 11 |
|
| 12 |
sub splitter { |
| 13 |
my ($coder, $text) = @_; |
| 14 |
|
| 15 |
# work around hash randomisation bug introduced in 5.18 |
| 16 |
$coder->canonical; |
| 17 |
|
| 18 |
for (0 .. length $text) { |
| 19 |
my $a = substr $text, 0, $_; |
| 20 |
my $b = substr $text, $_; |
| 21 |
|
| 22 |
$coder->incr_parse ($a); |
| 23 |
$coder->incr_parse ($b); |
| 24 |
|
| 25 |
my $data = $coder->incr_parse; |
| 26 |
#ok (defined $data, "split<$a><$b>"); |
| 27 |
ok (defined $data, "split"); |
| 28 |
my $e1 = $coder->encode ($data); |
| 29 |
my $e2 = $coder->encode ($coder->decode ($text)); |
| 30 |
#ok ($e1 eq $e2, "data<$a><$b><$e1><$e2>"); |
| 31 |
#ok ($coder->incr_text =~ /^\s*$/, "tailws<$a><$b>"); |
| 32 |
ok ($e1 eq $e2, "data"); |
| 33 |
ok ($coder->incr_text =~ /^\s*$/, "tailws"); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
splitter +JSON->new->allow_nonref (0), ' ["x\\"","\\u1000\\\\n\\nx",1,{"\\\\" :5 , "": "x"}]'; |
| 38 |
splitter +JSON->new->allow_nonref (0), '[ "x\\"","\\u1000\\\\n\\nx" , 1,{"\\\\ " :5 , "": " x"} ] '; |
| 39 |
splitter +JSON->new->allow_nonref (1), '"test"'; |
| 40 |
splitter +JSON->new->allow_nonref (1), ' "5" '; |
| 41 |
splitter +JSON->new->allow_nonref (1), '-1e5'; |
| 42 |
SKIP: { skip "requires $JSON::BackendModule 3 or newer", 33 if $JSON::BackendModulePP and eval $JSON::BackendModulePP->VERSION < 3; |
| 43 |
splitter +JSON->new->allow_nonref (1), ' 0.00E+00 '; |
| 44 |
} |
| 45 |
|
| 46 |
{ |
| 47 |
my $text = '[5],{"":1} , [ 1,2, 3], {"3":null}'; |
| 48 |
my $coder = JSON->new; |
| 49 |
for (0 .. length $text) { |
| 50 |
my $a = substr $text, 0, $_; |
| 51 |
my $b = substr $text, $_; |
| 52 |
|
| 53 |
$coder->incr_parse ($a); |
| 54 |
$coder->incr_parse ($b); |
| 55 |
|
| 56 |
my $j1 = $coder->incr_parse; ok ($coder->incr_text =~ s/^\s*,//, "cskip1"); |
| 57 |
my $j2 = $coder->incr_parse; ok ($coder->incr_text =~ s/^\s*,//, "cskip2"); |
| 58 |
my $j3 = $coder->incr_parse; ok ($coder->incr_text =~ s/^\s*,//, "cskip3"); |
| 59 |
my $j4 = $coder->incr_parse; ok ($coder->incr_text !~ s/^\s*,//, "cskip4"); |
| 60 |
my $j5 = $coder->incr_parse; ok ($coder->incr_text !~ s/^\s*,//, "cskip5"); |
| 61 |
|
| 62 |
ok ('[5]' eq encode_json($j1), "cjson1"); |
| 63 |
ok ('{"":1}' eq encode_json($j2), "cjson2"); |
| 64 |
ok ('[1,2,3]' eq encode_json($j3), "cjson3"); |
| 65 |
ok ('{"3":null}' eq encode_json($j4), "cjson4"); |
| 66 |
ok (!defined $j5, "cjson5"); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
{ |
| 71 |
my $text = '[x][5]'; |
| 72 |
my $coder = JSON->new; |
| 73 |
$coder->incr_parse ($text); |
| 74 |
ok (!eval { $coder->incr_parse }, "sparse1"); |
| 75 |
ok (!eval { $coder->incr_parse }, "sparse2"); |
| 76 |
$coder->incr_skip; |
| 77 |
ok ('[5]' eq $coder->encode (scalar $coder->incr_parse), "sparse3"); |
| 78 |
} |
| 79 |
|
| 80 |
{ |
| 81 |
my $coder = JSON->new->max_size (5); |
| 82 |
ok (!$coder->incr_parse ("[ "), "incsize1"); |
| 83 |
eval { !$coder->incr_parse ("] ") }; ok ($@ =~ /6 bytes/, "incsize2 $@"); |
| 84 |
} |
| 85 |
|
| 86 |
{ |
| 87 |
my $coder = JSON->new->max_depth (3); |
| 88 |
ok (!$coder->incr_parse ("[[["), "incdepth1"); |
| 89 |
eval { !$coder->incr_parse (" [] ") }; ok ($@ =~ /maximum nesting/, "incdepth2 $@"); |
| 90 |
} |
| 91 |
|
| 92 |
# contributed by yuval kogman, reformatted to fit style |
| 93 |
{ |
| 94 |
my $coder = JSON->new; |
| 95 |
|
| 96 |
my $res = eval { $coder->incr_parse("]") }; |
| 97 |
my $e = $@; # test more clobbers $@, we need it twice |
| 98 |
|
| 99 |
ok (!$res, "unbalanced bracket"); |
| 100 |
ok ($e, "got error"); |
| 101 |
like ($e, qr/malformed/, "malformed json string error"); |
| 102 |
|
| 103 |
$coder->incr_skip; |
| 104 |
|
| 105 |
is_deeply (eval { $coder->incr_parse("[42]") }, [42], "valid data after incr_skip"); |
| 106 |
} |
| 107 |
|
| 108 |
|