From: Armin Ronacher Date: Fri, 2 May 2008 21:51:32 +0000 (+0200) Subject: added unittest for context import/includes X-Git-Tag: 2.0rc1~107 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=132757b7cc87b9575e4c34ef65677743b304dc1f;p=jinja2.git added unittest for context import/includes --HG-- branch : trunk --- diff --git a/tests/test_imports.py b/tests/test_imports.py new file mode 100644 index 0000000..7ce8bde --- /dev/null +++ b/tests/test_imports.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- +""" + unit test for the imports and includes + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyright: 2007 by Armin Ronacher. + :license: BSD, see LICENSE for more details. +""" +from jinja2 import Environment, DictLoader + + +test_env = Environment(loader=DictLoader(dict( + module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}', + header='[{{ foo }}|{{ 23 }}]' +))) +test_env.globals['bar'] = '23' + + +def test_context_imports(): + t = test_env.from_string('{% import "module" as m %}{{ m.test() }}') + assert t.render(foo=42) == '[|23]' + t = test_env.from_string('{% import "module" as m without context %}{{ m.test() }}') + assert t.render(foo=42) == '[|23]' + t = test_env.from_string('{% import "module" as m with context %}{{ m.test() }}') + assert t.render(foo=42) == '[42|23]' + t = test_env.from_string('{% from "module" import test %}{{ test() }}') + assert t.render(foo=42) == '[|23]' + t = test_env.from_string('{% from "module" import test without context %}{{ test() }}') + assert t.render(foo=42) == '[|23]' + t = test_env.from_string('{% from "module" import test with context %}{{ test() }}') + assert t.render(foo=42) == '[42|23]' + + +def test_context_include(): + t = test_env.from_string('{% include "header" %}') + assert t.render(foo=42) == '[42|23]' + t = test_env.from_string('{% include "header" with context %}') + assert t.render(foo=42) == '[42|23]' + t = test_env.from_string('{% include "header" without context %}') + assert t.render(foo=42) == '[|23]' + + +def test_trailing_comma(): + test_env.from_string('{% from "foo" import bar, baz with context %}') + test_env.from_string('{% from "foo" import bar, baz, with context %}') + test_env.from_string('{% from "foo" import bar, with context %}')