mirror of
https://github.com/nlohmann/json.git
synced 2026-09-30 14:05:18 +00:00
Fix CI: json_view tests without exceptions, on clang 3.6, and single header
- ci_test_noexceptions: the helpers that compare the exceptions of json_document::parse() and json::parse() catch them outside a CHECK_THROWS, so with JSON_NOEXCEPTION the first parse error aborted the test. Compile those comparisons only with exceptions, as unit-class_parser.cpp does. - ci_test_gcc: -Werror=unused-result for CHECK_THROWS_AS(json_document:: parse(...)); assign the result to a dummy document. - ci_test_compilers_clang (3.6): `const json_view invalid;` needs a user-provided default constructor there (CWG 253); value-initialize it. - ci_test_single_header: json_view.hpp now exists as a single header and contains the internal view headers, so unit-json_view_builder.cpp includes it instead of the detail headers in that mode, and the test is built again with the single header. - Regenerate single_include/nlohmann/json_view.hpp for the builder change merged from json-view/08-view-builder. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -955,6 +955,14 @@ class builder
|
||||
return w;
|
||||
}
|
||||
|
||||
/// a compile-time option as a runtime condition: testing the template
|
||||
/// argument directly makes a condition like `TrailingCommas && c == ']'`
|
||||
/// constant when the option is off, which MSVC reports as C4127
|
||||
static NLOHMANN_VIEW_ALWAYS_INLINE bool enabled(bool option) noexcept
|
||||
{
|
||||
return option;
|
||||
}
|
||||
|
||||
/// The parse state and the parser proper. The cursor is a local object of
|
||||
/// run() whose address never escapes (everything it calls out of line is a
|
||||
/// member of the builder and gets the positions it needs), so that the
|
||||
@@ -1084,7 +1092,7 @@ arr_next:
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (TrailingCommas && cur() == ']')
|
||||
if (enabled(TrailingCommas) && cur() == ']')
|
||||
{
|
||||
++p;
|
||||
goto close_container;
|
||||
@@ -1151,7 +1159,7 @@ obj_next:
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (TrailingCommas && cur() == '}')
|
||||
if (enabled(TrailingCommas) && cur() == '}')
|
||||
{
|
||||
++p;
|
||||
goto close_container;
|
||||
@@ -1293,7 +1301,7 @@ indent_done:
|
||||
{
|
||||
++p;
|
||||
}
|
||||
if (Comments && cur() == '/')
|
||||
if (enabled(Comments) && cur() == '/')
|
||||
{
|
||||
const unsigned char* const q = cold.comment(p);
|
||||
if (q == nullptr)
|
||||
|
||||
@@ -279,10 +279,6 @@ if(json_32bit_test_only)
|
||||
elseif(NOT json_32bit_test)
|
||||
list(FILTER files EXCLUDE REGEX src/unit-32bit.cpp)
|
||||
endif()
|
||||
if(NOT JSON_MultipleHeaders)
|
||||
# the internal headers of json_view are not part of a single header yet
|
||||
list(FILTER files EXCLUDE REGEX src/unit-json_view_builder.cpp)
|
||||
endif()
|
||||
|
||||
foreach(file ${files})
|
||||
json_test_add_test_for(${file} MAIN test_main CXX_STANDARDS ${test_cxx_standards} ${test_force})
|
||||
|
||||
@@ -29,6 +29,7 @@ using nlohmann::ordered_json_document;
|
||||
|
||||
namespace
|
||||
{
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
// the exception parse() throws for a text, or "" if it accepts it
|
||||
std::string parse_exception(const std::string& text, bool comments = false, bool trailing_commas = false)
|
||||
{
|
||||
@@ -57,6 +58,7 @@ std::string view_exception(const std::string& text, bool comments = false, bool
|
||||
}
|
||||
return "";
|
||||
}
|
||||
#endif
|
||||
|
||||
// a small deterministic generator of documents
|
||||
struct generator
|
||||
@@ -146,7 +148,7 @@ TEST_CASE("json_view")
|
||||
CHECK(v.materialize() == j);
|
||||
}
|
||||
|
||||
const json_view invalid;
|
||||
const json_view invalid{};
|
||||
CHECK(invalid.is_discarded());
|
||||
CHECK(!static_cast<bool>(invalid));
|
||||
CHECK(invalid.type() == json::value_t::discarded);
|
||||
@@ -191,9 +193,11 @@ TEST_CASE("json_view")
|
||||
})
|
||||
{
|
||||
CAPTURE(text);
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
const std::string expected = parse_exception(text);
|
||||
REQUIRE(!expected.empty());
|
||||
CHECK(view_exception(text) == expected);
|
||||
#endif
|
||||
CHECK(!json_document::accept(text));
|
||||
const json_document d = json_document::parse(text, false);
|
||||
CHECK(d.is_discarded());
|
||||
@@ -201,8 +205,9 @@ TEST_CASE("json_view")
|
||||
CHECK(d.node_count() == 0);
|
||||
}
|
||||
// the exception types
|
||||
CHECK_THROWS_AS(json_document::parse("[1,"), json::parse_error&);
|
||||
CHECK_THROWS_AS(json_document::parse("1e400"), json::out_of_range&);
|
||||
json_document _;
|
||||
CHECK_THROWS_AS(_ = json_document::parse("[1,"), json::parse_error&);
|
||||
CHECK_THROWS_AS(_ = json_document::parse("1e400"), json::out_of_range&);
|
||||
}
|
||||
|
||||
SECTION("parse options")
|
||||
@@ -217,7 +222,9 @@ TEST_CASE("json_view")
|
||||
const bool comments = (options & 1) != 0;
|
||||
const bool trailing_commas = (options & 2) != 0;
|
||||
CHECK(json_document::accept(text, comments, trailing_commas) == json::accept(text, comments, trailing_commas));
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
CHECK(view_exception(text, comments, trailing_commas) == parse_exception(text, comments, trailing_commas));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,7 +236,9 @@ TEST_CASE("json_view")
|
||||
const std::string nul_in_comment("[1, // c\0\n2]", 12);
|
||||
CHECK(json_document::accept(nul_in_comment, true) == json::accept(nul_in_comment, true));
|
||||
CHECK(json_document::parse("\xEF\xBB\xBF[1]").root().materialize() == json::parse("\xEF\xBB\xBF[1]"));
|
||||
#if !defined(JSON_NOEXCEPTION)
|
||||
CHECK(view_exception("\xEF\xBB") == parse_exception("\xEF\xBB"));
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("inputs")
|
||||
|
||||
@@ -9,8 +9,12 @@
|
||||
#include "doctest_compatibility.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <nlohmann/detail/view/builder.hpp>
|
||||
#include <nlohmann/detail/view/string_ref.hpp>
|
||||
#if JSON_TEST_USING_MULTIPLE_HEADERS
|
||||
#include <nlohmann/detail/view/builder.hpp>
|
||||
#include <nlohmann/detail/view/string_ref.hpp>
|
||||
#else
|
||||
#include <nlohmann/json_view.hpp> // the single header contains the internal headers
|
||||
#endif
|
||||
using nlohmann::json;
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
Reference in New Issue
Block a user