json-compare: add a json comparison tool and test suite

This commit is contained in:
Danny Robson 2018-07-05 17:46:43 +10:00
parent 3d51be1372
commit 4c0f129a37
29 changed files with 78 additions and 1 deletions

View File

@ -462,7 +462,7 @@ target_link_libraries(cruft-util dl)
############################################################################### ###############################################################################
foreach (tool cpuid json-clean json-schema json-validate poisson macro scratch) foreach (tool cpuid json-clean json-schema json-validate json-compare poisson macro scratch)
add_executable (util_${tool} tools/${tool}.cpp) add_executable (util_${tool} tools/${tool}.cpp)
set_target_properties (util_${tool} PROPERTIES OUTPUT_NAME ${tool}) set_target_properties (util_${tool} PROPERTIES OUTPUT_NAME ${tool})
target_link_libraries (util_${tool} cruft-util) target_link_libraries (util_${tool} cruft-util)
@ -580,6 +580,10 @@ if (TESTS)
add_test(NAME util_test_json_parse COMMAND util_test_json_parse.sh) add_test(NAME util_test_json_parse COMMAND util_test_json_parse.sh)
set_property(TEST util_test_json_parse APPEND PROPERTY DEPENDS util_json-validate) set_property(TEST util_test_json_parse APPEND PROPERTY DEPENDS util_json-validate)
configure_file (test/json/compare.py.in util_test_json_compare.py @ONLY)
add_test(NAME util_test_json_compare COMMAND util_test_json_compare.py)
set_property(TEST util_test_json_compare APPEND PROPERTY DEPENDS util_json-compare)
configure_file (test/cpp.sh.in util_test_cpp.sh @ONLY) configure_file (test/cpp.sh.in util_test_cpp.sh @ONLY)
add_test (NAME util_test_cpp COMMAND util_test_cpp.sh) add_test (NAME util_test_cpp COMMAND util_test_cpp.sh)
set_property (TEST util_test_cpp APPEND PROPERTY DEPENDS util_macro) set_property (TEST util_test_cpp APPEND PROPERTY DEPENDS util_macro)

21
test/json/compare.py.in Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
from glob import glob
from subprocess import call
from os.path import basename
tool="@CMAKE_CURRENT_BINARY_DIR@/json-compare"
src="@CMAKE_CURRENT_SOURCE_DIR@/test/json/compare"
suffix=".a.json"
for expected in [ "good", "bad" ]:
for path in glob(f"{src}/{expected}/*{suffix}"):
base = path[:-len(suffix)]
inputs = glob(f"{base}*.json")
code = call([tool, *inputs])
success = (expected == "good") == (code == 0)
prefix = "not " if not success else ""
print(prefix, "ok - ", expected, basename(base), code)

View File

@ -0,0 +1 @@
3.14

View File

@ -0,0 +1 @@
3.142

View File

@ -0,0 +1 @@
[0,1,2]

View File

@ -0,0 +1 @@
[0,1]

View File

@ -0,0 +1 @@
{"a":0,"b":1}

View File

@ -0,0 +1 @@
{"a":0}

View File

@ -0,0 +1 @@
{"a":0}

View File

@ -0,0 +1 @@
{"a":0.00001}

View File

@ -0,0 +1 @@
638

View File

@ -0,0 +1 @@
638

View File

@ -0,0 +1 @@
"the quick brown fox"

View File

@ -0,0 +1 @@
"the quick brown fox"

View File

@ -0,0 +1 @@
3.14

View File

@ -0,0 +1 @@
3.14

View File

@ -0,0 +1 @@
true

View File

@ -0,0 +1 @@
true

View File

@ -0,0 +1 @@
false

View File

@ -0,0 +1 @@
false

View File

@ -0,0 +1 @@
null

View File

@ -0,0 +1 @@
null

View File

@ -0,0 +1 @@
{"foo":"bar"}

View File

@ -0,0 +1 @@
{"foo":"bar"}

View File

@ -0,0 +1 @@
[true]

View File

@ -0,0 +1 @@
[true]

View File

@ -0,0 +1 @@
{"a":0,"b":1}

View File

@ -0,0 +1 @@
{"b":1,"a":0}

26
tools/json-compare.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
#include "json/tree.hpp"
enum {
ARG_SELF,
ARG_A,
ARG_B,
NUM_ARGS
};
int
main (int argc, char **argv)
{
if (NUM_ARGS != argc) {
std::cerr << "usage: " << argv[ARG_SELF] << " <a> <b>\n";
return -1;
}
auto const &a = json::tree::parse (argv[ARG_A]);
auto const &b = json::tree::parse (argv[ARG_B]);
return *a == *b ? 0 : 1;
}