MQTTSuite
Loading...
Searching...
No Matches
errors.cpp File Reference
#include <nlohmann/json-schema.hpp>
#include <iostream>
Include dependency graph for errors.cpp:

Go to the source code of this file.

Classes

class  anonymous_namespace{errors.cpp}::store_ptr_err_handler

Namespaces

namespace  anonymous_namespace{errors.cpp}

Macros

#define EXPECT_EQ(a, b)

Functions

int main (void)

Variables

static int error_count
static json anonymous_namespace{errors.cpp}::person_schema

Macro Definition Documentation

◆ EXPECT_EQ

#define EXPECT_EQ ( a,
b )
Value:
do { \
if (a != b) { \
std::cerr << "Failed: '" << a << "' != '" << b << "'\n"; \
} \
} while (0)
static int error_count

Definition at line 7 of file errors.cpp.

7#define EXPECT_EQ(a, b) \
8 do { \
9 if (a != b) { \
10 std::cerr << "Failed: '" << a << "' != '" << b << "'\n"; \
11 error_count++; \
12 } \
13 } while (0)

Function Documentation

◆ main()

int main ( void )

Definition at line 73 of file errors.cpp.

74{
76
77 try {
78 validator.set_root_schema(person_schema); // insert root-schema
79 } catch (const std::exception &e) {
80 std::cerr << "Validation of schema failed, here is why: " << e.what() << "\n";
81 return EXIT_FAILURE;
82 }
83
85
86 validator.validate({{"age", 42}, {"name", "John"}}, err); // OK
87 EXPECT_EQ(err.failed_pointers.size(), 0);
88 err.reset();
89
90 validator.validate({{"age", 42}}, err); // no name
91
92 EXPECT_EQ(err.failed_pointers.size(), 1);
93 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
94 err.reset();
95
96 validator.validate({{"street", "Boulevard"}}, err); // no name and no age
97 EXPECT_EQ(err.failed_pointers.size(), 3);
98 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
99 EXPECT_EQ(err.failed_pointers[1].to_string(), "");
100 EXPECT_EQ(err.failed_pointers[2].to_string(), "");
101 err.reset();
102
103 validator.validate({{"age", 42}, {"name", 12}}, err); // name must be a string
104 EXPECT_EQ(err.failed_pointers.size(), 1);
105 EXPECT_EQ(err.failed_pointers[0].to_string(), "/name");
106 err.reset();
107
108 validator.validate({
109 {"age", 42},
110 {"name", "John"},
111 {"phones", {1234, "223"}},
112 },
113 err); // name must be a string
114 EXPECT_EQ(err.failed_pointers.size(), 1);
115 EXPECT_EQ(err.failed_pointers[0].to_string(), "/phones/1");
116 err.reset();
117
118 validator.validate({
119 {"age", 42},
120 {"name", "John"},
121 {"phones", {0}},
122 {"post-code", 12345},
123 },
124 err); // name must be a string
125 EXPECT_EQ(err.failed_pointers.size(), 1);
126 EXPECT_EQ(err.failed_pointers[0].to_string(), "");
127 err.reset();
128
129 return error_count;
130}
nlohmann::json_schema::json_validator validator
std::vector< nlohmann::json::json_pointer > failed_pointers
#define EXPECT_EQ(a, b)
Definition errors.cpp:7
static json person_schema
Definition readme.cpp:10

References error_count, anonymous_namespace{errors.cpp}::store_ptr_err_handler::failed_pointers, anonymous_namespace{errors.cpp}::person_schema, anonymous_namespace{errors.cpp}::store_ptr_err_handler::reset(), nlohmann::json_schema::json_validator::set_root_schema(), and nlohmann::json_schema::json_validator::validate().

Here is the call graph for this function:

Variable Documentation

◆ error_count

int error_count
static

Definition at line 5 of file errors.cpp.

Referenced by main().