comparison genshi/template/tests/loader.py @ 703:af57b12e3dd2 experimental-match-fastpaths

merge in trunk up through r818 - fundamentally changed the way MatchSet works, but actually is more consistent now
author aflett
date Mon, 31 Mar 2008 22:47:50 +0000
parents 9f11c745fac9
children d143dd73789b
comparison
equal deleted inserted replaced
701:52a597419c0d 703:af57b12e3dd2
102 tmpl = loader.load('sub/tmpl2.html') 102 tmpl = loader.load('sub/tmpl2.html')
103 self.assertEqual("""<html> 103 self.assertEqual("""<html>
104 <div>Included</div> 104 <div>Included</div>
105 </html>""", tmpl.generate().render()) 105 </html>""", tmpl.generate().render())
106 106
107 def test_relative_include_samesubdir(self):
108 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
109 try:
110 file1.write("""<div>Included tmpl1.html</div>""")
111 finally:
112 file1.close()
113
114 os.mkdir(os.path.join(self.dirname, 'sub'))
115 file2 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w')
116 try:
117 file2.write("""<div>Included sub/tmpl1.html</div>""")
118 finally:
119 file2.close()
120
121 file3 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
122 try:
123 file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
124 <xi:include href="tmpl1.html" />
125 </html>""")
126 finally:
127 file3.close()
128
129 loader = TemplateLoader([self.dirname])
130 tmpl = loader.load('sub/tmpl2.html')
131 self.assertEqual("""<html>
132 <div>Included sub/tmpl1.html</div>
133 </html>""", tmpl.generate().render())
134
107 def test_relative_include_without_search_path(self): 135 def test_relative_include_without_search_path(self):
108 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w') 136 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
109 try: 137 try:
110 file1.write("""<div>Included</div>""") 138 file1.write("""<div>Included</div>""")
111 finally: 139 finally:
169 </html>""", filename='subdir/tmpl2.html', loader=loader) 197 </html>""", filename='subdir/tmpl2.html', loader=loader)
170 198
171 self.assertEqual("""<html> 199 self.assertEqual("""<html>
172 <div>Included</div> 200 <div>Included</div>
173 </html>""", tmpl2.generate().render()) 201 </html>""", tmpl2.generate().render())
202
203 def test_relative_absolute_template_preferred(self):
204 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
205 try:
206 file1.write("""<div>Included</div>""")
207 finally:
208 file1.close()
209
210 os.mkdir(os.path.join(self.dirname, 'sub'))
211 file2 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w')
212 try:
213 file2.write("""<div>Included from sub</div>""")
214 finally:
215 file2.close()
216
217 file3 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
218 try:
219 file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
220 <xi:include href="tmpl1.html" />
221 </html>""")
222 finally:
223 file3.close()
224
225 loader = TemplateLoader()
226 tmpl = loader.load(os.path.abspath(os.path.join(self.dirname, 'sub',
227 'tmpl2.html')))
228 self.assertEqual("""<html>
229 <div>Included from sub</div>
230 </html>""", tmpl.generate().render())
231
232 def test_abspath_caching(self):
233 abspath = os.path.join(self.dirname, 'abs')
234 os.mkdir(abspath)
235 file1 = open(os.path.join(abspath, 'tmpl1.html'), 'w')
236 try:
237 file1.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
238 <xi:include href="tmpl2.html" />
239 </html>""")
240 finally:
241 file1.close()
242
243 file2 = open(os.path.join(abspath, 'tmpl2.html'), 'w')
244 try:
245 file2.write("""<div>Included from abspath.</div>""")
246 finally:
247 file2.close()
248
249 searchpath = os.path.join(self.dirname, 'searchpath')
250 os.mkdir(searchpath)
251 file3 = open(os.path.join(searchpath, 'tmpl2.html'), 'w')
252 try:
253 file3.write("""<div>Included from searchpath.</div>""")
254 finally:
255 file3.close()
256
257 loader = TemplateLoader(searchpath)
258 tmpl1 = loader.load(os.path.join(abspath, 'tmpl1.html'))
259 self.assertEqual("""<html>
260 <div>Included from searchpath.</div>
261 </html>""", tmpl1.generate().render())
262 assert 'tmpl2.html' in loader._cache
174 263
175 def test_load_with_default_encoding(self): 264 def test_load_with_default_encoding(self):
176 f = open(os.path.join(self.dirname, 'tmpl.html'), 'w') 265 f = open(os.path.join(self.dirname, 'tmpl.html'), 'w')
177 try: 266 try:
178 f.write(u'<div>\xf6</div>'.encode('iso-8859-1')) 267 f.write(u'<div>\xf6</div>'.encode('iso-8859-1'))
217 tmpl = loader.load('tmpl.html') 306 tmpl = loader.load('tmpl.html')
218 self.assertEqual("""<html> 307 self.assertEqual("""<html>
219 <p>Hello, hello</p> 308 <p>Hello, hello</p>
220 </html>""", tmpl.generate().render()) 309 </html>""", tmpl.generate().render())
221 310
311 def test_prefix_delegation_to_directories(self):
312 """
313 Test prefix delegation with the following layout:
314
315 templates/foo.html
316 sub1/templates/tmpl1.html
317 sub2/templates/tmpl2.html
318
319 Where sub1 and sub2 are prefixes, and both tmpl1.html and tmpl2.html
320 incldue foo.html.
321 """
322 dir1 = os.path.join(self.dirname, 'templates')
323 os.mkdir(dir1)
324 file1 = open(os.path.join(dir1, 'foo.html'), 'w')
325 try:
326 file1.write("""<div>Included foo</div>""")
327 finally:
328 file1.close()
329
330 dir2 = os.path.join(self.dirname, 'sub1', 'templates')
331 os.makedirs(dir2)
332 file2 = open(os.path.join(dir2, 'tmpl1.html'), 'w')
333 try:
334 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
335 <xi:include href="../foo.html" /> from sub1
336 </html>""")
337 finally:
338 file2.close()
339
340 dir3 = os.path.join(self.dirname, 'sub2', 'templates')
341 os.makedirs(dir3)
342 file3 = open(os.path.join(dir3, 'tmpl2.html'), 'w')
343 try:
344 file3.write("""<div>tmpl2</div>""")
345 finally:
346 file3.close()
347
348 loader = TemplateLoader([dir1, TemplateLoader.prefixed(
349 sub1 = os.path.join(dir2),
350 sub2 = os.path.join(dir3)
351 )])
352 tmpl = loader.load('sub1/tmpl1.html')
353 self.assertEqual("""<html>
354 <div>Included foo</div> from sub1
355 </html>""", tmpl.generate().render())
356
357 def test_prefix_delegation_to_directories_with_subdirs(self):
358 """
359 Test prefix delegation with the following layout:
360
361 templates/foo.html
362 sub1/templates/tmpl1.html
363 sub1/templates/tmpl2.html
364 sub1/templates/bar/tmpl3.html
365
366 Where sub1 is a prefix, and tmpl1.html includes all the others.
367 """
368 dir1 = os.path.join(self.dirname, 'templates')
369 os.mkdir(dir1)
370 file1 = open(os.path.join(dir1, 'foo.html'), 'w')
371 try:
372 file1.write("""<div>Included foo</div>""")
373 finally:
374 file1.close()
375
376 dir2 = os.path.join(self.dirname, 'sub1', 'templates')
377 os.makedirs(dir2)
378 file2 = open(os.path.join(dir2, 'tmpl1.html'), 'w')
379 try:
380 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
381 <xi:include href="../foo.html" /> from sub1
382 <xi:include href="tmpl2.html" /> from sub1
383 <xi:include href="bar/tmpl3.html" /> from sub1
384 </html>""")
385 finally:
386 file2.close()
387
388 file3 = open(os.path.join(dir2, 'tmpl2.html'), 'w')
389 try:
390 file3.write("""<div>tmpl2</div>""")
391 finally:
392 file3.close()
393
394 dir3 = os.path.join(self.dirname, 'sub1', 'templates', 'bar')
395 os.makedirs(dir3)
396 file4 = open(os.path.join(dir3, 'tmpl3.html'), 'w')
397 try:
398 file4.write("""<div>bar/tmpl3</div>""")
399 finally:
400 file4.close()
401
402 loader = TemplateLoader([dir1, TemplateLoader.prefixed(
403 sub1 = os.path.join(dir2),
404 sub2 = os.path.join(dir3)
405 )])
406 tmpl = loader.load('sub1/tmpl1.html')
407 self.assertEqual("""<html>
408 <div>Included foo</div> from sub1
409 <div>tmpl2</div> from sub1
410 <div>bar/tmpl3</div> from sub1
411 </html>""", tmpl.generate().render())
412
222 413
223 def suite(): 414 def suite():
224 suite = unittest.TestSuite() 415 suite = unittest.TestSuite()
225 suite.addTest(doctest.DocTestSuite(TemplateLoader.__module__)) 416 suite.addTest(doctest.DocTestSuite(TemplateLoader.__module__))
226 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test')) 417 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test'))
Copyright (C) 2012-2017 Edgewall Software