Beispiel #1
0
def test_from_context_key_error():
    from kemmering import bind, from_context, tag

    doc = tag('doc')(
        tag('p')('Hello ', from_context('name'), '!'), 'foo')
    with pytest.raises(KeyError):
        bind(doc, {})
Beispiel #2
0
def test_from_context_key_error():
    from kemmering import bind, from_context, tag

    doc = tag('doc')(
        tag('p')('Hello ', from_context('name'), '!'), 'foo')
    with pytest.raises(KeyError):
        bind(doc, {})
Beispiel #3
0
def test_in_context_key_error():
    from kemmering import bind, in_context, tag

    doc = tag('doc')(
        tag('p')('Hello ', in_context(['user', 'name']), '!'), 'foo')
    assert REPR(doc) == ("tag('doc')(tag('p')"
                         "('Hello ', in_context(['user', 'name']), '!'),"
                         " 'foo')")
    with pytest.raises(ValueError):
        STR(doc)
    with pytest.raises(KeyError):
        bind(doc, {'user': {}})
Beispiel #4
0
def test_in_context_key_error():
    from kemmering import bind, in_context, tag

    doc = tag('doc')(
        tag('p')('Hello ', in_context(['user', 'name']), '!'), 'foo')
    assert REPR(doc) == ("tag('doc')(tag('p')"
                         "('Hello ', in_context(['user', 'name']), '!'),"
                         " 'foo')")
    with pytest.raises(ValueError):
        STR(doc)
    with pytest.raises(KeyError):
        bind(doc, {'user': {}})
Beispiel #5
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 #6
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 #7
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 #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_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 #10
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 #11
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 #12
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 #13
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 #14
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 #15
0
def test_defer_attribute():
    from kemmering import bind, defer, tag

    @defer
    def deferred(context):
        return 'Hello {}!'.format(context['name'])
    doc = tag('doc', foo=deferred)('woot')
    assert REPR(doc) == "tag('doc', foo=defer(deferred))('woot')"
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == '<doc foo="Hello Fred!">woot</doc>'
Beispiel #16
0
def test_defer_attribute():
    from kemmering import bind, defer, tag

    @defer
    def deferred(context):
        return 'Hello {}!'.format(context['name'])
    doc = tag('doc', foo=deferred)('woot')
    assert REPR(doc) == "tag('doc', foo=defer(deferred))('woot')"
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == '<doc foo="Hello Fred!">woot</doc>'
Beispiel #17
0
def test_format_context():
    from kemmering import bind, format_context, tag

    doc = tag('doc')(tag('p')(format_context('Hello {name}!')), 'foo')
    assert REPR(doc) == ("tag('doc')(tag('p')"
                         "(format_context('Hello {name}!')), 'foo')")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == '<doc><p>Hello Fred!</p>foo</doc>'
Beispiel #18
0
def test_defer_remove_attribute():
    from kemmering import bind, defer, tag

    @defer
    def deferred(context):
        return None
    doc = tag('doc', foo=deferred)('woot')
    assert REPR(doc) == "tag('doc', foo=defer(deferred))('woot')"
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == '<doc>woot</doc>'
Beispiel #19
0
def test_format_context():
    from kemmering import bind, format_context, tag

    doc = tag('doc')(tag('p')(format_context('Hello {name}!')), 'foo')
    assert REPR(doc) == ("tag('doc')(tag('p')"
                         "(format_context('Hello {name}!')), 'foo')")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == '<doc><p>Hello Fred!</p>foo</doc>'
Beispiel #20
0
def test_defer_remove_attribute():
    from kemmering import bind, defer, tag

    @defer
    def deferred(context):
        return None
    doc = tag('doc', foo=deferred)('woot')
    assert REPR(doc) == "tag('doc', foo=defer(deferred))('woot')"
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == '<doc>woot</doc>'
Beispiel #21
0
def test_from_context_use_default():
    from kemmering import bind, from_context, tag

    doc = tag('doc')(
        tag('p')('Hello ', from_context('name', 'World'), '!'), 'foo')
    assert REPR(doc) == ("tag('doc')(tag('p')"
                         "('Hello ', from_context('name'), '!'), 'foo')")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {})
    assert STR(bound) == '<doc><p>Hello World!</p>foo</doc>'
Beispiel #22
0
def test_from_context_use_default():
    from kemmering import bind, from_context, tag

    doc = tag('doc')(
        tag('p')('Hello ', from_context('name', 'World'), '!'), 'foo')
    assert REPR(doc) == ("tag('doc')(tag('p')"
                         "('Hello ', from_context('name'), '!'), 'foo')")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {})
    assert STR(bound) == '<doc><p>Hello World!</p>foo</doc>'
Beispiel #23
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 #24
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 #25
0
def test_defer():
    from kemmering import bind, defer, tag

    @defer
    def deferred(context):
        return tag('p')('Hello {}!'.format(context['name']))
    doc = tag('doc')(deferred, 'foo')
    assert REPR(doc) == "tag('doc')(defer(deferred), 'foo')"
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == '<doc><p>Hello Fred!</p>foo</doc>'
Beispiel #26
0
def test_defer():
    from kemmering import bind, defer, tag

    @defer
    def deferred(context):
        return tag('p')('Hello {}!'.format(context['name']))
    doc = tag('doc')(deferred, 'foo')
    assert REPR(doc) == "tag('doc')(defer(deferred), 'foo')"
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == '<doc><p>Hello Fred!</p>foo</doc>'
Beispiel #27
0
def test_in_context():
    from kemmering import bind, in_context, tag

    doc = tag('doc')(
        tag('p')('Hello ', in_context(['user', 'name']), '!'), 'foo')
    assert REPR(doc) == ("tag('doc')(tag('p')"
                         "('Hello ', in_context(['user', 'name']), '!'),"
                         " 'foo')")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'user': {'name': 'Fred'}})
    assert STR(bound) == '<doc><p>Hello Fred!</p>foo</doc>'
Beispiel #28
0
def test_in_context():
    from kemmering import bind, in_context, tag

    doc = tag('doc')(
        tag('p')('Hello ', in_context(['user', 'name']), '!'), 'foo')
    assert REPR(doc) == ("tag('doc')(tag('p')"
                         "('Hello ', in_context(['user', 'name']), '!'),"
                         " 'foo')")
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'user': {'name': 'Fred'}})
    assert STR(bound) == '<doc><p>Hello Fred!</p>foo</doc>'
Beispiel #29
0
def test_loop():
    from kemmering import bind, from_context, loop, tag

    doc = tag('doc', foo=from_context('foo'))(
        tag('ul')(
            loop('foo', 'animals',
                 tag('li')(from_context('foo')))),
        'foo is ', from_context('foo'),
    )
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'animals': ['kitty', 'puppy', 'bunny'],
                       'foo': 'bar'})
    assert STR(bound) == (
        '<doc foo="bar"><ul>'
        '<li>kitty</li><li>puppy</li><li>bunny</li>'
        '</ul>foo is bar</doc>')
Beispiel #30
0
def test_loop():
    from kemmering import bind, from_context, loop, tag

    doc = tag('doc', foo=from_context('foo'))(
        tag('ul')(
            loop('foo', 'animals',
                 tag('li')(from_context('foo')))),
        'foo is ', from_context('foo'),
    )
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'animals': ['kitty', 'puppy', 'bunny'],
                       'foo': 'bar'})
    assert STR(bound) == (
        '<doc foo="bar"><ul>'
        '<li>kitty</li><li>puppy</li><li>bunny</li>'
        '</ul>foo is bar</doc>')
Beispiel #31
0
def test_defer_with_notag():
    from kemmering import bind, defer, notag, tag

    @defer
    def deferred(context):
        return notag(
            tag('p')('Hello {}!'.format(context['name'])),
            tag('p')('Nice to meet you!'),
            tag('br/'),
        )
    doc = tag('doc')(deferred, 'foo')
    assert REPR(doc) == "tag('doc')(defer(deferred), 'foo')"
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == ('<doc><p>Hello Fred!</p>'
                          '<p>Nice to meet you!</p><br/>foo</doc>')
    assert REPR(bound) == ("tag('doc')(notag(tag('p')('Hello Fred!'), "
                           "tag('p')('Nice to meet you!'), tag('br/')), "
                           "'foo')")
Beispiel #32
0
def test_defer_with_notag():
    from kemmering import bind, defer, notag, tag

    @defer
    def deferred(context):
        return notag(
            tag('p')('Hello {}!'.format(context['name'])),
            tag('p')('Nice to meet you!'),
            tag('br/'),
        )
    doc = tag('doc')(deferred, 'foo')
    assert REPR(doc) == "tag('doc')(defer(deferred), 'foo')"
    with pytest.raises(ValueError):
        STR(doc)
    bound = bind(doc, {'name': 'Fred'})
    assert STR(bound) == ('<doc><p>Hello Fred!</p>'
                          '<p>Nice to meet you!</p><br/>foo</doc>')
    assert REPR(bound) == ("tag('doc')(notag(tag('p')('Hello Fred!'), "
                           "tag('p')('Nice to meet you!'), tag('br/')), "
                           "'foo')")