]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Test template include/extend operations
authorBen Darnell <ben@bendarnell.com>
Sun, 29 May 2011 23:07:20 +0000 (16:07 -0700)
committerBen Darnell <ben@bendarnell.com>
Sun, 29 May 2011 23:07:20 +0000 (16:07 -0700)
tornado/template.py
tornado/test/template_test.py

index 67b17335422424e8e230ea3ac141d1d8f98343e1..465d210dc0730e119a198c231bb3986428d57db5 100644 (file)
@@ -195,6 +195,22 @@ class Loader(object):
         return self.templates[name]
 
 
+class DictLoader(object):
+    """A template loader that loads from a dictionary."""
+    def __init__(self, dict):
+        self.dict = dict
+        self.templates = {}
+
+    def reset(self):
+        self.templates = {}
+
+    def load(self, name, parent_path=None):
+        if name not in self.templates:
+            self.templates[name] = Template(self.dict[name], name=name,
+                                            loader=self)
+        return self.templates[name]
+
+
 class _Node(object):
     def each_child(self):
         return ()
index 66a31291a001d856ba95aa1c7abd69f90499cf40..d6ebc71ad430bd54006044e0653bfa767257f873 100644 (file)
@@ -1,4 +1,4 @@
-from tornado.template import Template
+from tornado.template import Template, DictLoader
 from tornado.testing import LogTrapTestCase
 
 class TemplateTest(LogTrapTestCase):
@@ -6,3 +6,26 @@ class TemplateTest(LogTrapTestCase):
         template = Template("Hello {{ name }}!")
         self.assertEqual(template.generate(name="Ben"),
                          "Hello Ben!")
+
+    def test_include(self):
+        loader = DictLoader({
+                "index.html": '{% include "header.html" %}\nbody text',
+                "header.html": "header text",
+                })
+        self.assertEqual(loader.load("index.html").generate(),
+                         "header text\nbody text")
+
+    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(),
+                         "<title>page title</title>\n<body>page body</body>\n")