annotate genshi/template/tests/text.py @ 500:0742f421caba experimental-inline

Merged revisions 487-603 via svnmerge from http://svn.edgewall.org/repos/genshi/trunk
author cmlenz
date Fri, 01 Jun 2007 17:21:47 +0000
parents 55cf81951686
children 1837f39efd6f
rev   line source
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
2 #
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
3 # Copyright (C) 2006 Edgewall Software
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
4 # All rights reserved.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
5 #
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
6 # This software is licensed as described in the file COPYING, which
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
7 # you should have received as part of this distribution. The terms
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
8 # are also available at http://genshi.edgewall.org/wiki/License.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
9 #
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
10 # This software consists of voluntary contributions made by many
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
11 # individuals. For the exact contribution history, see the revision
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
12 # history and logs, available at http://genshi.edgewall.org/log/.
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
13
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
14 import doctest
500
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
15 import os
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
16 import shutil
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
17 import tempfile
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
18 import unittest
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
19
500
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
20 from genshi.template.loader import TemplateLoader
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
21 from genshi.template.text import TextTemplate
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
22
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
23
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
24 class TextTemplateTestCase(unittest.TestCase):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
25 """Tests for text template processing."""
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
26
500
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
27 def setUp(self):
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
28 self.dirname = tempfile.mkdtemp(suffix='markup_test')
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
29
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
30 def tearDown(self):
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
31 shutil.rmtree(self.dirname)
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
32
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
33 def test_escaping(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
34 tmpl = TextTemplate('\\#escaped')
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
35 self.assertEqual('#escaped', str(tmpl.generate()))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
36
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
37 def test_comment(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
38 tmpl = TextTemplate('## a comment')
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
39 self.assertEqual('', str(tmpl.generate()))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
40
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
41 def test_comment_escaping(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
42 tmpl = TextTemplate('\\## escaped comment')
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
43 self.assertEqual('## escaped comment', str(tmpl.generate()))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
44
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
45 def test_end_with_args(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
46 tmpl = TextTemplate("""
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
47 #if foo
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
48 bar
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
49 #end 'if foo'""")
500
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
50 self.assertEqual('\n', str(tmpl.generate(foo=False)))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
51
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
52 def test_latin1_encoded(self):
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
53 text = u'$foo\xf6$bar'.encode('iso-8859-1')
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
54 tmpl = TextTemplate(text, encoding='iso-8859-1')
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
55 self.assertEqual(u'x\xf6y', unicode(tmpl.generate(foo='x', bar='y')))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
56
395
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
57 def test_empty_lines1(self):
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
58 tmpl = TextTemplate("""Your items:
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
59
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
60 #for item in items
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
61 * ${item}
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
62 #end""")
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
63 self.assertEqual("""Your items:
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
64
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
65 * 0
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
66 * 1
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
67 * 2
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
68 """, tmpl.generate(items=range(3)).render('text'))
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
69
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
70 def test_empty_lines2(self):
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
71 tmpl = TextTemplate("""Your items:
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
72
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
73 #for item in items
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
74 * ${item}
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
75
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
76 #end""")
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
77 self.assertEqual("""Your items:
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
78
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
79 * 0
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
80
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
81 * 1
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
82
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
83 * 2
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
84
55cf81951686 inline branch: Merged [439:479/trunk].
cmlenz
parents: 336
diff changeset
85 """, tmpl.generate(items=range(3)).render('text'))
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
86
500
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
87 def test_include(self):
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
88 file1 = open(os.path.join(self.dirname, 'tmpl1.txt'), 'w')
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
89 try:
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
90 file1.write("Included\n")
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
91 finally:
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
92 file1.close()
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
93
500
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
94 file2 = open(os.path.join(self.dirname, 'tmpl2.txt'), 'w')
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
95 try:
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
96 file2.write("""----- Included data below this line -----
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
97 #include tmpl1.txt
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
98 ----- Included data above this line -----""")
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
99 finally:
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
100 file2.close()
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
101
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
102 loader = TemplateLoader([self.dirname])
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
103 tmpl = loader.load('tmpl2.txt', cls=TextTemplate)
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
104 self.assertEqual("""----- Included data below this line -----
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
105 Included
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
106 ----- Included data above this line -----""",
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
107 tmpl.generate().render())
0742f421caba Merged revisions 487-603 via svnmerge from
cmlenz
parents: 395
diff changeset
108
336
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
109 def suite():
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
110 suite = unittest.TestSuite()
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
111 suite.addTest(doctest.DocTestSuite(TextTemplate.__module__))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
112 suite.addTest(unittest.makeSuite(TextTemplateTestCase, 'test'))
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
113 return suite
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
114
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
115 if __name__ == '__main__':
5f2c7782cd8a Refactoring: `genshi.template` is now a package, it was getting way to crowded in that file.
cmlenz
parents:
diff changeset
116 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software