summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_codecs.py
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2006-03-15 11:35:15 (GMT)
committerWalter Dörwald <walter@livinglogic.de>2006-03-15 11:35:15 (GMT)
commitabb02e59946f9ea3076e96e3b03b51d1cebd46b4 (patch)
tree165444acd89173a8832547078cbc417d4626116e /Lib/test/test_codecs.py
parente2ebb2d7f777db2de72cfeb0e3c489ac4cc5c400 (diff)
downloadcpython-abb02e59946f9ea3076e96e3b03b51d1cebd46b4.zip
cpython-abb02e59946f9ea3076e96e3b03b51d1cebd46b4.tar.gz
cpython-abb02e59946f9ea3076e96e3b03b51d1cebd46b4.tar.bz2
Patch #1436130: codecs.lookup() now returns a CodecInfo object (a subclass
of tuple) that provides incremental decoders and encoders (a way to use stateful codecs without the stream API). Functions codecs.getincrementaldecoder() and codecs.getincrementalencoder() have been added.
Diffstat (limited to 'Lib/test/test_codecs.py')
-rw-r--r--Lib/test/test_codecs.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index 3944d65..913aa91 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -41,6 +41,33 @@ class ReadTest(unittest.TestCase):
self.assertEqual(r.bytebuffer, "")
self.assertEqual(r.charbuffer, u"")
+ # do the check again, this time using a incremental decoder
+ d = codecs.getincrementaldecoder(self.encoding)()
+ result = u""
+ for (c, partialresult) in zip(input.encode(self.encoding), partialresults):
+ result += d.decode(c)
+ self.assertEqual(result, partialresult)
+ # check that there's nothing left in the buffers
+ self.assertEqual(d.decode("", True), u"")
+ self.assertEqual(d.buffer, "")
+
+ # Check whether the rest method works properly
+ d.reset()
+ result = u""
+ for (c, partialresult) in zip(input.encode(self.encoding), partialresults):
+ result += d.decode(c)
+ self.assertEqual(result, partialresult)
+ # check that there's nothing left in the buffers
+ self.assertEqual(d.decode("", True), u"")
+ self.assertEqual(d.buffer, "")
+
+ # check iterdecode()
+ encoded = input.encode(self.encoding)
+ self.assertEqual(
+ input,
+ u"".join(codecs.iterdecode(encoded, self.encoding))
+ )
+
def test_readline(self):
def getreader(input):
stream = StringIO.StringIO(input.encode(self.encoding))
@@ -977,6 +1004,12 @@ class BasicUnicodeTest(unittest.TestCase):
def test_basics(self):
s = u"abc123" # all codecs should be able to encode these
for encoding in all_unicode_encodings:
+ name = codecs.lookup(encoding).name
+ if encoding.endswith("_codec"):
+ name += "_codec"
+ elif encoding == "latin_1":
+ name = "latin_1"
+ self.assertEqual(encoding.replace("_", "-"), name.replace("_", "-"))
(bytes, size) = codecs.getencoder(encoding)(s)
if encoding != "unicode_internal":
self.assertEqual(size, len(s), "%r != %r (encoding=%r)" % (size, len(s), encoding))
@@ -999,6 +1032,30 @@ class BasicUnicodeTest(unittest.TestCase):
decodedresult += reader.read()
self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
+ # check incremental decoder/encoder and iterencode()/iterdecode()
+ try:
+ encoder = codecs.getincrementalencoder(encoding)()
+ except LookupError: # no IncrementalEncoder
+ pass
+ else:
+ # check incremental decoder/encoder
+ encodedresult = ""
+ for c in s:
+ encodedresult += encoder.encode(c)
+ decoder = codecs.getincrementaldecoder(encoding)()
+ decodedresult = u""
+ for c in encodedresult:
+ decodedresult += decoder.decode(c)
+ self.assertEqual(decodedresult, s, "%r != %r (encoding=%r)" % (decodedresult, s, encoding))
+
+ # check iterencode()/iterdecode()
+ result = u"".join(codecs.iterdecode(codecs.iterencode(s, encoding), encoding))
+ self.assertEqual(result, s, "%r != %r (encoding=%r)" % (result, s, encoding))
+
+ # check iterencode()/iterdecode() with empty string
+ result = u"".join(codecs.iterdecode(codecs.iterencode(u"", encoding), encoding))
+ self.assertEqual(result, u"")
+
def test_seek(self):
# all codecs should be able to encode these
s = u"%s\n%s\n" % (100*u"abc123", 100*u"def456")