annotate emeraldtree/tests/test_ElementTree.py @ 10:fc4b72233da0

Add testcases. emeraldtree/tests/test_ElementTree.py: Add testcases for Comment and ProcessingInstruction.
author Bastian Blank <bblank@thinkmo.de>
date Sat, 31 May 2008 16:20:53 +0200
parents e1d0cd336bf3
children 60f59340b897
rev   line source
4
705fc192cd9d Add new Node class and make it a base of Element.
Bastian Blank <bblank@thinkmo.de>
parents: 3
diff changeset
1 from emeraldtree.ElementTree import *
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
2
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
3 def serialize(elem, **options):
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
4 from cStringIO import StringIO
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
5 file = StringIO()
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
6 tree = ElementTree(elem)
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
7 tree.write(file, **options)
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
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
10 def test_Element___init__():
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
11 elem = Element('a')
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
12 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
13 assert isinstance(elem, Node)
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
14
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
15 def test_Element_append():
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
16 elem = Element('a')
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
17 elem.append(Element('b'))
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
18 assert serialize(elem) == '<a><b /></a>'
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
19
3
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
20 elem.append(Element('c'))
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
21 assert serialize(elem) == '<a><b /><c /></a>'
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
22
5
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
23 elem.append('d')
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
24 assert serialize(elem) == '<a><b /><c />d</a>'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
25
8
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
26 def test_Element_iter():
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
27 elem = Element('a')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
28 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
29 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
30
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
31 elem.append('b')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
32 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
33 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
34
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
35 elem.append(Element('c'))
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
36 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
37 assert len(l) == 2
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
38
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
39 elem.append('d')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
40 l = list(elem.iter())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
41 assert len(l) == 2
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
42
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
43 def test_Element_itertext():
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
44 elem = Element('a')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
45 l = list(elem.itertext())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
46 assert len(l) == 0
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
47
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
48 elem.append('b')
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
49 l = list(elem.itertext())
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
50 assert len(l) == 1
e1d0cd336bf3 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 6
diff changeset
51
10
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
52 def test_Comment():
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
53 elem = Comment('a')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
54 assert serialize(elem) == '<!--a-->'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
55 assert isinstance(elem, Node)
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
56
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
57 def test_ProcessingInstruction():
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
58 elem = ProcessingInstruction('a')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
59 assert serialize(elem) == '<?a?>'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
60 assert isinstance(elem, ProcessingInstruction)
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
61
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
62 elem = ProcessingInstruction('a', 'b')
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
63 assert serialize(elem) == '<?a b?>'
fc4b72233da0 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 8
diff changeset
64
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
65 def test_XMLParser_simple1():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
66 elem = XML('<a />')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
67 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
68 assert len(elem) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
69
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
70 def test_XMLParser_simple2():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
71 elem = XML('<a><b /></a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
72 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
73 assert len(elem) == 1
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
74 assert elem[0].tag == 'b'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
75 assert len(elem[0]) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
76
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
77 def test_XMLParser_text1():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
78 elem = XML('<a>b</a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
79 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
80 assert len(elem) == 1
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
81
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
82 def test_XMLParser_text2():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
83 elem = XML('<a>b<c>d</c>d</a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
84 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
85 assert len(elem) == 3
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
86 assert elem[0] == 'b'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
87 assert elem[1].tag == 'c'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
88 assert elem[2] == 'd'