From 9f258fffc4d2ee729669a5208aaf97021185e055 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 24 May 2008 22:28:52 +0200 Subject: [PATCH] added more unittests for various tasks --HG-- branch : trunk --- jinja2/tests.py | 4 ++-- tests/test_ext.py | 8 ++++++++ tests/test_forloop.py | 8 +++++++- tests/test_loaders.py | 7 +++++++ tests/test_lrucache.py | 14 ++++++++++++++ tests/test_tests.py | 40 +++++++++++++++++++++++++++++++++++++--- tests/test_various.py | 22 ++++++++++++++++------ 7 files changed, 91 insertions(+), 12 deletions(-) diff --git a/jinja2/tests.py b/jinja2/tests.py index 37e6f31..6873b5a 100644 --- a/jinja2/tests.py +++ b/jinja2/tests.py @@ -42,14 +42,14 @@ def test_defined(value): variable is not defined {% endif %} - See the ``default`` filter for a simple way to set undefined + See the :func:`default` filter for a simple way to set undefined variables. """ return not isinstance(value, Undefined) def test_undefined(value): - """Like `defined` but the other way round.""" + """Like :func:`defined` but the other way round.""" return isinstance(value, Undefined) diff --git a/tests/test_ext.py b/tests/test_ext.py index 7da2515..d3364bb 100644 --- a/tests/test_ext.py +++ b/tests/test_ext.py @@ -70,3 +70,11 @@ def test_extension_nodes(): def test_identifier(): assert TestExtension.identifier == __name__ + '.TestExtension' + + +def test_rebinding(): + original = Environment(extensions=[TestExtension]) + overlay = original.overlay() + for env in original, overlay: + for ext in env.extensions.itervalues(): + assert ext.environment is env diff --git a/tests/test_forloop.py b/tests/test_forloop.py index 67fb39e..b8d3ae6 100644 --- a/tests/test_forloop.py +++ b/tests/test_forloop.py @@ -7,7 +7,7 @@ :license: BSD, see LICENSE for more details. """ from py.test import raises -from jinja2.exceptions import UndefinedError +from jinja2.exceptions import UndefinedError, TemplateSyntaxError SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}''' @@ -40,6 +40,8 @@ LOOPFILTER = '''\ EXTENDEDLOOPFILTER = '''\ {% for item in range(10) if item is even %}[{{ loop.index }}:{{ item }}]{% endfor %}''' +LOOPUNASSIGNABLE = '''\ +{% for loop in seq %}...{% endfor %}''' def test_simple(env): @@ -126,3 +128,7 @@ def test_loop_filter(env): assert tmpl.render() == '[0][2][4][6][8]' tmpl = env.from_string(EXTENDEDLOOPFILTER) assert tmpl.render() == '[1:0][2:2][3:4][4:6][5:8]' + + +def test_loop_unassignable(env): + raises(TemplateSyntaxError, env.from_string, LOOPUNASSIGNABLE) diff --git a/tests/test_loaders.py b/tests/test_loaders.py index 9cc1dcc..8faf987 100644 --- a/tests/test_loaders.py +++ b/tests/test_loaders.py @@ -11,6 +11,7 @@ from py.test import raises import time import tempfile from jinja2 import Environment, loaders +from jinja2.loaders import split_template_path from jinja2.exceptions import TemplateNotFound @@ -101,3 +102,9 @@ def test_caching(): assert 'one' in env.cache assert 'two' not in env.cache assert 'three' in env.cache + + +def test_split_template_path(): + assert split_template_path('foo/bar') == ['foo', 'bar'] + assert split_template_path('./foo/bar') == ['foo', 'bar'] + raises(TemplateNotFound, split_template_path, '../foo') diff --git a/tests/test_lrucache.py b/tests/test_lrucache.py index 6617e5c..0f93f7b 100644 --- a/tests/test_lrucache.py +++ b/tests/test_lrucache.py @@ -11,6 +11,7 @@ import thread import time import random +import pickle from jinja2.utils import LRUCache @@ -23,3 +24,16 @@ def test_simple(): d["d"] = 4 assert len(d) == 3 assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d + + +def test_pickleable(): + cache = LRUCache(2) + cache["foo"] = 42 + cache["bar"] = 23 + cache["foo"] + + for protocol in range(3): + copy = pickle.loads(pickle.dumps(cache, protocol)) + assert copy.capacity == cache.capacity + assert copy._mapping == cache._mapping + assert copy._queue == cache._queue diff --git a/tests/test_tests.py b/tests/test_tests.py index 3943ab4..c02d0b3 100644 --- a/tests/test_tests.py +++ b/tests/test_tests.py @@ -6,6 +6,8 @@ :copyright: 2007 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ +from jinja2 import Environment, Markup + DEFINED = '''{{ missing is defined }}|{{ true is defined }}''' EVEN = '''{{ 1 is even }}|{{ 2 is even }}''' @@ -17,6 +19,19 @@ SEQUENCE = '''{{ [1, 2, 3] is sequence }}|\ UPPER = '''{{ "FOO" is upper }}|{{ "foo" is upper }}''' SAMEAS = '''{{ foo is sameas false }}|{{ 0 is sameas false }}''' NOPARENFORARG1 = '''{{ foo is sameas none }}''' +TYPECHECKS = '''\ +{{ 42 is undefined }} +{{ 42 is defined }} +{{ 42 is none }} +{{ none is none }} +{{ 42 is number }} +{{ 42 is string }} +{{ "foo" is string }} +{{ "foo" is sequence }} +{{ [1] is sequence }} +{{ range is callable }} +{{ 42 is callable }} +{{ range(5) is iterable }}''' def test_defined(env): @@ -29,14 +44,19 @@ def test_even(env): assert tmpl.render() == 'False|True' +def test_odd(env): + tmpl = env.from_string(ODD) + assert tmpl.render() == 'True|False' + + def test_lower(env): tmpl = env.from_string(LOWER) assert tmpl.render() == 'True|False' -def test_odd(env): - tmpl = env.from_string(ODD) - assert tmpl.render() == 'True|False' +def test_typechecks(env): + tmpl = env.from_string(TYPECHECKS) + assert tmpl.render() == '' def test_sequence(env): @@ -54,6 +74,20 @@ def test_sameas(env): assert tmpl.render(foo=False) == 'True|False' +def test_typechecks(env): + tmpl = env.from_string(TYPECHECKS) + assert tmpl.render() == ( + 'False\nTrue\nFalse\nTrue\nTrue\nFalse\n' + 'True\nTrue\nTrue\nTrue\nFalse\nTrue' + ) + + def test_no_paren_for_arg1(env): tmpl = env.from_string(NOPARENFORARG1) assert tmpl.render(foo=None) == 'True' + + +def test_escaped(): + env = Environment(autoescape=True) + tmpl = env.from_string('{{ x is escaped }}|{{ y is escaped }}') + assert tmpl.render(x='foo', y=Markup('foo')) == 'False|True' diff --git a/tests/test_various.py b/tests/test_various.py index e81dfc0..147f459 100644 --- a/tests/test_various.py +++ b/tests/test_various.py @@ -6,6 +6,9 @@ :copyright: 2007 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ +import gc +from py.test import raises +from jinja2 import escape from jinja2.exceptions import TemplateSyntaxError @@ -37,14 +40,21 @@ def test_const(env): def test_const_assign(env): for tmpl in CONSTASS1, CONSTASS2: - try: - env.from_string(tmpl) - except TemplateSyntaxError: - pass - else: - raise AssertionError('expected syntax error') + raises(TemplateSyntaxError, env.from_string, tmpl) def test_localset(env): tmpl = env.from_string(LOCALSET) assert tmpl.render() == '0' + + +def test_markup_leaks(): + counts = set() + for count in xrange(20): + for item in xrange(1000): + escape("foo") + escape("") + escape(u"foo") + escape(u"") + counts.add(len(gc.get_objects())) + assert len(counts) == 1, 'ouch, c extension seems to leak objects' -- 2.26.2