From: Eric Haszlakiewicz Date: Sat, 28 Apr 2012 19:14:26 +0000 (-0500) Subject: Extend test1 and test2 to run using json_object_to_json_string_ext() based on an... X-Git-Tag: json-c-0.10-20120530~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4c7f38eb9b4cb09aef7a8c6e73bf49d602cd0476;p=json-c Extend test1 and test2 to run using json_object_to_json_string_ext() based on an additional command line parameter. Extend the run_output_test() function so we actually can pass command line parameters and so we can support different output files for the same test executable. Also provide some hints about what to do if a test fails (i.e. set VERBOSE=1). --- diff --git a/tests/Makefile.am b/tests/Makefile.am index 1de7723..e2854dd 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -3,12 +3,26 @@ include ../Makefile.am.inc LIBJSON_LA=$(top_builddir)/libjson.la -check_PROGRAMS = test1 test2 test4 test_parse_int64 test_null test_cast test_parse +check_PROGRAMS = test1 test1Formatted +check_PROGRAMS += test2 test2Formatted +check_PROGRAMS += test4 +check_PROGRAMS += test_parse_int64 +check_PROGRAMS += test_null +check_PROGRAMS += test_cast +check_PROGRAMS += test_parse test1_LDADD = $(LIBJSON_LA) +test1Formatted_LDADD= $(LIBJSON_LA) +test1Formatted_SOURCES = test1.c parse_flags.c +test1Formatted_CPPFLAGS = -DTEST_FORMATTED + test2_LDADD = $(LIBJSON_LA) +test2Formatted_LDADD= $(LIBJSON_LA) +test2Formatted_SOURCES = test2.c parse_flags.c +test2Formatted_CPPFLAGS = -DTEST_FORMATTED + test4_LDADD = $(LIBJSON_LA) test_parse_int64_LDADD = $(LIBJSON_LA) diff --git a/tests/parse_flags.c b/tests/parse_flags.c new file mode 100644 index 0000000..fafabc8 --- /dev/null +++ b/tests/parse_flags.c @@ -0,0 +1,42 @@ +#include +#include + +#include "json.h" +#include "parse_flags.h" + +static struct { + const char *arg; + int flag; +} format_args[] = { + { "plain", JSON_C_TO_STRING_PLAIN }, + { "spaced", JSON_C_TO_STRING_SPACED }, + { "pretty", JSON_C_TO_STRING_PRETTY }, +}; + +#ifndef NELEM +#define NELEM(x) (sizeof(x) / sizeof(&x[0])) +#endif + +int parse_flags(int argc, char **argv) +{ + int arg_idx; + int sflags = 0; + for (arg_idx = 1; arg_idx < argc ; arg_idx++) + { + int jj; + for (jj = 0; jj < NELEM(format_args); jj++) + { + if (strcasecmp(argv[arg_idx], format_args[jj].arg) == 0) + { + sflags |= format_args[jj].flag; + break; + } + } + if (jj == NELEM(format_args)) + { + printf("Unknown arg: %s\n", argv[arg_idx]); + exit(1); + } + } + return sflags; +} diff --git a/tests/parse_flags.h b/tests/parse_flags.h new file mode 100644 index 0000000..c5e2f41 --- /dev/null +++ b/tests/parse_flags.h @@ -0,0 +1,4 @@ +#ifndef __parse_flags_h +#define __parse_flags_h +int parse_flags(int argc, char **argv); +#endif diff --git a/tests/test-defs.sh b/tests/test-defs.sh index b9b0f60..658a75d 100755 --- a/tests/test-defs.sh +++ b/tests/test-defs.sh @@ -26,22 +26,24 @@ top_builddir=${top_builddir}/tests progname=`echo "$0" | sed 's,^.*/,,'` testname=`echo "$progname" | sed 's,-.*$,,'` testsubdir=${testsubdir-testSubDir} +testsubdir=${testsubdir}/${progname} # User can set VERBOSE to cause output redirection case "$VERBOSE" in [Nn]|[Nn][Oo]|0|"") VERBOSE=0 - exec > /dev/null 2>&1 + exec > /dev/null ;; [Yy]|[Yy][Ee][Ss]) VERBOSE=1 ;; esac -rm -rf "$testsubdir/$progname" > /dev/null 2>&1 -mkdir -p "$testsubdir/$progname" -cd "$testsubdir/$progname" \ - || { echo "Cannot make or change into $testsubdir/$progname"; exit 1; } +rm -rf "$testsubdir" > /dev/null 2>&1 +mkdir -p "$testsubdir" +CURDIR=$(pwd) +cd "$testsubdir" \ + || { echo "Cannot make or change into $testsubdir"; exit 1; } echo "=== Running test $progname" @@ -68,44 +70,55 @@ fi # run_output_test() { + if [ "$1" = "-o" ] ; then + TEST_OUTPUT="$2" + shift + shift + fi TEST_COMMAND="$1" + shift + if [ -z "${TEST_OUTPUT}" ] ; then + TEST_OUTPUT=${TEST_COMMAND} + fi - REDIR_OUTPUT="> \"${TEST_COMMAND}.out\"" + REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\"" if [ $VERBOSE -gt 1 ] ; then - REDIR_OUTPUT="| tee \"${TEST_COMMAND}.out\"" + REDIR_OUTPUT="| tee \"${TEST_OUTPUT}.out\"" fi if [ $use_valgrind -eq 1 ] ; then eval valgrind --tool=memcheck \ --trace-children=yes \ --demangle=yes \ - --log-file=vg.out \ + --log-file="${TEST_OUTPUT}.vg.out" \ --leak-check=full \ --show-reachable=yes \ --run-libc-freeres=yes \ - "\"${top_builddir}/${TEST_COMMAND}\"" ${REDIR_OUTPUT} + "\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT} err=$? else - eval "\"${top_builddir}/${TEST_COMMAND}"\" ${REDIR_OUTPUT} + eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT} err=$? fi if [ $err -ne 0 ] ; then - echo "ERROR: ${TEST_COMMAND} exited with non-zero exit status: $err" 1>&2 + echo "ERROR: \"${TEST_COMMAND} $@\" exited with non-zero exit status: $err" 1>&2 fi if [ $use_valgrind -eq 1 ] ; then - if ! tail -1 "vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then + if ! tail -1 "${TEST_OUTPUT}.vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then echo "ERROR: valgrind found errors during execution:" 1>&2 - cat vg.out + cat "${TEST_OUTPUT}.vg.out" err=1 fi fi - if ! "$CMP" -s "${top_builddir}/${TEST_COMMAND}.expected" "${TEST_COMMAND}.out" ; then - echo "ERROR: ${TEST_COMMAND} failed:" 1>&2 - diff "${top_builddir}/${TEST_COMMAND}.expected" "${TEST_COMMAND}.out" 1>&2 + if ! "$CMP" -s "${top_builddir}/${TEST_OUTPUT}.expected" "${TEST_OUTPUT}.out" ; then + echo "ERROR: \"${TEST_COMMAND} $@\" (${TEST_OUTPUT}) failed (set VERBOSE=1 to see full output):" 1>&2 + (cd "${CURDIR}" ; set -x ; diff "${top_builddir}/${TEST_OUTPUT}.expected" "$testsubdir/${TEST_OUTPUT}.out") + echo "cp \"$testsubdir/${TEST_OUTPUT}.out\" \"${top_builddir}/${TEST_OUTPUT}.expected\"" 1>&2 + err=1 fi diff --git a/tests/test1.c b/tests/test1.c index e1e411d..9802eb1 100644 --- a/tests/test1.c +++ b/tests/test1.c @@ -5,6 +5,7 @@ #include #include "json.h" +#include "parse_flags.h" static int sort_fn (const void *j1, const void *j2) { @@ -29,13 +30,26 @@ static int sort_fn (const void *j1, const void *j2) return i1 - i2; } +#ifdef TEST_FORMATTED +#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags) +#else +/* no special define */ +#endif + int main(int argc, char **argv) { json_object *my_string, *my_int, *my_object, *my_array; int i; +#ifdef TEST_FORMATTED + int sflags = 0; +#endif MC_SET_DEBUG(1); +#ifdef TEST_FORMATTED + sflags = parse_flags(argc, argv); +#endif + my_string = json_object_new_string("\t"); printf("my_string=%s\n", json_object_get_string(my_string)); printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string)); diff --git a/tests/test1.test b/tests/test1.test index 6074fac..79d2e09 100755 --- a/tests/test1.test +++ b/tests/test1.test @@ -9,4 +9,14 @@ fi . "$srcdir/test-defs.sh" run_output_test test1 -exit $? +_err=$? + +for flag in plain spaced pretty ; do + run_output_test -o test1Formatted_${flag} test1Formatted ${flag} + _err2=$? + if [ $_err -eq 0 ] ; then + _err=$_err2 + fi +done + +exit $_err diff --git a/tests/test1Formatted_plain.expected b/tests/test1Formatted_plain.expected new file mode 100644 index 0000000..65b19ed --- /dev/null +++ b/tests/test1Formatted_plain.expected @@ -0,0 +1,35 @@ +my_string= +my_string.to_string()="\t" +my_string=\ +my_string.to_string()="\\" +my_string=foo +my_string.to_string()="foo" +my_int=9 +my_int.to_string()=9 +my_array= + [0]=1 + [1]=2 + [2]=3 + [3]=null + [4]=5 +my_array.to_string()=[1,2,3,null,5] +my_array= + [0]=3 + [1]=1 + [2]=2 + [3]=null + [4]=0 +my_array.to_string()=[3,1,2,null,0] +my_array= + [0]=null + [1]=0 + [2]=1 + [3]=2 + [4]=3 +my_array.to_string()=[null,0,1,2,3] +my_object= + abc: 12 + foo: "bar" + bool0: false + bool1: true +my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true} diff --git a/tests/test1Formatted_pretty.expected b/tests/test1Formatted_pretty.expected new file mode 100644 index 0000000..f2334c4 --- /dev/null +++ b/tests/test1Formatted_pretty.expected @@ -0,0 +1,58 @@ +my_string= +my_string.to_string()="\t" +my_string=\ +my_string.to_string()="\\" +my_string=foo +my_string.to_string()="foo" +my_int=9 +my_int.to_string()=9 +my_array= + [0]=1 + [1]=2 + [2]=3 + [3]=null + [4]=5 +my_array.to_string()=[ + 1, + 2, + 3, + null, + 5 +] +my_array= + [0]=3 + [1]=1 + [2]=2 + [3]=null + [4]=0 +my_array.to_string()=[ + 3, + 1, + 2, + null, + 0 +] +my_array= + [0]=null + [1]=0 + [2]=1 + [3]=2 + [4]=3 +my_array.to_string()=[ + null, + 0, + 1, + 2, + 3 +] +my_object= + abc: 12 + foo: "bar" + bool0: false + bool1: true +my_object.to_string()={ + "abc":12, + "foo":"bar", + "bool0":false, + "bool1":true +} diff --git a/tests/test1Formatted_spaced.expected b/tests/test1Formatted_spaced.expected new file mode 100644 index 0000000..6653fe0 --- /dev/null +++ b/tests/test1Formatted_spaced.expected @@ -0,0 +1,35 @@ +my_string= +my_string.to_string()="\t" +my_string=\ +my_string.to_string()="\\" +my_string=foo +my_string.to_string()="foo" +my_int=9 +my_int.to_string()=9 +my_array= + [0]=1 + [1]=2 + [2]=3 + [3]=null + [4]=5 +my_array.to_string()=[ 1, 2, 3, null, 5 ] +my_array= + [0]=3 + [1]=1 + [2]=2 + [3]=null + [4]=0 +my_array.to_string()=[ 3, 1, 2, null, 0 ] +my_array= + [0]=null + [1]=0 + [2]=1 + [3]=2 + [4]=3 +my_array.to_string()=[ null, 0, 1, 2, 3 ] +my_object= + abc: 12 + foo: "bar" + bool0: false + bool1: true +my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true } diff --git a/tests/test2.c b/tests/test2.c index 5f95565..4a6b660 100644 --- a/tests/test2.c +++ b/tests/test2.c @@ -4,14 +4,28 @@ #include #include "json.h" +#include "parse_flags.h" + +#ifdef TEST_FORMATTED +#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags) +#else +/* no special define */ +#endif int main(int argc, char **argv) { json_object *new_obj; +#ifdef TEST_FORMATTED + int sflags = 0; +#endif MC_SET_DEBUG(1); +#ifdef TEST_FORMATTED + sflags = parse_flags(argc, argv); +#endif + new_obj = json_tokener_parse("/* more difficult test case */ { \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": [ { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": [\"GML\", \"XML\", \"markup\"] } ] } } }"); printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj)); json_object_put(new_obj); diff --git a/tests/test2.test b/tests/test2.test index cbb3830..d4a4e79 100755 --- a/tests/test2.test +++ b/tests/test2.test @@ -9,4 +9,14 @@ fi . "$srcdir/test-defs.sh" run_output_test test2 -exit $? +_err=$? + +for flag in plain spaced pretty ; do + run_output_test -o test2Formatted_${flag} test2Formatted ${flag} + _err2=$? + if [ $_err -eq 0 ] ; then + _err=$_err2 + fi +done + +exit $_err diff --git a/tests/test2Formatted_plain.expected b/tests/test2Formatted_plain.expected new file mode 100644 index 0000000..cc587e9 --- /dev/null +++ b/tests/test2Formatted_plain.expected @@ -0,0 +1 @@ +new_obj.to_string()={"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":[{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML","markup"]}]}}} diff --git a/tests/test2Formatted_pretty.expected b/tests/test2Formatted_pretty.expected new file mode 100644 index 0000000..8d6d740 --- /dev/null +++ b/tests/test2Formatted_pretty.expected @@ -0,0 +1,23 @@ +new_obj.to_string()={ + "glossary":{ + "title":"example glossary", + "GlossDiv":{ + "title":"S", + "GlossList":[ + { + "ID":"SGML", + "SortAs":"SGML", + "GlossTerm":"Standard Generalized Markup Language", + "Acronym":"SGML", + "Abbrev":"ISO 8879:1986", + "GlossDef":"A meta-markup language, used to create markup languages such as DocBook.", + "GlossSeeAlso":[ + "GML", + "XML", + "markup" + ] + } + ] + } + } +} diff --git a/tests/test2Formatted_spaced.expected b/tests/test2Formatted_spaced.expected new file mode 100644 index 0000000..0b740a9 --- /dev/null +++ b/tests/test2Formatted_spaced.expected @@ -0,0 +1 @@ +new_obj.to_string()={ "glossary": { "title": "example glossary", "GlossDiv": { "title": "S", "GlossList": [ { "ID": "SGML", "SortAs": "SGML", "GlossTerm": "Standard Generalized Markup Language", "Acronym": "SGML", "Abbrev": "ISO 8879:1986", "GlossDef": "A meta-markup language, used to create markup languages such as DocBook.", "GlossSeeAlso": [ "GML", "XML", "markup" ] } ] } } }