Skip to content

Commit

Permalink
Extend test1 and test2 to run using json_object_to_json_string_ext() …
Browse files Browse the repository at this point in the history
…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).
  • Loading branch information
hawicz committed Apr 28, 2012
1 parent 3fcffe1 commit 4c7f38e
Show file tree
Hide file tree
Showing 14 changed files with 293 additions and 19 deletions.
16 changes: 15 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions tests/parse_flags.c
Original file line number Diff line number Diff line change
@@ -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 changes: 4 additions & 0 deletions tests/parse_flags.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef __parse_flags_h
#define __parse_flags_h
int parse_flags(int argc, char **argv);
#endif
45 changes: 29 additions & 16 deletions tests/test-defs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"

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

Expand Down
14 changes: 14 additions & 0 deletions tests/test1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <assert.h>

#include "json.h"
#include "parse_flags.h"

static int sort_fn (const void *j1, const void *j2)
{
Expand All @@ -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));
Expand Down
12 changes: 11 additions & 1 deletion tests/test1.test
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 35 additions & 0 deletions tests/test1Formatted_plain.expected
Original file line number Diff line number Diff line change
@@ -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 changes: 58 additions & 0 deletions tests/test1Formatted_pretty.expected
Original file line number Diff line number Diff line change
@@ -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 changes: 35 additions & 0 deletions tests/test1Formatted_spaced.expected
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions tests/test2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 11 additions & 1 deletion tests/test2.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 4c7f38e

Please sign in to comment.