def test_appended_text_contents_arent_lost():
    root = Document("<root><a><b><c><d/></c></b></a></root>").root

    for node in tuple(root.child_nodes(is_tag_node, recurse=True)):
        node.prepend_child("D")
        node.add_next("T")
    for node in tuple(root.child_nodes(is_text_node, recurse=True)):
        node.add_next("Z")

    gc.collect()
    assert str(
        root) == "<root><a>DZ<b>DZ<c>DZ<d>DZ</d>TZ</c>TZ</b>TZ</a>TZ</root>"
Beispiel #2
0
def test_first():
    assert first([]) is None
    assert first([1]) == 1
    assert first((1, 2)) == 1

    root = Document("<root><a/><a/><b/></root>").root
    assert first(root.child_nodes(is_tag_node, lambda x: x.local_name == "c")) is None
    assert (
        first(root.child_nodes(is_tag_node, lambda x: x.local_name == "b")) is root[2]
    )
    assert (
        first(root.child_nodes(is_tag_node, lambda x: x.local_name == "a")) is root[0]
    )

    with pytest.raises(TypeError):
        first({})
Beispiel #3
0
def test_iter_next_node_over_long_stream(files_path):
    root = Document(files_path / "marx_manifestws_1848.TEI-P5.xml").root

    node = root.next_node_in_stream(lambda _: False)
    assert node is None

    all_node_locations = set()
    for node in root.child_nodes(is_tag_node, recurse=True):
        all_node_locations.add(node.location_path)
    encountered_location_paths = set()
    for node in root.iterate_next_nodes_in_stream(is_tag_node):
        location_path = node.location_path
        assert location_path not in encountered_location_paths
        encountered_location_paths.add(location_path)
    assert encountered_location_paths == all_node_locations

    expected_text = root.full_text  # operates in tree dimension
    collected_text = ""
    for node in root.iterate_next_nodes_in_stream(is_text_node):
        collected_text += node.content
    assert collected_text == expected_text