MQTTSuite
Loading...
Searching...
No Matches
uri.cpp
Go to the documentation of this file.
1/*
2 * JSON schema validator for JSON for modern C++
3 *
4 * Copyright (c) 2016-2019 Patrick Boettcher <p@yai.se>.
5 *
6 * SPDX-License-Identifier: MIT
7 *
8 */
9#include <cstdlib>
10#include <nlohmann/json-schema.hpp>
11
12#include <iostream>
13
14using nlohmann::json;
15using nlohmann::json_uri;
16
17static int errors;
18
19static void EXPECT_EQ(const std::string &a, const std::string &b)
20{
21 if (a != b) {
22 std::cerr << "Failed: '" << a << "' != '" << b << "'\n";
23 errors++;
24 }
25}
26
27static void EXPECT_EQ(const nlohmann::json_uri &a, const std::string &b)
28{
30}
31
32static void paths(json_uri start,
33 const std::string &full,
34 const std::string &full_path,
35 const std::string &no_path)
36{
37 EXPECT_EQ(start, full + " # ");
38
39 auto a = start.derive("other.json");
40 EXPECT_EQ(a, full_path + "/other.json # ");
41
42 auto b = a.derive("base.json");
43 EXPECT_EQ(b, full_path + "/base.json # ");
44
45 auto c = b.derive("subdir/base.json");
46 EXPECT_EQ(c, full_path + "/subdir/base.json # ");
47
48 auto d = c.derive("subdir2/base.json");
49 EXPECT_EQ(d, full_path + "/subdir/subdir2/base.json # ");
50
51 auto e = c.derive("/subdir2/base.json");
52 EXPECT_EQ(e, no_path + "/subdir2/base.json # ");
53
54 auto f = c.derive("new.json");
55 EXPECT_EQ(f, full_path + "/subdir/new.json # ");
56
57 auto g = c.derive("/new.json");
58 EXPECT_EQ(g, no_path + "/new.json # ");
59}
60
61static void pointer_plain_name(json_uri start,
62 const std::string &full,
63 const std::string &full_path,
64 const std::string &no_path)
65{
66 auto a = start.derive("#/json/path");
67 EXPECT_EQ(a, full + " # /json/path");
68
69 a = start.derive("#/json/special_%22");
70 EXPECT_EQ(a, full + " # /json/special_\"");
71
72 a = a.derive("#foo");
73 EXPECT_EQ(a, full + " # foo");
74
75 a = a.derive("#foo/looks_like_json/poiner/but/isnt");
76 EXPECT_EQ(a, full + " # foo/looks_like_json/poiner/but/isnt");
77 EXPECT_EQ(a.identifier(), "foo/looks_like_json/poiner/but/isnt");
78 EXPECT_EQ(a.pointer().to_string(), "");
79
80 a = a.derive("#/looks_like_json/poiner/and/it/is");
81 EXPECT_EQ(a, full + " # /looks_like_json/poiner/and/it/is");
82 EXPECT_EQ(a.pointer().to_string(), "/looks_like_json/poiner/and/it/is");
84}
85
86int main(void)
87{
88 json_uri empty("");
89 paths(empty,
90 "",
91 "",
92 "");
93
94 json_uri http("http://json-schema.org/draft-07/schema#");
95 paths(http,
96 "http://json-schema.org/draft-07/schema",
97 "http://json-schema.org/draft-07",
98 "http://json-schema.org");
99
101 "http://json-schema.org/draft-07/schema",
102 "http://json-schema.org/draft-07",
103 "http://json-schema.org");
104
105 return errors;
106}
const std::string & identifier() const
std::string to_string() const
Definition json-uri.cpp:118
json_uri(const std::string &uri)
const json::json_pointer & pointer() const
json_uri derive(const std::string &uri) const
int main()
Definition format.cpp:34
static void EXPECT_EQ(const nlohmann::json_uri &a, const std::string &b)
Definition uri.cpp:27
static void pointer_plain_name(json_uri start, const std::string &full, const std::string &full_path, const std::string &no_path)
Definition uri.cpp:61
static void paths(json_uri start, const std::string &full, const std::string &full_path, const std::string &no_path)
Definition uri.cpp:32
static void EXPECT_EQ(const std::string &a, const std::string &b)
Definition uri.cpp:19
static int errors
Definition uri.cpp:17