From 89fb2b238cd89fab2c8f936c8242199b55af09be Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 20 Apr 2012 18:17:38 +1000 Subject: [PATCH] Use the json-validate tool for unit tests --- test/Makefile.am | 7 ++-- test/json-check.cpp | 33 ---------------- test/json.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++ test/json.pl | 2 +- 4 files changed, 98 insertions(+), 37 deletions(-) delete mode 100644 test/json-check.cpp create mode 100644 test/json.cpp diff --git a/test/Makefile.am b/test/Makefile.am index 113d4bc1..cef55c7c 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -13,6 +13,7 @@ TEST_BIN = \ float \ hton \ ip \ + json \ maths \ matrix \ pool \ @@ -21,7 +22,7 @@ TEST_BIN = \ version TESTS = $(TEST_BIN) json.pl -check_PROGRAMS = $(TEST_BIN) json-check +check_PROGRAMS = $(TEST_BIN) EXTRA_DIST = json.pl backtrace_CPPFLAGS = $(COMMON_CXXFLAGS) @@ -40,8 +41,8 @@ hton_SOURCES = hton.cpp ip_LDADD = $(builddir)/../libutil.la ip_SOURCES = ip.cpp -json_check_LDADD = $(builddir)/../libutil.la $(BOOST_FILESYSTEM_LIB) -json_check_SOURCES = json-check.cpp +json_LDADD = $(builddir)/../libutil.la +json_SOURCES = json.cpp maths_LDADD = $(builddir)/../libutil.la maths_SOURCES = maths.cpp diff --git a/test/json-check.cpp b/test/json-check.cpp deleted file mode 100644 index e383ce29..00000000 --- a/test/json-check.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "../json.hpp" - -#include -#include -#include - - -enum { - ARG_CMD, - ARG_PATH, - - NUM_ARGS -}; - - -int -main (int argc, char ** argv) { - if (argc != NUM_ARGS) { - std::cerr << "Invalid arguments. " - << argv[ARG_CMD] << " " - << std::endl; - return EXIT_FAILURE; - } - - try { - json::parse (boost::filesystem::path (argv[ARG_PATH])); - } catch (json::error &x) { - std::cerr << x.what () << std::endl; - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; -} diff --git a/test/json.cpp b/test/json.cpp new file mode 100644 index 00000000..5e62cbfd --- /dev/null +++ b/test/json.cpp @@ -0,0 +1,93 @@ + +#include "../debug.hpp" +#include "../json.hpp" + +#include +#include + +int +main (int, char**) { + static const char TEST_STRING[] = + "{" + " \"string\" : \"brad\"," + " \"integer\": 1, " + " \"null\": null, " + " \"false\": false, " + " \"true\": true, " + " \"double\": 3.14, " + " \"object\": { " + " \"test\": \"test\" " + " }, " + " \"array\": [ " + " 1, 2, 3, 4 " + " ]" + "}"; + + std::unique_ptr ptr = json::parse (TEST_STRING); + check_hard (ptr->is_object ()); + + const json::node &ref = *ptr; + + check_hard ( ref["string"].is_string ()); + check_hard (!ref["string"].is_array ()); + check_hard (!ref["string"].is_boolean ()); + check_hard (!ref["string"].is_null ()); + check_hard (!ref["string"].is_number ()); + check_hard (!ref["string"].is_object ()); + check_eq ( ref["string"].as_string (), "brad"); + + check_hard ( ref["integer"].is_number ()); + check_hard (!ref["integer"].is_array ()); + check_hard (!ref["integer"].is_boolean ()); + check_hard (!ref["integer"].is_null ()); + check_hard (!ref["integer"].is_object ()); + check_hard (!ref["integer"].is_string ()); + check_eq ( ref["integer"].as_number (), 1u); + + check_hard ( ref["null"].is_null ()); + check_hard (!ref["null"].is_array ()); + check_hard (!ref["null"].is_boolean ()); + check_hard (!ref["null"].is_number ()); + check_hard (!ref["null"].is_object ()); + check_hard (!ref["null"].is_string ()); + + check_hard ( ref["false"].is_boolean ()); + check_hard (!ref["false"].is_array ()); + check_hard (!ref["false"].is_null ()); + check_hard (!ref["false"].is_number ()); + check_hard (!ref["false"].is_object ()); + check_hard (!ref["false"].is_string ()); + check_eq ( ref["false"].as_boolean (), false); + + check_hard ( ref["true"].is_boolean ()); + check_hard (!ref["true"].is_array ()); + check_hard (!ref["true"].is_null ()); + check_hard (!ref["true"].is_number ()); + check_hard (!ref["true"].is_object ()); + check_hard (!ref["true"].is_string ()); + check_eq ( ref["true"].as_boolean (), true); + + check_hard ( ref["double"].is_number ()); + check_hard (!ref["double"].is_array ()); + check_hard (!ref["double"].is_boolean ()); + check_hard (!ref["double"].is_null ()); + check_hard (!ref["double"].is_object ()); + check_hard (!ref["double"].is_string ()); + check_eq ( ref["double"].as_number (), 3.14); + + check_hard ( ref["object"].is_object ()); + check_hard (!ref["object"].is_array ()); + check_hard (!ref["object"].is_boolean ()); + check_hard (!ref["object"].is_null ()); + check_hard (!ref["object"].is_number ()); + check_hard (!ref["object"].is_string ()); + + check_hard ( ref["array"].is_array ()); + check_hard (!ref["array"].is_boolean ()); + check_hard (!ref["array"].is_null ()); + check_hard (!ref["array"].is_number ()); + check_hard (!ref["array"].is_object ()); + check_hard (!ref["array"].is_string ()); + + return EXIT_SUCCESS; +} diff --git a/test/json.pl b/test/json.pl index c9bf8972..c2508c40 100755 --- a/test/json.pl +++ b/test/json.pl @@ -1,6 +1,6 @@ #!/usr/bin/perl -$COMMAND = "./json-check"; +$COMMAND = "../json-validate"; @good = ; @bad = ;