Skip to content
Myr

[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

5 files+207 added−3 removed

modified:jinja/datastructure.py+6 added-3 removed
137 unmodified lines
138 An object that does not exist.139 """140 __slots__ = ()141142 def __len__(self):143 """Getting the length raises error."""144 raise TemplateRuntimeError('Operated on undefined object')145146 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')149150 def __nonzero__(self):151 """`Undefined` is considered boolean `False`"""152 return False
429 unmodified lines
added:tests/test_i18n.py+79 added
1# -*- coding: utf-8 -*-2"""3 unit test for the i18n functions4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""9from jinja import Environment, DictLoader1011templates = {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}192021languages = {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}293031class SimpleTranslator(object):32 """Yes i know it's only suitable for english and german but33 that's a stupid unittest..."""3435 def __init__(self, language):36 self.strings = languages.get(language, {})3738 def gettext(self, string):39 return self.strings.get(string, string)4041 def ngettext(self, s, p, n):42 if n != 1:43 return self.strings.get(p, p)44 return self.strings.get(s, s)454647class I18NEnvironment(Environment):4849 def __init__(self):50 super(I18NEnvironment, self).__init__(loader=DictLoader(templates))5152 def get_translator(self, context):53 return SimpleTranslator(context['LANGUAGE'] or 'en')545556i18n_env = I18NEnvironment()575859def 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)636465def 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)697071def test_trans():72 tmpl = i18n_env.get_template('child.html')73 assert tmpl.render(LANGUAGE='de') == '<title>fehlend</title>pass auf'747576def 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 lines
6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""9from jinja import Environment, DictLoader10from jinja.exceptions import TemplateSyntaxError1112LAYOUTTEMPLATE = '''\13|{% block block1 %}block 1 from layout{% endblock %}14|{% block block2 %}block 2 from layout{% endblock %}
20 unmodified lines
35{% extends "level3" %}36{% block block3 %}block 3 from level4{% endblock %}37'''3839BROKENTEMPLATE = '''\40{% extends "layout" %}41{% if false %}42 {% block block1 %}43 this is broken44 {% endblock %}45{% endif %}46'''4748WORKINGTEMPLATE = '''\49{% extends "layout" %}50{% block block1 %}51 {% if false %}52 {% block block2 %}53 this should workd54 {% endblock %}55 {% endif %}56{% endblock %}57'''5859def 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 lines
97 '%}[{{ super() }}]{% endblock %}'98 }))99 tmpl = env.get_template('c')100 assert tmpl.render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'101102103def test_broken(env):104 try:105 tmpl = env.get_template('broken')106 except TemplateSyntaxError:107 pass108 else:109 raise RuntimeError('no syntax error occured')110111112def test_working(env):113 tmpl = env.get_template('working')
modified:tests/test_macros.py+21 added
27 unmodified lines
28{% 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'''3132PARENTHESES = '''\33{% macro foo(a, b) %}{{ a }}|{{ b }}{% endmacro %}\34{{ foo(1, 2) }}\35'''3637VARARGS = '''\38{% macro test %}{{ varargs|join('|') }}{% endmacro %}\39{{ test(1, 2, 3) }}\40'''414243def test_simple(env):44 tmpl = env.from_string(SIMPLE)45 assert tmpl.render() == 'Hello Peter!'46
16 unmodified lines
6364def 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'676869def test_parentheses(env):70 tmpl = env.from_string(PARENTHESES)71 assert tmpl.render() == '1|2'727374def 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""910from jinja import Environment11from jinja.exceptions import TemplateRuntimeError12from jinja.datastructure import SilentUndefined, ComplainingUndefined131415silent_env = Environment(undefined_singleton=SilentUndefined)16complaining_env = Environment(undefined_singleton=ComplainingUndefined)171819JUSTUNDEFINED = '''{{ missing }}'''20DEFINEDUNDEFINED = '''{{ missing is defined }}|{{ given is defined }}'''21ITERATION = '''{% for item in missing %}{{ item }}{% endfor %}'''22CONCATENATION = '''{{ missing + [1, 2] + missing + [3] }}'''232425def test_silent_defined():26 tmpl = silent_env.from_string(DEFINEDUNDEFINED)27 assert tmpl.render(given=0) == 'False|True'282930def test_complaining_defined():31 tmpl = complaining_env.from_string(DEFINEDUNDEFINED)32 assert tmpl.render(given=0) == 'False|True'333435def test_silent_rendering():36 tmpl = silent_env.from_string(JUSTUNDEFINED)37 assert tmpl.render() == ''383940def 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')484950def test_silent_iteration():51 tmpl = silent_env.from_string(ITERATION)52 assert tmpl.render() == ''535455def 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')636465def test_concatenation():66 tmpl = silent_env.from_string(CONCATENATION)67 assert tmpl.render() == '[1, 2, 3]'