Skip to content
Myr

added more unittests for various tasks

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 e62b7ef · Browse files · 7 files+91 added−12 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+91 added−12 removed

  • tests:+89 added−10 removed
  • other:+2 added−2 removed

7 files+91 added−12 removed

modified:jinja2/tests.py+2 added-2 removed
40 unmodified lines
41 {% else %}42 variable is not defined43 {% endif %}4445 See the ``default`` filter for a simple way to set undefined45 See the :func:`default` filter for a simple way to set undefined46 variables.47 """48 return not isinstance(value, Undefined)495051def test_undefined(value):52 """Like `defined` but the other way round."""52 """Like :func:`defined` but the other way round."""53 return isinstance(value, Undefined)545556def test_none(value):
80 unmodified lines
modified:tests/test_ext.py+8 added
68 unmodified lines
697071def test_identifier():72 assert TestExtension.identifier == __name__ + '.TestExtension'737475def test_rebinding():76 original = Environment(extensions=[TestExtension])77 overlay = original.overlay()78 for env in original, overlay:79 for ext in env.extensions.itervalues():80 assert ext.environment is env
modified:tests/test_forloop.py+7 added-1 removed
5 unmodified lines
6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""9from py.test import raises10from jinja2.exceptions import UndefinedError10from jinja2.exceptions import UndefinedError, TemplateSyntaxError111213SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''14ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}'''
24 unmodified lines
39{% for item in range(10) if item is even %}[{{ item }}]{% endfor %}'''40EXTENDEDLOOPFILTER = '''\41{% for item in range(10) if item is even %}[{{ loop.index42}}:{{ item }}]{% endfor %}'''43LOOPUNASSIGNABLE = '''\44{% for loop in seq %}...{% endfor %}'''454647def test_simple(env):48 tmpl = env.from_string(SIMPLE)
78 unmodified lines
127 tmpl = env.from_string(LOOPFILTER)128 assert tmpl.render() == '[0][2][4][6][8]'129 tmpl = env.from_string(EXTENDEDLOOPFILTER)130 assert tmpl.render() == '[1:0][2:2][3:4][4:6][5:8]'131132133def test_loop_unassignable(env):134 raises(TemplateSyntaxError, env.from_string, LOOPUNASSIGNABLE)
modified:tests/test_loaders.py+7 added
9 unmodified lines
10from py.test import raises11import time12import tempfile13from jinja2 import Environment, loaders14from jinja2.loaders import split_template_path15from jinja2.exceptions import TemplateNotFound161718dict_loader = loaders.DictLoader({
82 unmodified lines
101 t3 = env.get_template('three')102 assert 'one' in env.cache103 assert 'two' not in env.cache104 assert 'three' in env.cache105106107def test_split_template_path():108 assert split_template_path('foo/bar') == ['foo', 'bar']109 assert split_template_path('./foo/bar') == ['foo', 'bar']110 raises(TemplateNotFound, split_template_path, '../foo')
modified:tests/test_lrucache.py+14 added
9 unmodified lines
10"""11import thread12import time13import random14import pickle15from jinja2.utils import LRUCache161718def test_simple():
4 unmodified lines
23 d["a"]24 d["d"] = 425 assert len(d) == 326 assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d272829def test_pickleable():30 cache = LRUCache(2)31 cache["foo"] = 4232 cache["bar"] = 2333 cache["foo"]3435 for protocol in range(3):36 copy = pickle.loads(pickle.dumps(cache, protocol))37 assert copy.capacity == cache.capacity38 assert copy._mapping == cache._mapping39 assert copy._queue == cache._queue
modified:tests/test_tests.py+37 added-3 removed
4 unmodified lines
56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""9from jinja2 import Environment, Markup101112DEFINED = '''{{ missing is defined }}|{{ true is defined }}'''13EVEN = '''{{ 1 is even }}|{{ 2 is even }}'''14LOWER = '''{{ "foo" is lower }}|{{ "FOO" is lower }}'''15ODD = '''{{ 1 is odd }}|{{ 2 is odd }}'''
2 unmodified lines
18{{ 42 is sequence }}'''19UPPER = '''{{ "FOO" is upper }}|{{ "foo" is upper }}'''20SAMEAS = '''{{ foo is sameas false }}|{{ 0 is sameas false }}'''21NOPARENFORARG1 = '''{{ foo is sameas none }}'''22TYPECHECKS = '''\23{{ 42 is undefined }}24{{ 42 is defined }}25{{ 42 is none }}26{{ none is none }}27{{ 42 is number }}28{{ 42 is string }}29{{ "foo" is string }}30{{ "foo" is sequence }}31{{ [1] is sequence }}32{{ range is callable }}33{{ 42 is callable }}34{{ range(5) is iterable }}'''353637def test_defined(env):38 tmpl = env.from_string(DEFINED)
4 unmodified lines
43 tmpl = env.from_string(EVEN)44 assert tmpl.render() == 'False|True'454647def test_odd(env):48 tmpl = env.from_string(ODD)49 assert tmpl.render() == 'True|False'505152def test_lower(env):53 tmpl = env.from_string(LOWER)54 assert tmpl.render() == 'True|False'555637def test_odd(env):38 tmpl = env.from_string(ODD)39 assert tmpl.render() == 'True|False'57def test_typechecks(env):58 tmpl = env.from_string(TYPECHECKS)59 assert tmpl.render() == ''606162def test_sequence(env):63 tmpl = env.from_string(SEQUENCE)
9 unmodified lines
73 tmpl = env.from_string(SAMEAS)74 assert tmpl.render(foo=False) == 'True|False'757677def test_typechecks(env):78 tmpl = env.from_string(TYPECHECKS)79 assert tmpl.render() == (80 'False\nTrue\nFalse\nTrue\nTrue\nFalse\n'81 'True\nTrue\nTrue\nTrue\nFalse\nTrue'82 )838485def test_no_paren_for_arg1(env):86 tmpl = env.from_string(NOPARENFORARG1)87 assert tmpl.render(foo=None) == 'True'888990def test_escaped():91 env = Environment(autoescape=True)92 tmpl = env.from_string('{{ x is escaped }}|{{ y is escaped }}')93 assert tmpl.render(x='foo', y=Markup('foo')) == 'False|True'
modified:tests/test_various.py+16 added-6 removed
4 unmodified lines
56 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8"""9import gc10from py.test import raises11from jinja2 import escape12from jinja2.exceptions import TemplateSyntaxError131415UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''
23 unmodified lines
394041def test_const_assign(env):42 for tmpl in CONSTASS1, CONSTASS2:40 try:41 env.from_string(tmpl)42 except TemplateSyntaxError:43 pass44 else:45 raise AssertionError('expected syntax error')43 raises(TemplateSyntaxError, env.from_string, tmpl)444546def test_localset(env):47 tmpl = env.from_string(LOCALSET)48 assert tmpl.render() == '0'495051def test_markup_leaks():52 counts = set()53 for count in xrange(20):54 for item in xrange(1000):55 escape("foo")56 escape("<foo>")57 escape(u"foo")58 escape(u"<foo>")59 counts.add(len(gc.get_objects()))60 assert len(counts) == 1, 'ouch, c extension seems to leak objects'