Exemplo n.º 1
0
    def test_try(self):
        template = Template(
            utf8("""{% try %}
try{% set y = 1/x %}
{% except %}-except
{% else %}-else
{% finally %}-finally
{% end %}"""))
        self.assertEqual(template.generate(x=1), b"\ntry\n-else\n-finally\n")
        self.assertEqual(template.generate(x=0), b"\ntry-except\n-finally\n")
Exemplo n.º 2
0
 def test_unicode_literal_expression(self):
     # Unicode literals should be usable in templates.  Note that this
     # test simulates unicode characters appearing directly in the
     # template file (with utf8 encoding), i.e. \u escapes would not
     # be used in the template file itself.
     if str is unicode_type:
         # python 3 needs a different version of this test since
         # 2to3 doesn't run on template internals
         template = Template(utf8(u'{{ "\u00e9" }}'))
     else:
         template = Template(utf8(u'{{ u"\u00e9" }}'))
     self.assertEqual(template.generate(), utf8(u"\u00e9"))
Exemplo n.º 3
0
    def test_break_continue(self):
        template = Template(
            utf8("""\
{% for i in range(10) %}
    {% if i == 2 %}
        {% continue %}
    {% end %}
    {{ i }}
    {% if i == 6 %}
        {% break %}
    {% end %}
{% end %}"""))
        result = template.generate()
        # remove extraneous whitespace
        result = b''.join(result.split())
        self.assertEqual(result, b"013456")
Exemplo n.º 4
0
 def test_break_in_apply(self):
     # This test verifies current behavior, although of course it would
     # be nice if apply didn't cause seemingly unrelated breakage
     try:
         Template(
             utf8(
                 "{% for i in [] %}{% apply foo %}{% break %}{% end %}{% end %}"
             ))
         raise Exception("Did not get expected exception")
     except ParseError:
         pass
Exemplo n.º 5
0
 def test_escaping(self):
     self.assertRaises(ParseError, lambda: Template("{{"))
     self.assertRaises(ParseError, lambda: Template("{%"))
     self.assertEqual(Template("{{!").generate(), b"{{")
     self.assertEqual(Template("{%!").generate(), b"{%")
     self.assertEqual(Template("{#!").generate(), b"{#")
     self.assertEqual(
         Template("{{ 'expr' }} {{!jquery expr}}").generate(),
         b"expr {{jquery expr}}")
Exemplo n.º 6
0
 def test_unicode_template(self):
     template = Template(utf8(u"\u00e9"))
     self.assertEqual(template.generate(), utf8(u"\u00e9"))
Exemplo n.º 7
0
 def test_comment(self):
     template = Template("Hello{# TODO i18n #} {{ name }}!")
     self.assertEqual(template.generate(name=utf8("Ben")), b"Hello Ben!")
Exemplo n.º 8
0
 def test_expressions(self):
     template = Template("2 + 2 = {{ 2 + 2 }}")
     self.assertEqual(template.generate(), b"2 + 2 = 4")
Exemplo n.º 9
0
 def test_bytes(self):
     template = Template("Hello {{ name }}!")
     self.assertEqual(template.generate(name=utf8("Ben")), b"Hello Ben!")
Exemplo n.º 10
0
 def test_no_inherit_future(self):
     # This file has from __future__ import division...
     self.assertEqual(1 / 2, 0.5)
     # ...but the template doesn't
     template = Template('{{ 1 / 2 }}')
     self.assertEqual(template.generate(), '0')
Exemplo n.º 11
0
 def test_if(self):
     template = Template(utf8("{% if x > 4 %}yes{% else %}no{% end %}"))
     self.assertEqual(template.generate(x=5), b"yes")
     self.assertEqual(template.generate(x=3), b"no")
Exemplo n.º 12
0
 def test_break_outside_loop(self):
     try:
         Template(utf8("{% break %}"))
         raise Exception("Did not get expected exception")
     except ParseError:
         pass
Exemplo n.º 13
0
 def test_simple(self):
     template = Template("Hello {{ name }}!")
     self.assertEqual(template.generate(name="Ben"), b"Hello Ben!")
Exemplo n.º 14
0
 def test_comment_directive(self):
     template = Template(utf8("{% comment blah blah %}foo"))
     self.assertEqual(template.generate(), b"foo")
Exemplo n.º 15
0
 def test_if_empty_body(self):
     template = Template(utf8("{% if True %}{% else %}{% end %}"))
     self.assertEqual(template.generate(), b"")
Exemplo n.º 16
0
    def test_apply(self):
        def upper(s):
            return s.upper()

        template = Template(utf8("{% apply upper %}foo{% end %}"))
        self.assertEqual(template.generate(upper=upper), b"FOO")
Exemplo n.º 17
0
    def test_bytes_apply(self):
        def upper(s):
            return utf8(to_unicode(s).upper())

        template = Template(utf8(u"{% apply upper %}foo \u00e9{% end %}"))
        self.assertEqual(template.generate(upper=upper), utf8(u"FOO \u00c9"))