From 27468fa77ef4969dcb156781c5fa0823baee17da Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 29 Sep 2026 09:53:48 +0200 Subject: [PATCH] Test the C++11 stand-in for std::string_view of json_view Six members of string_ref (length, begin, end, operator[], operator!=, and operator<<) were not reached before C++17, where string_ref is std::string_view. Signed-off-by: Niels Lohmann --- tests/src/unit-json_view_builder.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/src/unit-json_view_builder.cpp b/tests/src/unit-json_view_builder.cpp index 065bf0cee..4584e8728 100644 --- a/tests/src/unit-json_view_builder.cpp +++ b/tests/src/unit-json_view_builder.cpp @@ -10,6 +10,7 @@ #include #include +#include using nlohmann::json; #include @@ -214,6 +215,22 @@ struct generator }; } // namespace +TEST_CASE("json_view string_ref") +{ + // std::string_view in C++17, a stand-in with the same members before + using nlohmann::detail::view::string_ref; + const std::string text = "abc"; + const string_ref r(text); + CHECK(r.length() == 3); + CHECK(std::string(r.begin(), r.end()) == "abc"); + CHECK(r[1] == 'b'); + CHECK(r != string_ref("abd")); + CHECK_FALSE(r != string_ref("abcd", 3)); + std::ostringstream o; + o << r; + CHECK(o.str() == "abc"); +} + TEST_CASE("json_view builder") { SECTION("scalars and containers")