MQTTSuite
Loading...
Searching...
No Matches
anonymous_namespace{issue-105-verbose-combination-errors.cpp} Namespace Reference

Classes

class  MyErrorHandler

Functions

auto generateSchema (const std::string &first_combination, const std::string &second_combination) -> nlohmann::json
auto operator<< (std::string first, const std::string &second) -> std::string
auto rootError (const std::string &combination_type, std::size_t number_of_subschemas) -> std::string
auto combinationError (const std::string &combination_type, std::size_t test_case_number) -> std::string
auto validate (const nlohmann::json &schema, const nlohmann::json &instance, nlohmann::json_schema::error_handler *error_handler=nullptr) -> void
auto simpleTest (const std::string &first_combination, const std::string &second_combination) -> void
auto verboseTest (const std::string &first_combination, const std::string &second_combination) -> void

Variables

int g_error_count = 0
const std::string g_schema_template

Function Documentation

◆ combinationError()

auto anonymous_namespace{issue-105-verbose-combination-errors.cpp}::combinationError ( const std::string & combination_type,
std::size_t test_case_number )->std::string

Definition at line 152 of file issue-105-verbose-combination-errors.cpp.

153{
154 return "[combination: " + combination_type + " / case#" + std::to_string(test_case_number) + "]";
155}

Referenced by verboseTest().

Here is the caller graph for this function:

◆ generateSchema()

auto anonymous_namespace{issue-105-verbose-combination-errors.cpp}::generateSchema ( const std::string & first_combination,
const std::string & second_combination )->nlohmann::json

Definition at line 99 of file issue-105-verbose-combination-errors.cpp.

100{
101 static const std::regex first_replace_re{"%COMBINATION_FIRST_LEVEL%"};
102 static const std::regex second_replace_re{"%COMBINATION_SECOND_LEVEL%"};
103
104 std::string intermediate = std::regex_replace(g_schema_template, first_replace_re, first_combination);
105
106 return nlohmann::json::parse(std::regex_replace(intermediate, second_replace_re, second_combination));
107}

References g_schema_template.

Referenced by simpleTest(), and verboseTest().

Here is the caller graph for this function:

◆ operator<<()

auto anonymous_namespace{issue-105-verbose-combination-errors.cpp}::operator<< ( std::string first,
const std::string & second )->std::string

Definition at line 140 of file issue-105-verbose-combination-errors.cpp.

141{
142 first += ".*";
143 first += second;
144 return first;
145}

Referenced by verboseTest().

Here is the caller graph for this function:

◆ rootError()

auto anonymous_namespace{issue-105-verbose-combination-errors.cpp}::rootError ( const std::string & combination_type,
std::size_t number_of_subschemas )->std::string

Definition at line 147 of file issue-105-verbose-combination-errors.cpp.

148{
149 return "no subschema has succeeded, but one of them is required to validate. Type: " + combination_type + ", number of failed subschemas: " + std::to_string(number_of_subschemas);
150}

Referenced by simpleTest(), and verboseTest().

Here is the caller graph for this function:

◆ simpleTest()

auto anonymous_namespace{issue-105-verbose-combination-errors.cpp}::simpleTest ( const std::string & first_combination,
const std::string & second_combination )->void

Definition at line 175 of file issue-105-verbose-combination-errors.cpp.

176{
177 const nlohmann::json schema = generateSchema(first_combination, second_combination);
178 EXPECT_THROW_WITH_MESSAGE(validate(schema, nlohmann::json{{"first", {{"second", 1}}}}), rootError(first_combination, 3));
179 if (second_combination == "oneOf") {
180 EXPECT_THROW_WITH_MESSAGE(validate(schema, nlohmann::json{{"first", {{"second", 8}}}}), rootError(first_combination, 3));
181 }
182 EXPECT_THROW_WITH_MESSAGE(validate(schema, nlohmann::json{{"first", 10}}), rootError(first_combination, 3));
183 EXPECT_THROW_WITH_MESSAGE(validate(schema, nlohmann::json{{"first", "short"}}), rootError(first_combination, 3));
184}
auto schema
Definition id-ref.cpp:69
#define EXPECT_THROW_WITH_MESSAGE(EXPRESSION, MESSAGE)
auto validate(const nlohmann::json &schema, const nlohmann::json &instance, nlohmann::json_schema::error_handler *error_handler=nullptr) -> void
auto rootError(const std::string &combination_type, std::size_t number_of_subschemas) -> std::string
auto generateSchema(const std::string &first_combination, const std::string &second_combination) -> nlohmann::json

References generateSchema(), rootError(), and validate().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ validate()

auto anonymous_namespace{issue-105-verbose-combination-errors.cpp}::validate ( const nlohmann::json & schema,
const nlohmann::json & instance,
nlohmann::json_schema::error_handler * error_handler = nullptr )->void

Definition at line 160 of file issue-105-verbose-combination-errors.cpp.

161{
163 validator.set_root_schema(schema);
164
165 if (error_handler) {
166 validator.validate(instance, *error_handler);
167 } else {
168 validator.validate(instance);
169 }
170}
nlohmann::json_schema::json_validator validator
static const auto instance
Definition issue-93.cpp:14

References nlohmann::json_schema::json_validator::set_root_schema(), nlohmann::json_schema::json_validator::validate(), and nlohmann::json_schema::json_validator::validate().

Referenced by simpleTest(), and verboseTest().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ verboseTest()

auto anonymous_namespace{issue-105-verbose-combination-errors.cpp}::verboseTest ( const std::string & first_combination,
const std::string & second_combination )->void

Definition at line 186 of file issue-105-verbose-combination-errors.cpp.

187{
188 const nlohmann::json schema = generateSchema(first_combination, second_combination);
189
190 {
191 MyErrorHandler error_handler;
192 validate(schema, nlohmann::json{{"first", {{"second", 1}}}}, &error_handler);
193
194 const MyErrorHandler::ErrorEntryList &error_list = error_handler.getErrors();
195 EXPECT_EQ(error_list.size(), 6);
196
197 EXPECT_EQ(error_list[0].ptr, nlohmann::json::json_pointer{"/first"});
198 EXPECT_MATCH(error_list[0].message, rootError(first_combination, 3));
199
200 EXPECT_EQ(error_list[1].ptr, nlohmann::json::json_pointer{"/first/second"});
201 EXPECT_MATCH(error_list[1].message, combinationError(first_combination, 0) << rootError(second_combination, 2));
202
203 EXPECT_EQ(error_list[2].ptr, nlohmann::json::json_pointer{"/first/second"});
204 EXPECT_MATCH(error_list[2].message, combinationError(first_combination, 0) << combinationError(second_combination, 0) << "instance is below minimum of 5");
205
206 EXPECT_EQ(error_list[3].ptr, nlohmann::json::json_pointer{"/first/second"});
207 EXPECT_MATCH(error_list[3].message, combinationError(first_combination, 0) << combinationError(second_combination, 1) << "instance is not a multiple of 2.0");
208
209 EXPECT_EQ(error_list[4].ptr, nlohmann::json::json_pointer{"/first"});
210 EXPECT_MATCH(error_list[4].message, combinationError(first_combination, 1) << "unexpected instance type");
211
212 EXPECT_EQ(error_list[5].ptr, nlohmann::json::json_pointer{"/first"});
213 EXPECT_MATCH(error_list[5].message, combinationError(first_combination, 2) << "unexpected instance type");
214 }
215
216 {
217 MyErrorHandler error_handler;
218 validate(schema, nlohmann::json{{"first", {{"second", "not-an-integer"}}}}, &error_handler);
219
220 const MyErrorHandler::ErrorEntryList &error_list = error_handler.getErrors();
221 EXPECT_EQ(error_list.size(), 6);
222
223 EXPECT_EQ(error_list[0].ptr, nlohmann::json::json_pointer{"/first"});
224 EXPECT_MATCH(error_list[0].message, rootError(first_combination, 3));
225
226 EXPECT_EQ(error_list[1].ptr, nlohmann::json::json_pointer{"/first/second"});
227 EXPECT_MATCH(error_list[1].message, combinationError(first_combination, 0) << rootError(second_combination, 2));
228
229 EXPECT_EQ(error_list[2].ptr, nlohmann::json::json_pointer{"/first/second"});
230 EXPECT_MATCH(error_list[2].message, combinationError(first_combination, 0) << combinationError(second_combination, 0) << "unexpected instance type");
231
232 EXPECT_EQ(error_list[3].ptr, nlohmann::json::json_pointer{"/first/second"});
233 EXPECT_MATCH(error_list[3].message, combinationError(first_combination, 0) << combinationError(second_combination, 1) << "unexpected instance type");
234
235 EXPECT_EQ(error_list[4].ptr, nlohmann::json::json_pointer{"/first"});
236 EXPECT_MATCH(error_list[4].message, combinationError(first_combination, 1) << "unexpected instance type");
237
238 EXPECT_EQ(error_list[5].ptr, nlohmann::json::json_pointer{"/first"});
239 EXPECT_MATCH(error_list[5].message, combinationError(first_combination, 2) << "unexpected instance type");
240 }
241
242 if (second_combination == "oneOf") {
243 MyErrorHandler error_handler;
244 validate(schema, nlohmann::json{{"first", {{"second", 8}}}}, &error_handler);
245
246 const MyErrorHandler::ErrorEntryList &error_list = error_handler.getErrors();
247 EXPECT_EQ(error_list.size(), 4);
248
249 EXPECT_EQ(error_list[0].ptr, nlohmann::json::json_pointer{"/first"});
250 EXPECT_MATCH(error_list[0].message, rootError(first_combination, 3));
251
252 EXPECT_EQ(error_list[1].ptr, nlohmann::json::json_pointer{"/first/second"});
253 EXPECT_MATCH(error_list[1].message, combinationError(first_combination, 0) << "more than one subschema has succeeded, but exactly one of them is required to validate");
254
255 EXPECT_EQ(error_list[2].ptr, nlohmann::json::json_pointer{"/first"});
256 EXPECT_MATCH(error_list[2].message, combinationError(first_combination, 1) << "unexpected instance type");
257
258 EXPECT_EQ(error_list[3].ptr, nlohmann::json::json_pointer{"/first"});
259 EXPECT_MATCH(error_list[3].message, combinationError(first_combination, 2) << "unexpected instance type");
260 }
261
262 {
263 MyErrorHandler error_handler;
264 validate(schema, nlohmann::json{{"first", 10}}, &error_handler);
265
266 const MyErrorHandler::ErrorEntryList &error_list = error_handler.getErrors();
267 EXPECT_EQ(error_list.size(), 4);
268
269 EXPECT_EQ(error_list[0].ptr, nlohmann::json::json_pointer{"/first"});
270 EXPECT_MATCH(error_list[0].message, rootError(first_combination, 3));
271
272 EXPECT_EQ(error_list[1].ptr, nlohmann::json::json_pointer{"/first"});
273 EXPECT_MATCH(error_list[1].message, combinationError(first_combination, 0) << "unexpected instance type");
274
275 EXPECT_EQ(error_list[2].ptr, nlohmann::json::json_pointer{"/first"});
276 EXPECT_MATCH(error_list[2].message, combinationError(first_combination, 1) << "instance is below minimum of 20");
277
278 EXPECT_EQ(error_list[3].ptr, nlohmann::json::json_pointer{"/first"});
279 EXPECT_MATCH(error_list[3].message, combinationError(first_combination, 2) << "unexpected instance type");
280 }
281
282 {
283 MyErrorHandler error_handler;
284 validate(schema, nlohmann::json{{"first", "short"}}, &error_handler);
285
286 const MyErrorHandler::ErrorEntryList &error_list = error_handler.getErrors();
287 EXPECT_EQ(error_list.size(), 4);
288
289 EXPECT_EQ(error_list[0].ptr, nlohmann::json::json_pointer{"/first"});
290 EXPECT_MATCH(error_list[0].message, rootError(first_combination, 3));
291
292 EXPECT_EQ(error_list[1].ptr, nlohmann::json::json_pointer{"/first"});
293 EXPECT_MATCH(error_list[1].message, combinationError(first_combination, 0) << "unexpected instance type");
294
295 EXPECT_EQ(error_list[2].ptr, nlohmann::json::json_pointer{"/first"});
296 EXPECT_MATCH(error_list[2].message, combinationError(first_combination, 1) << "unexpected instance type");
297
298 EXPECT_EQ(error_list[3].ptr, nlohmann::json::json_pointer{"/first"});
299 EXPECT_MATCH(error_list[3].message, combinationError(first_combination, 2) << "instance is too short as per minLength:10");
300 }
301}
#define EXPECT_EQ(a, b)
#define EXPECT_MATCH(STRING, REGEX)
auto combinationError(const std::string &combination_type, std::size_t test_case_number) -> std::string

References combinationError(), generateSchema(), anonymous_namespace{issue-105-verbose-combination-errors.cpp}::MyErrorHandler::getErrors(), anonymous_namespace{issue-105-verbose-combination-errors.cpp}::MyErrorHandler::ErrorEntry::message, operator<<(), anonymous_namespace{issue-105-verbose-combination-errors.cpp}::MyErrorHandler::ErrorEntry::ptr, rootError(), and validate().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_error_count

int anonymous_namespace{issue-105-verbose-combination-errors.cpp}::g_error_count = 0

Definition at line 57 of file issue-105-verbose-combination-errors.cpp.

Referenced by main().

◆ g_schema_template

const std::string anonymous_namespace{issue-105-verbose-combination-errors.cpp}::g_schema_template

Definition at line 62 of file issue-105-verbose-combination-errors.cpp.

Referenced by generateSchema().