If the expression-statement extension is loaded a tag called `do` is available
that works exactly like the regular variable expression (``{{ ... }}``) just
-that it doesn't print anything. This can be used to modify lists:
+that it doesn't print anything. This can be used to modify lists::
{% do navigation.append('a string') %}
finally:
self._wlock.release()
- def __iter__(self):
- """Iterate over all values in the cache dict, ordered by
+ def items(self):
+ """Return a list of items."""
+ result = [(key, self._mapping[key]) for key in list(self._queue)]
+ result.reverse()
+ return result
+
+ def iteritems(self):
+ """Iterate over all items."""
+ return iter(self.items())
+
+ def values(self):
+ """Return a list of all values."""
+ return [x[1] for x in self.items()]
+
+ def itervalue(self):
+ """Iterate over all values."""
+ return iter(self.values())
+
+ def keys(self):
+ """Return a list of all keys ordered by most recent usage."""
+ return list(self)
+
+ def iterkeys(self):
+ """Iterate over all keys in the cache dict, ordered by
the most recent usage.
"""
return reversed(tuple(self._queue))
+ __iter__ = iterkeys
+
def __reversed__(self):
"""Iterate over the values in the cache dict, oldest items
coming first.
:copyright: 2007 by Armin Ronacher.
:license: BSD, see LICENSE for more details.
"""
+from jinja2 import Markup
CAPITALIZE = '''{{ "foo bar"|capitalize }}'''
CENTER = '''{{ "foo"|center(9) }}'''
FILTERTAG = '''{% filter upper|replace('FOO', 'foo') %}foobar{% endfilter %}'''
-
def test_capitalize(env):
tmpl = env.from_string(CAPITALIZE)
assert tmpl.render() == 'Foo bar'
def test_filtertag(env):
tmpl = env.from_string(FILTERTAG)
assert tmpl.render() == 'fooBAR'
+
+
+def test_replace(env):
+ tmpl = env.from_string('{{ "foo"|replace("o", 42)}}')
+ assert tmpl.render() == 'f4242'
+
+
+def test_forceescape(env):
+ tmpl = env.from_string('{{ x|forceescape }}')
+ assert tmpl.render(x=Markup('<div />')) == u'<div />'
test_env.from_string('{% from "foo" import bar, with context %}')
test_env.from_string('{% from "foo" import bar, with, context %}')
test_env.from_string('{% from "foo" import bar, with with context %}')
+
+
+def test_exports():
+ m = test_env.from_string('''
+ {% macro toplevel() %}...{% endmacro %}
+ {% macro __private() %}...{% endmacro %}
+ {% set variable = 42 %}
+ {% for item in [1] %}
+ {% macro notthere() %}{% endmacro %}
+ {% endfor %}
+ ''').module
+ assert m.toplevel() == '...'
+ assert not hasattr(m, '__missing')
+ assert m.variable == 42
+ assert not hasattr(m, 'notthere')
def test_working(env):
tmpl = env.get_template('working')
+
+
+def test_reuse_blocks(env):
+ tmpl = env.from_string('{{ self.foo() }}|{% block foo %}42{% endblock %}|{{ self.foo() }}')
+ assert tmpl.render() == '42|42|42'
--- /dev/null
+# -*- coding: utf-8 -*-
+"""
+ Tests the LRUCache
+ ~~~~~~~~~~~~~~~~~~
+
+ This module tests the LRU Cache
+
+ :copyright: Copyright 2008 by Armin Ronacher.
+ :license: GNU GPL.
+"""
+import thread
+import time
+import random
+from jinja2.utils import LRUCache
+
+
+def test_simple():
+ d = LRUCache(3)
+ d["a"] = 1
+ d["b"] = 2
+ d["c"] = 3
+ d["a"]
+ d["d"] = 4
+ assert len(d) == 3
+ assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d
assert tmpl.render() == '{{ FOO }} and {% BAR %}'
-def test_lru_cache():
- from jinja2.utils import LRUCache
- d = LRUCache(3)
- d["a"] = 1
- d["b"] = 2
- d["c"] = 3
- d["a"]
- 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_const(env):
tmpl = env.from_string(CONST)
assert tmpl.render() == 'True|False|None|True|False'