forked from matee911/lxml-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
141 lines (111 loc) · 4.15 KB
/
test.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
# -*- coding: utf-8 -*-
import unittest
from lxmlwrapper import E, SE, etree
to_s = etree.tostring
class TestSingleElement(unittest.TestCase):
def test_element(self):
e = E('root')
s = '<root/>'
self.assertEqual(to_s(e), s)
e = E('root', foo='bar')
s = '<root foo="bar"/>'
self.assertEqual(to_s(e), s)
e = E('root', bar=0)
s = '<root bar="0"/>'
self.assertEqual(to_s(e), s)
e = E('root', baz=None)
s = '<root baz=""/>'
self.assertEqual(to_s(e), s)
class TestAddElement(unittest.TestCase):
def test_add_element(self):
e = E('root').add()
s = '<root/>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child1'))
s = '<root><child1/></root>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child1'), E('child2'))
s = '<root><child1/><child2/></root>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child1'), E('child2'), E('child3'))
s = '<root><child1/><child2/><child3/></root>'
self.assertEqual(to_s(e), s)
def test_add_none(self):
e = E('root').add(None)
s = '<root/>'
self.assertEqual(to_s(e), s)
def test_add_text(self):
e = E('root').add('text')
s = '<root>text</root>'
self.assertEqual(to_s(e), s)
e = E('root').add('text1', 'text2')
s = '<root>text1text2</root>'
self.assertEqual(to_s(e), s)
def test_add_elem_text(self):
e = E('root').add('text', E('child'))
s = '<root>text<child/></root>'
self.assertEqual(to_s(e), s)
e = E('root').add('text1', 'text2', E('child'))
s = '<root>text1text2<child/></root>'
self.assertEqual(to_s(e), s)
def test_add_elem_tail(self):
e = E('root').add(E('child'), 'tail')
s = '<root><child/>tail</root>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child'), 'tail1', 'tail2')
s = '<root><child/>tail1tail2</root>'
self.assertEqual(to_s(e), s)
e = E('root').add(E('child1'), 'tail1', 'tail2', E('child2'))
s = '<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 = '<root>text<child/>tail</root>'
self.assertEqual(to_s(e), s)
e = E('root').add('text1', 'text2', E('child'), 'tail1', 'tail2')
s = '<root>text1text2<child/>tail1tail2</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 = '<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 = '<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 = '<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 = '<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 = """<root>
<child/>
</root>
"""
self.assertEqual(to_s(e, pretty_print=True), s)
if __name__ == '__main__':
unittest.main()