-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml_test.py
More file actions
431 lines (327 loc) · 10.4 KB
/
Copy pathxml_test.py
File metadata and controls
431 lines (327 loc) · 10.4 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
###########################################################
#
# Copyright (c) 2005, Southpaw Technology
# All Rights Reserved
#
# PROPRIETARY INFORMATION. This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
__all__ = ["XmlTest"]
import tacticenv
import unittest, pickle
Xml = None
class XmlTest(unittest.TestCase):
def test_all(my):
global Xml
print "Testing: 4Suite"
my.mode = "4Suite"
from pyasm.common.xml_wrapper import Xml as classXml
Xml = classXml
my._test_all()
print "Testing: lxml"
my.mode = "lxml"
from pyasm.common.lxml_wrapper import Xml as classXml
Xml = classXml
my._test_all()
def _test_all(my):
#my._test_read_string()
my._test_attribute()
my._test_create_doc()
my._test_xpath()
my._test_node_values()
my._test_comment()
#my._test_colon()
my._test_to_string()
my._test_color()
def _test_read_string(my):
xml = Xml()
xml_string = '''
<snapshot>
<a>pig</a>
<a>cow</a>
<a>horse</a>
<b>child</b>
<c name="horse"/>
</snapshot>'''
xml.read_string(xml_string)
expected = '''<?xml version='1.0' encoding='UTF-8'?>
<snapshot>
<a>pig</a>
<a>cow</a>
<a>horse</a>
<b>child</b>
<c name='horse'/>
</snapshot>
'''
print "[%s]" % xml.to_string()
print "[%s]" % expected
my.assertEquals(expected, xml.to_string())
def _test_create_doc(my):
xml = Xml()
# create a new document
doc = xml.create_doc("element")
my.assertEquals(True, doc != None)
# get the root element
root = xml.get_root_node()
my.assertEquals(True, doc != None)
# create a new element with attributes
display_node = xml.create_element("display")
xml.set_attribute(display_node, "widget", "expression")
xml.append_child(root, display_node)
# get the node name
name = xml.get_node_name(display_node)
my.assertEquals("display", name)
# test get attribute
value = xml.get_attribute(display_node, "widget")
my.assertEquals("expression", value)
# test delete attribute
xml.set_attribute(display_node, "garbage", "!!!!")
value = xml.del_attribute(display_node, "garbage")
my.assertEquals("!!!!", value)
xml.del_attribute(display_node, "garbage2")
# create a new element with text
expr_node = xml.create_element("expression")
xml.set_node_value(expr_node, "@GET(.completion)")
xml.append_child(display_node, expr_node)
value = xml.get_node_value(expr_node)
my.assertEquals("@GET(.completion)", value)
# create a new element with text
expr_node = xml.create_text_element("mode", "check")
xml.append_child(display_node, expr_node)
# get all of the children values as a dict
values = xml.get_node_values_of_children(display_node)
expected = {'expression': '@GET(.completion)', 'mode': 'check'}
my.assertEquals(expected, values)
# FIXME: add CDATA element
#cdata = xml.create_data_element("cbjs_action", '''a > b''')
#display_node.append(cdata)
#print xml.to_string()
def _test_attribute(my):
xml = Xml()
xml.create_doc()
element = xml.create_element("test")
xml.set_attribute(element, "wow1", "1")
xml.set_attribute(element, "wow2", "2")
attrs = xml.get_attributes(element)
keys = attrs.keys()
keys.sort()
my.assertEquals('wow1', keys[0])
my.assertEquals('wow2', keys[1])
def _test_xpath(my):
xml_string = """
<snapshot>
<a>pig</a>
<a>cow</a>
<a>horse</a>
<b>child</b>
<c name="horse"/>
<d name="cow"/>
<d name="pig"/>
<d name="dog" xx='1'/>
<not_test/>
</snapshot>
"""
# build the xml object and find the values using xpath
xml = Xml()
xml.read_string(xml_string)
# test get_node
nodes = xml.get_nodes("snapshot/a")
my.assertEquals(3, len(nodes) )
node = xml.get_node("snapshot/b")
my.assertEquals(1, node != None)
# test not with no elements
# WARNING: this is different in lxml
xpath = "snapshot/not_test"
node = xml.get_node(xpath)
my.assertEquals(True, node != None)
if node == None:
my.fail()
# Do not use!!!
#if not node:
# ERROR
xpath = "snapshot/a"
values = xml.get_values(xpath)
my.assertEqual("pig",values[0])
my.assertEqual("cow",values[1])
my.assertEqual("horse",values[2])
# test single value
value = xml.get_value("snapshot/b")
my.assertEqual("child",value)
# test getting an attribute
value = xml.get_value("snapshot/c/@name")
my.assertEqual("horse",value)
# try "or" with nodes
nodes = xml.get_nodes("snapshot/b | snapshot/c")
my.assertEqual(len(nodes), 2)
# try "or" with attributes
nodes = xml.get_nodes("snapshot/d[@name='cow' or @name='pig']")
my.assertEqual(len(nodes), 2)
# try "and" with attributes
nodes = xml.get_nodes("snapshot/d[@name='dog' and @xx='1']")
my.assertEqual(len(nodes), 1)
#xml.dump()
# test none returned
node = xml.get_node("snapshot/xxxxxx")
my.assertEqual(None,node)
def _test_node_values(my):
xml_string = '''
<element name='cow'>
<display class='Whatever'>
<a>1</a>
<b>2</b>
<c>3</c>
</display>
</element>'''
xml = Xml()
xml.read_string(xml_string)
# get all of the children values as a dict
display_node = xml.get_node("element/display")
values = xml.get_node_values_of_children(display_node)
expected = {
'a': '1',
'b': '2',
'c': '3',
}
my.assertEquals(expected, values)
xml_string = '''
<element name='cow'>
<display class='Whatever'>
<a>1</a>
<b>
<x>123</x>
<y>234</y>
<z>345</z>
</b>
<c>3</c>
</display>
</element>'''
xml = Xml()
xml.read_string(xml_string)
# get all of the children values as a dict
display_node = xml.get_node("element/display")
values = xml.get_recursive_node_values(display_node)
expected = {
'a': '1',
'b': {
'x': '123',
'y': '234',
'z': '345',
},
'c': '3',
}
my.assertEquals(expected, values)
# get all of the children values as a dict
node = xml.get_node("element")
values = xml.get_recursive_node_values(node)
expected = {
'display': {
'a': '1',
'b': {
'x': '123',
'y': '234',
'z': '345',
},
'c': '3',
}
}
my.assertEquals(expected, values)
def _test_comment(my):
xml_string = '''
<element>
<!-- This is a comment -->
<display class='SelectWdg'/>
</element>
'''
xml = Xml()
xml.read_string(xml_string)
root = xml.get_root_node()
children = xml.get_children(root)
my.assertEquals( len(children), 1)
def _test_colon(my):
xml_string = '''<?xml version='1.0' encoding='UTF-8'?>
<config>
<search:whatever>
<element class='whatever'/>
</search:whatever>
</config>
'''
xml = Xml()
xml.read_string(xml_string)
node = xml.get_node("config/search:whatever")
def _test_to_string(my):
xml_string = '''<?xml version='1.0' encoding='UTF-8'?>
<config>
<snapshot>
<a>pig</a>
<a>cow</a>
<a>horse</a>
<b>child</b>
<c name="horse"/>
</snapshot>
</config>'''
xml = Xml()
xml.read_string(xml_string)
node = xml.get_node("config/snapshot")
node_string = xml.get_node_xml(node)
if my.mode == '4Suite':
expected = u'''<snapshot>
<a>pig</a>
<a>cow</a>
<a>horse</a>
<b>child</b>
<c name='horse'/>
</snapshot>'''
else:
expected = u'''<snapshot>
<a>pig</a>
<a>cow</a>
<a>horse</a>
<b>child</b>
<c name="horse"/>
</snapshot>'''
my.assertEquals(expected, node_string.strip() )
node = xml.get_node("config")
nodes = xml.xpath(node, "snapshot")
child = nodes[0]
my.assertEquals( "snapshot", Xml.get_node_name(child) )
def _test_color(my):
xml_string = '''<?xml version='1.0' encoding='UTF-8'?>
<config>
<color>
<element name='test'>
<colors>
<value name='AAA'>#FF0000</value>
<value name='BBB'>#FF8000</value>
<value name='CCC'>#FFBD7A</value>
<value name='DDD'>#FFFF00</value>
<value name='EEE'>#FFFFFF</value>
<value name='FFF'>#2EB800</value>
<value name='GGG'>#0033FF</value>
</colors>
<text_colors>
<value name='GGG'>#FFFFFF</value>
<value name='FFF'>#FFFFFF</value>
<value name='EEE'>#000000</value>
<value name='DDD'>#000000</value>
<value name='CCC'>#000000</value>
<value name='BBB'>#FFFFFF</value>
<value name='AAA'>#FFFFFF</value>
</text_colors>
</element>
</color>
</config>
'''
xml = Xml()
xml.read_string(xml_string)
name = 'test'
xpath = "config/color/element[@name='%s']/colors" % name
text_xpath = "config/color/element[@name='%s']/text_colors" % name
bg_color_node = xml.get_node(xpath)
bg_color_map = xml.get_node_values_of_children(bg_color_node)
my.assertEquals("#FF0000", bg_color_map.get("AAA"))
my.assertEquals("#FF8000", bg_color_map.get("BBB"))
if __name__ == '__main__':
unittest.main()