Add tests for the json_parse cli tool.

This commit is contained in:
Eric Hawicz
2026-06-21 13:44:59 -04:00
parent bb9c1238ad
commit d9d8cab822
4 changed files with 38 additions and 2 deletions
+7 -2
View File
@@ -39,6 +39,7 @@
static int formatted_output = JSON_C_TO_STRING_SPACED;
static int show_output = 1;
static int show_diag = 1;
static int strict_mode = 0;
static int validate_utf8 = 0;
static int tokener_flags = 0;
@@ -56,6 +57,8 @@ static int showobj(struct json_object *new_obj);
static void showmem(void)
{
if (!show_diag)
return;
#ifdef HAVE_GETRUSAGE
struct rusage rusage;
memset(&rusage, 0, sizeof(rusage));
@@ -171,10 +174,11 @@ static void usage(const char *argv0, int exitval, const char *errmsg)
fp = stderr;
if (errmsg != NULL)
fprintf(fp, "ERROR: %s\n\n", errmsg);
fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s] [-u] [filename]\n", argv0);
fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s] [-u] [-N] [filename]\n", argv0);
fprintf(fp, " -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
fprintf(fp, " -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
fprintf(fp, " -n - No output\n");
fprintf(fp, " -N - Omit diagnostic information, such as memory usage\n");
fprintf(fp, " -c - Set JSON_C_TO_STRING_COLOR to colorize the output\n");
fprintf(fp, " -P - Initialize tokener flags to the given value\n");
fprintf(fp, " -s - Parse in strict mode, add flags:\n");
@@ -191,7 +195,7 @@ int main(int argc, char **argv)
{
int opt;
while ((opt = getopt(argc, argv, "cfF:hnP:su")) != -1)
while ((opt = getopt(argc, argv, "cfF:hnNP:su")) != -1)
{
switch (opt)
{
@@ -199,6 +203,7 @@ int main(int argc, char **argv)
case 'f': formatted_output = JSON_C_TO_STRING_PRETTY; break;
case 'F': formatted_output = atoi(optarg); break;
case 'n': show_output = 0; break;
case 'N': show_diag = 0; break;
case 'P': tokener_flags = atoi(optarg); break;
case 's': strict_mode = 1; break;
case 'u': validate_utf8 = 1; break;
+8
View File
@@ -38,6 +38,10 @@ set(ALL_TEST_NAMES
test_visit
test_object_iterator)
set(NOBUILD_TEST_NAMES
test_json_parse_cli
)
if (NOT DISABLE_JSON_POINTER)
set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_json_pointer)
set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_safe_json_pointer_set)
@@ -46,6 +50,10 @@ if (NOT DISABLE_JSON_POINTER)
endif()
endif()
foreach(TESTNAME ${NOBUILD_TEST_NAMES})
add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test)
endforeach(TESTNAME)
foreach(TESTNAME ${ALL_TEST_NAMES})
add_executable(${TESTNAME} ${TESTNAME}.c)
+2
View File
@@ -0,0 +1,2 @@
Successfully parsed object from stdin
Failed at offset 71: invalid utf-8 string: char=0x00
+21
View File
@@ -0,0 +1,21 @@
#!/bin/sh
# Common definitions
if test -z "$srcdir"; then
srcdir="${0%/*}"
test "$srcdir" = "$0" && srcdir=.
test -z "$srcdir" && srcdir=.
fi
. "$srcdir/test-defs.sh"
set -x -v
echo -n '"tenant=blue;note=CANARY_STACK_WINDOW_2026;status=ok"' > file1.dat
(printf '"' ; printf 'A%.0s' $(seq 1 16) ; echo 'e2' | xxd -r -p) > file2.dat
TESTNAME="${0##*/}"
TESTNAME="${TESTNAME%.test}"
( cat file1.dat ; cat file2.dat ) | run_output_test -o "${TESTNAME}" --exit 1 ../apps/json_parse -u -N -n -
_err=$?
exit $_err