Build the tests with C++17/C++20-only sections in those standards

cmake/test.cmake only adds the _cpp17/_cpp20 variants of a unit test if the
file mentions JSON_HAS_CPP_17/JSON_HAS_CPP_20. Several files have sections
that need C++17 or C++20 (std::filesystem::path, std::string_view, char8_t,
std::u8string, operator<=>, ranges) but never mention the macros, so by
default these sections were only compiled away in the C++11 build and only
ran in the CI jobs that force a standard.

Mention the macros next to the guarded sections, as the other tests do:
unit-to_json-rvalue.cpp (C++17 and C++20), unit-json_view_builder.cpp
(C++17), unit-class_parser.cpp, unit-recursion_depth_limit.cpp,
unit-ubjson_valueless.cpp, unit-custom-number-types.cpp and
unit-allocator.cpp (C++20).

Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
Niels Lohmann committed 2026-11-16 12:40:54 +01:00
1 parent abbc29963a
commit 934b12a9e1
8 files changed
+61 -5

No files matched your search

+9 -1
View File
@@ -6,6 +6,13 @@
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
// cmake/test.cmake selects the C++ standard versions with which to build a
// unit test based on the presence of JSON_HAS_CPP_<VERSION> macros.
// When using macros that are only defined for particular versions of the standard
// (e.g., JSON_HAS_FILESYSTEM for C++17 and up), please mention the corresponding
// version macro in a comment close by, like this:
// JSON_HAS_CPP_<VERSION> (do not remove; see note at top of file)
#include "doctest_compatibility.h"
#include <nlohmann/json.hpp>
@@ -272,6 +279,7 @@ struct generator
TEST_CASE("json_view string_ref")
{
// std::string_view in C++17, a stand-in with the same members before
// JSON_HAS_CPP_17 (do not remove; see note at top of file)
using nlohmann::detail::view::string_ref;
const std::string text = "abc";
const string_ref r(text);
@@ -945,7 +953,7 @@ TEST_CASE("json_view document_data::holds() (the debug check of read())")
SECTION("current edit buffer")
{
d->edits.reset(new document_data::edit_state()); // NOLINT(cppcoreguidelines-owning-memory)
d->edits.reset(new document_data::edit_state()); // NOLINT(cppcoreguidelines-owning-memory,modernize-make-unique)
CHECK(!d->holds(other.data(), other.size()));
d->edits->texts.emplace_back(new char[16]); // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays,cppcoreguidelines-owning-memory)
d->edits->text_cap = 16;