more test cases

This commit is contained in:
Niels 2015-02-08 16:08:48 +01:00
parent 8ed9eaa629
commit 441a6f267f
3 changed files with 62 additions and 16 deletions

View file

@ -497,6 +497,8 @@ class basic_json
///////////////////////
/*!
@brief serialization
Serialization function for JSON objects. The function tries to mimick Python's
@p json.dumps() function, and currently supports its @p indent parameter.
@ -536,7 +538,7 @@ class basic_json
// value conversion //
//////////////////////
/// get an object
/// get an object (explicit)
template <class T, typename
std::enable_if<
std::is_constructible<string_t, typename T::key_type>::value and
@ -553,7 +555,7 @@ class basic_json
}
}
/// get an array
/// get an array (explicit)
template <class T, typename
std::enable_if<
not std::is_same<T, string_t>::value and
@ -570,7 +572,7 @@ class basic_json
}
}
/// get a string
/// get a string (explicit)
template <typename T, typename
std::enable_if<
std::is_constructible<T, string_t>::value, int>::type
@ -586,7 +588,7 @@ class basic_json
}
}
/// get a boolean
/// get a boolean (explicit)
template <typename T, typename
std::enable_if<
std::is_same<boolean_t, T>::value, int>::type
@ -602,7 +604,7 @@ class basic_json
}
}
/// explicitly get a number
/// get a number (explicit)
template<typename T, typename
std::enable_if<
not std::is_same<boolean_t, T>::value and
@ -621,7 +623,7 @@ class basic_json
}
}
/// explicitly get a value
/// get a value (implicit)
template<typename T>
inline operator T() const
{

View file

@ -497,6 +497,8 @@ class basic_json
///////////////////////
/*!
@brief serialization
Serialization function for JSON objects. The function tries to mimick Python's
@p json.dumps() function, and currently supports its @p indent parameter.
@ -536,7 +538,7 @@ class basic_json
// value conversion //
//////////////////////
/// get an object
/// get an object (explicit)
template <class T, typename
std::enable_if<
std::is_constructible<string_t, typename T::key_type>::value and
@ -553,7 +555,7 @@ class basic_json
}
}
/// get an array
/// get an array (explicit)
template <class T, typename
std::enable_if<
not std::is_same<T, string_t>::value and
@ -570,7 +572,7 @@ class basic_json
}
}
/// get a string
/// get a string (explicit)
template <typename T, typename
std::enable_if<
std::is_constructible<T, string_t>::value, int>::type
@ -586,7 +588,7 @@ class basic_json
}
}
/// get a boolean
/// get a boolean (explicit)
template <typename T, typename
std::enable_if<
std::is_same<boolean_t, T>::value, int>::type
@ -602,7 +604,7 @@ class basic_json
}
}
/// explicitly get a number
/// get a number (explicit)
template<typename T, typename
std::enable_if<
not std::is_same<boolean_t, T>::value and
@ -621,7 +623,7 @@ class basic_json
}
}
/// explicitly get a value
/// get a value (implicit)
template<typename T>
inline operator T() const
{

View file

@ -1034,23 +1034,26 @@ TEST_CASE("other constructors and destructor")
TEST_CASE("object inspection")
{
SECTION("dump")
SECTION("serialization")
{
json j {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} };
SECTION("no indent")
{
CHECK(j.dump() == "{\"array\":[1,2,3,4],\"boolean\":false,\"null\":null,\"number\":42,\"object\":{},\"string\":\"Hello world\"}");
CHECK(j.dump() ==
"{\"array\":[1,2,3,4],\"boolean\":false,\"null\":null,\"number\":42,\"object\":{},\"string\":\"Hello world\"}");
}
SECTION("indent=0")
{
CHECK(j.dump(0) == "{\n\"array\": [\n1,\n2,\n3,\n4\n],\n\"boolean\": false,\n\"null\": null,\n\"number\": 42,\n\"object\": {},\n\"string\": \"Hello world\"\n}");
CHECK(j.dump(0) ==
"{\n\"array\": [\n1,\n2,\n3,\n4\n],\n\"boolean\": false,\n\"null\": null,\n\"number\": 42,\n\"object\": {},\n\"string\": \"Hello world\"\n}");
}
SECTION("indent=4")
{
CHECK(j.dump(4) == "{\n \"array\": [\n 1,\n 2,\n 3,\n 4\n ],\n \"boolean\": false,\n \"null\": null,\n \"number\": 42,\n \"object\": {},\n \"string\": \"Hello world\"\n}");
CHECK(j.dump(4) ==
"{\n \"array\": [\n 1,\n 2,\n 3,\n 4\n ],\n \"boolean\": false,\n \"null\": null,\n \"number\": 42,\n \"object\": {},\n \"string\": \"Hello world\"\n}");
}
SECTION("dump and floating-point numbers")
@ -1157,3 +1160,42 @@ TEST_CASE("object inspection")
}
}
}
TEST_CASE("value conversion")
{
SECTION("get an object (explicit)")
{
json::object_t o_reference = {{"object", json::object()}, {"array", {1, 2, 3, 4}}, {"number", 42}, {"boolean", false}, {"null", nullptr}, {"string", "Hello world"} };
json j(o_reference);
SECTION("json::object_t")
{
json::object_t o = j.get<std::map<std::string, json>>();
CHECK(json(o) == j);
}
SECTION("std::map<std::string, json>")
{
std::map<std::string, json> o = j.get<std::map<std::string, json>>();
CHECK(json(o) == j);
}
SECTION("std::multimap<std::string, json>")
{
std::multimap<std::string, json> o = j.get<std::multimap<std::string, json>>();
CHECK(json(o) == j);
}
SECTION("std::unordered_map<std::string, json>")
{
std::unordered_map<std::string, json> o = j.get<std::unordered_map<std::string, json>>();
CHECK(json(o) == j);
}
SECTION("std::unordered_multimap<std::string, json>")
{
std::unordered_multimap<std::string, json> o = j.get<std::unordered_multimap<std::string, json>>();
CHECK(json(o) == j);
}
}
}