diff options
Diffstat (limited to 'test/test_def.py')
-rw-r--r-- | test/test_def.py | 371 |
1 files changed, 201 insertions, 170 deletions
diff --git a/test/test_def.py b/test/test_def.py index 19142c8..99b929d 100644 --- a/test/test_def.py +++ b/test/test_def.py @@ -1,13 +1,18 @@ -from mako.template import Template +from mako import compat from mako import lookup +from mako.template import Template +from test import assert_raises +from test import eq_ +from test import requires_python_3 from test import TemplateTest -from test.util import flatten_result, result_lines -from test import eq_, assert_raises, requires_python_3 -from mako import compat +from test.util import flatten_result +from test.util import result_lines + class DefTest(TemplateTest): def test_def_noargs(self): - template = Template(""" + template = Template( + """ ${mycomp()} @@ -15,52 +20,55 @@ class DefTest(TemplateTest): hello mycomp ${variable} </%def> - """) - eq_( - template.render(variable='hi').strip(), - """hello mycomp hi""" + """ ) + eq_(template.render(variable="hi").strip(), """hello mycomp hi""") def test_def_blankargs(self): - template = Template(""" + template = Template( + """ <%def name="mycomp()"> hello mycomp ${variable} </%def> - ${mycomp()}""") - eq_( - template.render(variable='hi').strip(), - "hello mycomp hi" + ${mycomp()}""" ) + eq_(template.render(variable="hi").strip(), "hello mycomp hi") def test_def_args(self): - template = Template(""" + template = Template( + """ <%def name="mycomp(a, b)"> hello mycomp ${variable}, ${a}, ${b} </%def> - ${mycomp(5, 6)}""") + ${mycomp(5, 6)}""" + ) eq_( - template.render(variable='hi', a=5, b=6).strip(), - """hello mycomp hi, 5, 6""" + template.render(variable="hi", a=5, b=6).strip(), + """hello mycomp hi, 5, 6""", ) @requires_python_3 def test_def_py3k_args(self): - template = Template(""" + template = Template( + """ <%def name="kwonly(one, two, *three, four, five=5, **six)"> - look at all these args: ${one} ${two} ${three[0]} ${four} ${five} ${six['seven']} + look at all these args: ${one} ${two} ${three[0]} """ + """${four} ${five} ${six['seven']} </%def> - ${kwonly('one', 'two', 'three', four='four', seven='seven')}""") + ${kwonly('one', 'two', 'three', four='four', seven='seven')}""" + ) eq_( template.render(one=1, two=2, three=(3,), six=6).strip(), - """look at all these args: one two three four 5 seven""" + """look at all these args: one two three four 5 seven""", ) def test_inter_def(self): """test defs calling each other""" - template = Template(""" + template = Template( + """ ${b()} <%def name="a()">\ @@ -75,7 +83,8 @@ class DefTest(TemplateTest): <%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 @@ -85,15 +94,13 @@ class DefTest(TemplateTest): 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" - ) + eq_(flatten_result(template.render()), "im b and heres a: im a") def test_toplevel(self): """test calling a def from the top level""" - template = Template(""" + template = Template( + """ this is the body @@ -105,28 +112,37 @@ class DefTest(TemplateTest): 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) + 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'}) + self._do_test( + template.get_def("a"), + "this is a", + filters=flatten_result, + template_args={"q": 5, "zq": "test"}, + ) def test_def_operations(self): """test get/list/has def""" - template = Template(""" + template = Template( + """ this is the body @@ -138,14 +154,12 @@ class DefTest(TemplateTest): this is b, ${x} ${y} </%def> - """) + """ + ) assert template.get_def("a") assert template.get_def("b") - assert_raises(AttributeError, - template.get_def, - ("c") - ) + assert_raises(AttributeError, template.get_def, ("c")) assert template.has_def("a") assert template.has_def("b") @@ -163,7 +177,8 @@ class ScopeTest(TemplateTest): scope always takes precedence over contextual scope.""" def test_scope_one(self): - self._do_memory_test(""" + self._do_memory_test( + """ <%def name="a()"> this is a, and y is ${y} </%def> @@ -179,11 +194,12 @@ class ScopeTest(TemplateTest): """, "this is a, and y is None this is a, and y is 7", filters=flatten_result, - template_args={'y': None} + template_args={"y": None}, ) def test_scope_two(self): - t = Template(""" + t = Template( + """ y is ${y} <% @@ -191,7 +207,8 @@ class ScopeTest(TemplateTest): %> y is ${y} -""") +""" + ) try: t.render(y=None) assert False @@ -201,7 +218,8 @@ class ScopeTest(TemplateTest): def test_scope_four(self): """test that variables are pulled from 'enclosing' scope before context.""" - t = Template(""" + t = Template( + """ <% x = 5 %> @@ -218,17 +236,19 @@ class ScopeTest(TemplateTest): </%def> ${b()} -""") +""" + ) eq_( flatten_result(t.render()), - "this is b. x is 9. calling a. this is a. x is 5." + "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(""" + t = Template( + """ <%def name="enclosing()"> <% x = 5 @@ -248,16 +268,18 @@ class ScopeTest(TemplateTest): ${b()} </%def> ${enclosing()} -""") +""" + ) eq_( flatten_result(t.render()), - "this is b. x is 9. calling a. this is a. x is 5." + "this is b. x is 9. calling a. this is a. x is 5.", ) def test_scope_six(self): """test that the initial context counts as 'enclosing' scope, for plain defs""" - t = Template(""" + t = Template( + """ <%def name="a()"> a: x is ${x} @@ -271,16 +293,15 @@ class ScopeTest(TemplateTest): </%def> ${b()} - """) - eq_( - flatten_result(t.render(x=5)), - "b. x is 10. a: x is 5" + """ ) + eq_(flatten_result(t.render(x=5)), "b. x is 10. a: x is 5") def test_scope_seven(self): """test that the initial context counts as 'enclosing' scope, for nested defs""" - t = Template(""" + t = Template( + """ <%def name="enclosing()"> <%def name="a()"> a: x is ${x} @@ -296,16 +317,15 @@ class ScopeTest(TemplateTest): ${b()} </%def> ${enclosing()} - """) - eq_( - flatten_result(t.render(x=5)), - "b. x is 10. a: x is 5" + """ ) + eq_(flatten_result(t.render(x=5)), "b. x is 10. a: x is 5") def test_scope_eight(self): """test that the initial context counts as 'enclosing' scope, for nested defs""" - t = Template(""" + t = Template( + """ <%def name="enclosing()"> <%def name="a()"> a: x is ${x} @@ -322,35 +342,40 @@ class ScopeTest(TemplateTest): ${b()} </%def> ${enclosing()} - """) - eq_( - flatten_result(t.render(x=5)), - "b. x is 10. a: x is 5" + """ ) + eq_(flatten_result(t.render(x=5)), "b. x is 10. a: x is 5") def test_scope_nine(self): """test that 'enclosing scope' doesnt get exported to other templates""" l = lookup.TemplateLookup() - l.put_string('main', """ + l.put_string( + "main", + """ <% x = 5 %> this is main. <%include file="secondary"/> -""") +""", + ) - l.put_string('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" + flatten_result(l.get_template("main").render(x=2)), + "this is main. this is secondary. x is 2", ) def test_scope_ten(self): - t = Template(""" + t = Template( + """ <%def name="a()"> <%def name="b()"> <% @@ -378,14 +403,16 @@ class ScopeTest(TemplateTest): %> 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" + "main/a: a/y: 10 a/b: b/c: c/y: 10 b/y: 19 main/y: 7", ) def test_scope_eleven(self): - t = Template(""" + t = Template( + """ x is ${x} <%def name="a(x)"> this is a, ${b()} @@ -395,17 +422,16 @@ class ScopeTest(TemplateTest): </%def> ${a(x=5)} -""") +""" + ) eq_( result_lines(t.render(x=10)), - [ - "x is 10", - "this is a,", - "this is b, x is 5" - ]) + ["x is 10", "this is a,", "this is b, x is 5"], + ) def test_unbound_scope(self): - t = Template(""" + t = Template( + """ <% y = 10 %> @@ -418,14 +444,13 @@ class ScopeTest(TemplateTest): y is ${y} </%def> ${a()} -""") - assert_raises( - UnboundLocalError, - t.render - ) +""" + ) + assert_raises(UnboundLocalError, t.render) def test_unbound_scope_two(self): - t = Template(""" + t = Template( + """ <%def name="enclosing()"> <% y = 10 @@ -441,7 +466,8 @@ class ScopeTest(TemplateTest): ${a()} </%def> ${enclosing()} -""") +""" + ) try: print(t.render()) assert False @@ -452,13 +478,18 @@ class ScopeTest(TemplateTest): """test that arguments passed to the body() function are accessible by top-level defs""" l = lookup.TemplateLookup() - l.put_string("base", """ + l.put_string( + "base", + """ ${next.body(x=12)} - """) + """, + ) - l.put_string("main", """ + l.put_string( + "main", + """ <%inherit file="base"/> <%page args="x"/> this is main. x is ${x} @@ -468,28 +499,28 @@ class ScopeTest(TemplateTest): <%def name="a(**args)"> this is a, x is ${x} </%def> - """) + """, + ) # test via inheritance eq_( result_lines(l.get_template("main").render()), - [ - "this is main. x is 12", - "this is a, x is 12" - ]) + ["this is main. x is 12", "this is a, x is 12"], + ) - l.put_string("another", """ + l.put_string( + "another", + """ <%namespace name="ns" file="main"/> ${ns.body(x=15)} - """) + """, + ) # test via namespace eq_( result_lines(l.get_template("another").render()), - [ - "this is main. x is 15", - "this is a, x is 15" - ]) + ["this is main. x is 15", "this is a, x is 15"], + ) def test_inline_expression_from_arg_one(self): """test that cache_key=${foo} gets its value from @@ -499,20 +530,20 @@ class ScopeTest(TemplateTest): this is #191. """ - t = Template(""" + t = Template( + """ <%def name="layout(foo)" cached="True" cache_key="${foo}"> foo: ${foo} </%def> ${layout(3)} - """, strict_undefined=True, - cache_impl="plain") - - eq_( - result_lines(t.render()), - ["foo: 3"] + """, + strict_undefined=True, + cache_impl="plain", ) + eq_(result_lines(t.render()), ["foo: 3"]) + 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 @@ -522,26 +553,25 @@ class ScopeTest(TemplateTest): to existing behavior before and after #191. """ - t = Template(""" + 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"] + """, + 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"]) + + class NestedDefTest(TemplateTest): def test_nested_def(self): - t = Template(""" + t = Template( + """ ${hi()} @@ -557,14 +587,16 @@ class NestedDefTest(TemplateTest): this is bar </%def> </%def> -""") +""" + ) eq_( flatten_result(t.render()), - "hey, im hi. and heres this is foo , this is bar" + "hey, im hi. and heres this is foo , this is bar", ) def test_nested_2(self): - t = Template(""" + t = Template( + """ x is ${x} <%def name="a()"> this is a, x is ${x} @@ -574,15 +606,17 @@ class NestedDefTest(TemplateTest): </%def> </%def> ${a()} -""") +""" + ) eq_( flatten_result(t.render(x=10)), - "x is 10 this is a, x is 10 this is b: 10" + "x is 10 this is a, x is 10 this is b: 10", ) def test_nested_with_args(self): - t = Template(""" + t = Template( + """ ${a()} <%def name="a()"> <%def name="b(x, y=2)"> @@ -590,14 +624,13 @@ class NestedDefTest(TemplateTest): </%def> a ${b(5)} </%def> -""") - eq_( - flatten_result(t.render()), - "a b x is 5 y is 2" +""" ) + eq_(flatten_result(t.render()), "a b x is 5 y is 2") def test_nested_def_2(self): - template = Template(""" + template = Template( + """ ${a()} <%def name="a()"> <%def name="b()"> @@ -608,14 +641,13 @@ class NestedDefTest(TemplateTest): </%def> ${b()} </%def> -""") - eq_( - flatten_result(template.render()), - "comp c" +""" ) + eq_(flatten_result(template.render()), "comp c") def test_nested_nested_def(self): - t = Template(""" + t = Template( + """ ${a()} <%def name="a()"> @@ -648,16 +680,18 @@ class NestedDefTest(TemplateTest): ${b1()} ${b2()} ${b3()} </%def> -""") +""" + ) eq_( flatten_result(t.render(x=5, y=None)), "a a_b1 a_b2 a_b2_c1 a_b3 a_b3_c1 " "heres x: 5 y is 7 a_b3_c2 y is " - "None c1 is a_b3_c1 heres x: 5 y is 7" + "None c1 is a_b3_c1 heres x: 5 y is 7", ) def test_nested_nested_def_2(self): - t = Template(""" + t = Template( + """ <%def name="a()"> this is a ${b()} <%def name="b()"> @@ -670,14 +704,13 @@ class NestedDefTest(TemplateTest): </%def> </%def> ${a()} -""") - eq_( - flatten_result(t.render()), - "this is a this is b this is c" +""" ) + eq_(flatten_result(t.render()), "this is a this is b this is c") def test_outer_scope(self): - t = Template(""" + t = Template( + """ <%def name="a()"> a: x is ${x} </%def> @@ -696,36 +729,34 @@ class NestedDefTest(TemplateTest): ${b()} x is ${x} -""") - eq_( - flatten_result(t.render(x=5)), - "b. c. x is 10. a: x is 5 x is 5" +""" ) + eq_(flatten_result(t.render(x=5)), "b. c. x is 10. a: x is 5 x is 5") + class ExceptionTest(TemplateTest): def test_raise(self): - template = Template(""" + template = Template( + """ <% raise Exception("this is a test") %> - """, format_exceptions=False) - assert_raises( - Exception, - template.render - ) + """, + format_exceptions=False, + ) + assert_raises(Exception, template.render) def test_handler(self): def handle(context, error): context.write("error message is " + str(error)) return True - template = Template(""" + template = Template( + """ <% raise Exception("this is a test") %> - """, error_handler=handle) - eq_( - template.render().strip(), - "error message is this is a test" + """, + error_handler=handle, ) - + eq_(template.render().strip(), "error message is this is a test") |