Skip to content
Myr

And import tests :)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

--HG-- branch : trunk

authored by Armin Ronacher · committed by Armin Ronacher · parent 2108244 · Browse files · 3 files+148 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+148 added−2 removed

  • other:+148 added−2 removed

3 files+148 added−2 removed

modified:jinja2/testsuite/__init__.py+2 added-1 removed
43 unmodified lines
444546def suite():47 from jinja2.testsuite import ext, filters, tests, core_tags, \48 loader, inheritance48 loader, inheritance, imports49 suite = unittest.TestSuite()50 suite.addTest(ext.suite())51 suite.addTest(filters.suite())52 suite.addTest(tests.suite())53 suite.addTest(core_tags.suite())54 suite.addTest(loader.suite())55 suite.addTest(inheritance.suite())56 suite.addTest(imports.suite())57 return suite
added:jinja2/testsuite/imports.py+144 added
1# -*- coding: utf-8 -*-2"""3 jinja2.testsuite.imports4 ~~~~~~~~~~~~~~~~~~~~~~~~56 Tests the import features (with includes).78 :copyright: (c) 2010 by the Jinja Team.9 :license: BSD, see LICENSE for more details.10"""11import os12import time13import tempfile14import unittest1516from jinja2.testsuite import JinjaTestCase1718from jinja2 import Environment, DictLoader19from jinja2.exceptions import TemplateNotFound, TemplatesNotFound202122test_env = Environment(loader=DictLoader(dict(23 module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}',24 header='[{{ foo }}|{{ 23 }}]',25 o_printer='({{ o }})'26)))27test_env.globals['bar'] = 23282930class ImportsTestCase(JinjaTestCase):3132 def test_context_imports(self):33 t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')34 assert t.render(foo=42) == '[|23]'35 t = test_env.from_string('{% import "module" as m without context %}{{ m.test() }}')36 assert t.render(foo=42) == '[|23]'37 t = test_env.from_string('{% import "module" as m with context %}{{ m.test() }}')38 assert t.render(foo=42) == '[42|23]'39 t = test_env.from_string('{% from "module" import test %}{{ test() }}')40 assert t.render(foo=42) == '[|23]'41 t = test_env.from_string('{% from "module" import test without context %}{{ test() }}')42 assert t.render(foo=42) == '[|23]'43 t = test_env.from_string('{% from "module" import test with context %}{{ test() }}')44 assert t.render(foo=42) == '[42|23]'4546 def test_trailing_comma(self):47 test_env.from_string('{% from "foo" import bar, baz with context %}')48 test_env.from_string('{% from "foo" import bar, baz, with context %}')49 test_env.from_string('{% from "foo" import bar, with context %}')50 test_env.from_string('{% from "foo" import bar, with, context %}')51 test_env.from_string('{% from "foo" import bar, with with context %}')5253 def test_exports(self):54 m = test_env.from_string('''55 {% macro toplevel() %}...{% endmacro %}56 {% macro __private() %}...{% endmacro %}57 {% set variable = 42 %}58 {% for item in [1] %}59 {% macro notthere() %}{% endmacro %}60 {% endfor %}61 ''').module62 assert m.toplevel() == '...'63 assert not hasattr(m, '__missing')64 assert m.variable == 4265 assert not hasattr(m, 'notthere')666768class IncludesTestCase(JinjaTestCase):6970 def test_context_include(self):71 t = test_env.from_string('{% include "header" %}')72 assert t.render(foo=42) == '[42|23]'73 t = test_env.from_string('{% include "header" with context %}')74 assert t.render(foo=42) == '[42|23]'75 t = test_env.from_string('{% include "header" without context %}')76 assert t.render(foo=42) == '[|23]'7778 def test_choice_includes(self):79 t = test_env.from_string('{% include ["missing", "header"] %}')80 assert t.render(foo=42) == '[42|23]'8182 t = test_env.from_string('{% include ["missing", "missing2"] ignore missing %}')83 assert t.render(foo=42) == ''8485 t = test_env.from_string('{% include ["missing", "missing2"] %}')86 self.assert_raises(TemplateNotFound, t.render)87 try:88 t.render()89 except TemplatesNotFound, e:90 assert e.templates == ['missing', 'missing2']91 assert e.name == 'missing2'92 else:93 assert False, 'thou shalt raise'9495 def test_includes(t, **ctx):96 ctx['foo'] = 4297 assert t.render(ctx) == '[42|23]'9899 t = test_env.from_string('{% include ["missing", "header"] %}')100 test_includes(t)101 t = test_env.from_string('{% include x %}')102 test_includes(t, x=['missing', 'header'])103 t = test_env.from_string('{% include [x, "header"] %}')104 test_includes(t, x='missing')105 t = test_env.from_string('{% include x %}')106 test_includes(t, x='header')107 t = test_env.from_string('{% include x %}')108 test_includes(t, x='header')109 t = test_env.from_string('{% include [x] %}')110 test_includes(t, x='header')111112 def test_include_ignoring_missing(self):113 t = test_env.from_string('{% include "missing" %}')114 self.assert_raises(TemplateNotFound, t.render)115 for extra in '', 'with context', 'without context':116 t = test_env.from_string('{% include "missing" ignore missing ' +117 extra + ' %}')118 assert t.render() == ''119120 def test_context_include_with_overrides(self):121 env = Environment(loader=DictLoader(dict(122 main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",123 item="{{ item }}"124 )))125 assert env.get_template("main").render() == "123"126127 def test_unoptimized_scopes(self):128 t = test_env.from_string("""129 {% macro outer(o) %}130 {% macro inner() %}131 {% include "o_printer" %}132 {% endmacro %}133 {{ inner() }}134 {% endmacro %}135 {{ outer("FOO") }}136 """)137 assert t.render().strip() == '(FOO)'138139140def suite():141 suite = unittest.TestSuite()142 suite.addTest(unittest.makeSuite(ImportsTestCase))143 suite.addTest(unittest.makeSuite(IncludesTestCase))144 return suite
modified:jinja2/testsuite/inheritance.py+2 added-1 removed
148 unmodified lines
149 env = Environment(loader=DictLoader({150 'master.html': '{% for item in seq %}[{% block item scoped %}'151 '{% endblock %}]{% endfor %}'152 }))153 t = env.from_string('{% extends "master.html" %}{% block item %}{{ item }}{% endblock %}')153 t = env.from_string('{% extends "master.html" %}{% block item %}'154 '{{ item }}{% endblock %}')155 assert t.render(seq=range(5)) == '[0][1][2][3][4]'156157 def test_super_in_scoped_block(self):158 env = Environment(loader=DictLoader({
50 unmodified lines