xref: /aosp_15_r20/external/fonttools/Tests/misc/testTools_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1import fontTools.misc.testTools as testTools
2import unittest
3
4
5class TestToolsTest(unittest.TestCase):
6    def test_parseXML_str(self):
7        self.assertEqual(
8            testTools.parseXML(
9                '<Foo n="1"/>'
10                '<Foo n="2">'
11                "    some ünıcòðe text"
12                '    <Bar color="red"/>'
13                "    some more text"
14                "</Foo>"
15                '<Foo n="3"/>'
16            ),
17            [
18                ("Foo", {"n": "1"}, []),
19                (
20                    "Foo",
21                    {"n": "2"},
22                    [
23                        "    some ünıcòðe text    ",
24                        ("Bar", {"color": "red"}, []),
25                        "    some more text",
26                    ],
27                ),
28                ("Foo", {"n": "3"}, []),
29            ],
30        )
31
32    def test_parseXML_bytes(self):
33        self.assertEqual(
34            testTools.parseXML(
35                b'<Foo n="1"/>'
36                b'<Foo n="2">'
37                b"    some \xc3\xbcn\xc4\xb1c\xc3\xb2\xc3\xb0e text"
38                b'    <Bar color="red"/>'
39                b"    some more text"
40                b"</Foo>"
41                b'<Foo n="3"/>'
42            ),
43            [
44                ("Foo", {"n": "1"}, []),
45                (
46                    "Foo",
47                    {"n": "2"},
48                    [
49                        "    some ünıcòðe text    ",
50                        ("Bar", {"color": "red"}, []),
51                        "    some more text",
52                    ],
53                ),
54                ("Foo", {"n": "3"}, []),
55            ],
56        )
57
58    def test_parseXML_str_list(self):
59        self.assertEqual(
60            testTools.parseXML(['<Foo n="1"/>' '<Foo n="2"/>']),
61            [("Foo", {"n": "1"}, []), ("Foo", {"n": "2"}, [])],
62        )
63
64    def test_parseXML_bytes_list(self):
65        self.assertEqual(
66            testTools.parseXML([b'<Foo n="1"/>' b'<Foo n="2"/>']),
67            [("Foo", {"n": "1"}, []), ("Foo", {"n": "2"}, [])],
68        )
69
70    def test_getXML(self):
71        def toXML(writer, ttFont):
72            writer.simpletag("simple")
73            writer.newline()
74            writer.begintag("tag", attr="value")
75            writer.newline()
76            writer.write("hello world")
77            writer.newline()
78            writer.endtag("tag")
79            writer.newline()  # toXML always ends with a newline
80
81        self.assertEqual(
82            testTools.getXML(toXML),
83            ["<simple/>", '<tag attr="value">', "  hello world", "</tag>"],
84        )
85
86
87if __name__ == "__main__":
88    import sys
89
90    sys.exit(unittest.main())
91