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