-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtest_XmlElement.py
More file actions
340 lines (255 loc) · 12.5 KB
/
test_XmlElement.py
File metadata and controls
340 lines (255 loc) · 12.5 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
import os
import sys
import pytest
from utilities import get_runtime_data_folder
import yup
#==================================================================================================
this_file = yup.File(os.path.abspath(__file__))
@pytest.fixture
def xml_file():
yield yup.File(this_file.getSiblingFile("data").getChildFile("test.xml"))
@pytest.fixture
def broken_xml_file():
yield yup.File(this_file.getSiblingFile("data").getChildFile("test_broken.xml"))
#==================================================================================================
def test_construct_tag():
a = yup.XmlElement("root")
assert a.getTagName() == "root"
#==================================================================================================
def test_to_string_text_format():
a = yup.XmlElement("root")
assert a.toString() == """<?xml version="1.0" encoding="UTF-8"?>\r\n\r\n<root/>\r\n"""
t1 = yup.XmlElement.TextFormat()
t1.addDefaultHeader = False
assert a.toString(t1) == """<root/>\r\n"""
t2 = yup.XmlElement.TextFormat()
t2.newLineChars = None
assert a.toString(t2) == """<?xml version="1.0" encoding="UTF-8"?> <root/>"""
t3 = yup.XmlElement.TextFormat()
t3.customEncoding = "ISO-8859-1"
assert a.toString(t3) == """<?xml version="1.0" encoding="ISO-8859-1"?>\r\n\r\n<root/>\r\n"""
t4 = yup.XmlElement.TextFormat().singleLine()
t4.addDefaultHeader = False
assert a.toString(t4) == """<root/>"""
t5 = yup.XmlElement.TextFormat().singleLine()
t5.dtd = """<!DOCTYPE root SYSTEM "root.dtd">"""
assert a.toString(t5) == """<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE root SYSTEM "root.dtd"> <root/>"""
#==================================================================================================
def test_tag_name_with_namespace():
a = yup.XmlElement("yup:root")
assert a.hasTagName("yup:root") == True
assert a.getTagName() == "yup:root"
assert a.getNamespace() == "yup"
assert a.hasTagNameIgnoringNamespace("root") == True
assert a.getTagNameWithoutNamespace() == "root"
a.setTagName("other")
assert a.hasTagName("yup:root") == False
assert a.hasTagName("other") == True
assert a.getTagName() == "other"
assert a.getNamespace() == ""
assert a.hasTagNameIgnoringNamespace("root") == False
assert a.hasTagNameIgnoringNamespace("other") == True
assert a.getTagNameWithoutNamespace() == "other"
#==================================================================================================
@pytest.mark.parametrize("name_type", (yup.Identifier, str))
def test_attributes(name_type):
a = yup.XmlElement("root")
assert a.getNumAttributes() == 0
a.setAttribute(name_type("test1"), "123")
assert a.getNumAttributes() == 1
assert a.hasAttribute("test1") == True
assert a.hasAttribute("noexistent") == False
assert a.getAttributeName(0) == "test1"
assert a.getAttributeValue(0) == "123"
assert a.getStringAttribute("test1") == "123"
assert a.getStringAttribute("test_x") == ""
assert a.getStringAttribute("test_x", "xxx") == "xxx"
a.setAttribute(name_type("test2"), 1)
assert a.getNumAttributes() == 2
assert a.hasAttribute("test2") == True
assert a.hasAttribute("noexistent") == False
assert a.getAttributeName(1) == "test2"
assert a.getAttributeValue(1) == "1"
assert a.getIntAttribute("test2") == 1
assert a.getIntAttribute("test_x") == 0
assert a.getIntAttribute("test_x", 42) == 42
a.setAttribute(name_type("test3"), 1.111111)
assert a.getNumAttributes() == 3
assert a.hasAttribute("test3") == True
assert a.hasAttribute("noexistent") == False
assert a.getAttributeName(2) == "test3"
assert a.getAttributeValue(2) == "1.111111"
assert a.getDoubleAttribute("test3") == pytest.approx(1.111111)
assert a.getDoubleAttribute("test_x") == pytest.approx(0.0)
assert a.getDoubleAttribute("test_x", 9.999999) == pytest.approx(9.999999)
a.setAttribute(name_type("test4"), True)
assert a.getNumAttributes() == 4
assert a.hasAttribute("test4") == True
assert a.hasAttribute("noexistent") == False
assert a.getAttributeName(3) == "test4"
assert a.getAttributeValue(3) == "1"
assert a.getBoolAttribute("test4") == True
assert a.getBoolAttribute("test_x") == False
assert a.getBoolAttribute("test_x", True) == True
a.removeAttribute(name_type("nonexisting"))
assert a.getNumAttributes() == 4
a.removeAttribute(name_type("test4"))
assert a.getNumAttributes() == 3
a.removeAllAttributes()
assert a.getNumAttributes() == 0
#==================================================================================================
def test_compare_attributes():
a = yup.XmlElement("root")
a.setAttribute("test1", "123")
a.setAttribute("test2", "abcdef")
assert a.compareAttribute("test1", "123")
assert not a.compareAttribute("test1", "abcdef")
assert a.compareAttribute("test2", "abcdef")
assert not a.compareAttribute("test2", "ABCDEF")
assert a.compareAttribute("test2", "ABCDEF", True)
assert not a.compareAttribute("nonexistent", "abc", True)
assert not a.compareAttribute("nonexistent", "ABC", False)
#==================================================================================================
def test_child_elements():
a = yup.XmlElement("root")
a.addChildElement(yup.XmlElement("child"))
a.addChildElement(yup.XmlElement("child"))
assert a.getNumChildElements() == 2
#==================================================================================================
def test_text_element():
a = yup.XmlElement("root")
assert not a.isTextElement()
a.addChildElement(yup.XmlElement.createTextElement("abcdefghijklmnopqrstuvwxyz"))
child = a.getChildElement(0)
assert child.getTagName() == ""
assert child.isTextElement()
assert child.getText() == "abcdefghijklmnopqrstuvwxyz"
assert child.getAllSubText() == "abcdefghijklmnopqrstuvwxyz"
assert a.getAllSubText() == "abcdefghijklmnopqrstuvwxyz"
format = yup.XmlElement.TextFormat().singleLine().withoutHeader()
assert a.toString(format) == """<root>abcdefghijklmnopqrstuvwxyz</root>"""
child.setTagName("child")
assert a.toString(format) == """<root><child text="abcdefghijklmnopqrstuvwxyz"/></root>"""
assert a.getAllSubText() == ""
assert child.getAllSubText() == ""
#==================================================================================================
def test_child_elements_manipulation():
parent = yup.XmlElement("PARENT")
child1 = yup.XmlElement("CHILD1")
child2 = yup.XmlElement("CHILD2")
parent.addChildElement(child1)
parent.prependChildElement(child2)
assert parent.getNumChildElements() == 2
assert parent.getFirstChildElement().getTagName() == "CHILD2"
parent.removeChildElement(child1, True)
assert parent.getNumChildElements() == 1
parent.deleteAllChildElements()
assert parent.getNumChildElements() == 0
#==================================================================================================
def test_invalid_xml_name():
assert not yup.XmlElement.isValidXmlName("1InvalidName")
#==================================================================================================
def test_namespace_handling():
elem = yup.XmlElement("ns:ANIMAL")
assert elem.getTagName() == "ns:ANIMAL"
assert elem.getNamespace() == "ns"
assert elem.getTagNameWithoutNamespace() == "ANIMAL"
assert elem.hasTagName("ns:ANIMAL")
assert elem.hasTagNameIgnoringNamespace("ANIMAL")
#==================================================================================================
def test_deeply_nested_elements():
root = yup.XmlElement("root")
level1 = root.createNewChildElement("level1")
level2 = level1.createNewChildElement("level2")
level3 = level2.createNewChildElement("level3")
assert root.findParentElementOf(level3) is level2
assert root.getNumChildElements() == 1
assert root.getFirstChildElement().getFirstChildElement().getTagName() == "level2"
#==================================================================================================
def test_sorting_child_elements():
root = yup.XmlElement("root")
for name in ["banana", "apple", "cherry"]:
child = root.createNewChildElement("item")
child.setAttribute("name", name)
class Comparator(yup.XmlElement.Comparator):
def compareElements(self, a, b):
x = a.getStringAttribute("name")
y = b.getStringAttribute("name")
if x == y: return 0
elif x < y: return -1
else: return 1
root.sortChildElements(Comparator())
names = [child.getStringAttribute("name") for child in root.getChildIterator()]
assert names == ["apple", "banana", "cherry"]
#==================================================================================================
def test_sorting_child_elements_lambda():
root = yup.XmlElement("root")
for name in ["banana", "apple", "cherry"]:
child = root.createNewChildElement("item")
child.setAttribute("name", name)
def strncmp(a, b):
if a == b: return 0
elif a < b: return -1
else: return 1
root.sortChildElements(lambda x, y: strncmp(x.getStringAttribute("name"), y.getStringAttribute("name")))
names = [child.getStringAttribute("name") for child in root.getChildIterator()]
assert names == ["apple", "banana", "cherry"]
#==================================================================================================
def test_complex_xml_structure_to_string():
root = yup.XmlElement("menu")
item = root.createNewChildElement("item")
item.setAttribute("name", "coffee")
item.setAttribute("price", "$1")
item.addTextElement("Hot and invigorating")
xml_string = root.toString()
assert "<menu>" in xml_string
assert "<item name=\"coffee\" price=\"$1\">" in xml_string
assert "Hot and invigorating" in xml_string
assert "</item>" in xml_string
assert "</menu>" in xml_string
#==================================================================================================
def test_attribute_value_comparisons():
elem = yup.XmlElement("item")
elem.setAttribute("available", "true")
assert elem.compareAttribute("available", "true")
assert not elem.compareAttribute("available", "false")
assert elem.getBoolAttribute("available")
#==================================================================================================
def test_handling_invalid_characters_in_text_elements():
elem = yup.XmlElement("text")
elem.addTextElement("Invalid character: \u0000")
assert elem.getChildElement(0).getAllSubText() == "Invalid character: "
#==================================================================================================
def test_write_to_file():
elem = yup.XmlElement("note")
elem.addTextElement("This is a note")
temp_file = get_runtime_data_folder().getChildFile("test_write_to_file.xml")
assert elem.writeTo(temp_file)
with open(temp_file.getFullPathName(), "r") as file:
content = file.read()
assert "note" in content
assert "This is a note" in content
#==================================================================================================
def test_write_to_stream():
elem = yup.XmlElement("note")
elem.addTextElement("This is a note")
temp_file = get_runtime_data_folder().getChildFile("test_write_to_stream.xml")
elem.writeTo(temp_file.createOutputStream())
with open(temp_file.getFullPathName(), "r") as file:
content = file.read()
assert "note" in content
assert "This is a note" in content
#==================================================================================================
#@pytest.mark.skipif(sys.platform == "win32", reason="On windows this randomly fails for some reason, investigate")
@pytest.mark.skip(reason="This randomly fails for some reason, investigate")
def test_read_from_file():
temp_file = get_runtime_data_folder().getChildFile("test_read_from_file.xml")
with open(temp_file.getFullPathName(), "w") as file:
file.write("<note><to>User</to><message>Hello, World!</message></note>")
root = yup.XmlDocument.parse(temp_file)
assert root.getTagName() == "note"
assert root.getNumChildElements() == 2
assert root.getChildElement(0) is not None
assert root.getChildElement(0).getAllSubText() == "User"
assert root.getChildElement(1) is not None
assert root.getChildElement(1).getAllSubText() == "Hello, World!"