(node: Node, cls: Any = None, xpath: str = '', **kwargs: Any)
| 28 | |
| 29 | |
| 30 | def assert_node(node: Node, cls: Any = None, xpath: str = '', **kwargs: Any) -> None: |
| 31 | if cls: |
| 32 | if isinstance(cls, list): |
| 33 | assert_node(node, cls[0], xpath=xpath, **kwargs) |
| 34 | if cls[1:]: |
| 35 | if isinstance(cls[1], tuple): |
| 36 | assert_node(node, cls[1], xpath=xpath, **kwargs) |
| 37 | else: |
| 38 | assert ( |
| 39 | isinstance(node, nodes.Element) |
| 40 | ), f'The node{xpath} does not have any children' # fmt: skip |
| 41 | assert len(node) == 1, ( |
| 42 | f'The node{xpath} has {len(node)} child nodes, not one' |
| 43 | ) |
| 44 | assert_node(node[0], cls[1:], xpath=xpath + '[0]', **kwargs) |
| 45 | elif isinstance(cls, tuple): |
| 46 | assert ( |
| 47 | isinstance(node, (list, nodes.Element)) |
| 48 | ), f'The node{xpath} does not have any items' # fmt: skip |
| 49 | assert ( |
| 50 | len(node) == len(cls) |
| 51 | ), f'The node{xpath} has {len(node)} child nodes, not {len(cls)!r}' # fmt: skip |
| 52 | for i, nodecls in enumerate(cls): |
| 53 | path = xpath + f'[{i}]' |
| 54 | assert_node(node[i], nodecls, xpath=path, **kwargs) |
| 55 | elif isinstance(cls, str): |
| 56 | assert node == cls, f'The node {xpath!r} is not {cls!r}: {node!r}' |
| 57 | else: |
| 58 | assert ( |
| 59 | isinstance(node, cls) |
| 60 | ), f'The node{xpath} is not subclass of {cls!r}: {node!r}' # fmt: skip |
| 61 | |
| 62 | if kwargs: |
| 63 | assert ( |
| 64 | isinstance(node, nodes.Element) |
| 65 | ), f'The node{xpath} does not have any attributes' # fmt: skip |
| 66 | |
| 67 | for key, value in kwargs.items(): |
| 68 | if key not in node: |
| 69 | if (key := key.replace('_', '-')) not in node: |
| 70 | msg = f'The node{xpath} does not have {key!r} attribute: {node!r}' |
| 71 | raise AssertionError(msg) |
| 72 | assert node[key] == value, ( |
| 73 | f'The node{xpath}[{key}] is not {value!r}: {node[key]!r}' |
| 74 | ) |
| 75 | |
| 76 | |
| 77 | # keep this to restrict the API usage and to have a correct return type |
searching dependent graphs…