Merge pull request #163 from vincentbernat/fix/unpack-mix-optional-and-strict

Fix unpack when mixing strict mode and optional keys
This commit is contained in:
Petri Lehtinen
2014-02-16 22:09:41 +02:00
parent 946531bd7b
commit 78da1de021
2 changed files with 41 additions and 5 deletions

View File

@@ -370,4 +370,23 @@ static void run_tests()
if(i1 != 42)
fail("json_unpack failed to unpack");
json_decref(j);
/* Combine ? and ! */
j = json_pack("{si}", "foo", 42);
i1 = i2 = 0;
if(json_unpack(j, "{sis?i!}", "foo", &i1, "bar", &i2))
fail("json_unpack failed for optional values with strict mode");
if(i1 != 42)
fail("json_unpack failed to unpack");
if(i2 != 0)
fail("json_unpack failed to unpack");
json_decref(j);
/* But don't compensate a missing key with an optional one. */
j = json_pack("{sisi}", "foo", 42, "baz", 43);
i1 = i2 = i3 = 0;
if(!json_unpack_ex(j, &error, 0, "{sis?i!}", "foo", &i1, "bar", &i2))
fail("json_unpack failed for optional values with strict mode and compensation");
check_error("1 object item(s) left unpacked", "<validation>", 1, 8, 8);
json_decref(j);
}