def _do_test_traceback(self, utf8, memory, syntax): if memory: if syntax: source = u('## coding: utf-8\n<% print "m’a réveillé. '\ 'Elle disait: « S’il vous plaît… dessine-moi un mouton! » %>') else: source = u('## coding: utf-8\n<% print u"m’a réveillé. '\ 'Elle disait: « S’il vous plaît… dessine-moi un mouton! »" + str(5/0) %>') if utf8: source = source.encode('utf-8') else: source = source templateargs = {'text': source} else: if syntax: filename = 'unicode_syntax_error.html' else: filename = 'unicode_runtime_error.html' source = util.read_file(self._file_path(filename), 'rb') if not utf8: source = source.decode('utf-8') templateargs = {'filename': self._file_path(filename)} try: template = Template(**templateargs) if not syntax: template.render_unicode() assert False except Exception: tback = errors.RichTraceback() if utf8: assert tback.source == source.decode('utf-8') else: assert tback.source == source
def test_html_error_template(self): """test the html_error_template""" code = """ % i = 0 """ try: template = Template(code) template.render_unicode() assert False except errors.CompileException: html_error = errors.html_error_template().render_unicode() assert ("CompileException: Fragment 'i = 0' is not " "a partial control statement at line: 2 char: 1") in html_error assert '<style>' in html_error html_error_stripped = html_error.strip() assert html_error_stripped.startswith('<html>') assert html_error_stripped.endswith('</html>') not_full = errors.html_error_template().\ render_unicode(full=False) assert '<html>' not in not_full assert '<style>' in not_full no_css = errors.html_error_template().\ render_unicode(css=False) assert '<style>' not in no_css else: assert False, ("This function should trigger a CompileException, " "but didn't")
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> """) self._do_test(template.get_def("a"), "this is a", filters=flatten_result) self._do_test(template.get_def("b"), "this is b, 10 15", template_args={'x': 10, 'y': 15}, filters=flatten_result) self._do_test(template.get_def("body"), "this is the body", filters=flatten_result) # test that args outside of the dict can be used self._do_test(template.get_def("a"), "this is a", filters=flatten_result, template_args={'q': 5, 'zq': 'test'})
def test_fileargs_pagetag(self): t = Template(""" <%%page cache_dir='%s' cache_type='dbm'/> <%%! callcount = [0] %%> <%%def name="foo()" cached="True"> this is foo <%% callcount[0] += 1 %%> </%%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """ % module_base) m = self._install_mock_cache(t) assert result_lines(t.render()) == [ 'this is foo', 'this is foo', 'this is foo', 'callcount: [1]', ] eq_(m.kwargs, {'dir': module_base, 'type': 'dbm'})
def test_strict(self): t = Template(""" % if x is UNDEFINED: undefined % else: x: ${x} % endif """, strict_undefined=True) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises( NameError, t.render, y=12 ) l = TemplateLookup(strict_undefined=True) l.put_string("a", "some template") l.put_string("b", """ <%namespace name='a' file='a' import='*'/> % if x is UNDEFINED: undefined % else: x: ${x} % endif """) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises( NameError, t.render, y=12 )
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_def(self): t = Template(""" <%! callcount = [0] %> <%def name="foo()" cached="True"> this is foo <% callcount[0] += 1 %> </%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """) m = self._install_mock_cache(t) assert result_lines(t.render()) == [ 'this is foo', 'this is foo', 'this is foo', 'callcount: [1]', ] assert m.kwargs == {}
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_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_strict(self): t = Template(""" % if x is UNDEFINED: undefined % else: x: ${x} % endif """, strict_undefined=True) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises(NameError, t.render, y=12) l = TemplateLookup(strict_undefined=True) l.put_string("a", "some template") l.put_string( "b", """ <%namespace name='a' file='a' import='*'/> % if x is UNDEFINED: undefined % else: x: ${x} % endif """) assert result_lines(t.render(x=12)) == ['x: 12'] assert_raises(NameError, t.render, y=12)
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_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_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_ccall_caller(self): t = Template(""" <%def name="outer_func()"> OUTER BEGIN <%call expr="caller.inner_func()"> INNER CALL </%call> OUTER END </%def> <%call expr="outer_func()"> <%def name="inner_func()"> INNER BEGIN ${caller.body()} INNER END </%def> </%call> """) #print t.code assert result_lines(t.render()) == [ "OUTER BEGIN", "INNER BEGIN", "INNER CALL", "INNER END", "OUTER END", ]
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_def_operations(self): """test get/list/has def""" template = Template(""" this is the body <%def name="a()"> this is a </%def> <%def name="b(x, y)"> this is b, ${x} ${y} </%def> """) assert template.get_def("a") assert template.get_def("b") assert_raises(AttributeError, template.get_def, ("c") ) assert template.has_def("a") assert template.has_def("b") assert not template.has_def("c") defs = template.list_defs() assert "a" in defs assert "b" in defs assert "body" in defs assert "c" not in defs
def test_new_syntax(self): """test foo:bar syntax, including multiline args and expression eval.""" # note the trailing whitespace in the bottom ${} expr, need to strip # that off < python 2.7 t = Template(""" <%def name="foo(x, y, q, z)"> ${x} ${y} ${q} ${",".join("%s->%s" % (a, b) for a, b in z)} </%def> <%self:foo x="this is x" y="${'some ' + 'y'}" q=" this is q" z="${[ (1, 2), (3, 4), (5, 6) ] }"/> """) eq_( result_lines(t.render()), ['this is x', 'some y', 'this', 'is', 'q', '1->2,3->4,5->6'] )
def test_basic(self): t = Template(""" <%! cached = None %> <%def name="foo()"> <% global cached if cached: return "cached: " + cached __M_writer = context._push_writer() %> this is foo <% buf, __M_writer = context._pop_buffer_and_writer() cached = buf.getvalue() return cached %> </%def> ${foo()} ${foo()} """) assert result_lines(t.render()) == [ "this is foo", "cached:", "this is foo" ]
def test_chained_call_in_nested(self): t = Template(""" <%def name="embedded()"> <%def name="a()"> this is a. <%call expr="b()"> this is a's ccall. heres my body: ${caller.body()} </%call> </%def> <%def name="b()"> this is b. heres my body: ${caller.body()} whats in the body's caller's body ? ${context.caller_stack[-2].body()} </%def> <%call expr="a()"> heres the main templ call </%call> </%def> ${embedded()} """) #print t.code #print result_lines(t.render()) assert result_lines(t.render()) == [ 'this is a.', 'this is b. heres my body:', "this is a's ccall. heres my body:", 'heres the main templ call', "whats in the body's caller's body ?", 'heres the main templ call' ]
def test_compound_call(self): t = Template(""" <%def name="bar()"> this is bar </%def> <%def name="comp1()"> this comp1 should not be called </%def> <%def name="foo()"> foo calling comp1: ${caller.comp1(x=5)} foo calling body: ${caller.body()} </%def> <%call expr="foo()"> <%def name="comp1(x)"> this is comp1, ${x} </%def> this is the body, ${comp1(6)} </%call> ${bar()} """) assert result_lines(t.render()) == ['foo calling comp1:', 'this is comp1, 5', 'foo calling body:', 'this is the body,', 'this is comp1, 6', 'this is bar']
def test_nested_call(self): """test %calls that are nested inside each other""" t = Template(""" <%def name="foo()"> ${caller.body(x=10)} </%def> x is ${x} <%def name="bar()"> bar: ${caller.body()} </%def> <%call expr="foo()" args="x"> this is foo body: ${x} <%call expr="bar()"> this is bar body: ${x} </%call> </%call> """) assert result_lines(t.render(x=5)) == [ "x is 5", "this is foo body: 10", "bar:", "this is bar body: 10" ]
def test_stack_pop(self): t = Template(""" <%def name="links()" buffered="True"> Some links </%def> <%def name="wrapper(links)"> <h1>${caller.body()}</h1> ${links} </%def> ## links() pushes a stack frame on. when complete, ## 'nextcaller' must be restored <%call expr="wrapper(links())"> Some title </%call> """) assert result_lines(t.render()) == [ "<h1>", "Some title", "</h1>", "Some links" ]
def test_chained_call(self): """test %calls that are chained through their targets""" t = Template(""" <%def name="a()"> this is a. <%call expr="b()"> this is a's ccall. heres my body: ${caller.body()} </%call> </%def> <%def name="b()"> this is b. heres my body: ${caller.body()} whats in the body's caller's body ? ${context.caller_stack[-2].body()} </%def> <%call expr="a()"> heres the main templ call </%call> """) assert result_lines(t.render()) == [ 'this is a.', 'this is b. heres my body:', "this is a's ccall. heres my body:", 'heres the main templ call', "whats in the body's caller's body ?", 'heres the main templ call' ]
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_call_in_nested_2(self): t = Template(""" <%def name="a()"> <%def name="d()"> not this d </%def> this is a ${b()} <%def name="b()"> <%def name="d()"> not this d either </%def> this is b <%call expr="c()"> <%def name="d()"> this is d </%def> this is the body in b's call </%call> </%def> <%def name="c()"> this is c: ${caller.body()} the embedded "d" is: ${caller.d()} </%def> </%def> ${a()} """) assert result_lines(t.render()) == ['this is a', 'this is b', 'this is c:', "this is the body in b's call", 'the embedded "d" is:', 'this is d']
def _do_test_traceback(self, utf8, memory, syntax): if memory: if syntax: source = u('## coding: utf-8\n<% print "m’a réveillé. '\ 'Elle disait: « S’il vous plaît… dessine-moi un mouton! » %>') else: source = u('## coding: utf-8\n<% print u"m’a réveillé. '\ 'Elle disait: « S’il vous plaît… dessine-moi un mouton! »" + str(5/0) %>') if utf8: source = source.encode('utf-8') else: source = source templateargs = {'text': source} else: if syntax: filename = 'unicode_syntax_error.html' else: filename = 'unicode_runtime_error.html' source = util.read_file(self._file_path(filename), 'rb') if not utf8: source = source.decode('utf-8') templateargs = {'filename': self._file_path(filename)} try: template = Template(**templateargs) if not syntax: template.render_unicode() assert False except Exception: tback = errors.RichTraceback() if utf8: assert tback.source == source.decode('utf-8') else: assert tback.source == source
def test_interpret_expression_from_arg_two(self): """test that cache_key=${foo} gets its value from the 'foo' argument regardless of it being passed from the context. This is here testing that there's no change to existing behavior before and after #191. """ t = Template(""" <%def name="layout(foo)" cached="True" cache_key="${foo}"> foo: ${value} </%def> ${layout(3)} """, cache_impl="plain") eq_( result_lines(t.render(foo='foo', value=1)), ["foo: 1"] ) eq_( result_lines(t.render(foo='bar', value=2)), ["foo: 1"] )
def test_fileargs_pagetag(self): t = Template(""" <%%page cache_dir='%s' cache_type='dbm'/> <%%! callcount = [0] %%> <%%def name="foo()" cached="True"> this is foo <%% callcount[0] += 1 %%> </%%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """ % module_base) m = self._install_mock_cache(t) assert result_lines(t.render()) == [ 'this is foo', 'this is foo', 'this is foo', 'callcount: [1]', ] eq_(m.kwargs, {'dir': module_base, 'type': 'dbm'})
def test_def(self): t = Template(""" <%! callcount = [0] %> <%def name="foo()" cached="True"> this is foo <% callcount[0] += 1 %> </%def> ${foo()} ${foo()} ${foo()} callcount: ${callcount} """) m = self._install_mock_cache(t) assert result_lines(t.render()) == [ 'this is foo', 'this is foo', 'this is foo', 'callcount: [1]', ] assert m.kwargs == {}
def test_module_roundtrip(self): lookup = TemplateLookup() template = Template(""" <%inherit file="base.html"/> % for x in range(5): ${x} % endfor """, lookup=lookup) base = Template(""" This is base. ${self.body()} """, lookup=lookup) lookup.put_template("base.html", base) lookup.put_template("template.html", template) assert result_lines(template.render()) == [ "This is base.", "0", "1", "2", "3", "4" ] lookup = TemplateLookup() template = ModuleTemplate(template.module, lookup=lookup) base = ModuleTemplate(base.module, lookup=lookup) lookup.put_template("base.html", base) lookup.put_template("template.html", template) assert result_lines(template.render()) == [ "This is base.", "0", "1", "2", "3", "4" ]
def test_nested_call_2(self): t = Template(""" x is ${x} <%def name="foo()"> ${caller.foosub(x=10)} </%def> <%def name="bar()"> bar: ${caller.barsub()} </%def> <%call expr="foo()"> <%def name="foosub(x)"> this is foo body: ${x} <%call expr="bar()"> <%def name="barsub()"> this is bar body: ${x} </%def> </%call> </%def> </%call> """) assert result_lines(t.render(x=5)) == [ "x is 5", "this is foo body: 10", "bar:", "this is bar body: 10" ]
def test_quoting(self): t = Template(""" foo ${bar | h} """) eq_(flatten_result(t.render(bar="<'some bar'>")), "foo <'some bar'>")
def test_no_loop(self): template = Template("""% for x in 'spam': ${x} % endfor""") code = template.code assert not re.match(r"loop = __M_loop._enter\(:", code), "No need to "\ "generate a loop context if the loop variable wasn't accessed" print(template.render())
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_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_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_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_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_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_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_encode_filter(self): t = Template("""# coding: utf-8 some stuff.... ${x} """, default_filters=['decode.utf8']) eq_( t.render_unicode(x=u("voix m’a réveillé")).strip(), u("some stuff.... voix m’a réveillé"))
def test_encode_filter_non_str(self): t = Template("""# coding: utf-8 some stuff.... ${x} """, default_filters=['decode.utf8']) eq_( t.render_unicode(x=3).strip(), u("some stuff.... 3") )
def test_import_2(self): t = Template(""" trim this string: ${" some string to trim " | filters.trim} continue\ """, imports=["from choco import filters"]) #print t.code assert t.render().strip( ) == "trim this string: some string to trim continue"
def test_def_blankargs(self): template = Template(""" <%def name="mycomp()"> hello mycomp ${variable} </%def> ${mycomp()}""") eq_(template.render(variable='hi').strip(), "hello mycomp hi")
def test_no_loop(self): template = Template("""% for x in 'spam': ${x} % endfor""") code = template.code assert not re.match(r"loop = __M_loop._enter\(:", code), "No need to "\ "generate a loop context if the loop variable wasn't accessed" print(template.render())
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_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"