[svn] added more jinja unittests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 4f41711 · Browse files · 5 files·+207 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+207 added−3 removed
- tests:+201 added−0 removed
- other:+6 added−3 removed
Provenance
- Commit
44fb2e1- Tree
7f1e66d- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
4f41711
5 files changed+207 added−3 removed
modified:jinja/datastructure.py+6 added-3 removed
137 unmodified lines138 An object that does not exist.139 """140 __slots__ = ()141 142+ def __len__(self):143+ """Getting the length raises error."""144+ raise TemplateRuntimeError('Operated on undefined object')145+146 def __iter__(self):143- """Iterating over `Undefined` returns an empty iterator."""144- if False:145- yield None147+ """Iterating over `Undefined` raises an error."""148+ raise TemplateRuntimeError('Iterated over undefined object')149 150 def __nonzero__(self):151 """`Undefined` is considered boolean `False`"""152 return False429 unmodified linesadded:tests/test_i18n.py+79 added
1+# -*- coding: utf-8 -*-2+"""3+ unit test for the i18n functions4+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~5+6+ :copyright: 2007 by Armin Ronacher.7+ :license: BSD, see LICENSE for more details.8+"""9+from jinja import Environment, DictLoader10+11+templates = {12+ 'master.html': '<title>{{ page_title|default(_("missing")) }}</title>'13+ '{% block body %}{% endblock %}',14+ 'child.html': '{% extends "master.html" %}{% block body %}'15+ '{% trans "watch out" %}{% endblock %}',16+ 'plural.html': '{% trans user_count %}One user online{% pluralize %}'17+ '{{ user_count }} users online{% endtrans %}'18+}19+20+21+languages = {22+ 'de': {23+ 'missing': 'fehlend',24+ 'watch out': 'pass auf',25+ 'One user online': 'Ein Benutzer online',26+ '%(user_count)s users online': '%(user_count)s Benutzer online'27+ }28+}29+30+31+class SimpleTranslator(object):32+ """Yes i know it's only suitable for english and german but33+ that's a stupid unittest..."""34+35+ def __init__(self, language):36+ self.strings = languages.get(language, {})37+38+ def gettext(self, string):39+ return self.strings.get(string, string)40+41+ def ngettext(self, s, p, n):42+ if n != 1:43+ return self.strings.get(p, p)44+ return self.strings.get(s, s)45+46+47+class I18NEnvironment(Environment):48+49+ def __init__(self):50+ super(I18NEnvironment, self).__init__(loader=DictLoader(templates))51+52+ def get_translator(self, context):53+ return SimpleTranslator(context['LANGUAGE'] or 'en')54+55+56+i18n_env = I18NEnvironment()57+58+59+def test_get_translations():60+ trans = list(i18n_env.get_translations('child.html'))61+ assert len(trans) == 162+ assert trans[0] == (1, u'watch out', None)63+64+65+def test_get_translations_for_string():66+ trans = list(i18n_env.get_translations('master.html'))67+ assert len(trans) == 168+ assert trans[0] == (1, u'missing', None)69+70+71+def test_trans():72+ tmpl = i18n_env.get_template('child.html')73+ assert tmpl.render(LANGUAGE='de') == '<title>fehlend</title>pass auf'74+75+76+def test_trans_plural():77+ tmpl = i18n_env.get_template('plural.html')78+ assert tmpl.render(LANGUAGE='de', user_count=1) == 'Ein Benutzer online'79+ assert tmpl.render(LANGUAGE='de', user_count=2) == '2 Benutzer online'modified:tests/test_inheritance.py+34 added
5 unmodified lines6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9 from jinja import Environment, DictLoader10+from jinja.exceptions import TemplateSyntaxError11 12 LAYOUTTEMPLATE = '''\13 |{% block block1 %}block 1 from layout{% endblock %}14 |{% block block2 %}block 2 from layout{% endblock %}20 unmodified lines35 {% extends "level3" %}36 {% block block3 %}block 3 from level4{% endblock %}37 '''38 39+BROKENTEMPLATE = '''\40+{% extends "layout" %}41+{% if false %}42+ {% block block1 %}43+ this is broken44+ {% endblock %}45+{% endif %}46+'''47+48+WORKINGTEMPLATE = '''\49+{% extends "layout" %}50+{% block block1 %}51+ {% if false %}52+ {% block block2 %}53+ this should workd54+ {% endblock %}55+ {% endif %}56+{% endblock %}57+'''58+59 def test_layout(env):60 tmpl = env.get_template('layout')61 assert tmpl.render() == ('|block 1 from layout|block 2 from '62 'layout|nested block 4 from layout|')34 unmodified lines97 '%}[{{ super() }}]{% endblock %}'98 }))99 tmpl = env.get_template('c')100 assert tmpl.render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'101+102+103+def test_broken(env):104+ try:105+ tmpl = env.get_template('broken')106+ except TemplateSyntaxError:107+ pass108+ else:109+ raise RuntimeError('no syntax error occured')110+111+112+def test_working(env):113+ tmpl = env.get_template('working')modified:tests/test_macros.py+21 added
27 unmodified lines28 {% macro m a, b, c='c', d='d' %}{{ a }}|{{ b }}|{{ c }}|{{ d }}{% endmacro %}29 {{ m() }}|{{ m('a') }}|{{ m('a', 'b') }}|{{ m(1, 2, 3) }}\30 '''31 32+PARENTHESES = '''\33+{% macro foo(a, b) %}{{ a }}|{{ b }}{% endmacro %}\34+{{ foo(1, 2) }}\35+'''36+37+VARARGS = '''\38+{% macro test %}{{ varargs|join('|') }}{% endmacro %}\39+{{ test(1, 2, 3) }}\40+'''41+42+43 def test_simple(env):44 tmpl = env.from_string(SIMPLE)45 assert tmpl.render() == 'Hello Peter!'46 16 unmodified lines63 64 def test_arguments(env):65 tmpl = env.from_string(ARGUMENTS)66 assert tmpl.render() == '||c|d|a||c|d|a|b|c|d|1|2|3|d'67+68+69+def test_parentheses(env):70+ tmpl = env.from_string(PARENTHESES)71+ assert tmpl.render() == '1|2'72+73+74+def test_varargs(env):75+ tmpl = env.from_string(VARARGS)76+ assert tmpl.render() == '1|2|3'added:tests/test_undefined.py+67 added
1+# -*- coding: utf-8 -*-2+"""3+ unit test for the undefined singletons4+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~5+6+ :copyright: 2007 by Armin Ronacher.7+ :license: BSD, see LICENSE for more details.8+"""9+10+from jinja import Environment11+from jinja.exceptions import TemplateRuntimeError12+from jinja.datastructure import SilentUndefined, ComplainingUndefined13+14+15+silent_env = Environment(undefined_singleton=SilentUndefined)16+complaining_env = Environment(undefined_singleton=ComplainingUndefined)17+18+19+JUSTUNDEFINED = '''{{ missing }}'''20+DEFINEDUNDEFINED = '''{{ missing is defined }}|{{ given is defined }}'''21+ITERATION = '''{% for item in missing %}{{ item }}{% endfor %}'''22+CONCATENATION = '''{{ missing + [1, 2] + missing + [3] }}'''23+24+25+def test_silent_defined():26+ tmpl = silent_env.from_string(DEFINEDUNDEFINED)27+ assert tmpl.render(given=0) == 'False|True'28+29+30+def test_complaining_defined():31+ tmpl = complaining_env.from_string(DEFINEDUNDEFINED)32+ assert tmpl.render(given=0) == 'False|True'33+34+35+def test_silent_rendering():36+ tmpl = silent_env.from_string(JUSTUNDEFINED)37+ assert tmpl.render() == ''38+39+40+def test_complaining_undefined():41+ tmpl = complaining_env.from_string(JUSTUNDEFINED)42+ try:43+ tmpl.render()44+ except TemplateRuntimeError:45+ pass46+ else:47+ raise ValueError('template runtime error expected')48+49+50+def test_silent_iteration():51+ tmpl = silent_env.from_string(ITERATION)52+ assert tmpl.render() == ''53+54+55+def test_complaining_iteration():56+ tmpl = complaining_env.from_string(ITERATION)57+ try:58+ tmpl.render()59+ except TemplateRuntimeError:60+ pass61+ else:62+ raise ValueError('template runtime error expected')63+64+65+def test_concatenation():66+ tmpl = silent_env.from_string(CONCATENATION)67+ assert tmpl.render() == '[1, 2, 3]'