Removed spurious static_path.
[smonitor.git] / monitor / jinja2 / testsuite / imports.py
blob1cb12cbd38ac18d4eeee56e3cf8a4a91f1998a7f
1 # -*- coding: utf-8 -*-
2 """
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.
10 """
11 import unittest
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 }}]',
22 o_printer='({{ o }})'
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 %}
55 {% for item in [1] %}
56 {% macro notthere() %}{% endmacro %}
57 {% endfor %}
58 ''').module
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)
84 try:
85 t.render()
86 except TemplatesNotFound, e:
87 assert e.templates == ['missing', 'missing2']
88 assert e.name == 'missing2'
89 else:
90 assert False, 'thou shalt raise'
92 def test_includes(t, **ctx):
93 ctx['foo'] = 42
94 assert t.render(ctx) == '[42|23]'
96 t = test_env.from_string('{% include ["missing", "header"] %}')
97 test_includes(t)
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 ' +
114 extra + ' %}')
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 %}",
120 item="{{ item }}"
122 assert env.get_template("main").render() == "123"
124 def test_unoptimized_scopes(self):
125 t = test_env.from_string("""
126 {% macro outer(o) %}
127 {% macro inner() %}
128 {% include "o_printer" %}
129 {% endmacro %}
130 {{ inner() }}
131 {% endmacro %}
132 {{ outer("FOO") }}
133 """)
134 assert t.render().strip() == '(FOO)'
137 def suite():
138 suite = unittest.TestSuite()
139 suite.addTest(unittest.makeSuite(ImportsTestCase))
140 suite.addTest(unittest.makeSuite(IncludesTestCase))
141 return suite