diff options
Diffstat (limited to 'libtests')
-rw-r--r-- | libtests/json.cc | 53 | ||||
-rw-r--r-- | libtests/libtests.testcov | 2 | ||||
-rw-r--r-- | libtests/qtest/json/json.out | 6 |
3 files changed, 50 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() diff --git a/libtests/libtests.testcov b/libtests/libtests.testcov index 6284c0e8..907e6579 100644 --- a/libtests/libtests.testcov +++ b/libtests/libtests.testcov @@ -89,3 +89,5 @@ JSON parse premature end of u 0 JSON parse bad hex after u 0 JSONHandler unhandled value 0 JSONHandler unexpected key 0 +JSON schema other type 0 +JSON optional key 0 diff --git a/libtests/qtest/json/json.out b/libtests/qtest/json/json.out index 1a320dcf..c7cb85a7 100644 --- a/libtests/qtest/json/json.out +++ b/libtests/qtest/json/json.out @@ -21,6 +21,12 @@ top-level object schema array contains other than one item json key ".one.a.r" schema array contains other than one item json key ".two" schema array contains other than one item --- +--- bad schema field type +json key ".a" schema value is not dictionary, array, or string +--- +--- not optional +json key ".three.<objid>": key "o" is present in schema but missing in object +--- --- pass --- end of json tests |