MCPcopy
hub / github.com/sphinx-doc/sphinx / assert_node

Function assert_node

sphinx/testing/util.py:30–74  ·  view source on GitHub ↗
(node: Node, cls: Any = None, xpath: str = '', **kwargs: Any)

Source from the content-addressed store, hash-verified

28
29
30def 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

Callers 15

test_sampFunction · 0.90
test_samp_roleFunction · 0.90
test_download_roleFunction · 0.90
test_XRefRoleFunction · 0.90
test_rst_prologFunction · 0.90
test_default_role1Function · 0.90
test_default_role2Function · 0.90
test_productionlist_xrefFunction · 0.90
test_toctreeFunction · 0.90
test_relative_toctreeFunction · 0.90

Calls 2

replaceMethod · 0.80
itemsMethod · 0.45

Tested by 15

test_sampFunction · 0.72
test_samp_roleFunction · 0.72
test_download_roleFunction · 0.72
test_XRefRoleFunction · 0.72
test_rst_prologFunction · 0.72
test_default_role1Function · 0.72
test_default_role2Function · 0.72
test_productionlist_xrefFunction · 0.72
test_toctreeFunction · 0.72
test_relative_toctreeFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…