1. Ronny Pfannschmidt
  2. gtkarchitect-main

Source

gtkarchitect-main / tests / model / test_entity.py

from lxml.etree import Element, SubElement, tostring, fromstring


from gtkarchitect.model.entity import (
        get_entity_type,
        get_entity_prop,
        set_entity_prop,
        add_entity_child,
        _fix_indent,
        tostring,
        )


def test_type():
    entity = Element('object', {'class':'test'})

    assert get_entity_type(entity) == 'test'


def test_prop_make():
    data = Element('test')
    assert len(data) == 0

    set_entity_prop(data, 'name', 'test')
    assert len(data) == 1

    set_entity_prop(data, 'name', 'test2')
    assert len(data) == 1

    set_entity_prop(data, 'name2', 'test')
    assert len(data) == 2


def test_prop_get():
    data = Element('test')
    for i in range(4):
        set_entity_prop(data, 'name%s'%i, 'test%s'%i)
        got = get_entity_prop(data, 'name%s'%i)
        assert got == 'test%s'%i

    assert get_entity_prop(data, 'unknown') is None


input = '<a><b/><c><b/><d name="a">test</d></c></a>'
output = '''<a>
    <b/>
    <c>
        <b/>
        <d name="a">test</d>
    </c>
</a>'''

def test_fix_indent():
    tree = fromstring(input)
    _fix_indent(tree)
    result = tostring(tree)
    print input
    print
    print output
    print
    print result
    assert result == output

def test_add_child():
    e = Element('test')
    c = Element('testchild')
    add_entity_child(e, c)
    print tostring(e)
    assert tostring(e) == '<test><child><testchild/></child></test>'

def test_add_internal_child():
    e = Element('test')
    c = Element('testchild')
    add_entity_child(e, c, internal='internal_name')
    print tostring(e)
    assert tostring(e) == '<test><child internal-child="internal_name"><testchild/></child></test>'

def test_add_nochild_child():
    e = Element('test')
    c = Element('testchild')
    add_entity_child(e, c, no_child=True)
    print tostring(e)
    assert tostring(e) == '<test><testchild/></test>'