|
0 |
use strict;
|
|
1 |
use warnings;
|
|
2 |
use Test::More;
|
|
3 |
use JSON::PP;
|
|
4 |
|
|
5 |
plan tests => 19 * 3 + 1 * 6;
|
|
6 |
|
|
7 |
sub run_test {
|
|
8 |
my ($input, $sub) = @_;
|
|
9 |
$sub->($input);
|
|
10 |
}
|
|
11 |
|
|
12 |
run_test('{"one": 1}', sub {
|
|
13 |
my $input = shift;
|
|
14 |
my $coder = JSON::PP->new;
|
|
15 |
my $res = eval { $coder->incr_parse($input) };
|
|
16 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
17 |
ok ($res, "curly braces okay -- '$input'");
|
|
18 |
ok (!$e, "no error -- '$input'");
|
|
19 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error");
|
|
20 |
});
|
|
21 |
|
|
22 |
run_test('{"one": 1]', sub {
|
|
23 |
my $input = shift;
|
|
24 |
my $coder = JSON::PP->new;
|
|
25 |
my $res = eval { $coder->incr_parse($input) };
|
|
26 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
27 |
ok (!$res, "unbalanced curly braces -- '$input'");
|
|
28 |
ok ($e, "got error -- '$input'");
|
|
29 |
like ($e, qr/, or \} expected while parsing object\/hash/, "'} expected' json string error");
|
|
30 |
});
|
|
31 |
|
|
32 |
run_test('"', sub {
|
|
33 |
my $input = shift;
|
|
34 |
my $coder = JSON::PP->new;
|
|
35 |
my $res = eval { $coder->incr_parse($input) };
|
|
36 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
37 |
ok (!$res, "truncated input='$input'");
|
|
38 |
ok (!$e, "no error for input='$input'");
|
|
39 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
40 |
});
|
|
41 |
|
|
42 |
run_test('{', sub {
|
|
43 |
my $input = shift;
|
|
44 |
my $coder = JSON::PP->new;
|
|
45 |
my $res = eval { $coder->incr_parse($input) };
|
|
46 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
47 |
ok (!$res, "truncated input='$input'");
|
|
48 |
ok (!$e, "no error for input='$input'");
|
|
49 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
50 |
});
|
|
51 |
|
|
52 |
run_test('[', sub {
|
|
53 |
my $input = shift;
|
|
54 |
my $coder = JSON::PP->new;
|
|
55 |
my $res = eval { $coder->incr_parse($input) };
|
|
56 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
57 |
ok (!$res, "truncated input='$input'");
|
|
58 |
ok (!$e, "no error for input='$input'");
|
|
59 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
60 |
});
|
|
61 |
|
|
62 |
run_test('}', sub {
|
|
63 |
my $input = shift;
|
|
64 |
my $coder = JSON::PP->new;
|
|
65 |
my $res = eval { $coder->incr_parse($input) };
|
|
66 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
67 |
ok (!$res, "truncated input='$input'");
|
|
68 |
ok ($e, "no error for input='$input'");
|
|
69 |
like ($e, qr/malformed JSON string/, "'malformed JSON string' json string error for input='$input'");
|
|
70 |
});
|
|
71 |
|
|
72 |
run_test(']', sub {
|
|
73 |
my $input = shift;
|
|
74 |
my $coder = JSON::PP->new;
|
|
75 |
my $res = eval { $coder->incr_parse($input) };
|
|
76 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
77 |
ok (!$res, "truncated input='$input'");
|
|
78 |
ok ($e, "no error for input='$input'");
|
|
79 |
like ($e, qr/malformed JSON string/, "'malformed JSON string' json string error for input='$input'");
|
|
80 |
});
|
|
81 |
|
|
82 |
run_test('1', sub {
|
|
83 |
my $input = shift;
|
|
84 |
my $coder = JSON::PP->new;
|
|
85 |
my $res = eval { $coder->incr_parse($input) };
|
|
86 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
87 |
ok ($res, "truncated input='$input'");
|
|
88 |
ok (!$e, "no error for input='$input'");
|
|
89 |
unlike ($e, qr/malformed JSON string/, "'malformed JSON string' json string error for input='$input'");
|
|
90 |
});
|
|
91 |
|
|
92 |
run_test('1', sub {
|
|
93 |
my $input = shift;
|
|
94 |
my $coder = JSON::PP->new->allow_nonref(0);
|
|
95 |
my $res = eval { $coder->incr_parse($input) };
|
|
96 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
97 |
ok (!$res, "truncated input='$input'");
|
|
98 |
ok ($e, "no error for input='$input'");
|
|
99 |
like ($e, qr/JSON text must be an object or array/, "'JSON text must be an object or array' json string error for input='$input'");
|
|
100 |
});
|
|
101 |
|
|
102 |
run_test('"1', sub {
|
|
103 |
my $input = shift;
|
|
104 |
my $coder = JSON::PP->new;
|
|
105 |
my $res = eval { $coder->incr_parse($input) };
|
|
106 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
107 |
ok (!$res, "truncated input='$input'");
|
|
108 |
ok (!$e, "no error for input='$input'");
|
|
109 |
unlike ($e, qr/malformed JSON string/, "'malformed JSON string' json string error for input='$input'");
|
|
110 |
});
|
|
111 |
|
|
112 |
run_test('\\', sub {
|
|
113 |
my $input = shift;
|
|
114 |
my $coder = JSON::PP->new;
|
|
115 |
my $res = eval { $coder->incr_parse($input) };
|
|
116 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
117 |
ok (!$res, "truncated input='$input'");
|
|
118 |
ok ($e, "no error for input='$input'");
|
|
119 |
like ($e, qr/malformed JSON string/, "'malformed JSON string' json string error for input='$input'");
|
|
120 |
});
|
|
121 |
|
|
122 |
run_test('{"one": "', sub {
|
|
123 |
my $input = shift;
|
|
124 |
my $coder = JSON::PP->new;
|
|
125 |
my $res = eval { $coder->incr_parse($input) };
|
|
126 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
127 |
ok (!$res, "truncated input='$input'");
|
|
128 |
ok (!$e, "no error for input='$input'");
|
|
129 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
130 |
});
|
|
131 |
|
|
132 |
run_test('{"one": {', sub {
|
|
133 |
my $input = shift;
|
|
134 |
my $coder = JSON::PP->new;
|
|
135 |
my $res = eval { $coder->incr_parse($input) };
|
|
136 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
137 |
ok (!$res, "truncated input='$input'");
|
|
138 |
ok (!$e, "no error for input='$input'");
|
|
139 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
140 |
});
|
|
141 |
|
|
142 |
run_test('{"one": [', sub {
|
|
143 |
my $input = shift;
|
|
144 |
my $coder = JSON::PP->new;
|
|
145 |
my $res = eval { $coder->incr_parse($input) };
|
|
146 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
147 |
ok (!$res, "truncated input='$input'");
|
|
148 |
ok (!$e, "no error for input='$input'");
|
|
149 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
150 |
});
|
|
151 |
|
|
152 |
run_test('{"one": t', sub {
|
|
153 |
my $input = shift;
|
|
154 |
my $coder = JSON::PP->new;
|
|
155 |
my $res = eval { $coder->incr_parse($input) };
|
|
156 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
157 |
ok (!$res, "truncated input='$input'");
|
|
158 |
ok (!$e, "no error for input='$input'");
|
|
159 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
160 |
});
|
|
161 |
|
|
162 |
run_test('{"one": \\', sub {
|
|
163 |
my $input = shift;
|
|
164 |
my $coder = JSON::PP->new;
|
|
165 |
my $res = eval { $coder->incr_parse($input) };
|
|
166 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
167 |
ok (!$res, "truncated input='$input'");
|
|
168 |
ok (!$e, "no error for input='$input'");
|
|
169 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
170 |
});
|
|
171 |
|
|
172 |
run_test('{"one": ', sub {
|
|
173 |
my $input = shift;
|
|
174 |
my $coder = JSON::PP->new;
|
|
175 |
my $res = eval { $coder->incr_parse($input) };
|
|
176 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
177 |
ok (!$res, "truncated input='$input'");
|
|
178 |
ok (!$e, "no error for input='$input'");
|
|
179 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
180 |
});
|
|
181 |
|
|
182 |
run_test('{"one": 1', sub {
|
|
183 |
my $input = shift;
|
|
184 |
my $coder = JSON::PP->new;
|
|
185 |
my $res = eval { $coder->incr_parse($input) };
|
|
186 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
187 |
ok (!$res, "truncated input='$input'");
|
|
188 |
ok (!$e, "no error for input='$input'");
|
|
189 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
190 |
});
|
|
191 |
|
|
192 |
run_test('{"one": {"two": 2', sub {
|
|
193 |
my $input = shift;
|
|
194 |
my $coder = JSON::PP->new;
|
|
195 |
my $res = eval { $coder->incr_parse($input) };
|
|
196 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
197 |
ok (!$res, "truncated '$input'");
|
|
198 |
ok (!$e, "no error -- '$input'");
|
|
199 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error -- $input");
|
|
200 |
});
|
|
201 |
|
|
202 |
# Test Appending Closing '}' Curly Bracket
|
|
203 |
run_test('{"one": 1', sub {
|
|
204 |
my $input = shift;
|
|
205 |
my $coder = JSON::PP->new;
|
|
206 |
my $res = eval { $coder->incr_parse($input) };
|
|
207 |
my $e = $@; # test more clobbers $@, we need it twice
|
|
208 |
ok (!$res, "truncated input='$input'");
|
|
209 |
ok (!$e, "no error for input='$input'");
|
|
210 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input'");
|
|
211 |
|
|
212 |
$res = eval { $coder->incr_parse('}') };
|
|
213 |
$e = $@; # test more clobbers $@, we need it twice
|
|
214 |
ok ($res, "truncated input='$input' . '}'");
|
|
215 |
ok (!$e, "no error for input='$input' . '}'");
|
|
216 |
unlike ($e, qr/, or \} expected while parsing object\/hash/, "No '} expected' json string error for input='$input' . '}'");
|
|
217 |
});
|