Skip to content

Commit 4c7f38e

Browse files
committed
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).
1 parent 3fcffe1 commit 4c7f38e

14 files changed

+293
-19
lines changed

tests/Makefile.am

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,26 @@ include ../Makefile.am.inc
33

44
LIBJSON_LA=$(top_builddir)/libjson.la
55

6-
check_PROGRAMS = test1 test2 test4 test_parse_int64 test_null test_cast test_parse
6+
check_PROGRAMS = test1 test1Formatted
7+
check_PROGRAMS += test2 test2Formatted
8+
check_PROGRAMS += test4
9+
check_PROGRAMS += test_parse_int64
10+
check_PROGRAMS += test_null
11+
check_PROGRAMS += test_cast
12+
check_PROGRAMS += test_parse
713

814
test1_LDADD = $(LIBJSON_LA)
915

16+
test1Formatted_LDADD= $(LIBJSON_LA)
17+
test1Formatted_SOURCES = test1.c parse_flags.c
18+
test1Formatted_CPPFLAGS = -DTEST_FORMATTED
19+
1020
test2_LDADD = $(LIBJSON_LA)
1121

22+
test2Formatted_LDADD= $(LIBJSON_LA)
23+
test2Formatted_SOURCES = test2.c parse_flags.c
24+
test2Formatted_CPPFLAGS = -DTEST_FORMATTED
25+
1226
test4_LDADD = $(LIBJSON_LA)
1327

1428
test_parse_int64_LDADD = $(LIBJSON_LA)

tests/parse_flags.c

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#include "json.h"
5+
#include "parse_flags.h"
6+
7+
static struct {
8+
const char *arg;
9+
int flag;
10+
} format_args[] = {
11+
{ "plain", JSON_C_TO_STRING_PLAIN },
12+
{ "spaced", JSON_C_TO_STRING_SPACED },
13+
{ "pretty", JSON_C_TO_STRING_PRETTY },
14+
};
15+
16+
#ifndef NELEM
17+
#define NELEM(x) (sizeof(x) / sizeof(&x[0]))
18+
#endif
19+
20+
int parse_flags(int argc, char **argv)
21+
{
22+
int arg_idx;
23+
int sflags = 0;
24+
for (arg_idx = 1; arg_idx < argc ; arg_idx++)
25+
{
26+
int jj;
27+
for (jj = 0; jj < NELEM(format_args); jj++)
28+
{
29+
if (strcasecmp(argv[arg_idx], format_args[jj].arg) == 0)
30+
{
31+
sflags |= format_args[jj].flag;
32+
break;
33+
}
34+
}
35+
if (jj == NELEM(format_args))
36+
{
37+
printf("Unknown arg: %s\n", argv[arg_idx]);
38+
exit(1);
39+
}
40+
}
41+
return sflags;
42+
}

tests/parse_flags.h

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef __parse_flags_h
2+
#define __parse_flags_h
3+
int parse_flags(int argc, char **argv);
4+
#endif

tests/test-defs.sh

+29-16
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@ top_builddir=${top_builddir}/tests
2626
progname=`echo "$0" | sed 's,^.*/,,'`
2727
testname=`echo "$progname" | sed 's,-.*$,,'`
2828
testsubdir=${testsubdir-testSubDir}
29+
testsubdir=${testsubdir}/${progname}
2930

3031
# User can set VERBOSE to cause output redirection
3132
case "$VERBOSE" in
3233
[Nn]|[Nn][Oo]|0|"")
3334
VERBOSE=0
34-
exec > /dev/null 2>&1
35+
exec > /dev/null
3536
;;
3637
[Yy]|[Yy][Ee][Ss])
3738
VERBOSE=1
3839
;;
3940
esac
4041

41-
rm -rf "$testsubdir/$progname" > /dev/null 2>&1
42-
mkdir -p "$testsubdir/$progname"
43-
cd "$testsubdir/$progname" \
44-
|| { echo "Cannot make or change into $testsubdir/$progname"; exit 1; }
42+
rm -rf "$testsubdir" > /dev/null 2>&1
43+
mkdir -p "$testsubdir"
44+
CURDIR=$(pwd)
45+
cd "$testsubdir" \
46+
|| { echo "Cannot make or change into $testsubdir"; exit 1; }
4547

4648
echo "=== Running test $progname"
4749

@@ -68,44 +70,55 @@ fi
6870
#
6971
run_output_test()
7072
{
73+
if [ "$1" = "-o" ] ; then
74+
TEST_OUTPUT="$2"
75+
shift
76+
shift
77+
fi
7178
TEST_COMMAND="$1"
79+
shift
80+
if [ -z "${TEST_OUTPUT}" ] ; then
81+
TEST_OUTPUT=${TEST_COMMAND}
82+
fi
7283

73-
REDIR_OUTPUT="> \"${TEST_COMMAND}.out\""
84+
REDIR_OUTPUT="> \"${TEST_OUTPUT}.out\""
7485
if [ $VERBOSE -gt 1 ] ; then
75-
REDIR_OUTPUT="| tee \"${TEST_COMMAND}.out\""
86+
REDIR_OUTPUT="| tee \"${TEST_OUTPUT}.out\""
7687
fi
7788

7889
if [ $use_valgrind -eq 1 ] ; then
7990
eval valgrind --tool=memcheck \
8091
--trace-children=yes \
8192
--demangle=yes \
82-
--log-file=vg.out \
93+
--log-file="${TEST_OUTPUT}.vg.out" \
8394
--leak-check=full \
8495
--show-reachable=yes \
8596
--run-libc-freeres=yes \
86-
"\"${top_builddir}/${TEST_COMMAND}\"" ${REDIR_OUTPUT}
97+
"\"${top_builddir}/${TEST_COMMAND}\"" \"\$@\" ${REDIR_OUTPUT}
8798
err=$?
8899

89100
else
90-
eval "\"${top_builddir}/${TEST_COMMAND}"\" ${REDIR_OUTPUT}
101+
eval "\"${top_builddir}/${TEST_COMMAND}"\" \"\$@\" ${REDIR_OUTPUT}
91102
err=$?
92103
fi
93104

94105
if [ $err -ne 0 ] ; then
95-
echo "ERROR: ${TEST_COMMAND} exited with non-zero exit status: $err" 1>&2
106+
echo "ERROR: \"${TEST_COMMAND} $@\" exited with non-zero exit status: $err" 1>&2
96107
fi
97108

98109
if [ $use_valgrind -eq 1 ] ; then
99-
if ! tail -1 "vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then
110+
if ! tail -1 "${TEST_OUTPUT}.vg.out" | grep -q "ERROR SUMMARY: 0 errors" ; then
100111
echo "ERROR: valgrind found errors during execution:" 1>&2
101-
cat vg.out
112+
cat "${TEST_OUTPUT}.vg.out"
102113
err=1
103114
fi
104115
fi
105116

106-
if ! "$CMP" -s "${top_builddir}/${TEST_COMMAND}.expected" "${TEST_COMMAND}.out" ; then
107-
echo "ERROR: ${TEST_COMMAND} failed:" 1>&2
108-
diff "${top_builddir}/${TEST_COMMAND}.expected" "${TEST_COMMAND}.out" 1>&2
117+
if ! "$CMP" -s "${top_builddir}/${TEST_OUTPUT}.expected" "${TEST_OUTPUT}.out" ; then
118+
echo "ERROR: \"${TEST_COMMAND} $@\" (${TEST_OUTPUT}) failed (set VERBOSE=1 to see full output):" 1>&2
119+
(cd "${CURDIR}" ; set -x ; diff "${top_builddir}/${TEST_OUTPUT}.expected" "$testsubdir/${TEST_OUTPUT}.out")
120+
echo "cp \"$testsubdir/${TEST_OUTPUT}.out\" \"${top_builddir}/${TEST_OUTPUT}.expected\"" 1>&2
121+
109122
err=1
110123
fi
111124

tests/test1.c

+14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <assert.h>
66

77
#include "json.h"
8+
#include "parse_flags.h"
89

910
static int sort_fn (const void *j1, const void *j2)
1011
{
@@ -29,13 +30,26 @@ static int sort_fn (const void *j1, const void *j2)
2930
return i1 - i2;
3031
}
3132

33+
#ifdef TEST_FORMATTED
34+
#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags)
35+
#else
36+
/* no special define */
37+
#endif
38+
3239
int main(int argc, char **argv)
3340
{
3441
json_object *my_string, *my_int, *my_object, *my_array;
3542
int i;
43+
#ifdef TEST_FORMATTED
44+
int sflags = 0;
45+
#endif
3646

3747
MC_SET_DEBUG(1);
3848

49+
#ifdef TEST_FORMATTED
50+
sflags = parse_flags(argc, argv);
51+
#endif
52+
3953
my_string = json_object_new_string("\t");
4054
printf("my_string=%s\n", json_object_get_string(my_string));
4155
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));

tests/test1.test

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@ fi
99
. "$srcdir/test-defs.sh"
1010

1111
run_output_test test1
12-
exit $?
12+
_err=$?
13+
14+
for flag in plain spaced pretty ; do
15+
run_output_test -o test1Formatted_${flag} test1Formatted ${flag}
16+
_err2=$?
17+
if [ $_err -eq 0 ] ; then
18+
_err=$_err2
19+
fi
20+
done
21+
22+
exit $_err

tests/test1Formatted_plain.expected

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
my_string=
2+
my_string.to_string()="\t"
3+
my_string=\
4+
my_string.to_string()="\\"
5+
my_string=foo
6+
my_string.to_string()="foo"
7+
my_int=9
8+
my_int.to_string()=9
9+
my_array=
10+
[0]=1
11+
[1]=2
12+
[2]=3
13+
[3]=null
14+
[4]=5
15+
my_array.to_string()=[1,2,3,null,5]
16+
my_array=
17+
[0]=3
18+
[1]=1
19+
[2]=2
20+
[3]=null
21+
[4]=0
22+
my_array.to_string()=[3,1,2,null,0]
23+
my_array=
24+
[0]=null
25+
[1]=0
26+
[2]=1
27+
[3]=2
28+
[4]=3
29+
my_array.to_string()=[null,0,1,2,3]
30+
my_object=
31+
abc: 12
32+
foo: "bar"
33+
bool0: false
34+
bool1: true
35+
my_object.to_string()={"abc":12,"foo":"bar","bool0":false,"bool1":true}

tests/test1Formatted_pretty.expected

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
my_string=
2+
my_string.to_string()="\t"
3+
my_string=\
4+
my_string.to_string()="\\"
5+
my_string=foo
6+
my_string.to_string()="foo"
7+
my_int=9
8+
my_int.to_string()=9
9+
my_array=
10+
[0]=1
11+
[1]=2
12+
[2]=3
13+
[3]=null
14+
[4]=5
15+
my_array.to_string()=[
16+
1,
17+
2,
18+
3,
19+
null,
20+
5
21+
]
22+
my_array=
23+
[0]=3
24+
[1]=1
25+
[2]=2
26+
[3]=null
27+
[4]=0
28+
my_array.to_string()=[
29+
3,
30+
1,
31+
2,
32+
null,
33+
0
34+
]
35+
my_array=
36+
[0]=null
37+
[1]=0
38+
[2]=1
39+
[3]=2
40+
[4]=3
41+
my_array.to_string()=[
42+
null,
43+
0,
44+
1,
45+
2,
46+
3
47+
]
48+
my_object=
49+
abc: 12
50+
foo: "bar"
51+
bool0: false
52+
bool1: true
53+
my_object.to_string()={
54+
"abc":12,
55+
"foo":"bar",
56+
"bool0":false,
57+
"bool1":true
58+
}

tests/test1Formatted_spaced.expected

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
my_string=
2+
my_string.to_string()="\t"
3+
my_string=\
4+
my_string.to_string()="\\"
5+
my_string=foo
6+
my_string.to_string()="foo"
7+
my_int=9
8+
my_int.to_string()=9
9+
my_array=
10+
[0]=1
11+
[1]=2
12+
[2]=3
13+
[3]=null
14+
[4]=5
15+
my_array.to_string()=[ 1, 2, 3, null, 5 ]
16+
my_array=
17+
[0]=3
18+
[1]=1
19+
[2]=2
20+
[3]=null
21+
[4]=0
22+
my_array.to_string()=[ 3, 1, 2, null, 0 ]
23+
my_array=
24+
[0]=null
25+
[1]=0
26+
[2]=1
27+
[3]=2
28+
[4]=3
29+
my_array.to_string()=[ null, 0, 1, 2, 3 ]
30+
my_object=
31+
abc: 12
32+
foo: "bar"
33+
bool0: false
34+
bool1: true
35+
my_object.to_string()={ "abc": 12, "foo": "bar", "bool0": false, "bool1": true }

tests/test2.c

+14
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,28 @@
44
#include <string.h>
55

66
#include "json.h"
7+
#include "parse_flags.h"
8+
9+
#ifdef TEST_FORMATTED
10+
#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags)
11+
#else
12+
/* no special define */
13+
#endif
714

815

916
int main(int argc, char **argv)
1017
{
1118
json_object *new_obj;
19+
#ifdef TEST_FORMATTED
20+
int sflags = 0;
21+
#endif
1222

1323
MC_SET_DEBUG(1);
1424

25+
#ifdef TEST_FORMATTED
26+
sflags = parse_flags(argc, argv);
27+
#endif
28+
1529
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\"] } ] } } }");
1630
printf("new_obj.to_string()=%s\n", json_object_to_json_string(new_obj));
1731
json_object_put(new_obj);

tests/test2.test

+11-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,14 @@ fi
99
. "$srcdir/test-defs.sh"
1010

1111
run_output_test test2
12-
exit $?
12+
_err=$?
13+
14+
for flag in plain spaced pretty ; do
15+
run_output_test -o test2Formatted_${flag} test2Formatted ${flag}
16+
_err2=$?
17+
if [ $_err -eq 0 ] ; then
18+
_err=$_err2
19+
fi
20+
done
21+
22+
exit $_err

0 commit comments

Comments
 (0)