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" assert_raises(TypeError, template.render, y=10)
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_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_nine(self): """test that 'enclosing scope' doesnt get exported to other templates""" l = lookup.TemplateLookup() l.put_string( "main", """ <% x = 5 %> this is main. <%include file="secondary"/> """, ) l.put_string( "secondary", """ this is secondary. x is ${x} """, ) eq_( flatten_result(l.get_template("main").render(x=2)), "this is main. this is secondary. x is 2", )
def test_unicode_file_lookup(self): lookup = TemplateLookup(directories=[template_base], output_encoding="utf-8", default_filters=["decode.utf8"]) if compat.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_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_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_import_calledfromdef(self): l = lookup.TemplateLookup() l.put_string( "a", """ <%def name="table()"> im table </%def> """, ) l.put_string( "b", """ <%namespace file="a" import="table"/> <% def table2(): table() return "" %> ${table2()} """, ) t = l.get_template("b") assert flatten_result(t.render()) == "im table"
def test_template(self): collection = lookup.TemplateLookup() collection.put_string( "main.html", """ <%namespace name="comp" file="defs.html"/> this is main. ${comp.def1("hi")} ${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. def1: hi def2: there")
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_quoting(self): t = Template(""" foo ${bar | h} """) eq_(flatten_result(t.render(bar="<'some bar'>")), "foo <'some bar'>")
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_crlf(self): template = util.read_file(self._file_path("crlf.html")) nodes = Lexer(template).parse() self._compare( nodes, TemplateNode({}, [ Text('<html>\r\n\r\n', (1, 1)), PageTag('page', { 'args': "a=['foo',\n 'bar']" }, (3, 1), []), Text('\r\n\r\nlike the name says.\r\n\r\n', (4, 26)), ControlLine('for', 'for x in [1,2,3]:', False, (8, 1)), Text(' ', (9, 1)), Expression('x', [], (9, 9)), ControlLine('for', 'endfor', True, (10, 1)), Text('\r\n', (11, 1)), Expression("trumpeter == 'Miles' and " "trumpeter or \\\n 'Dizzy'", [], (12, 1)), Text('\r\n\r\n', (13, 15)), DefTag('def', {'name': 'hi()'}, (15, 1), [ Text('\r\n hi!\r\n', (15, 19))]), Text('\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_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" if compat.py3k: assert "a" not in template.module.render_c.__code__.co_varnames assert "a" in template.module.render_b.__code__.co_varnames else: 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_convert_str(self): """test that string conversion happens in expressions before sending to filters""" t = Template(""" ${x | trim} """) assert flatten_result(t.render(x=5)) == "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} """) eq_(flatten_result(t.render()), "main/a: a/y: 10 a/b: b/c: c/y: 10 b/y: 19 main/y: 7")
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()} """) eq_(flatten_result(t.render()), "this is b. x is 9. calling a. this is a. 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" if compat.py3k: assert "a" not in template.module.render_c.__code__.co_varnames assert "a" in template.module.render_b.__code__.co_varnames else: assert "a" not in template.module.render_c.__code__.co_varnames assert "a" in template.module.render_b.__code__.co_varnames # then test output eq_(flatten_result(template.render()), "im b and heres a: im a")
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()} """) eq_( flatten_result(t.render()), "this is b. x is 9. calling a. this is a. x is 5." )
def test_url_escaping(self): t = Template(""" http://example.com/?bar=${bar | u}&v=1 """) eq_(flatten_result(t.render(bar=u"酒吧bar")), "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1")
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} """) eq_( flatten_result(t.render()), "main/a: a/y: 10 a/b: b/c: c/y: 10 b/y: 19 main/y: 7" )
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_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_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_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_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_quoting_non_unicode(self): t = Template(""" foo ${bar | h} """, disable_unicode=True, output_encoding=None) eq_(flatten_result(t.render(bar="<'привет'>")), "foo <'привет'>")
def test_url_escaping_non_unicode(self): t = Template(""" http://example.com/?bar=${bar | u}&v=1 """, disable_unicode=True, output_encoding=None) eq_(flatten_result(t.render(bar="酒吧bar")), "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1")
def test_url_escaping(self): t = Template(""" http://example.com/?bar=${bar | u}&v=1 """) eq_( flatten_result(t.render(bar=u"酒吧bar")), "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1" )
def test_quoting(self): t = Template(""" foo ${bar | h} """) eq_( flatten_result(t.render(bar="<'some bar'>")), "foo <'some bar'>" )
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_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_url_escaping_non_unicode(self): t = Template(""" http://example.com/?bar=${bar | u}&v=1 """, disable_unicode=True, output_encoding=None) eq_( flatten_result(t.render(bar="酒吧bar")), "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1" )
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 compat.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_quoting_non_unicode(self): t = Template(""" foo ${bar | h} """, disable_unicode=True, output_encoding=None) eq_( flatten_result(t.render(bar="<'привет'>")), "foo <'привет'>" )
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"
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_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_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_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_old_comments(self): t = Template(""" im a template # old style comment # more old style comment ## new style comment - # not a comment - ## not a comment """, preprocessor=convert_comments) assert flatten_result(t.render()) == "im a template - # not a comment - ## not a comment"