Skip to content

Commit

Permalink
Skip enum tests when JSON_DisableEnumSerialization=ON (#4504)
Browse files Browse the repository at this point in the history
* ✅ skip enum tests when JSON_DisableEnumSerialization=ON

* ✅ skip enum tests when JSON_DisableEnumSerialization=ON
  • Loading branch information
nlohmann authored Nov 21, 2024
1 parent f9f8c07 commit a97041a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions tests/src/unit-conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nlohmann/json.hpp>
using nlohmann::json;
Expand Down Expand Up @@ -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 };
Expand All @@ -1292,6 +1298,7 @@ TEST_CASE("value conversion")
CHECK(json(value_1).get<c_enum>() == value_1);
CHECK(json(cpp_enum::value_1).get<cpp_enum>() == cpp_enum::value_1);
}
#endif

SECTION("more involved conversions")
{
Expand Down
17 changes: 13 additions & 4 deletions tests/src/unit-noexcept.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,12 +41,16 @@ static_assert(noexcept(json{}), "");
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), 2)), "");
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), 2.5)), "");
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), true)), "");
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), test{})), "");
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), pod{})), "");
#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), test {})), "");
#endif
static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), pod {})), "");
static_assert(!noexcept(nlohmann::to_json(std::declval<json&>(), 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<json>().get<pod>()), "");
static_assert(!noexcept(std::declval<json>().get<pod_bis>()), "");
static_assert(noexcept(json(pod{})), "");
Expand Down
7 changes: 7 additions & 0 deletions tests/src/unit-regression1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
// for some reason including this after the json header leads to linker errors with VS 2017...
#include <locale>

// 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 <nlohmann/json.hpp>
using nlohmann::json;
Expand Down Expand Up @@ -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};
Expand All @@ -191,6 +197,7 @@ TEST_CASE("regression tests 1")
{"game_type", t}
}));
}
#endif

SECTION("issue #76 - dump() / parse() not idempotent")
{
Expand Down
29 changes: 28 additions & 1 deletion tests/src/unit-udt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

}

Expand All @@ -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);
Expand All @@ -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")
Expand All @@ -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;
Expand All @@ -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
}
Expand Down

0 comments on commit a97041a

Please sign in to comment.