From 37cd4347848622984dd0dbbe381fd6d9f2dedbd8 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 21 Oct 2026 19:21:53 +0200 Subject: [PATCH] Move the std::vector tests to unit-custom-binary-type-cpp17.cpp The develop merge kept the test case that #5809 moved from unit-regression3.cpp in unit-custom-binary-type.cpp, but byte_binary_json is only defined in the C++17 file now, so the C++17 build of the former failed. Signed-off-by: Niels Lohmann --- tests/src/unit-custom-binary-type-cpp17.cpp | 55 ++++++++++++++++++++ tests/src/unit-custom-binary-type.cpp | 57 --------------------- 2 files changed, 55 insertions(+), 57 deletions(-) diff --git a/tests/src/unit-custom-binary-type-cpp17.cpp b/tests/src/unit-custom-binary-type-cpp17.cpp index 5d4897daf..cd4ceb391 100644 --- a/tests/src/unit-custom-binary-type-cpp17.cpp +++ b/tests/src/unit-custom-binary-type-cpp17.cpp @@ -13,6 +13,13 @@ #include "doctest_compatibility.h" +// capture whether JSON_DISABLE_ENUM_SERIALIZATION was enabled on the command +// line before including json.hpp, which #undefs it; std::byte is an enum, so +// converting std::vector needs enum serialization +#if defined(JSON_DISABLE_ENUM_SERIALIZATION) && (JSON_DISABLE_ENUM_SERIALIZATION == 1) + #define SKIP_TESTS_FOR_ENUM_SERIALIZATION +#endif + #include #ifdef JSON_HAS_CPP_17 @@ -53,4 +60,52 @@ TEST_CASE("binary type whose value type is not std::uint8_t (C++17)") } } +// moved from unit-regression3.cpp, so that file does not instantiate a second +// basic_json specialization (MinGW section limit, see windows.yml) +TEST_CASE("std::vector as binary type") +{ + SECTION("issue #4804: from_cbor incompatible with std::vector as binary_t") + { + const std::vector data = {0x80}; + const auto decoded = byte_binary_json::from_cbor(data); + CHECK((decoded == byte_binary_json::array())); + } + +#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION + SECTION("discussion #4209 - custom BinaryType direct assignment and round-tripping") + { + // Test that assigning a custom BinaryType directly creates a binary value, not an array + const std::vector original{std::byte{1}, std::byte{2}, std::byte{3}}; + const byte_binary_json j = original; + CHECK(j.is_binary()); + CHECK(!j.is_array()); + + // Test round-tripping: extracting the binary value back as the custom container type + const auto extracted = j.get>(); + CHECK(extracted == original); + + // Test that the default json alias behavior is unchanged: std::vector -> array + const nlohmann::json default_json = std::vector {1, 2, 3}; + CHECK(default_json.is_array()); + CHECK(!default_json.is_binary()); + } + + SECTION("discussion #4209 - custom BinaryType extraction from parsed array") + { + // Test that extracting a custom BinaryType from a parsed JSON array still works + // (not just from a binary-typed node) + const auto j = byte_binary_json::parse("[1,2,3]"); + CHECK(j.is_array()); + CHECK(!j.is_binary()); + + // Extracting as custom BinaryType should work from arrays + const auto extracted = j.get>(); + CHECK(extracted.size() == 3); + CHECK(extracted[0] == std::byte{1}); + CHECK(extracted[1] == std::byte{2}); + CHECK(extracted[2] == std::byte{3}); + } +#endif +} + #endif diff --git a/tests/src/unit-custom-binary-type.cpp b/tests/src/unit-custom-binary-type.cpp index 012adfc82..31f4b8461 100644 --- a/tests/src/unit-custom-binary-type.cpp +++ b/tests/src/unit-custom-binary-type.cpp @@ -8,13 +8,6 @@ #include "doctest_compatibility.h" -// capture whether JSON_DISABLE_ENUM_SERIALIZATION was enabled on the command -// line before including json.hpp, which #undefs it; std::byte is an enum, so -// converting std::vector needs enum serialization -#if defined(JSON_DISABLE_ENUM_SERIALIZATION) && (JSON_DISABLE_ENUM_SERIALIZATION == 1) - #define SKIP_TESTS_FOR_ENUM_SERIALIZATION -#endif - #include #include @@ -58,53 +51,3 @@ TEST_CASE("binary type whose value type is not std::uint8_t") CHECK(nlohmann::json::binary({0, 1, 255}, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})"); } } - -#ifdef JSON_HAS_CPP_17 -// moved from unit-regression3.cpp, so that file does not instantiate a second -// basic_json specialization (MinGW section limit, see windows.yml) -TEST_CASE("std::vector as binary type") -{ - SECTION("issue #4804: from_cbor incompatible with std::vector as binary_t") - { - const std::vector data = {0x80}; - const auto decoded = byte_binary_json::from_cbor(data); - CHECK((decoded == byte_binary_json::array())); - } - -#ifndef SKIP_TESTS_FOR_ENUM_SERIALIZATION - SECTION("discussion #4209 - custom BinaryType direct assignment and round-tripping") - { - // Test that assigning a custom BinaryType directly creates a binary value, not an array - const std::vector original{std::byte{1}, std::byte{2}, std::byte{3}}; - const byte_binary_json j = original; - CHECK(j.is_binary()); - CHECK(!j.is_array()); - - // Test round-tripping: extracting the binary value back as the custom container type - const auto extracted = j.get>(); - CHECK(extracted == original); - - // Test that the default json alias behavior is unchanged: std::vector -> array - const nlohmann::json default_json = std::vector {1, 2, 3}; - CHECK(default_json.is_array()); - CHECK(!default_json.is_binary()); - } - - SECTION("discussion #4209 - custom BinaryType extraction from parsed array") - { - // Test that extracting a custom BinaryType from a parsed JSON array still works - // (not just from a binary-typed node) - const auto j = byte_binary_json::parse("[1,2,3]"); - CHECK(j.is_array()); - CHECK(!j.is_binary()); - - // Extracting as custom BinaryType should work from arrays - const auto extracted = j.get>(); - CHECK(extracted.size() == 3); - CHECK(extracted[0] == std::byte{1}); - CHECK(extracted[1] == std::byte{2}); - CHECK(extracted[2] == std::byte{3}); - } -#endif -} -#endif