1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
import doctest
from formencode.htmlgen import html
# A test value that can't be encoded as ascii:
uni_value = '\xff'
def test_basic():
output = '<a href="test">hey there</a>'
assert str(html.a(href='test')('hey there')) == output
assert str(html.a('hey there')(href='test')) == output
assert str(html.a(href='test', c='hey there')) == output
assert str(html.a('hey there', href='test')) == output
assert str(html.a(href='test')('hey ', 'there')) == output
assert str(html.a(href='test')(['hey ', 'there'])) == output
def test_compound():
output = '<b>Hey <i>you</i>!</b>'
assert str(html.b('Hey ', html.i('you'), '!')) == output
assert str(html.b()('Hey ')(html.i()('you'))('!')) == output
inner = html('Hey ', html.i('you'), '!')
assert html.str(inner) == 'Hey <i>you</i>!'
assert str(inner) == 'Hey <i>you</i>!'
assert str(html.b(inner)) == output
def test_unicode():
try:
uni_value.encode('ascii')
except ValueError:
pass
else:
assert False, (
"We need something that can't be ASCII-encoded: %r (%r)"
% (uni_value, uni_value.encode('ascii')))
assert str(html.b(uni_value)) == '<b>%s</b>' % uni_value
def test_quote():
assert html.quote('<hey>!') == '<hey>!'
assert html.quote(uni_value) == uni_value
assert html.quote(None) == ''
assert html.str(None) == ''
assert str(html.b('<hey>')) == '<b><hey></b>'
def test_comment():
def strip(s):
"""ElementTree in Py < 2.7 adds whitespace, strip this."""
s = str(s).strip()
if s.startswith('<!--') and s.endswith('-->'):
s = '<!--%s-->' % s[4:-3].strip()
return s
assert strip(html.comment('test')) == '<!--test-->'
assert strip(html.comment(uni_value)) == '<!--%s-->' % uni_value
assert strip(html.comment('test')('this')) == '<!--testthis-->'
def test_none():
assert html.str(None) == ''
assert str(html.b(class_=None)('hey')) == '<b>hey</b>'
assert str(html.b(class_=' ')(None)) == '<b class=" " />'
def test_namespace():
output = '<b tal:content="options/whatever" />'
assert str(html.b(**{'tal:content': 'options/whatever'})) == output
assert str(html.b(tal__content='options/whatever')) == output
if __name__ == '__main__':
# It's like a super-mini py.test...
for name, value in globals().items():
if name.startswith('test'):
print(name)
value()
from formencode import htmlgen
doctest.testmod(htmlgen)
print('doctest')
|