11.08., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

Commit afda6166 authored by di68kap's avatar di68kap

- syntaxtree.py: added Node.index()-method

parent abb212a2
...@@ -505,6 +505,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil ...@@ -505,6 +505,7 @@ class Node: # (collections.abc.Sized): Base class omitted for cython-compatibil
if child.tag_name == tag_name: if child.tag_name == tag_name:
return i return i
i += 1 i += 1
raise ValueError("Node with tag name '%s' not among child-nodes." % tag_name)
def select(self, match_function: Callable, include_root: bool = False, reverse: bool = False) \ def select(self, match_function: Callable, include_root: bool = False, reverse: bool = False) \
-> Iterator['Node']: -> Iterator['Node']:
......
...@@ -317,6 +317,17 @@ class TestNodeFind(): ...@@ -317,6 +317,17 @@ class TestNodeFind():
assert 'c' not in tree assert 'c' not in tree
assert any(tree.select_by_tag('c', False)) assert any(tree.select_by_tag('c', False))
def test_index(self):
tree = parse_sxpr('(a (b 0) (c 1) (d 2))')
assert tree.index('d') == 2
assert tree.index('b') == 0
assert tree.index('c') == 1
try:
i = tree.index('x')
raise AssertionError('ValueError expected!')
except ValueError:
pass
class TestSerialization: class TestSerialization:
def test_sxpr_roundtrip(self): def test_sxpr_roundtrip(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment