1*e1fe3e4aSElliott Hughesfrom io import BytesIO 2*e1fe3e4aSElliott Hughesimport os 3*e1fe3e4aSElliott Hughesimport unittest 4*e1fe3e4aSElliott Hughesfrom fontTools.misc.textTools import bytesjoin, tobytes 5*e1fe3e4aSElliott Hughesfrom fontTools.misc.xmlWriter import XMLWriter 6*e1fe3e4aSElliott Hughes 7*e1fe3e4aSElliott HughesHEADER = b'<?xml version="1.0" encoding="UTF-8"?>\n' 8*e1fe3e4aSElliott Hughes 9*e1fe3e4aSElliott Hughes 10*e1fe3e4aSElliott Hughesclass TestXMLWriter(unittest.TestCase): 11*e1fe3e4aSElliott Hughes def test_comment_escaped(self): 12*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 13*e1fe3e4aSElliott Hughes writer.comment("This&that are <comments>") 14*e1fe3e4aSElliott Hughes self.assertEqual( 15*e1fe3e4aSElliott Hughes HEADER + b"<!-- This&that are <comments> -->", 16*e1fe3e4aSElliott Hughes writer.file.getvalue(), 17*e1fe3e4aSElliott Hughes ) 18*e1fe3e4aSElliott Hughes 19*e1fe3e4aSElliott Hughes def test_comment_multiline(self): 20*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 21*e1fe3e4aSElliott Hughes writer.comment("Hello world\nHow are you?") 22*e1fe3e4aSElliott Hughes self.assertEqual( 23*e1fe3e4aSElliott Hughes HEADER + b"<!-- Hello world\n How are you? -->", writer.file.getvalue() 24*e1fe3e4aSElliott Hughes ) 25*e1fe3e4aSElliott Hughes 26*e1fe3e4aSElliott Hughes def test_encoding_default(self): 27*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 28*e1fe3e4aSElliott Hughes self.assertEqual( 29*e1fe3e4aSElliott Hughes b'<?xml version="1.0" encoding="UTF-8"?>\n', writer.file.getvalue() 30*e1fe3e4aSElliott Hughes ) 31*e1fe3e4aSElliott Hughes 32*e1fe3e4aSElliott Hughes def test_encoding_utf8(self): 33*e1fe3e4aSElliott Hughes # https://github.com/fonttools/fonttools/issues/246 34*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO(), encoding="utf8") 35*e1fe3e4aSElliott Hughes self.assertEqual( 36*e1fe3e4aSElliott Hughes b'<?xml version="1.0" encoding="UTF-8"?>\n', writer.file.getvalue() 37*e1fe3e4aSElliott Hughes ) 38*e1fe3e4aSElliott Hughes 39*e1fe3e4aSElliott Hughes def test_encoding_UTF_8(self): 40*e1fe3e4aSElliott Hughes # https://github.com/fonttools/fonttools/issues/246 41*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO(), encoding="UTF-8") 42*e1fe3e4aSElliott Hughes self.assertEqual( 43*e1fe3e4aSElliott Hughes b'<?xml version="1.0" encoding="UTF-8"?>\n', writer.file.getvalue() 44*e1fe3e4aSElliott Hughes ) 45*e1fe3e4aSElliott Hughes 46*e1fe3e4aSElliott Hughes def test_encoding_UTF8(self): 47*e1fe3e4aSElliott Hughes # https://github.com/fonttools/fonttools/issues/246 48*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO(), encoding="UTF8") 49*e1fe3e4aSElliott Hughes self.assertEqual( 50*e1fe3e4aSElliott Hughes b'<?xml version="1.0" encoding="UTF-8"?>\n', writer.file.getvalue() 51*e1fe3e4aSElliott Hughes ) 52*e1fe3e4aSElliott Hughes 53*e1fe3e4aSElliott Hughes def test_encoding_other(self): 54*e1fe3e4aSElliott Hughes self.assertRaises(Exception, XMLWriter, BytesIO(), encoding="iso-8859-1") 55*e1fe3e4aSElliott Hughes 56*e1fe3e4aSElliott Hughes def test_write(self): 57*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 58*e1fe3e4aSElliott Hughes writer.write("foo&bar") 59*e1fe3e4aSElliott Hughes self.assertEqual(HEADER + b"foo&bar", writer.file.getvalue()) 60*e1fe3e4aSElliott Hughes 61*e1fe3e4aSElliott Hughes def test_indent_dedent(self): 62*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 63*e1fe3e4aSElliott Hughes writer.write("foo") 64*e1fe3e4aSElliott Hughes writer.newline() 65*e1fe3e4aSElliott Hughes writer.indent() 66*e1fe3e4aSElliott Hughes writer.write("bar") 67*e1fe3e4aSElliott Hughes writer.newline() 68*e1fe3e4aSElliott Hughes writer.dedent() 69*e1fe3e4aSElliott Hughes writer.write("baz") 70*e1fe3e4aSElliott Hughes self.assertEqual( 71*e1fe3e4aSElliott Hughes HEADER + bytesjoin(["foo", " bar", "baz"], "\n"), writer.file.getvalue() 72*e1fe3e4aSElliott Hughes ) 73*e1fe3e4aSElliott Hughes 74*e1fe3e4aSElliott Hughes def test_writecdata(self): 75*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 76*e1fe3e4aSElliott Hughes writer.writecdata("foo&bar") 77*e1fe3e4aSElliott Hughes self.assertEqual(HEADER + b"<![CDATA[foo&bar]]>", writer.file.getvalue()) 78*e1fe3e4aSElliott Hughes 79*e1fe3e4aSElliott Hughes def test_simpletag(self): 80*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 81*e1fe3e4aSElliott Hughes writer.simpletag("tag", a="1", b="2") 82*e1fe3e4aSElliott Hughes self.assertEqual(HEADER + b'<tag a="1" b="2"/>', writer.file.getvalue()) 83*e1fe3e4aSElliott Hughes 84*e1fe3e4aSElliott Hughes def test_begintag_endtag(self): 85*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 86*e1fe3e4aSElliott Hughes writer.begintag("tag", attr="value") 87*e1fe3e4aSElliott Hughes writer.write("content") 88*e1fe3e4aSElliott Hughes writer.endtag("tag") 89*e1fe3e4aSElliott Hughes self.assertEqual( 90*e1fe3e4aSElliott Hughes HEADER + b'<tag attr="value">content</tag>', writer.file.getvalue() 91*e1fe3e4aSElliott Hughes ) 92*e1fe3e4aSElliott Hughes 93*e1fe3e4aSElliott Hughes def test_dumphex(self): 94*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 95*e1fe3e4aSElliott Hughes writer.dumphex( 96*e1fe3e4aSElliott Hughes "Type is a beautiful group of letters, not a group of beautiful letters." 97*e1fe3e4aSElliott Hughes ) 98*e1fe3e4aSElliott Hughes self.assertEqual( 99*e1fe3e4aSElliott Hughes HEADER 100*e1fe3e4aSElliott Hughes + bytesjoin( 101*e1fe3e4aSElliott Hughes [ 102*e1fe3e4aSElliott Hughes "54797065 20697320 61206265 61757469", 103*e1fe3e4aSElliott Hughes "66756c20 67726f75 70206f66 206c6574", 104*e1fe3e4aSElliott Hughes "74657273 2c206e6f 74206120 67726f75", 105*e1fe3e4aSElliott Hughes "70206f66 20626561 75746966 756c206c", 106*e1fe3e4aSElliott Hughes "65747465 72732e ", 107*e1fe3e4aSElliott Hughes "", 108*e1fe3e4aSElliott Hughes ], 109*e1fe3e4aSElliott Hughes joiner="\n", 110*e1fe3e4aSElliott Hughes ), 111*e1fe3e4aSElliott Hughes writer.file.getvalue(), 112*e1fe3e4aSElliott Hughes ) 113*e1fe3e4aSElliott Hughes 114*e1fe3e4aSElliott Hughes def test_stringifyattrs(self): 115*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 116*e1fe3e4aSElliott Hughes expected = ' attr="0"' 117*e1fe3e4aSElliott Hughes self.assertEqual(expected, writer.stringifyattrs(attr=0)) 118*e1fe3e4aSElliott Hughes self.assertEqual(expected, writer.stringifyattrs(attr=b"0")) 119*e1fe3e4aSElliott Hughes self.assertEqual(expected, writer.stringifyattrs(attr="0")) 120*e1fe3e4aSElliott Hughes self.assertEqual(expected, writer.stringifyattrs(attr="0")) 121*e1fe3e4aSElliott Hughes 122*e1fe3e4aSElliott Hughes def test_carriage_return_escaped(self): 123*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO()) 124*e1fe3e4aSElliott Hughes writer.write("two lines\r\nseparated by Windows line endings") 125*e1fe3e4aSElliott Hughes self.assertEqual( 126*e1fe3e4aSElliott Hughes HEADER + b"two lines \nseparated by Windows line endings", 127*e1fe3e4aSElliott Hughes writer.file.getvalue(), 128*e1fe3e4aSElliott Hughes ) 129*e1fe3e4aSElliott Hughes 130*e1fe3e4aSElliott Hughes def test_newlinestr(self): 131*e1fe3e4aSElliott Hughes header = b'<?xml version="1.0" encoding="UTF-8"?>' 132*e1fe3e4aSElliott Hughes 133*e1fe3e4aSElliott Hughes for nls in (None, "\n", "\r\n", "\r", ""): 134*e1fe3e4aSElliott Hughes writer = XMLWriter(BytesIO(), newlinestr=nls) 135*e1fe3e4aSElliott Hughes writer.write("hello") 136*e1fe3e4aSElliott Hughes writer.newline() 137*e1fe3e4aSElliott Hughes writer.write("world") 138*e1fe3e4aSElliott Hughes writer.newline() 139*e1fe3e4aSElliott Hughes 140*e1fe3e4aSElliott Hughes linesep = tobytes(os.linesep) if nls is None else tobytes(nls) 141*e1fe3e4aSElliott Hughes 142*e1fe3e4aSElliott Hughes self.assertEqual( 143*e1fe3e4aSElliott Hughes header + linesep + b"hello" + linesep + b"world" + linesep, 144*e1fe3e4aSElliott Hughes writer.file.getvalue(), 145*e1fe3e4aSElliott Hughes ) 146*e1fe3e4aSElliott Hughes 147*e1fe3e4aSElliott Hughes 148*e1fe3e4aSElliott Hughesif __name__ == "__main__": 149*e1fe3e4aSElliott Hughes import sys 150*e1fe3e4aSElliott Hughes 151*e1fe3e4aSElliott Hughes sys.exit(unittest.main()) 152