]> granicus.if.org Git - json-c/commitdiff
Extend test1 and test2 to run using json_object_to_json_string_ext() based on an...
authorEric Haszlakiewicz <erh+git@nimenees.com>
Sat, 28 Apr 2012 19:14:26 +0000 (14:14 -0500)
committerEric Haszlakiewicz <erh+git@nimenees.com>
Sat, 28 Apr 2012 19:14:26 +0000 (14:14 -0500)
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).

14 files changed:
tests/Makefile.am
tests/parse_flags.c [new file with mode: 0644]
tests/parse_flags.h [new file with mode: 0644]
tests/test-defs.sh
tests/test1.c
tests/test1.test
tests/test1Formatted_plain.expected [new file with mode: 0644]
tests/test1Formatted_pretty.expected [new file with mode: 0644]
tests/test1Formatted_spaced.expected [new file with mode: 0644]
tests/test2.c
tests/test2.test
tests/test2Formatted_plain.expected [new file with mode: 0644]
tests/test2Formatted_pretty.expected [new file with mode: 0644]
tests/test2Formatted_spaced.expected [new file with mode: 0644]

index 1de77232de04a81144620e0f0a6db879ddcac409..e2854dd2db20626b1d603fa69bc873c4473b83f6 100644 (file)
@@ -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 (file)
index 0000000..fafabc8
--- /dev/null
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <string.h>
+
+#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 (file)
index 0000000..c5e2f41
--- /dev/null
@@ -0,0 +1,4 @@
+#ifndef __parse_flags_h
+#define __parse_flags_h
+int parse_flags(int argc, char **argv);
+#endif
index b9b0f60da6f9e0d802cad1ccba09d97be0467245..658a75d9e489d6c5c82985987fbde4f968f3c979 100755 (executable)
@@ -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
 
index e1e411d96a4f8b876fdac8053a5a267c44a20704..9802eb10d305f73f549421b472483d21bc6b52d6 100644 (file)
@@ -5,6 +5,7 @@
 #include <assert.h>
 
 #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));
index 6074faca72766823c8b01e39ba5a7c7b6aba47fd..79d2e09a88eeaa531a246dd34f2eeb2cea35922c 100755 (executable)
@@ -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 (file)
index 0000000..65b19ed
--- /dev/null
@@ -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 (file)
index 0000000..f2334c4
--- /dev/null
@@ -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 (file)
index 0000000..6653fe0
--- /dev/null
@@ -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 }
index 5f95565982170e89308c47bbf8a251b4c15a70c9..4a6b66084c070a6e0251b2fee02de795ed3e948f 100644 (file)
@@ -4,14 +4,28 @@
 #include <string.h>
 
 #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);
index cbb38303734e8af92457df54ecca41e5298e5285..d4a4e792b7695fba4d4848fec60d32d72f094a03 100755 (executable)
@@ -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 (file)
index 0000000..cc587e9
--- /dev/null
@@ -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 (file)
index 0000000..8d6d740
--- /dev/null
@@ -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 (file)
index 0000000..0b740a9
--- /dev/null
@@ -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" ] } ] } } }