test_forloop.py
This file
Path
tests/test_forloop.py
186 lines · 5.7 KiB · Python
Last changed by
Fixed a bug that caused internal errors if names where used as iterationArmin Ronacherbefore adoption271a0eb
History of this fileTake it
The walk stopped after 200 commits: lines older than 4dc9578 point at it.
fixed template reloadingArmin Ronacherbefore adoption4dc9578
lines 1–5why, one step back1# -*- coding: utf-8 -*-2"""3 unit test for loop functions4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~5Copyright switch to "The Jinja Team".Armin Ronacherbefore adoption62ccd1b
line 6why, one step back6 :copyright: (c) 2009 by the Jinja Team.fixed template reloadingArmin Ronacherbefore adoption4dc9578
lines 7–9why, one step back7 :license: BSD, see LICENSE for more details.8"""9from py.test import raisesadded more unittests for various tasksArmin Ronacherbefore adoption9f258ff
line 10why, one step back10from jinja2.exceptions import UndefinedError, TemplateSyntaxErrorfixed template reloadingArmin Ronacherbefore adoption4dc9578
lines 11–24why, one step back111213SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}'''14ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}'''15EMPTYBLOCKS = '''<{% for item in seq %}{% else %}{% endfor %}>'''16CONTEXTVARS = '''{% for item in seq %}\17{{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{18 loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{19 loop.length }}###{% endfor %}'''20CYCLING = '''{% for item in seq %}{{ loop.cycle('<1>', '<2>') }}{% endfor %}\21{% for item in seq %}{{ loop.cycle(*through) }}{% endfor %}'''22SCOPE = '''{% for item in seq %}{% endfor %}{{ item }}'''23VARLEN = '''{% for item in iter %}{{ item }}{% endfor %}'''24NONITER = '''{% for item in none %}...{% endfor %}'''added unittest for recursive for loopArmin Ronacherbefore adoption66a9344
line 25why, one step back25RECURSIVE = '''{% for item in seq recursive -%}made the recursive for loop test easier to debugArmin Ronacherbefore adoptionb455c31
line 26why, one step back26 [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}]added unittest for recursive for loopArmin Ronacherbefore adoption66a9344
line 27why, one step back27{%- endfor %}'''added unittest for the latest fixed bugArmin Ronacherbefore adoption24db451
lines 28–33why, one step back28LOOPLOOP = '''{% for row in table %}29 {%- set rowloop = loop -%}30 {% for cell in row -%}31 [{{ rowloop.index }}|{{ loop.index }}]32 {%- endfor %}33{%- endfor %}'''refactored compiler and improved identifier handling for for-loopsArmin Ronacherbefore adoption105f0dc
lines 34–37why, one step back34LOOPERROR1 = '''\35{% for item in [1] if loop.index == 0 %}...{% endfor %}'''36LOOPERROR2 = '''\37{% for item in [] %}...{% else %}{{ loop }}{% endfor %}'''more unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e
lines 38–42why, one step back38LOOPFILTER = '''\39{% for item in range(10) if item is even %}[{{ item }}]{% endfor %}'''40EXTENDEDLOOPFILTER = '''\41{% for item in range(10) if item is even %}[{{ loop.index42}}:{{ item }}]{% endfor %}'''added more unittests for various tasksArmin Ronacherbefore adoption9f258ff
lines 43–44why, one step back43LOOPUNASSIGNABLE = '''\44{% for loop in seq %}...{% endfor %}'''fixed template reloadingArmin Ronacherbefore adoption4dc9578
lines 45–102why, one step back454647def test_simple(env):48 tmpl = env.from_string(SIMPLE)49 assert tmpl.render(seq=range(10)) == '0123456789'505152def test_else(env):53 tmpl = env.from_string(ELSE)54 assert tmpl.render() == '...'555657def test_empty_blocks(env):58 tmpl = env.from_string(EMPTYBLOCKS)59 assert tmpl.render() == '<>'606162def test_context_vars(env):63 tmpl = env.from_string(CONTEXTVARS)64 one, two, _ = tmpl.render(seq=[0, 1]).split('###')65 (one_index, one_index0, one_revindex, one_revindex0, one_first,66 one_last, one_length) = one.split('|')67 (two_index, two_index0, two_revindex, two_revindex0, two_first,68 two_last, two_length) = two.split('|')6970 assert int(one_index) == 1 and int(two_index) == 271 assert int(one_index0) == 0 and int(two_index0) == 172 assert int(one_revindex) == 2 and int(two_revindex) == 173 assert int(one_revindex0) == 1 and int(two_revindex0) == 074 assert one_first == 'True' and two_first == 'False'75 assert one_last == 'False' and two_last == 'True'76 assert one_length == two_length == '2'777879def test_cycling(env):80 tmpl = env.from_string(CYCLING)81 output = tmpl.render(seq=range(4), through=('<1>', '<2>'))82 assert output == '<1><2>' * 4838485def test_scope(env):86 tmpl = env.from_string(SCOPE)87 output = tmpl.render(seq=range(10))88 assert not output899091def test_varlen(env):92 def inner():93 for item in range(5):94 yield item95 tmpl = env.from_string(VARLEN)96 output = tmpl.render(iter=inner())97 assert output == '01234'9899100def test_noniter(env):101 tmpl = env.from_string(NONITER)102 raises(TypeError, tmpl.render)added unittest for recursive for loopArmin Ronacherbefore adoption66a9344
lines 103–110why, one step back103104105def test_recursive(env):106 tmpl = env.from_string(RECURSIVE)107 assert tmpl.render(seq=[108 dict(a=1, b=[dict(a=1), dict(a=2)]),109 dict(a=2, b=[dict(a=1), dict(a=2)]),110 dict(a=3, b=[dict(a='a')])made the recursive for loop test easier to debugArmin Ronacherbefore adoptionb455c31
line 111why, one step back111 ]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]'added unittest for the latest fixed bugArmin Ronacherbefore adoption24db451
lines 112–116why, one step back112113114def test_looploop(env):115 tmpl = env.from_string(LOOPLOOP)116 assert tmpl.render(table=['ab', 'cd']) == '[1|1][1|2][2|1][2|2]'refactored compiler and improved identifier handling for for-loopsArmin Ronacherbefore adoption105f0dc
lines 117–118why, one step back117118Fixed a bug with the loop context of a for loop if the iterator passed has a volatile `__len__` like the listreverseiterator. `else` in inline if-expressions is optional now.Armin Ronacherbefore adoption547d0b6
lines 119–124why, one step back119def test_reversed_bug(env):120 tmpl = env.from_string('{% for i in items %}{{ i }}{% if not loop.last %}'121 ',{% endif %}{% endfor %}')122 assert tmpl.render(items=reversed([3, 2, 1])) == '1,2,3'123124refactored compiler and improved identifier handling for for-loopsArmin Ronacherbefore adoption105f0dc
lines 125–129why, one step back125def test_loop_errors(env):126 tmpl = env.from_string(LOOPERROR1)127 raises(UndefinedError, tmpl.render)128 tmpl = env.from_string(LOOPERROR2)129 assert tmpl.render() == ''more unittests and updated documentation for extensions. Fixed bug in optimizer that caused blocks to be optimized away under some circumstances.Armin Ronacherbefore adoption6df604e
lines 130–136why, one step back130131132def test_loop_filter(env):133 tmpl = env.from_string(LOOPFILTER)134 assert tmpl.render() == '[0][2][4][6][8]'135 tmpl = env.from_string(EXTENDEDLOOPFILTER)136 assert tmpl.render() == '[1:0][2:2][3:4][4:6][5:8]'added more unittests for various tasksArmin Ronacherbefore adoption9f258ff
lines 137–140why, one step back137138139def test_loop_unassignable(env):140 raises(TemplateSyntaxError, env.from_string, LOOPUNASSIGNABLE)fixed a bug with nested loops and the special loop variable.Armin Ronacherbefore adoptionff53c78
lines 141–146why, one step back141142143def test_scoped_special_var(env):144 t = env.from_string('{% for s in seq %}[{{ loop.first }}{% for c in s %}'145 '|{{ loop.first }}{% endfor %}]{% endfor %}')146 assert t.render(seq=('ab', 'cd')) == '[True|True|False][False|True|False]'Fixed a bug in the compiler that caused problems with loop not being referenced in an outer scoped. (Introduced in the last checkin)Armin Ronacherbefore adoption833a3b5
lines 147–155why, one step back147148149def test_scoped_loop_var(env):150 t = env.from_string('{% for x in seq %}{{ loop.first }}'151 '{% for y in seq %}{% endfor %}{% endfor %}')152 assert t.render(seq='ab') == 'TrueFalse'153 t = env.from_string('{% for x in seq %}{% for y in seq %}'154 '{{ loop.first }}{% endfor %}{% endfor %}')155 assert t.render(seq='ab') == 'TrueFalseTrueFalse'Fixed a translation error caused by looping over empty recursive loops.Armin Ronacherbefore adoptioncebd838
lines 156–162why, one step back156157158def test_recursive_empty_loop_iter(env):159 t = env.from_string('''160 {%- for item in foo recursive -%}{%- endfor -%}161 ''')162 assert t.render(dict(foo=[])) == ''Fixed a bug that caused syntax errors when defining macros or using theArmin Ronacherbefore adoption330fbc0
lines 163–177why, one step back163164165def test_call_in_loop(env):166 t = env.from_string('''167 {%- macro do_something() -%}168 [{{ caller() }}]169 {%- endmacro %}170171 {%- for i in [1, 2, 3] %}172 {%- call do_something() -%}173 {{ i }}174 {%- endcall %}175 {%- endfor -%}176 ''')177 assert t.render() == '[1][2][3]'Fixed a bug that caused internal errors if names where used as iterationArmin Ronacherbefore adoption271a0eb
lines 178–186why, one step back178179180def test_scoping_bug(env):181 t = env.from_string('''182 {%- for item in foo %}...{{ item }}...{% endfor %}183 {%- macro item(a) %}...{{ a }}...{% endmacro %}184 {{- item(2) -}}185 ''')186 assert t.render(foo=(1,)) == '...1......2...'