Example #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
        )
Example #2
0
    def test_inherited_block_nested_inner_only(self):
        l = TemplateLookup()
        l.put_string("index",
            """
                <%inherit file="base"/>
                <%block name="title">
                    index title
                </%block>

            """
        )
        l.put_string("base","""
            above
            <%block name="header">
                base header
                <%block name="title">
                    the title
                </%block>
            </%block>

            ${next.body()}
            below
        """)
        self._do_test(l.get_template("index"),
                ["above", "base header", "index title", "below"],
                filters=result_lines)
Example #3
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)
Example #4
0
    def test_inherited_block_nested_inner_only(self):
        l = TemplateLookup()
        l.put_string(
            "index", """
                <%inherit file="base"/>
                <%block name="title">
                    index title
                </%block>

            """)
        l.put_string(
            "base", """
            above
            <%block name="header">
                base header
                <%block name="title">
                    the title
                </%block>
            </%block>

            ${next.body()}
            below
        """)
        self._do_test(l.get_template("index"),
                      ["above", "base header", "index title", "below"],
                      filters=result_lines)
Example #5
0
 def test_lookup(self):
     l = TemplateLookup(cache_impl='mock')
     l.put_string("x", """
         <%page cached="True" />
         ${y}
     """)
     t = l.get_template("x")
     self._install_mock_cache(t)
     assert result_lines(t.render(y=5)) == ["5"]
     assert result_lines(t.render(y=7)) == ["5"]
     assert isinstance(t.cache.impl, MockCacheImpl)
Example #6
0
 def test_basic(self):
     lookup = TemplateLookup()
     lookup.put_string("a", """
         this is a
         <%include file="b" args="a=3,b=4,c=5"/>
     """)
     lookup.put_string("b", """
         <%page args="a,b,c"/>
         this is b.  ${a}, ${b}, ${c}
     """)
     assert flatten_result(lookup.get_template("a").render()) == "this is a this is b. 3, 4, 5"
Example #7
0
 def test_localargs(self):
     lookup = TemplateLookup()
     lookup.put_string("a", """
         this is a
         <%include file="b" args="a=a,b=b,c=5"/>
     """)
     lookup.put_string("b", """
         <%page args="a,b,c"/>
         this is b.  ${a}, ${b}, ${c}
     """)
     assert flatten_result(lookup.get_template("a").render(a=7,b=8)) == "this is a this is b. 7, 8, 5"
Example #8
0
 def test_include_withargs(self):
     lookup = TemplateLookup()
     lookup.put_string("a", """
         this is a
         <%include file="${i}" args="c=5, **context.kwargs"/>
     """)
     lookup.put_string("b", """
         <%page args="a,b,c"/>
         this is b.  ${a}, ${b}, ${c}
     """)
     assert flatten_result(lookup.get_template("a").render(a=7,b=8,i='b')) == "this is a this is b. 7, 8, 5"
Example #9
0
 def test_lookup(self):
     l = TemplateLookup(cache_impl='mock')
     l.put_string(
         "x", """
         <%page cached="True" />
         ${y}
     """)
     t = l.get_template("x")
     self._install_mock_cache(t)
     assert result_lines(t.render(y=5)) == ["5"]
     assert result_lines(t.render(y=7)) == ["5"]
     assert isinstance(t.cache.impl, MockCacheImpl)
Example #10
0
    def test_loop_disabled_lookup(self):
        l = TemplateLookup(enable_loop=False)
        l.put_string("x", """
            the loop: ${loop}
        """)

        self._do_test(
            l.get_template("x"),
            "the loop: hi",
            template_args=dict(loop='hi'),
            filters=flatten_result,
        )
Example #11
0
    def test_loop_disabled_lookup(self):
        l = TemplateLookup(enable_loop=False)
        l.put_string("x",
        """
            the loop: ${loop}
        """
        )

        self._do_test(
            l.get_template("x"),
            "the loop: hi",
            template_args=dict(loop='hi'),
            filters=flatten_result,
        )
Example #12
0
 def test_localargs(self):
     lookup = TemplateLookup()
     lookup.put_string(
         "a", """
         this is a
         <%include file="b" args="a=a,b=b,c=5"/>
     """)
     lookup.put_string(
         "b", """
         <%page args="a,b,c"/>
         this is b.  ${a}, ${b}, ${c}
     """)
     assert flatten_result(lookup.get_template("a").render(
         a=7, b=8)) == "this is a this is b. 7, 8, 5"
Example #13
0
 def test_basic(self):
     lookup = TemplateLookup()
     lookup.put_string(
         "a", """
         this is a
         <%include file="b" args="a=3,b=4,c=5"/>
     """)
     lookup.put_string(
         "b", """
         <%page args="a,b,c"/>
         this is b.  ${a}, ${b}, ${c}
     """)
     assert flatten_result(lookup.get_template(
         "a").render()) == "this is a this is b. 3, 4, 5"
Example #14
0
    def test_format_errors_no_pygments(self):
        l = TemplateLookup(format_errors=True)

        l.put_string("foo.html", """
<%inherit file="base.html"/>
${foobar}
        """)

        l.put_string("base.html", """
        ${self.body()}
        """)

        assert '<div class="sourceline">${foobar}</div>' in \
            result_lines(l.get_template("foo.html").render_unicode())
Example #15
0
    def test_format_errors_pygments(self):
        l = TemplateLookup(format_errors=True)

        l.put_string("foo.html", """
<%inherit file="base.html"/>
${foobar}
        """)

        l.put_string("base.html", """
        ${self.body()}
        """)

        assert '<div class="sourceline"><table class="syntax-highlightedtable">' in \
            l.get_template("foo.html").render_unicode()
Example #16
0
 def test_include_withargs(self):
     lookup = TemplateLookup()
     lookup.put_string(
         "a", """
         this is a
         <%include file="${i}" args="c=5, **context.kwargs"/>
     """)
     lookup.put_string(
         "b", """
         <%page args="a,b,c"/>
         this is b.  ${a}, ${b}, ${c}
     """)
     assert flatten_result(
         lookup.get_template("a").render(
             a=7, b=8, i='b')) == "this is a this is b. 7, 8, 5"
Example #17
0
    def test_loop_enabled_override_lookup(self):
        l = TemplateLookup()
        l.put_string(
            "x", """
            <%page enable_loop="True" />
            % for i in (1, 2, 3):
                ${i} ${loop.index}
            % endfor
        """)

        self._do_test(
            l.get_template("x"),
            "1 0 2 1 3 2",
            template_args=dict(),
            filters=flatten_result,
        )
Example #18
0
    def test_block_pageargs(self):
        l = TemplateLookup()
        l.put_string(
            "caller", """

            <%include file="callee" args="val1='3', val2='4'"/>

        """)
        l.put_string(
            "callee", """
            <%block name="foob">
                foob, ${pageargs['val1']}, ${pageargs['val2']}
            </%block>
        """)
        self._do_test(l.get_template("caller"), ['foob, 3, 4'],
                      filters=result_lines)
Example #19
0
    def test_block_pageargs(self):
        l = TemplateLookup()
        l.put_string("caller", """

            <%include file="callee" args="val1='3', val2='4'"/>

        """)
        l.put_string("callee", """
            <%block name="foob">
                foob, ${pageargs['val1']}, ${pageargs['val2']}
            </%block>
        """)
        self._do_test(
            l.get_template("caller"),
            ['foob, 3, 4'],
            filters=result_lines
        )
Example #20
0
    def test_utf8_format_errors_no_pygments(self):
        """test that htmlentityreplace formatting is applied to
           errors reported with format_errors=True"""

        l = TemplateLookup(format_errors=True)
        if compat.py3k:
            l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""")
        else:
            l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""")

        if compat.py3k:
            assert '<div class="sourceline">${&#39;привет&#39; + foobar}</div>'\
                in result_lines(l.get_template("foo.html").render().decode('utf-8'))
        else:
            assert '${u&#39;&#x43F;&#x440;&#x438;&#x432;&#x435;'\
                   '&#x442;&#39; + foobar}' in \
                result_lines(l.get_template("foo.html").render().decode('utf-8'))
Example #21
0
    def test_utf8_format_errors_pygments(self):
        """test that htmlentityreplace formatting is applied to
           errors reported with format_errors=True"""

        l = TemplateLookup(format_errors=True)
        if compat.py3k:
            l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${'привет' + foobar}""")
        else:
            l.put_string("foo.html", """# -*- coding: utf-8 -*-\n${u'привет' + foobar}""")

        if compat.py3k:
            assert '&#39;привет&#39;</span>' in \
                l.get_template("foo.html").render().decode('utf-8')
        else:
            assert 'u&#39;&#x43F;&#x440;&#x438;&#x432;'\
                    '&#x435;&#x442;&#39;</span>' in \
                l.get_template("foo.html").render().decode('utf-8')
Example #22
0
    def test_loop_enabled_override_lookup(self):
        l = TemplateLookup()
        l.put_string("x",
        """
            <%page enable_loop="True" />
            % for i in (1, 2, 3):
                ${i} ${loop.index}
            % endfor
        """
        )

        self._do_test(
            l.get_template("x"),
            "1 0 2 1 3 2",
            template_args=dict(),
            filters=flatten_result,
        )
Example #23
0
    def test_builtin_names_dont_clobber_defaults_in_includes(self):
        lookup = TemplateLookup()
        lookup.put_string("test.choco",
        """
        <%include file="test1.choco"/>

        """)

        lookup.put_string("test1.choco", """
        <%page args="id='foo'"/>

        ${id}
        """)

        for template in ("test.choco", "test1.choco"):
            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>"
Example #24
0
    def test_includes(self):
        lookup = TemplateLookup()
        lookup.put_string(
            "incl1.tmpl", """
        <%page args="bar" />
        ${bar}
        ${pageargs['foo']}
        """)
        lookup.put_string("incl2.tmpl", """
        ${pageargs}
        """)
        lookup.put_string(
            "index.tmpl", """
        <%include file="incl1.tmpl" args="**pageargs"/>
        <%page args="variable" />
        ${variable}
        <%include file="incl2.tmpl" />
        """)

        self._do_test(lookup.get_template("index.tmpl"),
                      "bar foo var {}",
                      filters=flatten_result,
                      template_args={
                          'variable': 'var',
                          'bar': 'bar',
                          'foo': 'foo'
                      })
Example #25
0
    def test_two_levels_one(self):
        l = TemplateLookup()
        l.put_string(
            "index", """
                <%inherit file="middle"/>
                <%block name="header">
                    index header
                </%block>
                <%block>
                    index anon
                </%block>
            """)
        l.put_string(
            "middle", """
            <%inherit file="base"/>
            <%block>
                middle anon
            </%block>
            ${next.body()}
        """)
        l.put_string(
            "base", """
            above
            <%block name="header">
                the header
            </%block>

            ${next.body()}
            below
        """)
        self._do_test(
            l.get_template("index"),
            ["above", "index header", "middle anon", "index anon", "below"],
            filters=result_lines)
Example #26
0
    def test_includes(self):
        lookup = TemplateLookup()
        lookup.put_string("incl1.tmpl",
        """
        <%page args="bar" />
        ${bar}
        ${pageargs['foo']}
        """
        )
        lookup.put_string("incl2.tmpl",
        """
        ${pageargs}
        """
        )
        lookup.put_string("index.tmpl", """
        <%include file="incl1.tmpl" args="**pageargs"/>
        <%page args="variable" />
        ${variable}
        <%include file="incl2.tmpl" />
        """)

        self._do_test(
            lookup.get_template("index.tmpl"),
            "bar foo var {}",
            filters=flatten_result,
            template_args={'variable':'var', 'bar':'bar', 'foo':'foo'}

        )
Example #27
0
    def test_two_levels_one(self):
        l = TemplateLookup()
        l.put_string("index",
            """
                <%inherit file="middle"/>
                <%block name="header">
                    index header
                </%block>
                <%block>
                    index anon
                </%block>
            """
        )
        l.put_string("middle", """
            <%inherit file="base"/>
            <%block>
                middle anon
            </%block>
            ${next.body()}
        """)
        l.put_string("base","""
            above
            <%block name="header">
                the header
            </%block>

            ${next.body()}
            below
        """)
        self._do_test(l.get_template("index"),
                ["above", "index header", "middle anon",
                "index anon", "below"],
                filters=result_lines)
Example #28
0
    def test_noninherited_block_no_render(self):
        l = TemplateLookup()
        l.put_string(
            "index", """
                <%inherit file="base"/>
                <%block name="some_thing">
                    some thing
                </%block>
            """)
        l.put_string(
            "base", """
            above
            <%block name="header">
                the header
            </%block>

            ${next.body()}
            below
        """)
        self._do_test(l.get_template("index"),
                      ["above", "the header", "some thing", "below"],
                      filters=result_lines)
Example #29
0
    def test_builtin_names_dont_clobber_defaults_in_includes(self):
        lookup = TemplateLookup()
        lookup.put_string(
            "test.choco", """
        <%include file="test1.choco"/>

        """)

        lookup.put_string(
            "test1.choco", """
        <%page args="id='foo'"/>

        ${id}
        """)

        for template in ("test.choco", "test1.choco"):
            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>"
Example #30
0
    def test_noninherited_block_no_render(self):
        l = TemplateLookup()
        l.put_string("index",
            """
                <%inherit file="base"/>
                <%block name="some_thing">
                    some thing
                </%block>
            """
        )
        l.put_string("base","""
            above
            <%block name="header">
                the header
            </%block>

            ${next.body()}
            below
        """)
        self._do_test(l.get_template("index"),
                ["above", "the header", "some thing", "below"],
                filters=result_lines)
Example #31
0
    def test_block_overridden_by_def(self):
        l = TemplateLookup()
        l.put_string("index",
            """
                <%inherit file="base"/>
                <%def name="header()">
                    inner header
                </%def>
            """
        )
        l.put_string("base","""
            above
            <%block name="header">
                the header
            </%block>

            ${next.body()}
            below
        """)
        self._do_test(l.get_template("index"),
                ["above", "inner header", "below"],
                filters=result_lines)
Example #32
0
    def test_block_overridden_by_def(self):
        l = TemplateLookup()
        l.put_string(
            "index", """
                <%inherit file="base"/>
                <%def name="header()">
                    inner header
                </%def>
            """)
        l.put_string(
            "base", """
            above
            <%block name="header">
                the header
            </%block>

            ${next.body()}
            below
        """)
        self._do_test(l.get_template("index"),
                      ["above", "inner header", "below"],
                      filters=result_lines)
Example #33
0
    def test_inherits(self):
        lookup = TemplateLookup()
        lookup.put_string("base.tmpl",
        """
        <%page args="bar" />
        ${bar}
        ${pageargs['foo']}
        ${self.body(**pageargs)}
        """
        )
        lookup.put_string("index.tmpl", """
        <%inherit file="base.tmpl" />
        <%page args="variable" />
        ${variable}
        """)

        self._do_test(
            lookup.get_template("index.tmpl"),
            "bar foo var",
            filters=flatten_result,
            template_args={'variable':'var', 'bar':'bar', 'foo':'foo'}

        )
Example #34
0
    def test_inherits(self):
        lookup = TemplateLookup()
        lookup.put_string(
            "base.tmpl", """
        <%page args="bar" />
        ${bar}
        ${pageargs['foo']}
        ${self.body(**pageargs)}
        """)
        lookup.put_string(
            "index.tmpl", """
        <%inherit file="base.tmpl" />
        <%page args="variable" />
        ${variable}
        """)

        self._do_test(lookup.get_template("index.tmpl"),
                      "bar foo var",
                      filters=flatten_result,
                      template_args={
                          'variable': 'var',
                          'bar': 'bar',
                          'foo': 'foo'
                      })
Example #35
0
    def test_expression_declared(self):
        t = Template("""
            ${",".join([t for t in ("a", "b", "c")])}
        """, strict_undefined=True)

        eq_(result_lines(t.render()), ['a,b,c'])

        t = Template("""
            <%self:foo value="${[(val, n) for val, n in [(1, 2)]]}"/>

            <%def name="foo(value)">
                ${value}
            </%def>

        """, strict_undefined=True)

        eq_(result_lines(t.render()), ['[(1, 2)]'])

        t = Template("""
            <%call expr="foo(value=[(val, n) for val, n in [(1, 2)]])" />

            <%def name="foo(value)">
                ${value}
            </%def>

        """, strict_undefined=True)

        eq_(result_lines(t.render()), ['[(1, 2)]'])

        l = TemplateLookup(strict_undefined=True)
        l.put_string("i", "hi, ${pageargs['y']}")
        l.put_string("t", """
            <%include file="i" args="y=[x for x in range(3)]" />
        """)
        eq_(
            result_lines(l.get_template("t").render()), ['hi, [0, 1, 2]']
        )

        l.put_string('q', """
            <%namespace name="i" file="${(str([x for x in range(3)][2]) + 'i')[-1]}" />
            ${i.body(y='x')}
        """)
        eq_(
            result_lines(l.get_template("q").render()), ['hi, x']
        )

        t = Template("""
            <%
                y = lambda q: str(q)
            %>
            ${y('hi')}
        """, strict_undefined=True)
        eq_(
            result_lines(t.render()), ["hi"]
        )
Example #36
0
    def test_expression_declared(self):
        t = Template("""
            ${",".join([t for t in ("a", "b", "c")])}
        """,
                     strict_undefined=True)

        eq_(result_lines(t.render()), ['a,b,c'])

        t = Template("""
            <%self:foo value="${[(val, n) for val, n in [(1, 2)]]}"/>

            <%def name="foo(value)">
                ${value}
            </%def>

        """,
                     strict_undefined=True)

        eq_(result_lines(t.render()), ['[(1, 2)]'])

        t = Template("""
            <%call expr="foo(value=[(val, n) for val, n in [(1, 2)]])" />

            <%def name="foo(value)">
                ${value}
            </%def>

        """,
                     strict_undefined=True)

        eq_(result_lines(t.render()), ['[(1, 2)]'])

        l = TemplateLookup(strict_undefined=True)
        l.put_string("i", "hi, ${pageargs['y']}")
        l.put_string(
            "t", """
            <%include file="i" args="y=[x for x in range(3)]" />
        """)
        eq_(result_lines(l.get_template("t").render()), ['hi, [0, 1, 2]'])

        l.put_string(
            'q', """
            <%namespace name="i" file="${(str([x for x in range(3)][2]) + 'i')[-1]}" />
            ${i.body(y='x')}
        """)
        eq_(result_lines(l.get_template("q").render()), ['hi, x'])

        t = Template("""
            <%
                y = lambda q: str(q)
            %>
            ${y('hi')}
        """,
                     strict_undefined=True)
        eq_(result_lines(t.render()), ["hi"])
Example #37
0
 def test_within_ccall(self):
     lookup = TemplateLookup()
     lookup.put_string("a", """this is a""")
     lookup.put_string("b", """
     <%def name="bar()">
         bar: ${caller.body()}
         <%include file="a"/>
     </%def>
     """)
     lookup.put_string("c", """
     <%namespace name="b" file="b"/>
     <%b:bar>
         calling bar
     </%b:bar>
     """)
     assert flatten_result(lookup.get_template("c").render()) == "bar: calling bar this is a"
Example #38
0
 def test_within_ccall(self):
     lookup = TemplateLookup()
     lookup.put_string("a", """this is a""")
     lookup.put_string(
         "b", """
     <%def name="bar()">
         bar: ${caller.body()}
         <%include file="a"/>
     </%def>
     """)
     lookup.put_string(
         "c", """
     <%namespace name="b" file="b"/>
     <%b:bar>
         calling bar
     </%b:bar>
     """)
     assert flatten_result(
         lookup.get_template("c").render()) == "bar: calling bar this is a"
Example #39
0
 def test_via_lookup(self):
     tl = TemplateLookup(lexer_cls=self._fixture())
     tl.put_string("foo", "foo")
     t = tl.get_template("foo")
     self._test_custom_lexer(t)
Example #40
0
 def test_via_lookup(self):
     tl = TemplateLookup(lexer_cls=self._fixture())
     tl.put_string("foo", "foo")
     t = tl.get_template("foo")
     self._test_custom_lexer(t)