Browse Source

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).
tags/json-c-0.10-20120530
Eric Haszlakiewicz 13 years ago
parent
commit
4c7f38eb9b
14 changed files with 293 additions and 19 deletions
  1. +15
    -1
      tests/Makefile.am
  2. +42
    -0
      tests/parse_flags.c
  3. +4
    -0
      tests/parse_flags.h
  4. +29
    -16
      tests/test-defs.sh
  5. +14
    -0
      tests/test1.c
  6. +11
    -1
      tests/test1.test
  7. +35
    -0
      tests/test1Formatted_plain.expected
  8. +58
    -0
      tests/test1Formatted_pretty.expected
  9. +35
    -0
      tests/test1Formatted_spaced.expected
  10. +14
    -0
      tests/test2.c
  11. +11
    -1
      tests/test2.test
  12. +1
    -0
      tests/test2Formatted_plain.expected
  13. +23
    -0
      tests/test2Formatted_pretty.expected
  14. +1
    -0
      tests/test2Formatted_spaced.expected

+ 15
- 1
tests/Makefile.am View 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)


+ 42
- 0
tests/parse_flags.c View File

@@ -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;
}

+ 4
- 0
tests/parse_flags.h View File

@@ -0,0 +1,4 @@
#ifndef __parse_flags_h
#define __parse_flags_h
int parse_flags(int argc, char **argv);
#endif

+ 29
- 16
tests/test-defs.sh View File

@@ -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



+ 14
- 0
tests/test1.c View 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));


+ 11
- 1
tests/test1.test View File

@@ -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

+ 35
- 0
tests/test1Formatted_plain.expected View File

@@ -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}

+ 58
- 0
tests/test1Formatted_pretty.expected View File

@@ -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
}

+ 35
- 0
tests/test1Formatted_spaced.expected View File

@@ -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 }

+ 14
- 0
tests/test2.c View 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);


+ 11
- 1
tests/test2.test View File

@@ -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

+ 1
- 0
tests/test2Formatted_plain.expected View File

@@ -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"]}]}}}

+ 23
- 0
tests/test2Formatted_pretty.expected View File

@@ -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"
]
}
]
}
}
}

+ 1
- 0
tests/test2Formatted_spaced.expected View File

@@ -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" ] } ] } } }

Loading…
Cancel
Save