def test_tag_definition_copies_attributes(): root = Document('<root foo="bar"/>').root definition = tag("test", root.attributes) root.attributes["bar"] = "foo" root.append_child(definition) assert root.attributes == {"bar": "foo", "foo": "bar"} assert root.first_child.attributes == {"foo": "bar"}
def test_clone_quick_and_unsafe(sample_document): assert str(sample_document.root.clone( deep=True, quick_and_unsafe=True)) == str(sample_document.root) root = Document("<root>node a</root>").root root.append_child("|node b") assert str(root.clone(deep=True, quick_and_unsafe=True)) == "<root>node a</root>"
def test_setup_long_term_references(long_term_references): long_term_references["a_document"] = Document("<document/>") root = Document("<root>a</root>").root root.append_child("b") long_term_references["appended_b"] = root.last_child root = Document("<root>a</root>").root root.append_child("b") root.append_child("c") long_term_references["appended_b_with_c"] = root.last_child
def test_wrapper_cache(): gc.collect() assert len(_wrapper_cache.wrappers) == 0 root = Document("<root/>").root assert len(_wrapper_cache.wrappers) == 1 root.append_child(tag("node")) assert len(_wrapper_cache.wrappers) == 2 root.first_child.detach() gc.collect() assert len(_wrapper_cache.wrappers) == 1
def test_fetch_or_create_by_xpath(): root = Document("<root><intermediate/></root>").root assert str(root.fetch_or_create_by_xpath("./test")) == "<test/>" assert str(root) == "<root><intermediate/><test/></root>" assert str(root.fetch_or_create_by_xpath("./intermediate/target")) == "<target/>" assert str(root) == "<root><intermediate><target/></intermediate><test/></root>" assert str(root.fetch_or_create_by_xpath("./intermediate/target")) == "<target/>" assert str(root) == "<root><intermediate><target/></intermediate><test/></root>" root.append_child(tag("intermediate")) with pytest.raises(InvalidOperation): root.fetch_or_create_by_xpath("./intermediate") with pytest.raises(InvalidOperation): root.fetch_or_create_by_xpath("./intermediate/test")
def test_processing_instruction_node(): root = Document("<root/>").root root.append_child(new_processing_instruction_node("foo", "bar")) with altered_default_filters(): pi = root.first_child assert pi.target == "foo" assert pi.content == "bar" assert str(pi) == "<?foo bar?>" assert pi == new_processing_instruction_node("foo", "bar") assert pi != root assert pi != new_processing_instruction_node("ham", "bar") assert str(root) == "<root><?foo bar?></root>" assert pi.new_tag_node("ham").local_name == "ham"
def test_serialization(): root = Document("<root>a</root>").root root.append_child("b") assert str(root) == "<root>ab</root>"