"""
__slots__ = ()
+ def __len__(self):
+ """Getting the length raises error."""
+ raise TemplateRuntimeError('Operated on undefined object')
+
def __iter__(self):
- """Iterating over `Undefined` returns an empty iterator."""
- if False:
- yield None
+ """Iterating over `Undefined` raises an error."""
+ raise TemplateRuntimeError('Iterated over undefined object')
def __nonzero__(self):
"""`Undefined` is considered boolean `False`"""
--- /dev/null
+# -*- coding: utf-8 -*-
+"""
+ unit test for the i18n functions
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ :copyright: 2007 by Armin Ronacher.
+ :license: BSD, see LICENSE for more details.
+"""
+from jinja import Environment, DictLoader
+
+templates = {
+ 'master.html': '<title>{{ page_title|default(_("missing")) }}</title>'
+ '{% block body %}{% endblock %}',
+ 'child.html': '{% extends "master.html" %}{% block body %}'
+ '{% trans "watch out" %}{% endblock %}',
+ 'plural.html': '{% trans user_count %}One user online{% pluralize %}'
+ '{{ user_count }} users online{% endtrans %}'
+}
+
+
+languages = {
+ 'de': {
+ 'missing': 'fehlend',
+ 'watch out': 'pass auf',
+ 'One user online': 'Ein Benutzer online',
+ '%(user_count)s users online': '%(user_count)s Benutzer online'
+ }
+}
+
+
+class SimpleTranslator(object):
+ """Yes i know it's only suitable for english and german but
+ that's a stupid unittest..."""
+
+ def __init__(self, language):
+ self.strings = languages.get(language, {})
+
+ def gettext(self, string):
+ return self.strings.get(string, string)
+
+ def ngettext(self, s, p, n):
+ if n != 1:
+ return self.strings.get(p, p)
+ return self.strings.get(s, s)
+
+
+class I18NEnvironment(Environment):
+
+ def __init__(self):
+ super(I18NEnvironment, self).__init__(loader=DictLoader(templates))
+
+ def get_translator(self, context):
+ return SimpleTranslator(context['LANGUAGE'] or 'en')
+
+
+i18n_env = I18NEnvironment()
+
+
+def test_get_translations():
+ trans = list(i18n_env.get_translations('child.html'))
+ assert len(trans) == 1
+ assert trans[0] == (1, u'watch out', None)
+
+
+def test_get_translations_for_string():
+ trans = list(i18n_env.get_translations('master.html'))
+ assert len(trans) == 1
+ assert trans[0] == (1, u'missing', None)
+
+
+def test_trans():
+ tmpl = i18n_env.get_template('child.html')
+ assert tmpl.render(LANGUAGE='de') == '<title>fehlend</title>pass auf'
+
+
+def test_trans_plural():
+ tmpl = i18n_env.get_template('plural.html')
+ assert tmpl.render(LANGUAGE='de', user_count=1) == 'Ein Benutzer online'
+ assert tmpl.render(LANGUAGE='de', user_count=2) == '2 Benutzer online'
:license: BSD, see LICENSE for more details.
"""
from jinja import Environment, DictLoader
+from jinja.exceptions import TemplateSyntaxError
LAYOUTTEMPLATE = '''\
|{% block block1 %}block 1 from layout{% endblock %}
{% block block3 %}block 3 from level4{% endblock %}
'''
+BROKENTEMPLATE = '''\
+{% extends "layout" %}
+{% if false %}
+ {% block block1 %}
+ this is broken
+ {% endblock %}
+{% endif %}
+'''
+
+WORKINGTEMPLATE = '''\
+{% extends "layout" %}
+{% block block1 %}
+ {% if false %}
+ {% block block2 %}
+ this should workd
+ {% endblock %}
+ {% endif %}
+{% endblock %}
+'''
+
def test_layout(env):
tmpl = env.get_template('layout')
assert tmpl.render() == ('|block 1 from layout|block 2 from '
}))
tmpl = env.get_template('c')
assert tmpl.render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'
+
+
+def test_broken(env):
+ try:
+ tmpl = env.get_template('broken')
+ except TemplateSyntaxError:
+ pass
+ else:
+ raise RuntimeError('no syntax error occured')
+
+
+def test_working(env):
+ tmpl = env.get_template('working')
{{ m() }}|{{ m('a') }}|{{ m('a', 'b') }}|{{ m(1, 2, 3) }}\
'''
+PARENTHESES = '''\
+{% macro foo(a, b) %}{{ a }}|{{ b }}{% endmacro %}\
+{{ foo(1, 2) }}\
+'''
+
+VARARGS = '''\
+{% macro test %}{{ varargs|join('|') }}{% endmacro %}\
+{{ test(1, 2, 3) }}\
+'''
+
+
def test_simple(env):
tmpl = env.from_string(SIMPLE)
assert tmpl.render() == 'Hello Peter!'
def test_arguments(env):
tmpl = env.from_string(ARGUMENTS)
assert tmpl.render() == '||c|d|a||c|d|a|b|c|d|1|2|3|d'
+
+
+def test_parentheses(env):
+ tmpl = env.from_string(PARENTHESES)
+ assert tmpl.render() == '1|2'
+
+
+def test_varargs(env):
+ tmpl = env.from_string(VARARGS)
+ assert tmpl.render() == '1|2|3'
--- /dev/null
+# -*- 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]'