comparison markup/tests/template.py @ 152:064ba1078f92

Add some tests for relative template includes (see #27).
author cmlenz
date Wed, 16 Aug 2006 10:25:15 +0000
parents 7306bf730ff3
children 1c404be518d1
comparison
equal deleted inserted replaced
151:77779d507d40 152:064ba1078f92
10 # This software consists of voluntary contributions made by many 10 # This software consists of voluntary contributions made by many
11 # individuals. For the exact contribution history, see the revision 11 # individuals. For the exact contribution history, see the revision
12 # history and logs, available at http://markup.edgewall.org/log/. 12 # history and logs, available at http://markup.edgewall.org/log/.
13 13
14 import doctest 14 import doctest
15 import os
15 import unittest 16 import unittest
17 import shutil
16 import sys 18 import sys
19 import tempfile
17 20
18 from markup.core import Markup, Stream 21 from markup.core import Markup, Stream
19 from markup.template import BadDirectiveError, Template, TemplateSyntaxError 22 from markup.template import BadDirectiveError, Template, TemplateLoader, \
23 TemplateSyntaxError
20 24
21 25
22 class AttrsDirectiveTestCase(unittest.TestCase): 26 class AttrsDirectiveTestCase(unittest.TestCase):
23 """Tests for the `py:attrs` template directive.""" 27 """Tests for the `py:attrs` template directive."""
24 28
604 </div>""") 608 </div>""")
605 self.assertEqual("""<div> 609 self.assertEqual("""<div>
606 </div>""", str(tmpl.generate())) 610 </div>""", str(tmpl.generate()))
607 611
608 612
613 class TemplateLoaderTestCase(unittest.TestCase):
614 """Tests for the template loader."""
615
616 def setUp(self):
617 self.dirname = tempfile.mkdtemp(suffix='markup_test')
618
619 def tearDown(self):
620 shutil.rmtree(self.dirname)
621
622 def test_relative_include_samedir(self):
623 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
624 try:
625 file1.write("""<div>Included</div>""")
626 finally:
627 file1.close()
628
629 file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
630 try:
631 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
632 <xi:include href="tmpl1.html" />
633 </html>""")
634 finally:
635 file2.close()
636
637 loader = TemplateLoader([self.dirname])
638 tmpl = loader.load('tmpl2.html')
639 self.assertEqual("""<html>
640 <div>Included</div>
641 </html>""", tmpl.generate().render())
642
643 def test_relative_include_subdir(self):
644 os.mkdir(os.path.join(self.dirname, 'sub'))
645 file1 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w')
646 try:
647 file1.write("""<div>Included</div>""")
648 finally:
649 file1.close()
650
651 file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
652 try:
653 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
654 <xi:include href="sub/tmpl1.html" />
655 </html>""")
656 finally:
657 file2.close()
658
659 loader = TemplateLoader([self.dirname])
660 tmpl = loader.load('tmpl2.html')
661 self.assertEqual("""<html>
662 <div>Included</div>
663 </html>""", tmpl.generate().render())
664
665 def test_relative_include_parentdir(self):
666 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
667 try:
668 file1.write("""<div>Included</div>""")
669 finally:
670 file1.close()
671
672 os.mkdir(os.path.join(self.dirname, 'sub'))
673 file2 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
674 try:
675 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
676 <xi:include href="../tmpl1.html" />
677 </html>""")
678 finally:
679 file2.close()
680
681 loader = TemplateLoader([self.dirname])
682 tmpl = loader.load('sub/tmpl2.html')
683 self.assertEqual("""<html>
684 <div>Included</div>
685 </html>""", tmpl.generate().render())
686
687
609 def suite(): 688 def suite():
610 suite = unittest.TestSuite() 689 suite = unittest.TestSuite()
611 suite.addTest(doctest.DocTestSuite(Template.__module__)) 690 suite.addTest(doctest.DocTestSuite(Template.__module__))
612 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
613 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test')) 691 suite.addTest(unittest.makeSuite(AttrsDirectiveTestCase, 'test'))
614 suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test')) 692 suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test'))
615 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test')) 693 suite.addTest(unittest.makeSuite(DefDirectiveTestCase, 'test'))
616 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test')) 694 suite.addTest(unittest.makeSuite(ForDirectiveTestCase, 'test'))
617 suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test')) 695 suite.addTest(unittest.makeSuite(IfDirectiveTestCase, 'test'))
618 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test')) 696 suite.addTest(unittest.makeSuite(MatchDirectiveTestCase, 'test'))
619 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test')) 697 suite.addTest(unittest.makeSuite(StripDirectiveTestCase, 'test'))
620 suite.addTest(unittest.makeSuite(WithDirectiveTestCase, 'test')) 698 suite.addTest(unittest.makeSuite(WithDirectiveTestCase, 'test'))
699 suite.addTest(unittest.makeSuite(TemplateTestCase, 'test'))
700 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test'))
621 return suite 701 return suite
622 702
623 if __name__ == '__main__': 703 if __name__ == '__main__':
624 unittest.main(defaultTest='suite') 704 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software