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