• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import fontTools.misc.testTools as testTools
2import unittest
3
4
5class TestToolsTest(unittest.TestCase):
6
7    def test_parseXML_str(self):
8        self.assertEqual(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                ("Foo", {"n": "1"}, []),
17                ("Foo", {"n": "2"}, [
18                    "    some ünıcòðe text    ",
19                    ("Bar", {"color": "red"}, []),
20                    "    some more text",
21                ]),
22                ("Foo", {"n": "3"}, [])
23            ])
24
25    def test_parseXML_bytes(self):
26        self.assertEqual(testTools.parseXML(
27            b'<Foo n="1"/>'
28            b'<Foo n="2">'
29            b'    some \xc3\xbcn\xc4\xb1c\xc3\xb2\xc3\xb0e text'
30            b'    <Bar color="red"/>'
31            b'    some more text'
32            b'</Foo>'
33            b'<Foo n="3"/>'), [
34                ("Foo", {"n": "1"}, []),
35                ("Foo", {"n": "2"}, [
36                    "    some ünıcòðe text    ",
37                    ("Bar", {"color": "red"}, []),
38                    "    some more text",
39                ]),
40                ("Foo", {"n": "3"}, [])
41            ])
42
43    def test_parseXML_str_list(self):
44        self.assertEqual(testTools.parseXML(
45            ['<Foo n="1"/>'
46             '<Foo n="2"/>']), [
47                ("Foo", {"n": "1"}, []),
48                ("Foo", {"n": "2"}, [])
49            ])
50
51    def test_parseXML_bytes_list(self):
52        self.assertEqual(testTools.parseXML(
53            [b'<Foo n="1"/>'
54             b'<Foo n="2"/>']), [
55                ("Foo", {"n": "1"}, []),
56                ("Foo", {"n": "2"}, [])
57            ])
58
59    def test_getXML(self):
60        def toXML(writer, ttFont):
61            writer.simpletag("simple")
62            writer.newline()
63            writer.begintag("tag", attr='value')
64            writer.newline()
65            writer.write("hello world")
66            writer.newline()
67            writer.endtag("tag")
68            writer.newline()  # toXML always ends with a newline
69
70        self.assertEqual(testTools.getXML(toXML),
71                         ['<simple/>',
72                          '<tag attr="value">',
73                          '  hello world',
74                          '</tag>'])
75
76
77if __name__ == "__main__":
78    import sys
79    sys.exit(unittest.main())
80