annotate emeraldtree/tests/test_HTMLTreeBuilder.py @ 51:847897e5fab8

HTMLTreeBuilder - Make namespace aware, add testcases
author Bastian Blank <bblank@thinkmo.de>
date Sun, 21 Sep 2008 11:01:19 +0200
parents
children
rev   line source
51
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
1 import py.test
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
2 from emeraldtree.ElementTree import *
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
3 from emeraldtree.HTMLTreeBuilder import *
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
4
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
5 def serialize(elem, **options):
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
6 from cStringIO import StringIO
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
7 file = StringIO()
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
8 tree = ElementTree(elem)
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
9 tree.write(file, **options)
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
10 return file.getvalue()
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
11
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
12 def test_HTMLParser_simple1():
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
13 elem = HTML('<a></a>')
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
14 assert elem.tag.name == 'a'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
15 assert len(elem) == 0
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
16
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
17 def test_XMLParser_simple2():
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
18 elem = HTML('<a><b /></a>')
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
19 assert elem.tag.name == 'a'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
20 assert len(elem) == 1
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
21 assert elem[0].tag.name == 'b'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
22 assert len(elem[0]) == 0
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
23
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
24 def test_XMLParser_text1():
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
25 elem = HTML('<a>b</a>')
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
26 assert elem.tag.name == 'a'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
27 assert isinstance(elem, Element)
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
28 assert len(elem) == 1
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
29 assert elem[0] == 'b'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
30 assert isinstance(elem[0], unicode)
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
31
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
32 def test_XMLParser_text2():
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
33 elem = HTML('<a>b<c>d</c>d</a>')
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
34 assert elem.tag.name == 'a'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
35 assert len(elem) == 3
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
36 assert elem[0] == 'b'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
37 assert elem[1].tag.name == 'c'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
38 assert elem[2] == 'd'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
39
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
40 def test_HTMLParser_ignoreend():
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
41 elem = HTML('<br>')
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
42 assert elem.tag.name == 'br'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
43 assert len(elem) == 0
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
44
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
45 elem = HTML('<br></br>')
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
46 assert elem.tag.name == 'br'
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
47 assert len(elem) == 0
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
48
847897e5fab8 HTMLTreeBuilder - Make namespace aware, add testcases
Bastian Blank <bblank@thinkmo.de>
parents:
diff changeset
49