Esempio n. 1
0
    def test_toplevel_contextual(self):
        template = Template(
            """
            <%!
                def bar(fn):
                    def decorate(context):
                        context.write("BAR")
                        fn()
                        context.write("BAR")
                        return ''
                    return decorate
            %>

            <%def name="foo()" decorator="bar">
                this is foo
            </%def>

            ${foo()}
        """
        )

        assert flatten_result(template.render()) == "BAR this is foo BAR"

        assert (
            flatten_result(template.get_def("foo").render())
            == "BAR this is foo BAR"
        )
Esempio n. 2
0
    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>")
Esempio n. 3
0
    def test_builtins(self):
        t = Template("""
            ${"this is <text>" | h}
""")
        assert flatten_result(t.render()) == "this is &lt;text&gt;"

        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.")
Esempio n. 4
0
    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)
Esempio n. 5
0
    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",
        )
Esempio n. 6
0
 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"
Esempio n. 7
0
    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")
Esempio n. 8
0
    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.",
        )
Esempio n. 9
0
    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")
Esempio n. 10
0
    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",
        )
Esempio n. 11
0
    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"
Esempio n. 12
0
    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")
Esempio n. 13
0
    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"
Esempio n. 14
0
 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")
Esempio n. 15
0
    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"
Esempio n. 16
0
    def test_quoting(self):
        t = Template("""
            foo ${bar | h}
        """)

        eq_(
            flatten_result(t.render(bar="<'some bar'>")),
            "foo &lt;&#39;some bar&#39;&gt;",
        )
Esempio n. 17
0
    def test_url_escaping(self):
        t = Template("""
            http://example.com/?bar=${bar | u}&v=1
        """)

        eq_(
            flatten_result(t.render(bar="酒吧bar")),
            "http://example.com/?bar=%E9%85%92%E5%90%A7bar&v=1",
        )
Esempio n. 18
0
    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")
Esempio n. 19
0
    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")
Esempio n. 20
0
    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"
Esempio n. 21
0
    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")
Esempio n. 22
0
 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")
Esempio n. 23
0
 def test_unicode_file_lookup(self):
     lookup = TemplateLookup(
         directories=[config.template_base],
         output_encoding="utf-8",
         default_filters=["decode.utf8"],
     )
     template = lookup.get_template("/chs_unicode_py3k.html")
     eq_(
         flatten_result(template.render_unicode(name="毛泽东")),
         ("毛泽东 是 新中国的主席<br/> Welcome 你 to 北京."),
     )
Esempio n. 24
0
    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>
""")
        eq_(flatten_result(t.render()), "a b x is 5 y is 2")
Esempio n. 25
0
    def test_import_local(self):
        t = Template("""
            <%namespace import="*">
                <%def name="foo()">
                    this is foo
                </%def>
            </%namespace>

            ${foo()}

        """)
        assert flatten_result(t.render()) == "this is foo"
Esempio n. 26
0
    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")
Esempio n. 27
0
    def test_nested_def_2(self):
        template = Template("""
        ${a()}
        <%def name="a()">
            <%def name="b()">
                <%def name="c()">
                    comp c
                </%def>
                ${c()}
            </%def>
            ${b()}
        </%def>
""")
        eq_(flatten_result(template.render()), "comp c")
Esempio n. 28
0
    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")
Esempio n. 29
0
    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")
Esempio n. 30
0
    def test_def(self):
        t = Template("""
            <%def name="foo()" filter="myfilter">
                this is foo
            </%def>
            ${foo()}
""")

        eq_(
            flatten_result(
                t.render(
                    x="this is x",
                    myfilter=lambda t: "MYFILTER->%s<-MYFILTER" % t,
                )),
            "MYFILTER-> this is foo <-MYFILTER",
        )