Armin Ronacher | 132757b | 2008-05-02 23:51:32 +0200 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | unit test for the imports and includes |
| 4 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 | |
| 6 | :copyright: 2007 by Armin Ronacher. |
| 7 | :license: BSD, see LICENSE for more details. |
| 8 | """ |
| 9 | from jinja2 import Environment, DictLoader |
| 10 | |
| 11 | |
| 12 | test_env = Environment(loader=DictLoader(dict( |
| 13 | module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}', |
| 14 | header='[{{ foo }}|{{ 23 }}]' |
| 15 | ))) |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 16 | test_env.globals['bar'] = 23 |
Armin Ronacher | 132757b | 2008-05-02 23:51:32 +0200 | [diff] [blame] | 17 | |
| 18 | |
| 19 | def test_context_imports(): |
| 20 | t = test_env.from_string('{% import "module" as m %}{{ m.test() }}') |
| 21 | assert t.render(foo=42) == '[|23]' |
| 22 | t = test_env.from_string('{% import "module" as m without context %}{{ m.test() }}') |
| 23 | assert t.render(foo=42) == '[|23]' |
| 24 | t = test_env.from_string('{% import "module" as m with context %}{{ m.test() }}') |
| 25 | assert t.render(foo=42) == '[42|23]' |
| 26 | t = test_env.from_string('{% from "module" import test %}{{ test() }}') |
| 27 | assert t.render(foo=42) == '[|23]' |
| 28 | t = test_env.from_string('{% from "module" import test without context %}{{ test() }}') |
| 29 | assert t.render(foo=42) == '[|23]' |
| 30 | t = test_env.from_string('{% from "module" import test with context %}{{ test() }}') |
| 31 | assert t.render(foo=42) == '[42|23]' |
| 32 | |
| 33 | |
| 34 | def test_context_include(): |
| 35 | t = test_env.from_string('{% include "header" %}') |
| 36 | assert t.render(foo=42) == '[42|23]' |
| 37 | t = test_env.from_string('{% include "header" with context %}') |
| 38 | assert t.render(foo=42) == '[42|23]' |
| 39 | t = test_env.from_string('{% include "header" without context %}') |
| 40 | assert t.render(foo=42) == '[|23]' |
| 41 | |
| 42 | |
| 43 | def test_trailing_comma(): |
| 44 | test_env.from_string('{% from "foo" import bar, baz with context %}') |
| 45 | test_env.from_string('{% from "foo" import bar, baz, with context %}') |
| 46 | test_env.from_string('{% from "foo" import bar, with context %}') |
Armin Ronacher | 7ceced5 | 2008-05-03 10:15:31 +0200 | [diff] [blame] | 47 | test_env.from_string('{% from "foo" import bar, with, context %}') |
| 48 | test_env.from_string('{% from "foo" import bar, with with context %}') |