1 // __ _____ _____ _____
2 // __| | __| | | | JSON for Modern C++ (supporting code)
3 // | | |__ | | | | | | version 3.11.2
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 //
6 // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
7 // SPDX-License-Identifier: MIT
8
9 #include "doctest_compatibility.h"
10
11 // disable -Wnoexcept due to struct pod_bis
12 DOCTEST_GCC_SUPPRESS_WARNING_PUSH
13 DOCTEST_GCC_SUPPRESS_WARNING("-Wnoexcept")
14
15 #include <nlohmann/json.hpp>
16
17 using nlohmann::json;
18
19 namespace
20 {
21 enum test
22 {
23 };
24
25 struct pod {};
26 struct pod_bis {};
27
28 void to_json(json& /*unused*/, pod /*unused*/) noexcept;
29 void to_json(json& /*unused*/, pod_bis /*unused*/);
30 void from_json(const json& /*unused*/, pod /*unused*/) noexcept;
31 void from_json(const json& /*unused*/, pod_bis /*unused*/);
to_json(json &,pod)32 void to_json(json& /*unused*/, pod /*unused*/) noexcept {}
to_json(json &,pod_bis)33 void to_json(json& /*unused*/, pod_bis /*unused*/) {}
from_json(const json &,pod)34 void from_json(const json& /*unused*/, pod /*unused*/) noexcept {}
from_json(const json &,pod_bis)35 void from_json(const json& /*unused*/, pod_bis /*unused*/) {}
36
37 json* j = nullptr;
38
39 static_assert(noexcept(json{}), "");
40 static_assert(noexcept(nlohmann::to_json(*j, 2)), "");
41 static_assert(noexcept(nlohmann::to_json(*j, 2.5)), "");
42 static_assert(noexcept(nlohmann::to_json(*j, true)), "");
43 static_assert(noexcept(nlohmann::to_json(*j, test{})), "");
44 static_assert(noexcept(nlohmann::to_json(*j, pod{})), "");
45 static_assert(!noexcept(nlohmann::to_json(*j, pod_bis{})), "");
46 static_assert(noexcept(json(2)), "");
47 static_assert(noexcept(json(test{})), "");
48 static_assert(noexcept(json(pod{})), "");
49 static_assert(noexcept(j->get<pod>()), "");
50 static_assert(!noexcept(j->get<pod_bis>()), "");
51 static_assert(noexcept(json(pod{})), "");
52 } // namespace
53
54 TEST_CASE("runtime checks")
55 {
56 SECTION("nothrow-copy-constructible exceptions")
57 {
58 // for ERR60-CPP (https://github.com/nlohmann/json/issues/531):
59 // Exceptions should be nothrow-copy-constructible. However, compilers
60 // treat std::runtime_exception differently in this regard. Therefore,
61 // we can only demand nothrow-copy-constructibility for our exceptions
62 // if std::runtime_exception is.
63 CHECK(std::is_nothrow_copy_constructible<json::exception>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
64 CHECK(std::is_nothrow_copy_constructible<json::parse_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
65 CHECK(std::is_nothrow_copy_constructible<json::invalid_iterator>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
66 CHECK(std::is_nothrow_copy_constructible<json::type_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
67 CHECK(std::is_nothrow_copy_constructible<json::out_of_range>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
68 CHECK(std::is_nothrow_copy_constructible<json::other_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
69 }
70
71 SECTION("silence -Wunneeded-internal-declaration errors")
72 {
73 j = nullptr;
74 json j2;
75 to_json(j2, pod());
76 to_json(j2, pod_bis());
77 from_json(j2, pod());
78 from_json(j2, pod_bis());
79 }
80 }
81
82 DOCTEST_GCC_SUPPRESS_WARNING_POP
83