aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorW. Trevor King <wking@tremily.us>2015-12-17 16:56:22 -0800
committerW. Trevor King <wking@tremily.us>2015-12-22 10:05:31 -0800
commit1d513d063a65b7b2cfbb042b4068728f49516df5 (patch)
tree1af19f6bcca766a63f9b4aa64c672f3331665b9f /test
parente44b2231b50aea5de78b7ea2debec0d5327cd711 (diff)
downloadjansson-1d513d063a65b7b2cfbb042b4068728f49516df5.zip
jansson-1d513d063a65b7b2cfbb042b4068728f49516df5.tar.gz
jansson-1d513d063a65b7b2cfbb042b4068728f49516df5.tar.bz2
pack_unpack: List unrecognized keys in strict unpacking
Otherwise figuring out what's wrong with your JSON can be tricky, especially if you're using a single fmt string to validate a large, complicated schema. The comma delimiting will make separating keys that contain commas difficult. For example: {"foo, bar": true, "baz": false} will generate errors like: 2 object item(s) left unpacked: foo, bar, baz but that seems like a small enough corner case to not be worth much worrying. I wanted to find a way to handle this without have_unrecognized_keys, but the strbuffer tooling makes it look like I shouldn't be reaching in to do things like: strbuffer_t unrecognized_keys; unrecognized_keys.value = NULL; and then using 'unrecognized_keys.value == NULL' in place of have_unrecognized_keys.
Diffstat (limited to 'test')
-rw-r--r--test/suites/api/test_unpack.c14
-rw-r--r--test/suites/api/util.h22
2 files changed, 29 insertions, 7 deletions
diff --git a/test/suites/api/test_unpack.c b/test/suites/api/test_unpack.c
index 6b76106..babe0dd 100644
--- a/test/suites/api/test_unpack.c
+++ b/test/suites/api/test_unpack.c
@@ -298,10 +298,16 @@ static void run_tests()
json_decref(j);
/* Unpack the same item twice */
- j = json_pack("{s:s, s:i}", "foo", "bar", "baz", 42);
+ j = json_pack("{s:s, s:i, s:b}", "foo", "bar", "baz", 42, "quux", 1);
if(!json_unpack_ex(j, &error, 0, "{s:s,s:s!}", "foo", &s, "foo", &s))
fail("json_unpack object with strict validation failed");
- check_error("1 object item(s) left unpacked", "<validation>", 1, 10, 10);
+ {
+ const char *possible_errors[] = {
+ "2 object item(s) left unpacked: baz, quux",
+ "2 object item(s) left unpacked: quux, baz"
+ };
+ check_errors(possible_errors, 2, "<validation>", 1, 10, 10);
+ }
json_decref(j);
j = json_pack("[i,{s:i,s:n},[i,i]]", 1, "foo", 2, "bar", 3, 4);
@@ -335,7 +341,7 @@ static void run_tests()
j = json_pack("{s{snsn}}", "foo", "bar", "baz");
if(!json_unpack_ex(j, &error, 0, "{s{sn!}}", "foo", "bar"))
fail("json_unpack nested object with strict validation failed");
- check_error("1 object item(s) left unpacked", "<validation>", 1, 7, 7);
+ check_error("1 object item(s) left unpacked: baz", "<validation>", 1, 7, 7);
json_decref(j);
/* Error in nested array */
@@ -395,6 +401,6 @@ static void run_tests()
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);
+ check_error("1 object item(s) left unpacked: baz", "<validation>", 1, 8, 8);
json_decref(j);
}
diff --git a/test/suites/api/util.h b/test/suites/api/util.h
index 1bc4c9c..ea0be34 100644
--- a/test/suites/api/util.h
+++ b/test/suites/api/util.h
@@ -30,11 +30,22 @@
} while(0)
/* Assumes json_error_t error */
-#define check_error(text_, source_, line_, column_, position_) \
+#define check_errors(texts_, num_, source_, line_, column_, position_) \
do { \
- if(strcmp(error.text, text_) != 0) { \
+ int i_, found_ = 0; \
+ for(i_ = 0; i_ < num_; i_++) { \
+ if(strcmp(error.text, texts_[i_]) == 0) { \
+ found_ = 1; \
+ break; \
+ } \
+ } \
+ if (!found_) { \
failhdr; \
- fprintf(stderr, "text: \"%s\" != \"%s\"\n", error.text, text_); \
+ if (num_ == 1) { \
+ fprintf(stderr, "text: \"%s\" != \"%s\"\n", error.text, texts_[0]); \
+ } else { \
+ fprintf(stderr, "text: \"%s\" does not match\n", error.text); \
+ } \
exit(1); \
} \
if(strcmp(error.source, source_) != 0) { \
@@ -61,6 +72,11 @@
} while(0)
+/* Assumes json_error_t error */
+#define check_error(text_, source_, line_, column_, position_) \
+ check_errors(&text_, 1, source_, line_, column_, position_)
+
+
static void run_tests();
int main() {