1
# -*- coding: utf-8 -*-
3
jinja2.testsuite.imports
4
~~~~~~~~~~~~~~~~~~~~~~~~
6
Tests the import features (with includes).
8
:copyright: (c) 2010 by the Jinja Team.
9
:license: BSD, see LICENSE for more details.
13
from jinja2.testsuite import JinjaTestCase
15
from jinja2 import Environment, DictLoader
16
from jinja2.exceptions import TemplateNotFound, TemplatesNotFound
19
test_env = Environment(loader=DictLoader(dict(
20
module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}',
21
header='[{{ foo }}|{{ 23 }}]',
24
test_env.globals['bar'] = 23
27
class ImportsTestCase(JinjaTestCase):
29
def test_context_imports(self):
30
t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')
31
assert t.render(foo=42) == '[|23]'
32
t = test_env.from_string('{% import "module" as m without context %}{{ m.test() }}')
33
assert t.render(foo=42) == '[|23]'
34
t = test_env.from_string('{% import "module" as m with context %}{{ m.test() }}')
35
assert t.render(foo=42) == '[42|23]'
36
t = test_env.from_string('{% from "module" import test %}{{ test() }}')
37
assert t.render(foo=42) == '[|23]'
38
t = test_env.from_string('{% from "module" import test without context %}{{ test() }}')
39
assert t.render(foo=42) == '[|23]'
40
t = test_env.from_string('{% from "module" import test with context %}{{ test() }}')
41
assert t.render(foo=42) == '[42|23]'
43
def test_trailing_comma(self):
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 %}')
47
test_env.from_string('{% from "foo" import bar, with, context %}')
48
test_env.from_string('{% from "foo" import bar, with with context %}')
50
def test_exports(self):
51
m = test_env.from_string('''
52
{% macro toplevel() %}...{% endmacro %}
53
{% macro __private() %}...{% endmacro %}
54
{% set variable = 42 %}
56
{% macro notthere() %}{% endmacro %}
59
assert m.toplevel() == '...'
60
assert not hasattr(m, '__missing')
61
assert m.variable == 42
62
assert not hasattr(m, 'notthere')
65
class IncludesTestCase(JinjaTestCase):
67
def test_context_include(self):
68
t = test_env.from_string('{% include "header" %}')
69
assert t.render(foo=42) == '[42|23]'
70
t = test_env.from_string('{% include "header" with context %}')
71
assert t.render(foo=42) == '[42|23]'
72
t = test_env.from_string('{% include "header" without context %}')
73
assert t.render(foo=42) == '[|23]'
75
def test_choice_includes(self):
76
t = test_env.from_string('{% include ["missing", "header"] %}')
77
assert t.render(foo=42) == '[42|23]'
79
t = test_env.from_string('{% include ["missing", "missing2"] ignore missing %}')
80
assert t.render(foo=42) == ''
82
t = test_env.from_string('{% include ["missing", "missing2"] %}')
83
self.assert_raises(TemplateNotFound, t.render)
86
except TemplatesNotFound as e:
87
assert e.templates == ['missing', 'missing2']
88
assert e.name == 'missing2'
90
assert False, 'thou shalt raise'
92
def test_includes(t, **ctx):
94
assert t.render(ctx) == '[42|23]'
96
t = test_env.from_string('{% include ["missing", "header"] %}')
98
t = test_env.from_string('{% include x %}')
99
test_includes(t, x=['missing', 'header'])
100
t = test_env.from_string('{% include [x, "header"] %}')
101
test_includes(t, x='missing')
102
t = test_env.from_string('{% include x %}')
103
test_includes(t, x='header')
104
t = test_env.from_string('{% include x %}')
105
test_includes(t, x='header')
106
t = test_env.from_string('{% include [x] %}')
107
test_includes(t, x='header')
109
def test_include_ignoring_missing(self):
110
t = test_env.from_string('{% include "missing" %}')
111
self.assert_raises(TemplateNotFound, t.render)
112
for extra in '', 'with context', 'without context':
113
t = test_env.from_string('{% include "missing" ignore missing ' +
115
assert t.render() == ''
117
def test_context_include_with_overrides(self):
118
env = Environment(loader=DictLoader(dict(
119
main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",
122
assert env.get_template("main").render() == "123"
124
def test_unoptimized_scopes(self):
125
t = test_env.from_string("""
128
{% include "o_printer" %}
134
assert t.render().strip() == '(FOO)'
138
suite = unittest.TestSuite()
139
suite.addTest(unittest.makeSuite(ImportsTestCase))
140
suite.addTest(unittest.makeSuite(IncludesTestCase))