Exemplo n.º 1
0
def test_parse_xmlstring4():
    """Parse xml tree with fragment flag"""
    x = xml.Renderer()
    roots = x.fromstring(xml_fragments_1, fragment=True, no_leading_text=True)
    assert roots[0].tostring() == b'<fragment1/>text'
    assert roots[0].tail == 'text'
    assert roots[1].tostring() == b'<fragment2/>'
Exemplo n.º 2
0
def test_replace6():
    """Replace root node"""
    x = xml.Renderer()

    node = x.node()
    node.replace('test')
    assert node.tostring() == b'<node/>'
Exemplo n.º 3
0
def test_global5():
    """Test findmeld() & replace()"""
    x = xml.Renderer()
    root = x.fromstring(xml_test2_in)

    root.findmeld('title').text = 'My document'
    root.findmeld('form1').set('action', './handler')

    data = (
        {
            'name': 'Girls',
            'description': 'Pretty'
        },
        {
            'name': 'Boys',
            'description': 'Ugly'
        },
    )

    children = []
    for elt in data:
        children.append(
            x.tr([x.td(elt['name']),
                  x.td(elt['description'])], {'class': 'bar'}))

    root.findmeld('tr').replace(children)

    assert root.findall('.//tr')[1].attrib['class'] == 'bar'
    assert [elt.text for elt in root.xpath('.//td')
            ] == ['Girls', 'Pretty', 'Boys', 'Ugly']
Exemplo n.º 4
0
def test_append_text1():
    """Append text to an empty node"""
    x = xml.Renderer()

    node = x.node('test')
    assert node.text == 'test'
    assert node.tostring() == b'<node>test</node>'
Exemplo n.º 5
0
def test_global7():
    """Test tostring() with option pipeline=True"""
    x = xml.Renderer()
    root = x.fromstring(xml_test2_in)

    root.findmeld('title').text = 'My document'
    root.findmeld('form1').set('action', './handler')

    data = (
        {
            'name': 'Girls',
            'description': 'Pretty'
        },
        {
            'name': 'Boys',
            'description': 'Ugly'
        },
    )

    children = []
    for elt in data:
        children.append(
            x.tr([x.td(elt['name']),
                  x.td(elt['description'])], {'class': 'bar'}))

    root.findmeld('tr').replace(children)

    root.tostring(xml_declaration=True, pretty_print=True, pipeline=True)

    assert root.findmeld('tr') is None
    assert root.findmeld('content_well') is not None
Exemplo n.º 6
0
def test_repeat4():
    x = xml.Renderer()

    with x.contacts:
        with x.contact.meld_id('contact'):
            x << x.name.meld_id('name')
            with x.address.meld_id('addr'):
                x << 'ici, rennes'

    for e, (name, addr) in x.root.repeat((('bill', 'seatle'), ('steve', 'cupertino')), 'contact'):
        e.findmeld('name').text = name
        e.findmeld('addr').text = addr

    result = b'''
    <contacts>
      <contact xmlns:ns0="http://www.plope.com/software/meld3" ns0:id="contact">
        <name ns0:id="name">bill</name>
        <address ns0:id="addr">seatle</address>
      </contact>
      <contact xmlns:ns0="http://www.plope.com/software/meld3" ns0:id="contact">
        <name ns0:id="name">steve</name>
        <address ns0:id="addr">cupertino</address>
      </contact>
    </contacts>'''

    result = b''.join(line.lstrip() for line in result.splitlines())
    assert x.root.tostring() == result
Exemplo n.º 7
0
def test_root3():
    """Two elements"""
    x = xml.Renderer()
    x << x.node()
    x << x.node()

    assert isinstance(x.root, list)
Exemplo n.º 8
0
def test_global4():
    """Test findmeld() with children affectation"""

    x = xml.Renderer()
    root = x.fromstring(xml_test2_in)

    root.findmeld('title').text = 'My document'
    root.findmeld('form1').set('action', './handler')
    data = (
        {
            'name': 'Girls',
            'description': 'Pretty'
        },
        {
            'name': 'Boys',
            'description': 'Ugly'
        },
    )

    iterator = root.findmeld('tr').repeat(data)
    for element, item in iterator:
        td1 = element.findmeld('td1')
        td1.text = item['name']
        element.findmeld('td2').text = item['description']

    assert [
        elt.text
        for elt in root.xpath('.//x:td',
                              namespaces={'x': 'http://www.w3.org/1999/xhtml'})
    ] == ['Girls', 'Pretty', 'Boys', 'Ugly']
    assert root[0][1].text == 'My document'
    assert root.xpath('.//x:form',
                      namespaces={'x': 'http://www.w3.org/1999/xhtml'
                                  })[0].attrib['action'] == './handler'
Exemplo n.º 9
0
def test_append_text3():
    """Append text to node with node child"""
    x = xml.Renderer()

    node = x.node(x.child())
    node('test')
    assert node.getchildren()[0].tail == 'test'
    assert node.tostring() == b'<node><child/>test</node>'
Exemplo n.º 10
0
def test_append_text2():
    """Append text to node with text child"""
    x = xml.Renderer()

    node = x.node('test1')
    node('test2')
    assert node.text == 'test1test2'
    assert node.tostring() == b'<node>test1test2</node>'
Exemplo n.º 11
0
def test_parse6():
    h = html.Renderer()
    root = h.fromstring('<a>text</a>')
    assert type(root) == html.Tag

    x = xml.Renderer()
    root = x.fromstring('<a>text</a>')
    assert type(root) == xml.Tag
Exemplo n.º 12
0
def test_escape():
    e = Renderer()
    x = xml.Renderer()

    root = x.div(
        e.esi('<p><esi:vars>Hello, $(HTTP_COOKIE{name})!</esi:vars></p>'))
    assert root.tostring(
    ) == b'<div><!--esi <p><esi:vars>Hello, $(HTTP_COOKIE{name})!</esi:vars></p>--></div>'
Exemplo n.º 13
0
def test_append4():
    """Append text to node with text & node children"""
    x = xml.Renderer()

    node = x.node(['test1', x.child()])
    node('test2')
    assert node.text == 'test1'
    assert node.getchildren()[0].tail == 'test2'
    assert node.tostring() == b'<node>test1<child/>test2</node>'
Exemplo n.º 14
0
def test_parse_xml1():
    """Good encoding"""
    try:
        x = xml.Renderer()
        x.fromfile(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml'))
    except UnicodeDecodeError:
        assert False
    else:
        assert True
Exemplo n.º 15
0
def test_root1():
    x = xml.Renderer()

    foo = x.foo
    assert foo.root is foo

    bar = x.bar
    foo = x.foo(bar)
    assert foo.root is foo
    assert bar.root is foo
Exemplo n.º 16
0
def test_replace4():
    """Replace simple node by text"""
    x = xml.Renderer()

    child1 = x.child1()
    node = x.node(child1)
    assert node.getchildren()[0] == child1
    child1.replace('test')
    assert node.text == 'test'
    assert node.tostring() == b'<node>test</node>'
Exemplo n.º 17
0
def test_repeat1():
    """Repeat with 2 simple text, use childname argument"""
    x = xml.Renderer()

    node = x.fromstring(xml_test1_in)
    iterator = node.repeat(['test1', 'test2'], childname='child')

    for child, value in iterator:
        child(value)
    assert [child.text for child in node.getchildren()] == ['test1', 'test2']
Exemplo n.º 18
0
def test_parse_xmlstring2():
    """Bad encoding"""
    try:
        x = xml.Renderer()
        f = open(os.path.join(os.path.dirname(__file__), 'iso-8859.xml'))
        x.fromfile(f, encoding='utf-8')
        f.close()
    except (etree.XMLSyntaxError, UnicodeDecodeError):
        assert True
    else:
        assert False
Exemplo n.º 19
0
def test_parse_xmlstring1():
    """Good encoding"""
    try:
        x = xml.Renderer()
        f = open(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml'))
        x.fromstring(f.read())
        f.close()
    except UnicodeDecodeError:
        assert False
    else:
        assert True
Exemplo n.º 20
0
def test_replace2():
    """Replace simple node with text before by node"""
    x = xml.Renderer()

    child1 = x.child1()
    node = x.node('test', child1)
    assert node.getchildren()[0] == child1
    child2 = x.child2()
    child1.replace(child2)
    assert node.getchildren()[0] == child2
    assert node.tostring() == b'<node>test<child2/></node>'
Exemplo n.º 21
0
def test_replace7():
    x = xml.Renderer()

    t1 = x.fromstring('<a>before<x>kjkjkl</x>#<b b="b">hello</b>after</a>')
    t2 = x.fromstring('<toto>xxx<titi a="a">world</titi>yyy</toto>')

    t1[1].replace(t2[0])
    # t1[1].replace('new')
    # t1[1].replace()

    assert t1.tostring() == b'<a>before<x>kjkjkl</x>#<titi a="a">world</titi>after</a>'
Exemplo n.º 22
0
def test_replace3():
    """Replace simple node with text after by node"""
    x = xml.Renderer()

    child1 = x.child1()
    node = x.node(child1, 'test')
    assert node.getchildren()[0] == child1
    child2 = x.child2()
    child1.replace(child2)
    assert node.getchildren()[0] == child2
    assert node.tostring() == b'<node><child2/>test</node>'
Exemplo n.º 23
0
def test_repeat2():
    """Repeat with 2 simple text, don't use childname argument"""
    x = xml.Renderer()

    node = x.fromstring(xml_test1_in)
    child = node.findmeld('child')
    iterator = child.repeat(['test1', 'test2'])

    for child, value in iterator:
        child(value)
    assert [child.text for child in node.getchildren()] == ['test1', 'test2']
Exemplo n.º 24
0
def test_comment():
    x = xml.Renderer()
    p = presentation.PresentationService(None, None, False)

    r = p.serialize(x.comment('hello'), 'utf-8')
    assert r == b'<!--hello-->'

    r = p.serialize(x.comment(u'été'), 'utf-8', None)
    assert r == b'<!--\xc3\xa9t\xc3\xa9-->'

    r = p.serialize(x.comment('hello'), 'utf-8', '<!DOCTYPE html>')
    assert r == b'<!DOCTYPE html>\n<!--hello-->'
Exemplo n.º 25
0
def test_xml():
    x = xml.Renderer()
    p = presentation.PresentationService(None, None, False)

    r = p.serialize(x.person('hello'))
    assert r == b'<person>hello</person>'

    r = p.serialize(x.person(u'été'), 'utf-8', None)
    assert r == b'<person>\xc3\xa9t\xc3\xa9</person>'

    r = p.serialize(x.person('hello'), 'utf-8', '<!DOCTYPE html>')
    assert r == b'<!DOCTYPE html>\n<person>hello</person>'
Exemplo n.º 26
0
def test_identity():
    for x in (xsl.Renderer(), xsl2.Renderer(), xsl3.Renderer()):
        style_sheet = x.stylesheet(
            x.output(encoding='utf-8'),
            x.template(x.copy_of(select='.'), match='/'))

        h = xml.Renderer()

        page = h.html(h.h1('Hello'), h.h2('world'))

        r = page.getroottree().xslt(style_sheet)

        assert etree.tostring(r) == page.tostring()
Exemplo n.º 27
0
def test2():
    for x in (xsl.Renderer(), xsl2.Renderer(), xsl3.Renderer()):
        style_sheet = x.stylesheet(
            x.output(encoding='utf-8'),
            x.template(x.copy_of(select='.'), match='/'))

        h = xml.Renderer()
        page = h.html(h.h1('Hello'), h.h2('World'))

        r = page.getroottree().xslt(style_sheet)

        assert r.xpath('//html/h1')[0].text == 'Hello'
        assert r.xpath('//html/h2')[0].text == 'World'
Exemplo n.º 28
0
def test_pi():
    x = xml.Renderer()
    p = presentation.PresentationService(None, None, False)

    r = p.serialize(x.processing_instruction('hello'))
    assert r == b'<?hello ?>'

    r = p.serialize(x.processing_instruction(u'été'), 'utf-8', None)
    assert r == b'<?\xc3\xa9t\xc3\xa9 ?>'

    r = p.serialize(x.processing_instruction('hello'), 'utf-8',
                    '<!DOCTYPE html>')
    assert r == b'<!DOCTYPE html>\n<?hello ?>'
Exemplo n.º 29
0
def test_global8():
    """Create xml"""
    x = xml.Renderer()
    x.namespaces = {'meld': 'http://www.plope.com/software/meld3'}
    data = (
        {
            'name': 'Girls',
            'description': 'Pretty'
        },
        {
            'name': 'Boys',
            'description': 'Ugly'
        },
    )

    with x.html:
        with x.head:
            with x.meta:
                x << {
                    'content': 'text/html; charset=ISO-8859-1',
                    'http-equiv': 'content-type'
                }
            with x.title.meld_id('title'):
                x << 'My document'
        with x.body:
            with x.div:
                pass
            x << x.comment(' empty tag ')
            with x.div:
                with x.form.meld_id('form1'):
                    x << {'action': './handler'}
                    with x.table:
                        with x.tbody:
                            with x.tr:
                                x << x.th('Name') << x.th('Description')
                            for elt in data:
                                with x.tr.meld_id('tr'):
                                    x << x.td(
                                        elt['name']).meld_id('td') << x.td(
                                            elt['description']).meld_id('td')
                    with x.input:
                        x << {
                            'type': 'submit',
                            'name': 'next',
                            'value': ' Next '
                        }

    assert [elt.text for elt in x.root.xpath('.//td')
            ] == ['Girls', 'Pretty', 'Boys', 'Ugly']
    assert x.root[0][1].text == 'My document'
    assert x.root.xpath('.//form')[0].attrib['action'] == './handler'
Exemplo n.º 30
0
def test_global3():
    """Test parse() method"""
    x = xml.Renderer()

    with open(os.path.join(os.path.dirname(__file__),
                           'test_xmlns_1.xml')) as f:
        root = x.fromstring(f.read())

    xml_to_test = root.tostring(xml_declaration=True, pretty_print=True)

    with open(os.path.join(os.path.dirname(__file__), 'test_xmlns_1.xml'),
              'rb') as f:
        xml_to_compare = f.read()
    assert xml_to_test == xml_to_compare