forked from cs327e-spring-2014/xml-tests
/
joshen-TestXML.py
196 lines (156 loc) · 6.5 KB
/
joshen-TestXML.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
# ----------
# TestXML.py
# ----------
"""
To test the program:
% python TestCollatz.py >& TestCollatz.out
% chmod ugo+x TestCollatz.py
% TestCollatz.py >& TestCollatz.out
"""
# -------
# imports
# -------
import io
import unittest
from XML import xml_buildlist, xml_compare, xml_comparehelper, xml_searchpattern, xml_read, xml_solve, xml_print
from xml.etree.ElementTree import Element, fromstring
# -----------
# TestCollatz
# -----------
class TestCollatz (unittest.TestCase) :
# --------
# xml_read
# --------
def test_read (self) :
r = io.StringIO("<Team><Cooly></Cooly></Team>")
b = xml_read(r)
self.assertTrue("<xml><Team><Cooly></Cooly></Team></xml>")
def test_read_2 (self) :
r = io.StringIO("<Dragon></Dragon><Colly></Colly>")
b = xml_read(r)
self.assertTrue("<xml><Dragon><Cooly></Dragon></Colly></xml>")
def test_read_3(self):
r = io.StringIO("<Cooly><Team></Team></Cooly>")
b=xml_read(r)
self.assertTrue("<Cooly><Team></Team></Team></xml>")
# ---------
# xml_solve
# ---------
def test_solve_1(self):
r = io.StringIO('<THU><Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team><JiaJia><Team><Ahyangyi></Ahyangyi><Dragon></Dragon><Cooly><Amber></Amber></Cooly></Team></JiaJia></THU>\n<Team><Cooly></Cooly></Team>')
w = io.StringIO()
xml_solve(r, w)
self.assertTrue(w.getvalue() == "2\n2\n7\n")
def test_solve_2(self):
r = io.StringIO("<THU><Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team><JiaJia><Team><Ahyangyi></Ahyangyi><Dragon></Dragon><Cooly><Amber></Amber></Cooly></Team></JiaJia></THU>\n<Team><Cooly></Cooly><Dragon></Dragon></Team>" )
w = io.StringIO()
xml_solve(r, w)
self.assertTrue(w.getvalue() == "1\n7\n")
def test_solve_3(self):
r = io.StringIO("<stuff>\n<what>\n<bees>\n<junk>\n</junk>\n</bees>\n</what>\n</stuff>\n<what><junk></junk></what>")
w = io.StringIO()
xml_solve(r,w)
self.assertTrue(w.getvalue() == "0\n")
def test_solve_4(self):
r = io.StringIO("<cool>\n</cool>\n<Cool></Cool>")
w = io.StringIO()
xml_solve(r,w)
self.assertTrue(w.getvalue() == "0\n")
def test_solve_5(self):
r = io.StringIO("<cool>\n</cool>\n<no></no>")
w = io.StringIO()
xml_solve(r,w)
self.assertTrue(w.getvalue() == "0\n")
def test_solve_6(self):
r = io.StringIO("<never>\n</never>\n<never></never>")
w = io.StringIO()
xml_solve(r,w)
self.assertTrue(w.getvalue() == "1\n1\n")
def test_solve7(self):
r = io.StringIO("<cool>\n<what>\n<travel>\n</travel>\n</what>\n</cool>\n<travel></travel>")
w = io.StringIO()
xml_solve(r,w)
self.assertTrue(w.getvalue() == "1\n3\n")
def test_solve8(self):
r = io.StringIO("<cool>\n<what>\n<travel>\n</travel>\n</what>\n</cool>\n<travel></travel>")
w = io.StringIO()
xml_solve(r,w)
self.assertTrue(w.getvalue() == "1\n3\n")
# -------------
# xml_buildlist
# -------------
def test_buildlist1(self):
x = fromstring("<xml><THU><Team><ACRush></ACRush><Jelly>" \
+ "</Jelly><Cooly></Cooly></Team><JiaJia>" \
+ "<Team><Ahyangyi></Ahyangyi><Dragon></Dragon>" \
+ "<Cooly><Amber></Amber></Cooly></Team></JiaJia>" \
+ "</THU><Team><Cooly></Cooly></Team></xml>")
L = [i for i in x[0].iter() if i.tag == 'Cooly']
B = xml_buildlist('Cooly', x)
for i in B:
self.assertTrue(i in L)
# -----------------
# xml_searchpattern
# -----------------
def test_searchPattern_1(self):
x = fromstring('<xml><THU><Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team><JiaJia><Team><Ahyangyi></Ahyangyi><Dragon></Dragon><Cooly><Amber></Amber></Cooly></Team></JiaJia></THU>\n<Team><Cooly></Cooly></Team></xml>')
v = xml_searchpattern(x)
self.assertTrue(v == [2,2,7])
def test_searchPattern_2(self):
x = fromstring('<xml><a><b><e></e><d></d></b></a><b><d></d></b></xml>')
v = xml_searchpattern(x)
self.assertTrue(v ==[1,2])
def test_searchPattern_3(self):
x = fromstring('<xml><red><green><blue></blue><yellow></yellow></green></red><green><yellow></yellow><blue></blue></green></xml>')
v = xml_searchpattern(x)
self.assertTrue(v ==[1,2])
def test_searchPattern_4(self):
x = fromstring('<xml><red><green><blue></blue><yellow></yellow></green></red><green><purple></purple></green></xml>')
v = xml_searchpattern(x)
self.assertTrue(v ==[0])
def test_searchPattern_5(self):
x = fromstring('<xml><cool>\n</cool>\n<no></no></xml>')
v = xml_searchpattern(x)
self.assertTrue(v ==[0])
def test_searchPattern_6(self):
x = fromstring('<xml>"<cool>\n</cool>\n<Cool></Cool>"</xml>')
v = xml_searchpattern(x)
self.assertTrue(v ==[0])
# -----------
# xml_compare
# -----------
def test_compare_1(self):
x = fromstring('<xml><THU><Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team><JiaJia><Team><Ahyangyi></Ahyangyi><Dragon></Dragon><Cooly><Amber></Amber></Cooly></Team></JiaJia></THU>\n<Team><Cooly></Cooly></Team></xml>')
a = x[0][0]
b = x[1]
v = xml_compare(a,b)
self.assertTrue(v == None)
# ------------------
# xml_comparehelpter
# ------------------
def test_compareHelper_1(self):
x = fromstring('<xml><THU><Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team><JiaJia><Team><Ahyangyi></Ahyangyi><Dragon></Dragon><Cooly><Amber></Amber></Cooly></Team></JiaJia></THU>\n<Team><Cooly></Cooly></Team></xml>')
a = [i for i in x[0][0]]
b = [j for j in x[0][1][0]]
v = xml_comparehelper(a,b)
self.assertTrue(v == False)
# ---------
# xml_print
# ---------
def test_print_1(self):
w = io.StringIO()
a = [5, 6, 7, 8]
xml_print(a, w)
self.assertTrue(w.getvalue() == "5\n6\n7\n8\n")
def test_print_2(self):
w = io.StringIO()
a = [3,5,75,4,3,5]
xml_print(a, w)
self.assertTrue(w.getvalue() == "3\n5\n75\n4\n3\n5\n")
# ----
# main
# ----
print("TestCollatz.py")
unittest.main()
print("Done.")