From a97041a98fc7436cda3b28fb6e353071829d3f02 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 21 Nov 2024 17:19:16 +0100 Subject: [PATCH] Skip enum tests when JSON_DisableEnumSerialization=ON (#4504) * :white_check_mark: skip enum tests when JSON_DisableEnumSerialization=ON * :white_check_mark: skip enum tests when JSON_DisableEnumSerialization=ON --- CMakeLists.txt | 6 +++++- tests/src/unit-conversions.cpp | 7 +++++++ tests/src/unit-noexcept.cpp | 17 +++++++++++++---- tests/src/unit-regression1.cpp | 7 +++++++ tests/src/unit-udt.cpp | 29 ++++++++++++++++++++++++++++- 5 files changed, 60 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bec5676ab..df61fc332 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,7 @@ endif() option(JSON_BuildTests "Build the unit tests when BUILD_TESTING is enabled." ${JSON_BuildTests_INIT}) option(JSON_CI "Enable CI build targets." OFF) option(JSON_Diagnostics "Use extended diagnostic messages." OFF) -option(JSON_GlobalUDLs "Place use-defined string literals in the global namespace." ON) +option(JSON_GlobalUDLs "Place user-defined string literals in the global namespace." ON) option(JSON_ImplicitConversions "Enable implicit conversions." ON) option(JSON_DisableEnumSerialization "Disable default integer enum serialization." OFF) option(JSON_LegacyDiscardedValueComparison "Enable legacy discarded value comparison." OFF) @@ -96,6 +96,10 @@ if (JSON_Diagnostics) message(STATUS "Diagnostics enabled") endif() +if (NOT JSON_GlobalUDLs) + message(STATUS "User-defined string literals are not put in the global namespace") +endif() + if (JSON_SystemInclude) set(NLOHMANN_JSON_SYSTEM_INCLUDE "SYSTEM") endif() diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 911fbcaad..80e70cedd 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -16,6 +16,11 @@ #include "doctest_compatibility.h" +// 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 + #define JSON_TESTS_PRIVATE #include using nlohmann::json; @@ -1284,6 +1289,7 @@ TEST_CASE("value conversion") } #endif +#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION SECTION("get an enum") { enum c_enum { value_1, value_2 }; @@ -1292,6 +1298,7 @@ TEST_CASE("value conversion") CHECK(json(value_1).get() == value_1); CHECK(json(cpp_enum::value_1).get() == cpp_enum::value_1); } +#endif SECTION("more involved conversions") { diff --git a/tests/src/unit-noexcept.cpp b/tests/src/unit-noexcept.cpp index bba230d01..9c07b6122 100644 --- a/tests/src/unit-noexcept.cpp +++ b/tests/src/unit-noexcept.cpp @@ -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 using nlohmann::json; @@ -36,12 +41,16 @@ static_assert(noexcept(json{}), ""); static_assert(noexcept(nlohmann::to_json(std::declval(), 2)), ""); static_assert(noexcept(nlohmann::to_json(std::declval(), 2.5)), ""); static_assert(noexcept(nlohmann::to_json(std::declval(), true)), ""); -static_assert(noexcept(nlohmann::to_json(std::declval(), test{})), ""); -static_assert(noexcept(nlohmann::to_json(std::declval(), pod{})), ""); +#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION +static_assert(noexcept(nlohmann::to_json(std::declval(), test {})), ""); +#endif +static_assert(noexcept(nlohmann::to_json(std::declval(), pod {})), ""); static_assert(!noexcept(nlohmann::to_json(std::declval(), pod_bis{})), ""); static_assert(noexcept(json(2)), ""); -static_assert(noexcept(json(test{})), ""); -static_assert(noexcept(json(pod{})), ""); +#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION +static_assert(noexcept(json(test {})), ""); +#endif +static_assert(noexcept(json(pod {})), ""); static_assert(noexcept(std::declval().get()), ""); static_assert(!noexcept(std::declval().get()), ""); static_assert(noexcept(json(pod{})), ""); diff --git a/tests/src/unit-regression1.cpp b/tests/src/unit-regression1.cpp index f5ef5d80b..167c27791 100644 --- a/tests/src/unit-regression1.cpp +++ b/tests/src/unit-regression1.cpp @@ -11,6 +11,11 @@ // for some reason including this after the json header leads to linker errors with VS 2017... #include +// 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 + #define JSON_TESTS_PRIVATE #include using nlohmann::json; @@ -169,6 +174,7 @@ TEST_CASE("regression tests 1") } } +#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION SECTION("pull request #71 - handle enum type") { enum { t = 0, u = 102}; @@ -191,6 +197,7 @@ TEST_CASE("regression tests 1") {"game_type", t} })); } +#endif SECTION("issue #76 - dump() / parse() not idempotent") { diff --git a/tests/src/unit-udt.cpp b/tests/src/unit-udt.cpp index 57cf278d8..a0befd8aa 100644 --- a/tests/src/unit-udt.cpp +++ b/tests/src/unit-udt.cpp @@ -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 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(); +#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION cb.m_book_id = j["id"].get(); +#endif cb.m_contacts = j["contacts"].get>(); } } // 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(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(); const auto book_name = big_json["name"].get(); +#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION const auto book_id = big_json["id"].get(); +#endif const auto contacts = big_json["contacts"].get>(); 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 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(42u)); CHECK(book == parsed_book); +#endif } #endif }