Skip to content

Commit

Permalink
Merge pull request #683 from akheron/refactor-tests
Browse files Browse the repository at this point in the history
Refactor tests
  • Loading branch information
akheron committed Mar 21, 2024
2 parents 842708a + 649c935 commit 53383b9
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 180 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ jobs:
CC: ${{matrix.cc}}
run: cmake .
- run: cmake --build .
- run: ctest
- run: ctest --output-on-failure

valgrind:
runs-on: ubuntu-latest
Expand All @@ -63,4 +63,4 @@ jobs:
- run: sudo apt update && sudo apt install valgrind
- run: cmake -DJANSSON_TEST_WITH_VALGRIND=ON .
- run: cmake --build .
- run: ctest
- run: ctest --output-on-failure
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Work in progress
is used to switch locales inside the threads (#674, #675, #677. Thanks to
Bruno Haible the report and help with fixing.)

* Build:

- Make test output nicer in CMake based builds (#683)

Version 2.14
============

Expand Down
2 changes: 1 addition & 1 deletion scripts/clang-format
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

find . -type f -a '(' -name '*.c' -o -name '*.h' ')' | xargs clang-format -i
git ls-files | grep '\.[ch]$' | xargs clang-format -i
7 changes: 5 additions & 2 deletions scripts/clang-format-check
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ fi
errors=0
paths=$(git ls-files | grep '\.[ch]$')
for path in $paths; do
echo "Checking $path"
$CLANG_FORMAT $path > $path.formatted
in=$(cat $path)
out=$($CLANG_FORMAT $path)
out=$(cat $path.formatted)

if [ "$in" != "$out" ]; then
diff -u -L $path -L "$path.formatted" $path - <<<$out
diff -u $path $path.formatted
errors=1
fi
rm $path.formatted
done

if [ $errors -ne 0 ]; then
Expand Down
1 change: 1 addition & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ suites/api/test_cpp
suites/api/test_dump
suites/api/test_dump_callback
suites/api/test_equal
suites/api/test_fixed_size
suites/api/test_load
suites/api/test_load_callback
suites/api/test_loadb
Expand Down
132 changes: 15 additions & 117 deletions test/bin/json_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ struct config {
int ensure_ascii;
int sort_keys;
int strip;
int use_env;
int have_hashseed;
int hashseed;
int precision;
Expand Down Expand Up @@ -138,10 +137,16 @@ static int cmpfile(const char *str, const char *path, const char *fname) {
}

buffer = loadfile(file);
if (strcmp(buffer, str) != 0)
if (strcmp(buffer, str) != 0) {
fprintf(stderr, "=== Expected %s ===\n", fname);
fprintf(stderr, "%s\n", buffer);
fprintf(stderr, "=== Actual %s ===\n", fname);
fprintf(stderr, "%s\n", str);
ret = 1;
else
} else {
ret = 0;
}

free(buffer);
fclose(file);

Expand Down Expand Up @@ -206,8 +211,9 @@ int use_conf(char *test_path) {
buffer = loadfile(infile);
json = json_loads(strip(buffer), 0, &error);
free(buffer);
} else
} else {
json = json_loadf(infile, 0, &error);
}

fclose(infile);

Expand All @@ -227,108 +233,6 @@ int use_conf(char *test_path) {
return ret;
}

static int getenv_int(const char *name) {
char *value, *end;
long result;

value = getenv(name);
if (!value)
return 0;

result = strtol(value, &end, 10);
if (*end != '\0')
return 0;

return (int)result;
}

int use_env() {
int indent, precision;
size_t flags = 0;
json_t *json;
json_error_t error;

#ifdef _WIN32
/* On Windows, set stdout and stderr to binary mode to avoid
outputting DOS line terminators */
_setmode(_fileno(stdout), _O_BINARY);
_setmode(_fileno(stderr), _O_BINARY);
#endif

indent = getenv_int("JSON_INDENT");
if (indent < 0 || indent > 31) {
fprintf(stderr, "invalid value for JSON_INDENT: %d\n", indent);
return 2;
}
if (indent > 0)
flags |= JSON_INDENT(indent);

if (getenv_int("JSON_COMPACT") > 0)
flags |= JSON_COMPACT;

if (getenv_int("JSON_ENSURE_ASCII"))
flags |= JSON_ENSURE_ASCII;

if (getenv_int("JSON_PRESERVE_ORDER"))
flags |= JSON_PRESERVE_ORDER;

if (getenv_int("JSON_SORT_KEYS"))
flags |= JSON_SORT_KEYS;

precision = getenv_int("JSON_REAL_PRECISION");
if (precision < 0 || precision > 31) {
fprintf(stderr, "invalid value for JSON_REAL_PRECISION: %d\n", precision);
return 2;
}

if (getenv("HASHSEED"))
json_object_seed(getenv_int("HASHSEED"));

if (precision > 0)
flags |= JSON_REAL_PRECISION(precision);

if (getenv_int("STRIP")) {
/* Load to memory, strip leading and trailing whitespace */
size_t size = 0, used = 0;
char *buffer = NULL, *buf_ck = NULL;

while (1) {
size_t count;

size = (size == 0 ? 128 : size * 2);
buf_ck = realloc(buffer, size);
if (!buf_ck) {
fprintf(stderr, "Unable to allocate %d bytes\n", (int)size);
free(buffer);
return 1;
}
buffer = buf_ck;

count = fread(buffer + used, 1, size - used, stdin);
if (count < size - used) {
buffer[used + count] = '\0';
break;
}
used += count;
}

json = json_loads(strip(buffer), 0, &error);
free(buffer);
} else
json = json_loadf(stdin, 0, &error);

if (!json) {
fprintf(stderr, "%d %d %d\n%s\n", error.line, error.column, error.position,
error.text);
return 1;
}

json_dumpf(json, stdout, flags);
json_decref(json);

return 0;
}

int main(int argc, char *argv[]) {
int i;
char *test_path = NULL;
Expand All @@ -344,23 +248,17 @@ int main(int argc, char *argv[]) {
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "--strip"))
conf.strip = 1;
else if (!strcmp(argv[i], "--env"))
conf.use_env = 1;
else
test_path = argv[i];
}

if (conf.use_env)
return use_env();
else {
if (!test_path)
goto usage;

return use_conf(test_path);
if (!test_path) {
goto usage;
}

return use_conf(test_path);

usage:
fprintf(stderr, "argc =%d\n", argc);
fprintf(stderr, "usage: %s [--strip] [--env] test_dir\n", argv[0]);
fprintf(stderr, "usage: %s [--strip] test_dir\n", argv[0]);
return 2;
}
14 changes: 2 additions & 12 deletions test/suites/encoding-flags/run
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,13 @@ is_test() {
}

run_test() {
(
if [ -f $test_path/env ]; then
. $test_path/env
fi
$json_process --env <$test_path/input >$test_log/stdout 2>$test_log/stderr
)
$json_process $test_path >$test_log/stdout 2>$test_log/stderr || return 1
valgrind_check $test_log/stderr || return 1
cmp -s $test_path/output $test_log/stdout
}

show_error() {
valgrind_show_error && return

echo "EXPECTED OUTPUT:"
nl -bn $test_path/output
echo "ACTUAL OUTPUT:"
nl -bn $test_log/stdout
cat $test_log/stderr
}

. $top_srcdir/test/scripts/run-tests.sh
11 changes: 3 additions & 8 deletions test/suites/invalid-unicode/run
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,13 @@ is_test() {
}

run_test() {
$json_process --env <$test_path/input >$test_log/stdout 2>$test_log/stderr
valgrind_check $test_log/stderr || return 1
cmp -s $test_path/error $test_log/stderr
$json_process $test_path >$test_log/stdout 2>$test_log/stderr || return 1
valgrind_check $test_log/stderr$s || return 1
}

show_error() {
valgrind_show_error && return

echo "EXPECTED ERROR:"
nl -bn $test_path/error
echo "ACTUAL ERROR:"
nl -bn $test_log/stderr
cat $test_log/stderr
}

. $top_srcdir/test/scripts/run-tests.sh
25 changes: 6 additions & 19 deletions test/suites/invalid/run
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@ do_run() {
variant=$1
s=".$1"

strip=0
strip=""
if [ "$variant" = "strip" ]; then
# This test should not be stripped
[ -f $test_path/nostrip ] && return
strip=1
strip="--strip"
fi

STRIP=$strip $json_process --env \
<$test_path/input >$test_log/stdout$s 2>$test_log/stderr$s
valgrind_check $test_log/stderr$s || return 1

ref=error
[ -f $test_path/error$s ] && ref=error$s

if ! cmp -s $test_path/$ref $test_log/stderr$s; then
echo $variant > $test_log/variant
if ! $json_process $strip $test_path >$test_log/stdout$s 2>$test_log/stderr$s; then
echo $variant >$test_log/variant
return 1
fi
valgrind_check $test_log/stderr$s || return 1
}

run_test() {
Expand All @@ -44,14 +38,7 @@ show_error() {
s=".$variant"

echo "VARIANT: $variant"

echo "EXPECTED ERROR:"
ref=error
[ -f $test_path/error$s ] && ref=error$s
nl -bn $test_path/$ref

echo "ACTUAL ERROR:"
nl -bn $test_log/stderr$s
cat $test_log/stderr$s
}

. $top_srcdir/test/scripts/run-tests.sh
25 changes: 6 additions & 19 deletions test/suites/valid/run
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@ do_run() {
variant=$1
s=".$1"

strip=0
[ "$variant" = "strip" ] && strip=1
strip=""
[ "$variant" = "strip" ] && strip="--strip"

STRIP=$strip $json_process --env \
<$test_path/input >$test_log/stdout$s 2>$test_log/stderr$s
valgrind_check $test_log/stderr$s || return 1

ref=output
[ -f $test_path/output$s ] && ref=output$s

if ! cmp -s $test_path/$ref $test_log/stdout$s; then
echo $variant > $test_log/variant
if ! $json_process $strip $test_path >$test_log/stdout$s 2>$test_log/stderr$s; then
echo $variant >$test_log/variant
return 1
fi
valgrind_check $test_log/stderr$s || return 1
}

run_test() {
Expand All @@ -43,14 +37,7 @@ show_error() {
s=".$variant"

echo "VARIANT: $variant"

echo "EXPECTED OUTPUT:"
ref=output
[ -f $test_path/output$s ] && ref=output$s
nl -bn $test_path/$ref

echo "ACTUAL OUTPUT:"
nl -bn $test_log/stdout$s
cat $test_log/stderr$s
}

. $top_srcdir/test/scripts/run-tests.sh

0 comments on commit 53383b9

Please sign in to comment.