aboutsummaryrefslogtreecommitdiff
path: root/src/test/modules/test_json_parser/t/002_inline.pl
blob: e63d36a6a874e5c1a2f1570889464c8cb7a27fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use strict;
use warnings;

use PostgreSQL::Test::Utils;
use Test::More;

use File::Temp qw(tempfile);

sub test
{
	local $Test::Builder::Level = $Test::Builder::Level + 1;

	my ($name, $json, %params) = @_;
	my $exe = "test_json_parser_incremental";
	my $chunk = length($json);

	if ($chunk > 64)
	{
		$chunk = 64;
	}

	my ($fh, $fname) = tempfile(UNLINK => 1);
	print $fh "$json";
	close($fh);

	foreach my $size (reverse(1..$chunk))
	{
		my ($stdout, $stderr) = run_command( [$exe, "-c", $size, $fname] );

		if (defined($params{error}))
		{
			unlike($stdout, qr/SUCCESS/, "$name, chunk size $size: test fails");
			like($stderr, $params{error}, "$name, chunk size $size: correct error output");
		}
		else
		{
			like($stdout, qr/SUCCESS/, "$name, chunk size $size: test succeeds");
			is($stderr, "", "$name, chunk size $size: no error output");
		}
	}
}

test("number", "12345");
test("string", '"hello"');
test("false", "false");
test("true", "true");
test("null", "null");
test("empty object", "{}");
test("empty array", "[]");
test("array with number", "[12345]");
test("array with numbers", "[12345,67890]");
test("array with null", "[null]");
test("array with string", '["hello"]');
test("array with boolean", '[false]');
test("single pair", '{"key": "value"}');
test("heavily nested array", "[" x 3200 . "]" x 3200);
test("serial escapes", '"\\\\\\\\\\\\\\\\"');
test("interrupted escapes", '"\\\\\\"\\\\\\\\\\"\\\\"');
test("whitespace", '     ""     ');

test("unclosed empty object", "{", error => qr/input string ended unexpectedly/);
test("bad key", "{{", error => qr/Expected string or "}", but found "\{"/);
test("bad key", "{{}", error => qr/Expected string or "}", but found "\{"/);
test("numeric key", "{1234: 2}", error => qr/Expected string or "}", but found "1234"/);
test("second numeric key", '{"a": "a", 1234: 2}', error => qr/Expected string, but found "1234"/);
test("unclosed object with pair", '{"key": "value"', error => qr/input string ended unexpectedly/);
test("missing key value", '{"key": }', error => qr/Expected JSON value, but found "}"/);
test("missing colon", '{"key" 12345}', error => qr/Expected ":", but found "12345"/);
test("missing comma", '{"key": 12345 12345}', error => qr/Expected "," or "}", but found "12345"/);
test("overnested array", "[" x 6401, error => qr/maximum permitted depth is 6400/);
test("overclosed array", "[]]", error => qr/Expected end of input, but found "]"/);
test("unexpected token in array", "[ }}} ]", error => qr/Expected array element or "]", but found "}"/);
test("junk punctuation", "[ ||| ]", error => qr/Token "|" is invalid/);
test("missing comma in array", "[123 123]", error => qr/Expected "," or "]", but found "123"/);
test("misspelled boolean", "tru", error => qr/Token "tru" is invalid/);
test("misspelled boolean in array", "[tru]", error => qr/Token "tru" is invalid/);
test("smashed top-level scalar", "12zz", error => qr/Token "12zz" is invalid/);
test("smashed scalar in array", "[12zz]", error => qr/Token "12zz" is invalid/);
test("unknown escape sequence", '"hello\vworld"', error => qr/Escape sequence "\\v" is invalid/);
test("unescaped control", "\"hello\tworld\"", error => qr/Character with value 0x09 must be escaped/);
test("incorrect escape count", '"\\\\\\\\\\\\\\"', error => qr/Token ""\\\\\\\\\\\\\\"" is invalid/);

done_testing();