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
Provenance
- Commit
9f258ff- Tree
466053f- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
e62b7ef
7 files changed+91 added−12 removed
modified:jinja2/tests.py+2 added-2 removed
40 unmodified lines41 {% else %}42 variable is not defined43 {% endif %}44 45- 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)49 50 51 def 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)54 55 56 def test_none(value):80 unmodified linesmodified:tests/test_ext.py+8 added
68 unmodified lines69 70 71 def test_identifier():72 assert TestExtension.identifier == __name__ + '.TestExtension'73+74+75+def 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 envmodified:tests/test_forloop.py+7 added-1 removed
5 unmodified lines6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9 from py.test import raises10-from jinja2.exceptions import UndefinedError10+from jinja2.exceptions import UndefinedError, TemplateSyntaxError11 12 13 SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''14 ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}'''24 unmodified lines39 {% for item in range(10) if item is even %}[{{ item }}]{% endfor %}'''40 EXTENDEDLOOPFILTER = '''\41 {% for item in range(10) if item is even %}[{{ loop.index42 }}:{{ item }}]{% endfor %}'''43+LOOPUNASSIGNABLE = '''\44+{% for loop in seq %}...{% endfor %}'''45 46 47 def test_simple(env):48 tmpl = env.from_string(SIMPLE)78 unmodified lines127 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]'131+132+133+def test_loop_unassignable(env):134+ raises(TemplateSyntaxError, env.from_string, LOOPUNASSIGNABLE)modified:tests/test_loaders.py+7 added
9 unmodified lines10 from py.test import raises11 import time12 import tempfile13 from jinja2 import Environment, loaders14+from jinja2.loaders import split_template_path15 from jinja2.exceptions import TemplateNotFound16 17 18 dict_loader = loaders.DictLoader({82 unmodified lines101 t3 = env.get_template('three')102 assert 'one' in env.cache103 assert 'two' not in env.cache104 assert 'three' in env.cache105+106+107+def 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 lines10 """11 import thread12 import time13 import random14+import pickle15 from jinja2.utils import LRUCache16 17 18 def test_simple():4 unmodified lines23 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 d27+28+29+def test_pickleable():30+ cache = LRUCache(2)31+ cache["foo"] = 4232+ cache["bar"] = 2333+ cache["foo"]34+35+ 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._queuemodified:tests/test_tests.py+37 added-3 removed
4 unmodified lines5 6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9+from jinja2 import Environment, Markup10 11+12 DEFINED = '''{{ missing is defined }}|{{ true is defined }}'''13 EVEN = '''{{ 1 is even }}|{{ 2 is even }}'''14 LOWER = '''{{ "foo" is lower }}|{{ "FOO" is lower }}'''15 ODD = '''{{ 1 is odd }}|{{ 2 is odd }}'''2 unmodified lines18 {{ 42 is sequence }}'''19 UPPER = '''{{ "FOO" is upper }}|{{ "foo" is upper }}'''20 SAMEAS = '''{{ foo is sameas false }}|{{ 0 is sameas false }}'''21 NOPARENFORARG1 = '''{{ foo is sameas none }}'''22+TYPECHECKS = '''\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 }}'''35 36 37 def test_defined(env):38 tmpl = env.from_string(DEFINED)4 unmodified lines43 tmpl = env.from_string(EVEN)44 assert tmpl.render() == 'False|True'45 46 47+def test_odd(env):48+ tmpl = env.from_string(ODD)49+ assert tmpl.render() == 'True|False'50+51+52 def test_lower(env):53 tmpl = env.from_string(LOWER)54 assert tmpl.render() == 'True|False'55 56 37-def test_odd(env):38- tmpl = env.from_string(ODD)39- assert tmpl.render() == 'True|False'57+def test_typechecks(env):58+ tmpl = env.from_string(TYPECHECKS)59+ assert tmpl.render() == ''60 61 62 def test_sequence(env):63 tmpl = env.from_string(SEQUENCE)9 unmodified lines73 tmpl = env.from_string(SAMEAS)74 assert tmpl.render(foo=False) == 'True|False'75 76 77+def 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+ )83+84+85 def test_no_paren_for_arg1(env):86 tmpl = env.from_string(NOPARENFORARG1)87 assert tmpl.render(foo=None) == 'True'88+89+90+def 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 lines5 6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9+import gc10+from py.test import raises11+from jinja2 import escape12 from jinja2.exceptions import TemplateSyntaxError13 14 15 UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''23 unmodified lines39 40 41 def 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)44 45 46 def test_localset(env):47 tmpl = env.from_string(LOCALSET)48 assert tmpl.render() == '0'49+50+51+def 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'