imports.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding: utf-8 -*-
  2. """
  3. jinja2.testsuite.imports
  4. ~~~~~~~~~~~~~~~~~~~~~~~~
  5. Tests the import features (with includes).
  6. :copyright: (c) 2010 by the Jinja Team.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import unittest
  10. from jinja2.testsuite import JinjaTestCase
  11. from jinja2 import Environment, DictLoader
  12. from jinja2.exceptions import TemplateNotFound, TemplatesNotFound
  13. test_env = Environment(loader=DictLoader(dict(
  14. module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}',
  15. header='[{{ foo }}|{{ 23 }}]',
  16. o_printer='({{ o }})'
  17. )))
  18. test_env.globals['bar'] = 23
  19. class ImportsTestCase(JinjaTestCase):
  20. def test_context_imports(self):
  21. t = test_env.from_string('{% import "module" as m %}{{ m.test() }}')
  22. assert t.render(foo=42) == '[|23]'
  23. t = test_env.from_string('{% import "module" as m without context %}{{ m.test() }}')
  24. assert t.render(foo=42) == '[|23]'
  25. t = test_env.from_string('{% import "module" as m with context %}{{ m.test() }}')
  26. assert t.render(foo=42) == '[42|23]'
  27. t = test_env.from_string('{% from "module" import test %}{{ test() }}')
  28. assert t.render(foo=42) == '[|23]'
  29. t = test_env.from_string('{% from "module" import test without context %}{{ test() }}')
  30. assert t.render(foo=42) == '[|23]'
  31. t = test_env.from_string('{% from "module" import test with context %}{{ test() }}')
  32. assert t.render(foo=42) == '[42|23]'
  33. def test_trailing_comma(self):
  34. test_env.from_string('{% from "foo" import bar, baz with context %}')
  35. test_env.from_string('{% from "foo" import bar, baz, with context %}')
  36. test_env.from_string('{% from "foo" import bar, with context %}')
  37. test_env.from_string('{% from "foo" import bar, with, context %}')
  38. test_env.from_string('{% from "foo" import bar, with with context %}')
  39. def test_exports(self):
  40. m = test_env.from_string('''
  41. {% macro toplevel() %}...{% endmacro %}
  42. {% macro __private() %}...{% endmacro %}
  43. {% set variable = 42 %}
  44. {% for item in [1] %}
  45. {% macro notthere() %}{% endmacro %}
  46. {% endfor %}
  47. ''').module
  48. assert m.toplevel() == '...'
  49. assert not hasattr(m, '__missing')
  50. assert m.variable == 42
  51. assert not hasattr(m, 'notthere')
  52. class IncludesTestCase(JinjaTestCase):
  53. def test_context_include(self):
  54. t = test_env.from_string('{% include "header" %}')
  55. assert t.render(foo=42) == '[42|23]'
  56. t = test_env.from_string('{% include "header" with context %}')
  57. assert t.render(foo=42) == '[42|23]'
  58. t = test_env.from_string('{% include "header" without context %}')
  59. assert t.render(foo=42) == '[|23]'
  60. def test_choice_includes(self):
  61. t = test_env.from_string('{% include ["missing", "header"] %}')
  62. assert t.render(foo=42) == '[42|23]'
  63. t = test_env.from_string('{% include ["missing", "missing2"] ignore missing %}')
  64. assert t.render(foo=42) == ''
  65. t = test_env.from_string('{% include ["missing", "missing2"] %}')
  66. self.assert_raises(TemplateNotFound, t.render)
  67. try:
  68. t.render()
  69. except TemplatesNotFound as e:
  70. assert e.templates == ['missing', 'missing2']
  71. assert e.name == 'missing2'
  72. else:
  73. assert False, 'thou shalt raise'
  74. def test_includes(t, **ctx):
  75. ctx['foo'] = 42
  76. assert t.render(ctx) == '[42|23]'
  77. t = test_env.from_string('{% include ["missing", "header"] %}')
  78. test_includes(t)
  79. t = test_env.from_string('{% include x %}')
  80. test_includes(t, x=['missing', 'header'])
  81. t = test_env.from_string('{% include [x, "header"] %}')
  82. test_includes(t, x='missing')
  83. t = test_env.from_string('{% include x %}')
  84. test_includes(t, x='header')
  85. t = test_env.from_string('{% include x %}')
  86. test_includes(t, x='header')
  87. t = test_env.from_string('{% include [x] %}')
  88. test_includes(t, x='header')
  89. def test_include_ignoring_missing(self):
  90. t = test_env.from_string('{% include "missing" %}')
  91. self.assert_raises(TemplateNotFound, t.render)
  92. for extra in '', 'with context', 'without context':
  93. t = test_env.from_string('{% include "missing" ignore missing ' +
  94. extra + ' %}')
  95. assert t.render() == ''
  96. def test_context_include_with_overrides(self):
  97. env = Environment(loader=DictLoader(dict(
  98. main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}",
  99. item="{{ item }}"
  100. )))
  101. assert env.get_template("main").render() == "123"
  102. def test_unoptimized_scopes(self):
  103. t = test_env.from_string("""
  104. {% macro outer(o) %}
  105. {% macro inner() %}
  106. {% include "o_printer" %}
  107. {% endmacro %}
  108. {{ inner() }}
  109. {% endmacro %}
  110. {{ outer("FOO") }}
  111. """)
  112. assert t.render().strip() == '(FOO)'
  113. def suite():
  114. suite = unittest.TestSuite()
  115. suite.addTest(unittest.makeSuite(ImportsTestCase))
  116. suite.addTest(unittest.makeSuite(IncludesTestCase))
  117. return suite