libcruft-util/test/json/types.cpp
Danny Robson 52f53caee5 debug: drop support for CHECK_HARD
HARD vs SOFT assertions were never very well defined or supported.
Currently they just imply a level of functionality that isn't present;
it's better to remove them instead of expending the effort at this
point.
2015-01-28 14:49:34 +11:00

105 lines
3.0 KiB
C++

#include "../debug.hpp"
#include "../json.hpp"
#include "../maths.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 (ptr->is_object ());
const json::node &ref = *ptr;
CHECK ( ref["string"].is_string ());
CHECK (!ref["string"].is_array ());
CHECK (!ref["string"].is_boolean ());
CHECK (!ref["string"].is_null ());
CHECK (!ref["string"].is_number ());
CHECK (!ref["string"].is_object ());
CHECK_EQ ( ref["string"].as_string (), "brad");
CHECK ( ref["integer"].is_number ());
CHECK (!ref["integer"].is_array ());
CHECK (!ref["integer"].is_boolean ());
CHECK (!ref["integer"].is_null ());
CHECK (!ref["integer"].is_object ());
CHECK (!ref["integer"].is_string ());
CHECK (
exactly_equal (
(unsigned)ref["integer"].as_number ().native (),
1u
)
);
CHECK ( ref["null"].is_null ());
CHECK (!ref["null"].is_array ());
CHECK (!ref["null"].is_boolean ());
CHECK (!ref["null"].is_number ());
CHECK (!ref["null"].is_object ());
CHECK (!ref["null"].is_string ());
CHECK ( ref["false"].is_boolean ());
CHECK (!ref["false"].is_array ());
CHECK (!ref["false"].is_null ());
CHECK (!ref["false"].is_number ());
CHECK (!ref["false"].is_object ());
CHECK (!ref["false"].is_string ());
CHECK_EQ ( ref["false"].as_boolean (), false);
CHECK ( ref["true"].is_boolean ());
CHECK (!ref["true"].is_array ());
CHECK (!ref["true"].is_null ());
CHECK (!ref["true"].is_number ());
CHECK (!ref["true"].is_object ());
CHECK (!ref["true"].is_string ());
CHECK_EQ ( ref["true"].as_boolean (), true);
CHECK ( ref["double"].is_number ());
CHECK (!ref["double"].is_array ());
CHECK (!ref["double"].is_boolean ());
CHECK (!ref["double"].is_null ());
CHECK (!ref["double"].is_object ());
CHECK (!ref["double"].is_string ());
CHECK (
exactly_equal (
ref["double"].as_number ().native (),
3.14
)
);
CHECK ( ref["object"].is_object ());
CHECK (!ref["object"].is_array ());
CHECK (!ref["object"].is_boolean ());
CHECK (!ref["object"].is_null ());
CHECK (!ref["object"].is_number ());
CHECK (!ref["object"].is_string ());
CHECK ( ref["array"].is_array ());
CHECK (!ref["array"].is_boolean ());
CHECK (!ref["array"].is_null ());
CHECK (!ref["array"].is_number ());
CHECK (!ref["array"].is_object ());
CHECK (!ref["array"].is_string ());
return EXIT_SUCCESS;
}