json-compare: add a json comparison tool and test suite
This commit is contained in:
parent
3d51be1372
commit
4c0f129a37
@ -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
21
test/json/compare.py.in
Executable 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)
|
1
test/json/compare/bad/0001-float.a.json
Normal file
1
test/json/compare/bad/0001-float.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
3.14
|
1
test/json/compare/bad/0001-float.b.json
Normal file
1
test/json/compare/bad/0001-float.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
3.142
|
1
test/json/compare/bad/1000-truncated_array.a.json
Normal file
1
test/json/compare/bad/1000-truncated_array.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
[0,1,2]
|
1
test/json/compare/bad/1000-truncated_array.b.json
Normal file
1
test/json/compare/bad/1000-truncated_array.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
[0,1]
|
1
test/json/compare/bad/2000-truncated_object.a.json
Normal file
1
test/json/compare/bad/2000-truncated_object.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"a":0,"b":1}
|
1
test/json/compare/bad/2000-truncated_object.b.json
Normal file
1
test/json/compare/bad/2000-truncated_object.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"a":0}
|
1
test/json/compare/bad/2001-object_value.a.json
Normal file
1
test/json/compare/bad/2001-object_value.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"a":0}
|
1
test/json/compare/bad/2001-object_value.b.json
Normal file
1
test/json/compare/bad/2001-object_value.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"a":0.00001}
|
1
test/json/compare/good/0001-integer.a.json
Normal file
1
test/json/compare/good/0001-integer.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
638
|
1
test/json/compare/good/0001-integer.b.json
Normal file
1
test/json/compare/good/0001-integer.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
638
|
1
test/json/compare/good/0002-string.a.json
Normal file
1
test/json/compare/good/0002-string.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
"the quick brown fox"
|
1
test/json/compare/good/0002-string.b.json
Normal file
1
test/json/compare/good/0002-string.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
"the quick brown fox"
|
1
test/json/compare/good/0004-float.a.json
Normal file
1
test/json/compare/good/0004-float.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
3.14
|
1
test/json/compare/good/0004-float.b.json
Normal file
1
test/json/compare/good/0004-float.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
3.14
|
1
test/json/compare/good/0005-true.a.json
Normal file
1
test/json/compare/good/0005-true.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
true
|
1
test/json/compare/good/0005-true.b.json
Normal file
1
test/json/compare/good/0005-true.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
true
|
1
test/json/compare/good/0006-false.a.json
Normal file
1
test/json/compare/good/0006-false.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
false
|
1
test/json/compare/good/0006-false.b.json
Normal file
1
test/json/compare/good/0006-false.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
false
|
1
test/json/compare/good/0007-null.a.json
Normal file
1
test/json/compare/good/0007-null.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
null
|
1
test/json/compare/good/0007-null.b.json
Normal file
1
test/json/compare/good/0007-null.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
null
|
1
test/json/compare/good/0010-object.a.json
Normal file
1
test/json/compare/good/0010-object.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"foo":"bar"}
|
1
test/json/compare/good/0010-object.b.json
Normal file
1
test/json/compare/good/0010-object.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"foo":"bar"}
|
1
test/json/compare/good/0020-array.a.json
Normal file
1
test/json/compare/good/0020-array.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
[true]
|
1
test/json/compare/good/0020-array.b.json
Normal file
1
test/json/compare/good/0020-array.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
[true]
|
1
test/json/compare/good/5000-object.a.json
Normal file
1
test/json/compare/good/5000-object.a.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"a":0,"b":1}
|
1
test/json/compare/good/5000-object.b.json
Normal file
1
test/json/compare/good/5000-object.b.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"b":1,"a":0}
|
26
tools/json-compare.cpp
Normal file
26
tools/json-compare.cpp
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user