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