Source

EmeraldTree / emeraldtree / tests / test_tree.py

Full commit
  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
import py.test
from emeraldtree.tree import *

def serialize(elem, namespaces={}):
    from StringIO import StringIO
    file = StringIO()
    elem.write(file.write, namespaces=namespaces)
    return file.getvalue()

def test_Element():
    elem = Element(u'a')

    assert isinstance(elem, Node)
    assert elem.tag == u'a'

def test_Element__len__():
    elem = Element(u'a', children=(u'1', u'2', u'3', u'4', u'5'))

    assert len(elem) == 5

def test_Element__nonzero__():
    elem = Element(u'a')

    assert elem
    assert len(elem) == 0

def test_Element___getitem__():
    elem = Element(u'a', children=(Element(u'b'), Element(u'c')))

    assert elem[0].tag == u'b'
    assert elem[1].tag == u'c'
    assert len(elem[:1]) == 1
    assert len(elem[:2]) == 2
    assert len(elem[:3]) == 2
    assert len(elem[1:3]) == 1
    assert len(elem[2:3]) == 0
    assert elem[:2][0].tag == u'b'
    assert elem[:2][1].tag == u'c'

def test_Element___setitem__():
    elem = Element(u'a', children=(Element(u'b1'), Element(u'b2')))

    elem[0] = Element(u'c')
    assert elem[0].tag == u'c'
    assert elem[1].tag == u'b2'

    elem[1] = Element(u'd')
    assert elem[0].tag == u'c'
    assert elem[1].tag == u'd'

    elem[0:0] = [Element(u'e')]
    assert elem[0].tag == u'e'
    assert elem[1].tag == u'c'
    assert elem[2].tag == u'd'

def test_Element___delitem__():
    elem = Element(u'a', children=(Element(u'b1'), Element(u'b2')))

    del elem[0]
    assert len(elem) == 1
    assert elem[0].tag == u'b2'

def test_Element_append():
    elem = Element(u'a')

    elem.append(Element(u'b'))
    assert len(elem) == 1
    assert elem[0].tag == u'b'

    elem.append(Element(u'c'))
    assert len(elem) == 2
    assert elem[1].tag == u'c'

    elem.append(u'd')
    assert len(elem) == 3
    assert elem[2] == u'd'

def test_Element_extend():
    pass

def test_Element_insert():
    pass

def test_Element_remove():
    pass

def test_Element_find():
    child_1 = Element(u'b1')
    child_2 = Element(u'b2')
    elem = Element(u'a', children=(child_1, child_2))

    result = elem.find(u'b1')
    assert result is child_1

    result = elem.find(u'b2')
    assert result is child_2

    child_1 = Element(QName(u'b1', u'url1'))
    child_2 = Element(QName(u'{url2}b2'))
    elem = Element(u'a', children=(child_1, child_2))

    result = elem.find(u'{url1}b1')
    assert result is child_1

def test_Element_findall():
    child_1 = Element(u'b1')
    child_2 = Element(u'b2')
    child_3 = u"text"
    elem = Element(u'a', children=(child_1, child_2, child_3))

    result = list(elem.findall(u'b1'))
    assert len(result) == 1
    assert result[0] is child_1

    result = list(elem.findall(u'b2'))
    assert len(result) == 1
    assert result[0] is child_2

    result = list(elem.findall(u'*'))
    assert len(result) == 3
    assert result[0] is child_1
    assert result[1] is child_2
    assert result[2] is child_3

    child_1 = Element(QName(u'b1', u'url1'))
    child_2 = Element(QName(u'{url2}b2'))
    elem = Element(u'a', children=(child_1, child_2))

    result = list(elem.findall(u'{url1}b1'))
    assert len(result) == 1
    assert result[0] is child_1

def test_Element_findall_tag_multimatch():
    c1 = Element(u'c')
    c2 = Element(u'c')
    text = u"text"
    b1 = Element(u'b', children=(c1, text, c2))
    b2 = Element(u'b')
    a1 = Element(u'a', children=(b1, b2, ))

    result = list(b1.findall(u'c'))
    assert len(result) == 2
    assert result[0] is c1
    assert result[1] is c2

    result = list(a1.findall(u'c'))
    assert len(result) == 0 # no 'c' childs

    result = list(a1.findall(u'*/c'))
    assert len(result) == 2 # but 2 'c' grandchilds
    assert result[0] is c1
    assert result[1] is c2

def test_Element_findall_bracketed_tag():
    c1 = Element(u'c')
    c2 = Element(u'c')
    text = u"text"
    b1 = Element(u'b', children=(c1, text, c2))
    b2 = Element(u'b')
    a1 = Element(u'a', children=(b1, b2, ))

    result = list(b1.findall(u'[c]'))
    assert len(result) == 1
    assert result[0] is b1 # b1 has 'c' childs

    result = list(a1.findall(u'*/[c]'))
    assert len(result) == 1
    assert result[0] is b1 # b1 has 'c' childs

def test_Element_findall_dotdot():
    py.test.skip('broken')
    c1 = Element(u'c')
    c2 = Element(u'c')
    text = u"text"
    b1 = Element(u'b', children=(c1, text, c2))
    b2 = Element(u'b')
    a1 = Element(u'a', children=(b1, b2, ))
    
    result = list(c1.findall(u'../c'))
    assert len(result) == 2
    assert result[0] is c1
    assert result[1] is c2

def test_Element_findall_slashslash():
    py.test.skip('broken')
    c1 = Element(u'c')
    c2 = Element(u'c')
    text = u"text"
    b1 = Element(u'b', children=(c1, text, c2))
    b2 = Element(u'b')
    a1 = Element(u'a', children=(b1, b2, ))

    a1t = ElementTree(element=a1) # we need a tree to use //
    result = list(a1t.findall(u'//c'))
    assert len(result) == 2
    assert result[0] is c1
    assert result[1] is c2

def test_Element_findall_dotslashslash():
    py.test.skip('broken')
    c1 = Element(u'c')
    c2 = Element(u'c')
    text = u"text"
    b1 = Element(u'b', children=(c1, text, c2))
    b2 = Element(u'b')
    a1 = Element(u'a', children=(b1, b2, ))

    result = list(a1.findall(u'.//c'))
    assert len(result) == 2
    assert result[0] is c1
    assert result[1] is c2

def test_Element_findall_attribute():
    c1 = Element(u'c')
    c2 = Element(u'c', testattr='testvalue')
    text = u"text"
    b1 = Element(u'b', children=(c1, text, c2))
    b2 = Element(u'b')
    a1 = Element(u'a', children=(b1, b2, ))

    result = list(b1.findall(u"c[@testattr]"))
    # note: does not work without c, like b1.findall(u"[@testattr]") - should it?
    assert len(result) == 1
    assert result[0] is c2

    result = list(b1.findall(u"c[@testattr='testvalue']"))
    # note: does not work without c, like b1.findall(u"[@testattr='testvalue']") - should it?
    assert len(result) == 1
    assert result[0] is c2

    result = list(b1.findall(u"c[@testattr='othervalue']"))
    # note: does not work without c, like b1.findall(u"[@testattr='othervalue']") - should it?
    assert len(result) == 0

def test_Element_findall_position():
    py.test.skip('not supported')
    c1 = Element(u'c')
    c2 = Element(u'c')
    text = u"text"
    b1 = Element(u'b', children=(c1, text, c2))
    b2 = Element(u'b')
    a1 = Element(u'a', children=(b1, b2, ))

    result = list(b1.findall(u'c[1]')) # note: index is 1-based, [1] (not [0]) is first
    assert len(result) == 1
    assert result[0] is c1

    result = list(b1.findall(u'c[2]'))
    assert len(result) == 1
    assert result[0] is c2

def test_Element_findtext_default():
    elem = Element(u'a')
    default_text = u'defaulttext'
    result = elem.findtext(u'doesnotexist', default=default_text)
    assert result is default_text

def test_Element_findtext():
    child_text = u"text"
    child = Element(u'b', children=(child_text, ))
    elem = Element(u'a', children=(child, ))
    result = elem.findtext(u'b')
    assert result is child_text

def test_Element_clear():
    pass

def test_Element_get():
    pass

def test_Element_set():
    pass

def test_Element_keys():
    pass

def test_Element_items():
    pass

def test_Element_iter():
    elem = Element(u'a')
    l = list(elem.iter())
    assert len(l) == 1

    elem.append(u'b')
    l = list(elem.iter())
    assert len(l) == 2

    elem.append(Element(u'c'))
    l = list(elem.iter())
    assert len(l) == 3

    elem.append(u'd')
    l = list(elem.iter())
    assert len(l) == 4

def test_Element_itertext():
    elem = Element(u'a')
    l = list(elem.itertext())
    assert len(l) == 0

    elem.append(u'b')
    l = list(elem.itertext())
    assert len(l) == 1

def test_Element_iter_elements():
    elem = Element(u'a')
    assert len(list(elem.iter_elements())) == 0

    elem.append(Element(u'c'))
    assert len(list(elem.iter_elements())) == 1

    elem.append(u'b')
    assert len(list(elem.iter_elements())) == 1

def test_Comment():
    elem = Comment(u'a')
    assert serialize(elem) == u'<!--a-->'
    assert isinstance(elem, Node)

def test_ProcessingInstruction():
    elem = ProcessingInstruction(u'a')
    assert serialize(elem) == u'<?a?>'
    assert isinstance(elem, ProcessingInstruction)

    elem = ProcessingInstruction(u'a', u'b')
    assert serialize(elem) == u'<?a b?>'

def test_QName___init__():
    qname = QName(u'a')
    assert qname.uri is None
    assert qname.name == u'a'
    assert isinstance(qname.name, unicode)
    assert unicode(qname) == u'a'

    qname = QName(u'{b}a')
    assert qname.uri == u'b'
    assert isinstance(qname.uri, unicode)
    assert qname.name == u'a'
    assert unicode(qname) == u'{b}a'

    qname = QName(u'a', u'b')
    assert qname.uri == u'b'
    assert qname.name == u'a'
    assert unicode(qname) == u'{b}a'

    py.test.raises(ValueError, QName, u'{bau')
    py.test.raises(ValueError, QName, u'{b}a', u'c')

def test_QName___cmp__():
    qname1 = QName(u'a')
    qname2 = QName(u'a')

    assert qname1 == qname2
    assert qname1 == u'a'
    assert u'a' == qname1

    qname1 = QName(u'a', u'b')
    qname2 = QName(u'{b}a')

    assert qname1 == qname2
    assert qname1 == u'{b}a'
    assert u'{b}a' == qname1

def test_XMLParser_simple1():
    elem = XML(u'<a />')
    assert elem.tag == u'a'
    assert len(elem) == 0

def test_XMLParser_simple2():
    elem = XML(u'<a><b /></a>')
    assert elem.tag == u'a'
    assert len(elem) == 1
    assert elem[0].tag == u'b'
    assert len(elem[0]) == 0

def test_XMLParser_text1():
    elem = XML(u'<a>b</a>')
    assert elem.tag == u'a'
    assert isinstance(elem, Element)
    assert len(elem) == 1
    assert elem[0] == u'b'
    assert isinstance(elem[0], unicode)

def test_XMLParser_text2():
    elem = XML(u'<a>b<c>d</c>d</a>')
    assert elem.tag == u'a'
    assert len(elem) == 3
    assert elem[0] == u'b'
    assert elem[1].tag == u'c'
    assert elem[2] == u'd'

def test_XMLParser_namespace_1():
    elem = XML(u'<b xmlns="c" d="e"/>')
    assert isinstance(elem.tag, QName)
    assert elem.tag == QName(u'b', u'c')
    assert elem.attrib == {QName(u'd', None): u'e'}
    assert serialize(elem) == u'<ns0:b d="e" xmlns:ns0="c" />'
    assert serialize(elem, namespaces={u'c': u''}) == u'<b d="e" xmlns="c" />'

def test_XMLParser_namespace_2():
    elem = XML(u'<a:b xmlns:a="c" d="e" a:f="g"/>')
    assert isinstance(elem.tag, QName)
    assert elem.tag == QName(u'b', u'c')
    assert elem.attrib == {u'd': u'e', QName(u'f', u'c'): u'g'}
    assert serialize(elem) == u'<ns0:b d="e" ns0:f="g" xmlns:ns0="c" />'
    assert serialize(elem, namespaces={u'c': u''}) == u'<b d="e" f="g" xmlns="c" />'