1# coding: utf-8 2from fontTools.misc import etree 3from collections import OrderedDict 4import io 5import pytest 6 7 8@pytest.mark.parametrize( 9 "xml", 10 [ 11 ( 12 "<root>" 13 '<element key="value">text</element>' 14 "<element>text</element>tail" 15 "<empty-element/>" 16 "</root>" 17 ), 18 ( 19 "<root>\n" 20 ' <element key="value">text</element>\n' 21 " <element>text</element>tail\n" 22 " <empty-element/>\n" 23 "</root>" 24 ), 25 ( 26 '<axis default="400" maximum="1000" minimum="1" name="weight" tag="wght">' 27 '<labelname xml:lang="fa-IR">قطر</labelname>' 28 "</axis>" 29 ), 30 ], 31 ids=["simple_xml_no_indent", "simple_xml_indent", "xml_ns_attrib_utf_8"], 32) 33def test_roundtrip_string(xml): 34 root = etree.fromstring(xml.encode("utf-8")) 35 result = etree.tostring(root, encoding="utf-8").decode("utf-8") 36 assert result == xml 37 38 39def test_pretty_print(): 40 root = etree.Element("root") 41 attrs = OrderedDict([("c", "2"), ("b", "1"), ("a", "0")]) 42 etree.SubElement(root, "element", attrs).text = "text" 43 etree.SubElement(root, "element").text = "text" 44 root.append(etree.Element("empty-element")) 45 46 result = etree.tostring(root, encoding="unicode", pretty_print=True) 47 48 assert result == ( 49 "<root>\n" 50 ' <element c="2" b="1" a="0">text</element>\n' 51 " <element>text</element>\n" 52 " <empty-element/>\n" 53 "</root>\n" 54 ) 55