NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

imports.py (4183B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\jinja2\testsuite\imports.py
      2 import unittest
      3 from jinja2.testsuite import JinjaTestCase
      4 from jinja2 import Environment, DictLoader
      5 from jinja2.exceptions import TemplateNotFound, TemplatesNotFound
      6 test_env = Environment(loader=DictLoader(dict(module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}', header='[{{ foo }}|{{ 23 }}]', o_printer='({{ o }})')))
      7 test_env.globals['bar'] = 23
      8 
      9 class ImportsTestCase(JinjaTestCase):
     10 
     11     def test_context_imports(self):
     12         t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')
     13         t = test_env.from_string('{% import "module" as m without context %}{{ m.test() }}')
     14         t = test_env.from_string('{% import "module" as m with context %}{{ m.test() }}')
     15         t = test_env.from_string('{% from "module" import test %}{{ test() }}')
     16         t = test_env.from_string('{% from "module" import test without context %}{{ test() }}')
     17         t = test_env.from_string('{% from "module" import test with context %}{{ test() }}')
     18 
     19     def test_trailing_comma(self):
     20         test_env.from_string('{% from "foo" import bar, baz with context %}')
     21         test_env.from_string('{% from "foo" import bar, baz, with context %}')
     22         test_env.from_string('{% from "foo" import bar, with context %}')
     23         test_env.from_string('{% from "foo" import bar, with, context %}')
     24         test_env.from_string('{% from "foo" import bar, with with context %}')
     25 
     26     def test_exports(self):
     27         m = test_env.from_string('\n            {% macro toplevel() %}...{% endmacro %}\n            {% macro __private() %}...{% endmacro %}\n            {% set variable = 42 %}\n            {% for item in [1] %}\n                {% macro notthere() %}{% endmacro %}\n            {% endfor %}\n        ').module
     28 
     29 
     30 class IncludesTestCase(JinjaTestCase):
     31 
     32     def test_context_include(self):
     33         t = test_env.from_string('{% include "header" %}')
     34         t = test_env.from_string('{% include "header" with context %}')
     35         t = test_env.from_string('{% include "header" without context %}')
     36 
     37     def test_choice_includes(self):
     38         t = test_env.from_string('{% include ["missing", "header"] %}')
     39         t = test_env.from_string('{% include ["missing", "missing2"] ignore missing %}')
     40         t = test_env.from_string('{% include ["missing", "missing2"] %}')
     41         self.assert_raises(TemplateNotFound, t.render)
     42         try:
     43             t.render()
     44         except TemplatesNotFound as e:
     45             pass
     46 
     47         def test_includes(t, **ctx):
     48             ctx['foo'] = 42
     49 
     50         t = test_env.from_string('{% include ["missing", "header"] %}')
     51         test_includes(t)
     52         t = test_env.from_string('{% include x %}')
     53         test_includes(t, x=['missing', 'header'])
     54         t = test_env.from_string('{% include [x, "header"] %}')
     55         test_includes(t, x='missing')
     56         t = test_env.from_string('{% include x %}')
     57         test_includes(t, x='header')
     58         t = test_env.from_string('{% include x %}')
     59         test_includes(t, x='header')
     60         t = test_env.from_string('{% include [x] %}')
     61         test_includes(t, x='header')
     62 
     63     def test_include_ignoring_missing(self):
     64         t = test_env.from_string('{% include "missing" %}')
     65         self.assert_raises(TemplateNotFound, t.render)
     66         for extra in ('', 'with context', 'without context'):
     67             t = test_env.from_string('{% include "missing" ignore missing ' + extra + ' %}')
     68 
     69     def test_context_include_with_overrides(self):
     70         env = Environment(loader=DictLoader(dict(main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}", item='{{ item }}')))
     71 
     72     def test_unoptimized_scopes(self):
     73         t = test_env.from_string('\n            {% macro outer(o) %}\n            {% macro inner() %}\n            {% include "o_printer" %}\n            {% endmacro %}\n            {{ inner() }}\n            {% endmacro %}\n            {{ outer("FOO") }}\n        ')
     74 
     75 
     76 def suite():
     77     suite = unittest.TestSuite()
     78     suite.addTest(unittest.makeSuite(ImportsTestCase))
     79     suite.addTest(unittest.makeSuite(IncludesTestCase))
     80     return suite