Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    def test_wrongcase_tag(self):
        template = """
            <%DEF name="foo()">
            </%def>

        """
        assert_raises(exceptions.CompileException, Lexer(template).parse)
Ejemplo n.º 3
0
 def test_def_syntax_2(self):
     template = """
     <%def name="lala">
         hi
     </%def>
 """
     assert_raises(exceptions.CompileException, Lexer(template).parse)
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
    def test_traditional_assignment_plus_undeclared(self):
        t = Template("""
            t is: ${t}

            <%
                t = 12
            %>
        """)
        assert_raises(UnboundLocalError, t.render, t="T")
Ejemplo n.º 6
0
 def test_raise(self):
     template = Template(
         """
         <%
             raise Exception("this is a test")
         %>
 """,
         format_exceptions=False,
     )
     assert_raises(Exception, template.render)
Ejemplo n.º 7
0
    def test_no_global_imports(self):
        code = """
from foo import *
import x as bar
"""
        assert_raises(
            exceptions.CompileException,
            ast.PythonCode,
            code,
            **exception_kwargs,
        )
Ejemplo n.º 8
0
    def test_onlyclosed_tag(self):
        template = """
            <%def name="foo()">
                foo
            </%def>

            </%namespace>

            hi.
        """
        assert_raises(exceptions.SyntaxException, Lexer(template).parse)
Ejemplo n.º 9
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)
Ejemplo n.º 10
0
    def test_unmatched_tag(self):
        template = """
        <%namespace name="bar">
        <%def name="foo()">
            foo
            </%namespace>
        </%def>


        hi.
"""
        assert_raises(exceptions.SyntaxException, Lexer(template).parse)
Ejemplo n.º 11
0
    def test_unbound_scope(self):
        t = Template("""
            <%
                y = 10
            %>
            <%def name="a()">
                y is: ${y}
                <%
                    # should raise error ?
                    y = 15
                %>
                y is ${y}
            </%def>
            ${a()}
""")
        assert_raises(UnboundLocalError, t.render)
Ejemplo n.º 12
0
    def test_unicode_bom(self):
        self._do_file_test(
            "bom.html",
            ("Alors vous imaginez ma surprise, au lever du jour, quand "
             "une drôle de petite voix m’a réveillé. Elle disait: "
             "« S’il vous plaît… dessine-moi un mouton! »"),
        )

        self._do_file_test(
            "bommagic.html",
            ("Alors vous imaginez ma surprise, au lever du jour, quand "
             "une drôle de petite voix m’a réveillé. Elle disait: "
             "« S’il vous plaît… dessine-moi un mouton! »"),
        )

        assert_raises(
            exceptions.CompileException,
            Template,
            filename=self._file_path("badbom.html"),
            module_directory=config.module_base,
        )
Ejemplo n.º 13
0
    def test_attr_raise(self):
        l = lookup.TemplateLookup()

        l.put_string(
            "foo.html",
            """
            <%def name="foo()">
            </%def>
        """,
        )

        l.put_string(
            "bar.html",
            """
        <%namespace name="foo" file="foo.html"/>

        ${foo.notfoo()}
        """,
        )

        assert_raises(AttributeError, l.get_template("bar.html").render)
Ejemplo n.º 14
0
 def test_nonexistent_tag(self):
     template = """
         <%lala x="5"/>
     """
     assert_raises(exceptions.CompileException, Lexer(template).parse)
Ejemplo n.º 15
0
 def test_noexpr_allowed(self):
     template = """
         <%namespace name="${foo}"/>
     """
     assert_raises(exceptions.CompileException, Lexer(template).parse)