Commit b042d084 by Chris Jerdonek

More unit tests for Python 3: type-checking in Renderer.render() for byte strings.

parent b06ea722
...@@ -5,6 +5,8 @@ This module provides a Renderer class to render templates. ...@@ -5,6 +5,8 @@ This module provides a Renderer class to render templates.
""" """
import sys
from pystache import defaults from pystache import defaults
from pystache.context import Context from pystache.context import Context
from pystache.loader import Loader from pystache.loader import Loader
...@@ -13,6 +15,17 @@ from pystache.specloader import SpecLoader ...@@ -13,6 +15,17 @@ from pystache.specloader import SpecLoader
from pystache.template_spec import TemplateSpec from pystache.template_spec import TemplateSpec
# TODO: come up with a better solution for this. One of the issues here
# is that in Python 3 there is no common base class for unicode strings
# and byte strings, and 2to3 seems to convert all of "str", "unicode",
# and "basestring" to Python 3's "str".
if sys.version_info < (3, ):
_STRING_TYPES = basestring
else:
# The latter evaluates to "bytes" in Python 3 -- even after conversion by 2to3.
_STRING_TYPES = (unicode, type(u"a".encode('utf-8')))
class Renderer(object): class Renderer(object):
""" """
...@@ -336,7 +349,7 @@ class Renderer(object): ...@@ -336,7 +349,7 @@ class Renderer(object):
all items in the *context list. all items in the *context list.
""" """
if isinstance(template, basestring): if isinstance(template, _STRING_TYPES):
return self._render_string(template, *context, **kwargs) return self._render_string(template, *context, **kwargs)
# Otherwise, we assume the template is an object. # Otherwise, we assume the template is an object.
......
...@@ -180,13 +180,13 @@ class RendererTests(unittest.TestCase, AssertStringMixin): ...@@ -180,13 +180,13 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
""" """
renderer = Renderer() renderer = Renderer()
s = "é" b = u"é".encode('utf-8')
renderer.string_encoding = "ascii" renderer.string_encoding = "ascii"
self.assertRaises(UnicodeDecodeError, renderer.unicode, s) self.assertRaises(UnicodeDecodeError, renderer.unicode, b)
renderer.string_encoding = "utf-8" renderer.string_encoding = "utf-8"
self.assertEqual(renderer.unicode(s), u"é") self.assertEqual(renderer.unicode(b), u"é")
def test_unicode__decode_errors(self): def test_unicode__decode_errors(self):
""" """
...@@ -195,14 +195,14 @@ class RendererTests(unittest.TestCase, AssertStringMixin): ...@@ -195,14 +195,14 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
""" """
renderer = Renderer() renderer = Renderer()
renderer.string_encoding = "ascii" renderer.string_encoding = "ascii"
s = "déf" b = u"déf".encode('utf-8')
renderer.decode_errors = "ignore" renderer.decode_errors = "ignore"
self.assertEqual(renderer.unicode(s), "df") self.assertEqual(renderer.unicode(b), "df")
renderer.decode_errors = "replace" renderer.decode_errors = "replace"
# U+FFFD is the official Unicode replacement character. # U+FFFD is the official Unicode replacement character.
self.assertEqual(renderer.unicode(s), u'd\ufffd\ufffdf') self.assertEqual(renderer.unicode(b), u'd\ufffd\ufffdf')
## Test the _make_loader() method. ## Test the _make_loader() method.
......
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