示例#1
0
    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'})
示例#2
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
示例#3
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
示例#4
0
    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'
                      })
示例#5
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"
示例#6
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"