1#!/usr/bin/perl -w
2
3# copied over from JSON::PP::XS and modified to use JSON::PP
4
5use strict;
6
7use Test::More;
8BEGIN { plan tests => 697 };
9BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
10
11
12use JSON::PP;
13
14if ( $] >= 5.006 ) {
15
16eval <<'TEST' or die "Failed to eval test code for version $]: $@";
17
18sub splitter {
19   my ($coder, $text) = @_;
20
21   $coder->canonical(1) if $] >= 5.017009;
22
23   for (0 .. length $text) {
24      my $a = substr $text, 0, $_;
25      my $b = substr $text, $_;
26
27      $coder->incr_parse ($a);
28      $coder->incr_parse ($b);
29
30      my $data = $coder->incr_parse;
31      ok ($data);
32      is ($coder->encode ($data), $coder->encode ($coder->decode ($text)), "data");
33      ok ($coder->incr_text =~ /^\s*$/, "tailws");
34   }
35}
36
37
38
39splitter +JSON::PP->new              , '  ["x\\"","\\u1000\\\\n\\nx",1,{"\\\\" :5 , "": "x"}]';
40splitter +JSON::PP->new              , '[ "x\\"","\\u1000\\\\n\\nx" , 1,{"\\\\ " :5 , "": " x"} ] ';
41splitter +JSON::PP->new->allow_nonref, '"test"';
42splitter +JSON::PP->new->allow_nonref, ' "5" ';
43
44
45
46{
47   my $text = '[5],{"":1} , [ 1,2, 3], {"3":null}';
48   my $coder = new JSON::PP;
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 = new JSON::PP;
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
801
81TEST
82
83
84}
85else {
86
87
88eval <<'TEST' or die "Failed to eval test code for version $]: $@";
89
90my $incr_text;
91
92sub splitter {
93   my ($coder, $text) = @_;
94
95   for (0 .. length $text) {
96      my $a = substr $text, 0, $_;
97      my $b = substr $text, $_;
98
99      $coder->incr_parse ($a);
100      $coder->incr_parse ($b);
101
102      my $data = $coder->incr_parse;
103      ok ($data);
104      ok ($coder->encode ($data) eq $coder->encode ($coder->decode ($text)), "data");
105      ok (($incr_text = $coder->incr_text) =~ /^\s*$/, "tailws");
106   }
107}
108
109splitter +JSON::PP->new              , '  ["x\\"","\\u1000\\\\n\\nx",1,{"\\\\" :5 , "": "x"}]';
110splitter +JSON::PP->new              , '[ "x\\"","\\u1000\\\\n\\nx" , 1,{"\\\\ " :5 , "": " x"} ] ';
111splitter +JSON::PP->new->allow_nonref, '"test"';
112splitter +JSON::PP->new->allow_nonref, ' "5" ';
113
114
115{
116   my $text = '[5],{"":1} , [ 1,2, 3], {"3":null}';
117   my $coder = new JSON::PP;
118   for (0 .. length $text) {
119      my $a = substr $text, 0, $_;
120      my $b = substr $text, $_;
121
122      $coder->incr_parse ($a);
123      $coder->incr_parse ($b);
124
125      my $j1 = $coder->incr_parse; ok ( $coder->incr_text(  ($incr_text = $coder->incr_text) =~ s/^\s*,// and $incr_text ), "cskip1");
126      my $j2 = $coder->incr_parse; ok ( $coder->incr_text(  ($incr_text = $coder->incr_text) =~ s/^\s*,// and $incr_text ), "cskip2");
127      my $j3 = $coder->incr_parse; ok ( $coder->incr_text(  ($incr_text = $coder->incr_text) =~ s/^\s*,// and $incr_text ), "cskip3");
128      my $j4 = $coder->incr_parse; ok (($incr_text = $coder->incr_text) !~ s/^\s*,//, "cskip4");
129      my $j5 = $coder->incr_parse; ok (($incr_text = $coder->incr_text) !~ s/^\s*,//, "cskip5");
130
131      ok ('[5]' eq encode_json $j1, "cjson1");
132      ok ('{"":1}' eq encode_json $j2, "cjson2");
133      ok ('[1,2,3]' eq encode_json $j3, "cjson3");
134      ok ('{"3":null}' eq encode_json $j4, "cjson4");
135      ok (!defined $j5, "cjson5");
136   }
137}
138
139{
140   my $text = '[x][5]';
141   my $coder = new JSON::PP;
142   $coder->incr_parse ($text);
143   ok (!eval { $coder->incr_parse }, "sparse1");
144   ok (!eval { $coder->incr_parse }, "sparse2");
145   $coder->incr_skip;
146   ok ('[5]' eq $coder->encode (scalar $coder->incr_parse), "sparse3");
147}
148
149
150TEST
151
152} # for 5.005
153
154
155
156
157{
158   my $coder = JSON::PP->new->max_size (5);
159   ok (!$coder->incr_parse ("[    "), "incsize1");
160   eval q{ !$coder->incr_parse ("]  ") }; ok ($@ =~ /6 bytes/, "incsize2 $@");
161}
162
163{
164   my $coder = JSON::PP->new->max_depth (3);
165   ok (!$coder->incr_parse ("[[["), "incdepth1");
166   eval q{ !$coder->incr_parse (" [] ") }; ok ($@ =~ /maximum nesting/, "incdepth2 $@");
167}
168
169{
170   my $coder = JSON::PP->new;
171
172   my $res = eval { $coder->incr_parse("]") };
173   my $e = $@; # test more clobbers $@, we need it twice
174
175   ok(!$res, "unbalanced bracket" );
176   ok($e, "got error");
177   like( $e, qr/malformed/, "malformed json string error" );
178
179   $coder->incr_skip;
180
181   is_deeply(eval { $coder->incr_parse("[42]") }, [42], "valid data after incr_skip");
182}
183
184