Mercurial > genshi > mirror
annotate genshi/template/tests/plugin.py @ 410:d14d89995c29 trunk
Improve the handling of namespaces in serialization.
author | cmlenz |
---|---|
date | Mon, 26 Feb 2007 18:26:59 +0000 |
parents | 85d8e57a23e5 |
children | 1da8de3e5e51 acf7c5ee36e7 |
rev | line source |
---|---|
353 | 1 # -*- coding: utf-8 -*- |
2 # | |
3 # Copyright (C) 2006 Edgewall Software | |
4 # Copyright (C) 2006 Matthew Good | |
5 # All rights reserved. | |
6 # | |
7 # This software is licensed as described in the file COPYING, which | |
8 # you should have received as part of this distribution. The terms | |
9 # are also available at http://genshi.edgewall.org/wiki/License. | |
10 # | |
11 # This software consists of voluntary contributions made by many | |
12 # individuals. For the exact contribution history, see the revision | |
13 # history and logs, available at http://genshi.edgewall.org/log/. | |
14 | |
15 import doctest | |
16 import os | |
17 import unittest | |
18 | |
19 from genshi.core import Stream | |
20 from genshi.output import DocType | |
21 from genshi.template import MarkupTemplate, TextTemplate | |
22 from genshi.template.plugin import ConfigurationError, \ | |
23 MarkupTemplateEnginePlugin, \ | |
24 TextTemplateEnginePlugin | |
25 | |
26 PACKAGE = 'genshi.template.tests' | |
27 | |
28 | |
29 class MarkupTemplateEnginePluginTestCase(unittest.TestCase): | |
30 | |
31 def test_init_no_options(self): | |
32 plugin = MarkupTemplateEnginePlugin() | |
33 self.assertEqual('utf-8', plugin.default_encoding) | |
34 self.assertEqual('html', plugin.default_format) | |
35 self.assertEqual(None, plugin.default_doctype) | |
36 | |
37 self.assertEqual([], plugin.loader.search_path) | |
38 self.assertEqual(True, plugin.loader.auto_reload) | |
39 self.assertEqual(25, plugin.loader._cache.capacity) | |
40 | |
41 def test_init_with_loader_options(self): | |
42 plugin = MarkupTemplateEnginePlugin(options={ | |
43 'genshi.auto_reload': 'off', | |
44 'genshi.max_cache_size': '100', | |
45 'genshi.search_path': '/usr/share/tmpl:/usr/local/share/tmpl', | |
46 }) | |
47 self.assertEqual(['/usr/share/tmpl', '/usr/local/share/tmpl'], | |
48 plugin.loader.search_path) | |
49 self.assertEqual(False, plugin.loader.auto_reload) | |
50 self.assertEqual(100, plugin.loader._cache.capacity) | |
51 | |
52 def test_init_with_invalid_cache_size(self): | |
53 self.assertRaises(ConfigurationError, MarkupTemplateEnginePlugin, | |
54 options={'genshi.max_cache_size': 'thirty'}) | |
55 | |
56 def test_init_with_output_options(self): | |
57 plugin = MarkupTemplateEnginePlugin(options={ | |
58 'genshi.default_encoding': 'iso-8859-15', | |
59 'genshi.default_format': 'xhtml', | |
60 'genshi.default_doctype': 'xhtml-strict', | |
61 }) | |
62 self.assertEqual('iso-8859-15', plugin.default_encoding) | |
63 self.assertEqual('xhtml', plugin.default_format) | |
64 self.assertEqual(DocType.XHTML, plugin.default_doctype) | |
65 | |
66 def test_init_with_invalid_output_format(self): | |
67 self.assertRaises(ConfigurationError, MarkupTemplateEnginePlugin, | |
68 options={'genshi.default_format': 'foobar'}) | |
69 | |
70 def test_init_with_invalid_doctype(self): | |
71 self.assertRaises(ConfigurationError, MarkupTemplateEnginePlugin, | |
72 options={'genshi.default_doctype': 'foobar'}) | |
73 | |
74 def test_load_template_from_file(self): | |
75 plugin = MarkupTemplateEnginePlugin() | |
76 tmpl = plugin.load_template(PACKAGE + '.templates.test') | |
77 self.assertEqual('test.html', os.path.basename(tmpl.filename)) | |
78 assert isinstance(tmpl, MarkupTemplate) | |
79 | |
80 def test_load_template_from_string(self): | |
81 plugin = MarkupTemplateEnginePlugin() | |
82 tmpl = plugin.load_template(None, template_string="""<p> | |
83 $message | |
84 </p>""") | |
85 self.assertEqual(None, tmpl.filename) | |
86 assert isinstance(tmpl, MarkupTemplate) | |
87 | |
88 def test_transform_with_load(self): | |
89 plugin = MarkupTemplateEnginePlugin() | |
90 tmpl = plugin.load_template(PACKAGE + '.templates.test') | |
91 stream = plugin.transform({'message': 'Hello'}, tmpl) | |
92 assert isinstance(stream, Stream) | |
93 | |
94 def test_transform_without_load(self): | |
95 plugin = MarkupTemplateEnginePlugin() | |
96 stream = plugin.transform({'message': 'Hello'}, | |
97 PACKAGE + '.templates.test') | |
98 assert isinstance(stream, Stream) | |
99 | |
100 def test_render(self): | |
101 plugin = MarkupTemplateEnginePlugin() | |
102 tmpl = plugin.load_template(PACKAGE + '.templates.test') | |
103 output = plugin.render({'message': 'Hello'}, template=tmpl) | |
104 self.assertEqual("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
105 <html lang="en"> | |
106 <head> | |
107 <title>Test</title> | |
108 </head> | |
109 <body> | |
110 <h1>Test</h1> | |
111 <p>Hello</p> | |
112 </body> | |
113 </html>""", output) | |
114 | |
115 def test_render_with_format(self): | |
116 plugin = MarkupTemplateEnginePlugin() | |
117 tmpl = plugin.load_template(PACKAGE + '.templates.test') | |
118 output = plugin.render({'message': 'Hello'}, format='xhtml', | |
119 template=tmpl) | |
120 self.assertEqual("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
410
d14d89995c29
Improve the handling of namespaces in serialization.
cmlenz
parents:
353
diff
changeset
|
121 <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> |
353 | 122 <head> |
123 <title>Test</title> | |
124 </head> | |
125 <body> | |
126 <h1>Test</h1> | |
127 <p>Hello</p> | |
128 </body> | |
129 </html>""", output) | |
130 | |
131 def test_render_with_doctype(self): | |
132 plugin = MarkupTemplateEnginePlugin(options={ | |
133 'genshi.default_doctype': 'html-strict', | |
134 }) | |
135 tmpl = plugin.load_template(PACKAGE + '.templates.test') | |
136 output = plugin.render({'message': 'Hello'}, template=tmpl) | |
137 self.assertEqual("""<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
138 <html lang="en"> | |
139 <head> | |
140 <title>Test</title> | |
141 </head> | |
142 <body> | |
143 <h1>Test</h1> | |
144 <p>Hello</p> | |
145 </body> | |
146 </html>""", output) | |
147 | |
148 def test_helper_functions(self): | |
149 plugin = MarkupTemplateEnginePlugin() | |
150 tmpl = plugin.load_template(PACKAGE + '.templates.functions') | |
151 output = plugin.render({'snippet': '<b>Foo</b>'}, template=tmpl) | |
152 self.assertEqual("""<div> | |
153 False | |
154 bar | |
155 <b>Foo</b> | |
156 <b>Foo</b> | |
157 </div>""", output) | |
158 | |
159 | |
160 class TextTemplateEnginePluginTestCase(unittest.TestCase): | |
161 | |
162 def test_init_no_options(self): | |
163 plugin = TextTemplateEnginePlugin() | |
164 self.assertEqual('utf-8', plugin.default_encoding) | |
165 self.assertEqual('text', plugin.default_format) | |
166 | |
167 self.assertEqual([], plugin.loader.search_path) | |
168 self.assertEqual(True, plugin.loader.auto_reload) | |
169 self.assertEqual(25, plugin.loader._cache.capacity) | |
170 | |
171 def test_init_with_loader_options(self): | |
172 plugin = TextTemplateEnginePlugin(options={ | |
173 'genshi.auto_reload': 'off', | |
174 'genshi.max_cache_size': '100', | |
175 'genshi.search_path': '/usr/share/tmpl:/usr/local/share/tmpl', | |
176 }) | |
177 self.assertEqual(['/usr/share/tmpl', '/usr/local/share/tmpl'], | |
178 plugin.loader.search_path) | |
179 self.assertEqual(False, plugin.loader.auto_reload) | |
180 self.assertEqual(100, plugin.loader._cache.capacity) | |
181 | |
182 def test_init_with_output_options(self): | |
183 plugin = TextTemplateEnginePlugin(options={ | |
184 'genshi.default_encoding': 'iso-8859-15', | |
185 }) | |
186 self.assertEqual('iso-8859-15', plugin.default_encoding) | |
187 | |
188 def test_load_template_from_file(self): | |
189 plugin = TextTemplateEnginePlugin() | |
190 tmpl = plugin.load_template(PACKAGE + '.templates.test') | |
191 assert isinstance(tmpl, TextTemplate) | |
192 self.assertEqual('test.txt', os.path.basename(tmpl.filename)) | |
193 | |
194 def test_load_template_from_string(self): | |
195 plugin = TextTemplateEnginePlugin() | |
196 tmpl = plugin.load_template(None, template_string="$message") | |
197 self.assertEqual(None, tmpl.filename) | |
198 assert isinstance(tmpl, TextTemplate) | |
199 | |
200 def test_transform_without_load(self): | |
201 plugin = TextTemplateEnginePlugin() | |
202 stream = plugin.transform({'message': 'Hello'}, | |
203 PACKAGE + '.templates.test') | |
204 assert isinstance(stream, Stream) | |
205 | |
206 def test_transform_with_load(self): | |
207 plugin = TextTemplateEnginePlugin() | |
208 tmpl = plugin.load_template(PACKAGE + '.templates.test') | |
209 stream = plugin.transform({'message': 'Hello'}, tmpl) | |
210 assert isinstance(stream, Stream) | |
211 | |
212 def test_render(self): | |
213 plugin = TextTemplateEnginePlugin() | |
214 tmpl = plugin.load_template(PACKAGE + '.templates.test') | |
215 output = plugin.render({'message': 'Hello'}, template=tmpl) | |
216 self.assertEqual("""Test | |
217 ==== | |
218 | |
219 Hello | |
220 """, output) | |
221 | |
222 def test_helper_functions(self): | |
223 plugin = TextTemplateEnginePlugin() | |
224 tmpl = plugin.load_template(PACKAGE + '.templates.functions') | |
225 output = plugin.render({}, template=tmpl) | |
226 self.assertEqual("""False | |
227 bar | |
228 """, output) | |
229 | |
230 | |
231 def suite(): | |
232 suite = unittest.TestSuite() | |
233 suite.addTest(unittest.makeSuite(MarkupTemplateEnginePluginTestCase, 'test')) | |
234 suite.addTest(unittest.makeSuite(TextTemplateEnginePluginTestCase, 'test')) | |
235 return suite | |
236 | |
237 if __name__ == '__main__': | |
238 unittest.main(defaultTest='suite') |