diff --git a/delb/__init__.py b/delb/__init__.py index 64571fd..a6ce8f0 100644 --- a/delb/__init__.py +++ b/delb/__init__.py @@ -228,7 +228,7 @@ class Document(metaclass=DocumentMeta): >>> document = Document("") >>> str(document) - "" + '' :param source: Anything that the configured loaders can make sense of to return a parsed document tree. @@ -488,7 +488,7 @@ def save( ) def __serialize(self, serializer, encoding, indentation): - declaration = f"" + declaration = f'' if indentation: declaration += "\n" serializer.buffer.write(declaration) @@ -496,8 +496,11 @@ def __serialize(self, serializer, encoding, indentation): serializer.serialize_node(node) with altered_default_filters(): serializer.serialize_root(self.root) - for node in self.tail_nodes: - serializer.serialize_node(node) + if self.tail_nodes: + if indentation: + serializer.buffer.write("\n") + for node in self.tail_nodes: + serializer.serialize_node(node) def write( self, diff --git a/tests/test_comment_and_pi_nodes.py b/tests/test_comment_and_pi_nodes.py index 0fd048f..df94c9e 100644 --- a/tests/test_comment_and_pi_nodes.py +++ b/tests/test_comment_and_pi_nodes.py @@ -69,7 +69,7 @@ def test_comment_node(): with altered_default_filters(): assert ( str(root.document) - == "" + == '' ) diff --git a/tests/test_document.py b/tests/test_document.py index f55ad61..e8c3af7 100644 --- a/tests/test_document.py +++ b/tests/test_document.py @@ -120,7 +120,7 @@ class DocumentSubclass(Document): def test_set_root(): document = Document("") document.root = document.root[0].detach() - assert str(document) == "" + assert str(document) == '' document_2 = Document("parts") with pytest.raises(ValueError, match="detached node"): @@ -140,8 +140,8 @@ def test_root_siblings(): assert len(head_nodes) == len(tail_nodes) == 2 - assert ( - str(document) == "" + assert str(document) == ( + '' "" ) diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 7b6a525..f9acbf5 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -86,9 +86,8 @@ def test_insert_issue_in_a_more_complex_situation(): document = Document(" ") for node in document.root.css_select("bar,foo"): node.detach(retain_child_nodes=True) - assert ( - str(document) == "" - " " + assert str(document) == ( + '' " " ) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 1567885..79714ff 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -14,7 +14,7 @@ ) from _delb.nodes import DETACHED -from tests.utils import assert_documents_are_semantical_equal, count_pis +from tests.utils import assert_documents_are_semantical_equal @pytest.mark.parametrize( @@ -57,7 +57,7 @@ def test_align_attributes(indentation, out): " ", 'hi', """\ - + hi @@ -91,8 +91,8 @@ def test_significant_whitespace_is_saved(result_file): root[2].append_children("world!") document.save(result_file) - assert ( - result_file.read_text() == "" + assert result_file.read_text() == ( + '' "Hello world!" ) @@ -106,7 +106,7 @@ def test_significant_whitespace_is_saved(result_file): ) assert result_file.read_text().splitlines() == [ - "", + '', "", " Hello", " ", # FIXME? @@ -154,30 +154,33 @@ def test_single_nodes(declarations, node_constructor, args, out): def test_that_root_siblings_are_preserved(files_path, result_file): - Document(files_path / "root_siblings.xml").clone().save(result_file) - assert count_pis(result_file) == { - '': 1, - '': 2, - } + origin_path = files_path / "root_siblings.xml" + Document(origin_path).save(result_file, indentation=" ") - assert result_file.read_text() == ( - "" - '' - '' - "" - '' - "" - "" + assert ( + origin_path.read_text() + == result_file.read_text() + == ( + '\n' + '\n' + '\n' + "\n" + '\n' + "\n" + "\n" + ) ) def test_transparency(files_path, result_file): + parser_options = ParserOptions(collapse_whitespace=False) for file in (x for x in files_path.glob("[!tei_]*.xml")): - Document(file, parser_options=ParserOptions(collapse_whitespace=False)).save( - result_file - ) + origin = Document(file, parser_options=parser_options) + origin.save(result_file) + _copy = Document(file, parser_options=parser_options) assert_documents_are_semantical_equal(file, result_file) - assert count_pis(file) == count_pis(result_file) + assert origin.head_nodes == _copy.head_nodes + assert origin.tail_nodes == _copy.tail_nodes @pytest.mark.parametrize( @@ -279,5 +282,5 @@ def test_text_width(files_path, indentation, text_width, out): def test_xml_declaration(files_path): assert str(Document(files_path / "tei_marx_manifestws_1848.TEI-P5.xml")).startswith( - "" + '' ) diff --git a/tests/test_tag_node.py b/tests/test_tag_node.py index 1482400..7def1f7 100644 --- a/tests/test_tag_node.py +++ b/tests/test_tag_node.py @@ -131,7 +131,7 @@ def test_detach_and_document_property(): assert node.parent is None assert node.document is None assert root.document is document - assert str(document) == "" + assert str(document) == '' def test_detach_node_with_tail_1(): diff --git a/tests/utils.py b/tests/utils.py index 48c873a..3534e94 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,6 +1,5 @@ import re import sys -from collections import Counter from xmldiff import main as xmldiff @@ -47,12 +46,6 @@ def assert_nodes_are_in_document_order(*nodes): raise AssertionError -def count_pis(source): - with source.open("rt") as f: - pi_strings = find_processing_instructions(f.read()) - return Counter(x[0] for x in pi_strings if not x[0].startswith("