# HG changeset patch # User cmlenz # Date 1163412953 0 # Node ID b0278e5c8806188de2e23d20ca1cabcf0e82f444 # Parent 3f8fa7fd27c4e9806a05ff1d4f89bb1541436306 Unit tests for the template engine plugin(s). diff --git a/genshi/template/plugin.py b/genshi/template/plugin.py --- a/genshi/template/plugin.py +++ b/genshi/template/plugin.py @@ -26,6 +26,9 @@ from genshi.template.markup import MarkupTemplate from genshi.template.text import TextTemplate +__all__ = ['ConfigurationError', 'MarkupTemplateEnginePlugin', + 'TextTemplateEnginePlugin'] + class ConfigurationError(Exception): """Exception raised when invalid plugin options are encountered.""" @@ -44,14 +47,15 @@ self.options = options self.default_encoding = options.get('genshi.default_encoding', 'utf-8') - auto_reload = options.get('genshi.auto_reload', '1').lower() \ - in ('1', 'yes', 'true') + auto_reload = options.get('genshi.auto_reload', '1') + if isinstance(auto_reload, basestring): + auto_reload = auto_reload.lower() in ('1', 'on', 'yes', 'true') search_path = options.get('genshi.search_path', '').split(':') try: max_cache_size = int(options.get('genshi.max_cache_size', 25)) except ValueError: raise ConfigurationError('Invalid value for max_cache_size: "%s"' % - max_cache_size) + options.get('genshi.max_cache_size')) self.loader = TemplateLoader(filter(None, search_path), auto_reload=auto_reload, @@ -116,12 +120,12 @@ def __init__(self, extra_vars_func=None, options=None): AbstractTemplateEnginePlugin.__init__(self, extra_vars_func, options) - doctype = options.get('genshi.default_doctype') + doctype = self.options.get('genshi.default_doctype') if doctype and doctype not in self.doctypes: raise ConfigurationError('Unknown doctype "%s"' % doctype) self.default_doctype = self.doctypes.get(doctype) - format = options.get('genshi.default_format', 'html') + format = self.options.get('genshi.default_format', 'html') if format not in ('html', 'xhtml', 'xml', 'text'): raise ConfigurationError('Unknown output format "%s"' % format) self.default_format = format diff --git a/genshi/template/tests/__init__.py b/genshi/template/tests/__init__.py --- a/genshi/template/tests/__init__.py +++ b/genshi/template/tests/__init__.py @@ -17,13 +17,14 @@ def suite(): from genshi.template.tests import core, directives, eval, loader, markup, \ - text + plugin, text suite = unittest.TestSuite() suite.addTest(core.suite()) suite.addTest(directives.suite()) suite.addTest(eval.suite()) suite.addTest(loader.suite()) suite.addTest(markup.suite()) + suite.addTest(plugin.suite()) suite.addTest(text.suite()) return suite diff --git a/genshi/template/tests/plugin.py b/genshi/template/tests/plugin.py new file mode 100644 --- /dev/null +++ b/genshi/template/tests/plugin.py @@ -0,0 +1,238 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2006 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 +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('utf-8', 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="""

+ $message +

""") + 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(""" + + + Test + + +

Test

+

Hello

+ +""", 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(""" + + + Test + + +

Test

+

Hello

+ +""", 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(""" + + + Test + + +

Test

+

Hello

+ +""", output) + + def test_helper_functions(self): + plugin = MarkupTemplateEnginePlugin() + tmpl = plugin.load_template(PACKAGE + '.templates.functions') + output = plugin.render({'snippet': 'Foo'}, template=tmpl) + self.assertEqual("""
+False +bar +Foo +Foo +
""", output) + + +class TextTemplateEnginePluginTestCase(unittest.TestCase): + + def test_init_no_options(self): + plugin = TextTemplateEnginePlugin() + self.assertEqual('utf-8', 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_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') diff --git a/genshi/template/tests/templates/__init__.py b/genshi/template/tests/templates/__init__.py new file mode 100644 diff --git a/genshi/template/tests/templates/functions.html b/genshi/template/tests/templates/functions.html new file mode 100644 --- /dev/null +++ b/genshi/template/tests/templates/functions.html @@ -0,0 +1,6 @@ +
+${defined('foo')} +${value_of('foo', 'bar')} +${XML(snippet)} +${HTML(snippet)} +
diff --git a/genshi/template/tests/templates/functions.txt b/genshi/template/tests/templates/functions.txt new file mode 100644 --- /dev/null +++ b/genshi/template/tests/templates/functions.txt @@ -0,0 +1,2 @@ +${defined('foo')} +${value_of('foo', 'bar')} diff --git a/genshi/template/tests/templates/test.html b/genshi/template/tests/templates/test.html new file mode 100644 --- /dev/null +++ b/genshi/template/tests/templates/test.html @@ -0,0 +1,16 @@ + + + + + Test + + + +

Test

+

$message

+ + + diff --git a/genshi/template/tests/templates/test.txt b/genshi/template/tests/templates/test.txt new file mode 100644 --- /dev/null +++ b/genshi/template/tests/templates/test.txt @@ -0,0 +1,4 @@ +Test +==== + +$message