add a few tests

This commit is contained in:
Théo DELRIEU 2016-11-26 01:31:06 +01:00 committed by Théo DELRIEU
parent 74bb11d92c
commit e5999c6c82
2 changed files with 41 additions and 2 deletions

View file

@ -289,6 +289,8 @@ struct is_compatible_basic_json_type
T>::value;
};
// This trait checks if JSONSerializer<T>::from_json exists
template <template <typename, typename> class JSONSerializer, typename Json,
typename T>
struct has_from_json
@ -305,6 +307,7 @@ public:
detect(std::declval<JSONSerializer<T, void>>()))>::value;
};
// This trait checks if JSONSerializer<T>::to_json exists
template <template <typename, typename> class JSONSerializer, typename Json,
typename T>
struct has_to_json
@ -322,7 +325,7 @@ public:
};
// those declarations are needed to workaround a MSVC bug related to ADL
// (idea taken from MSVC-Ranges implementation
// (taken from MSVC-Ranges implementation)
void to_json();
void from_json();

View file

@ -397,6 +397,29 @@ struct my_serializer
}
};
// partial specialization on optional_type
template <typename T>
struct my_serializer<udt::optional_type<T>>
{
template <typename Json>
static void from_json(Json const& j, udt::optional_type<T>& opt)
{
if (j.is_null())
opt = nullptr;
else
opt = j.get<T>();
}
template <typename Json>
static void to_json(Json& j, udt::optional_type<T> const& opt)
{
if (opt)
j = *opt;
else
j = nullptr;
}
};
using my_json = nlohmann::basic_json<std::map, std::vector, std::string, bool,
std::int64_t, std::uint64_t, double,
std::allocator, my_serializer>;
@ -414,7 +437,7 @@ namespace udt
}
}
TEST_CASE("custom serializer")
TEST_CASE("custom serializer", "[udt]")
{
SECTION("default use works like default serializer")
{
@ -429,4 +452,17 @@ TEST_CASE("custom serializer")
CHECK(pod2 == pod3);
CHECK(pod2 == pod);
}
SECTION("serializer specialization")
{
udt::optional_type<int> opt;
json j{opt};
CHECK(j.is_null());
opt = 42;
j = json{opt};
CHECK(j.get<udt::optional_type<int>>() == opt);
CHECK(42 == j.get<int>());
}
}