From 0e535ba3bc159d29852d5e620c954deb79bad576 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 11 Jul 2026 18:49:11 +0200 Subject: [PATCH] Extend std::optional null regression coverage (assignment, get_to) Adds two cases to the existing null-handling regression test for std::optional: assignment (opt = json_null) throws for the same reason as direct construction, and get_to() correctly yields std::nullopt. Prompted by a review comment on #5246 asking for a fuller regression matrix; construction and get>() were already covered by #5247. Signed-off-by: Niels Lohmann --- tests/src/unit-conversions.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index ad6623401..d290659d5 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1782,6 +1782,17 @@ TEST_CASE("std::optional") "[json.exception.type_error.302] type must be string, but is null", json::type_error&); CHECK_THROWS_WITH_AS(std::optional(j_null), "[json.exception.type_error.302] type must be number, but is null", json::type_error&); + + // Assignment goes through the same overload resolution as direct + // construction, so it throws for the same reason. + std::optional opt_assign; + CHECK_THROWS_WITH_AS(opt_assign = j_null, + "[json.exception.type_error.302] type must be string, but is null", json::type_error&); + + // get_to() is the correct way to obtain std::nullopt from a JSON null. + std::optional opt_get_to = "placeholder"; + j_null.get_to(opt_get_to); + CHECK(opt_get_to == std::nullopt); } SECTION("string")