changeset 152:cdb2a1f930e2 trunk

Add some tests for relative template includes (see #27).
author cmlenz
date Wed, 16 Aug 2006 10:25:15 +0000
parents fb71efbd6ad4
children fc2ff46d1ae3
files markup/template.py markup/tests/template.py
diffstat 2 files changed, 84 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/markup/template.py
+++ b/markup/template.py
@@ -1006,6 +1006,8 @@
     
     >>> loader.load(os.path.basename(path)) is template
     True
+    
+    >>> os.remove(path)
     """
     def __init__(self, search_path=None, auto_reload=False):
         """Create the template laoder.
--- a/markup/tests/template.py
+++ b/markup/tests/template.py
@@ -12,11 +12,15 @@
 # history and logs, available at http://markup.edgewall.org/log/.
 
 import doctest
+import os
 import unittest
+import shutil
 import sys
+import tempfile
 
 from markup.core import Markup, Stream
-from markup.template import BadDirectiveError, Template, TemplateSyntaxError
+from markup.template import BadDirectiveError, Template, TemplateLoader, \
+                            TemplateSyntaxError
 
 
 class AttrsDirectiveTestCase(unittest.TestCase):
@@ -606,10 +610,84 @@
         </div>""", str(tmpl.generate()))
 
 
+class TemplateLoaderTestCase(unittest.TestCase):
+    """Tests for the template loader."""
+
+    def setUp(self):
+        self.dirname = tempfile.mkdtemp(suffix='markup_test')
+
+    def tearDown(self):
+        shutil.rmtree(self.dirname)
+
+    def test_relative_include_samedir(self):
+        file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
+        try:
+            file1.write("""<div>Included</div>""")
+        finally:
+            file1.close()
+
+        file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
+        try:
+            file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+  <xi:include href="tmpl1.html" />
+</html>""")
+        finally:
+            file2.close()
+
+        loader = TemplateLoader([self.dirname])
+        tmpl = loader.load('tmpl2.html')
+        self.assertEqual("""<html>
+  <div>Included</div>
+</html>""", tmpl.generate().render())
+
+    def test_relative_include_subdir(self):
+        os.mkdir(os.path.join(self.dirname, 'sub'))
+        file1 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w')
+        try:
+            file1.write("""<div>Included</div>""")
+        finally:
+            file1.close()
+
+        file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
+        try:
+            file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+  <xi:include href="sub/tmpl1.html" />
+</html>""")
+        finally:
+            file2.close()
+
+        loader = TemplateLoader([self.dirname])
+        tmpl = loader.load('tmpl2.html')
+        self.assertEqual("""<html>
+  <div>Included</div>
+</html>""", tmpl.generate().render())
+
+    def test_relative_include_parentdir(self):
+        file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
+        try:
+            file1.write("""<div>Included</div>""")
+        finally:
+            file1.close()
+
+        os.mkdir(os.path.join(self.dirname, 'sub'))
+        file2 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
+        try:
+            file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+  <xi:include href="../tmpl1.html" />
+</html>""")
+        finally:
+            file2.close()
+
+        loader = TemplateLoader([self.dirname])
+        tmpl = loader.load('sub/tmpl2.html')
+        self.assertEqual("""<html>
+  <div>Included</div>
+</html>""", tmpl.generate().render())
+
+
 def suite():
     suite = unittest.TestSuite()
     suite.addTest(doctest.DocTestSuite(Template.__module__))
-    suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
     suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test'))
     suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test'))
     suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test'))
@@ -618,6 +696,8 @@
     suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
     suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
     suite.addTest(unittest.makeSuite(WithDirectiveTestCase, 'test'))
+    suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
+    suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test'))
     return suite
 
 if __name__ == '__main__':
Copyright (C) 2012-2017 Edgewall Software