annotate emeraldtree/tests/test_ElementTree.py @ 30:96e02876b1a2

Testcases for ElementTree - Add testcases
author Bastian Blank <bblank@thinkmo.de>
date Tue, 08 Jul 2008 14:47:43 +0200
parents 4dc29d2eac0e
children 7b5ed8600569
rev   line source
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
1 import py.test
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 3
diff changeset
2 from emeraldtree.ElementTree import *
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
4 def serialize(elem, **options):
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
5 from cStringIO import StringIO
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
6 file = StringIO()
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
7 tree = ElementTree(elem)
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
8 tree.write(file, **options)
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
9 return file.getvalue()
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
10
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
11 def test_Element():
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
12 elem = Element('a')
24
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
13
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 3
diff changeset
14 assert isinstance(elem, Node)
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
15 assert elem.tag == 'a'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
16
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
17 def test_Element__len__():
23
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
18 elem = Element('a', children = range(10))
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
19
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
20 assert len(elem) == 10
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
21
24
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
22 def test_Element__nonzero__():
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
23 elem = Element('a')
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
24
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
25 assert elem
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
26 assert len(elem) == 0
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
27
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
28 def test_Element___getitem__():
23
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
29 elem = Element('a', children = [Element('b'), Element('c')])
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
30
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
31 assert elem[0].tag == 'b'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
32 assert elem[1].tag == 'c'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
33 assert len(elem[:1]) == 1
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
34 assert len(elem[:2]) == 2
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
35 assert len(elem[:3]) == 2
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
36 assert len(elem[1:3]) == 1
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
37 assert len(elem[2:3]) == 0
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
38 assert elem[:2][0].tag == 'b'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
39 assert elem[:2][1].tag == 'c'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
40
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
41 def test_Element___setitem__():
23
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
42 elem = Element('a', children = [Element('b1'), Element('b2')])
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
43
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
44 elem[0] = Element('c')
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
45 assert elem[0].tag == 'c'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
46 assert elem[1].tag == 'b2'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
47
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
48 elem[1] = Element('d')
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
49 assert elem[0].tag == 'c'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
50 assert elem[1].tag == 'd'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
51
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
52 elem[0:0] = [Element('e')]
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
53 assert elem[0].tag == 'e'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
54 assert elem[1].tag == 'c'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
55 assert elem[2].tag == 'd'
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
56
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
57 def test_Element___delitem__():
23
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
58 elem = Element('a', children = [Element('b1'), Element('b2')])
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
59
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
60 del elem[0]
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
61 assert len(elem) == 1
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
62 assert elem[0].tag == 'b2'
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
63
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
64 def test_Element_append():
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
65 elem = Element('a')
24
2015d11467bc Add testcase.
Bastian Blank <bblank@thinkmo.de>
parents: 23
diff changeset
66
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
67 elem.append(Element('b'))
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
68 assert len(elem) == 1
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
69 assert elem[0].tag == 'b'
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
70
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
71 elem.append(Element('c'))
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
72 assert len(elem) == 2
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
73 assert elem[1].tag == 'c'
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
74
5
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
75 elem.append('d')
18
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
76 assert len(elem) == 3
42e1c75e8bcb Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 16
diff changeset
77 assert elem[2] == 'd'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
78
30
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
79 def test_Element_extend():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
80 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
81
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
82 def test_Element_insert():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
83 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
84
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
85 def test_Element_remove():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
86 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
87
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
88 def test_Element_find():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
89 elem_1 = Element('b1')
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
90 elem_2 = Element('b2')
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
91 elem = Element('a', children=[elem_1, elem_2])
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
92
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
93 result = elem.find('b1')
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
94 assert result is elem_1
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
95
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
96 result = elem.find('b2')
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
97 assert result is elem_2
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
98
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
99 def test_Element_findtext():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
100 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
101
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
102 def test_Element_findall():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
103 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
104
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
105 def test_Element_clear():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
106 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
107
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
108 def test_Element_get():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
109 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
110
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
111 def test_Element_set():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
112 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
113
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
114 def test_Element_keys():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
115 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
116
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
117 def test_Element_items():
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
118 pass
96e02876b1a2 Testcases for ElementTree - Add testcases
Bastian Blank <bblank@thinkmo.de>
parents: 27
diff changeset
119
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
120 def test_Element_iter():
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
121 elem = Element('a')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
122 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
123 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
124
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
125 elem.append('b')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
126 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
127 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
128
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
129 elem.append(Element('c'))
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
130 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
131 assert len(l) == 2
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
132
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
133 elem.append('d')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
134 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
135 assert len(l) == 2
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
136
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
137 def test_Element_itertext():
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
138 elem = Element('a')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
139 l = list(elem.itertext())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
140 assert len(l) == 0
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
141
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
142 elem.append('b')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
143 l = list(elem.itertext())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
144 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
145
10
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
146 def test_Comment():
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
147 elem = Comment('a')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
148 assert serialize(elem) == '<!--a-->'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
149 assert isinstance(elem, Node)
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
150
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
151 def test_ProcessingInstruction():
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
152 elem = ProcessingInstruction('a')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
153 assert serialize(elem) == '<?a?>'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
154 assert isinstance(elem, ProcessingInstruction)
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
155
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
156 elem = ProcessingInstruction('a', 'b')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
157 assert serialize(elem) == '<?a b?>'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
158
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
159 def test_QName___init__():
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
160 qname = QName('a')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
161 assert qname.uri is None
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
162 assert qname.name == 'a'
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
163 assert isinstance(qname.name, unicode)
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
164 assert str(qname) == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
165 assert qname.text == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
166
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
167 qname = QName('{b}a')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
168 assert qname.uri == 'b'
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
169 assert isinstance(qname.uri, unicode)
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
170 assert qname.name == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
171 assert str(qname) == '{b}a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
172 assert qname.text == '{b}a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
173
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
174 qname = QName('a', 'b')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
175 assert qname.uri == 'b'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
176 assert qname.name == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
177 assert str(qname) == '{b}a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
178 assert qname.text == '{b}a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
179
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
180 py.test.raises(ValueError, QName, '{ba')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
181 py.test.raises(ValueError, QName, '{b}a', 'c')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
182
27
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
183 def test_QName___cmp__():
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
184 qname1 = QName('a')
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
185 qname2 = QName('a')
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
186
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
187 assert cmp(qname1, qname2) == 0
4dc29d2eac0e Force QName to be unicode.
Bastian Blank <bblank@thinkmo.de>
parents: 26
diff changeset
188
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
189 def test_XMLParser_simple1():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
190 elem = XML('<a />')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
191 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
192 assert len(elem) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
193
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
194 def test_XMLParser_simple2():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
195 elem = XML('<a><b /></a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
196 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
197 assert len(elem) == 1
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
198 assert elem[0].tag == 'b'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
199 assert len(elem[0]) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
200
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
201 def test_XMLParser_text1():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
202 elem = XML('<a>b</a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
203 assert elem.tag == 'a'
23
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
204 assert isinstance(elem, Element)
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
205 assert len(elem) == 1
23
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
206 assert elem[0] == 'b'
4c1912ff58a3 Support initialization with children.
Bastian Blank <bblank@thinkmo.de>
parents: 18
diff changeset
207 assert isinstance(elem[0], unicode)
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
208
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
209 def test_XMLParser_text2():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
210 elem = XML('<a>b<c>d</c>d</a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
211 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
212 assert len(elem) == 3
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
213 assert elem[0] == 'b'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
214 assert elem[1].tag == 'c'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
215 assert elem[2] == 'd'
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
216
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
217 def test_XMLParser_namespace():
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
218 elem = XML('<a:b xmlns:a="c"/>')
25
47ae505e63ea Parse tags and attribute names always in QName.
Bastian Blank <bblank@thinkmo.de>
parents: 24
diff changeset
219 assert isinstance(elem.tag, QName)
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
220 assert elem.tag == QName('b', 'c')
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
221 assert serialize(elem) == '<ns0:b xmlns:ns0="c" />'
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
222
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
223 elem = XML('<a:b xmlns:a="c" d="e"/>')
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
224 assert elem.tag == QName('b', 'c')
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
225 assert elem.attrib == {'d': 'e'}
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
226 assert serialize(elem) == '<ns0:b d="e" xmlns:ns0="c" />'
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
227
26
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
228 elem = XML('<a:b xmlns:a="c" a:d="e"/>')
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
229 assert elem.tag == QName('b', 'c')
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
230 assert elem.attrib == {QName('d', 'c'): 'e'}
eeb0eb69c1da Fix namespace uri in XML parser.
Bastian Blank <bblank@thinkmo.de>
parents: 25
diff changeset
231 assert serialize(elem) == '<ns0:b ns0:d="e" xmlns:ns0="c" />'
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
232