diff options
Diffstat (limited to 'pystache/tests/test_renderer.py')
-rw-r--r-- | pystache/tests/test_renderer.py | 182 |
1 files changed, 155 insertions, 27 deletions
diff --git a/pystache/tests/test_renderer.py b/pystache/tests/test_renderer.py index f04c799..0dbe0d9 100644 --- a/pystache/tests/test_renderer.py +++ b/pystache/tests/test_renderer.py @@ -14,6 +14,7 @@ from examples.simple import Simple from pystache import Renderer from pystache import TemplateSpec from pystache.common import TemplateNotFoundError +from pystache.context import ContextStack, KeyNotFoundError from pystache.loader import Loader from pystache.tests.common import get_data_path, AssertStringMixin, AssertExceptionMixin @@ -124,6 +125,22 @@ class RendererInitTestCase(unittest.TestCase): renderer = Renderer(file_extension='foo') self.assertEqual(renderer.file_extension, 'foo') + def test_missing_tags(self): + """ + Check that the missing_tags attribute is set correctly. + + """ + renderer = Renderer(missing_tags='foo') + self.assertEqual(renderer.missing_tags, 'foo') + + def test_missing_tags__default(self): + """ + Check the missing_tags default. + + """ + renderer = Renderer() + self.assertEqual(renderer.missing_tags, 'ignore') + def test_search_dirs__default(self): """ Check the search_dirs default. @@ -319,37 +336,44 @@ class RendererTests(unittest.TestCase, AssertStringMixin): renderer.string_encoding = 'utf_8' self.assertEqual(renderer.render(template), u"déf") - def test_make_load_partial(self): + def test_make_resolve_partial(self): """ - Test the _make_load_partial() method. + Test the _make_resolve_partial() method. """ renderer = Renderer() renderer.partials = {'foo': 'bar'} - load_partial = renderer._make_load_partial() + resolve_partial = renderer._make_resolve_partial() - actual = load_partial('foo') + actual = resolve_partial('foo') self.assertEqual(actual, 'bar') self.assertEqual(type(actual), unicode, "RenderEngine requires that " - "load_partial return unicode strings.") + "resolve_partial return unicode strings.") - def test_make_load_partial__unicode(self): + def test_make_resolve_partial__unicode(self): """ - Test _make_load_partial(): that load_partial doesn't "double-decode" Unicode. + Test _make_resolve_partial(): that resolve_partial doesn't "double-decode" Unicode. """ renderer = Renderer() renderer.partials = {'partial': 'foo'} - load_partial = renderer._make_load_partial() - self.assertEqual(load_partial("partial"), "foo") + resolve_partial = renderer._make_resolve_partial() + self.assertEqual(resolve_partial("partial"), "foo") # Now with a value that is already unicode. renderer.partials = {'partial': u'foo'} - load_partial = renderer._make_load_partial() + resolve_partial = renderer._make_resolve_partial() # If the next line failed, we would get the following error: # TypeError: decoding Unicode is not supported - self.assertEqual(load_partial("partial"), "foo") + self.assertEqual(resolve_partial("partial"), "foo") + + def test_render_name(self): + """Test the render_name() method.""" + data_dir = get_data_path() + renderer = Renderer(search_dirs=data_dir) + actual = renderer.render_name("say_hello", to='foo') + self.assertString(actual, u"Hello, foo") def test_render_path(self): """ @@ -401,12 +425,45 @@ class RendererTests(unittest.TestCase, AssertStringMixin): actual = renderer.render(view) self.assertEqual('Hi pizza!', actual) + def test_custom_string_coercion_via_assignment(self): + """ + Test that string coercion can be customized via attribute assignment. + + """ + renderer = self._renderer() + def to_str(val): + if not val: + return '' + else: + return str(val) + + self.assertEqual(renderer.render('{{value}}', value=None), 'None') + renderer.str_coerce = to_str + self.assertEqual(renderer.render('{{value}}', value=None), '') + + def test_custom_string_coercion_via_subclassing(self): + """ + Test that string coercion can be customized via subclassing. + + """ + class MyRenderer(Renderer): + def str_coerce(self, val): + if not val: + return '' + else: + return str(val) + renderer1 = Renderer() + renderer2 = MyRenderer() + + self.assertEqual(renderer1.render('{{value}}', value=None), 'None') + self.assertEqual(renderer2.render('{{value}}', value=None), '') + # By testing that Renderer.render() constructs the right RenderEngine, # we no longer need to exercise all rendering code paths through # the Renderer. It suffices to test rendering paths through the # RenderEngine for the same amount of code coverage. -class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertExceptionMixin): +class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertStringMixin, AssertExceptionMixin): """ Check the RenderEngine returned by Renderer._make_render_engine(). @@ -420,11 +477,11 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertExceptionMixin): """ return _make_renderer() - ## Test the engine's load_partial attribute. + ## Test the engine's resolve_partial attribute. - def test__load_partial__returns_unicode(self): + def test__resolve_partial__returns_unicode(self): """ - Check that load_partial returns unicode (and not a subclass). + Check that resolve_partial returns unicode (and not a subclass). """ class MyUnicode(unicode): @@ -436,43 +493,70 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertExceptionMixin): engine = renderer._make_render_engine() - actual = engine.load_partial('str') + actual = engine.resolve_partial('str') self.assertEqual(actual, "foo") self.assertEqual(type(actual), unicode) # Check that unicode subclasses are not preserved. - actual = engine.load_partial('subclass') + actual = engine.resolve_partial('subclass') self.assertEqual(actual, "abc") self.assertEqual(type(actual), unicode) - def test__load_partial__not_found__default(self): + def test__resolve_partial__not_found(self): + """ + Check that resolve_partial returns the empty string when a template is not found. + + """ + renderer = Renderer() + + engine = renderer._make_render_engine() + resolve_partial = engine.resolve_partial + + self.assertString(resolve_partial('foo'), u'') + + def test__resolve_partial__not_found__missing_tags_strict(self): """ - Check that load_partial provides a nice message when a template is not found. + Check that resolve_partial provides a nice message when a template is not found. """ renderer = Renderer() + renderer.missing_tags = 'strict' engine = renderer._make_render_engine() - load_partial = engine.load_partial + resolve_partial = engine.resolve_partial self.assertException(TemplateNotFoundError, "File 'foo.mustache' not found in dirs: ['.']", - load_partial, "foo") + resolve_partial, "foo") - def test__load_partial__not_found__dict(self): + def test__resolve_partial__not_found__partials_dict(self): """ - Check that load_partial provides a nice message when a template is not found. + Check that resolve_partial returns the empty string when a template is not found. """ renderer = Renderer() renderer.partials = {} engine = renderer._make_render_engine() - load_partial = engine.load_partial + resolve_partial = engine.resolve_partial + + self.assertString(resolve_partial('foo'), u'') + + def test__resolve_partial__not_found__partials_dict__missing_tags_strict(self): + """ + Check that resolve_partial provides a nice message when a template is not found. - # Include dict directly since str(dict) is different in Python 2 and 3: - # <type 'dict'> versus <class 'dict'>, respectively. + """ + renderer = Renderer() + renderer.missing_tags = 'strict' + renderer.partials = {} + + engine = renderer._make_render_engine() + resolve_partial = engine.resolve_partial + + # Include dict directly since str(dict) is different in Python 2 and 3: + # <type 'dict'> versus <class 'dict'>, respectively. self.assertException(TemplateNotFoundError, "Name 'foo' not found in partials: %s" % dict, - load_partial, "foo") + resolve_partial, "foo") ## Test the engine's literal attribute. @@ -595,3 +679,47 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase, AssertExceptionMixin): self.assertTrue(isinstance(s, unicode)) self.assertEqual(type(escape(s)), unicode) + ## Test the missing_tags attribute. + + def test__missing_tags__unknown_value(self): + """ + Check missing_tags attribute: setting an unknown value. + + """ + renderer = Renderer() + renderer.missing_tags = 'foo' + + self.assertException(Exception, "Unsupported 'missing_tags' value: 'foo'", + renderer._make_render_engine) + + ## Test the engine's resolve_context attribute. + + def test__resolve_context(self): + """ + Check resolve_context(): default arguments. + + """ + renderer = Renderer() + + engine = renderer._make_render_engine() + + stack = ContextStack({'foo': 'bar'}) + + self.assertEqual('bar', engine.resolve_context(stack, 'foo')) + self.assertString(u'', engine.resolve_context(stack, 'missing')) + + def test__resolve_context__missing_tags_strict(self): + """ + Check resolve_context(): missing_tags 'strict'. + + """ + renderer = Renderer() + renderer.missing_tags = 'strict' + + engine = renderer._make_render_engine() + + stack = ContextStack({'foo': 'bar'}) + + self.assertEqual('bar', engine.resolve_context(stack, 'foo')) + self.assertException(KeyNotFoundError, "Key 'missing' not found: first part", + engine.resolve_context, stack, 'missing') |