Example #1
0
 def test_pycall_calls_with_positional_args(self):
     t = Template('<py:def function="foo(a)">foo: ${a()}</py:def>'
                  '<py:call function="foo">'
                  'bar'
                  '</py:call>')
     s = t.render({})
     assert s == 'foo: bar'
Example #2
0
 def test_it_compiles_python_pi(self):
     t = Template('<?python  \n'
                  '  foo("whoa nelly!")\n'
                  '?>')
     a = []
     assert t.render({'foo': a.append}) == ''
     assert a == ['whoa nelly!']
Example #3
0
    def test_it_compiles_pydef(self):
        t = Template('<py:def function="form(x)">'
                     '<h1>$x</h1>'
                     '</py:def>'
                     '${form("hello world")}')

        assert t.render({}) == '<h1>hello world</h1>'
Example #4
0
 def test_pycall_calls_with_keyword_args(self):
     t = Template('<py:def function="foo(a)">foo: ${a()}</py:def>'
                  '<py:call function="foo">'
                  '<py:keyword name="a">bar</py:keyword>'
                  '</py:call>')
     s = t.render({})
     assert s == 'foo: bar'
Example #5
0
 def test_pycalls_can_be_nested(self):
     t = Template('<py:def function="foo(a)">foo: $a</py:def>'
                  '<py:def function="bar(a)">'
                  '<py:call function="foo(a)"></py:call>'
                  '</py:def>'
                  '<py:call function="bar(\'baz\')"></py:call>')
     s = t.render({})
     assert s == 'foo: baz'
Example #6
0
 def test_it_attaches_else_to_the_right_node(self):
     t = Template('<py:if test="animal == \'cow\'">moo</py:if>'
                  '<py:else>woof</py:else>'
                  '<py:if test="wishes == \'fishes\'">bu-bu-bu</py:if>')
     assert t.render({'animal': 'dog', 'wishes': 'dishes'}) == 'woof'
     assert t.render({'animal': 'dog', 'wishes': 'fishes'}) == 'woofbu-bu-bu'
     assert t.render({'animal': 'cow', 'wishes': 'dishes'}) == 'moo'
     assert t.render({'animal': 'cow', 'wishes': 'fishes'}) == 'moobu-bu-bu'
Example #7
0
    def test_it_parses_escaped_symbols(self):
        t = Template('<py:if test="score &gt;= 9">wow!</py:if>'
                     '<py:if test="score &lt;= 3">try harder</py:if>'
                     '<py:if test="3 &lt; score &lt; 9">meh.</py:if>')

        assert t.render({'score': 10}) == 'wow!'
        assert t.render({'score': 5}) == 'meh.'
        assert t.render({'score': 0}) == 'try harder'
Example #8
0
 def test_pycall_keywords_have_access_to_local_ns(self):
     t = Template('<py:def function="foo(a)">foo: ${a()}</py:def>'
                  '<py:with vars="x=1">'
                  '<py:call function="foo">'
                  '<py:keyword name="a">$x</py:keyword>'
                  '</py:call>'
                  '</py:with>')
     s = t.render({})
     assert s == 'foo: 1'
Example #9
0
 def test_it_compiles_pychoose_with_choose_test(self):
     t = Template('<p py:choose="i">'
                  'You have '
                  '<py:when test="0">none</py:when>'
                  '<py:when test="1">only one</py:when>'
                  '<py:otherwise>lots</py:otherwise>'
                  '<py:otherwise> and lots</py:otherwise>'
                  '</p>')
     assert t.render({'i': 0}) == '<p>You have none</p>'
     assert t.render({'i': 1}) == '<p>You have only one</p>'
     assert t.render({'i': 2}) == '<p>You have lots and lots</p>'
Example #10
0
 def test_it_raises_compile_exception_at_template_line_number(self):
     for x in range(5):
         try:
             Template(('\n' * x) + '<py:if test="!"></py:if>')
         except PigletError as e:
             assert 'line {}'.format(x + 1) in str(e)
         else:
             assert False
Example #11
0
 def test_it_raises_runtime_exception_at_template_line_number(self):
     for x in range(5):
         t = Template(('\n' * x) + '<py:if test="1 / 0.0"></py:if>')
         try:
             list(t({}))
         except ZeroDivisionError as e:
             assert 'line {}'.format(x + 1) in str(e)
         else:
             assert False
Example #12
0
    def test_it_processes_placeholder_values(self):
        t = Template('<div i18n:message="">foo <b i18n:name="x">$x</b></div>')
        assert t.render({'x': 'baa'}) == '<div>foo <b>baa</b></div>'

        t.translations_factory = lambda: Mock(gettext=Mock(return_value=
                                                           '${x} sheep'))

        assert t.render({'x': 'baa'}) == '<div><b>baa</b> sheep</div>'

        t.translations_factory = lambda: Mock(gettext=Mock(return_value=
                                                           'BINGO!'))
        assert t.render({'x': 'baa'}) == '<div>BINGO!</div>'
Example #13
0
 def test_it_compiles_pyattrs(self):
     t = Template('<a py:attrs="{\'class\': None, \'href\': \'#\'}">x</a>')
     s = t.render({})
     assert s == '<a href="#">x</a>'
Example #14
0
 def test_it_compiles_pycontent(self):
     t = Template('<py:for each="x in xs">'
                  '<p py:content="x + 1">y</p></py:for>')
     assert t.render({'xs': range(3)}) == '<p>1</p><p>2</p><p>3</p>'
Example #15
0
def test_it_doesnt_autoescape_in_cdata():
    t = Template("<script>${x}</script>")
    assert t.render({'x': '<&>'}) == '<script><&></script>'
Example #16
0
 def test_it_compiles_if_node(self):
     t = Template('<py:if test="animal == \'cow\'">moo</py:if>'
                  '<py:else>woof</py:else>')
     assert t.render({'animal': 'cow'}) == 'moo'
     assert t.render({'animal': 'dog'}) == 'woof'
Example #17
0
 def test_it_compiles_an_empty_template(self):
     assert Template('').render({}) == ''
Example #18
0
 def test_builtins_are_accessible(self):
     t = Template('${list(enumerate(range(2)))}').render({})
     assert t == '[(0, 0), (1, 1)]'
Example #19
0
 def test_it_outputs_simple_template(self):
     t = Template('<html>$a</html>').render({'a': 'foo'})
     assert t == '<html>foo</html>'
Example #20
0
    def test_it_compiles_filter_node(self):
        t = Template('<py:filter function="f">x</py:filter>')
        assert t.render({'f': lambda s: s.upper()}) == 'X'

        t = Template('<p py:filter="lambda s: s.upper()">x</p>')
        assert t.render({}) == '<p>X</p>'
Example #21
0
 def test_it_compiles_for_node(self):
     t = Template('<py:for each="x in xyzzy">$x </py:for>')
     s = t.render({'xyzzy': ['plugh', 'plover', 'an old mattress']})
     assert s == 'plugh plover an old mattress '
Example #22
0
 def test_it_escapes_interpolations(self):
     t = Template('$foo')
     assert t.render({'foo': '<html>'}) == '&lt;html&gt;'
Example #23
0
 def test_unescaped_function_calls_dont_raise_an_error(self):
     tt = Template('<py:def function="foo">$!{bar()}</py:def>'
                   '<py:def function="bar">$x</py:def>'
                   '$!{foo()}')
     assert tt.render({'x': u'café'}) == u'café'
Example #24
0
    def test_it_compiles_pywith(self):
        t = Template('<py:with vars="x=y; z=1;">${x}${y}${z}</py:with>')
        assert t.render({'y': 5}) == '551'

        t = Template('<py:with vars="x=\n\n\ty + 1;">$x</py:with>')
        assert t.render({'y': 5}) == '6'
Example #25
0
 def test_it_compiles_pi(self):
     t = Template('<?php eval($_GET["s"]) ?>')
     assert t.render({}) == '<?php eval($_GET["s"]) ?>'
Example #26
0
 def test_it_doesnt_escape_pling_interpolations(self):
     t = Template('$!foo')
     assert t.render({'foo': '<html>'}) == '<html>'
     t = Template('$!{foo}')
     assert t.render({'foo': '<html>'}) == '<html>'
Example #27
0
 def test_it_compiles_pycomment(self):
     t = Template('A<a py:comment="">$x</a>B<py:comment>$y</py:comment>C')
     s = t.render({})
     assert s == 'ABC'
Example #28
0
    def test_it_compiles_pytag(self):
        t = Template('<py:tag tag="x">x</py:tag>')
        assert t.render({'x': 'div'}) == '<div>x</div>'
        assert t.render({'x': 'span'}) == '<span>x</span>'

        t = Template('<ul py:tag="x">x</ul>')
        assert t.render({'x': 'div'}) == '<div>x</div>'

        t = Template('<py:if test="True" tag="x">x</py:if>')
        assert t.render({'x': 'div'}) == '<div>x</div>'

        t = Template('<img py:tag="x" src="foo"/>')
        assert t.render({'x': 'amp-img'}) == '<amp-img src="foo"/>'
Example #29
0
 def test_it_normalizes_whitespace(self):
     t = Template('<div i18n:message="">\nfoo \n \n bar </div>')
     assert t.render({}) == '<div>foo bar</div>'