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