-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest.py
More file actions
187 lines (146 loc) · 5.45 KB
/
test.py
File metadata and controls
187 lines (146 loc) · 5.45 KB
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
178
179
180
181
182
183
184
185
186
# -*- coding: utf-8 -*-
# http://lxml.de/FAQ.html#why-does-lxml-sometimes-return-str-values-for-text-in-python-2
from __future__ import unicode_literals
import unittest
from lxmlwrapper import E, SE, etree
to_s = etree.tostring
class TestSingleElement(unittest.TestCase):
def test_element(self):
e = E('root')
s = b'<root/>'
self.assertEqual(to_s(e), s)
e = E('root', foo='bar')
s = b'<root foo="bar"/>'
self.assertEqual(to_s(e), s)
e = E('root', bar=0)
s = b'<root bar="0"/>'
self.assertEqual(to_s(e), s)
e = E('root', baz=None)
s = b'<root baz=""/>'
self.assertEqual(to_s(e), s)
e = E('root', baz='Zażółć gęślą jaźń')
s = b'<root baz="Zażółć gęślą jaźń"/>'
self.assertEqual(to_s(e), s)
class TestAddElement(unittest.TestCase):
def test_add_element(self):
e = E('root').add()
s = b'<root/>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child1'))
s = b'<root><child1/></root>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child1'), E('child2'))
s = b'<root><child1/><child2/></root>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child1'), E('child2'), E('child3'))
s = b'<root><child1/><child2/><child3/></root>'
self.assertEqual(to_s(e), s)
def test_add_none(self):
e = E('root').add(None)
s = b'<root/>'
self.assertEqual(to_s(e), s)
def test_add_text(self):
e = E('root').add('text')
s = b'<root>text</root>'
self.assertEqual(to_s(e), s)
e = E('root').add('text1', 'text2')
s = b'<root>text1text2</root>'
self.assertEqual(to_s(e), s)
def test_add_elem_text(self):
e = E('root').add('text', E('child'))
s = b'<root>text<child/></root>'
self.assertEqual(to_s(e), s)
e = E('root').add('text1', 'text2', E('child'))
s = b'<root>text1text2<child/></root>'
self.assertEqual(to_s(e), s)
def test_add_elem_tail(self):
e = E('root').add(E('child'), 'tail')
s = b'<root><child/>tail</root>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child'), 'tail1', 'tail2')
s = b'<root><child/>tail1tail2</root>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child1'), 'tail1', 'tail2', E('child2'))
s = b'<root><child1/>tail1tail2<child2/></root>'
self.assertEqual(to_s(e), s)
def test_add_elem_text_tail(self):
e = E('root').add('text', E('child'), 'tail')
s = b'<root>text<child/>tail</root>'
self.assertEqual(to_s(e), s)
e = E('root').add('text1', 'text2', E('child'), 'tail1', 'tail2')
s = b'<root>text1text2<child/>tail1tail2</root>'
self.assertEqual(to_s(e), s)
def test_add_empty_string(self):
e = E('root').add('t1' ,'', 't3')
s = b'<root>t1t3</root>'
self.assertEqual(to_s(e), s)
def test_add_number(self):
e = E('root').add(17)
s = b'<root>17</root>'
self.assertEqual(to_s(e), s)
class TestAddIf(unittest.TestCase):
def test_true_condition(self):
e = E('root').add_if(True, E('SE'))
s = b'<root><SE/></root>'
self.assertEqual(to_s(e), s)
e = E('root').add_if(True, 'foo')
s = b'<root>foo</root>'
self.assertEqual(to_s(e), s)
def test_false_condition(self):
e = E('root').add_if(False, E('SE'))
s = b'<root/>'
self.assertEqual(to_s(e), s)
e = E('root').add_if(False, 'foo')
s = b'<root/>'
self.assertEqual(to_s(e), s)
class TestAddFor(unittest.TestCase):
def test_iterable(self):
e = E('root').add_for(range(3), lambda i: E('foo%s' % i))
s = b'<root><foo0/><foo1/><foo2/></root>'
self.assertEqual(to_s(e), s)
e = E('root').add_for([], lambda i: E('foo%s' % i))
s = b'<root/>'
self.assertEqual(to_s(e), s)
class TestSE(unittest.TestCase):
"""Testing subelement"""
def test_add(self):
e = E('root')
se = SE(e,'child')
s = b'<root><child/></root>'
self.assertEqual(to_s(e), s)
def test_add_with_text(self):
e = E('root')
se = SE(e,'child').add('text')
s = b'<root><child>text</child></root>'
self.assertEqual(to_s(e), s)
def test_add_with_elems(self):
e = E('root')
se = SE(e,'child').add(E('child2'))
s = b'<root><child><child2/></child></root>'
self.assertEqual(to_s(e), s)
class TestRandomComplexExamples(unittest.TestCase):
def test_complex(self):
e = E('root', atr=100).add(
'text1',
E('child', atr="atr").add(
E('superchild', atr=None).add('sctext1'),
'tail1',
'tail2'
),
'tail',
E('child', atr="").add(
'text'
)
)
s = b'<root atr="100">text1<child atr="atr"><superchild atr="">sctext1</superchild>tail1tail2</child>tail<child atr="">text</child></root>'
self.assertEqual(to_s(e), s)
class TestPrettyPrint(unittest.TestCase):
def test_pretty_print(self):
e = E('root').add(E('child'))
s = b"""<root>
<child/>
</root>
"""
self.assertEqual(to_s(e, pretty_print=True), s)
if __name__ == '__main__':
unittest.main()