fields: Add FormField.add_kid() method.

This commit is contained in:
Brett Smith 2021-01-05 15:07:49 -05:00
parent 391fde5447
commit 0045d8d032
2 changed files with 14 additions and 0 deletions

View file

@ -131,6 +131,12 @@ class FormField:
except KeyError:
return None
def add_kid(self, kid: 'FormField') -> None:
if kid.parent() is not None:
raise ValueError("given kid field already has a parent")
kid._source['Parent'] = self._source
self._source.setdefault('Kids', []).append(kid._source)
def is_terminal(self) -> bool:
return not self._get_value('Kids', None)

View file

@ -368,3 +368,11 @@ def test_recursive_as_mapping():
_, _, expected_name = expected_key.rpartition('.')
assert field.name() == expected_name
assert next(actual, None) is None
def test_add_kid():
parent = fieldsmod.FormField(field_source('parent'))
kid = fieldsmod.FormField(field_source('kid'))
parent.add_kid(kid)
actual, = parent.kids()
assert actual.name() == 'kid'
assert actual.parent().name() == 'parent'