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)
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
: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 %}'''
EXTENDEDLOOPFILTER = '''\
{% for item in range(10) if item is even %}[{{ loop.index
}}:{{ item }}]{% endfor %}'''
+LOOPUNASSIGNABLE = '''\
+{% for loop in seq %}...{% endfor %}'''
def test_simple(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)
import time
import tempfile
from jinja2 import Environment, loaders
+from jinja2.loaders import split_template_path
from jinja2.exceptions import TemplateNotFound
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')
import thread
import time
import random
+import pickle
from jinja2.utils import LRUCache
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
: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 }}'''
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):
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):
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'
: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
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("<foo>")
+ escape(u"foo")
+ escape(u"<foo>")
+ counts.add(len(gc.get_objects()))
+ assert len(counts) == 1, 'ouch, c extension seems to leak objects'