Commit a8e390cb by Chris Jerdonek

All unit tests except for spec tests now work in both Python 2/3.

parent f9bda3a5
......@@ -13,14 +13,20 @@ from pystache import defaults
from pystache.locator import Locator
def _default_to_unicode(s, encoding=None):
"""
Raises a TypeError exception if the given string is already unicode.
# We make a function so that the current defaults take effect.
# TODO: revisit whether this is necessary.
# TODO: change assertNotEquals to assertNotEqual everywhere.
"""
if encoding is None:
encoding = defaults.STRING_ENCODING
return unicode(s, encoding, defaults.DECODE_ERRORS)
def _make_to_unicode():
def to_unicode(s, encoding=None):
"""
Raises a TypeError exception if the given string is already unicode.
"""
if encoding is None:
encoding = defaults.STRING_ENCODING
return unicode(s, encoding, defaults.DECODE_ERRORS)
return to_unicode
class Loader(object):
......@@ -68,7 +74,7 @@ class Loader(object):
search_dirs = defaults.SEARCH_DIRS
if to_unicode is None:
to_unicode = _default_to_unicode
to_unicode = _make_to_unicode()
self.extension = extension
self.file_encoding = file_encoding
......
......@@ -40,8 +40,9 @@ class Renderer(object):
>>> partials = {'partial': 'Hello, {{thing}}!'}
>>> renderer = Renderer(partials=partials)
>>> renderer.render('{{>partial}}', {'thing': 'world'})
u'Hello, world!'
>>> # We apply print to make the test work in Python 3 after 2to3.
>>> print renderer.render('{{>partial}}', {'thing': 'world'})
Hello, world!
"""
......
......@@ -88,3 +88,28 @@ class AssertIsMixin:
# http://docs.python.org/library/unittest.html#unittest.TestCase.assertIsNone
def assertIs(self, first, second):
self.assertTrue(first is second, msg="%s is not %s" % (repr(first), repr(second)))
class SetupDefaults(object):
"""
Mix this class in to a unittest.TestCase for standard defaults.
This class allows for consistent test results across Python 2/3.
"""
def setup_defaults(self):
self.original_decode_errors = defaults.DECODE_ERRORS
self.original_file_encoding = defaults.FILE_ENCODING
self.original_string_encoding = defaults.STRING_ENCODING
defaults.DECODE_ERRORS = 'strict'
defaults.FILE_ENCODING = 'ascii'
defaults.STRING_ENCODING = 'ascii'
def teardown_defaults(self):
defaults.DECODE_ERRORS = self.original_decode_errors
defaults.FILE_ENCODING = self.original_file_encoding
defaults.STRING_ENCODING = self.original_string_encoding
......@@ -9,25 +9,18 @@ import os
import sys
import unittest
from pystache.tests.common import AssertStringMixin, DATA_DIR
from pystache.tests.common import AssertStringMixin, DATA_DIR, SetupDefaults
from pystache import defaults
from pystache.loader import Loader
class LoaderTests(unittest.TestCase, AssertStringMixin):
class LoaderTests(unittest.TestCase, AssertStringMixin, SetupDefaults):
# Switching to standard encodings allows for consistent test
# results across Python 2/3.
def setUp(self):
self.original_string_encoding = defaults.STRING_ENCODING
self.original_file_encoding = defaults.FILE_ENCODING
defaults.STRING_ENCODING = 'ascii'
defaults.FILE_ENCODING = 'ascii'
self.setup_defaults()
def tearDown(self):
defaults.STRING_ENCODING = self.original_string_encoding
defaults.FILE_ENCODING = self.original_file_encoding
self.teardown_defaults()
def test_init__extension(self):
loader = Loader(extension='foo')
......@@ -63,25 +56,19 @@ class LoaderTests(unittest.TestCase, AssertStringMixin):
decode_errors = defaults.DECODE_ERRORS
string_encoding = defaults.STRING_ENCODING
nonascii = 'abcdé'
nonascii = u'abcdé'.encode('utf-8')
try:
defaults.DECODE_ERRORS = 'strict'
defaults.STRING_ENCODING = 'ascii'
loader = Loader()
self.assertRaises(UnicodeDecodeError, loader.to_unicode, nonascii)
loader = Loader()
self.assertRaises(UnicodeDecodeError, loader.to_unicode, nonascii)
defaults.DECODE_ERRORS = 'ignore'
loader = Loader()
self.assertString(loader.to_unicode(nonascii), u'abcd')
defaults.DECODE_ERRORS = 'ignore'
loader = Loader()
self.assertString(loader.to_unicode(nonascii), u'abcd')
defaults.STRING_ENCODING = 'utf-8'
loader = Loader()
self.assertString(loader.to_unicode(nonascii), u'abcdé')
defaults.STRING_ENCODING = 'utf-8'
loader = Loader()
self.assertString(loader.to_unicode(nonascii), u'abcdé')
finally:
defaults.DECODE_ERRORS = decode_errors
defaults.STRING_ENCODING = string_encoding
def _get_path(self, filename):
return os.path.join(DATA_DIR, filename)
......@@ -126,10 +113,10 @@ class LoaderTests(unittest.TestCase, AssertStringMixin):
Test unicode(): encoding attribute.
"""
# TODO: rename reader to loader everywhere in this module.
reader = Loader()
non_ascii = u'abcdé'.encode('utf-8')
self.assertRaises(UnicodeDecodeError, reader.unicode, non_ascii)
def to_unicode(s, encoding=None):
......
......@@ -15,8 +15,7 @@ from pystache import Renderer
from pystache import TemplateSpec
from pystache.loader import Loader
from pystache.tests.common import get_data_path
from pystache.tests.common import AssertStringMixin
from pystache.tests.common import get_data_path, AssertStringMixin
from pystache.tests.data.views import SayHello
......@@ -179,7 +178,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test that the string_encoding attribute is respected.
"""
renderer = Renderer()
renderer = self._renderer()
b = u"é".encode('utf-8')
renderer.string_encoding = "ascii"
......@@ -193,7 +192,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test that the decode_errors attribute is respected.
"""
renderer = Renderer()
renderer = self._renderer()
renderer.string_encoding = "ascii"
b = u"déf".encode('utf-8')
......@@ -211,7 +210,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test that _make_loader() returns a Loader.
"""
renderer = Renderer()
renderer = self._renderer()
loader = renderer._make_loader()
self.assertEqual(type(loader), Loader)
......@@ -223,7 +222,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
"""
unicode_ = lambda x: x
renderer = Renderer()
renderer = self._renderer()
renderer.file_encoding = 'enc'
renderer.file_extension = 'ext'
renderer.unicode = unicode_
......@@ -241,22 +240,22 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Check that render() returns a string of type unicode.
"""
renderer = Renderer()
renderer = self._renderer()
rendered = renderer.render('foo')
self.assertEqual(type(rendered), unicode)
def test_render__unicode(self):
renderer = Renderer()
renderer = self._renderer()
actual = renderer.render(u'foo')
self.assertEqual(actual, u'foo')
def test_render__str(self):
renderer = Renderer()
renderer = self._renderer()
actual = renderer.render('foo')
self.assertEqual(actual, 'foo')
def test_render__non_ascii_character(self):
renderer = Renderer()
renderer = self._renderer()
actual = renderer.render(u'Poincaré')
self.assertEqual(actual, u'Poincaré')
......@@ -265,7 +264,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test render(): passing a context.
"""
renderer = Renderer()
renderer = self._renderer()
self.assertEqual(renderer.render('Hi {{person}}', {'person': 'Mom'}), 'Hi Mom')
def test_render__context_and_kwargs(self):
......@@ -273,7 +272,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test render(): passing a context and **kwargs.
"""
renderer = Renderer()
renderer = self._renderer()
template = 'Hi {{person1}} and {{person2}}'
self.assertEqual(renderer.render(template, {'person1': 'Mom'}, person2='Dad'), 'Hi Mom and Dad')
......@@ -282,7 +281,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test render(): passing **kwargs and no context.
"""
renderer = Renderer()
renderer = self._renderer()
self.assertEqual(renderer.render('Hi {{person}}', person='Mom'), 'Hi Mom')
def test_render__context_and_kwargs__precedence(self):
......@@ -290,7 +289,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
Test render(): **kwargs takes precedence over context.
"""
renderer = Renderer()
renderer = self._renderer()
self.assertEqual(renderer.render('Hi {{person}}', {'person': 'Mom'}, person='Dad'), 'Hi Dad')
def test_render__kwargs_does_not_modify_context(self):
......@@ -299,7 +298,7 @@ class RendererTests(unittest.TestCase, AssertStringMixin):
"""
context = {}
renderer = Renderer()
renderer = self._renderer()
renderer.render('Hi {{person}}', context=context, foo="bar")
self.assertEqual(context, {})
......
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