-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.py
177 lines (159 loc) · 5.61 KB
/
tests.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# -*- coding:utf-8 -*-
from __future__ import with_statement
import six
import unittest
from io import BytesIO
import xml.etree.cElementTree as ET
import elementflow
def _bytes(value):
if six.PY2:
return value
return bytes(value, encoding='utf-8')
class XML(unittest.TestCase):
def test_xml(self):
buffer = BytesIO()
with elementflow.xml(buffer, u'root') as xml:
with xml.container(u'container', {u'key': u'"значение"'}):
xml.text(u'<Текст> контейнера')
xml.element(u'item')
xml.element(u'item', text=u'Текст')
buffer.seek(0)
tree = ET.parse(buffer)
buffer = BytesIO()
tree.write(buffer, encoding='utf-8')
self.assertEqual(
buffer.getvalue(),
_bytes(
'<root>'
'<container key=""значение"">'
'<Текст> контейнера'
'<item />'
'</container>'
'<item>Текст</item>'
'</root>'
)
)
def test_non_well_formed_on_exception(self):
buffer = BytesIO()
try:
with elementflow.xml(buffer, u'root') as xml:
xml.text(u'Text')
raise Exception()
except:
pass
buffer.seek(0)
# Parsing this buffer should cause a parsing error due to unclosed
# root element
self.assertRaises(SyntaxError, lambda: ET.parse(buffer))
def test_comment(self):
buffer = BytesIO()
with elementflow.xml(buffer, u'root') as xml:
xml.comment(u'comment')
buffer.seek(0)
self.assertEqual(
buffer.getvalue(),
_bytes('<?xml version="1.0" encoding="utf-8"?><root><!--comment--></root>')
)
def test_comment_with_double_hyphen(self):
buffer = BytesIO()
with elementflow.xml(buffer, u'root') as xml:
xml.comment(u'--comm-->ent--')
buffer.seek(0)
self.assertEqual(
buffer.getvalue(),
_bytes('<?xml version="1.0" encoding="utf-8"?><root><!--comm>ent--></root>')
)
def test_namespaces(self):
buffer = BytesIO()
with elementflow.xml(buffer, 'root', namespaces={'': 'urn:n', 'n1': 'urn:n1'}) as xml:
xml.element('item')
with xml.container('n2:item', namespaces={'n2': 'urn:n2'}):
xml.element('item')
xml.element('n1:item')
buffer.seek(0)
tree = ET.parse(buffer)
root = tree.getroot()
self.assertEqual(root.tag, '{urn:n}root')
self.assertNotEqual(root.find('{urn:n}item'), None)
self.assertNotEqual(root.find('{urn:n2}item/{urn:n}item'), None)
self.assertNotEqual(root.find('{urn:n2}item/{urn:n1}item'), None)
def test_bad_namespace(self):
buffer = BytesIO()
def g():
with elementflow.xml(buffer, 'n1:root', namespaces={'n': 'urn:n'}) as xml:
pass
self.assertRaises(ValueError, g)
def g():
with elementflow.xml(buffer, 'n:root', attrs={'n1:k': 'v'}, namespaces={'n': 'urn:n'}) as xml:
pass
self.assertRaises(ValueError, g)
def test_map(self):
data = [(1, u'One'), (2, u'Two'), (3, u'Three')]
buffer = BytesIO()
with elementflow.xml(buffer, u'root') as xml:
xml.map(lambda item: (
'item',
{'key': str(item[0])},
item[1],
), data)
xml.map(lambda item: (item[1],), data)
buffer.seek(0)
tree = ET.parse(buffer)
buffer = BytesIO()
tree.write(buffer, encoding='utf-8')
self.assertEqual(
buffer.getvalue(),
_bytes(
'<root>'
'<item key="1">One</item>'
'<item key="2">Two</item>'
'<item key="3">Three</item>'
'<One /><Two /><Three />'
'</root>'
)
)
def test_indent(self):
buffer = BytesIO()
with elementflow.xml(buffer, u'root', indent = True) as xml:
with xml.container(u'a'):
xml.element(u'b', text = ''.join(['blah '] * 20))
xml.comment(u' '.join(['comment'] * 20))
buffer.seek(0)
self.assertEqual(
buffer.getvalue(),
_bytes(
"""<?xml version="1.0" encoding="utf-8"?>
<root>
<a>
<b>
blah blah blah blah blah blah blah blah blah blah blah
blah blah blah blah blah blah blah blah blah
</b>
<!--
comment comment comment comment comment comment comment
comment comment comment comment comment comment comment
comment comment comment comment comment comment
-->
</a>
</root>
"""))
def test_indent_nowrap(self):
buffer = BytesIO()
with elementflow.xml(buffer, u'root', indent = True, text_wrap = False) as xml:
with xml.container(u'a'):
xml.element(u'b', text = ''.join(['blah '] * 20))
xml.comment(u' '.join(['comment'] * 20))
buffer.seek(0)
self.assertEqual(
buffer.getvalue(),
_bytes(
"""<?xml version="1.0" encoding="utf-8"?>
<root>
<a>
<b>blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah </b>
<!--comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment comment-->
</a>
</root>
"""))
if __name__ == '__main__':
unittest.main()