annotate genshi/template/tests/plugin.py @ 935:705727288d7e

Merge r1143 from py3k: add support for python 3 to remaining genshi.template components: * minor changes to track encoding=None API change in core genshi modules. * genshi/template/directives: * slightly odd syntax changes to make the 2to3 .next() fixer pick up *stream.next() * minor test fix for change in behaviour of division (/) in Python 3. * genshi/template/loader: * add 'b' to file modes to ensure it's loaded as bytes in Python 3. * use not isinstance(s, unicode) instead of isinstance(s, str) since the former is correctly converted by 2to3.
author hodgestar
date Fri, 18 Mar 2011 09:17:52 +0000
parents 09a90feb9269
children
rev   line source
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
1 # -*- coding: utf-8 -*-
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
2 #
719
09a90feb9269 Fix copyright years.
cmlenz
parents: 651
diff changeset
3 # Copyright (C) 2006-2007 Edgewall Software
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
4 # Copyright (C) 2006 Matthew Good
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
5 # All rights reserved.
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
6 #
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
7 # This software is licensed as described in the file COPYING, which
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
8 # you should have received as part of this distribution. The terms
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
9 # are also available at http://genshi.edgewall.org/wiki/License.
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
10 #
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
11 # This software consists of voluntary contributions made by many
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
12 # individuals. For the exact contribution history, see the revision
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
13 # history and logs, available at http://genshi.edgewall.org/log/.
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
14
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
15 import doctest
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
16 import os
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
17 import unittest
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
18
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
19 from genshi.core import Stream
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
20 from genshi.output import DocType
592
7145e4eba2ec 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
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
22 from genshi.template.plugin import ConfigurationError, \
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
23 MarkupTemplateEnginePlugin, \
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
24 TextTemplateEnginePlugin
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
25
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
26 PACKAGE = 'genshi.template.tests'
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
27
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
28
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
29 class MarkupTemplateEnginePluginTestCase(unittest.TestCase):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
30
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
31 def test_init_no_options(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
32 plugin = MarkupTemplateEnginePlugin()
935
705727288d7e Merge r1143 from py3k:
hodgestar
parents: 719
diff changeset
33 self.assertEqual(None, plugin.default_encoding)
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
34 self.assertEqual('html', plugin.default_format)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
35 self.assertEqual(None, plugin.default_doctype)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
36
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
37 self.assertEqual([], plugin.loader.search_path)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
38 self.assertEqual(True, plugin.loader.auto_reload)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
39 self.assertEqual(25, plugin.loader._cache.capacity)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
40
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
41 def test_init_with_loader_options(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
42 plugin = MarkupTemplateEnginePlugin(options={
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
43 'genshi.auto_reload': 'off',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
44 'genshi.max_cache_size': '100',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
45 'genshi.search_path': '/usr/share/tmpl:/usr/local/share/tmpl',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
46 })
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
47 self.assertEqual(['/usr/share/tmpl', '/usr/local/share/tmpl'],
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
48 plugin.loader.search_path)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
49 self.assertEqual(False, plugin.loader.auto_reload)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
50 self.assertEqual(100, plugin.loader._cache.capacity)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
51
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
52 def test_init_with_invalid_cache_size(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
53 self.assertRaises(ConfigurationError, MarkupTemplateEnginePlugin,
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
54 options={'genshi.max_cache_size': 'thirty'})
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
55
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
56 def test_init_with_output_options(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
57 plugin = MarkupTemplateEnginePlugin(options={
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
58 'genshi.default_encoding': 'iso-8859-15',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
59 'genshi.default_format': 'xhtml',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
60 'genshi.default_doctype': 'xhtml-strict',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
61 })
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
62 self.assertEqual('iso-8859-15', plugin.default_encoding)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
63 self.assertEqual('xhtml', plugin.default_format)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
64 self.assertEqual(DocType.XHTML, plugin.default_doctype)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
65
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
66 def test_init_with_invalid_output_format(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
67 self.assertRaises(ConfigurationError, MarkupTemplateEnginePlugin,
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
68 options={'genshi.default_format': 'foobar'})
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
69
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
70 def test_init_with_invalid_doctype(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
71 self.assertRaises(ConfigurationError, MarkupTemplateEnginePlugin,
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
72 options={'genshi.default_doctype': 'foobar'})
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
73
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
74 def test_load_template_from_file(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
75 plugin = MarkupTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
76 tmpl = plugin.load_template(PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
77 self.assertEqual('test.html', os.path.basename(tmpl.filename))
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
78 assert isinstance(tmpl, MarkupTemplate)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
79
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
80 def test_load_template_from_string(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
81 plugin = MarkupTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
82 tmpl = plugin.load_template(None, template_string="""<p>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
83 $message
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
84 </p>""")
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
85 self.assertEqual(None, tmpl.filename)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
86 assert isinstance(tmpl, MarkupTemplate)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
87
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
88 def test_transform_with_load(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
89 plugin = MarkupTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
90 tmpl = plugin.load_template(PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
91 stream = plugin.transform({'message': 'Hello'}, tmpl)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
92 assert isinstance(stream, Stream)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
93
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
94 def test_transform_without_load(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
95 plugin = MarkupTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
96 stream = plugin.transform({'message': 'Hello'},
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
97 PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
98 assert isinstance(stream, Stream)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
99
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
100 def test_render(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
101 plugin = MarkupTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
102 tmpl = plugin.load_template(PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
103 output = plugin.render({'message': 'Hello'}, template=tmpl)
b0278e5c8806 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">
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
105 <html lang="en">
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
106 <head>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
107 <title>Test</title>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
108 </head>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
109 <body>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
110 <h1>Test</h1>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
111 <p>Hello</p>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
112 </body>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
113 </html>""", output)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
114
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
115 def test_render_with_format(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
116 plugin = MarkupTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
117 tmpl = plugin.load_template(PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
118 output = plugin.render({'message': 'Hello'}, format='xhtml',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
119 template=tmpl)
b0278e5c8806 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
3460b04daeac Improve the handling of namespaces in serialization.
cmlenz
parents: 353
diff changeset
121 <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
122 <head>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
123 <title>Test</title>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
124 </head>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
125 <body>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
126 <h1>Test</h1>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
127 <p>Hello</p>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
128 </body>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
129 </html>""", output)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
130
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
131 def test_render_with_doctype(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
132 plugin = MarkupTemplateEnginePlugin(options={
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
133 'genshi.default_doctype': 'html-strict',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
134 })
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
135 tmpl = plugin.load_template(PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
136 output = plugin.render({'message': 'Hello'}, template=tmpl)
b0278e5c8806 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">
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
138 <html lang="en">
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
139 <head>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
140 <title>Test</title>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
141 </head>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
142 <body>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
143 <h1>Test</h1>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
144 <p>Hello</p>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
145 </body>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
146 </html>""", output)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
147
651
168dcc0b4782 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):
168dcc0b4782 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={
168dcc0b4782 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',
168dcc0b4782 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 })
168dcc0b4782 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')
168dcc0b4782 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,
168dcc0b4782 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)
168dcc0b4782 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">
168dcc0b4782 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>
168dcc0b4782 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>
168dcc0b4782 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>
168dcc0b4782 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>
168dcc0b4782 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>
168dcc0b4782 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>
168dcc0b4782 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>
168dcc0b4782 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)
168dcc0b4782 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
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
165 def test_helper_functions(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
166 plugin = MarkupTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
167 tmpl = plugin.load_template(PACKAGE + '.templates.functions')
935
705727288d7e Merge r1143 from py3k:
hodgestar
parents: 719
diff changeset
168 output = plugin.render({'snippet': u'<b>Foo</b>'}, template=tmpl)
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
169 self.assertEqual("""<div>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
170 False
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
171 bar
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
172 <b>Foo</b>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
173 <b>Foo</b>
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
174 </div>""", output)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
175
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
176
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
177 class TextTemplateEnginePluginTestCase(unittest.TestCase):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
178
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
179 def test_init_no_options(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
180 plugin = TextTemplateEnginePlugin()
935
705727288d7e Merge r1143 from py3k:
hodgestar
parents: 719
diff changeset
181 self.assertEqual(None, plugin.default_encoding)
353
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
182 self.assertEqual('text', plugin.default_format)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
183
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
184 self.assertEqual([], plugin.loader.search_path)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
185 self.assertEqual(True, plugin.loader.auto_reload)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
186 self.assertEqual(25, plugin.loader._cache.capacity)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
187
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
188 def test_init_with_loader_options(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
189 plugin = TextTemplateEnginePlugin(options={
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
190 'genshi.auto_reload': 'off',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
191 'genshi.max_cache_size': '100',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
192 'genshi.search_path': '/usr/share/tmpl:/usr/local/share/tmpl',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
193 })
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
194 self.assertEqual(['/usr/share/tmpl', '/usr/local/share/tmpl'],
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
195 plugin.loader.search_path)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
196 self.assertEqual(False, plugin.loader.auto_reload)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
197 self.assertEqual(100, plugin.loader._cache.capacity)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
198
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
199 def test_init_with_output_options(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
200 plugin = TextTemplateEnginePlugin(options={
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
201 'genshi.default_encoding': 'iso-8859-15',
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
202 })
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
203 self.assertEqual('iso-8859-15', plugin.default_encoding)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
204
592
7145e4eba2ec 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):
7145e4eba2ec 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={
7145e4eba2ec 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',
7145e4eba2ec 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 })
7145e4eba2ec 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)
7145e4eba2ec 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')
7145e4eba2ec 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)
7145e4eba2ec 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)
7145e4eba2ec 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
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
214 def test_load_template_from_file(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
215 plugin = TextTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
216 tmpl = plugin.load_template(PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
217 assert isinstance(tmpl, TextTemplate)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
218 self.assertEqual('test.txt', os.path.basename(tmpl.filename))
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
219
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
220 def test_load_template_from_string(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
221 plugin = TextTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
222 tmpl = plugin.load_template(None, template_string="$message")
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
223 self.assertEqual(None, tmpl.filename)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
224 assert isinstance(tmpl, TextTemplate)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
225
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
226 def test_transform_without_load(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
227 plugin = TextTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
228 stream = plugin.transform({'message': 'Hello'},
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
229 PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
230 assert isinstance(stream, Stream)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
231
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
232 def test_transform_with_load(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
233 plugin = TextTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
234 tmpl = plugin.load_template(PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
235 stream = plugin.transform({'message': 'Hello'}, tmpl)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
236 assert isinstance(stream, Stream)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
237
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
238 def test_render(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
239 plugin = TextTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
240 tmpl = plugin.load_template(PACKAGE + '.templates.test')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
241 output = plugin.render({'message': 'Hello'}, template=tmpl)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
242 self.assertEqual("""Test
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
243 ====
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
244
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
245 Hello
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
246 """, output)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
247
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
248 def test_helper_functions(self):
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
249 plugin = TextTemplateEnginePlugin()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
250 tmpl = plugin.load_template(PACKAGE + '.templates.functions')
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
251 output = plugin.render({}, template=tmpl)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
252 self.assertEqual("""False
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
253 bar
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
254 """, output)
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
255
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
256
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
257 def suite():
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
258 suite = unittest.TestSuite()
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
259 suite.addTest(unittest.makeSuite(MarkupTemplateEnginePluginTestCase, 'test'))
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
260 suite.addTest(unittest.makeSuite(TextTemplateEnginePluginTestCase, 'test'))
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
261 return suite
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
262
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
263 if __name__ == '__main__':
b0278e5c8806 Unit tests for the template engine plugin(s).
cmlenz
parents:
diff changeset
264 unittest.main(defaultTest='suite')
Copyright (C) 2012-2017 Edgewall Software