summaryrefslogtreecommitdiff
path: root/tests/test_def.c
diff options
context:
space:
mode:
authorChris Fallin <cfallin@c1f.net>2015-01-14 11:18:20 -0800
committerChris Fallin <cfallin@c1f.net>2015-01-14 11:18:20 -0800
commit87a18f37743efde6f66f77209c98400cdec67cbe (patch)
treefcbab052ead901543a135a15e25fee36189b15bd /tests/test_def.c
parentbebdc009b484295f06185b5bab884a3754bbbacd (diff)
Support oneof defs in upb.
This change adds support for a OneofDef (upb_oneofdef), which represents a 'oneof' as introduced by Protocol Buffers. This is semantically a union type that contains fields and in turn may be added to a MessageDef. This change does not alter parsing or the handler abstraction in any way, because a oneof has impact only at a higher semantic level (i.e., any sort of storage of the fields in a message object), which is user-specific with respect to upb.
Diffstat (limited to 'tests/test_def.c')
-rw-r--r--tests/test_def.c57
1 files changed, 56 insertions, 1 deletions
diff --git a/tests/test_def.c b/tests/test_def.c
index dcf80c0..b85b031 100644
--- a/tests/test_def.c
+++ b/tests/test_def.c
@@ -189,7 +189,9 @@ static upb_fielddef *newfield(
ASSERT(upb_fielddef_setnumber(f, num, NULL));
upb_fielddef_settype(f, type);
upb_fielddef_setlabel(f, label);
- ASSERT(upb_fielddef_setsubdefname(f, type_name, NULL));
+ if (type_name) {
+ ASSERT(upb_fielddef_setsubdefname(f, type_name, NULL));
+ }
return f;
}
@@ -342,6 +344,58 @@ static void test_descriptor_flags() {
upb_msgdef_unref(m2, &m2);
}
+static void test_oneofs() {
+ upb_status s = UPB_STATUS_INIT;
+ bool ok = true;
+
+ upb_symtab *symtab = upb_symtab_new(&symtab);
+ ASSERT(symtab != NULL);
+
+ // Create a test message for fields to refer to.
+ upb_msgdef *subm = upb_msgdef_newnamed("SubMessage", &symtab);
+ upb_msgdef_addfield(subm, newfield("field1", 1, UPB_TYPE_INT32,
+ UPB_LABEL_OPTIONAL, NULL, &symtab),
+ &symtab, NULL);
+ upb_def *subm_defs[] = {UPB_UPCAST(subm)};
+ ASSERT_STATUS(upb_symtab_add(symtab, subm_defs, 1, &symtab, &s), &s);
+
+ upb_msgdef *m = upb_msgdef_newnamed("TestMessage", &symtab);
+ ASSERT(upb_msgdef_numoneofs(m) == 0);
+
+ upb_oneofdef *o = upb_oneofdef_new(&o);
+ ASSERT(upb_oneofdef_numfields(o) == 0);
+ ASSERT(upb_oneofdef_name(o) == NULL);
+
+ ok = upb_oneofdef_setname(o, "test_oneof", &s);
+ ASSERT_STATUS(ok, &s);
+
+ ok = upb_oneofdef_addfield(o, newfield("field1", 1, UPB_TYPE_INT32,
+ UPB_LABEL_OPTIONAL, NULL, &symtab),
+ &symtab, NULL);
+ ASSERT_STATUS(ok, &s);
+ ok = upb_oneofdef_addfield(o, newfield("field2", 2, UPB_TYPE_MESSAGE,
+ UPB_LABEL_OPTIONAL, ".SubMessage",
+ &symtab),
+ &symtab, NULL);
+ ASSERT_STATUS(ok, &s);
+
+ ok = upb_msgdef_addoneof(m, o, NULL, &s);
+ ASSERT_STATUS(ok, &s);
+
+ upb_def *defs[] = {UPB_UPCAST(m)};
+ ASSERT_STATUS(upb_symtab_add(symtab, defs, 1, &symtab, &s), &s);
+
+ ASSERT(upb_msgdef_numoneofs(m) == 1);
+ const upb_oneofdef *lookup_o = upb_msgdef_ntooz(m, "test_oneof");
+ ASSERT(lookup_o == o);
+
+ const upb_fielddef *lookup_field = upb_oneofdef_ntofz(o, "field1");
+ ASSERT(lookup_field != NULL && upb_fielddef_number(lookup_field) == 1);
+
+ upb_symtab_unref(symtab, &symtab);
+ upb_oneofdef_unref(o, &o);
+}
+
int run_tests(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: test_def <test.proto.pb>\n");
@@ -358,5 +412,6 @@ int run_tests(int argc, char *argv[]) {
test_partial_freeze();
test_noreftracking();
test_descriptor_flags();
+ test_oneofs();
return 0;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback