def test_preserve_blocks(self): env = Environment(loader=DictLoader({ 'a': '{% if false %}{% block x %}A{% endblock %}{% endif %}{{ self.x() }}', 'b': '{% extends "a" %}{% block x %}B{{ super() }}{% endblock %}' })) tmpl = env.get_template('b') tmpl.assert_render() == 'BA'
def test_correct_prefix_loader_name(self): env = Environment(loader=PrefixLoader({ 'foo': DictLoader({}) })) try: env.get_template('foo/bar.html') except TemplateNotFound, e: assert e.name == 'foo/bar.html'
def test_super_in_scoped_block(self): env = Environment(loader=DictLoader({ 'master.html': '{% for item in seq %}[{% block item scoped %}' '{{ item }}{% endblock %}]{% endfor %}' })) t = env.from_string('{% extends "master.html" %}{% block item %}' '{{ super() }}|{{ item * 2 }}{% endblock %}') t.assert_render(seq=range(5)) == '[0|0][1|2][2|4][3|6][4|8]'
def test_dynamic_inheritance(self): env = Environment(loader=DictLoader({ 'master1': 'MASTER1{% block x %}{% endblock %}', 'master2': 'MASTER2{% block x %}{% endblock %}', 'child': '{% extends master %}{% block x %}CHILD{% endblock %}' })) tmpl = env.get_template('child') for m in range(1, 3): tmpl.assert_render(master='master%d' % m) == 'MASTER%dCHILD' % m
def test_extends_output_bugs(self): env = Environment(loader=DictLoader({ 'parent.html': '(({% block title %}{% endblock %}))' })) t = env.from_string('{% if expr %}{% extends "parent.html" %}{% endif %}' '[[{% block title %}title{% endblock %}]]' '{% for item in [1, 2, 3] %}({{ item }}){% endfor %}') t.assert_render(expr=False) == '[[title]](1)(2)(3)' t.assert_render(expr=True) == '((title))'
def test_multi_inheritance(self): env = Environment(loader=DictLoader({ 'master1': 'MASTER1{% block x %}{% endblock %}', 'master2': 'MASTER2{% block x %}{% endblock %}', 'child': '''{% if master %}{% extends master %}{% else %}{% extends 'master1' %}{% endif %}{% block x %}CHILD{% endblock %}''' })) tmpl = env.get_template('child') tmpl.assert_render(master='master2') == 'MASTER2CHILD' tmpl.assert_render(master='master1') == 'MASTER1CHILD' tmpl.assert_render() == 'MASTER1CHILD'
def test_urlencode(self): env = Environment() tmpl = env.from_string('{{ "Hello, world!"|urlencode }}') tmpl.assert_render() == "Hello%2C%20world%21" tmpl = env.from_string("{{ o|urlencode }}") tmpl.assert_render(o=u"Hello, world\u203d") == "Hello%2C%20world%E2%80%BD" tmpl.assert_render(o=(("f", 1),)) == "f=1" tmpl.assert_render(o=(("f", 1), ("z", 2))) == "f=1&z=2" tmpl.assert_render(o=((u"\u203d", 1),)) == "%E2%80%BD=1" tmpl.assert_render(o={u"\u203d": 1}) == "%E2%80%BD=1" tmpl.assert_render(o={0: 1}) == "0=1"
def test_super(self): env = Environment(loader=DictLoader({ 'a': '{% block intro %}INTRO{% endblock %}|' 'BEFORE|{% block data %}INNER{% endblock %}|AFTER', 'b': '{% extends "a" %}{% block data %}({{ ' 'super() }}){% endblock %}', 'c': '{% extends "b" %}{% block intro %}--{{ ' 'super() }}--{% endblock %}\n{% block data ' '%}[{{ super() }}]{% endblock %}' })) tmpl = env.get_template('c') tmpl.assert_render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'
def test_scoped_block_after_inheritance(self): env = Environment(loader=DictLoader({ 'layout.html': ''' {% block useless %}{% endblock %} ''', 'index.html': ''' {%- extends 'layout.html' %} {% from 'helpers.html' import foo with context %} {% block useless %} {% for x in [1, 2, 3] %} {% block testing scoped %} {{ foo(x) }} {% endblock %} {% endfor %} {% endblock %} ''', 'helpers.html': ''' {% macro foo(x) %}{{ the_foo + x }}{% endmacro %} ''' })) rv = env.get_template('index.html').assert_render(the_foo=42).split() assert rv == ['43', '44', '45']
def test_stacked_locals_scoping_bug(self): env = Environment(line_statement_prefix='#') t = env.from_string('''\ # for j in [1, 2]: # set x = 1 # for i in [1, 2]: # print x # if i % 2 == 0: # set x = x + 1 # endif # endfor # endfor # if a # print 'A' # elif b # print 'B' # elif c == d # print 'C' # else # print 'D' # endif ''') t.assert_render(a=0, b=False, c=42, d=42.0) == '1111C'
def test_context_include_with_overrides(self): env = Environment(loader=DictLoader(dict( main="{% for item in [1, 2, 3] %}{% include 'item' %}{% endfor %}", item="{{ item }}" ))) assert env.get_template("main").assert_render() == "123"
# -*- coding: utf-8 -*- import unittest from jinja2.testsuite import JinjaTestCase from jinja2 import DictLoader from jinja2.exceptions import TemplateNotFound, TemplatesNotFound from jinja2js.testsuite import Environment test_env = Environment(loader=DictLoader(dict( module='{% macro test() %}[{{ foo }}|{{ bar }}]{% endmacro %}', header='[{{ foo }}|{{ 23 }}]', o_printer='({{ o }})' ))) test_env.globals['bar'] = 23 class ImportsTestCase(JinjaTestCase): def test_context_imports(self): t = test_env.from_string('{% import "module" as m %}{{ m.test() }}') t.assert_render(foo=42) == '[|23]' t = test_env.from_string('{% import "module" as m without context %}{{ m.test() }}') t.assert_render(foo=42) == '[|23]' t = test_env.from_string('{% import "module" as m with context %}{{ m.test() }}') t.assert_render(foo=42) == '[42|23]' t = test_env.from_string('{% from "module" import test %}{{ test() }}') t.assert_render(foo=42) == '[|23]' t = test_env.from_string('{% from "module" import test without context %}{{ test() }}') t.assert_render(foo=42) == '[|23]'
def test_keyword_folding(self): env = Environment() env.filters['testing'] = lambda value, some: value + some assert env.from_string("{{ 'test'|testing(some='stuff') }}") \ .assert_render() == 'teststuff'
def test_replace(self): env = Environment() tmpl = env.from_string('{{ string|replace("o", 42) }}') tmpl.assert_render(string="<foo>") == "<f4242>"