Mercurial > genshi > mirror
comparison genshi/template/tests/loader.py @ 698:408ab81767c7 trunk
Fix for prefix-dispatched template loading. Closes #206. Thanks to Waldemar Kornewald for the patch.
author | cmlenz |
---|---|
date | Thu, 27 Mar 2008 14:45:11 +0000 |
parents | 66eead58c120 |
children | cfe3b4f02d77 |
comparison
equal
deleted
inserted
replaced
697:3d3c322ca978 | 698:408ab81767c7 |
---|---|
275 self.assertEqual("""<html> | 275 self.assertEqual("""<html> |
276 <p>Hello, hello</p> | 276 <p>Hello, hello</p> |
277 </html>""", tmpl.generate().render()) | 277 </html>""", tmpl.generate().render()) |
278 | 278 |
279 def test_prefix_delegation_to_directories(self): | 279 def test_prefix_delegation_to_directories(self): |
280 """ | |
281 Test prefix delegation with the following layout: | |
282 | |
283 templates/foo.html | |
284 sub1/templates/tmpl1.html | |
285 sub2/templates/tmpl2.html | |
286 | |
287 Where sub1 and sub2 are prefixes, and both tmpl1.html and tmpl2.html | |
288 incldue foo.html. | |
289 """ | |
280 dir1 = os.path.join(self.dirname, 'templates') | 290 dir1 = os.path.join(self.dirname, 'templates') |
281 os.mkdir(dir1) | 291 os.mkdir(dir1) |
282 file1 = open(os.path.join(dir1, 'foo.html'), 'w') | 292 file1 = open(os.path.join(dir1, 'foo.html'), 'w') |
283 try: | 293 try: |
284 file1.write("""<div>Included foo</div>""") | 294 file1.write("""<div>Included foo</div>""") |
288 dir2 = os.path.join(self.dirname, 'sub1', 'templates') | 298 dir2 = os.path.join(self.dirname, 'sub1', 'templates') |
289 os.makedirs(dir2) | 299 os.makedirs(dir2) |
290 file2 = open(os.path.join(dir2, 'tmpl1.html'), 'w') | 300 file2 = open(os.path.join(dir2, 'tmpl1.html'), 'w') |
291 try: | 301 try: |
292 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> | 302 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> |
293 <xi:include href="foo.html" /> from sub1 | 303 <xi:include href="../foo.html" /> from sub1 |
294 </html>""") | 304 </html>""") |
295 finally: | 305 finally: |
296 file2.close() | 306 file2.close() |
297 | 307 |
298 dir3 = os.path.join(self.dirname, 'sub2', 'templates') | 308 dir3 = os.path.join(self.dirname, 'sub2', 'templates') |
299 os.makedirs(dir3) | 309 os.makedirs(dir3) |
300 file3 = open(os.path.join(dir3, 'tmpl2.html'), 'w') | 310 file3 = open(os.path.join(dir3, 'tmpl2.html'), 'w') |
301 try: | 311 try: |
302 file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> | 312 file3.write("""<div>tmpl2</div>""") |
303 <xi:include href="foo.html" /> from sub2 | |
304 </html>""") | |
305 finally: | 313 finally: |
306 file3.close() | 314 file3.close() |
307 | 315 |
308 loader = TemplateLoader([dir1, TemplateLoader.prefixed( | 316 loader = TemplateLoader([dir1, TemplateLoader.prefixed( |
309 sub1 = os.path.join(dir2), | 317 sub1 = os.path.join(dir2), |
312 tmpl = loader.load('sub1/tmpl1.html') | 320 tmpl = loader.load('sub1/tmpl1.html') |
313 self.assertEqual("""<html> | 321 self.assertEqual("""<html> |
314 <div>Included foo</div> from sub1 | 322 <div>Included foo</div> from sub1 |
315 </html>""", tmpl.generate().render()) | 323 </html>""", tmpl.generate().render()) |
316 | 324 |
325 def test_prefix_delegation_to_directories_with_subdirs(self): | |
326 """ | |
327 Test prefix delegation with the following layout: | |
328 | |
329 templates/foo.html | |
330 sub1/templates/tmpl1.html | |
331 sub1/templates/tmpl2.html | |
332 sub1/templates/bar/tmpl3.html | |
333 | |
334 Where sub1 is a prefix, and tmpl1.html includes all the others. | |
335 """ | |
336 dir1 = os.path.join(self.dirname, 'templates') | |
337 os.mkdir(dir1) | |
338 file1 = open(os.path.join(dir1, 'foo.html'), 'w') | |
339 try: | |
340 file1.write("""<div>Included foo</div>""") | |
341 finally: | |
342 file1.close() | |
343 | |
344 dir2 = os.path.join(self.dirname, 'sub1', 'templates') | |
345 os.makedirs(dir2) | |
346 file2 = open(os.path.join(dir2, 'tmpl1.html'), 'w') | |
347 try: | |
348 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> | |
349 <xi:include href="../foo.html" /> from sub1 | |
350 <xi:include href="tmpl2.html" /> from sub1 | |
351 <xi:include href="bar/tmpl3.html" /> from sub1 | |
352 </html>""") | |
353 finally: | |
354 file2.close() | |
355 | |
356 file3 = open(os.path.join(dir2, 'tmpl2.html'), 'w') | |
357 try: | |
358 file3.write("""<div>tmpl2</div>""") | |
359 finally: | |
360 file3.close() | |
361 | |
362 dir3 = os.path.join(self.dirname, 'sub1', 'templates', 'bar') | |
363 os.makedirs(dir3) | |
364 file4 = open(os.path.join(dir3, 'tmpl3.html'), 'w') | |
365 try: | |
366 file4.write("""<div>bar/tmpl3</div>""") | |
367 finally: | |
368 file4.close() | |
369 | |
370 loader = TemplateLoader([dir1, TemplateLoader.prefixed( | |
371 sub1 = os.path.join(dir2), | |
372 sub2 = os.path.join(dir3) | |
373 )]) | |
374 tmpl = loader.load('sub1/tmpl1.html') | |
375 self.assertEqual("""<html> | |
376 <div>Included foo</div> from sub1 | |
377 <div>tmpl2</div> from sub1 | |
378 <div>bar/tmpl3</div> from sub1 | |
379 </html>""", tmpl.generate().render()) | |
380 | |
317 | 381 |
318 def suite(): | 382 def suite(): |
319 suite = unittest.TestSuite() | 383 suite = unittest.TestSuite() |
320 suite.addTest(doctest.DocTestSuite(TemplateLoader.__module__)) | 384 suite.addTest(doctest.DocTestSuite(TemplateLoader.__module__)) |
321 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test')) | 385 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test')) |