• 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 #define private public
33 #include <nlohmann/json.hpp>
34 using nlohmann::json;
35 #undef private
36 
37 #include <sstream>
38 
39 namespace
40 {
41 void check_escaped(const char* original, const char* escaped = "", const bool ensure_ascii = false);
check_escaped(const char * original,const char * escaped,const bool ensure_ascii)42 void check_escaped(const char* original, const char* escaped, const bool ensure_ascii)
43 {
44     std::stringstream ss;
45     json::serializer s(nlohmann::detail::output_adapter<char>(ss), ' ');
46     s.dump_escaped(original, ensure_ascii);
47     CHECK(ss.str() == escaped);
48 }
49 }
50 
51 TEST_CASE("convenience functions")
52 {
53     SECTION("type name as string")
54     {
55         CHECK(std::string(json(json::value_t::null).type_name()) == "null");
56         CHECK(std::string(json(json::value_t::object).type_name()) == "object");
57         CHECK(std::string(json(json::value_t::array).type_name()) == "array");
58         CHECK(std::string(json(json::value_t::number_integer).type_name()) == "number");
59         CHECK(std::string(json(json::value_t::number_unsigned).type_name()) == "number");
60         CHECK(std::string(json(json::value_t::number_float).type_name()) == "number");
61         CHECK(std::string(json(json::value_t::binary).type_name()) == "binary");
62         CHECK(std::string(json(json::value_t::boolean).type_name()) == "boolean");
63         CHECK(std::string(json(json::value_t::string).type_name()) == "string");
64         CHECK(std::string(json(json::value_t::discarded).type_name()) == "discarded");
65     }
66 
67     SECTION("string escape")
68     {
69         check_escaped("\"", "\\\"");
70         check_escaped("\\", "\\\\");
71         check_escaped("\b", "\\b");
72         check_escaped("\f", "\\f");
73         check_escaped("\n", "\\n");
74         check_escaped("\r", "\\r");
75         check_escaped("\t", "\\t");
76 
77         check_escaped("\x01", "\\u0001");
78         check_escaped("\x02", "\\u0002");
79         check_escaped("\x03", "\\u0003");
80         check_escaped("\x04", "\\u0004");
81         check_escaped("\x05", "\\u0005");
82         check_escaped("\x06", "\\u0006");
83         check_escaped("\x07", "\\u0007");
84         check_escaped("\x08", "\\b");
85         check_escaped("\x09", "\\t");
86         check_escaped("\x0a", "\\n");
87         check_escaped("\x0b", "\\u000b");
88         check_escaped("\x0c", "\\f");
89         check_escaped("\x0d", "\\r");
90         check_escaped("\x0e", "\\u000e");
91         check_escaped("\x0f", "\\u000f");
92         check_escaped("\x10", "\\u0010");
93         check_escaped("\x11", "\\u0011");
94         check_escaped("\x12", "\\u0012");
95         check_escaped("\x13", "\\u0013");
96         check_escaped("\x14", "\\u0014");
97         check_escaped("\x15", "\\u0015");
98         check_escaped("\x16", "\\u0016");
99         check_escaped("\x17", "\\u0017");
100         check_escaped("\x18", "\\u0018");
101         check_escaped("\x19", "\\u0019");
102         check_escaped("\x1a", "\\u001a");
103         check_escaped("\x1b", "\\u001b");
104         check_escaped("\x1c", "\\u001c");
105         check_escaped("\x1d", "\\u001d");
106         check_escaped("\x1e", "\\u001e");
107         check_escaped("\x1f", "\\u001f");
108 
109         // invalid UTF-8 characters
110         CHECK_THROWS_AS(check_escaped("ä\xA9ü"), json::type_error&);
111         CHECK_THROWS_WITH(check_escaped("ä\xA9ü"),
112                           "[json.exception.type_error.316] invalid UTF-8 byte at index 2: 0xA9");
113 
114         CHECK_THROWS_AS(check_escaped("\xC2"), json::type_error&);
115         CHECK_THROWS_WITH(check_escaped("\xC2"),
116                           "[json.exception.type_error.316] incomplete UTF-8 string; last byte: 0xC2");
117     }
118 }
119