annotate emeraldtree/tests/test_tree.py @ 57:c1feee84d5d8

add tests for findtext()
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 18 May 2010 13:20:51 +0200
parents 125ce968352d
children 9d0db78957e0
rev   line source
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
1 import py.test
53
d071598a93ef Tree support - Change module name
Bastian Blank <bblank@thinkmo.de>
parents: 49
diff changeset
2 from emeraldtree.tree import *
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
3
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
4 def serialize(elem, namespaces={}):
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
5 from StringIO import StringIO
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
6 file = StringIO()
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
7 XMLWriter(namespaces=namespaces).write(file.write, elem)
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
8 return file.getvalue()
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
9
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
10 def test_Element():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
11 elem = Element(u'a')
24
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
12
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 3
diff changeset
13 assert isinstance(elem, Node)
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
14 assert elem.tag == u'a'
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
15
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
16 def test_Element__len__():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
17 elem = Element(u'a', children=(u'1', u'2', u'3', u'4', u'5'))
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
18
49
78ef2b77ca4a Tests for ElementTree - Fix tests for default namespace, fix types
Bastian Blank <bblank@thinkmo.de>
parents: 40
diff changeset
19 assert len(elem) == 5
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
20
24
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
21 def test_Element__nonzero__():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
22 elem = Element(u'a')
24
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
23
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
24 assert elem
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
25 assert len(elem) == 0
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
26
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
27 def test_Element___getitem__():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
28 elem = Element(u'a', children=(Element(u'b'), Element(u'c')))
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
29
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
30 assert elem[0].tag == u'b'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
31 assert elem[1].tag == u'c'
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
32 assert len(elem[:1]) == 1
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
33 assert len(elem[:2]) == 2
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
34 assert len(elem[:3]) == 2
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
35 assert len(elem[1:3]) == 1
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
36 assert len(elem[2:3]) == 0
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
37 assert elem[:2][0].tag == u'b'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
38 assert elem[:2][1].tag == u'c'
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
39
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
40 def test_Element___setitem__():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
41 elem = Element(u'a', children=(Element(u'b1'), Element(u'b2')))
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
42
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
43 elem[0] = Element(u'c')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
44 assert elem[0].tag == u'c'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
45 assert elem[1].tag == u'b2'
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
46
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
47 elem[1] = Element(u'd')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
48 assert elem[0].tag == u'c'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
49 assert elem[1].tag == u'd'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
50
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
51 elem[0:0] = [Element(u'e')]
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
52 assert elem[0].tag == u'e'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
53 assert elem[1].tag == u'c'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
54 assert elem[2].tag == u'd'
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
55
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
56 def test_Element___delitem__():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
57 elem = Element(u'a', children=(Element(u'b1'), Element(u'b2')))
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
58
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
59 del elem[0]
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
60 assert len(elem) == 1
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
61 assert elem[0].tag == u'b2'
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
62
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
63 def test_Element_append():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
64 elem = Element(u'a')
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
65
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
66 elem.append(Element(u'b'))
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
67 assert len(elem) == 1
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
68 assert elem[0].tag == u'b'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
69
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
70 elem.append(Element(u'c'))
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
71 assert len(elem) == 2
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
72 assert elem[1].tag == u'c'
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
73
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
74 elem.append(u'd')
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
75 assert len(elem) == 3
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
76 assert elem[2] == u'd'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
77
30
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
78 def test_Element_extend():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
79 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
80
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
81 def test_Element_insert():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
82 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
83
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
84 def test_Element_remove():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
85 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
86
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
87 def test_Element_find():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
88 child_1 = Element(u'b1')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
89 child_2 = Element(u'b2')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
90 elem = Element(u'a', children=(child_1, child_2))
30
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
91
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
92 result = elem.find(u'b1')
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
93 assert result is child_1
30
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
94
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
95 result = elem.find(u'b2')
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
96 assert result is child_2
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
97
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
98 child_1 = Element(QName(u'b1', u'url1'))
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
99 child_2 = Element(QName(u'{url2}b2'))
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
100 elem = Element(u'a', children=(child_1, child_2))
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
101
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
102 result = elem.find(u'{url1}b1')
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
103 assert result is child_1
30
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
104
57
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
105 def test_Element_findtext_default():
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
106 elem = Element(u'a')
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
107 default_text = u'defaulttext'
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
108 result = elem.findtext(u'b', default=default_text)
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
109 assert result is default_text
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
110
30
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
111 def test_Element_findtext():
57
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
112 child_text = u"text"
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
113 child = Element(u'b', children=(child_text, ))
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
114 elem = Element(u'a', children=(child, ))
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
115 result = elem.findtext(u'b')
c1feee84d5d8 add tests for findtext()
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 55
diff changeset
116 assert result is child_text
30
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
117
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
118 def test_Element_findall():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
119 child_1 = Element(u'b1')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
120 child_2 = Element(u'b2')
49
78ef2b77ca4a Tests for ElementTree - Fix tests for default namespace, fix types
Bastian Blank <bblank@thinkmo.de>
parents: 40
diff changeset
121 child_3 = u"text"
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
122 elem = Element(u'a', children=(child_1, child_2, child_3))
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
123
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
124 result = list(elem.findall(u'b1'))
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
125 assert len(result) == 1
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
126 assert result[0] is child_1
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
127
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
128 result = list(elem.findall(u'b2'))
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
129 assert len(result) == 1
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
130 assert result[0] is child_2
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
131
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
132 result = list(elem.findall(u'*'))
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
133 assert len(result) == 3
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
134 assert result[0] is child_1
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
135 assert result[1] is child_2
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
136 assert result[2] is child_3
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
137
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
138 child_1 = Element(QName(u'b1', u'url1'))
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
139 child_2 = Element(QName(u'{url2}b2'))
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
140 elem = Element(u'a', children=(child_1, child_2))
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
141
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
142 result = list(elem.findall(u'{url1}b1'))
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
143 assert len(result) == 1
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
144 assert result[0] is child_1
30
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
145
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
146 def test_Element_clear():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
147 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
148
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
149 def test_Element_get():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
150 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
151
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
152 def test_Element_set():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
153 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
154
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
155 def test_Element_keys():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
156 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
157
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
158 def test_Element_items():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
159 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
160
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
161 def test_Element_iter():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
162 elem = Element(u'a')
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
163 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
164 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
165
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
166 elem.append(u'b')
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
167 l = list(elem.iter())
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 32
diff changeset
168 assert len(l) == 2
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
169
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
170 elem.append(Element(u'c'))
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
171 l = list(elem.iter())
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 32
diff changeset
172 assert len(l) == 3
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
173
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
174 elem.append(u'd')
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
175 l = list(elem.iter())
38
6e38bcef6030 ElementTree - Make iterators return all childs
Bastian Blank <bblank@thinkmo.de>
parents: 32
diff changeset
176 assert len(l) == 4
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
177
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
178 def test_Element_itertext():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
179 elem = Element(u'a')
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
180 l = list(elem.itertext())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
181 assert len(l) == 0
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
182
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
183 elem.append(u'b')
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
184 l = list(elem.itertext())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
185 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
186
10
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
187 def test_Comment():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
188 elem = Comment(u'a')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
189 assert serialize(elem) == u'<!--a-->'
10
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
190 assert isinstance(elem, Node)
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
191
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
192 def test_ProcessingInstruction():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
193 elem = ProcessingInstruction(u'a')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
194 assert serialize(elem) == u'<?a?>'
10
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
195 assert isinstance(elem, ProcessingInstruction)
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
196
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
197 elem = ProcessingInstruction(u'a', u'b')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
198 assert serialize(elem) == u'<?a b?>'
10
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
199
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
200 def test_QName___init__():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
201 qname = QName(u'a')
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
202 assert qname.uri is None
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
203 assert qname.name == u'a'
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
204 assert isinstance(qname.name, unicode)
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
205 assert str(qname) == u'a'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
206 assert qname.text == u'a'
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
207
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
208 qname = QName(u'{b}a')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
209 assert qname.uri == u'b'
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
210 assert isinstance(qname.uri, unicode)
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
211 assert qname.name == u'a'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
212 assert str(qname) == u'{b}a'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
213 assert qname.text == u'{b}a'
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
214
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
215 qname = QName(u'a', u'b')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
216 assert qname.uri == u'b'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
217 assert qname.name == u'a'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
218 assert str(qname) == u'{b}a'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
219 assert qname.text == u'{b}a'
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
220
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
221 py.test.raises(ValueError, QName, u'{bau')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
222 py.test.raises(ValueError, QName, u'{b}a', u'c')
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
223
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
224 def test_QName___cmp__():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
225 qname1 = QName(u'a')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
226 qname2 = QName(u'a')
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
227
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
228 assert qname1 == qname2
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
229 assert qname1 == u'a'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
230 assert u'a' == qname1
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
231
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
232 qname1 = QName(u'a', u'b')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
233 qname2 = QName(u'{b}a')
32
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
234
7b5ed8600569 Testcases for ElementTree - Add testcases for find and findall
Bastian Blank <bblank@thinkmo.de>
parents: 30
diff changeset
235 assert qname1 == qname2
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
236 assert qname1 == u'{b}a'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
237 assert u'{b}a' == qname1
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
238
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
239 def test_XMLParser_simple1():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
240 elem = XML(u'<a />')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
241 assert elem.tag == u'a'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
242 assert len(elem) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
243
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
244 def test_XMLParser_simple2():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
245 elem = XML(u'<a><b /></a>')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
246 assert elem.tag == u'a'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
247 assert len(elem) == 1
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
248 assert elem[0].tag == u'b'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
249 assert len(elem[0]) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
250
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
251 def test_XMLParser_text1():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
252 elem = XML(u'<a>b</a>')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
253 assert elem.tag == u'a'
23
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
254 assert isinstance(elem, Element)
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
255 assert len(elem) == 1
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
256 assert elem[0] == u'b'
23
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
257 assert isinstance(elem[0], unicode)
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
258
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
259 def test_XMLParser_text2():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
260 elem = XML(u'<a>b<c>d</c>d</a>')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
261 assert elem.tag == u'a'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
262 assert len(elem) == 3
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
263 assert elem[0] == u'b'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
264 assert elem[1].tag == u'c'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
265 assert elem[2] == u'd'
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
266
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
267 def test_XMLParser_namespace_1():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
268 elem = XML(u'<b xmlns="c" d="e"/>')
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 24
diff changeset
269 assert isinstance(elem.tag, QName)
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
270 assert elem.tag == QName(u'b', u'c')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
271 assert elem.attrib == {QName(u'd', None): u'e'}
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
272 assert serialize(elem) == u'<ns0:b d="e" xmlns:ns0="c" />'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
273 assert serialize(elem, namespaces={u'c': u''}) == u'<b d="e" xmlns="c" />'
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
274
54
6d2aaba7ac4d Tree - Move serialization code into classes
Bastian Blank <bblank@thinkmo.de>
parents: 53
diff changeset
275 def test_XMLParser_namespace_2():
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
276 elem = XML(u'<a:b xmlns:a="c" d="e" a:f="g"/>')
40
a8382b8cded5 Testcases for ElementTree - Check all variants of namespace attributes
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
277 assert isinstance(elem.tag, QName)
55
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
278 assert elem.tag == QName(u'b', u'c')
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
279 assert elem.attrib == {u'd': u'e', QName(u'f', u'c'): u'g'}
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
280 assert serialize(elem) == u'<ns0:b d="e" ns0:f="g" xmlns:ns0="c" />'
125ce968352d Tests for tree - Use unicode, style cleanup
Bastian Blank <bblank@thinkmo.de>
parents: 54
diff changeset
281 assert serialize(elem, namespaces={u'c': u''}) == u'<b d="e" f="g" xmlns="c" />'
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
282
40
a8382b8cded5 Testcases for ElementTree - Check all variants of namespace attributes
Bastian Blank <bblank@thinkmo.de>
parents: 38
diff changeset
283