annotate emeraldtree/tests/test_ElementTree.py @ 16:533331338691

Use parsed values of QName in namespace lookup. * emeraldtree/ElementTree.py - Use parsed QName in namespace lookup. - Remove support for QNames as values. * emeraldtree/tests/test_ElementTree.py: Add testcases.
author Bastian Blank <bblank@thinkmo.de>
date Sat, 31 May 2008 18:21:41 +0200
parents 60f59340b897
children 42e1c75e8bcb
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')
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
13 assert serialize(elem) == '<a />'
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)
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
15
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
16 def test_Element_append():
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
17 elem = Element('a')
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
18 elem.append(Element('b'))
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
19 assert serialize(elem) == '<a><b /></a>'
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
20
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
21 elem.append(Element('c'))
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
22 assert serialize(elem) == '<a><b /><c /></a>'
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
23
5
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
24 elem.append('d')
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
25 assert serialize(elem) == '<a><b /><c />d</a>'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
26
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
27 def test_Element_iter():
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
28 elem = Element('a')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
29 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
30 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
31
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
32 elem.append('b')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
33 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
34 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
35
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
36 elem.append(Element('c'))
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
37 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
38 assert len(l) == 2
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
39
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
40 elem.append('d')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
41 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
42 assert len(l) == 2
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
43
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
44 def test_Element_itertext():
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
45 elem = Element('a')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
46 l = list(elem.itertext())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
47 assert len(l) == 0
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
48
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
49 elem.append('b')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
50 l = list(elem.itertext())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
51 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
52
10
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
53 def test_Comment():
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
54 elem = Comment('a')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
55 assert serialize(elem) == '<!--a-->'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
56 assert isinstance(elem, Node)
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
57
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
58 def test_ProcessingInstruction():
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
59 elem = ProcessingInstruction('a')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
60 assert serialize(elem) == '<?a?>'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
61 assert isinstance(elem, ProcessingInstruction)
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
62
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
63 elem = ProcessingInstruction('a', 'b')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
64 assert serialize(elem) == '<?a b?>'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
65
15
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
66 def test_QName():
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
67 qname = QName('a')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
68 assert qname.uri is None
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
69 assert qname.name == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
70 assert str(qname) == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
71 assert qname.text == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
72
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
73 qname = QName('{b}a')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
74 assert qname.uri == 'b'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
75 assert qname.name == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
76 assert str(qname) == '{b}a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
77 assert qname.text == '{b}a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
78
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
79 qname = QName('a', 'b')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
80 assert qname.uri == 'b'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
81 assert qname.name == 'a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
82 assert str(qname) == '{b}a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
83 assert qname.text == '{b}a'
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
84
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
85 py.test.raises(ValueError, QName, '{ba')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
86 py.test.raises(ValueError, QName, '{b}a', 'c')
60f59340b897 Remove argument ordering weirdness of QName.
Bastian Blank <bblank@thinkmo.de>
parents: 10
diff changeset
87
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
88 def test_XMLParser_simple1():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
89 elem = XML('<a />')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
90 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
91 assert len(elem) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
92
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
93 def test_XMLParser_simple2():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
94 elem = XML('<a><b /></a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
95 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
96 assert len(elem) == 1
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
97 assert elem[0].tag == 'b'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
98 assert len(elem[0]) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
99
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
100 def test_XMLParser_text1():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
101 elem = XML('<a>b</a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
102 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
103 assert len(elem) == 1
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
104
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
105 def test_XMLParser_text2():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
106 elem = XML('<a>b<c>d</c>d</a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
107 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
108 assert len(elem) == 3
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
109 assert elem[0] == 'b'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
110 assert elem[1].tag == 'c'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
111 assert elem[2] == 'd'
16
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
112
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
113 def test_XMLParser_namespace():
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
114 elem = XML('<a:a xmlns:a="a"/>')
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
115 assert elem.tag == QName('a', 'a')
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
116 assert serialize(elem) == '<ns0:a xmlns:ns0="a" />'
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
117
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
118 elem = XML('<a:a xmlns:a="a" a="a"/>')
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
119 assert elem.tag == QName('a', 'a')
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
120 assert elem.attrib == {'a': 'a'}
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
121 assert serialize(elem) == '<ns0:a a="a" xmlns:ns0="a" />'
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
122
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
123 elem = XML('<a:a xmlns:a="a" a:a="a"/>')
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
124 assert elem.tag == QName('a', 'a')
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
125 assert elem.attrib == {QName('a', 'a'): 'a'}
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
126 assert serialize(elem) == '<ns0:a ns0:a="a" xmlns:ns0="a" />'
533331338691 Use parsed values of QName in namespace lookup.
Bastian Blank <bblank@thinkmo.de>
parents: 15
diff changeset
127