annotate genshi/template/tests/plugin.py @ 719:4bc6741b2811 trunk

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