diff genshi/template/tests/loader.py @ 720:acf7c5ee36e7 experimental-newctxt

newctxt branch: Merged revisions [678:835] via svnmerge from [source:trunk].
author cmlenz
date Fri, 11 Apr 2008 08:42:11 +0000
parents 9f11c745fac9
children
line wrap: on
line diff
--- a/genshi/template/tests/loader.py
+++ b/genshi/template/tests/loader.py
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2006 Edgewall Software
+# Copyright (C) 2006-2008 Edgewall Software
 # All rights reserved.
 #
 # This software is licensed as described in the file COPYING, which
@@ -104,6 +104,34 @@
               <div>Included</div>
             </html>""", tmpl.generate().render())
 
+    def test_relative_include_samesubdir(self):
+        file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
+        try:
+            file1.write("""<div>Included tmpl1.html</div>""")
+        finally:
+            file1.close()
+
+        os.mkdir(os.path.join(self.dirname, 'sub'))
+        file2 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w')
+        try:
+            file2.write("""<div>Included sub/tmpl1.html</div>""")
+        finally:
+            file2.close()
+
+        file3 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
+        try:
+            file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="tmpl1.html" />
+            </html>""")
+        finally:
+            file3.close()
+
+        loader = TemplateLoader([self.dirname])
+        tmpl = loader.load('sub/tmpl2.html')
+        self.assertEqual("""<html>
+              <div>Included sub/tmpl1.html</div>
+            </html>""", tmpl.generate().render())
+
     def test_relative_include_without_search_path(self):
         file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
         try:
@@ -172,6 +200,67 @@
           <div>Included</div>
         </html>""", tmpl2.generate().render())
 
+    def test_relative_absolute_template_preferred(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', 'tmpl1.html'), 'w')
+        try:
+            file2.write("""<div>Included from sub</div>""")
+        finally:
+            file2.close()
+
+        file3 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
+        try:
+            file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="tmpl1.html" />
+            </html>""")
+        finally:
+            file3.close()
+
+        loader = TemplateLoader()
+        tmpl = loader.load(os.path.abspath(os.path.join(self.dirname, 'sub',
+                                                        'tmpl2.html')))
+        self.assertEqual("""<html>
+              <div>Included from sub</div>
+            </html>""", tmpl.generate().render())
+
+    def test_abspath_caching(self):
+        abspath = os.path.join(self.dirname, 'abs')
+        os.mkdir(abspath)
+        file1 = open(os.path.join(abspath, 'tmpl1.html'), 'w')
+        try:
+            file1.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="tmpl2.html" />
+            </html>""")
+        finally:
+            file1.close()
+
+        file2 = open(os.path.join(abspath, 'tmpl2.html'), 'w')
+        try:
+            file2.write("""<div>Included from abspath.</div>""")
+        finally:
+            file2.close()
+
+        searchpath = os.path.join(self.dirname, 'searchpath')
+        os.mkdir(searchpath)
+        file3 = open(os.path.join(searchpath, 'tmpl2.html'), 'w')
+        try:
+            file3.write("""<div>Included from searchpath.</div>""")
+        finally:
+            file3.close()
+
+        loader = TemplateLoader(searchpath)
+        tmpl1 = loader.load(os.path.join(abspath, 'tmpl1.html'))
+        self.assertEqual("""<html>
+              <div>Included from searchpath.</div>
+            </html>""", tmpl1.generate().render())
+        assert 'tmpl2.html' in loader._cache
+
     def test_load_with_default_encoding(self):
         f = open(os.path.join(self.dirname, 'tmpl.html'), 'w')
         try:
@@ -219,6 +308,108 @@
               <p>Hello, hello</p>
             </html>""", tmpl.generate().render())
 
+    def test_prefix_delegation_to_directories(self):
+        """
+        Test prefix delegation with the following layout:
+        
+        templates/foo.html
+        sub1/templates/tmpl1.html
+        sub2/templates/tmpl2.html
+        
+        Where sub1 and sub2 are prefixes, and both tmpl1.html and tmpl2.html
+        incldue foo.html.
+        """
+        dir1 = os.path.join(self.dirname, 'templates')
+        os.mkdir(dir1)
+        file1 = open(os.path.join(dir1, 'foo.html'), 'w')
+        try:
+            file1.write("""<div>Included foo</div>""")
+        finally:
+            file1.close()
+
+        dir2 = os.path.join(self.dirname, 'sub1', 'templates')
+        os.makedirs(dir2)
+        file2 = open(os.path.join(dir2, 'tmpl1.html'), 'w')
+        try:
+            file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="../foo.html" /> from sub1
+            </html>""")
+        finally:
+            file2.close()
+
+        dir3 = os.path.join(self.dirname, 'sub2', 'templates')
+        os.makedirs(dir3)
+        file3 = open(os.path.join(dir3, 'tmpl2.html'), 'w')
+        try:
+            file3.write("""<div>tmpl2</div>""")
+        finally:
+            file3.close()
+
+        loader = TemplateLoader([dir1, TemplateLoader.prefixed(
+            sub1 = dir2,
+            sub2 = dir3
+        )])
+        tmpl = loader.load('sub1/tmpl1.html')
+        self.assertEqual("""<html>
+              <div>Included foo</div> from sub1
+            </html>""", tmpl.generate().render())
+
+    def test_prefix_delegation_to_directories_with_subdirs(self):
+        """
+        Test prefix delegation with the following layout:
+        
+        templates/foo.html
+        sub1/templates/tmpl1.html
+        sub1/templates/tmpl2.html
+        sub1/templates/bar/tmpl3.html
+        
+        Where sub1 is a prefix, and tmpl1.html includes all the others.
+        """
+        dir1 = os.path.join(self.dirname, 'templates')
+        os.mkdir(dir1)
+        file1 = open(os.path.join(dir1, 'foo.html'), 'w')
+        try:
+            file1.write("""<div>Included foo</div>""")
+        finally:
+            file1.close()
+
+        dir2 = os.path.join(self.dirname, 'sub1', 'templates')
+        os.makedirs(dir2)
+        file2 = open(os.path.join(dir2, 'tmpl1.html'), 'w')
+        try:
+            file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="../foo.html" /> from sub1
+              <xi:include href="tmpl2.html" /> from sub1
+              <xi:include href="bar/tmpl3.html" /> from sub1
+            </html>""")
+        finally:
+            file2.close()
+
+        file3 = open(os.path.join(dir2, 'tmpl2.html'), 'w')
+        try:
+            file3.write("""<div>tmpl2</div>""")
+        finally:
+            file3.close()
+
+        dir3 = os.path.join(self.dirname, 'sub1', 'templates', 'bar')
+        os.makedirs(dir3)
+        file4 = open(os.path.join(dir3, 'tmpl3.html'), 'w')
+        try:
+            file4.write("""<div>bar/tmpl3</div>""")
+        finally:
+            file4.close()
+
+        loader = TemplateLoader([dir1, TemplateLoader.prefixed(
+            sub1 = os.path.join(dir2),
+            sub2 = os.path.join(dir3)
+        )])
+        tmpl = loader.load('sub1/tmpl1.html')
+        self.assertEqual("""<html>
+              <div>Included foo</div> from sub1
+              <div>tmpl2</div> from sub1
+              <div>bar/tmpl3</div> from sub1
+            </html>""", tmpl.generate().render())
+
 
 def suite():
     suite = unittest.TestSuite()
Copyright (C) 2012-2017 Edgewall Software