diff options
Diffstat (limited to 'test/test_inheritance.py')
-rw-r--r-- | test/test_inheritance.py | 351 |
1 files changed, 221 insertions, 130 deletions
diff --git a/test/test_inheritance.py b/test/test_inheritance.py index 08a46b3..e8ed74e 100644 --- a/test/test_inheritance.py +++ b/test/test_inheritance.py @@ -1,12 +1,17 @@ -from mako import lookup, compat import unittest + +from mako import compat +from mako import lookup from test.util import result_lines + class InheritanceTest(unittest.TestCase): def test_basic(self): collection = lookup.TemplateLookup() - collection.put_string('main', """ + collection.put_string( + "main", + """ <%inherit file="base"/> <%def name="header()"> @@ -14,9 +19,12 @@ class InheritanceTest(unittest.TestCase): </%def> this is the content. -""") +""", + ) - collection.put_string('base', """ + collection.put_string( + "base", + """ This is base. header: ${self.header()} @@ -28,31 +36,38 @@ footer: ${self.footer()} <%def name="footer()"> this is the footer. header again ${next.header()} </%def> -""") - - assert result_lines(collection.get_template('main').render()) == [ - 'This is base.', - 'header:', - 'main header.', - 'body:', - 'this is the content.', - 'footer:', - 'this is the footer. header again', - 'main header.' +""", + ) + + assert result_lines(collection.get_template("main").render()) == [ + "This is base.", + "header:", + "main header.", + "body:", + "this is the content.", + "footer:", + "this is the footer. header again", + "main header.", ] def test_multilevel_nesting(self): collection = lookup.TemplateLookup() - collection.put_string('main', """ + collection.put_string( + "main", + """ <%inherit file="layout"/> <%def name="d()">main_d</%def> main_body ${parent.d()} full stack from the top: - ${self.name} ${parent.name} ${parent.context['parent'].name} ${parent.context['parent'].context['parent'].name} -""") - - collection.put_string('layout', """ + ${self.name} ${parent.name} ${parent.context['parent'].name} """ + """${parent.context['parent'].context['parent'].name} +""", + ) + + collection.put_string( + "layout", + """ <%inherit file="general"/> <%def name="d()">layout_d</%def> layout_body @@ -60,95 +75,122 @@ parent name: ${parent.name} ${parent.d()} ${parent.context['parent'].d()} ${next.body()} -""") +""", + ) - collection.put_string('general', """ + collection.put_string( + "general", + """ <%inherit file="base"/> <%def name="d()">general_d</%def> general_body ${next.d()} ${next.context['next'].d()} ${next.body()} -""") - collection.put_string('base', """ +""", + ) + collection.put_string( + "base", + """ base_body full stack from the base: - ${self.name} ${self.context['parent'].name} ${self.context['parent'].context['parent'].name} ${self.context['parent'].context['parent'].context['parent'].name} + ${self.name} ${self.context['parent'].name} """ + """${self.context['parent'].context['parent'].name} """ + """${self.context['parent'].context['parent'].context['parent'].name} ${next.body()} <%def name="d()">base_d</%def> -""") - - assert result_lines(collection.get_template('main').render()) == [ - 'base_body', - 'full stack from the base:', - 'self:main self:layout self:general self:base', - 'general_body', - 'layout_d', - 'main_d', - 'layout_body', - 'parent name: self:general', - 'general_d', - 'base_d', - 'main_body layout_d', - 'full stack from the top:', - 'self:main self:layout self:general self:base' +""", + ) + + assert result_lines(collection.get_template("main").render()) == [ + "base_body", + "full stack from the base:", + "self:main self:layout self:general self:base", + "general_body", + "layout_d", + "main_d", + "layout_body", + "parent name: self:general", + "general_d", + "base_d", + "main_body layout_d", + "full stack from the top:", + "self:main self:layout self:general self:base", ] def test_includes(self): - """test that an included template also has its full hierarchy invoked.""" + """test that an included template also has its full hierarchy + invoked.""" collection = lookup.TemplateLookup() - collection.put_string("base", """ + collection.put_string( + "base", + """ <%def name="a()">base_a</%def> This is the base. ${next.body()} End base. -""") +""", + ) - collection.put_string("index",""" + collection.put_string( + "index", + """ <%inherit file="base"/> this is index. a is: ${self.a()} <%include file="secondary"/> -""") +""", + ) - collection.put_string("secondary",""" + collection.put_string( + "secondary", + """ <%inherit file="base"/> this is secondary. a is: ${self.a()} -""") +""", + ) assert result_lines(collection.get_template("index").render()) == [ - 'This is the base.', - 'this is index.', - 'a is: base_a', - 'This is the base.', - 'this is secondary.', - 'a is: base_a', - 'End base.', - 'End base.' - ] + "This is the base.", + "this is index.", + "a is: base_a", + "This is the base.", + "this is secondary.", + "a is: base_a", + "End base.", + "End base.", + ] def test_namespaces(self): - """test that templates used via <%namespace> have access to an inheriting 'self', and that - the full 'self' is also exported.""" + """test that templates used via <%namespace> have access to an + inheriting 'self', and that the full 'self' is also exported.""" collection = lookup.TemplateLookup() - collection.put_string("base", """ + collection.put_string( + "base", + """ <%def name="a()">base_a</%def> <%def name="b()">base_b</%def> This is the base. ${next.body()} -""") +""", + ) - collection.put_string("layout", """ + collection.put_string( + "layout", + """ <%inherit file="base"/> <%def name="a()">layout_a</%def> This is the layout.. ${next.body()} -""") +""", + ) - collection.put_string("index",""" + collection.put_string( + "index", + """ <%inherit file="base"/> <%namespace name="sc" file="secondary"/> this is index. @@ -157,32 +199,41 @@ ${next.body()} sc.b is: ${sc.b()} sc.c is: ${sc.c()} sc.body is: ${sc.body()} -""") +""", + ) - collection.put_string("secondary",""" + collection.put_string( + "secondary", + """ <%inherit file="layout"/> - <%def name="c()">secondary_c. a is ${self.a()} b is ${self.b()} d is ${self.d()}</%def> + <%def name="c()">secondary_c. a is ${self.a()} b is ${self.b()} """ + """d is ${self.d()}</%def> <%def name="d()">secondary_d.</%def> this is secondary. a is: ${self.a()} c is: ${self.c()} -""") - - assert result_lines(collection.get_template('index').render()) == ['This is the base.', - 'this is index.', - 'a is: base_a', - 'sc.a is: layout_a', - 'sc.b is: base_b', - 'sc.c is: secondary_c. a is layout_a b is base_b d is secondary_d.', - 'sc.body is:', - 'this is secondary.', - 'a is: layout_a', - 'c is: secondary_c. a is layout_a b is base_b d is secondary_d.' - ] +""", + ) + + assert result_lines(collection.get_template("index").render()) == [ + "This is the base.", + "this is index.", + "a is: base_a", + "sc.a is: layout_a", + "sc.b is: base_b", + "sc.c is: secondary_c. a is layout_a b is base_b d is " + "secondary_d.", + "sc.body is:", + "this is secondary.", + "a is: layout_a", + "c is: secondary_c. a is layout_a b is base_b d is secondary_d.", + ] def test_pageargs(self): collection = lookup.TemplateLookup() - collection.put_string("base", """ + collection.put_string( + "base", + """ this is the base. <% @@ -195,29 +246,39 @@ ${next.body()} </%def> ${foo()} - """) - collection.put_string("index", """ + """, + ) + collection.put_string( + "index", + """ <%inherit file="base"/> <%page args="x, y, z=7"/> print ${x}, ${y}, ${z} - """) + """, + ) if compat.py3k: - assert result_lines(collection.get_template('index').render_unicode(x=5,y=10)) == [ + assert result_lines( + collection.get_template("index").render_unicode(x=5, y=10) + ) == [ "this is the base.", "pageargs: (type: <class 'dict'>) [('x', 5), ('y', 10)]", - "print 5, 10, 7" + "print 5, 10, 7", ] else: - assert result_lines(collection.get_template('index').render_unicode(x=5,y=10)) == [ + assert result_lines( + collection.get_template("index").render_unicode(x=5, y=10) + ) == [ "this is the base.", "pageargs: (type: <type 'dict'>) [('x', 5), ('y', 10)]", - "print 5, 10, 7" + "print 5, 10, 7", ] def test_pageargs_2(self): collection = lookup.TemplateLookup() - collection.put_string("base", """ + collection.put_string( + "base", + """ this is the base. ${next.body(**context.kwargs)} @@ -232,61 +293,84 @@ ${next.body()} ${foo(x=12, y=15, z=8)} ${bar(x=19, y=17)} - """) - collection.put_string("index", """ + """, + ) + collection.put_string( + "index", + """ <%inherit file="base"/> <%page args="x, y, z=7"/> pageargs: ${x}, ${y}, ${z} - """) - assert result_lines(collection.get_template('index').render(x=5,y=10)) == [ + """, + ) + assert result_lines( + collection.get_template("index").render(x=5, y=10) + ) == [ "this is the base.", "pageargs: 5, 10, 7", "pageargs: 12, 15, 8", - "pageargs: 5, 10, 16" + "pageargs: 5, 10, 16", ] def test_pageargs_err(self): collection = lookup.TemplateLookup() - collection.put_string("base", """ + collection.put_string( + "base", + """ this is the base. ${next.body()} - """) - collection.put_string("index", """ + """, + ) + collection.put_string( + "index", + """ <%inherit file="base"/> <%page args="x, y, z=7"/> print ${x}, ${y}, ${z} - """) + """, + ) try: - print(collection.get_template('index').render(x=5,y=10)) + print(collection.get_template("index").render(x=5, y=10)) assert False except TypeError: assert True def test_toplevel(self): collection = lookup.TemplateLookup() - collection.put_string("base", """ + collection.put_string( + "base", + """ this is the base. ${next.body()} - """) - collection.put_string("index", """ + """, + ) + collection.put_string( + "index", + """ <%inherit file="base"/> this is the body - """) - assert result_lines(collection.get_template('index').render()) == [ + """, + ) + assert result_lines(collection.get_template("index").render()) == [ "this is the base.", - "this is the body" - ] - assert result_lines(collection.get_template('index').get_def("body").render()) == [ - "this is the body" + "this is the body", ] + assert result_lines( + collection.get_template("index").get_def("body").render() + ) == ["this is the body"] def test_dynamic(self): collection = lookup.TemplateLookup() - collection.put_string("base", """ + collection.put_string( + "base", + """ this is the base. ${next.body()} - """) - collection.put_string("index", """ + """, + ) + collection.put_string( + "index", + """ <%! def dyn(context): if context.get('base', None) is not None: @@ -296,18 +380,20 @@ ${next.body()} %> <%inherit file="${dyn(context)}"/> this is index. - """) - assert result_lines(collection.get_template('index').render()) == [ - 'this is index.' - ] - assert result_lines(collection.get_template('index').render(base=True)) == [ - 'this is the base.', - 'this is index.' + """, + ) + assert result_lines(collection.get_template("index").render()) == [ + "this is index." ] + assert result_lines( + collection.get_template("index").render(base=True) + ) == ["this is the base.", "this is index."] def test_in_call(self): collection = lookup.TemplateLookup() - collection.put_string("/layout.html",""" + collection.put_string( + "/layout.html", + """ Super layout! <%call expr="self.grid()"> ${next.body()} @@ -319,10 +405,12 @@ ${next.body()} ${caller.body()} End Parent </%def> - """) - + """, + ) - collection.put_string("/subdir/layout.html", """ + collection.put_string( + "/subdir/layout.html", + """ ${next.body()} <%def name="grid()"> Subdir grid @@ -330,20 +418,23 @@ ${next.body()} End subdir </%def> <%inherit file="/layout.html"/> - """) + """, + ) - collection.put_string("/subdir/renderedtemplate.html",""" + collection.put_string( + "/subdir/renderedtemplate.html", + """ Holy smokes! <%inherit file="/subdir/layout.html"/> - """) + """, + ) - #print collection.get_template("/layout.html").code - #print collection.get_template("/subdir/renderedtemplate.html").render() - assert result_lines(collection.get_template("/subdir/renderedtemplate.html").render()) == [ + assert result_lines( + collection.get_template("/subdir/renderedtemplate.html").render() + ) == [ "Super layout!", "Subdir grid", "Holy smokes!", "End subdir", - "Oh yea!" + "Oh yea!", ] - |