Use the json-validate tool for unit tests

This commit is contained in:
Danny Robson 2012-04-20 18:17:38 +10:00
parent fd113f6c4d
commit 89fb2b238c
4 changed files with 98 additions and 37 deletions

View File

@ -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

View File

@ -1,33 +0,0 @@
#include "../json.hpp"
#include <iostream>
#include <cstdlib>
#include <boost/filesystem.hpp>
enum {
ARG_CMD,
ARG_PATH,
NUM_ARGS
};
int
main (int argc, char ** argv) {
if (argc != NUM_ARGS) {
std::cerr << "Invalid arguments. "
<< argv[ARG_CMD] << " <path> "
<< 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;
}

93
test/json.cpp Normal file
View File

@ -0,0 +1,93 @@
#include "../debug.hpp"
#include "../json.hpp"
#include <memory>
#include <cstdlib>
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<json::node> 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;
}

View File

@ -1,6 +1,6 @@
#!/usr/bin/perl
$COMMAND = "./json-check";
$COMMAND = "../json-validate";
@good = <json/good/*>;
@bad = <json/bad/*>;