def test_simple_inheritance(): """simple inheritance""" params = dict( message='hello, from breve', title=my_name() ) _test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_register_global(): """register_global() function""" params = dict( title=my_name() ) register_global('global_message', 'This is a global variable') _test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_simple_inheritance(self): '''simple inheritance''' vars = dict(message='hello, from breve', title=my_name()) test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_register_global(self): '''register_global() function''' vars = dict(title=my_name()) register_global('global_message', 'This is a global variable') test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_macros(): """test macros""" url_data = [ {'url': 'http://www.google.com', 'label': 'Google'}, {'url': 'http://www.yahoo.com', 'label': 'Yahoo!'}, {'url': 'http://www.amazon.com', 'label': 'Amazon'} ] template = ( macro('test_macro', lambda url, label: T.a(href=url)[label] ), T.html[ T.head[T.title[my_name()]], T.body[ T.ul[ [T.li[test_macro(**_item)] # @UndefinedVariable for _item in url_data] ] ] ] ) output = flatten(template) assert output == ('<html><head><title>test_macros</title></head>' '<body><ul><li><a href="http://www.google.com">Google</a></li>' '<li><a href="http://www.yahoo.com">Yahoo!</a></li>' '<li><a href="http://www.amazon.com">Amazon</a></li></ul></body></html>')
def test_macros ( self ): '''test macros''' T = tags url_data = [ { 'url': 'http://www.google.com', 'label': 'Google' }, { 'url': 'http://www.yahoo.com', 'label': 'Yahoo!' }, { 'url': 'http://www.amazon.com', 'label': 'Amazon' } ] template = ( macro ( 'test_macro', lambda url, label: T.a ( href = url ) [ label ] ), T.html [ T.head [ T.title [ my_name ( ) ] ], T.body [ T.ul [ [ T.li [ test_macro ( **_item ) ] for _item in url_data ] ] ] ] ) output = flatten ( template ) self.assertEqual ( output, ( u'<html><head><title>test_macros</title></head>' u'<body><ul><li><a href="http://www.google.com">Google</a></li>' u'<li><a href="http://www.yahoo.com">Yahoo!</a></li>' u'<li><a href="http://www.amazon.com">Amazon</a></li></ul></body></html>' ) )
def test_tag_multiplication(self): """tag multiplication""" T = tags url_data = [ dict(url='http://www.google.com', label='Google'), dict(url='http://www.yahoo.com', label='Yahoo!'), dict(url='http://www.amazon.com', label='Amazon') ] template = T.html[ T.head[T.title[my_name()]], T.body[ T.ul[ T.li[T.a(href="$url")["$label"]] * url_data ] ] ] output = flatten(template) self.assertEqual( output, (u'<html><head><title>test_tag_multiplication</title></head>' u'<body><ul><li><a href="http://www.google.com">Google</a></li>' u'<li><a href="http://www.yahoo.com">Yahoo!</a></li>' u'<li><a href="http://www.amazon.com">Amazon</a></li></ul></body></html>') )
def test_let_directive_scope(self): '''test let directive's scope''' vars = dict(message='hello, from breve', title=my_name(), do_fail=False) # don't fail - use variable in scope t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise # do fail - try to use the variable out of scope vars['do_fail'] = True t = Template(html, root=template_root()) self.failUnlessRaises(NameError, t.render, 'index', vars, namespace='v')
def test_custom_renderer ( self ): '''custom renderer''' def render_row ( tag, data ): T = html tag.clear ( ) return tag [ [ T.td [ _i ] for _i in data ] ] register_global ( 'render_row', render_row ) vars = dict ( title = my_name ( ), my_data = [ range ( 5 ), range ( 5, 10 ), range ( 10, 15 ) ] ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_autolist_macro ( self ): '''test autolist macro''' data = [ "Item %s" % _i for _i in range ( 1,9 ) ] template = ( macro ( 'AutoList', lambda data: data and ( T.ul ( class_='autolist' ) [ [ T.li [ _i ] for _i in data ] ] ) or '' ), T.html [ T.head [ T.title [ my_name ( ) ] ], T.body [ AutoList ( data ) ] ] ) output = flatten ( template ) self.assertEqual ( output, ( u'<html><head><title>test_autolist_macro</title></head>' u'<body><ul class="autolist"><li>Item 1</li><li>Item 2</li>' u'<li>Item 3</li><li>Item 4</li><li>Item 5</li><li>Item 6</li>' u'<li>Item 7</li><li>Item 8</li></ul></body></html>' ) )
def test_nested_macros(self): '''test nested macros''' T = tags url_data = [{ 'url': 'http://www.google.com', 'label': 'Google' }, { 'url': 'http://www.yahoo.com', 'label': 'Yahoo!' }, { 'url': 'http://www.amazon.com', 'label': 'Amazon' }] template = ( macro( 'list_macro', lambda url, label: (macro('link_macro', lambda _u, _l: T.a(href=_u)[_l]), T.li[ link_macro(url, label)])), T.html[T.head[T.title[my_name()]], T.body[T.ul[[list_macro(**_item) for _item in url_data]]]]) output = flatten(template) self.assertEqual(output, ( u'<html><head><title>test_nested_macros</title></head>' u'<body><ul><li><a href="http://www.google.com">Google</a></li>' u'<li><a href="http://www.yahoo.com">Yahoo!</a></li>' u'<li><a href="http://www.amazon.com">Amazon</a></li></ul></body></html>' ))
def test_tag_multiplication_with_macro(self): '''tag multiplication including macro''' T = tags url_data = [{ 'url': 'http://www.google.com', 'label': 'Google', 'class': 'link' }, { 'url': 'http://www.yahoo.com', 'label': 'Yahoo!', 'class': 'link' }, { 'url': 'http://www.amazon.com', 'label': 'Amazon', 'class': 'link' }] template = ( macro('test_macro', lambda url: T.a(href=url)["$label"]), T.html[T.head[T.title[my_name()]], T.body[T.ul[T.li(class_="$class")[test_macro("$url")] * url_data]]]) output = flatten(template) self.assertEqual(output, ( u'<html><head><title>test_tag_multiplication_with_macro</title></head>' u'<body><ul><li class="link"><a href="http://www.google.com">Google</a></li>' u'<li class="link"><a href="http://www.yahoo.com">Yahoo!</a></li>' u'<li class="link"><a href="http://www.amazon.com">Amazon</a></li></ul></body></html>' ))
def test_let_directive_scope ( self ): '''test let directive's scope''' vars = dict ( message = 'hello, from breve', title = my_name ( ), do_fail = False ) # don't fail - use variable in scope t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise # do fail - try to use the variable out of scope vars [ 'do_fail' ] = True t = Template ( html, root = template_root ( ) ) self.failUnlessRaises ( NameError, t.render, 'index', vars, namespace = 'v' )
def test_register_flattener(): """register_flattener() function""" def flatten_date(o): return escape(o.strftime('%Y/%m/%d')) register_flattener(datetime, flatten_date) register_global('flatten_date', flatten_date) params = dict( title=my_name(), today=datetime(2008, 4, 17) ) _test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_simple_inheritance ( self ): '''simple inheritance''' vars = dict ( message = 'hello, from breve', title = my_name ( ) ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_nested_macros(self): """test nested macros""" T = tags url_data = [ {'url': 'http://www.google.com', 'label': 'Google'}, {'url': 'http://www.yahoo.com', 'label': 'Yahoo!'}, {'url': 'http://www.amazon.com', 'label': 'Amazon'} ] template = ( macro('list_macro', lambda url, label: ( macro('link_macro', lambda _u, _l: T.a(href=_u)[_l] ), T.li[link_macro(url, label)] )), T.html[ T.head[T.title[my_name()]], T.body[ T.ul[ [list_macro(**_item) for _item in url_data] ] ] ] ) output = flatten(template) self.assertEqual( output, (u'<html><head><title>test_nested_macros</title></head>' u'<body><ul><li><a href="http://www.google.com">Google</a></li>' u'<li><a href="http://www.yahoo.com">Yahoo!</a></li>' u'<li><a href="http://www.amazon.com">Amazon</a></li></ul></body></html>') )
def test_tag_multiplication_with_macro(self): """tag multiplication including macro""" T = tags url_data = [ {'url': 'http://www.google.com', 'label': 'Google', 'class': 'link'}, {'url': 'http://www.yahoo.com', 'label': 'Yahoo!', 'class': 'link'}, {'url': 'http://www.amazon.com', 'label': 'Amazon', 'class': 'link'} ] template = ( macro('test_macro', lambda url: T.a(href=url)["$label"] ), T.html[ T.head[T.title[my_name()]], T.body[ T.ul[ T.li(class_="$class")[test_macro("$url")] * url_data ] ] ] ) output = flatten(template) self.assertEqual( output, (u'<html><head><title>test_tag_multiplication_with_macro</title></head>' u'<body><ul><li class="link"><a href="http://www.google.com">Google</a></li>' u'<li class="link"><a href="http://www.yahoo.com">Yahoo!</a></li>' u'<li class="link"><a href="http://www.amazon.com">Amazon</a></li></ul></body></html>') )
def test_register_global ( self ): '''register_global() function''' vars = dict ( title = my_name ( ) ) register_global ( 'global_message', 'This is a global variable' ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_tag_serialization(): """basic tag flattening""" template = T.html[ T.head[T.title[my_name()]], T.body[T.div['okay']] ] output = flatten(template) assert output == ('<html><head><title>test_tag_serialization</title></head>' '<body><div>okay</div></body></html>')
def test_dom_traversal(): """tag.walk() DOM traversal""" template = T.html[ T.head[T.title[my_name()]], T.body[T.div['okay']] ] traversal = [] def callback(item, is_tag): if is_tag: traversal.append(item.name) else: traversal.append(item) template.walk(callback) output = ''.join(traversal) assert output == 'htmlheadtitle%sbodydivokay' % my_name()
def test_encoding(): """encoding comments""" params = dict( title=my_name() ) t = Template(html, root=template_root()) expected = expected_output() actual = t.render('correct', params, namespace='v') assert actual == expected
def test_register_flattener(self): '''register_flattener() function''' def flatten_date(o): return escape(o.strftime('%Y/%m/%d')) register_flattener(datetime, flatten_date) register_global('flatten_date', flatten_date) vars = dict(title=my_name(), today=datetime(2008, 4, 17)) test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_nested_include(): """nested include() directives""" params = dict( message='hello, from breve', title=my_name() ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_tag_serialization(self): '''basic tag flattening''' T = tags template = T.html[T.head[T.title[my_name()]], T.body[T.div['okay']]] output = flatten(template) self.assertEqual( output, (u'<html><head><title>test_tag_serialization</title></head>' u'<body><div>okay</div></body></html>'))
def test_loop_include(): """looping over include() with listcomp""" params = dict( message='hello, from breve', title=my_name() ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_custom_loader_stack ( self ): '''custom loader stack''' class PathLoader ( object ): __slots__ = [ 'paths' ] def __init__ ( self, *paths ): self.paths = paths def stat ( self, template, root ): for p in self.paths: f = os.path.join ( root, p, template ) if os.path.isfile ( f ): timestamp = long ( os.stat ( f ).st_mtime ) uid = f return uid, timestamp raise OSError, 'No such file or directory %s' % template def load ( self, uid ): return file ( uid, 'U' ).read ( ) loader = PathLoader ( template_root ( ), os.path.join ( template_root ( ), 'path1' ), os.path.join ( template_root ( ), 'path2' ), os.path.join ( template_root ( ), 'path3' ), ) register_global ( 'path_loader', loader ) vars = dict ( title = my_name ( ), message = 'hello, world' ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_assign_scope(): """test assign directive's scope""" params = dict( message='hello, from breve', title=my_name() ) # don't fail - use variable in scope t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_assign(self): '''assign directive''' T = tags template = (assign('msg', 'okay'), T.html[T.head[T.title[my_name()]], T.body[T.div[msg]]]) output = flatten(template) self.assertEqual( output, u'<html><head><title>test_assign</title></head><body><div>okay</div></body></html>' )
def test_dom_traversal(self): '''tag.walk() DOM traversal''' T = tags template = T.html[T.head[T.title[my_name()]], T.body[T.div['okay']]] traversal = [] def callback(item, is_tag): if is_tag: traversal.append(item.name) else: traversal.append(item) template.walk(callback) output = ''.join(traversal) self.assertEqual( output, u'htmlheadtitle%sbodydivokay' % my_name(), )
def test_loop_include(self): '''looping over include() with listcomp''' vars = dict(message='hello, from breve', title=my_name()) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_macros_inside_inherits(): """test macros inside inherits(): scope issues""" # note: I'm not convinced this is the desired behaviour, but # it's *compatible* behaviour. params = dict( title=my_name(), message='Hello, from breve' ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_macro_includes(self): '''include() directive inside macro''' vars = dict(message='hello, from breve', title=my_name()) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_stacks_template(): """test stacks in template""" params = dict( title=my_name(), message='hello, from breve' ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected # stack should be empty when we finish assert not get_stacks()
def test_flatten_callable(self): '''test flattening of callables''' def my_callable(): return "Hello, World" T = tags template = (T.html[T.head[T.title[my_name()]], T.body[T.div[my_callable]]]) actual = flatten(template) self.assertEqual( actual, (u'<html><head><title>test_flatten_callable</title></head>' u'<body><div>Hello, World</div></body></html>'))
def test_assign(): """assign directive""" template = ( assign('msg', 'okay1'), T.html[ T.head[T.title[my_name()]], T.body[T.div[msg]] # @UndefinedVariable ] ) output = flatten(template) assert output == ('<html><head><title>test_assign</title></head>' '<body><div>okay1</div></body></html>')
def test_register_flattener ( self ): '''register_flattener() function''' def flatten_date ( o ): return escape ( o.strftime ( '%Y/%m/%d' ) ) register_flattener ( datetime, flatten_date ) register_global ( 'flatten_date', flatten_date ) vars = dict ( title = my_name ( ), today = datetime ( 2008, 4, 17 ) ) test_name = my_name ( ) t = Template ( html, root = template_root ( ) ) actual = t.render ( 'index', vars, namespace = 'v' ) expected = expected_output ( ) try: self.assertEqual ( actual, expected ) except AssertionError: diff ( actual, expected ) raise
def test_test(self): '''test() function''' T = tags template = T.html[T.head[T.title[my_name()]], T.body[test(1 == 1) and (T.span['This is displayed']), test(1 == 0) and (T.span['This is not displayed'])]] output = flatten(template) self.assertEqual( output, (u'<html><head><title>test_test</title></head>' u'<body><span>This is displayed</span></body></html>'))
def test_dom_traversal ( self ): '''tag.walk() DOM traversal''' T = tags template = T.html [ T.head [ T.title [ my_name ( ) ] ], T.body [ T.div [ 'okay' ] ] ] traversal = [ ] def callback ( item, is_tag ): if is_tag: traversal.append ( item.name ) else: traversal.append ( item ) template.walk ( callback ) output = ''.join ( traversal ) self.assertEqual ( output, u'htmlheadtitle%sbodydivokay' % my_name ( ), )
def test_custom_loader_stack(self): '''custom loader stack''' class PathLoader(object): __slots__ = ['paths'] def __init__(self, *paths): self.paths = paths def stat(self, template, root): for p in self.paths: f = os.path.join(root, p, template) if os.path.isfile(f): timestamp = long(os.stat(f).st_mtime) uid = f return uid, timestamp raise OSError, 'No such file or directory %s' % template def load(self, uid): return file(uid, 'U').read() loader = PathLoader( template_root(), os.path.join(template_root(), 'path1'), os.path.join(template_root(), 'path2'), os.path.join(template_root(), 'path3'), ) register_global('path_loader', loader) vars = dict(title=my_name(), message='hello, world') test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_unicode_attributes(self): '''unicode and string coercion in attributes''' T = tags template = T.html[T.head[T.title[my_name()]], T.body[ T.span(id='удерживать')["Coerce byte string to Unicode"], T.span(id='не оставляющий сомнений')["Explicit Unicode object"]]] output = flatten(template) self.assertEqual(output, ( u'<html><head><title>test_unicode_attributes</title></head><body>' u'<span id="удерживать">Coerce byte string to Unicode</span>' u'<span id="не оставляющий сомнений">Explicit Unicode object</span></body></html>' ))
def test_assign_with_macro(self): '''assign directive with macro''' T = tags template = (assign('msg', 'okay'), macro('display_msg', lambda _m: T.span[_m]), T.html[T.head[T.title[my_name()]], T.body[T.div[display_msg(msg)]]]) output = flatten(template) self.assertEqual( output, (u'<html><head><title>test_assign_with_macro</title></head>' u'<body><div><span>okay</span></div></body></html>'))
def test_encoding(self): '''encoding comments''' vars = dict(title=my_name()) t = Template(html, root=template_root()) actual = t.render('wrong', vars, namespace='v') expected = expected_output() self.assertNotEqual(actual, expected) actual = t.render('correct', vars, namespace='v') try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_render_parameters(self): '''test render-time parameters''' vars = dict(message='hello, from breve', title=my_name()) args = { 'tidy': True, 'debug': True, 'namespace': 'v', 'extension': '.breve' } t = Template(html, root=template_root()) t.render('index', vars, **args) for k, v in args.items(): self.failUnless(getattr(t, k) == v)
def test_tag_serialization(self): """basic tag flattening""" T = tags template = T.html[ T.head[T.title[my_name()]], T.body[T.div['okay']] ] output = flatten(template) self.assertEqual( output, (u'<html><head><title>test_tag_serialization</title></head>' u'<body><div>okay</div></body></html>') )
def test_unicode_attributes(): """unicode and string coercion in attributes""" template = T.html[ T.head[T.title[my_name()]], T.body[ T.span(id='удерживать')["Coerce byte string to Unicode"], T.span(id='не оставляющий сомнений')["Explicit Unicode object"] ] ] output = flatten(template) assert output == ('<html><head><title>test_unicode_attributes</title></head><body>' '<span id="удерживать">Coerce byte string to Unicode</span>' '<span id="не оставляющий сомнений">Explicit Unicode object</span>' '</body></html>')
def test_assign_scope(self): '''test assign directive's scope''' vars = dict(message='hello, from breve', title=my_name()) # don't fail - use variable in scope t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_custom_loader_stack(): """custom loader stack""" class PathLoader(object): __slots__ = ['paths'] def __init__(self, *paths): self.paths = paths def stat(self, template, root): for p in self.paths: f = os.path.join(root, p, template) if os.path.isfile(f): timestamp = int(os.stat(f).st_mtime) uid = f return uid, timestamp raise OSError('No such file or directory %s' % template) def load(self, uid): return open(uid, 'U').read() loader = PathLoader( template_root(), os.path.join(template_root(), 'path1'), os.path.join(template_root(), 'path2'), os.path.join(template_root(), 'path3'), ) register_global('path_loader', loader) params = dict( title=my_name(), message='hello, world' ) _test_name = my_name() t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_macros_inside_inherits(self): '''test macros inside inherits(): scope issues''' # note: I'm not convinced this is the desired behaviour, but # it's *compatible* behaviour. vars = dict(title=my_name(), message='Hello, from breve') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_custom_renderer_notag(): """custom renderer returning non-Tag type""" def render_text(tag, data): tag.clear() return data register_global('render_text', render_text) params = dict( title=my_name(), message='hello, world' ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_loop_macros(): """loop using macro""" params = dict( message='hello, from breve', title=my_name(), url_data=[ dict(url='http://www.google.com', label='Google'), dict(url='http://www.yahoo.com', label='Yahoo!'), dict(url='http://www.amazon.com', label='Amazon') ] ) t = Template(html, root=template_root()) actual = t.render('index', params, namespace='v') expected = expected_output() assert actual == expected
def test_unicode(self): '''unicode and string coercion''' T = tags template = T.html[T.head[T.title[my_name()]], T.body['Brev\xc3\xa9 converts plain strings', T.br, u'Brev\xe9 handles unicode strings', T.br, T.div["äåå? ▸ ", T.em["я не понимаю"], "▸ 3 km²"]]] output = flatten(template) self.assertEqual( output, (u'<html><head><title>test_unicode</title></head>' u'<body>Brevé converts plain strings<br />' u'Brevé handles unicode strings<br />' u'<div>äåå? ▸ <em>я не понимаю</em>▸ 3 km²</div></body></html>'))
def test_stacks_template(self): '''test stacks in template''' vars = dict(title=my_name(), message='hello, from breve') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise # stack should be empty when we finish self.failUnless(not get_stacks())
def test_escaping(self): '''escaping, xml() directive''' T = tags template = T.html[T.head[T.title[my_name()]], T.body[T.div( style='width: 400px;<should be &escaped&>' )[T.p(class_='foo')['&&&'], T.p['Coffee', E.nbsp, E.amp, E.nbsp, 'cream'], xml('''<div>this should be <u>unescaped</u> & unaltered.</div>''' )]]] output = flatten(template) self.assertEqual(output, ( u'<html><head><title>test_escaping</title></head>' u'<body><div style="width: 400px;<should be &escaped&>">' u'<p class="foo">&&&</p><p>Coffee & cream</p>' u'<div>this should be <u>unescaped</u> & unaltered.</div></div></body></html>' ))
def test_let_memory_freed(self): '''test that let() objects are freed''' # is this even meaningful? import gc vars = dict(title=my_name(), message="memory test", biglist=['hello'] * 1000) collection_count = gc.get_count() t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') del vars gc.collect() self.assertEqual(gc.get_count(), (0, 0, 0))
def test_custom_renderer_notag(self): '''custom renderer returning non-Tag type''' def render_text(tag, data): tag.clear() return data register_global('render_text', render_text) vars = dict(title=my_name(), message='hello, world') t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_loop_macros(self): '''loop using macro''' vars = dict(message='hello, from breve', title=my_name(), url_data=[ dict(url='http://www.google.com', label='Google'), dict(url='http://www.yahoo.com', label='Yahoo!'), dict(url='http://www.amazon.com', label='Amazon') ]) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise
def test_tag_multiplication(self): '''tag multiplication''' T = tags url_data = [ dict(url='http://www.google.com', label='Google'), dict(url='http://www.yahoo.com', label='Yahoo!'), dict(url='http://www.amazon.com', label='Amazon') ] template = T.html[T.head[T.title[my_name()]], T.body[T.ul[T.li[T.a(href="$url")["$label"]] * url_data]]] output = flatten(template) self.assertEqual(output, ( u'<html><head><title>test_tag_multiplication</title></head>' u'<body><ul><li><a href="http://www.google.com">Google</a></li>' u'<li><a href="http://www.yahoo.com">Yahoo!</a></li>' u'<li><a href="http://www.amazon.com">Amazon</a></li></ul></body></html>' ))
def test_custom_renderer(self): '''custom renderer''' def render_row(tag, data): T = html tag.clear() return tag[[T.td[_i] for _i in data]] register_global('render_row', render_row) vars = dict(title=my_name(), my_data=[range(5), range(5, 10), range(10, 15)]) t = Template(html, root=template_root()) actual = t.render('index', vars, namespace='v') expected = expected_output() try: self.assertEqual(actual, expected) except AssertionError: diff(actual, expected) raise