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