Add Union on top level union messages

Closes https://github.com/capnproto/pycapnp/issues/247
This commit is contained in:
John Vandenberg
2021-05-31 16:14:21 +08:00
parent 581332f9c9
commit 0c904443bd
3 changed files with 46 additions and 0 deletions

View File

@@ -50,3 +50,21 @@ struct TestAllTypes {
enumList @32 : List(TestEnum);
interfaceList @33 : List(Void); # TODO
}
struct UnionAllTypes {
union {
unionStructField1 @0 : TestAllTypes;
unionStructField2 @1 : TestAllTypes;
}
}
struct GroupedUnionAllTypes {
union {
g1 :group {
unionStructField1 @0 : TestAllTypes;
}
g2 :group {
unionStructField2 @1 : TestAllTypes;
}
}
}

View File

@@ -189,9 +189,30 @@ def test_set_dict(all_types):
def test_set_dict_union(addressbook):
person = addressbook.Person.new_message(**{'employment': {'employer': {'name': 'foo'}}})
assert person.employment.which == addressbook.Person.Employment.employer
assert person.employment.employer.name == 'foo'
def test_union_enum(all_types):
assert all_types.UnionAllTypes.Union.UnionStructField1 == 0
assert all_types.UnionAllTypes.Union.UnionStructField2 == 1
msg = all_types.UnionAllTypes.new_message(**{'unionStructField1': {'textField': "foo"}})
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField1
msg = all_types.UnionAllTypes.new_message(**{'unionStructField2': {'textField': "foo"}})
assert msg.which == all_types.UnionAllTypes.Union.UnionStructField2
assert all_types.GroupedUnionAllTypes.Union.G1 == 0
assert all_types.GroupedUnionAllTypes.Union.G2 == 1
msg = all_types.GroupedUnionAllTypes.new_message(**{'g1': {'unionStructField1': {'textField': "foo"}}})
assert msg.which == all_types.GroupedUnionAllTypes.Union.G1
msg = all_types.GroupedUnionAllTypes.new_message(**{'g2': {'unionStructField2': {'textField': "foo"}}})
assert msg.which == all_types.GroupedUnionAllTypes.Union.G2
def isstr(s):
return isinstance(s, str)