File: test_undefined.py

package info (click to toggle)
jinja 1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,408 kB
  • ctags: 1,171
  • sloc: python: 6,438; ansic: 397; makefile: 74
file content (67 lines) | stat: -rw-r--r-- 1,806 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -*- coding: utf-8 -*-
"""
    unit test for the undefined singletons
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: 2007 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
"""

from jinja import Environment
from jinja.exceptions import TemplateRuntimeError
from jinja.datastructure import SilentUndefined, ComplainingUndefined


silent_env = Environment(undefined_singleton=SilentUndefined)
complaining_env = Environment(undefined_singleton=ComplainingUndefined)


JUSTUNDEFINED = '''{{ missing }}'''
DEFINEDUNDEFINED = '''{{ missing is defined }}|{{ given is defined }}'''
ITERATION = '''{% for item in missing %}{{ item }}{% endfor %}'''
CONCATENATION = '''{{ missing + [1, 2] + missing + [3] }}'''


def test_silent_defined():
    tmpl = silent_env.from_string(DEFINEDUNDEFINED)
    assert tmpl.render(given=0) == 'False|True'


def test_complaining_defined():
    tmpl = complaining_env.from_string(DEFINEDUNDEFINED)
    assert tmpl.render(given=0) == 'False|True'


def test_silent_rendering():
    tmpl = silent_env.from_string(JUSTUNDEFINED)
    assert tmpl.render() == ''


def test_complaining_undefined():
    tmpl = complaining_env.from_string(JUSTUNDEFINED)
    try:
        tmpl.render()
    except TemplateRuntimeError:
        pass
    else:
        raise ValueError('template runtime error expected')


def test_silent_iteration():
    tmpl = silent_env.from_string(ITERATION)
    assert tmpl.render() == ''


def test_complaining_iteration():
    tmpl = complaining_env.from_string(ITERATION)
    try:
        tmpl.render()
    except TemplateRuntimeError:
        pass
    else:
        raise ValueError('template runtime error expected')


def test_concatenation():
    tmpl = silent_env.from_string(CONCATENATION)
    assert tmpl.render() == '[1, 2, 3]'