fields: Add FormField.add_kid() method.
This commit is contained in:
parent
391fde5447
commit
0045d8d032
2 changed files with 14 additions and 0 deletions
|
@ -131,6 +131,12 @@ class FormField:
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return None
|
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:
|
def is_terminal(self) -> bool:
|
||||||
return not self._get_value('Kids', None)
|
return not self._get_value('Kids', None)
|
||||||
|
|
||||||
|
|
|
@ -368,3 +368,11 @@ def test_recursive_as_mapping():
|
||||||
_, _, expected_name = expected_key.rpartition('.')
|
_, _, expected_name = expected_key.rpartition('.')
|
||||||
assert field.name() == expected_name
|
assert field.name() == expected_name
|
||||||
assert next(actual, None) is None
|
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'
|
||||||
|
|
Loading…
Reference in a new issue