Пример #1
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)
Пример #2
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)
Пример #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)
Пример #4
0
 def test_is(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ a is a }}', {'a': [1, 2], 'b': [3, 4]}, 'True'),
         ('{{ a is b }}', {'a': [1, 2], 'b': [3, 4]}, 'False'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Пример #5
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)
Пример #6
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)
Пример #7
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)
Пример #8
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)
Пример #9
0
 def test_single_filter_short_name(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 42|quote }}', {}, '"42"'),
         ('{{ 13.37|quote }}', {}, '"13.37"'),
         ('{{ "world"|quote }}', {}, '"world"'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Пример #10
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)
Пример #11
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)
Пример #12
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)
Пример #13
0
 def test_full_name_short_name(self):
     # A list of (template, context, output)
     TESTS = (
         ('{{ 42|tests.filters.quote|squote }}', {},
          '&#39;&quot;42&quot;&#39;'),
         ('{{ 13.37|tests.filters.quote|squote }}', {},
          '&#39;&quot;13.37&quot;&#39;'),
         ('{{ "world"|tests.filters.quote|squote }}', {},
          '&#39;&quot;world&quot;&#39;'),
     )
     for src, context, expect in TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertRendered(output, expect, src)
Пример #14
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)
Пример #15
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)
Пример #16
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)
Пример #17
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)
Пример #18
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)
Пример #19
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)
Пример #20
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)
Пример #21
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)
Пример #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)
Пример #23
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)
Пример #24
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)
Пример #25
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)
Пример #26
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)
Пример #27
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)
Пример #28
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)
Пример #29
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)
Пример #30
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)
Пример #31
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)
Пример #32
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)
Пример #33
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)
Пример #34
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)
Пример #35
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)
Пример #36
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)
Пример #37
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)
Пример #38
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)
Пример #39
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')
Пример #40
0
 def test_rendering(self):
     for src, context, expect in self. TESTS:
         tmpl = Template(src)
         output = tmpl.render(context)
         self.assertEqual(output, expect)
Пример #41
0
 def test_renderNumLiteral(self):
     tmpl = Template('{{ 23 }}')
     output = tmpl.render()
     self.assertEqual(output, '23')
Пример #42
0
 def test_renderStringLiteral(self):
     tmpl = Template("{{ 'hello' }}")
     output = tmpl.render()
     self.assertEqual(output, 'hello')
Пример #43
0
 def test_render_plain_text(self):
     tmpl = Template("Hello {{ 'world' }}!")
     output = tmpl.render()
     self.assertRendered(output, 'Hello world!', tmpl)
Пример #44
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)
Пример #45
0
 def test_render_plain_text(self):
     tmpl = Template("Hello {{ 'world' }}!")
     output = tmpl.render()
     self.assertRendered(output, 'Hello world!', tmpl)
Пример #46
0
 def test_renderStringLiteral(self):
     tmpl = Template("{{ 'hello' }}")
     output = tmpl.render()
     self.assertRendered(output, 'hello', tmpl)