def test_include(self): loader = DictLoader({ "index.html": '{% include "header.html" %}\nbody text', "header.html": "header text", }) self.assertEqual(loader.load("index.html").generate(), b("header text\nbody text"))
def test_relative_load(self): loader = DictLoader({ "a/1.html": "{% include '2.html' %}", "a/2.html": "{% include '../b/3.html' %}", "b/3.html": "ok", }) self.assertEqual(loader.load("a/1.html").generate(), b("ok"))
def test_include(self): loader = DictLoader({ "index.html": '{% include "header.html" %}\nbody text', "header.html": "header text", }) self.assertEqual( loader.load("index.html").generate(), b("header text\nbody text"))
def test_unextended_block(self): loader = DictLoader(self.templates) name = "<script>" self.assertEqual(loader.load("escaped_block.html").generate(name=name), b("base: <script>")) self.assertEqual(loader.load("unescaped_block.html").generate(name=name), b("base: <script>"))
def test_unextended_block(self): loader = DictLoader(self.templates) name = "<script>" self.assertEqual( loader.load("escaped_block.html").generate(name=name), b("base: <script>")) self.assertEqual( loader.load("unescaped_block.html").generate(name=name), b("base: <script>"))
def test_extends(self): loader = DictLoader({ "base.html": """\ <title>{% block title %}default title{% end %}</title> <body>{% block body %}default body{% end %}</body> """, "page.html": """\ {% extends "base.html" %} {% block title %}page title{% end %} {% block body %}page body{% end %} """, }) self.assertEqual(loader.load("page.html").generate(), b("<title>page title</title>\n<body>page body</body>\n"))
def test_default_on(self): loader = DictLoader(self.templates, autoescape="xhtml_escape") name = "Bobby <table>s" self.assertEqual(loader.load("escaped.html").generate(name=name), b("Bobby <table>s")) self.assertEqual(loader.load("unescaped.html").generate(name=name), b("Bobby <table>s")) self.assertEqual(loader.load("default.html").generate(name=name), b("Bobby <table>s")) self.assertEqual(loader.load("include.html").generate(name=name), b("escaped: Bobby <table>s\n" "unescaped: Bobby <table>s\n" "default: Bobby <table>s\n"))
def get_app(self): loader = DictLoader({ "linkify.html": "{% module linkify(message) %}", "page.html": """\ <html><head></head><body> {% for e in entries %} {% module Template("entry.html", entry=e) %} {% end %} </body></html>""", "entry.html": """\ {{ set_resources(embedded_css=".entry { margin-bottom: 1em; }", embedded_javascript="js_embed()", css_files=["/base.css", "/foo.css"], javascript_files="/common.js", html_head="<meta>", html_body='<script src="/analytics.js"/>') }} <div class="entry">...</div>""", }) urls = [ url("/typecheck/(.*)", TypeCheckHandler, name='typecheck'), url("/decode_arg/(.*)", DecodeArgHandler), url("/decode_arg_kw/(?P<arg>.*)", DecodeArgHandler), url("/linkify", LinkifyHandler), url("/uimodule_resources", UIModuleResourceHandler), url("/optional_path/(.+)?", OptionalPathHandler), url("/flow_control", FlowControlHandler), url("/multi_header", MultiHeaderHandler), ] return Application(urls, template_loader=loader, autoescape="xhtml_escape")
def test_extends(self): loader = DictLoader({ "base.html": """\ <title>{% block title %}default title{% end %}</title> <body>{% block body %}default body{% end %}</body> """, "page.html": """\ {% extends "base.html" %} {% block title %}page title{% end %} {% block body %}page body{% end %} """, }) self.assertEqual( loader.load("page.html").generate(), b("<title>page title</title>\n<body>page body</body>\n"))
def test_raw_expression(self): loader = DictLoader(self.templates) def render(name): return loader.load(name).generate(name='<>&"') self.assertEqual(render("raw_expression.html"), b("expr: <>&"\n" "raw: <>&\""))
def test_extended_block(self): loader = DictLoader(self.templates) def render(name): return loader.load(name).generate(name="<script>") self.assertEqual(render("escaped_extends_unescaped.html"), b("base: <script>")) self.assertEqual(render("escaped_overrides_unescaped.html"), b("extended: <script>")) self.assertEqual(render("unescaped_extends_escaped.html"), b("base: <script>")) self.assertEqual(render("unescaped_overrides_escaped.html"), b("extended: <script>"))
def test_custom_escape(self): loader = DictLoader( {"foo.py": "{% autoescape py_escape %}s = {{ name }}\n"}) def py_escape(s): self.assertEqual(type(s), bytes_type) return repr(native_str(s)) def render(template, name): return loader.load(template).generate(py_escape=py_escape, name=name) self.assertEqual(render("foo.py", "<html>"), b("s = '<html>'\n")) self.assertEqual(render("foo.py", "';sys.exit()"), b("""s = "';sys.exit()"\n""")) self.assertEqual(render("foo.py", ["not a string"]), b("""s = "['not a string']"\n"""))
def test_default_on(self): loader = DictLoader(self.templates, autoescape="xhtml_escape") name = "Bobby <table>s" self.assertEqual( loader.load("escaped.html").generate(name=name), b("Bobby <table>s")) self.assertEqual( loader.load("unescaped.html").generate(name=name), b("Bobby <table>s")) self.assertEqual( loader.load("default.html").generate(name=name), b("Bobby <table>s")) self.assertEqual( loader.load("include.html").generate(name=name), b("escaped: Bobby <table>s\n" "unescaped: Bobby <table>s\n" "default: Bobby <table>s\n"))
def test_custom_namespace(self): loader = DictLoader({"test.html": "{{ inc(5) }}"}, namespace={"inc": lambda x: x + 1}) self.assertEqual(loader.load("test.html").generate(), b("6"))
def test_custom_namespace(self): loader = DictLoader({"test.html": "{{ inc(5) }}"}, namespace={"inc": lambda x: x+1}) self.assertEqual(loader.load("test.html").generate(), b("6"))