summaryrefslogtreecommitdiff
path: root/tests/test_def.c
diff options
context:
space:
mode:
authorChris Fallin <cfallin@c1f.net>2015-02-02 13:38:05 -0800
committerChris Fallin <cfallin@c1f.net>2015-02-02 13:38:05 -0800
commitfb585045692c482b6946fff63f0cd8425c8c70b5 (patch)
tree5ea54a02a338f2123503f840280a6e037cd65954 /tests/test_def.c
parent51513c6e7f9df3f04fde0ff94bfe654f8dccaaa0 (diff)
Support maps in JSON parsing and serialization.
This is a sync of our internal developing of JSON parsing and serialization. It implements native understanding of MapEntry submessages, so that map fields with (key, value) pairs are serialized as JSON maps (objects) natively rather than as arrays of objects with 'key' and 'value' fields. The parser also now understands how to emit handler calls corresponding to MapEntry objects when processing a map field. This sync also picks up a bugfix in `table.c` to handle an alloc-failed case.
Diffstat (limited to 'tests/test_def.c')
-rw-r--r--tests/test_def.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/test_def.c b/tests/test_def.c
index b85b031..800d685 100644
--- a/tests/test_def.c
+++ b/tests/test_def.c
@@ -344,6 +344,39 @@ static void test_descriptor_flags() {
upb_msgdef_unref(m2, &m2);
}
+static void test_mapentry_check() {
+ upb_status s = UPB_STATUS_INIT;
+
+ upb_msgdef *m = upb_msgdef_new(&m);
+ upb_msgdef_setfullname(m, "TestMessage", &s);
+ upb_fielddef *f = upb_fielddef_new(&f);
+ upb_fielddef_setname(f, "field1", &s);
+ upb_fielddef_setnumber(f, 1, &s);
+ upb_fielddef_setlabel(f, UPB_LABEL_OPTIONAL);
+ upb_fielddef_settype(f, UPB_TYPE_MESSAGE);
+ upb_fielddef_setsubdefname(f, ".MapEntry", &s);
+ upb_msgdef_addfield(m, f, &f, &s);
+ ASSERT(upb_ok(&s));
+
+ upb_msgdef *subm = upb_msgdef_new(&subm);
+ upb_msgdef_setfullname(subm, "MapEntry", &s);
+ upb_msgdef_setmapentry(subm, true);
+
+ upb_symtab *symtab = upb_symtab_new(&symtab);
+ upb_def *defs[] = {UPB_UPCAST(m), UPB_UPCAST(subm)};
+ upb_symtab_add(symtab, defs, 2, NULL, &s);
+ // Should not have succeeded: non-repeated field pointing to a MapEntry.
+ ASSERT(!upb_ok(&s));
+
+ upb_fielddef_setlabel(f, UPB_LABEL_REPEATED);
+ upb_symtab_add(symtab, defs, 2, NULL, &s);
+ ASSERT(upb_ok(&s));
+
+ upb_symtab_unref(symtab, &symtab);
+ upb_msgdef_unref(subm, &subm);
+ upb_msgdef_unref(m, &m);
+}
+
static void test_oneofs() {
upb_status s = UPB_STATUS_INIT;
bool ok = true;
@@ -412,6 +445,7 @@ int run_tests(int argc, char *argv[]) {
test_partial_freeze();
test_noreftracking();
test_descriptor_flags();
+ test_mapentry_check();
test_oneofs();
return 0;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback