Commit 830eaa7b by Chris Jerdonek

Can now pass unicode strings to Reader.unicode().

parent fabc097d
...@@ -42,11 +42,25 @@ class Reader(object): ...@@ -42,11 +42,25 @@ class Reader(object):
self.decode_errors = decode_errors self.decode_errors = decode_errors
self.encoding = encoding self.encoding = encoding
def unicode(self, text, encoding=None): def unicode(self, s, encoding=None):
"""
Call Python's built-in function unicode(), and return the result.
For unicode strings (or unicode subclasses), this function calls
Python's unicode() without the encoding and errors arguments.
Thus, unlike Python's built-in unicode(), it is okay to pass unicode
strings to this function. (Passing a unicode string to Python's
unicode() with the encoding argument throws the following
error: "TypeError: decoding Unicode is not supported.")
"""
if isinstance(s, unicode):
return unicode(s)
if encoding is None: if encoding is None:
encoding = self.encoding encoding = self.encoding
return unicode(text, encoding, self.decode_errors) return unicode(s, encoding, self.decode_errors)
def read(self, path, encoding=None): def read(self, path, encoding=None):
""" """
......
...@@ -24,7 +24,7 @@ class AssertStringMixin: ...@@ -24,7 +24,7 @@ class AssertStringMixin:
""" """
# Show both friendly and literal versions. # Show both friendly and literal versions.
message = """\ message = """String mismatch: %%s\
Expected: \"""%s\""" Expected: \"""%s\"""
...@@ -32,8 +32,11 @@ class AssertStringMixin: ...@@ -32,8 +32,11 @@ class AssertStringMixin:
Expected: %s Expected: %s
Actual: %s""" % (expected, actual, repr(expected), repr(actual)) Actual: %s""" % (expected, actual, repr(expected), repr(actual))
self.assertEquals(actual, expected, message)
self.assertEquals(type(actual), type(expected), "Type mismatch: " + message) self.assertEquals(actual, expected, message % "different characters")
details = "types different: %s != %s" % (repr(type(expected)), repr(type(actual)))
self.assertEquals(type(expected), type(actual), message % details)
class AssertIsMixin: class AssertIsMixin:
......
...@@ -9,13 +9,14 @@ import os ...@@ -9,13 +9,14 @@ import os
import sys import sys
import unittest import unittest
from .common import AssertStringMixin
from pystache.reader import Reader from pystache.reader import Reader
DATA_DIR = 'tests/data' DATA_DIR = 'tests/data'
class ReaderTestCase(unittest.TestCase): class ReaderTestCase(unittest.TestCase, AssertStringMixin):
def _get_path(self, filename): def _get_path(self, filename):
return os.path.join(DATA_DIR, filename) return os.path.join(DATA_DIR, filename)
...@@ -36,17 +37,40 @@ class ReaderTestCase(unittest.TestCase): ...@@ -36,17 +37,40 @@ class ReaderTestCase(unittest.TestCase):
reader = Reader(encoding='foo') reader = Reader(encoding='foo')
self.assertEquals(reader.encoding, 'foo') self.assertEquals(reader.encoding, 'foo')
def test_unicode(self): def test_unicode__basic__input_str(self):
""" """
Test unicode(): default values. Test unicode(): default arguments with str input.
""" """
reader = Reader() reader = Reader()
actual = reader.unicode("foo") actual = reader.unicode("foo")
self.assertEquals(type(actual), unicode) self.assertString(actual, u"foo")
self.assertEquals(actual, u"foo")
def test_unicode__basic__input_unicode(self):
"""
Test unicode(): default arguments with unicode input.
"""
reader = Reader()
actual = reader.unicode(u"foo")
self.assertString(actual, u"foo")
def test_unicode__basic__input_unicode_subclass(self):
"""
Test unicode(): default arguments with unicode-subclass input.
"""
class UnicodeSubclass(unicode):
pass
s = UnicodeSubclass(u"foo")
reader = Reader()
actual = reader.unicode(s)
self.assertString(actual, u"foo")
def test_unicode__encoding_attribute(self): def test_unicode__encoding_attribute(self):
""" """
......
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