From 44fb2e192388073118efe1cc43df0b6ccf044105 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 29 Apr 2007 09:45:00 +0200 Subject: [PATCH] [svn] added more jinja unittests --HG-- branch : trunk --- jinja/datastructure.py | 9 +++-- tests/test_i18n.py | 79 +++++++++++++++++++++++++++++++++++++++ tests/test_inheritance.py | 34 +++++++++++++++++ tests/test_macros.py | 21 +++++++++++ tests/test_undefined.py | 67 +++++++++++++++++++++++++++++++++ 5 files changed, 207 insertions(+), 3 deletions(-) create mode 100644 tests/test_i18n.py create mode 100644 tests/test_undefined.py diff --git a/jinja/datastructure.py b/jinja/datastructure.py index 01952df..86b74c7 100644 --- a/jinja/datastructure.py +++ b/jinja/datastructure.py @@ -139,10 +139,13 @@ class ComplainingUndefinedType(AbstractUndefinedType): """ __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`""" diff --git a/tests/test_i18n.py b/tests/test_i18n.py new file mode 100644 index 0000000..ddd5841 --- /dev/null +++ b/tests/test_i18n.py @@ -0,0 +1,79 @@ +# -*- 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': '{{ page_title|default(_("missing")) }}' + '{% 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') == 'fehlendpass 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' diff --git a/tests/test_inheritance.py b/tests/test_inheritance.py index 74d5181..2e5e8a5 100644 --- a/tests/test_inheritance.py +++ b/tests/test_inheritance.py @@ -7,6 +7,7 @@ :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 %} @@ -35,6 +36,26 @@ LEVEL4TEMPLATE = '''\ {% 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 ' @@ -77,3 +98,16 @@ def test_super(): })) 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') diff --git a/tests/test_macros.py b/tests/test_macros.py index 35cb291..3059384 100644 --- a/tests/test_macros.py +++ b/tests/test_macros.py @@ -29,6 +29,17 @@ ARGUMENTS = '''\ {{ 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!' @@ -53,3 +64,13 @@ def test_scoping(env): 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' diff --git a/tests/test_undefined.py b/tests/test_undefined.py new file mode 100644 index 0000000..2e83104 --- /dev/null +++ b/tests/test_undefined.py @@ -0,0 +1,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]' -- 2.26.2