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_error_line_number_directive(self): loader = DictLoader({"test.html": """one two{%if 1/0%} three{%end%} """}) try: loader.load("test.html").generate() except ZeroDivisionError: self.assertTrue("# test.html:2" in traceback.format_exc())
def test_error_line_number_expression(self): loader = DictLoader({"test.html": """one two{{1/0}} three """}) try: loader.load("test.html").generate() except ZeroDivisionError: self.assertTrue("# test.html:2" in traceback.format_exc())
def test_error_line_number_extends_base_error(self): loader = DictLoader({ "base.html": "{{1/0}}", "sub.html": "{% extends 'base.html' %}", }) try: loader.load("sub.html").generate() except ZeroDivisionError: exc_stack = traceback.format_exc() self.assertTrue("# base.html:1" in exc_stack)
def test_error_line_number_include(self): loader = DictLoader({ "base.html": "{% include 'sub.html' %}", "sub.html": "{{1/0}}", }) try: loader.load("base.html").generate() except ZeroDivisionError: self.assertTrue("# sub.html:1 (via base.html:1)" in traceback.format_exc())
def test_multi_includes(self): loader = DictLoader({ "a.html": "{% include 'b.html' %}", "b.html": "{% include 'c.html' %}", "c.html": "{{1/0}}", }) try: loader.load("a.html").generate() except ZeroDivisionError: self.assertTrue("# c.html:1 (via b.html:1, a.html:1)" in traceback.format_exc())
def test_error_line_number_module(self): loader = DictLoader({ "base.html": "{% module Template('sub.html') %}", "sub.html": "{{1/0}}", }, namespace={"_tt_modules": ObjectDict({"Template": lambda path, **kwargs: loader.load(path).generate(**kwargs)})}) try: loader.load("base.html").generate() except ZeroDivisionError: exc_stack = traceback.format_exc() self.assertTrue('# base.html:1' in exc_stack) self.assertTrue('# sub.html:1' in exc_stack)
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" b"unescaped: Bobby <table>s\n" b"default: Bobby <table>s\n")
def test_error_line_number_extends_sub_error(self): loader = DictLoader({ "base.html": "{% block 'block' %}{% end %}", "sub.html": """ {% extends 'base.html' %} {% block 'block' %} {{1/0}} {% end %} """}) try: loader.load("sub.html").generate() except ZeroDivisionError: self.assertTrue("# sub.html:4 (via base.html:1)" in traceback.format_exc())
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_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_custom_namespace(self): loader = DictLoader({"test.html": "{{ inc(5) }}"}, namespace={"inc": lambda x: x + 1}) self.assertEqual(loader.load("test.html").generate(), b"6")