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') assert tmpl.render() == 'BA'
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 %}') assert t.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): assert tmpl.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 %}') assert t.render(expr=False) == '[[title]](1)(2)(3)' assert t.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') assert tmpl.render(master='master2') == 'MASTER2CHILD' assert tmpl.render(master='master1') == 'MASTER1CHILD' assert tmpl.render() == 'MASTER1CHILD'
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') assert tmpl.render() == '--INTRO--|BEFORE|[(INNER)]|AFTER'
def test_autoescape_autoselect(self): def select_autoescape(name): if name is None or '.' not in name: return False return name.endswith('.html') env = Environment(autoescape=select_autoescape, loader=DictLoader({ 'test.txt': '{{ foo }}', 'test.html': '{{ foo }}' })) t = env.get_template('test.txt') assert t.render(foo='<foo>') == '<foo>' t = env.get_template('test.html') assert t.render(foo='<foo>') == '<foo>' t = env.from_string('{{ foo }}') assert t.render(foo='<foo>') == '<foo>'
def test_fixed_macro_scoping_bug(self): assert Environment(loader=DictLoader({ 'test.html': '''\ {% extends 'details.html' %} {% macro my_macro() %} my_macro {% endmacro %} {% block inner_box %} {{ my_macro() }} {% endblock %} ''', 'details.html': '''\ {% extends 'standard.html' %} {% macro my_macro() %} my_macro {% endmacro %} {% block content %} {% block outer_box %} outer_box {% block inner_box %} inner_box {% endblock %} {% endblock %} {% endblock %} ''', 'standard.html': ''' {% block content %} {% endblock %} ''' })).get_template("test.html").render().split() == [ u'outer_box', u'my_macro' ]
def test_include(self): self.env = Environment(loader=DictLoader( {'include': '{% macro test(foo) %}[{{ foo }}]{% endmacro %}'})) tmpl = self.env.from_string( '{% from "include" import test %}{{ test("foo") }}') assert tmpl.render() == '[foo]'
WORKINGTEMPLATE = '''\ {% extends "layout" %} {% block block1 %} {% if false %} {% block block2 %} this should workd {% endblock %} {% endif %} {% endblock %} ''' env = Environment(loader=DictLoader({ 'layout': LAYOUTTEMPLATE, 'level1': LEVEL1TEMPLATE, 'level2': LEVEL2TEMPLATE, 'level3': LEVEL3TEMPLATE, 'level4': LEVEL4TEMPLATE, 'working': WORKINGTEMPLATE }), trim_blocks=True) class InheritanceTestCase(JinjaTestCase): def test_layout(self): tmpl = env.get_template('layout') assert tmpl.render() == ('|block 1 from layout|block 2 from ' 'layout|nested block 4 from layout|') def test_level1(self): tmpl = env.get_template('level1') assert tmpl.render() == ('|block 1 from level1|block 2 from '
@contextfunction def gettext(context, string): language = context.get('LANGUAGE', 'en') return languages.get(language, {}).get(string, string) @contextfunction def ngettext(context, s, p, n): language = context.get('LANGUAGE', 'en') if n != 1: return languages.get(language, {}).get(p, p) return languages.get(language, {}).get(s, s) i18n_env = Environment( loader=DictLoader(i18n_templates), extensions=['ambari_jinja2.ext.i18n'] ) i18n_env.globals.update({ '_': gettext, 'gettext': gettext, 'ngettext': ngettext }) newstyle_i18n_env = Environment( loader=DictLoader(newstyle_i18n_templates), extensions=['ambari_jinja2.ext.i18n'] ) newstyle_i18n_env.install_gettext_callables(gettext, ngettext, newstyle=True) class TestExtension(Extension):
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").render() == "123"
:copyright: (c) 2010 by the Jinja Team. :license: BSD, see LICENSE for more details. """ import os import time import tempfile import unittest from ambari_jinja2.testsuite import JinjaTestCase from ambari_jinja2 import Environment, DictLoader from ambari_jinja2.exceptions import TemplateNotFound, TemplatesNotFound 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() }}') assert t.render(foo=42) == '[|23]' t = test_env.from_string( '{% import "module" as m without context %}{{ m.test() }}') assert t.render(foo=42) == '[|23]' t = test_env.from_string( '{% import "module" as m with context %}{{ m.test() }}') assert t.render(foo=42) == '[42|23]' t = test_env.from_string('{% from "module" import test %}{{ test() }}')
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'