test_various.py
This file
Path
tests/test_various.py
110 lines · 3.1 KiB · Python
Last changed by
Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b
History of this fileTake it
The walk stopped after 200 commits: lines older than b217886 point at it.
added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 1–5why, one step back1# -*- coding: utf-8 -*-2"""3 unit test for various things4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~5Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b
line 6why, one step back6 :copyright: (c) 2009 by the Jinja Team.added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 7–8why, one step back7 :license: BSD, see LICENSE for more details.8"""added more unittests for various tasksArmin Ronacherbefore adoption9f258ff
lines 9–10why, one step back9import gc10from py.test import raisesAdded support for `Environment.compile_expression`.Armin Ronacherbefore adoptionba6e25a
line 11why, one step back11from jinja2 import escape, is_undefinedVarious cleanups and added custom cycler.Armin Ronacherbefore adoptionccae055
line 12why, one step back12from jinja2.utils import Cycleradded author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 13–19why, one step back13from jinja2.exceptions import TemplateSyntaxError141516UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}'''17RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}'''18CONST = '''{{ true }}|{{ false }}|{{ none }}|\19{{ none is defined }}|{{ missing is defined }}'''switched back to explicit set for assignments. {% foo = 42 %} becomes {% set foo = 42 %} and {% foo.something() }} becomes {{ void(foo.something() }} with this commit.Armin Ronacherbefore adoption0a2ac69
lines 20–21why, one step back20LOCALSET = '''{% set foo = 0 %}\21{% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}\added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
line 22why, one step back22{{ foo }}'''switched back to explicit set for assignments. {% foo = 42 %} becomes {% set foo = 42 %} and {% foo.something() }} becomes {{ void(foo.something() }} with this commit.Armin Ronacherbefore adoption0a2ac69
line 23why, one step back23CONSTASS1 = '''{% set true = 42 %}'''added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 24–43why, one step back24CONSTASS2 = '''{% for none in seq %}{% endfor %}'''252627def test_unpacking(env):28 tmpl = env.from_string(UNPACKING)29 assert tmpl.render() == '1|2|3'303132def test_raw(env):33 tmpl = env.from_string(RAW)34 assert tmpl.render() == '{{ FOO }} and {% BAR %}'353637def test_const(env):38 tmpl = env.from_string(CONST)39 assert tmpl.render() == 'True|False|None|True|False'404142def test_const_assign(env):43 for tmpl in CONSTASS1, CONSTASS2:added more unittests for various tasksArmin Ronacherbefore adoption9f258ff
line 44why, one step back44 raises(TemplateSyntaxError, env.from_string, tmpl)added author information and fixed small off-by-one bugArmin Ronacherbefore adoptionb217886
lines 45–49why, one step back454647def test_localset(env):48 tmpl = env.from_string(LOCALSET)49 assert tmpl.render() == '0'added more unittests for various tasksArmin Ronacherbefore adoption9f258ff
lines 50–61why, one step back505152def test_markup_leaks():53 counts = set()54 for count in xrange(20):55 for item in xrange(1000):56 escape("foo")57 escape("<foo>")58 escape(u"foo")59 escape(u"<foo>")60 counts.add(len(gc.get_objects()))61 assert len(counts) == 1, 'ouch, c extension seems to leak objects'items have a higher priority than attributes now. That's compatible with Jinja1 and DjangoArmin Ronacherbefore adoptionf15f5f7
lines 62–63why, one step back6263Improved attribute and item lookup by allowing template designers to express the priority. foo.bar checks foo.bar first and then foo['bar'] and the other way round.Armin Ronacherbefore adoption6dc6f29
line 64why, one step back64def test_item_and_attribute():items have a higher priority than attributes now. That's compatible with Jinja1 and DjangoArmin Ronacherbefore adoptionf15f5f7
lines 65–69why, one step back65 from jinja2 import Environment66 from jinja2.sandbox import SandboxedEnvironment6768 for env in Environment(), SandboxedEnvironment():69 tmpl = env.from_string('{{ foo.items() }}')Improved attribute and item lookup by allowing template designers to express the priority. foo.bar checks foo.bar first and then foo['bar'] and the other way round.Armin Ronacherbefore adoption6dc6f29
line 70why, one step back70 assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"Fixed a broken unittest and fixed a bug that required multiple tests to be put into parentheses for chaning.Armin Ronacherbefore adoptione3290ea
line 71why, one step back71 tmpl = env.from_string('{{ foo|attr("items")() }}')Improved attribute and item lookup by allowing template designers to express the priority. foo.bar checks foo.bar first and then foo['bar'] and the other way round.Armin Ronacherbefore adoption6dc6f29
lines 72–74why, one step back72 assert tmpl.render(foo={'items': 42}) == "[('items', 42)]"73 tmpl = env.from_string('{{ foo["items"] }}')74 assert tmpl.render(foo={'items': 42}) == '42'Fixed bug in finalizerArmin Ronacherbefore adoption665bfb8
lines 75–87why, one step back757677def test_finalizer():78 from jinja2 import Environment79 def finalize_none_empty(value):80 if value is None:81 value = u''82 return value83 env = Environment(finalize=finalize_none_empty)84 tmpl = env.from_string('{% for item in seq %}|{{ item }}{% endfor %}')85 assert tmpl.render(seq=(None, 1, "foo")) == '||1|foo'86 tmpl = env.from_string('<{{ none }}>')87 assert tmpl.render() == '<>'Various cleanups and added custom cycler.Armin Ronacherbefore adoptionccae055
lines 88–99why, one step back888990def test_cycler():91 items = 1, 2, 392 c = Cycler(*items)93 for item in items + items:94 assert c.current == item95 assert c.next() == item96 c.next()97 assert c.current == 298 c.reset()99 assert c.current == 1Added support for `Environment.compile_expression`.Armin Ronacherbefore adoptionba6e25a
lines 100–110why, one step back100101102def test_expressions(env):103 expr = env.compile_expression("foo")104 assert expr() is None105 assert expr(foo=42) == 42106 expr2 = env.compile_expression("foo", undefined_to_none=False)107 assert is_undefined(expr2())108109 expr = env.compile_expression("42 + foo")110 assert expr(foo=42) == 84