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 | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 24 | |
| 25 | |
| 26 | def test_simple(env): |
| 27 | tmpl = env.from_string(SIMPLE) |
| 28 | assert tmpl.render(seq=range(10)) == '0123456789' |
| 29 | |
| 30 | |
| 31 | def test_else(env): |
| 32 | tmpl = env.from_string(ELSE) |
| 33 | assert tmpl.render() == '...' |
| 34 | |
| 35 | |
| 36 | def test_empty_blocks(env): |
| 37 | tmpl = env.from_string(EMPTYBLOCKS) |
| 38 | assert tmpl.render() == '<>' |
| 39 | |
| 40 | |
| 41 | def test_context_vars(env): |
| 42 | tmpl = env.from_string(CONTEXTVARS) |
| 43 | one, two, _ = tmpl.render(seq=[0, 1]).split('###') |
| 44 | (one_index, one_index0, one_revindex, one_revindex0, one_first, |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 45 | one_last, one_length) = one.split('|') |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 46 | (two_index, two_index0, two_revindex, two_revindex0, two_first, |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 47 | two_last, two_length) = two.split('|') |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 48 | |
| 49 | assert int(one_index) == 1 and int(two_index) == 2 |
| 50 | assert int(one_index0) == 0 and int(two_index0) == 1 |
| 51 | assert int(one_revindex) == 2 and int(two_revindex) == 1 |
| 52 | assert int(one_revindex0) == 1 and int(two_revindex0) == 0 |
| 53 | assert one_first == 'True' and two_first == 'False' |
| 54 | assert one_last == 'False' and two_last == 'True' |
Armin Ronacher | ab45b84 | 2007-03-18 20:47:50 +0100 | [diff] [blame] | 55 | assert one_length == two_length == '2' |
| 56 | |
| 57 | |
| 58 | def test_cycling(env): |
| 59 | tmpl = env.from_string(CYCLING) |
| 60 | output = tmpl.render(seq=range(4), through=('<1>', '<2>')) |
| 61 | assert output == '<1><2>' * 4 |
| 62 | |
| 63 | |
| 64 | def test_scope(env): |
| 65 | tmpl = env.from_string(SCOPE) |
| 66 | output = tmpl.render(seq=range(10)) |
| 67 | assert not output |
Armin Ronacher | 6dba4d6 | 2007-05-21 23:41:36 +0200 | [diff] [blame] | 68 | |
| 69 | |
| 70 | def test_varlen(env): |
| 71 | def inner(): |
| 72 | for item in range(5): |
| 73 | yield item |
| 74 | tmpl = env.from_string(VARLEN) |
| 75 | output = tmpl.render(iter=inner()) |
| 76 | assert output == '01234' |
Armin Ronacher | 9bcd411 | 2007-05-29 14:17:24 +0200 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def test_noniter(env): |
| 80 | tmpl = env.from_string(NONITER) |
Armin Ronacher | 10f3ba2 | 2008-04-18 11:30:37 +0200 | [diff] [blame] | 81 | raises(TypeError, tmpl.render) |