Commit 111cf436 by Chris Jerdonek

Added Template._make_render_engine() and tests for it.

parent 28233892
...@@ -136,6 +136,16 @@ class Template(object): ...@@ -136,6 +136,16 @@ class Template(object):
return context return context
def _make_render_engine(self):
"""
Return a RenderEngine instance for rendering.
"""
engine = RenderEngine(load_template=self.load_template,
literal=self.literal,
escape=self._unicode_and_escape)
return engine
def render(self, context=None, **kwargs): def render(self, context=None, **kwargs):
""" """
Return the template rendered using the given context. Return the template rendered using the given context.
...@@ -156,16 +166,13 @@ class Template(object): ...@@ -156,16 +166,13 @@ class Template(object):
These values take precedence over the context on any key conflicts. These values take precedence over the context on any key conflicts.
""" """
engine = self._make_render_engine()
context = self._make_context(context, **kwargs) context = self._make_context(context, **kwargs)
template = self.template template = self.template
if not isinstance(template, unicode): if not isinstance(template, unicode):
template = self.unicode(template) template = self.unicode(template)
engine = RenderEngine(load_template=self.load_template,
literal=self.literal,
escape=self._unicode_and_escape)
result = engine.render(template, context) result = engine.render(template, context)
if self.output_encoding is not None: if self.output_encoding is not None:
......
...@@ -355,3 +355,46 @@ class TemplateTestCase(unittest.TestCase): ...@@ -355,3 +355,46 @@ class TemplateTestCase(unittest.TestCase):
template.default_encoding = 'utf_8' template.default_encoding = 'utf_8'
self.assertEquals(template.render(), "déf") self.assertEquals(template.render(), "déf")
# By testing that Template.render() constructs the RenderEngine instance
# correctly, we no longer need to test the rendering code paths through
# the Template. We can test rendering paths through only the RenderEngine
# for the same amount of code coverage.
def test_make_render_engine__load_template(self):
"""
Test that _make_render_engine() passes the right load_template.
"""
template = Template()
template.load_template = "foo" # in real life, this would be a function.
engine = template._make_render_engine()
self.assertEquals(engine.load_template, "foo")
def test_make_render_engine__literal(self):
"""
Test that _make_render_engine() passes the right literal.
"""
template = Template()
template.literal = "foo" # in real life, this would be a function.
engine = template._make_render_engine()
self.assertEquals(engine.literal, "foo")
def test_make_render_engine__escape(self):
"""
Test that _make_render_engine() passes the right escape.
"""
template = Template()
template.unicode = lambda s: s.upper() # a test version.
template.escape = lambda s: "**" + s # a test version.
engine = template._make_render_engine()
escape = engine.escape
self.assertEquals(escape(u"foo"), "**foo")
# Test that escape converts str strings to unicode first.
self.assertEquals(escape("foo"), "**FOO")
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment