• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     __ _____ _____ _____
3  __|  |   __|     |   | |  JSON for Modern C++ (test suite)
4 |  |  |__   |  |  | | | |  version 3.9.1
5 |_____|_____|_____|_|___|  https://github.com/nlohmann/json
6 
7 Licensed under the MIT License <http://opensource.org/licenses/MIT>.
8 SPDX-License-Identifier: MIT
9 Copyright (c) 2013-2019 Niels Lohmann <http://nlohmann.me>.
10 
11 Permission is hereby  granted, free of charge, to any  person obtaining a copy
12 of this software and associated  documentation files (the "Software"), to deal
13 in the Software  without restriction, including without  limitation the rights
14 to  use, copy,  modify, merge,  publish, distribute,  sublicense, and/or  sell
15 copies  of  the Software,  and  to  permit persons  to  whom  the Software  is
16 furnished to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall be included in all
19 copies or substantial portions of the Software.
20 
21 THE SOFTWARE  IS PROVIDED "AS  IS", WITHOUT WARRANTY  OF ANY KIND,  EXPRESS OR
22 IMPLIED,  INCLUDING BUT  NOT  LIMITED TO  THE  WARRANTIES OF  MERCHANTABILITY,
23 FITNESS FOR  A PARTICULAR PURPOSE AND  NONINFRINGEMENT. IN NO EVENT  SHALL THE
24 AUTHORS  OR COPYRIGHT  HOLDERS  BE  LIABLE FOR  ANY  CLAIM,  DAMAGES OR  OTHER
25 LIABILITY, WHETHER IN AN ACTION OF  CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE  OR THE USE OR OTHER DEALINGS IN THE
27 SOFTWARE.
28 */
29 
30 #include "doctest_compatibility.h"
31 
32 #include <nlohmann/json.hpp>
33 
34 using nlohmann::json;
35 
36 namespace
37 {
38 enum test
39 {
40 };
41 
42 struct pod {};
43 struct pod_bis {};
44 
45 void to_json(json&, pod) noexcept;
46 void to_json(json&, pod_bis);
47 void from_json(const json&, pod) noexcept;
48 void from_json(const json&, pod_bis);
to_json(json &,pod)49 void to_json(json&, pod) noexcept {}
to_json(json &,pod_bis)50 void to_json(json&, pod_bis) {}
from_json(const json &,pod)51 void from_json(const json&, pod) noexcept {}
from_json(const json &,pod_bis)52 void from_json(const json&, pod_bis) {}
53 
54 static json* j = nullptr;
55 
56 static_assert(noexcept(json{}), "");
57 static_assert(noexcept(nlohmann::to_json(*j, 2)), "");
58 static_assert(noexcept(nlohmann::to_json(*j, 2.5)), "");
59 static_assert(noexcept(nlohmann::to_json(*j, true)), "");
60 static_assert(noexcept(nlohmann::to_json(*j, test{})), "");
61 static_assert(noexcept(nlohmann::to_json(*j, pod{})), "");
62 static_assert(!noexcept(nlohmann::to_json(*j, pod_bis{})), "");
63 static_assert(noexcept(json(2)), "");
64 static_assert(noexcept(json(test{})), "");
65 static_assert(noexcept(json(pod{})), "");
66 static_assert(noexcept(j->get<pod>()), "");
67 static_assert(!noexcept(j->get<pod_bis>()), "");
68 static_assert(noexcept(json(pod{})), "");
69 }
70 
71 TEST_CASE("runtime checks")
72 {
73     SECTION("nothrow-copy-constructible exceptions")
74     {
75         // for ERR60-CPP (https://github.com/nlohmann/json/issues/531):
76         // Exceptions should be nothrow-copy-constructible. However, compilers
77         // treat std::runtime_exception differently in this regard. Therefore,
78         // we can only demand nothrow-copy-constructibility for our exceptions
79         // if std::runtime_exception is.
80         CHECK(std::is_nothrow_copy_constructible<json::exception>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
81         CHECK(std::is_nothrow_copy_constructible<json::parse_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
82         CHECK(std::is_nothrow_copy_constructible<json::invalid_iterator>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
83         CHECK(std::is_nothrow_copy_constructible<json::type_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
84         CHECK(std::is_nothrow_copy_constructible<json::out_of_range>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
85         CHECK(std::is_nothrow_copy_constructible<json::other_error>::value == std::is_nothrow_copy_constructible<std::runtime_error>::value);
86     }
87 
88     SECTION("silence -Wunneeded-internal-declaration errors")
89     {
90         j = nullptr;
91         json j2;
92         to_json(j2, pod());
93         to_json(j2, pod_bis());
94         from_json(j2, pod());
95         from_json(j2, pod_bis());
96     }
97 }
98