Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | unit test for loop functions |
| 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 | |
| 6 | :copyright: 2007 by Armin Ronacher. |
| 7 | :license: BSD, see LICENSE for more details. |
| 8 | """ |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 9 | from py.test import raises |
| 10 | |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 11 | |
| 12 | SIMPLE = '''{% for item in seq %}{{ item }}{% endfor %}''' |
| 13 | ELSE = '''{% for item in seq %}XXX{% else %}...{% endfor %}''' |
| 14 | EMPTYBLOCKS = '''<{% for item in seq %}{% else %}{% endfor %}>''' |
| 15 | CONTEXTVARS = '''{% for item in seq %}\ |
| 16 | {{ loop.index }}|{{ loop.index0 }}|{{ loop.revindex }}|{{ |
| 17 | loop.revindex0 }}|{{ loop.first }}|{{ loop.last }}|{{ |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 18 | loop.length }}###{% endfor %}''' |
| 19 | CYCLING = '''{% for item in seq %}{{ loop.cycle('<1>', '<2>') }}{% endfor %}\ |
| 20 | {% for item in seq %}{{ loop.cycle(*through) }}{% endfor %}''' |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 21 | SCOPE = '''{% for item in seq %}{% endfor %}{{ item }}''' |
Armin Ronacher | 6dba4d6 | 2007-05-21 23:41:36 +0200 | [diff] [blame] | 22 | VARLEN = '''{% for item in iter %}{{ item }}{% endfor %}''' |
Armin Ronacher | 9bcd411 | 2007-05-29 14:17:24 +0200 | [diff] [blame] | 23 | NONITER = '''{% for item in none %}...{% endfor %}''' |
Armin Ronacher | 66a9344 | 2008-05-11 23:42:19 +0200 | [diff] [blame] | 24 | RECURSIVE = '''{% for item in seq recursive -%} |
Armin Ronacher | b455c31 | 2008-05-11 23:43:07 +0200 | [diff] [blame] | 25 | [{{ item.a }}{% if item.b %}<{{ loop(item.b) }}>{% endif %}] |
Armin Ronacher | 66a9344 | 2008-05-11 23:42:19 +0200 | [diff] [blame] | 26 | {%- endfor %}''' |
Armin Ronacher | 24db451 | 2008-05-19 09:28:17 +0200 | [diff] [blame^] | 27 | LOOPLOOP = '''{% for row in table %} |
| 28 | {%- set rowloop = loop -%} |
| 29 | {% for cell in row -%} |
| 30 | [{{ rowloop.index }}|{{ loop.index }}] |
| 31 | {%- endfor %} |
| 32 | {%- endfor %}''' |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 33 | |
| 34 | |
| 35 | def test_simple(env): |
| 36 | tmpl = env.from_string(SIMPLE) |
| 37 | assert tmpl.render(seq=range(10)) == '0123456789' |
| 38 | |
| 39 | |
| 40 | def test_else(env): |
| 41 | tmpl = env.from_string(ELSE) |
| 42 | assert tmpl.render() == '...' |
| 43 | |
| 44 | |
| 45 | def test_empty_blocks(env): |
| 46 | tmpl = env.from_string(EMPTYBLOCKS) |
| 47 | assert tmpl.render() == '<>' |
| 48 | |
| 49 | |
| 50 | def test_context_vars(env): |
| 51 | tmpl = env.from_string(CONTEXTVARS) |
| 52 | one, two, _ = tmpl.render(seq=[0, 1]).split('###') |
| 53 | (one_index, one_index0, one_revindex, one_revindex0, one_first, |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 54 | one_last, one_length) = one.split('|') |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 55 | (two_index, two_index0, two_revindex, two_revindex0, two_first, |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 56 | two_last, two_length) = two.split('|') |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 57 | |
| 58 | assert int(one_index) == 1 and int(two_index) == 2 |
| 59 | assert int(one_index0) == 0 and int(two_index0) == 1 |
| 60 | assert int(one_revindex) == 2 and int(two_revindex) == 1 |
| 61 | assert int(one_revindex0) == 1 and int(two_revindex0) == 0 |
| 62 | assert one_first == 'True' and two_first == 'False' |
| 63 | assert one_last == 'False' and two_last == 'True' |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 64 | assert one_length == two_length == '2' |
| 65 | |
| 66 | |
| 67 | def test_cycling(env): |
| 68 | tmpl = env.from_string(CYCLING) |
| 69 | output = tmpl.render(seq=range(4), through=('<1>', '<2>')) |
| 70 | assert output == '<1><2>' * 4 |
| 71 | |
| 72 | |
| 73 | def test_scope(env): |
| 74 | tmpl = env.from_string(SCOPE) |
| 75 | output = tmpl.render(seq=range(10)) |
| 76 | assert not output |
Armin Ronacher | 6dba4d6 | 2007-05-21 23:41:36 +0200 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def test_varlen(env): |
| 80 | def inner(): |
| 81 | for item in range(5): |
| 82 | yield item |
| 83 | tmpl = env.from_string(VARLEN) |
| 84 | output = tmpl.render(iter=inner()) |
| 85 | assert output == '01234' |
Armin Ronacher | 9bcd411 | 2007-05-29 14:17:24 +0200 | [diff] [blame] | 86 | |
| 87 | |
| 88 | def test_noniter(env): |
| 89 | tmpl = env.from_string(NONITER) |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 90 | raises(TypeError, tmpl.render) |
Armin Ronacher | 66a9344 | 2008-05-11 23:42:19 +0200 | [diff] [blame] | 91 | |
| 92 | |
| 93 | def test_recursive(env): |
| 94 | tmpl = env.from_string(RECURSIVE) |
| 95 | assert tmpl.render(seq=[ |
| 96 | dict(a=1, b=[dict(a=1), dict(a=2)]), |
| 97 | dict(a=2, b=[dict(a=1), dict(a=2)]), |
| 98 | dict(a=3, b=[dict(a='a')]) |
Armin Ronacher | b455c31 | 2008-05-11 23:43:07 +0200 | [diff] [blame] | 99 | ]) == '[1<[1][2]>][2<[1][2]>][3<[a]>]' |
Armin Ronacher | 24db451 | 2008-05-19 09:28:17 +0200 | [diff] [blame^] | 100 | |
| 101 | |
| 102 | def test_looploop(env): |
| 103 | tmpl = env.from_string(LOOPLOOP) |
| 104 | assert tmpl.render(table=['ab', 'cd']) == '[1|1][1|2][2|1][2|2]' |