From: Armin Ronacher Date: Sun, 18 Mar 2007 22:15:23 +0000 (+0100) Subject: [svn] added tests for "capture" and "format" filters X-Git-Tag: 2.0rc1~423 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0b666579b6d927bdfaa4ff9aec28f73d831ba1c2;p=jinja2.git [svn] added tests for "capture" and "format" filters --HG-- branch : trunk --- diff --git a/jinja/filters.py b/jinja/filters.py index 01cab4f..7de2176 100644 --- a/jinja/filters.py +++ b/jinja/filters.py @@ -577,6 +577,10 @@ def do_capture(name='captured'): {% endfilter %} {{ foo }} """ + def wrapped(env, context, value): + context[name] = value + return value + return wrapped FILTERS = { diff --git a/tests/test_filters.py b/tests/test_filters.py index eab73d5..53b142a 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -15,6 +15,7 @@ """ CAPITALIZE = '''{{ "foo bar"|capitalize }}''' +CAPTURE = '''{{ "foo"|capture('bar') }}|{{ bar }}''' CENTER = '''{{ "foo"|center(9) }}''' DEFAULT = '''{{ missing|default("no") }}|{{ false|default('no') }}|\ {{ false|default('no', true) }}|{{ given|default("no") }}''' @@ -29,6 +30,7 @@ FILESIZEFORMAT = '{{ 100|filesizeformat }}|\ {{ 1000000000000|filesizeformat }}' FIRST = '''{{ foo|first }}''' FLOAT = '''{{ "42"|float }}|{{ "ajsghasjgd"|float }}|{{ "32.32"|float }}''' +FORMAT = '''{{ "%s|%s"|format("a", "b") }}''' INDENT = '''{{ foo|indent(2) }}|{{ foo|indent(2, true) }}''' INT = '''{{ "42"|int }}|{{ "ajsghasjgd"|int }}|{{ "32.32"|int }}''' JOIN = '''{{ [1, 2, 3]|join("|") }}''' @@ -56,6 +58,11 @@ def test_capitalize(env): assert tmpl.render() == 'Foo bar' +def test_capture(env): + tmpl = env.from_string(CAPTURE) + assert tmpl.render() == 'foo|foo' + + def test_center(env): tmpl = env.from_string(CENTER) assert tmpl.render() == ' foo ' @@ -98,6 +105,12 @@ def test_float(env): assert out == '42.0|0.0|32.32' +def test_format(env): + tmpl = env.from_string(FORMAT) + out = tmpl.render() + assert out == 'a|b' + + def test_indent(env): tmpl = env.from_string(INDENT) text = '\n'.join([' '.join(['foo', 'bar'] * 2)] * 2)