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, {})
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': {}})
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>')
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>'
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>'
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>'
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>'
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>'
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>'
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>'
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>'
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'})
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>'
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>'
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>')
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')")