From 6efd83fcd00d826a2ee0de271ad512f59eeaebbd Mon Sep 17 00:00:00 2001 From: Erwin Nindl Date: Fri, 2 Sep 2022 11:43:49 +0200 Subject: [PATCH] Adds test for issue #218 --- test/CMakeLists.txt | 5 ++ test/issue-218.cpp | 147 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 test/issue-218.cpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fd1c309..7a8d52c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -73,3 +73,8 @@ add_test(NAME issue-149-entry-selection COMMAND issue-149-entry-selection) add_executable(issue-189-default-values issue-189-default-values.cpp) target_link_libraries(issue-189-default-values nlohmann_json_schema_validator) add_test(NAME issue-189-default-values COMMAND issue-189-default-values) + +add_executable(issue-218 issue-218.cpp) +target_link_libraries(issue-218 nlohmann_json_schema_validator) +add_test(NAME issue-218 COMMAND issue-218) + diff --git a/test/issue-218.cpp b/test/issue-218.cpp new file mode 100644 index 0000000..f227df4 --- /dev/null +++ b/test/issue-218.cpp @@ -0,0 +1,147 @@ +#include + +#include +#include +#include + +using nlohmann::json; +using nlohmann::json_uri; +using nlohmann::json_schema::json_validator; + +namespace { + +static int error_count; + +template +void EXPECT_EQ(T a, R b, std::string name="") { + if (a != b) { + if (name != "") { + std::cerr << name << " - "; + } + std::cerr << "Failed: '" << a << "' != '" << b << "'\n"; + error_count++; + } +} + + +static json schema = R"( +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "Schedule", + "type": "object", + "properties": { + "reply": { + "description": "Send reply", + "type": "boolean" + }, + "schedule": { + "description": "Array with schedule entries", + "type": "array", + "items": { "$ref": "#/definitions/schedule_entry" } + } + }, + "definitions": { + "schedule_entry": { + "type": "object", + "required": [ + "id", + "startdate", + "cmd", + "settings" + ], + "properties": { + "id": { + "type": "string" + }, + "startdate": { + "type": "number", + "minimum": 0 + }, + "cmd": { + "type": "string" + }, + "settings": { + "type": "object" + } + } + } + } +} +)"_json; + +json valid_schedule = R"( +{ + "reply": false, + "schedule": [ + { + "id": "abc", + "startdate": 1, + "cmd": "foo", + "settings": {} + } + ] +} +)"_json; + +json valid_schedule_entry = R"( +{ + "id": "abc", + "startdate": 1, + "cmd": "foo", + "settings": {} +} +)"_json; + +json invalid_schedule_entry = R"( +{ + "id": 23, + "startdate": 1, + "cmd": "foo", + "settings": {} +} +)"_json; + +class store_err_handler : public nlohmann::json_schema::basic_error_handler { +public: + void reset() override { + nlohmann::json_schema::basic_error_handler::reset(); + failed_.clear(); + } + std::size_t failed() const { + return failed_.size(); + } +private: + void error(const nlohmann::json::json_pointer &ptr, const json &instance, const std::string &message) override + { + nlohmann::json_schema::basic_error_handler::error(ptr, instance, message); + std::cerr << "ERROR: '" << ptr << "' - '" << instance << "': " << message << "\n"; + failed_.push_back(ptr); + } + std::vector failed_; +}; + +} // namespace + + +int main() { + json_validator validator(schema); + store_err_handler err; + + validator.validate(valid_schedule, err); // OK + EXPECT_EQ(err.failed(), 0, "valid object"); + err.reset(); + + validator.validate(R"({ "foo": "bar" })"_json, err); + EXPECT_EQ(err.failed(), 1, "invalid object"); + err.reset(); + + validator.validate(valid_schedule_entry, err, json_uri("#/definitions/schedule_entry")); + EXPECT_EQ(err.failed(), 0, "valid sub-object"); + err.reset(); + + validator.validate(invalid_schedule_entry, err, json_uri("#/definitions/schedule_entry")); + EXPECT_EQ(err.failed(), 1, "invalid sub-object"); + err.reset(); + + return error_count; +}