mirror of
https://github.com/nlohmann/json.git
synced 2026-03-04 16:26:26 +00:00
Skip enum tests when JSON_DisableEnumSerialization=ON (#4504)
* ✅ skip enum tests when JSON_DisableEnumSerialization=ON * ✅ skip enum tests when JSON_DisableEnumSerialization=ON
This commit is contained in:
@@ -12,6 +12,11 @@
|
||||
DOCTEST_GCC_SUPPRESS_WARNING_PUSH
|
||||
DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept")
|
||||
|
||||
// skip tests if JSON_DisableEnumSerialization=ON (#4384)
|
||||
#if defined(JSON_DISABLE_ENUM_SERIALIZATION) && (JSON_DISABLE_ENUM_SERIALIZATION == 1)
|
||||
#define SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
#endif
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
using nlohmann::json;
|
||||
#ifdef JSON_TEST_NO_GLOBAL_UDLS
|
||||
@@ -132,7 +137,11 @@ static void to_json(nlohmann::json& j, const contact& c)
|
||||
|
||||
static void to_json(nlohmann::json& j, const contact_book& cb)
|
||||
{
|
||||
j = json{{"name", cb.m_book_name}, {"id", cb.m_book_id}, {"contacts", cb.m_contacts}};
|
||||
j = json{{"name", cb.m_book_name},
|
||||
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
{"id", cb.m_book_id},
|
||||
#endif
|
||||
{"contacts", cb.m_contacts}};
|
||||
}
|
||||
|
||||
// operators
|
||||
@@ -222,7 +231,9 @@ static void from_json(const nlohmann::json& j, contact& c)
|
||||
static void from_json(const nlohmann::json& j, contact_book& cb)
|
||||
{
|
||||
cb.m_book_name = j["name"].get<name>();
|
||||
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
cb.m_book_id = j["id"].get<book_id>();
|
||||
#endif
|
||||
cb.m_contacts = j["contacts"].get<std::vector<contact>>();
|
||||
}
|
||||
} // namespace udt
|
||||
@@ -253,14 +264,22 @@ TEST_CASE("basic usage" * doctest::test_suite("udt"))
|
||||
CHECK(json("Paris") == json(addr));
|
||||
CHECK(json(cpp_programmer) ==
|
||||
R"({"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"})"_json);
|
||||
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
CHECK(json(large_id) == json(static_cast<std::uint64_t>(1) << 63));
|
||||
CHECK(json(large_id) > 0u);
|
||||
CHECK(to_string(json(large_id)) == "9223372036854775808");
|
||||
CHECK(json(large_id).is_number_unsigned());
|
||||
#endif
|
||||
|
||||
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
CHECK(
|
||||
json(book) ==
|
||||
R"({"name":"C++", "id":42, "contacts" : [{"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"}, {"person" : {"age":42, "country":"中华人民共和国", "name":"王芳"}, "address":"Paris"}]})"_json);
|
||||
#else
|
||||
CHECK(
|
||||
json(book) ==
|
||||
R"({"name":"C++", "contacts" : [{"person" : {"age":23, "name":"theo", "country":"France"}, "address":"Paris"}, {"person" : {"age":42, "country":"中华人民共和国", "name":"王芳"}, "address":"Paris"}]})"_json);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -272,7 +291,9 @@ TEST_CASE("basic usage" * doctest::test_suite("udt"))
|
||||
{
|
||||
const auto parsed_book = big_json.get<udt::contact_book>();
|
||||
const auto book_name = big_json["name"].get<udt::name>();
|
||||
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
const auto book_id = big_json["id"].get<udt::book_id>();
|
||||
#endif
|
||||
const auto contacts =
|
||||
big_json["contacts"].get<std::vector<udt::contact>>();
|
||||
const auto contact_json = big_json["contacts"].at(0);
|
||||
@@ -292,8 +313,10 @@ TEST_CASE("basic usage" * doctest::test_suite("udt"))
|
||||
CHECK(contact == cpp_programmer);
|
||||
CHECK(contacts == book.m_contacts);
|
||||
CHECK(book_name == udt::name{"C++"});
|
||||
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
CHECK(book_id == book.m_book_id);
|
||||
CHECK(book == parsed_book);
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("via explicit calls to get_to")
|
||||
@@ -314,7 +337,9 @@ TEST_CASE("basic usage" * doctest::test_suite("udt"))
|
||||
{
|
||||
const udt::contact_book parsed_book = big_json;
|
||||
const udt::name book_name = big_json["name"];
|
||||
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
const udt::book_id book_id = big_json["id"];
|
||||
#endif
|
||||
const std::vector<udt::contact> contacts = big_json["contacts"];
|
||||
const auto contact_json = big_json["contacts"].at(0);
|
||||
const udt::contact contact = contact_json;
|
||||
@@ -332,8 +357,10 @@ TEST_CASE("basic usage" * doctest::test_suite("udt"))
|
||||
CHECK(contact == cpp_programmer);
|
||||
CHECK(contacts == book.m_contacts);
|
||||
CHECK(book_name == udt::name{"C++"});
|
||||
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
|
||||
CHECK(book_id == static_cast<udt::book_id>(42u));
|
||||
CHECK(book == parsed_book);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user