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