diff options
author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-03-24 14:10:58 -0700 |
---|---|---|
committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-03-24 14:12:33 -0700 |
commit | 830eaa7b77fdf3220e5570dc84047ae11998198b (patch) | |
tree | bc4fa47eb00a8ada655ba2c4920076c2e7c358f4 | |
parent | fabc097de7009c4796434d817f04aa14ada81e40 (diff) | |
download | pystache-830eaa7b77fdf3220e5570dc84047ae11998198b.tar.gz |
Can now pass unicode strings to Reader.unicode().
-rw-r--r-- | pystache/reader.py | 18 | ||||
-rw-r--r-- | tests/common.py | 9 | ||||
-rw-r--r-- | tests/test_reader.py | 36 |
3 files changed, 52 insertions, 11 deletions
diff --git a/pystache/reader.py b/pystache/reader.py index 9bb5e1d..f069d00 100644 --- a/pystache/reader.py +++ b/pystache/reader.py @@ -42,11 +42,25 @@ class Reader(object): self.decode_errors = decode_errors 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: encoding = self.encoding - return unicode(text, encoding, self.decode_errors) + return unicode(s, encoding, self.decode_errors) def read(self, path, encoding=None): """ diff --git a/tests/common.py b/tests/common.py index cb703d7..603472a 100644 --- a/tests/common.py +++ b/tests/common.py @@ -24,7 +24,7 @@ class AssertStringMixin: """ # Show both friendly and literal versions. - message = """\ + message = """String mismatch: %%s\ Expected: \"""%s\""" @@ -32,8 +32,11 @@ class AssertStringMixin: Expected: %s 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: diff --git a/tests/test_reader.py b/tests/test_reader.py index 899fda1..b4d2fb5 100644 --- a/tests/test_reader.py +++ b/tests/test_reader.py @@ -9,13 +9,14 @@ import os import sys import unittest +from .common import AssertStringMixin from pystache.reader import Reader DATA_DIR = 'tests/data' -class ReaderTestCase(unittest.TestCase): +class ReaderTestCase(unittest.TestCase, AssertStringMixin): def _get_path(self, filename): return os.path.join(DATA_DIR, filename) @@ -36,17 +37,40 @@ class ReaderTestCase(unittest.TestCase): reader = 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() - actual = reader.unicode("foo") - self.assertEquals(type(actual), unicode) - self.assertEquals(actual, u"foo") + self.assertString(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): """ |