Beispiel #1
0
def test_loop_unpack():
    from kemmering import bind, cond, from_context, loop, tag

    def animals(context):
        return enumerate(('kitty', 'puppy', 'bunny'))

    def is_even(context):
        return context['i'] % 2 == 0

    doc = tag('doc', foo=from_context('foo'))(
        tag('ul')(
            loop(('i', 'foo'), animals,
                 tag('li', class_=cond(is_even, 'even', 'odd'))(
                     from_context('foo')))),
        'foo is ', from_context('foo'),
    )
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'foo': 'bar'})
    assert STR(bound) == (
        '<doc foo="bar"><ul>'
        '<li class="even">kitty</li>'
        '<li class="odd">puppy</li>'
        '<li class="even">bunny</li>'
        '</ul>foo is bar</doc>')
Beispiel #2
0
def test_loop_unpack():
    from kemmering import bind, cond, from_context, loop, tag

    def animals(context):
        return enumerate(('kitty', 'puppy', 'bunny'))

    def is_even(context):
        return context['i'] % 2 == 0

    doc = tag('doc', foo=from_context('foo'))(
        tag('ul')(
            loop(('i', 'foo'), animals,
                 tag('li', class_=cond(is_even, 'even', 'odd'))(
                     from_context('foo')))),
        'foo is ', from_context('foo'),
    )
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'foo': 'bar'})
    assert STR(bound) == (
        '<doc foo="bar"><ul>'
        '<li class="even">kitty</li>'
        '<li class="odd">puppy</li>'
        '<li class="even">bunny</li>'
        '</ul>foo is bar</doc>')
Beispiel #3
0
def test_cond_deferred_children():
    from kemmering import bind, cond, tag, format_context

    def is_admin(context):
        return context['user'] == 'admin'
    doc = tag('doc')(tag('p')('Hi there.', cond(
        is_admin, format_context(' Hi {user}!'))))
    assert REPR(doc) == (
        "tag('doc')(tag('p')('Hi there.', cond("
        "is_admin, format_context(' Hi {user}!'))))")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Hi there. Hi admin!</p></doc>'
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Hi there.</p></doc>'
Beispiel #4
0
def test_cond_no_negative():
    from kemmering import bind, cond, tag

    def is_admin(context):
        return context['user'] == 'admin'
    doc = tag('doc')(tag('p')('Hi there.', cond(
        is_admin, ' How do you do?')))
    assert REPR(doc) == (
        "tag('doc')(tag('p')('Hi there.', cond("
        "is_admin, ' How do you do?')))")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Hi there. How do you do?</p></doc>'
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Hi there.</p></doc>'
Beispiel #5
0
def test_cond_key_condition():
    from kemmering import bind, cond, tag

    doc = tag('doc')(cond('admin',
                          tag('p')('At your service!'),
                          tag('p')('Go away!')))
    assert REPR(doc) == (
        "tag('doc')(cond('admin', "
        "tag('p')('At your service!'), "
        "tag('p')('Go away!')))")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'admin': True})
    assert STR(bound) == '<doc><p>At your service!</p></doc>'
    bound = bind(doc, {'admin': False})
    assert STR(bound) == '<doc><p>Go away!</p></doc>'
Beispiel #6
0
def test_cond_deferred_children():
    from kemmering import bind, cond, tag, format_context

    def is_admin(context):
        return context['user'] == 'admin'
    doc = tag('doc')(tag('p')('Hi there.', cond(
        is_admin, format_context(' Hi {user}!'))))
    assert REPR(doc) == (
        "tag('doc')(tag('p')('Hi there.', cond("
        "is_admin, format_context(' Hi {user}!'))))")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Hi there. Hi admin!</p></doc>'
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Hi there.</p></doc>'
Beispiel #7
0
def test_cond_no_negative():
    from kemmering import bind, cond, tag

    def is_admin(context):
        return context['user'] == 'admin'
    doc = tag('doc')(tag('p')('Hi there.', cond(
        is_admin, ' How do you do?')))
    assert REPR(doc) == (
        "tag('doc')(tag('p')('Hi there.', cond("
        "is_admin, ' How do you do?')))")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Hi there. How do you do?</p></doc>'
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Hi there.</p></doc>'
Beispiel #8
0
def test_cond_key_condition():
    from kemmering import bind, cond, tag

    doc = tag('doc')(cond('admin',
                          tag('p')('At your service!'),
                          tag('p')('Go away!')))
    assert REPR(doc) == (
        "tag('doc')(cond('admin', "
        "tag('p')('At your service!'), "
        "tag('p')('Go away!')))")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'admin': True})
    assert STR(bound) == '<doc><p>At your service!</p></doc>'
    bound = bind(doc, {'admin': False})
    assert STR(bound) == '<doc><p>Go away!</p></doc>'
Beispiel #9
0
def test_cond():
    from kemmering import bind, cond, tag

    def is_admin(context):
        return context['user'] == 'admin'
    doc = tag('doc')(cond(is_admin,
                          tag('p')('At your service!'),
                          tag('p')('Go away!')))
    assert REPR(doc) == (
        "tag('doc')(cond(is_admin, "
        "tag('p')('At your service!'), "
        "tag('p')('Go away!')))")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>At your service!</p></doc>'
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Go away!</p></doc>'
Beispiel #10
0
def test_cond():
    from kemmering import bind, cond, tag

    def is_admin(context):
        return context['user'] == 'admin'
    doc = tag('doc')(cond(is_admin,
                          tag('p')('At your service!'),
                          tag('p')('Go away!')))
    assert REPR(doc) == (
        "tag('doc')(cond(is_admin, "
        "tag('p')('At your service!'), "
        "tag('p')('Go away!')))")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>At your service!</p></doc>'
    bound = bind(doc, {'user': '******'})
    assert STR(bound) == '<doc><p>Go away!</p></doc>'
Beispiel #11
0
def test_loop_unpack_not_enough():
    from kemmering import bind, cond, from_context, loop, tag

    def animals(context):
        return enumerate(('kitty', 'puppy', 'bunny'))

    def is_even(context):
        return context['i'] % 2 == 0

    doc = tag('doc', foo=from_context('foo'))(
        tag('ul')(
            loop(('i', 'foo', 'bar'), animals,
                 tag('li', class_=cond(is_even, 'even', 'odd'))(
                     from_context('foo')))),
        'foo is ', from_context('foo'),
    )
    with pytest.raises(ValueError):
        STR(doc)
    with pytest.raises(ValueError):
        bind(doc, {'foo': 'bar'})
Beispiel #12
0
def test_loop_unpack_not_enough():
    from kemmering import bind, cond, from_context, loop, tag

    def animals(context):
        return enumerate(('kitty', 'puppy', 'bunny'))

    def is_even(context):
        return context['i'] % 2 == 0

    doc = tag('doc', foo=from_context('foo'))(
        tag('ul')(
            loop(('i', 'foo', 'bar'), animals,
                 tag('li', class_=cond(is_even, 'even', 'odd'))(
                     from_context('foo')))),
        'foo is ', from_context('foo'),
    )
    with pytest.raises(ValueError):
        STR(doc)
    with pytest.raises(ValueError):
        bind(doc, {'foo': 'bar'})