mirror of
https://github.com/nlohmann/json.git
synced 2026-09-23 10:44:20 +00:00
* test: cover JSON_NO_IO, JSON_THROW/TRY/CATCH_USER, JSON_SKIP_LIBRARY_VERSION_CHECK, and JSON_DisableEnumSerialization in CI (#5423) These four supported configuration macros were never actually compiled anywhere in the test matrix: - JSON_NO_IO and the JSON_THROW_USER/JSON_TRY_USER/JSON_CATCH_USER trio are exercised together in a new tests/src/unit-no_io_and_user_exceptions.cpp, which is automatically picked up by the existing unit-*.cpp test glob and thus built across the whole standard test matrix. - JSON_SKIP_LIBRARY_VERSION_CHECK is exercised by a new, dedicated tests/src/skip_library_version_check.cpp, compiled directly by the new ci_test_skiplibraryversioncheck target in cmake/ci.cmake: the scenario it simulates (mixing two differently-versioned inclusions of the library) unavoidably triggers the compiler's own "macro redefined" warning, which would fail under the library's own -Weverything/-Werror unit test matrix for a reason unrelated to the macro under test. - JSON_DisableEnumSerialization already had #if-guarded tests in several unit-*.cpp files (from #4384), but no CMake target ever actually set the JSON_DisableEnumSerialization CMake option, so that guarded code was never compiled. Add ci_test_disableenumserialization, mirroring the existing ci_test_noimplicitconversions/ci_test_noglobaludls targets. Building the full test suite with this option on surfaced one real, narrow gap: get<T>() on std::vector<std::byte> (used by unit-regression2.cpp's custom BinaryType tests) relies on std::byte being handled via enum serialization, so add the same #if-guard convention to the two affected SECTIONs there. Both new CI targets are added to the ci_cmake_options matrix in .github/workflows/ubuntu.yml, alongside the existing ci_test_* targets. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * test: cover multi-digit widths and bare alignment in std::formatter<json> (#5423) Every existing std::formatter spec with a width used a single digit (e.g. "{:2}"), so the width-parsing loop's accumulation of a second/third digit was never exercised; add multi-digit width cases. Likewise, every existing spec with an alignment character also had an explicit fill character, so the bare-alignment branch (e.g. "{:<}", with no fill) was never exercised; add cases asserting it keeps the default space indent character. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * test: add coverage for patch_inplace() (#5423) patch_inplace() had no unit test at all. Add a happy-path case mirroring an existing patch() example, and -- more importantly -- pin its distinguishing contract versus patch(): when a multi-operation JSON Patch fails partway through, patch_inplace() (which mutates the document directly, operation by operation) leaves whatever operations already succeeded applied, whereas patch() (which applies the patch to an internal copy that is discarded on exception) leaves the original completely untouched either way. Verified empirically against the current implementation before writing the assertions. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * test: fix duplicate TEST_CASE name in unit-no-mem-leak-on-adl-serialize.cpp (#5423) Two distinct TEST_CASEs were both named "check_for_mem_leak_on_adl_to_json-2". doctest allows duplicate names, so both still ran, but it makes --test-case=<name> filtering and reporting ambiguous. Rename the second one to "-3", continuing the existing "-1"/"-2" sequence. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * test: add direct coverage for the std::u8string to_json overload (#5423) The ADL to_json overload for std::basic_string<char8_t, ...> was only ever reached indirectly, via std::filesystem::path::u8string(). Add a test that constructs a json value directly from a std::u8string, gated the same way as the overload itself (include/nlohmann/detail/conversions/to_json.hpp): behind both the std::filesystem::path feature guard and __cpp_lib_char8_t, since the overload only exists when both are satisfied. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * test: verify move semantics of byte_container_with_subtype's rvalue constructors (#5423) The two rvalue-reference constructors were never distinguished from their const-lvalue-reference twins by any test. Add a "move semantics" section that constructs from an rvalue std::vector, checks the resulting container keeps the exact same buffer address as the source (a stronger check than just observing the source ended up empty, since a copy-then-clear could do that too), and confirms the source vector was left empty. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Guard patch_inplace() partial-application test against JSON_NOEXCEPTION The "distinguishing contract vs patch(): partial application on failure" test relies on doc.patch_inplace(patch) actually throwing so the partially-applied state can be observed right after the throw point. Under ci_test_noexceptions, JSON_THROW() calls std::abort() instead of throwing, and doctest's --no-throw test filter (which that CI job passes) makes CHECK_THROWS_AS() a no-op that never even evaluates its expression -- so patch_inplace() is never called and the follow-up assertions fail against the untouched original document. Guard the whole SECTION with #if !defined(JSON_NOEXCEPTION), following the same convention already used elsewhere in the test suite (e.g. unit-class_parser.cpp) for exception-dependent tests. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix MSVC C2220 in the std::u8string conversion test MSVC's C5321 ("nonstandard extension used: encoding '\xNN' as a multi-byte utf-8 character") is promoted to a hard error by our MSVC CI configs. It fires because the test composed a non-ASCII UTF-8 sequence inside a u8"" literal using raw \x byte escapes; MSVC treats that as nonstandard and suggests using \u universal-character-names instead, which every compiler agrees on and which compiles down to the exact same encoded bytes. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Guard the JSON_THROW_USER test against JSON_NOEXCEPTION and GCC's -Wunused-result Two independent CI configurations failed to build/run this new test: - ci_test_noexceptions runs the whole suite with -DJSON_NOEXCEPTION and doctest's "--no-throw" filter, which compiles CHECK_THROWS_AS() down to a no-op that never even invokes the guarded expression. Since this test's whole point is to observe json_throw_user_call_count after json::parse()/at() actually throw, it can't be meaningfully run under that filter (our JSON_THROW_USER override still throws real exceptions regardless of JSON_NOEXCEPTION, but the assertion never gets a chance to run). Guard the TEST_CASE with #if !defined(JSON_NOEXCEPTION), mirroring the existing precedent in unit-json_patch.cpp. - ci_test_gcc and ci_test_standards_gcc(11) failed with -Werror=unused-result on the discarded json::parse() return value. json::parse() is marked warn_unused_result, and unlike a real [[nodiscard]] attribute, GCC does not consider that satisfied by doctest's (void)-cast around the expression in C++11 mode. Assign the result to a discarded local instead, matching the established `json _ = json::parse(...)` idiom already used throughout unit-class_parser.cpp. Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Suppress a clang-tidy false positive on an intentional defensive copy performance-unnecessary-copy-initialization suggests copy_for_patch could be a reference since it's never modified -- but the copy is the point: it guards against a hypothetical regression where patch() mutates its receiver, which a reference could never catch (the follow-up assertion would just compare `original` to itself). Signed-off-by: Niels Lohmann <mail@nlohmann.me> * Fix clang-tidy findings in the JSON_NO_IO/JSON_THROW_USER test - bugprone-macro-parentheses: wrap the JSON_THROW_USER macro argument in parentheses at the throw site. - modernize-raw-string-literal: switch two escaped JSON string literals to raw string literals. Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
92 lines
4.6 KiB
C++
92 lines
4.6 KiB
C++
// __ _____ _____ _____
|
|
// __| | __| | | | JSON for Modern C++ (supporting code)
|
|
// | | |__ | | | | | | version 3.12.0
|
|
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
//
|
|
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// This translation unit is a dedicated, small compile-and-run check for two
|
|
// configuration macros that (per #5423) were never exercised anywhere in the
|
|
// test matrix:
|
|
// - JSON_NO_IO, which removes the library's <istream>/<ostream> support
|
|
// (operator<<, operator>>, and the stream-based overloads of dump()/parse())
|
|
// - the JSON_THROW_USER / JSON_TRY_USER / JSON_CATCH_USER trio, which lets a
|
|
// user replace the library's internal exception handling
|
|
//
|
|
// Both macros are about excluding/replacing a facility the library would
|
|
// otherwise pull in on its own, and defining one has no bearing on the other,
|
|
// so -- to keep the test matrix small -- they are exercised together in a
|
|
// single dedicated file instead of two.
|
|
//
|
|
// JSON_NO_IO requires this file itself to never rely on <iostream>/<sstream>;
|
|
// only string-based parsing/dumping is used below.
|
|
#define JSON_NO_IO 1
|
|
|
|
// The user-supplied exception macros below are a *conforming* replacement:
|
|
// they simply forward to the real throw/try/catch keywords (via a counter so
|
|
// the test can assert each macro was actually invoked, not just defined), so
|
|
// every exception-related behavior the library relies on internally --
|
|
// including rethrowing std::out_of_range as json::out_of_range in at() --
|
|
// keeps working exactly as it would with the library's own default macros.
|
|
static int json_throw_user_call_count = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
|
|
#define JSON_THROW_USER(exception) do { ++json_throw_user_call_count; throw (exception); } while (false) // NOLINT(cppcoreguidelines-macro-usage)
|
|
#define JSON_TRY_USER try // NOLINT(cppcoreguidelines-macro-usage)
|
|
#define JSON_CATCH_USER(exception) catch (exception) // NOLINT(cppcoreguidelines-macro-usage)
|
|
|
|
#include "doctest_compatibility.h"
|
|
|
|
#include <nlohmann/json.hpp>
|
|
using json = nlohmann::json;
|
|
|
|
TEST_CASE("JSON_NO_IO")
|
|
{
|
|
// everything that does not touch <istream>/<ostream> must keep working:
|
|
// parsing from and dumping to std::string
|
|
const json j = json::parse(R"({"a":[1,2,3],"b":true})");
|
|
CHECK(j.dump() == R"({"a":[1,2,3],"b":true})");
|
|
CHECK(j.at("a").size() == 3);
|
|
CHECK(j.at("b").get<bool>() == true);
|
|
}
|
|
|
|
// this test relies on CHECK_THROWS_AS() actually invoking the guarded
|
|
// expression so json_throw_user_call_count gets bumped and can be observed
|
|
// afterwards; doctest's "--no-throw" test filter (which ci_test_noexceptions
|
|
// passes, together with a global -DJSON_NOEXCEPTION added to CMAKE_CXX_FLAGS
|
|
// for every translation unit in that build, this file included) compiles
|
|
// CHECK_THROWS_AS() out to a no-op that never even invokes the given
|
|
// expression -- so json::parse()/at() below would never be called at all and
|
|
// the call-count assertions would fail even though our JSON_THROW_USER
|
|
// override (which always really throws, regardless of JSON_NOEXCEPTION) would
|
|
// have worked fine on its own
|
|
#if !defined(JSON_NOEXCEPTION)
|
|
TEST_CASE("JSON_THROW_USER, JSON_TRY_USER, JSON_CATCH_USER")
|
|
{
|
|
json_throw_user_call_count = 0;
|
|
|
|
// json::parse() is [[nodiscard]] (JSON_HEDLEY_WARN_UNUSED_RESULT); under
|
|
// GCC in C++11 mode that expands to __attribute__((warn_unused_result)),
|
|
// which -- unlike a [[nodiscard]] attribute proper -- GCC does not
|
|
// consider satisfied by doctest's CHECK_THROWS_AS() wrapping the
|
|
// expression in a (void) cast, so the discarded return value would still
|
|
// be flagged under -Werror=unused-result; assign it to discard it instead,
|
|
// matching the established `json _ = json::parse(...)` pattern used
|
|
// elsewhere in the test suite (see unit-class_parser.cpp)
|
|
json _; // NOLINT(readability-identifier-naming)
|
|
|
|
// a parse error goes through JSON_THROW directly, i.e., through our
|
|
// JSON_THROW_USER override
|
|
CHECK_THROWS_AS(_ = json::parse("this is not JSON"), json::parse_error&);
|
|
CHECK(json_throw_user_call_count > 0);
|
|
|
|
// at() on an out-of-range array index internally catches std::out_of_range
|
|
// (JSON_TRY_USER/JSON_CATCH_USER) and rethrows it as json::out_of_range
|
|
// (JSON_THROW_USER again), so this exercises all three macros together
|
|
const int count_before = json_throw_user_call_count;
|
|
const json arr = json::array({1, 2, 3});
|
|
CHECK_THROWS_AS(arr.at(10), json::out_of_range&);
|
|
CHECK(json_throw_user_call_count > count_before);
|
|
}
|
|
#endif
|