ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/t/19_incr.t
Revision: 1.6
Committed: Thu Nov 15 22:35:35 2018 UTC (5 years, 7 months ago) by root
Content type: application/x-troff
Branch: MAIN
CVS Tags: rel-4_0, rel-4_01, rel-4_03, rel-4_02, rel-4_0_00, HEAD
Changes since 1.5: +14 -7 lines
Log Message:
*** empty log message ***

File Contents

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