1 // __ _____ _____ _____
2 // __| | __| | | | JSON for Modern C++ (supporting code)
3 // | | |__ | | | | | | version 3.11.3
4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json
5 //
6 // SPDX-FileCopyrightText: 2013-2023 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 struct pod {};
24 struct pod_bis {};
25
26 void to_json(json& /*unused*/, pod /*unused*/) noexcept;
27 void to_json(json& /*unused*/, pod_bis /*unused*/);
28 void from_json(const json& /*unused*/, pod /*unused*/) noexcept;
29 void from_json(const json& /*unused*/, pod_bis /*unused*/);
to_json(json &,pod)30 void to_json(json& /*unused*/, pod /*unused*/) noexcept {}
to_json(json &,pod_bis)31 void to_json(json& /*unused*/, pod_bis /*unused*/) {}
from_json(const json &,pod)32 void from_json(const json& /*unused*/, pod /*unused*/) noexcept {}
from_json(const json &,pod_bis)33 void from_json(const json& /*unused*/, pod_bis /*unused*/) {}
34
35 static_assert(noexcept(json{}), "");
36 static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), 2)), "");
37 static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), 2.5)), "");
38 static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), true)), "");
39 static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), test{})), "");
40 static_assert(noexcept(nlohmann::to_json(std::declval<json&>(), pod{})), "");
41 static_assert(!noexcept(nlohmann::to_json(std::declval<json&>(), pod_bis{})), "");
42 static_assert(noexcept(json(2)), "");
43 static_assert(noexcept(json(test{})), "");
44 static_assert(noexcept(json(pod{})), "");
45 static_assert(noexcept(std::declval<json>().get<pod>()), "");
46 static_assert(!noexcept(std::declval<json>().get<pod_bis>()), "");
47 static_assert(noexcept(json(pod{})), "");
48 } // namespace
49
50 TEST_CASE("noexcept")
51 {
52 // silence -Wunneeded-internal-declaration errors
53 static_cast<void>(static_cast<void(*)(json&, pod)>(&to_json));
54 static_cast<void>(static_cast<void(*)(json&, pod_bis)>(&to_json));
55 static_cast<void>(static_cast<void(*)(const json&, pod)>(&from_json));
56 static_cast<void>(static_cast<void(*)(const json&, pod_bis)>(&from_json));
57
58 SECTION("nothrow-copy-constructible exceptions")
59 {
60 // for ERR60-CPP (https://github.com/nlohmann/json/issues/531):
61 // Exceptions should be nothrow-copy-constructible. However, compilers
62 // treat std::runtime_exception differently in this regard. Therefore,
63 // we can only demand nothrow-copy-constructibility for our exceptions
64 // if std::runtime_exception is.
65 CHECK(std::is_nothrow_copy_constructible<json::exception>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
66 CHECK(std::is_nothrow_copy_constructible<json::parse_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
67 CHECK(std::is_nothrow_copy_constructible<json::invalid_iterator>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
68 CHECK(std::is_nothrow_copy_constructible<json::type_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
69 CHECK(std::is_nothrow_copy_constructible<json::out_of_range>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
70 CHECK(std::is_nothrow_copy_constructible<json::other_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
71 }
72 }
73
74 DOCTEST_GCC_SUPPRESS_WARNING_POP
75