Armin Ronacher | 93e14c2 | 2007-03-20 16:17:48 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | unit test for various things |
| 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 | |
| 6 | :copyright: 2007 by Armin Ronacher. |
| 7 | :license: BSD, see LICENSE for more details. |
| 8 | """ |
Christoph Hack | e9e43bb | 2008-04-13 23:35:48 +0200 | [diff] [blame] | 9 | from jinja2.exceptions import TemplateSyntaxError |
Armin Ronacher | 93e14c2 | 2007-03-20 16:17:48 +0100 | [diff] [blame] | 10 | |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 11 | |
Armin Ronacher | 93e14c2 | 2007-03-20 16:17:48 +0100 | [diff] [blame] | 12 | UNPACKING = '''{% for a, b, c in [[1, 2, 3]] %}{{ a }}|{{ b }}|{{ c }}{% endfor %}''' |
Armin Ronacher | 93e14c2 | 2007-03-20 16:17:48 +0100 | [diff] [blame] | 13 | RAW = '''{% raw %}{{ FOO }} and {% BAR %}{% endraw %}''' |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 14 | CONST = '''{{ true }}|{{ false }}|{{ none }}|\ |
| 15 | {{ none is defined }}|{{ missing is defined }}''' |
Armin Ronacher | 0a2ac69 | 2008-05-13 01:03:08 +0200 | [diff] [blame] | 16 | LOCALSET = '''{% set foo = 0 %}\ |
| 17 | {% for item in [1, 2] %}{% set foo = 1 %}{% endfor %}\ |
Armin Ronacher | 1cc232c | 2007-09-07 17:52:41 +0200 | [diff] [blame] | 18 | {{ foo }}''' |
Armin Ronacher | 0a2ac69 | 2008-05-13 01:03:08 +0200 | [diff] [blame] | 19 | CONSTASS1 = '''{% set true = 42 %}''' |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 20 | CONSTASS2 = '''{% for none in seq %}{% endfor %}''' |
Armin Ronacher | 93e14c2 | 2007-03-20 16:17:48 +0100 | [diff] [blame] | 21 | |
| 22 | |
| 23 | def test_unpacking(env): |
| 24 | tmpl = env.from_string(UNPACKING) |
| 25 | assert tmpl.render() == '1|2|3' |
| 26 | |
| 27 | |
| 28 | def test_raw(env): |
| 29 | tmpl = env.from_string(RAW) |
| 30 | assert tmpl.render() == '{{ FOO }} and {% BAR %}' |
| 31 | |
| 32 | |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 33 | def test_lru_cache(): |
| 34 | from jinja2.utils import LRUCache |
| 35 | d = LRUCache(3) |
Armin Ronacher | 93e14c2 | 2007-03-20 16:17:48 +0100 | [diff] [blame] | 36 | d["a"] = 1 |
| 37 | d["b"] = 2 |
| 38 | d["c"] = 3 |
| 39 | d["a"] |
| 40 | d["d"] = 4 |
| 41 | assert len(d) == 3 |
| 42 | assert 'a' in d and 'c' in d and 'd' in d and 'b' not in d |
Armin Ronacher | ee2c18e | 2007-04-20 22:39:04 +0200 | [diff] [blame] | 43 | |
| 44 | |
Armin Ronacher | ecc051b | 2007-06-01 18:25:28 +0200 | [diff] [blame] | 45 | def test_const(env): |
| 46 | tmpl = env.from_string(CONST) |
Armin Ronacher | 4f7d2d5 | 2008-04-22 10:40:26 +0200 | [diff] [blame] | 47 | assert tmpl.render() == 'True|False|None|True|False' |
Armin Ronacher | ecc051b | 2007-06-01 18:25:28 +0200 | [diff] [blame] | 48 | |
| 49 | |
| 50 | def test_const_assign(env): |
| 51 | for tmpl in CONSTASS1, CONSTASS2: |
| 52 | try: |
| 53 | env.from_string(tmpl) |
| 54 | except TemplateSyntaxError: |
| 55 | pass |
| 56 | else: |
| 57 | raise AssertionError('expected syntax error') |
Armin Ronacher | 1cc232c | 2007-09-07 17:52:41 +0200 | [diff] [blame] | 58 | |
| 59 | |
| 60 | def test_localset(env): |
| 61 | tmpl = env.from_string(LOCALSET) |
| 62 | assert tmpl.render() == '0' |