annotate emeraldtree/tests/test_ElementTree.py @ 6:ced434b6bf72

Add testcases. emeraldtree/tests/test_ElementTree.py: Add testcases for XMLParser.
author Bastian Blank <bblank@thinkmo.de>
date Sat, 31 May 2008 15:46:29 +0200
parents 9b72cb3ef3c9
children e1d0cd336bf3
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>'
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
19 elem.append(Element('c'))
40db0b033d47 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
20 assert serialize(elem) == '<a><b /><c /></a>'
5
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
21 elem.append('d')
9b72cb3ef3c9 Support strings a children.
Bastian Blank <bblank@thinkmo.de>
parents: 4
diff changeset
22 assert serialize(elem) == '<a><b /><c />d</a>'
6
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
23
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
24 def test_XMLParser_simple1():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
25 elem = XML('<a />')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
26 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
27 assert len(elem) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
28
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
29 def test_XMLParser_simple2():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
30 elem = XML('<a><b /></a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
31 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
32 assert len(elem) == 1
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
33 assert elem[0].tag == 'b'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
34 assert len(elem[0]) == 0
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
35
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
36 def test_XMLParser_text1():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
37 elem = XML('<a>b</a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
38 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
39 assert len(elem) == 1
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
40
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
41 def test_XMLParser_text2():
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
42 elem = XML('<a>b<c>d</c>d</a>')
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
43 assert elem.tag == 'a'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
44 assert len(elem) == 3
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
45 assert elem[0] == 'b'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
46 assert elem[1].tag == 'c'
ced434b6bf72 Add testcases.
Bastian Blank <bblank@thinkmo.de>
parents: 5
diff changeset
47 assert elem[2] == 'd'