more complex default-value-tests - fails

This commit is contained in:
Patrick Boettcher 2021-12-10 15:13:26 +01:00
parent 6c57980802
commit afe3cc1555

View File

@ -2,35 +2,52 @@
#include <nlohmann/json-schema.hpp> #include <nlohmann/json-schema.hpp>
using nlohmann::json; using nlohmann::json;
using nlohmann::json_uri;
using nlohmann::json_schema::json_validator; using nlohmann::json_schema::json_validator;
static const json rectangle_schema = R"( static const json quad_schema = R"(
{ {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"title": "A rectangle",
"properties": { "properties": {
"width": { "width": {
"$ref": "#/definitions/length", "$ref": "#/definitions/length",
"default": 20 "default": 20
}, },
"height": { "height": {
"$ref": "#/definitions/length" "$ref": "#/definitions/length"
},
"depths": {
"$ref": "default_schema#/definitions/defaultLength"
} }
}, },
"definitions": { "definitions": {
"length": { "length": {
"type": "integer", "$ref": "default_schema#/definitions/defaultLength",
"default": 10 "default": 10
} }
}, }
"additionalProperties": false,
"type": "object"
})"_json; })"_json;
static const json default_schema = R"(
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"defaultLength": {
"default": 5
}
}
})"_json;
static void loader(const json_uri &uri, json &schema)
{
schema = default_schema;
}
int main(void) int main(void)
{ {
json_validator validator{}; json_validator validator(loader);
validator.set_root_schema(rectangle_schema);
validator.set_root_schema(quad_schema);
{ {
json empty_rectangle = R"({})"_json; json empty_rectangle = R"({})"_json;
@ -39,7 +56,7 @@ int main(void)
const auto actual = empty_rectangle.patch(default_patch); const auto actual = empty_rectangle.patch(default_patch);
// height must be 10 according to the default specified in the length definition while width must be 10 overridden by the width element // height must be 10 according to the default specified in the length definition while width must be 10 overridden by the width element
const auto expected = R"({"height":10,"width":20})"_json; const auto expected = R"({"height":10,"width":20,"depth":5})"_json;
if (actual != expected) { if (actual != expected) {
std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl; std::cerr << "Patch with defaults contains wrong value: '" << actual << "' instead of expected '" << expected.dump() << "'" << std::endl;
return 1; return 1;