comparison emeraldtree/tests/test_html.py @ 73:641657361783

Tests for HTML support - Fix invocation, add tests for ouput
author Bastian Blank <bblank@thinkmo.de>
date Fri, 04 Jun 2010 13:02:19 +0200
parents d7e235461c97
children 223789ea05b2
comparison
equal deleted inserted replaced
72:0ce683d31c42 73:641657361783
1 import py.test 1 import py.test
2 2
3 from .. import html, tree 3 from .. import html, tree
4 4
5 def serialize(elem, **options): 5 def serialize(elem):
6 from cStringIO import StringIO 6 from StringIO import StringIO
7 file = StringIO() 7 file = StringIO()
8 tree = ElementTree(elem) 8 elem.write(file.write, method='html')
9 tree.write(file, **options)
10 return file.getvalue() 9 return file.getvalue()
11 10
12 def test_HTMLParser_simple1(): 11 def test_read_simple1():
13 elem = html.HTML('<a></a>') 12 elem = html.HTML('<a></a>')
14 assert elem.tag.name == 'a' 13 assert elem.tag.name == 'a'
15 assert len(elem) == 0 14 assert len(elem) == 0
16 15
17 def test_HTMLParser_simple2(): 16 def test_read_simple2():
18 elem = html.HTML('<a><b /></a>') 17 elem = html.HTML('<a><b /></a>')
19 assert elem.tag.name == 'a' 18 assert elem.tag.name == 'a'
20 assert len(elem) == 1 19 assert len(elem) == 1
21 assert elem[0].tag.name == 'b' 20 assert elem[0].tag.name == 'b'
22 assert len(elem[0]) == 0 21 assert len(elem[0]) == 0
23 22
24 def test_HTMLParser_text1(): 23 def test_read_text1():
25 elem = html.HTML('<a>b</a>') 24 elem = html.HTML('<a>b</a>')
26 assert elem.tag.name == 'a' 25 assert elem.tag.name == 'a'
27 assert isinstance(elem, tree.Element) 26 assert isinstance(elem, tree.Element)
28 assert len(elem) == 1 27 assert len(elem) == 1
29 assert elem[0] == 'b' 28 assert elem[0] == 'b'
30 assert isinstance(elem[0], unicode) 29 assert isinstance(elem[0], unicode)
31 30
32 def test_HTMLParser_text2(): 31 def test_read_text2():
33 elem = html.HTML('<a>b<c>d</c>d</a>') 32 elem = html.HTML('<a>b<c>d</c>d</a>')
34 assert elem.tag.name == 'a' 33 assert elem.tag.name == 'a'
35 assert len(elem) == 3 34 assert len(elem) == 3
36 assert elem[0] == 'b' 35 assert elem[0] == 'b'
37 assert elem[1].tag.name == 'c' 36 assert elem[1].tag.name == 'c'
38 assert elem[2] == 'd' 37 assert elem[2] == 'd'
39 38
40 def test_HTMLParser_ignoreend(): 39 def test_read_ignoreend():
41 elem = html.HTML('<br>') 40 elem = html.HTML('<br>')
42 assert elem.tag.name == 'br' 41 assert elem.tag.name == 'br'
43 assert len(elem) == 0 42 assert len(elem) == 0
44 43
45 elem = html.HTML('<br></br>') 44 elem = html.HTML('<br></br>')
46 assert elem.tag.name == 'br' 45 assert elem.tag.name == 'br'
47 assert len(elem) == 0 46 assert len(elem) == 0
48 47
49 48 def test_write():
49 elem = html.HTML(u'<br>')
50 assert serialize(elem) == u'<br>'