# coding: utf-8
from fontTools.misc import etree
from collections import OrderedDict
import io
import pytest
@pytest.mark.parametrize(
"xml",
[
(
""
'text'
"texttail"
""
""
),
(
"\n"
' text\n'
" texttail\n"
" \n"
""
),
(
''
'قطر'
""
),
],
ids=["simple_xml_no_indent", "simple_xml_indent", "xml_ns_attrib_utf_8"],
)
def test_roundtrip_string(xml):
root = etree.fromstring(xml.encode("utf-8"))
result = etree.tostring(root, encoding="utf-8").decode("utf-8")
assert result == xml
def test_pretty_print():
root = etree.Element("root")
attrs = OrderedDict([("c", "2"), ("b", "1"), ("a", "0")])
etree.SubElement(root, "element", attrs).text = "text"
etree.SubElement(root, "element").text = "text"
root.append(etree.Element("empty-element"))
result = etree.tostring(root, encoding="unicode", pretty_print=True)
assert result == (
"\n"
' text\n'
" text\n"
" \n"
"\n"
)