aboutsummaryrefslogtreecommitdiff
path: root/Tests/misc/xmlWriter_test.py
blob: fd4f24089916bd580988b4f0e90b46e873049d38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from fontTools.misc.py23 import bytesjoin, tobytes
from io import BytesIO
import os
import unittest
from fontTools.misc.xmlWriter import XMLWriter

linesep = tobytes(os.linesep)
HEADER = b'<?xml version="1.0" encoding="UTF-8"?>' + linesep

class TestXMLWriter(unittest.TestCase):

	def test_comment_escaped(self):
		writer = XMLWriter(BytesIO())
		writer.comment("This&that are <comments>")
		self.assertEqual(HEADER + b"<!-- This&amp;that are &lt;comments&gt; -->", writer.file.getvalue())

	def test_comment_multiline(self):
		writer = XMLWriter(BytesIO())
		writer.comment("Hello world\nHow are you?")
		self.assertEqual(HEADER + b"<!-- Hello world" + linesep + b"     How are you? -->",
				 writer.file.getvalue())

	def test_encoding_default(self):
		writer = XMLWriter(BytesIO())
		self.assertEqual(b'<?xml version="1.0" encoding="UTF-8"?>' + linesep,
				 writer.file.getvalue())

	def test_encoding_utf8(self):
		# https://github.com/fonttools/fonttools/issues/246
		writer = XMLWriter(BytesIO(), encoding="utf8")
		self.assertEqual(b'<?xml version="1.0" encoding="UTF-8"?>' + linesep,
				 writer.file.getvalue())

	def test_encoding_UTF_8(self):
		# https://github.com/fonttools/fonttools/issues/246
		writer = XMLWriter(BytesIO(), encoding="UTF-8")
		self.assertEqual(b'<?xml version="1.0" encoding="UTF-8"?>' + linesep,
				 writer.file.getvalue())

	def test_encoding_UTF8(self):
		# https://github.com/fonttools/fonttools/issues/246
		writer = XMLWriter(BytesIO(), encoding="UTF8")
		self.assertEqual(b'<?xml version="1.0" encoding="UTF-8"?>' + linesep,
				 writer.file.getvalue())

	def test_encoding_other(self):
		self.assertRaises(Exception, XMLWriter, BytesIO(),
				  encoding="iso-8859-1")

	def test_write(self):
		writer = XMLWriter(BytesIO())
		writer.write("foo&bar")
		self.assertEqual(HEADER + b"foo&amp;bar", writer.file.getvalue())

	def test_indent_dedent(self):
		writer = XMLWriter(BytesIO())
		writer.write("foo")
		writer.newline()
		writer.indent()
		writer.write("bar")
		writer.newline()
		writer.dedent()
		writer.write("baz")
		self.assertEqual(HEADER + bytesjoin(["foo", "  bar", "baz"], linesep),
				 writer.file.getvalue())

	def test_writecdata(self):
		writer = XMLWriter(BytesIO())
		writer.writecdata("foo&bar")
		self.assertEqual(HEADER + b"<![CDATA[foo&bar]]>", writer.file.getvalue())

	def test_simpletag(self):
		writer = XMLWriter(BytesIO())
		writer.simpletag("tag", a="1", b="2")
		self.assertEqual(HEADER + b'<tag a="1" b="2"/>', writer.file.getvalue())

	def test_begintag_endtag(self):
		writer = XMLWriter(BytesIO())
		writer.begintag("tag", attr="value")
		writer.write("content")
		writer.endtag("tag")
		self.assertEqual(HEADER + b'<tag attr="value">content</tag>', writer.file.getvalue())

	def test_dumphex(self):
		writer = XMLWriter(BytesIO())
		writer.dumphex("Type is a beautiful group of letters, not a group of beautiful letters.")
		self.assertEqual(HEADER + bytesjoin([
		    "54797065 20697320 61206265 61757469",
		    "66756c20 67726f75 70206f66 206c6574",
		    "74657273 2c206e6f 74206120 67726f75",
		    "70206f66 20626561 75746966 756c206c",
		    "65747465 72732e  ", ""], joiner=linesep), writer.file.getvalue())

	def test_stringifyattrs(self):
		writer = XMLWriter(BytesIO())
		expected = ' attr="0"'
		self.assertEqual(expected, writer.stringifyattrs(attr=0))
		self.assertEqual(expected, writer.stringifyattrs(attr=b'0'))
		self.assertEqual(expected, writer.stringifyattrs(attr='0'))
		self.assertEqual(expected, writer.stringifyattrs(attr=u'0'))

	def test_carriage_return_escaped(self):
		writer = XMLWriter(BytesIO())
		writer.write("two lines\r\nseparated by Windows line endings")
		self.assertEqual(
			HEADER + b'two lines&#13;\nseparated by Windows line endings',
			writer.file.getvalue())

	def test_newlinestr(self):
		header = b'<?xml version="1.0" encoding="UTF-8"?>'

		for nls in (None, '\n', '\r\n', '\r', ''):
			writer = XMLWriter(BytesIO(), newlinestr=nls)
			writer.write("hello")
			writer.newline()
			writer.write("world")
			writer.newline()

			linesep = tobytes(os.linesep) if nls is None else tobytes(nls)

			self.assertEqual(
				header + linesep + b"hello" + linesep + b"world" + linesep,
				writer.file.getvalue())


if __name__ == '__main__':
	import sys
	sys.exit(unittest.main())