def test_nested_call_4(self): base = """ <%def name="A()"> A_def ${caller.body()} </%def> <%def name="B()"> B_def ${caller.body()} </%def> """ template = Template(base + """ <%def name="C()"> C_def <%self:B> <%self:A> A_body </%self:A> B_body ${caller.body()} </%self:B> </%def> <%self:C> C_body </%self:C> """) eq_( flatten_result(template.render()), "C_def B_def A_def A_body B_body C_body" ) template = Template(base + """ <%def name="C()"> C_def <%self:B> B_body ${caller.body()} <%self:A> A_body </%self:A> </%self:B> </%def> <%self:C> C_body </%self:C> """) eq_( flatten_result(template.render()), "C_def B_def B_body C_body A_def A_body" )
def test_builtins(self): t = Template(""" ${"this is <text>" | h} """) assert flatten_result(t.render()) == "this is <text>" t = Template(""" http://foo.com/arg1=${"hi! this is a string." | u} """) assert flatten_result(t.render()) == "http://foo.com/arg1=hi%21+this+is+a+string."
def test_nested_call_4(self): base = """ <%def name="A()"> A_def ${caller.body()} </%def> <%def name="B()"> B_def ${caller.body()} </%def> """ template = Template(base + """ <%def name="C()"> C_def <%self:B> <%self:A> A_body </%self:A> B_body ${caller.body()} </%self:B> </%def> <%self:C> C_body </%self:C> """) eq_(flatten_result(template.render()), "C_def B_def A_def A_body B_body C_body") template = Template(base + """ <%def name="C()"> C_def <%self:B> B_body ${caller.body()} <%self:A> A_body </%self:A> </%self:B> </%def> <%self:C> C_body </%self:C> """) eq_(flatten_result(template.render()), "C_def B_def B_body C_body A_def A_body")
def test_basic(self): template = Template(""" <%page args="x, y, z=7"/> this is page, ${x}, ${y}, ${z} """) assert flatten_result(template.render(x=5, y=10)) == "this is page, 5, 10, 7" assert flatten_result(template.render(x=5, y=10, z=32)) == "this is page, 5, 10, 32" try: template.render(y=10) assert False except TypeError, e: assert True
def test_bytestring_passthru(self): lookup = TemplateLookup(directories=['./test_htdocs'], default_filters=[], disable_unicode=True) template = lookup.get_template('/chs_utf8.html') self.assertEquals(flatten_result(template.render(name='毛泽东')), '毛泽东 是 新中国的主席<br/> Welcome 你 to 北京. Welcome 你 to 北京.') lookup = TemplateLookup(directories=['./test_htdocs'], disable_unicode=True) template = lookup.get_template('/chs_utf8.html') self.assertEquals(flatten_result(template.render(name='毛泽东')), '毛泽东 是 新中国的主席<br/> Welcome 你 to 北京. Welcome 你 to 北京.') self.assertRaises(UnicodeDecodeError, template.render_unicode, name='毛泽东') template = Template("""${'Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »'}""", disable_unicode=True, input_encoding='utf-8') assert template.render() == """Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »""" template = Template("""${'Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »'}""", input_encoding='utf8', output_encoding='utf8', disable_unicode=False, default_filters=[]) self.assertRaises(UnicodeDecodeError, template.render) # raises because expression contains an encoded bytestring which cannot be decoded
def test_outer_scope(self): t = Template(""" <%def name="a()"> a: x is ${x} </%def> <%def name="b()"> <%def name="c()"> <% x = 10 %> c. x is ${x}. ${a()} </%def> b. ${c()} </%def> ${b()} x is ${x} """) eq_( flatten_result(t.render(x=5)), "b. c. x is 10. a: x is 5 x is 5" )
def test_inter_def(self): """test defs calling each other""" template = Template(""" ${b()} <%def name="a()">\ im a </%def> <%def name="b()"> im b and heres a: ${a()} </%def> <%def name="c()"> im c </%def> """) # check that "a" is declared in "b", but not in "c" assert "a" not in template.module.render_c.func_code.co_varnames assert "a" in template.module.render_b.func_code.co_varnames # then test output eq_( flatten_result(template.render()), "im b and heres a: im a" )
def test_scope_eight(self): """test that the initial context counts as 'enclosing' scope, for nested defs""" t = Template(""" <%def name="enclosing()"> <%def name="a()"> a: x is ${x} </%def> <%def name="b()"> <% x = 10 %> b. x is ${x}. ${a()} </%def> ${b()} </%def> ${enclosing()} """) eq_( flatten_result(t.render(x=5)), "b. x is 10. a: x is 5" )
def test_scope_four(self): """test that variables are pulled from 'enclosing' scope before context.""" t = Template(""" <% x = 5 %> <%def name="a()"> this is a. x is ${x}. </%def> <%def name="b()"> <% x = 9 %> this is b. x is ${x}. calling a. ${a()} </%def> ${b()} """) eq_( flatten_result(t.render()), "this is b. x is 9. calling a. this is a. x is 5." )
def test_context(self): """test that namespace callables get access to the current context""" collection = lookup.TemplateLookup() collection.put_string( 'main.html', """ <%namespace name="comp" file="defs.html"/> this is main. ${comp.def1()} ${comp.def2("there")} """) collection.put_string( 'defs.html', """ <%def name="def1()"> def1: x is ${x} </%def> <%def name="def2(x)"> def2: x is ${x} </%def> """) assert flatten_result( collection.get_template('main.html').render(x="context x") ) == "this is main. def1: x is context x def2: x is there"
def test_scope_five(self): """test that variables are pulled from 'enclosing' scope before context.""" # same as test four, but adds a scope around it. t = Template(""" <%def name="enclosing()"> <% x = 5 %> <%def name="a()"> this is a. x is ${x}. </%def> <%def name="b()"> <% x = 9 %> this is b. x is ${x}. calling a. ${a()} </%def> ${b()} </%def> ${enclosing()} """) assert flatten_result( t.render()) == "this is b. x is 9. calling a. this is a. x is 5."
def test_scope_five(self): """test that variables are pulled from 'enclosing' scope before context.""" # same as test four, but adds a scope around it. t = Template(""" <%def name="enclosing()"> <% x = 5 %> <%def name="a()"> this is a. x is ${x}. </%def> <%def name="b()"> <% x = 9 %> this is b. x is ${x}. calling a. ${a()} </%def> ${b()} </%def> ${enclosing()} """) assert flatten_result(t.render()) == "this is b. x is 9. calling a. this is a. x is 5."
def test_scope_ten(self): t = Template(""" <%def name="a()"> <%def name="b()"> <% y = 19 %> b/c: ${c()} b/y: ${y} </%def> <%def name="c()"> c/y: ${y} </%def> <% # we assign to "y". but the 'enclosing scope' of "b" and "c" is from the "y" on the outside y = 10 %> a/y: ${y} a/b: ${b()} </%def> <% y = 7 %> main/a: ${a()} main/y: ${y} """) assert flatten_result(t.render()) == "main/a: a/y: 10 a/b: b/c: c/y: 10 b/y: 19 main/y: 7"
def test_quoting(self): t = Template(""" foo ${bar | h} """) eq_(flatten_result(t.render(bar="<'some bar'>")), "foo <'some bar'>")
def test_overload(self): collection = lookup.TemplateLookup() collection.put_string('main.html', """ <%namespace name="comp" file="defs.html"> <%def name="def1(x, y)"> overridden def1 ${x}, ${y} </%def> </%namespace> this is main. ${comp.def1("hi", "there")} ${comp.def2("there")} """) collection.put_string('defs.html', """ <%def name="def1(s)"> def1: ${s} </%def> <%def name="def2(x)"> def2: ${x} </%def> """) assert flatten_result(collection.get_template('main.html').render()) == "this is main. overridden def1 hi, there def2: there"
def test_crlf(self): template = open(self._file_path("crlf.html"), 'rb').read() nodes = Lexer(template).parse() self._compare( nodes, TemplateNode({}, [ Text(u'<html>\r\n\r\n', (1, 1)), PageTag(u'page', {u'args': u"a=['foo',\n 'bar']"}, (3, 1), []), Text(u'\r\n\r\nlike the name says.\r\n\r\n', (4, 26)), ControlLine(u'for', u'for x in [1,2,3]:', False, (8, 1)), Text(u' ', (9, 1)), Expression(u'x', [], (9, 9)), ControlLine(u'for', u'endfor', True, (10, 1)), Text(u'\r\n', (11, 1)), Expression( u"trumpeter == 'Miles' and " "trumpeter or \\\n 'Dizzy'", [], (12, 1)), Text(u'\r\n\r\n', (13, 15)), DefTag(u'def', {u'name': u'hi()'}, (15, 1), [Text(u'\r\n hi!\r\n', (15, 19))]), Text(u'\r\n\r\n</html>\r\n', (17, 8)) ])) assert flatten_result(Template(template).render()) \ == """<html> like the name says. 1 2 3 Dizzy </html>"""
def test_scope_ten(self): t = Template(""" <%def name="a()"> <%def name="b()"> <% y = 19 %> b/c: ${c()} b/y: ${y} </%def> <%def name="c()"> c/y: ${y} </%def> <% # we assign to "y". but the 'enclosing scope' of "b" and "c" is from the "y" on the outside y = 10 %> a/y: ${y} a/b: ${b()} </%def> <% y = 7 %> main/a: ${a()} main/y: ${y} """) assert flatten_result(t.render( )) == "main/a: a/y: 10 a/b: b/c: c/y: 10 b/y: 19 main/y: 7"
def test_crlf(self): template = open(self._file_path("crlf.html"), 'rb').read() nodes = Lexer(template).parse() self._compare( nodes, TemplateNode({}, [ Text(u'<html>\r\n\r\n', (1, 1)), PageTag(u'page', { u'args': u"a=['foo',\n 'bar']" }, (3, 1), []), Text(u'\r\n\r\nlike the name says.\r\n\r\n', (4, 26)), ControlLine(u'for', u'for x in [1,2,3]:', False, (8, 1)), Text(u' ', (9, 1)), Expression(u'x', [], (9, 9)), ControlLine(u'for', u'endfor', True, (10, 1)), Text(u'\r\n', (11, 1)), Expression(u"trumpeter == 'Miles' and " "trumpeter or \\\n 'Dizzy'", [], (12, 1)), Text(u'\r\n\r\n', (13, 15)), DefTag(u'def', {u'name': u'hi()'}, (15, 1), [ Text(u'\r\n hi!\r\n', (15, 19))]), Text(u'\r\n\r\n</html>\r\n', (17, 8)) ]) ) assert flatten_result(Template(template).render()) \ == """<html> like the name says. 1 2 3 Dizzy </html>"""
def test_with_context(self): template = Template(""" <%page args="x, y, z=7"/> this is page, ${x}, ${y}, ${z}, ${w} """) #print template.code assert flatten_result(template.render(x=5, y=10, w=17)) == "this is page, 5, 10, 7, 17"
def test_canuse_builtin_names(self): template = Template(""" exception: ${Exception} id: ${id} """) assert flatten_result( template.render(id='some id', Exception='some exception') ) == "exception: some exception id: some id"
def test_basic(self): t = Template(""" ${x | myfilter} """) assert flatten_result( t.render(x="this is x", myfilter=lambda t: "MYFILTER->%s<-MYFILTER" % t) ) == "MYFILTER->this is x<-MYFILTER"
def test_unicode_literal_in_def(self): template = Template(u"""## -*- coding: utf-8 -*- <%def name="bello(foo, bar)"> Foo: ${ foo } Bar: ${ bar } </%def> <%call expr="bello(foo=u'árvíztűrő tükörfúrógép', bar=u'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')"> </%call>""".encode('utf-8')) assert flatten_result(template.render_unicode()) == u"""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP""" template = Template(u"""## -*- coding: utf-8 -*- <%def name="hello(foo=u'árvíztűrő tükörfúrógép', bar=u'ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP')"> Foo: ${ foo } Bar: ${ bar } </%def> ${ hello() }""".encode('utf-8')) assert flatten_result(template.render_unicode()) == u"""Foo: árvíztűrő tükörfúrógép Bar: ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP"""
def test_unbuffered_def(self): t = Template(""" <%def name="foo()" buffered="False"> this is foo </%def> ${"hi->" + foo() + "<-hi"} """) assert flatten_result(t.render()) == "this is foo hi-><-hi"
def test_expr(self): """test filters that are themselves expressions""" t = Template(""" ${x | myfilter(y)} """) def myfilter(y): return lambda x: "MYFILTER->%s<-%s" % (x, y) assert flatten_result(t.render(x="this is x", myfilter=myfilter, y="this is y")) == "MYFILTER->this is x<-this is y"
def test_quoting_non_unicode(self): t = Template(""" foo ${bar | h} """, disable_unicode=True) eq_(flatten_result(t.render(bar="<'привет'>")), "foo <'привет'>")
def test_capture(self): t = Template(""" <%def name="foo()" buffered="False"> this is foo </%def> ${"hi->" + capture(foo) + "<-hi"} """) assert flatten_result(t.render()) == "hi-> this is foo <-hi"
def test_overrides_builtins(self): template = Template(""" <%page args="id"/> this is page, id is ${id} """) assert flatten_result(template.render(id="im the id")) == "this is page, id is im the id"
def test_def(self): t = Template(""" <%def name="foo()" filter="myfilter"> this is foo </%def> ${foo()} """) assert flatten_result(t.render(x="this is x", myfilter=lambda t: "MYFILTER->%s<-MYFILTER" % t)) == "MYFILTER-> this is foo <-MYFILTER"
def test_toplevel(self): """test calling a def from the top level""" template = Template(""" this is the body <%def name="a()"> this is a </%def> <%def name="b(x, y)"> this is b, ${x} ${y} </%def> """, output_encoding='utf-8') assert flatten_result(template.get_def("a").render()) == "this is a" assert flatten_result(template.get_def("b").render(x=10, y=15)) == "this is b, 10 15" assert flatten_result(template.get_def("body").render()) == "this is the body"
def test_quoting(self): t = Template(""" foo ${bar | h} """) eq_( flatten_result(t.render(bar="<'some bar'>")), "foo <'some bar'>" )
def test_quoting_non_unicode(self): t = Template(""" foo ${bar | h} """, disable_unicode=True) eq_( flatten_result(t.render(bar="<'привет'>")), "foo <'привет'>" )
def test_builtin_names_dont_clobber_defaults_in_includes(self): lookup = TemplateLookup() lookup.put_string("test.mako", """ <%include file="test1.mako"/> """) lookup.put_string("test1.mako", """ <%page args="id='foo'"/> ${id} """) for template in ("test.mako", "test1.mako"): assert flatten_result(lookup.get_template(template).render()) == "foo" assert flatten_result(lookup.get_template(template).render(id=5)) == "5" assert flatten_result(lookup.get_template(template).render(id=id)) == "<built-in function id>"
def test_multiline_control(self): t = Template(""" % for x in \\ [y for y in [1,2,3]]: ${x} % endfor """) #print t.code assert flatten_result(t.render()) == "1 2 3"
def test_dict_locals(self): template = Template(""" <% dict = "this is dict" locals = "this is locals" %> dict: ${dict} locals: ${locals} """) assert flatten_result(template.render()) == "dict: this is dict locals: this is locals"
def test_unicode_file_lookup(self): lookup = TemplateLookup(directories=[template_base], output_encoding='utf-8', default_filters=['decode.utf8']) if util.py3k: template = lookup.get_template('/chs_unicode_py3k.html') else: template = lookup.get_template('/chs_unicode.html') eq_(flatten_result(template.render_unicode(name='毛泽东')), u'毛泽东 是 新中国的主席<br/> Welcome 你 to 北京.')
def test_localargs(self): lookup = TemplateLookup() lookup.put_string("a", """ this is a <%include file="b" args="a=a,b=b,c=5"/> """) lookup.put_string("b", """ <%page args="a,b,c"/> this is b. ${a}, ${b}, ${c} """) assert flatten_result(lookup.get_template("a").render(a=7,b=8)) == "this is a this is b. 7, 8, 5"
def test_basic(self): lookup = TemplateLookup() lookup.put_string("a", """ this is a <%include file="b" args="a=3,b=4,c=5"/> """) lookup.put_string("b", """ <%page args="a,b,c"/> this is b. ${a}, ${b}, ${c} """) assert flatten_result(lookup.get_template("a").render()) == "this is a this is b. 3, 4, 5"
def test_nested_with_args(self): t = Template(""" ${a()} <%def name="a()"> <%def name="b(x, y=2)"> b x is ${x} y is ${y} </%def> a ${b(5)} </%def> """) assert flatten_result(t.render()) == "a b x is 5 y is 2"
def test_module_imports_2(self): collection = lookup.TemplateLookup() collection.put_string('main.html', """ <%namespace import="foo1, foo2" module="test.foo.test_ns"/> this is main. ${foo1()} ${foo2("hi")} """) assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
def test_module(self): collection = lookup.TemplateLookup() collection.put_string('main.html', """ <%namespace name="comp" module="test.sample_module_namespace"/> this is main. ${comp.foo1()} ${comp.foo2("hi")} """) assert flatten_result(collection.get_template('main.html').render()) == "this is main. this is foo1. this is foo2, x is hi"
def test_include_withargs(self): lookup = TemplateLookup() lookup.put_string("a", """ this is a <%include file="${i}" args="c=5, **context.kwargs"/> """) lookup.put_string("b", """ <%page args="a,b,c"/> this is b. ${a}, ${b}, ${c} """) assert flatten_result(lookup.get_template("a").render(a=7,b=8,i='b')) == "this is a this is b. 7, 8, 5"