aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/json.cc
diff options
context:
space:
mode:
authorJay Berkenbilt <ejb@ql.org>2022-01-22 20:33:26 +0100
committerJay Berkenbilt <ejb@ql.org>2022-01-30 19:11:03 +0100
commit8dea480c9f065fdac76f848ed9ec7a07fd1e9870 (patch)
tree8334231ae683dcf239f439f1602268fa98f39f10 /libtests/json.cc
parent558ba2823e40867eac47686edea3fe318ce3c446 (diff)
downloadqpdf-8dea480c9f065fdac76f848ed9ec7a07fd1e9870.tar.zst
Allow optional fields in json "schema" checks
Diffstat (limited to 'libtests/json.cc')
-rw-r--r--libtests/json.cc53
1 files changed, 42 insertions, 11 deletions
diff --git a/libtests/json.cc b/libtests/json.cc
index 006f00cf..754c2b3e 100644
--- a/libtests/json.cc
+++ b/libtests/json.cc
@@ -112,12 +112,12 @@ static void test_main()
assert(dvalue == xdvalue);
}
-static void check_schema(JSON& obj, JSON& schema, bool exp,
- std::string const& description)
+static void check_schema(JSON& obj, JSON& schema, unsigned long flags,
+ bool exp, std::string const& description)
{
std::list<std::string> errors;
std::cout << "--- " << description << std::endl;
- assert(exp == obj.checkSchema(schema, errors));
+ assert(exp == obj.checkSchema(schema, flags, errors));
for (std::list<std::string>::iterator iter = errors.begin();
iter != errors.end(); ++iter)
{
@@ -134,8 +134,7 @@ static void test_schema()
"a": {
"q": "queue",
"r": {
- "x": "ecks",
- "y": "(bool) why"
+ "x": "ecks"
},
"s": [
"esses"
@@ -151,14 +150,14 @@ static void test_schema()
"three": {
"<objid>": {
"z": "ebra",
- "o": "(optional, string) optional"
+ "o": "ptional"
}
}
}
)");
JSON a = JSON::parse(R"(["not a", "dictionary"])");
- check_schema(a, schema, false, "top-level type mismatch");
+ check_schema(a, schema, 0, false, "top-level type mismatch");
JSON b = JSON::parse(R"(
{
"one": {
@@ -205,10 +204,42 @@ static void test_schema()
}
)");
- check_schema(b, schema, false, "missing items");
- check_schema(a, a, false, "top-level schema array error");
- check_schema(b, b, false, "lower-level schema array error");
- check_schema(schema, schema, true, "pass");
+ check_schema(b, schema, 0, false, "missing items");
+ check_schema(a, a, 0, false, "top-level schema array error");
+ check_schema(b, b, 0, false, "lower-level schema array error");
+
+ JSON bad_schema = JSON::parse(R"({"a": true, "b": "potato?"})");
+ check_schema(bad_schema, bad_schema, 0, false, "bad schema field type");
+
+ JSON good = JSON::parse(R"(
+{
+ "one": {
+ "a": {
+ "q": "potato",
+ "r": {
+ "x": [1, null]
+ },
+ "s": [
+ null,
+ "anything"
+ ]
+ }
+ },
+ "two": [
+ {
+ "glarp": "enspliel",
+ "goose": 3.14
+ }
+ ],
+ "three": {
+ "<objid>": {
+ "z": "ebra"
+ }
+ }
+}
+)");
+ check_schema(good, schema, 0, false, "not optional");
+ check_schema(good, schema, JSON::f_optional, true, "pass");
}
int main()