diff options
| author | Andrew Dunstan <andrew@dunslane.net> | 2024-04-23 15:27:40 -0400 |
|---|---|---|
| committer | Andrew Dunstan <andrew@dunslane.net> | 2024-04-23 15:32:06 -0400 |
| commit | ba3e6e2bca97df14920375b0a1ebf4eab95b78b5 (patch) | |
| tree | ee3e960a3d3ae2ff28dc719f5aa86aeea8b2173b /src/test/modules/test_json_parser/t | |
| parent | b7d35d393edbe2d4333dde81496e8a362abc85bd (diff) | |
Post review fixes for test_json_parser test module
. Add missing copytight notices
. improve code coverage
. put work files in a temp directory in the standard location
. improve error checking in C code
. indent perl files with perltidy
. add some comments
per comments from Michael Paquier
Discussion: https://postgr.es/m/ZiC3-cdFys4-6xSk@paquier.xyz
Diffstat (limited to 'src/test/modules/test_json_parser/t')
4 files changed, 106 insertions, 35 deletions
diff --git a/src/test/modules/test_json_parser/t/001_test_json_parser_incremental.pl b/src/test/modules/test_json_parser/t/001_test_json_parser_incremental.pl index 24f665c0218..30506be0339 100644 --- a/src/test/modules/test_json_parser/t/001_test_json_parser_incremental.pl +++ b/src/test/modules/test_json_parser/t/001_test_json_parser_incremental.pl @@ -1,4 +1,9 @@ +# Copyright (c) 2024, PostgreSQL Global Development Group + +# Test the incremental (table-driven) json parser. + + use strict; use warnings; @@ -6,15 +11,19 @@ use PostgreSQL::Test::Utils; use Test::More; use FindBin; -use File::Temp qw(tempfile); - my $test_file = "$FindBin::RealBin/../tiny.json"; my $exe = "test_json_parser_incremental"; +# Test the usage error +my ($stdout, $stderr) = run_command([ $exe, "-c", 10 ]); +like($stderr, qr/Usage:/, 'error message if not enough arguments'); + +# Test that we get success for small chunk sizes from 64 down to 1. + for (my $size = 64; $size > 0; $size--) { - my ($stdout, $stderr) = run_command( [$exe, "-c", $size, $test_file] ); + ($stdout, $stderr) = run_command([ $exe, "-c", $size, $test_file ]); like($stdout, qr/SUCCESS/, "chunk size $size: test succeeds"); is($stderr, "", "chunk size $size: no error output"); diff --git a/src/test/modules/test_json_parser/t/002_inline.pl b/src/test/modules/test_json_parser/t/002_inline.pl index e63d36a6a87..b95cb6b16a9 100644 --- a/src/test/modules/test_json_parser/t/002_inline.pl +++ b/src/test/modules/test_json_parser/t/002_inline.pl @@ -1,3 +1,9 @@ + +# Copyright (c) 2021-2024, PostgreSQL Global Development Group + +# Test success or failure of the incremental (table-driven) JSON parser +# for a variety of small inputs. + use strict; use warnings; @@ -6,6 +12,8 @@ use Test::More; use File::Temp qw(tempfile); +my $dir = PostgreSQL::Test::Utils::tempdir; + sub test { local $Test::Builder::Level = $Test::Builder::Level + 1; @@ -14,27 +22,32 @@ sub test my $exe = "test_json_parser_incremental"; my $chunk = length($json); + # Test the input with chunk sizes from max(input_size, 64) down to 1 + if ($chunk > 64) { $chunk = 64; } - my ($fh, $fname) = tempfile(UNLINK => 1); + my ($fh, $fname) = tempfile(DIR => $dir); print $fh "$json"; close($fh); - foreach my $size (reverse(1..$chunk)) + foreach my $size (reverse(1 .. $chunk)) { - my ($stdout, $stderr) = run_command( [$exe, "-c", $size, $fname] ); + 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"); + 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"); + like($stdout, qr/SUCCESS/, + "$name, chunk size $size: test succeeds"); is($stderr, "", "$name, chunk size $size: no error output"); } } @@ -58,26 +71,60 @@ test("serial escapes", '"\\\\\\\\\\\\\\\\"'); test("interrupted escapes", '"\\\\\\"\\\\\\\\\\"\\\\"'); test("whitespace", ' "" '); -test("unclosed empty object", "{", error => qr/input string ended unexpectedly/); +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("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("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/); +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(); diff --git a/src/test/modules/test_json_parser/t/003_test_semantic.pl b/src/test/modules/test_json_parser/t/003_test_semantic.pl index 7b4941b7f9f..7d3e07e750c 100644 --- a/src/test/modules/test_json_parser/t/003_test_semantic.pl +++ b/src/test/modules/test_json_parser/t/003_test_semantic.pl @@ -1,3 +1,9 @@ + +# Copyright (c) 2021-2024, PostgreSQL Global Development Group + +# Test the incremental JSON parser with semantic routines, and compare the +# output with the expected output. + use strict; use warnings; @@ -12,17 +18,18 @@ my $test_out = "$FindBin::RealBin/../tiny.out"; my $exe = "test_json_parser_incremental"; -my ($stdout, $stderr) = run_command( [$exe, "-s", $test_file] ); +my ($stdout, $stderr) = run_command([ $exe, "-s", $test_file ]); is($stderr, "", "no error output"); -my ($fh, $fname) = tempfile(); +my $dir = PostgreSQL::Test::Utils::tempdir; +my ($fh, $fname) = tempfile(DIR => $dir); -print $fh $stdout,"\n"; +print $fh $stdout, "\n"; close($fh); -($stdout, $stderr) = run_command(["diff", "-u", $fname, $test_out]); +($stdout, $stderr) = run_command([ "diff", "-u", $fname, $test_out ]); is($stdout, "", "no output diff"); is($stderr, "", "no diff error"); diff --git a/src/test/modules/test_json_parser/t/004_test_parser_perf.pl b/src/test/modules/test_json_parser/t/004_test_parser_perf.pl index ec322c13912..c61fdf898a9 100644 --- a/src/test/modules/test_json_parser/t/004_test_parser_perf.pl +++ b/src/test/modules/test_json_parser/t/004_test_parser_perf.pl @@ -1,4 +1,11 @@ +# Copyright (c) 2021-2024, PostgreSQL Global Development Group + +# Test the JSON parser performance tester. Here we are just checking that +# the performance tester can run, both with the standard parser and the +# incremental parser. An actual performance test will run with thousands +# of iterations onstead of just one. + use strict; use warnings; @@ -14,21 +21,22 @@ my $exe = "test_json_parser_perf"; my $contents = slurp_file($test_file); -my ($fh, $fname) = tempfile(UNLINK => 1); +my $dir = PostgreSQL::Test::Utils::tempdir; +my ($fh, $fname) = tempfile(DIR => $dir); # repeat the input json file 50 times in an array -print $fh, '[', $contents , ",$contents" x 49 , ']'; +print $fh, '[', $contents, ",$contents" x 49, ']'; close($fh); # but only do one iteration -my ($result) = run_log([ $exe, "1", $fname ] ); +my ($result) = run_log([ $exe, "1", $fname ]); -ok($result == 0, "perf test runs with RD parser"); +ok($result == 0, "perf test runs with recursive descent parser"); -$result = run_log([ $exe, "-i" , "1", $fname ]); +$result = run_log([ $exe, "-i", "1", $fname ]); ok($result == 0, "perf test runs with table driven parser"); |
