~brad-marshall/charms/trusty/apache2-wsgi/fix-haproxy-relations

« back to all changes in this revision

Viewing changes to hooks/lib/jinja2/testsuite/imports.py

  • Committer: Robin Winslow
  • Date: 2014-05-27 14:00:44 UTC
  • Revision ID: robin.winslow@canonical.com-20140527140044-8rpmb3wx4djzwa83
Add all files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
"""
 
3
    jinja2.testsuite.imports
 
4
    ~~~~~~~~~~~~~~~~~~~~~~~~
 
5
 
 
6
    Tests the import features (with includes).
 
7
 
 
8
    :copyright: (c) 2010 by the Jinja Team.
 
9
    :license: BSD, see LICENSE for more details.
 
10
"""
 
11
import unittest
 
12
 
 
13
from jinja2.testsuite import JinjaTestCase
 
14
 
 
15
from jinja2 import Environment, DictLoader
 
16
from jinja2.exceptions import TemplateNotFound, TemplatesNotFound
 
17
 
 
18
 
 
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
 
25
 
 
26
 
 
27
class ImportsTestCase(JinjaTestCase):
 
28
 
 
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]'
 
42
 
 
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 %}')
 
49
 
 
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')
 
63
 
 
64
 
 
65
class IncludesTestCase(JinjaTestCase):
 
66
 
 
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]'
 
74
 
 
75
    def test_choice_includes(self):
 
76
        t = test_env.from_string('{% include ["missing", "header"] %}')
 
77
        assert t.render(foo=42) == '[42|23]'
 
78
 
 
79
        t = test_env.from_string('{% include ["missing", "missing2"] ignore missing %}')
 
80
        assert t.render(foo=42) == ''
 
81
 
 
82
        t = test_env.from_string('{% include ["missing", "missing2"] %}')
 
83
        self.assert_raises(TemplateNotFound, t.render)
 
84
        try:
 
85
            t.render()
 
86
        except TemplatesNotFound as e:
 
87
            assert e.templates == ['missing', 'missing2']
 
88
            assert e.name == 'missing2'
 
89
        else:
 
90
            assert False, 'thou shalt raise'
 
91
 
 
92
        def test_includes(t, **ctx):
 
93
            ctx['foo'] = 42
 
94
            assert t.render(ctx) == '[42|23]'
 
95
 
 
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')
 
108
 
 
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() == ''
 
116
 
 
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 }}"
 
121
        )))
 
122
        assert env.get_template("main").render() == "123"
 
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)'
 
135
 
 
136
 
 
137
def suite():
 
138
    suite = unittest.TestSuite()
 
139
    suite.addTest(unittest.makeSuite(ImportsTestCase))
 
140
    suite.addTest(unittest.makeSuite(IncludesTestCase))
 
141
    return suite