Ejemplo n.º 1
0
def test_model_plugin():
    div_elem = MockElement('div', id='test')
    text_elem = MockElement('#text', id='text')
    text_elem.text = "{{ c['name'] }}"
    div_elem <= text_elem
    input_elem = MockElement('input', model='c["name"]', id='input')
    div_elem <= input_elem
    document <= div_elem
    tpl = Template(document['test'])
    ctx = Context({})
    ctx.c = {'name': ''}
    elem = tpl.bind_ctx(ctx)
    text_elem = document['text']
    input_elem = document['input']
    assert text_elem.text == ''
    input_elem.setAttribute('value', 'Jonathan')
    assert text_elem.text == 'Jonathan'
    assert ctx.c['name'] == 'Jonathan'
    assert input_elem.value == 'Jonathan'
    input_elem.value = 'Test'
    assert text_elem.text == 'Jonathan'
    assert ctx.c['name'] == 'Jonathan'
    ctx.c['name'] = 'Test2'
    assert input_elem.value == 'Test2'
    assert text_elem.text == 'Test2'
Ejemplo n.º 2
0
def test_extension():
    base = Context()
    base.a = 10
    base.c = 30

    child = Context(base=base)

    # Child should have access to parent
    assert child.a == 10

    # The _get method should work for accessing parent
    assert child._get('a') == 10

    # Child should not be allowed to modify parent
    child.a = 20
    assert child.a == 20
    assert base.a == 10

    # Attributes should propagate recursively
    second_child = Context(base=child)
    assert second_child.c == 30
    assert second_child.a == 20