summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2003-01-27 11:28:52 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2003-01-27 11:28:52 (GMT)
commitca87aefe83c05dbd85cc18246f361d19dc593acc (patch)
tree7181a798bfe75bde3ace57876b0561ad956dd687
parent7e172898688c0f601de9470ddfed1a4c365ec81d (diff)
downloadcpython-ca87aefe83c05dbd85cc18246f361d19dc593acc.zip
cpython-ca87aefe83c05dbd85cc18246f361d19dc593acc.tar.gz
cpython-ca87aefe83c05dbd85cc18246f361d19dc593acc.tar.bz2
Patch #670715: Universal Unicode Codec for POSIX iconv.
-rw-r--r--Lib/test/test_iconv_codecs.py95
1 files changed, 95 insertions, 0 deletions
diff --git a/Lib/test/test_iconv_codecs.py b/Lib/test/test_iconv_codecs.py
new file mode 100644
index 0000000..f64ef9b
--- /dev/null
+++ b/Lib/test/test_iconv_codecs.py
@@ -0,0 +1,95 @@
+from test import test_support
+import unittest, sys
+import codecs, _iconv_codec
+from encodings import iconv_codec
+from StringIO import StringIO
+
+class IconvCodecTest(unittest.TestCase):
+
+ if sys.byteorder == 'big':
+ spam = '\x00s\x00p\x00a\x00m\x00s\x00p\x00a\x00m'
+ else:
+ spam = 's\x00p\x00a\x00m\x00s\x00p\x00a\x00m\x00'
+
+ def test_sane(self):
+ self.encoder, self.decoder, self.reader, self.writer = \
+ codecs.lookup(_iconv_codec.internal_encoding)
+ self.assertEqual(self.decoder(self.spam), (u'spamspam', 16))
+ self.assertEqual(self.encoder(u'spamspam'), (self.spam, 8))
+ self.assertEqual(self.reader(StringIO(self.spam)).read(), u'spamspam')
+ f = StringIO()
+ self.writer(f).write(u'spamspam')
+ self.assertEqual(f.getvalue(), self.spam)
+
+ def test_basic_errors(self):
+ self.encoder, self.decoder, self.reader, self.writer = \
+ iconv_codec.lookup("ascii")
+ def testencerror(errors):
+ return self.encoder(u'sp\ufffdam', errors)
+ def testdecerror(errors):
+ return self.decoder('sp\xffam', errors)
+
+ self.assertRaises(UnicodeEncodeError, testencerror, 'strict')
+ self.assertRaises(UnicodeDecodeError, testdecerror, 'strict')
+ self.assertEqual(testencerror('replace'), ('sp?am', 5))
+ self.assertEqual(testdecerror('replace'), (u'sp\ufffdam', 5))
+ self.assertEqual(testencerror('ignore'), ('spam', 5))
+ self.assertEqual(testdecerror('ignore'), (u'spam', 5))
+
+ def test_pep293_errors(self):
+ self.encoder, self.decoder, self.reader, self.writer = \
+ iconv_codec.lookup("ascii")
+ def testencerror(errors):
+ return self.encoder(u'sp\ufffdam', errors)
+ def testdecerror(errors):
+ return self.decoder('sp\xffam', errors)
+
+ self.assertEqual(testencerror('xmlcharrefreplace'),
+ ('sp&#65533;am', 5))
+ self.assertEqual(testencerror('backslashreplace'),
+ ('sp\\ufffdam', 5))
+
+ def error_bomb(exc):
+ return (u'*'*40, len(exc.object))
+ def error_mock(exc):
+ error_mock.lastexc = exc
+ return (unicode(exc.object[exc.start - 1]), exc.end)
+
+ codecs.register_error('test_iconv_codec.bomb', error_bomb)
+ codecs.register_error('test_iconv_codec.mock', error_mock)
+
+ self.assertEqual(testencerror('test_iconv_codec.bomb'),
+ ('sp' + ('*'*40), 5))
+ self.assertEqual(testdecerror('test_iconv_codec.bomb'),
+ (u'sp' + (u'*'*40), 5))
+
+ self.assertEqual(testencerror('test_iconv_codec.mock'), ('sppam', 5))
+ exc = error_mock.lastexc
+ self.assertEqual(exc.object, u'sp\ufffdam')
+ self.assertEqual(exc.start, 2)
+ self.assertEqual(exc.end, 3)
+ self.assert_(isinstance(exc, UnicodeEncodeError))
+
+ self.assertEqual(testdecerror('test_iconv_codec.mock'), (u'sppam', 5))
+ exc = error_mock.lastexc
+ self.assertEqual(exc.object, 'sp\xffam')
+ self.assertEqual(exc.start, 2)
+ self.assertEqual(exc.end, 3)
+ self.assert_(isinstance(exc, UnicodeDecodeError))
+
+ def test_empty_escape_decode(self):
+ self.encoder, self.decoder, self.reader, self.writer = \
+ iconv_codec.lookup("ascii")
+ self.assertEquals(self.decoder(u""), ("", 0))
+ self.assertEquals(self.encoder(""), (u"", 0))
+
+def test_main():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(IconvCodecTest))
+ test_support.run_suite(suite)
+
+
+if __name__ == "__main__":
+ test_main()
+
+# ex: ts=8 sts=4 et