X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=test%2Fsuites%2Fapi%2Ftest_unpack.c;fp=test%2Fsuites%2Fapi%2Ftest_unpack.c;h=b6c7b11da66caf039c8c74c89080bd87f7a44cc2;hb=292ff8f6da2fb3ad3686029946a6d77fdb11aa6f;hp=cfc68eae3bf990a33133bd31b238034b7f902cf8;hpb=3008df9a5b53c9fd0cbdaeb010a2e1b067231eae;p=jansson.git diff --git a/test/suites/api/test_unpack.c b/test/suites/api/test_unpack.c index cfc68ea..b6c7b11 100644 --- a/test/suites/api/test_unpack.c +++ b/test/suites/api/test_unpack.c @@ -1,6 +1,6 @@ /* - * Copyright (c) 2009-2011 Petri Lehtinen - * Copyright (c) 2010-2011 Graeme Smecher + * Copyright (c) 2009-2012 Petri Lehtinen + * Copyright (c) 2010-2012 Graeme Smecher * * Jansson is free software; you can redistribute it and/or modify * it under the terms of the MIT license. See LICENSE for details. @@ -11,7 +11,7 @@ #include #include "util.h" -int main() +static void run_tests() { json_t *j, *j2; int i1, i2, i3; @@ -122,6 +122,13 @@ int main() fail("json_unpack simple array failed"); json_decref(j); + /* object with many items & strict checking */ + j = json_pack("{s:i, s:i, s:i}", "a", 1, "b", 2, "c", 3); + rv = json_unpack(j, "{s:i, s:i, s:i}", "a", &i1, "b", &i2, "c", &i3); + if(rv || i1 != 1 || i2 != 2 || i3 != 3) + fail("json_unpack object with many items failed"); + json_decref(j); + /* * Invalid cases */ @@ -285,7 +292,7 @@ int main() /* Unpack the same item twice */ j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42); if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s)) - fail("json_unpack object with strict validation failed"); + fail("json_unpack object with strict validation failed"); check_error("1 object item(s) left unpacked", "", 1, 10, 10); json_decref(j); @@ -330,5 +337,37 @@ int main() check_error("1 array item(s) left unpacked", "", 1, 5, 5); json_decref(j); - return 0; + /* Optional values */ + j = json_object(); + i1 = 0; + if(json_unpack(j, "{s?i}", "foo", &i1)) + fail("json_unpack failed for optional key"); + if(i1 != 0) + fail("json_unpack unpacked an optional key"); + json_decref(j); + + i1 = 0; + j = json_pack("{si}", "foo", 42); + if(json_unpack(j, "{s?i}", "foo", &i1)) + fail("json_unpack failed for an optional value"); + if(i1 != 42) + fail("json_unpack failed to unpack an optional value"); + json_decref(j); + + j = json_object(); + i1 = i2 = i3 = 0; + if(json_unpack(j, "{s?[ii]s?{s{si}}}", + "foo", &i1, &i2, + "bar", "baz", "quux", &i3)) + fail("json_unpack failed for complex optional values"); + if(i1 != 0 || i2 != 0 || i3 != 0) + fail("json_unpack unexpectedly unpacked something"); + json_decref(j); + + j = json_pack("{s{si}}", "foo", "bar", 42); + if(json_unpack(j, "{s?{s?i}}", "foo", "bar", &i1)) + fail("json_unpack failed for complex optional values"); + if(i1 != 42) + fail("json_unpack failed to unpack"); + json_decref(j); }