Exemplo n.º 1
0
 def test_kwargs_call(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ a|join(sep=",") }}', {
             'a': ['A', 'B']
         }, 'A,B'),
         ('{{ a|tests.filters.join(sep=",") }}', {
             'a': ['A', 'B']
         }, 'A,B'),
         ('{{ a|join(sep=",")|join(sep=":") }}', {
             'a': ['A', 'B']
         }, 'A:,:B'),
         ('{{ a|tests.filters.join(sep=",")|tests.filters.join(sep=":") }}',
          {
              'a': ['A', 'B']
          }, 'A:,:B'),
         ('{{ a|tests.filters.join(sep=",")|join(sep=":") }}', {
             'a': ['A', 'B']
         }, 'A:,:B'),
         ('{{ a|join(sep=",")|tests.filters.join(sep=":") }}', {
             'a': ['A', 'B']
         }, 'A:,:B'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 2
0
    def test_args_call(self):
        # A list of (template, context, output)
        TESTS = (
            ('{{ a|join(",") }}', {
                'a': ['A', 'B']
            }, 'A,B'),
            ('{{ a|tests.filters.join(",") }}', {
                'a': ['A', 'B']
            }, 'A,B'),
            ('{{ a|join(",")|join(":") }}', {
                'a': ['A', 'B']
            }, 'A:,:B'),
            ('{{ a|tests.filters.join(",")|tests.filters.join(":") }}', {
                'a': ['A', 'B']
            }, 'A:,:B'),
            ('{{ a|tests.filters.join(",")|join(":") }}', {
                'a': ['A', 'B']
            }, 'A:,:B'),
            ('{{ a|join(",")|tests.filters.join(":") }}', {
                'a': ['A', 'B']
            }, 'A:,:B'),

            # legacy syntax
            ('{{ a|join:"," }}', {
                'a': ['1', '2', '3']
            }, '1,2,3'),
        )
        for src, context, expect in TESTS:
            tmpl = Template(src)
            output = tmpl.render(context)
            self.assertRendered(output, expect, src)
Exemplo n.º 3
0
 def test_direct(self):
     # A list of (template, context, output)
     TESTS = (('{{ a }}', {'a': 'yes'}, 'yes'), )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 4
0
 def test_full_call_mixed(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ a|lcjoin(",", lower=True) }}', {
             'a': ['A', 'B']
         }, 'a,b'),
         ('{{ a|tests.filters.lcjoin(",", lower=True) }}', {
             'a': ['A', 'B']
         }, 'a,b'),
         ('{{ a|lcjoin(",", lower=True)|lcjoin(":", lower=True) }}', {
             'a': ['A', 'B']
         }, 'a:,:b'),
         (('{{ a|tests.filters.lcjoin(",", lower=True)'
           '|tests.filters.lcjoin(":", lower=True) }}'), {
               'a': ['A', 'B']
           }, 'a:,:b'),
         (('{{ a|tests.filters.lcjoin(",", lower=True)'
           '|lcjoin(":", lower=True) }}'), {
               'a': ['A', 'B']
           }, 'a:,:b'),
         (('{{ a|lcjoin(",", lower=True)'
           '|tests.filters.lcjoin(":", lower=True) }}'), {
               'a': ['A', 'B']
           }, 'a:,:b'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 5
0
 def test_mixed(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 1 + 2 - 3 + 4 }}', {}, '4'),
         ('{{ 1 + 2 * 3 }}', {}, '7'),
         ('{{ 1 + (2 * 3) }}', {}, '7'),
         ('{{ (1 + 2) * 3 }}', {}, '9'),
         ('{{ a(b + 3, 4) }}', {
             'a': lambda x, y: x * y,
             'b': 2
         }, '20'),
         ('{{ a[0] + a[1] * a[2] }}', {
             'a': [2, 3, 4]
         }, '14'),
         ('{{ a[0].x + a[1].x * b.y[0] }}', {
             'a': [Mock(x=2), Mock(x=3)],
             'b': Mock(y=[4])
         }, '14'),
         ('{{ (a[0].x + a[1].x) * b.y[0] }}', {
             'a': [Mock(x=2), Mock(x=3)],
             'b': Mock(y=[4])
         }, '20'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 6
0
 def test_greater(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 0>1 }}', {}, 'False'),
         ('{{ 1>1 }}', {}, 'False'),
         ('{{ 2>1 }}', {}, 'True'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 7
0
 def test_multiple_filter_short_name(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 42|quote|squote }}', {}, ''"42"''),
         ('{{ 13.37|quote|squote }}', {}, ''"13.37"''),
         ('{{ "world"|quote|squote }}', {}, ''"world"''),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 8
0
 def test_single_filter_full_name(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 42|tests.filters.quote }}', {}, '"42"'),
         ('{{ 13.37|tests.filters.quote }}', {}, '"13.37"'),
         ('{{ "world"|tests.filters.quote }}', {}, '"world"'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 9
0
 def test_less_equal(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 1<=0 }}', {}, 'False'),
         ('{{ 1<=1 }}', {}, 'True'),
         ('{{ 1<=2 }}', {}, 'True'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 10
0
 def test_notin(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 0 not in a }}', {
             'a': [1, 2]
         }, 'True'),
         ('{{ 1 not in a }}', {
             'a': [1, 2]
         }, 'False'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 11
0
 def test_renderNumLiteral(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 23 }}', '23'),
         ('{{ 2.3 }}', '2.3'),
         ('{{ 12.34 }}', '12.34'),
         ('{{ 12e1 }}', '120.0'),
         ('{{ 12E1 }}', '120.0'),
         ('{{ 12e-1 }}', '1.2'),
         ('{{ 12E-1 }}', '1.2'),
     )
     for src, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render()
         self.assertRendered(output, expect, src)
Exemplo n.º 12
0
 def test_single_filter(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 42|hello_filter }}', {
             'hello_filter': hello_filter
         }, 'Hello 42!'),
         ('{{ 13.37|hello_filter }}', {
             'hello_filter': hello_filter
         }, 'Hello 13.37!'),
         ('{{ "world"|hello_filter }}', {
             'hello_filter': hello_filter
         }, 'Hello world!'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 13
0
def select_template(templates, template_dirs=None):
    if template_dirs is None:
        template_dirs = TEMPLATE_DIRS
    if isinstance(templates, str):
        templates = [templates]
    for template in templates:
        for tdir in template_dirs:
            full_path = os.path.abspath(os.path.join(tdir, template))
            if not full_path.startswith(tdir):
                raise ValueError('Suspicious template name: %s [%s]' % (template, tdir))
            try:
                src = open(os.path.join(tdir, template), 'r').read()
            except IOError:
                pass
            else:
                return Template(src, full_path)
    raise ValueError('Not Found')
Exemplo n.º 14
0
 def test_empty_call(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ "world"|quote() }}', {}, '&quot;world&quot;'),
         ('{{ "world"|tests.filters.quote() }}', {}, '&quot;world&quot;'),
         ('{{ "world"|tests.filters.quote()|tests.filters.squote }}', {},
          '&#39;&quot;world&quot;&#39;'),
         ('{{ "world"|quote()|squote }}', {},
          '&#39;&quot;world&quot;&#39;'),
         ('{{ "world"|tests.filters.quote()|squote }}', {},
          '&#39;&quot;world&quot;&#39;'),
         ('{{ "world"|quote()|tests.filters.squote }}', {},
          '&#39;&quot;world&quot;&#39;'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 15
0
 def test_attribute(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ a.b }}', {
             'a': Mock(b=1)
         }, '1'),
         ('{{ a.b.c }}', {
             'a': Mock(b=Mock(c=1))
         }, '1'),
         ('{{ a["b"].c }}', {
             'a': {
                 'b': Mock(c=1)
             }
         }, '1'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 16
0
 def test_multiple_filters(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 42|bye_filter|hello_filter }}', {
             'hello_filter': hello_filter
         }, 'Hello Bye 42!!'),
         ('{{ 13.37|bye_filter|hello_filter }}', {
             'hello_filter': hello_filter
         }, 'Hello Bye 13.37!!'),
         ('{{ "world"|hello_filter|bye_filter }}', {
             'hello_filter': hello_filter
         }, 'Bye Hello world!!'),
         ('{{ "world"|bye_filter|hello_filter }}', {
             'hello_filter': hello_filter
         }, 'Hello Bye world!!'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 17
0
 def test_pipe_precendence(self):
     # A list of (template, context, output)
     TESTS = (
         # ASSIGN
         ('{{ "foo"|join(sep="::"|join(",")) }}', {}, 'f:,:o:,:o'),
         # BinOp
         ('{{ 21 + 21|quote }}', {}, '&quot;42&quot;'),
         ('{{ 21 - 21|quote }}', {}, '&quot;0&quot;'),
         ('{{ 21 * 21|quote }}', {}, '&quot;441&quot;'),
         ('{{ 21 / 21|quote }}', {},
          '&quot;1.0&quot;' if PY3 else '&quot;1&quot;'),
         # LSQB / RSQB
         ('{{ a[1]|join(",") }}', {
             'a': ['yes', 'no']
         }, 'n,o'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 18
0
 def test_index(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ a[1] }}', {
             'a': ['yes', 'no']
         }, 'no'),
         ('{{ a["b"] }}', {
             'a': {
                 'b': 'yes'
             }
         }, 'yes'),
         ('{{ a[c] }}', {
             'a': {
                 'b': 'yes',
                 'c': 'no'
             },
             'c': 'b'
         }, 'yes'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 19
0
 def test_mod(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 6 % 4 }}', {}, '2'),
         ('{{ a % 4 }}', {
             'a': 6
         }, '2'),
         ('{{ 6 % b }}', {
             'b': 4
         }, '2'),
         ('{{ a % b }}', {
             'a': 6,
             'b': 4
         }, '2'),
         ('{{ a % a }}', {
             'a': 6
         }, '0'),
         ('{{ 2.0 % 1.2 }}', {}, '0.8'),
         ('{{ a % 1.2 }}', {
             'a': 2.0
         }, '0.8'),
         ('{{ 2.0 % b }}', {
             'b': 1.2
         }, '0.8'),
         ('{{ a % b }}', {
             'a': 2.0,
             'b': 1.2
         }, '0.8'),
         ('{{ a % a }}', {
             'a': 2.0
         }, '0.0'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 20
0
 def test_div(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 6 / 3 }}', {}, '2.0' if PY3 else '2'),
         ('{{ a / 3 }}', {
             'a': 6
         }, '2.0' if PY3 else '2'),
         ('{{ 6 / b }}', {
             'b': 3
         }, '2.0' if PY3 else '2'),
         ('{{ a / b }}', {
             'a': 6,
             'b': 3
         }, '2.0' if PY3 else '2'),
         ('{{ a / a }}', {
             'a': 6
         }, '1.0' if PY3 else '1'),
         ('{{ 3.5 / 1.4 }}', {}, '2.5'),
         ('{{ a / 1.4 }}', {
             'a': 3.5
         }, '2.5'),
         ('{{ 3.5 / b }}', {
             'b': 1.4
         }, '2.5'),
         ('{{ a / b }}', {
             'a': 3.5,
             'b': 1.4
         }, '2.5'),
         ('{{ a / a }}', {
             'a': 3.5
         }, '1.0'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 21
0
 def test_mult(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 2 * 3 }}', {}, '6'),
         ('{{ a * 3 }}', {
             'a': 2
         }, '6'),
         ('{{ 2 * b }}', {
             'b': 3
         }, '6'),
         ('{{ a * b }}', {
             'a': 2,
             'b': 3
         }, '6'),
         ('{{ a * a }}', {
             'a': 2
         }, '4'),
         ('{{ 1.5 * 3.9 }}', {}, '5.85'),
         ('{{ a * 3.9 }}', {
             'a': 1.5
         }, '5.85'),
         ('{{ 1.5 * b }}', {
             'b': 3.9
         }, '5.85'),
         ('{{ a * b }}', {
             'a': 1.5,
             'b': 3.9
         }, '5.85'),
         ('{{ a * a }}', {
             'a': 1.5
         }, '2.25'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 22
0
 def test_sub(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 2 - 1 }}', {}, '1'),
         ('{{ a - 1 }}', {
             'a': 2
         }, '1'),
         ('{{ 2 - b }}', {
             'b': 1
         }, '1'),
         ('{{ a - b }}', {
             'a': 2,
             'b': 1
         }, '1'),
         ('{{ a - a }}', {
             'a': 2
         }, '0'),
         ('{{ 2.9 - 1.7 }}', {}, '1.2'),
         ('{{ a - 1.7 }}', {
             'a': 2.9
         }, '1.2'),
         ('{{ 2.9 - b }}', {
             'b': 1.7
         }, '1.2'),
         ('{{ a - b }}', {
             'a': 2.9,
             'b': 1.7
         }, '1.2'),
         ('{{ a - a }}', {
             'a': 2.9
         }, '0.0'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 23
0
 def test_add(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 1 + 2 }}', {}, '3'),
         ('{{ a + 2 }}', {
             'a': 1
         }, '3'),
         ('{{ 1 + b }}', {
             'b': 2
         }, '3'),
         ('{{ a + b }}', {
             'a': 1,
             'b': 2
         }, '3'),
         ('{{ a + a }}', {
             'a': 1
         }, '2'),
         ('{{ 1.8 + 2.9 }}', {}, '4.7'),
         ('{{ a + 2.9 }}', {
             'a': 1.8
         }, '4.7'),
         ('{{ 1.8 + b }}', {
             'b': 2.9
         }, '4.7'),
         ('{{ a + b }}', {
             'a': 1.8,
             'b': 2.9
         }, '4.7'),
         ('{{ a + a }}', {
             'a': 1.8
         }, '3.6'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Exemplo n.º 24
0
 def test_render_plain_text(self):
     tmpl = Template("Hello {{ 'world' }}!")
     output = tmpl.render()
     self.assertRendered(output, 'Hello world!', tmpl)
Exemplo n.º 25
0
    def test_function(self):
        def f_arg(arg1):
            return '%s' % (arg1)

        def f_args(arg1, arg2):
            return '%s %s' % (arg1, arg2)

        # A list of (template, context, output)
        TESTS = (
            ('{{ a(1) }}', {
                'a': lambda x: x
            }, '1'),
            ('{{ a(b,1) }}', {
                'a': lambda x, y: x + y,
                'b': 6
            }, '7'),

            # args
            ('{{ a(1) }}', {
                'a': f_arg
            }, '1'),
            ('{{ a(1, 2) }}', {
                'a': f_args
            }, '1 2'),

            # kwargs
            ('{{ a(arg1=1) }}', {
                'a': f_arg
            }, '1'),
            ('{{ a(arg1=1, arg2=2) }}', {
                'a': f_args
            }, '1 2'),

            # args + kwargs
            ('{{ a(1, arg2=2) }}', {
                'a': f_args
            }, '1 2'),

            # with vars
            ('{{ a(x) }}', {
                'a': f_arg,
                'x': 1
            }, '1'),
            ('{{ a(x, y) }}', {
                'a': f_args,
                'x': 1,
                'y': 2
            }, '1 2'),
            ('{{ a(arg1=x) }}', {
                'a': f_arg,
                'x': 1
            }, '1'),
            ('{{ a(arg1=x, arg2=y) }}', {
                'a': f_args,
                'x': 1,
                'y': 2
            }, '1 2'),
            ('{{ a(x, arg2=y) }}', {
                'a': f_args,
                'x': 1,
                'y': 2
            }, '1 2'),
        )
        for src, context, expect in TESTS:
            tmpl = Template(src)
            output = tmpl.render(context)
            self.assertRendered(output, expect, src)
Exemplo n.º 26
0
 def test_renderStringLiteral(self):
     tmpl = Template("{{ 'hello' }}")
     output = tmpl.render()
     self.assertRendered(output, 'hello', tmpl)