def _make_namespace_with_a_union(): # type: (...) -> ApiNamespace ns = ApiNamespace('ns_with_a_union') u1 = Union(name='Union', namespace=ns, token=None, closed=True) u1.set_attributes( doc=None, fields=[ UnionField(name="first", doc=None, data_type=Void(), token=None), UnionField(name="last", doc=None, data_type=Void(), token=None), ], ) ns.add_data_type(u1) # A more interesting case with non-void variants. shape_union = Union(name='Shape', namespace=ns, token=None, closed=True) shape_union.set_attributes( doc=None, fields=[ UnionField(name="point", doc=None, data_type=Void(), token=None), UnionField(name="circle", doc=None, data_type=Float64(), token=None), ], ) ns.add_data_type(shape_union) return ns
def test_union(self): ns = ApiNamespace('files') update_parent_rev = Struct( 'UpdateParentRev', None, ns, ) update_parent_rev.set_attributes( "Overwrite existing file if the parent rev matches.", [ StructField('parent_rev', String(), 'The revision to be updated.', None) ], ) update_parent_rev._add_example( StoneExample(path=None, lineno=None, lexpos=None, label='default', text=None, fields={ 'parent_rev': StoneExampleField(None, None, None, 'parent_rev', 'xyz123') })) # test variants with only tags, as well as those with structs. conflict = Union( 'WriteConflictPolicy', None, ns, True, ) conflict.set_attributes( 'Policy for managing write conflicts.', [ UnionField('reject', Void(), 'On a write conflict, reject the new file.', None), UnionField( 'overwrite', Void(), 'On a write conflict, overwrite the existing file.', None), UnionField( 'update_if_matching_parent_rev', update_parent_rev, 'On a write conflict, overwrite the existing file.', None), ], ) conflict._add_example( StoneExample(path=None, lineno=None, lexpos=None, label='default', text=None, fields={ 'update_if_matching_parent_rev': StoneExampleField( None, None, None, 'update_if_matching_parent_rev', StoneExampleRef(None, None, None, 'default')) })) conflict._compute_examples() # test that only null value is returned for an example of a Void type self.assertEqual(conflict.get_examples()['reject'].value, {'.tag': 'reject'}) # test that dict is returned for a tagged struct variant self.assertEqual(conflict.get_examples()['default'].value, { '.tag': 'update_if_matching_parent_rev', 'parent_rev': 'xyz123' })