Mercurial > malikania
diff tests/libcommon/util/main.cpp @ 88:469b6d558ab0
Common: add util::json functions, closes #628
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 04 Feb 2017 16:15:20 +0100 |
parents | 858621081b95 |
children | f4d23ad4aa27 |
line wrap: on
line diff
--- a/tests/libcommon/util/main.cpp Sat Feb 04 14:40:20 2017 +0100 +++ b/tests/libcommon/util/main.cpp Sat Feb 04 16:15:20 2017 +0100 @@ -22,6 +22,21 @@ #include <malikania/util.hpp> using namespace mlk; +using namespace nlohmann; + +namespace nlohmann { + +namespace detail { + +std::ostream& operator<<(std::ostream& out, json::value_t type) +{ + out << json(type).type_name(); + return out; +} + +} // !detail + +} // !nlohmann /* * util::clamp @@ -56,3 +71,267 @@ } BOOST_AUTO_TEST_SUITE_END() + +/* + * util::json::require + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(json_require) + +BOOST_AUTO_TEST_CASE(simple) +{ + json object{ + { "b", true }, + { "i", 123 }, + { "s", "blabla" } + }; + + BOOST_REQUIRE_EQUAL(util::json::require( + object, "/b"_json_pointer, json::value_t::boolean).type(), json::value_t::boolean); + BOOST_REQUIRE_EQUAL(util::json::require( + object, "/i"_json_pointer, json::value_t::number_integer).type(), json::value_t::number_integer); + BOOST_REQUIRE_EQUAL(util::json::require( + object, "/s"_json_pointer, json::value_t::string).type(), json::value_t::string); +} + +BOOST_AUTO_TEST_CASE(nonexistent) +{ + auto json = json::object(); + + try { + util::json::require(json, "/non-existent"_json_pointer, json::value_t::string); + BOOST_FAIL("exception expected"); + } catch (const util::json::property_error& ex) { + BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::null); + BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string); + } +} + +BOOST_AUTO_TEST_CASE(invalid) +{ + json object{ + { "not-string", 123 } + }; + + try { + util::json::require(object, "/not-string"_json_pointer, json::value_t::string); + BOOST_FAIL("exception expected"); + } catch (const util::json::property_error& ex) { + BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); + BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string); + } +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * util::json::require_array + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(json_require_array) + +BOOST_AUTO_TEST_CASE(simple) +{ + json object{ + { "a", { 1, 2, 3 } }, + { "l1", { + { "l2", { 4, 5, 6 } } + } + } + }; + + auto a = util::json::require_array(object, "/a"_json_pointer); + auto l2 = util::json::require_array(object, "/l1/l2"_json_pointer); + + BOOST_REQUIRE(a.is_array()); + BOOST_REQUIRE(l2.is_array()); +} + +BOOST_AUTO_TEST_CASE(invalid) +{ + json object{ + { "not-array", 123 } + }; + + try { + util::json::require_array(object, "/not-array"_json_pointer); + BOOST_FAIL("exception expected"); + } catch (const util::json::property_error& ex) { + BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); + BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::array); + } +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * util::json::require_bool + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(json_require_bool) + +BOOST_AUTO_TEST_CASE(simple) +{ + json object{ + { "b", true } + }; + + BOOST_REQUIRE_EQUAL(util::json::require_bool(object, "/b"_json_pointer), true); +} + +BOOST_AUTO_TEST_CASE(invalid) +{ + json object{ + { "not-bool", 123 } + }; + + try { + util::json::require_bool(object, "/not-bool"_json_pointer); + BOOST_FAIL("exception expected"); + } catch (const util::json::property_error& ex) { + BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); + BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::boolean); + } +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * util::json::require_int + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(json_require_int) + +BOOST_AUTO_TEST_CASE(simple) +{ + json object{ + { "i", 123 } + }; + + BOOST_REQUIRE_EQUAL(util::json::require_int(object, "/i"_json_pointer), 123); +} + +BOOST_AUTO_TEST_CASE(invalid) +{ + json object{ + { "not-int", true } + }; + + try { + util::json::require_int(object, "/not-int"_json_pointer); + BOOST_FAIL("exception expected"); + } catch (const util::json::property_error& ex) { + BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::boolean); + BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_integer); + } +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * util::json::require_object + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(json_require_object) + +BOOST_AUTO_TEST_CASE(simple) +{ + json object{ + { + "network", json::object({ + { "host", "localhost" }, + { "port", 9090 } + }) + } + }; + + BOOST_REQUIRE(util::json::require_object(object, "/network"_json_pointer).is_object()); +} + +BOOST_AUTO_TEST_CASE(invalid) +{ + json object{ + { "not-object", 123 } + }; + + try { + util::json::require_object(object, "/not-object"_json_pointer); + BOOST_FAIL("exception expected"); + } catch (const util::json::property_error& ex) { + BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); + BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::object); + } +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * util::json::require_string + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(json_require_string) + +BOOST_AUTO_TEST_CASE(simple) +{ + json object{ + { "s", "hello" } + }; + + BOOST_REQUIRE_EQUAL(util::json::require_string(object, "/s"_json_pointer), "hello"); +} + +BOOST_AUTO_TEST_CASE(invalid) +{ + json object{ + { "not-string", 123 } + }; + + try { + util::json::require_string(object, "/not-string"_json_pointer); + BOOST_FAIL("exception expected"); + } catch (const util::json::property_error& ex) { + BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer); + BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string); + } +} + +BOOST_AUTO_TEST_SUITE_END() + +/* + * util::json::require_uint + * ------------------------------------------------------------------ + */ + +BOOST_AUTO_TEST_SUITE(json_require_uint) + +BOOST_AUTO_TEST_CASE(simple) +{ + json object{ + { "u1", 123U } + }; + + BOOST_REQUIRE_EQUAL(util::json::require_uint(object, "/u1"_json_pointer), 123U); +} + +BOOST_AUTO_TEST_CASE(invalid) +{ + json object{ + { "not-uint", true } + }; + + try { + util::json::require_uint(object, "/not-uint"_json_pointer); + BOOST_FAIL("exception expected"); + } catch (const util::json::property_error& ex) { + BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::boolean); + BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned); + } +} + +BOOST_AUTO_TEST_SUITE_END()