view emeraldtree/tests/test_html.py @ 100:9d39d4bc2deb

ported to python 2.7 / >= 3.3 using six the port itself was half automated (using python-modernize) and half manual fixes and cleanups.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 19 May 2013 20:37:50 +0200
parents 0121a7ec18b2
children
line wrap: on
line source
import py.test

from .. import html, tree
import six
from six import StringIO

def serialize(elem, method):
    file = StringIO()
    elem.write(file.write, method=method)
    return file.getvalue()

def test_read_simple1():
    elem = html.HTML('<a></a>')
    assert elem.tag.name == 'a'
    assert len(elem) == 0

def test_read_simple2():
    elem = html.HTML('<a><b /></a>')
    assert elem.tag.name == 'a'
    assert len(elem) == 1
    assert elem[0].tag.name == 'b'
    assert len(elem[0]) == 0

def test_read_text1():
    elem = html.HTML('<a>b</a>')
    assert elem.tag.name == 'a'
    assert isinstance(elem, tree.Element)
    assert len(elem) == 1
    assert elem[0] == 'b'
    assert isinstance(elem[0], six.text_type)

def test_read_text2():
    elem = html.HTML('<a>b<c>d</c>d</a>')
    assert elem.tag.name == 'a'
    assert len(elem) == 3
    assert elem[0] == 'b'
    assert elem[1].tag.name == 'c'
    assert elem[2] == 'd'

def test_read_ignoreend():
    elem = html.HTML('<br>')
    assert elem.tag.name == 'br'
    assert len(elem) == 0

    elem = html.HTML('<br></br>')
    assert elem.tag.name == 'br'
    assert len(elem) == 0

def test_write():
    elem = html.HTML('<html><br><p></html>')
    h = serialize(elem, 'html')
    p = serialize(elem, 'polyglot')
    x = serialize(elem, 'xml')
    assert '<br><p>' in h
    assert '<br /><p></p>' in p
    assert '<br /><p />' in x