summaryrefslogtreecommitdiff
path: root/Lib/test/test_codeccallbacks.py
diff options
context:
space:
mode:
authorWalter Dörwald <walter@livinglogic.de>2005-11-17 18:51:34 +0000
committerWalter Dörwald <walter@livinglogic.de>2005-11-17 18:51:34 +0000
commit690402ff170abfc9b24269aa7db923eddf60e607 (patch)
treede6e027bd7e7d55a353e6a47b44f4249d2e62c86 /Lib/test/test_codeccallbacks.py
parenta53899272b71d90a2a2ade76532ff0b96d7ceed1 (diff)
downloadcpython-git-690402ff170abfc9b24269aa7db923eddf60e607.tar.gz
Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
Diffstat (limited to 'Lib/test/test_codeccallbacks.py')
-rw-r--r--Lib/test/test_codeccallbacks.py133
1 files changed, 131 insertions, 2 deletions
diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py
index 7d32e59ca1..c6e56c9dd7 100644
--- a/Lib/test/test_codeccallbacks.py
+++ b/Lib/test/test_codeccallbacks.py
@@ -18,6 +18,66 @@ class PosReturn:
self.pos = len(exc.object)
return (u"<?>", oldpos)
+# A UnicodeEncodeError object without a start attribute
+class NoStartUnicodeEncodeError(UnicodeEncodeError):
+ def __init__(self):
+ UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
+ del self.start
+
+# A UnicodeEncodeError object with a bad start attribute
+class BadStartUnicodeEncodeError(UnicodeEncodeError):
+ def __init__(self):
+ UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
+ self.start = []
+
+# A UnicodeEncodeError object without an end attribute
+class NoEndUnicodeEncodeError(UnicodeEncodeError):
+ def __init__(self):
+ UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
+ del self.end
+
+# A UnicodeEncodeError object without an object attribute
+class NoObjectUnicodeEncodeError(UnicodeEncodeError):
+ def __init__(self):
+ UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
+ del self.object
+
+# A UnicodeEncodeError object with a bad object attribute
+class BadObjectUnicodeEncodeError(UnicodeEncodeError):
+ def __init__(self):
+ UnicodeEncodeError.__init__(self, "ascii", u"", 0, 1, "bad")
+ self.object = []
+
+# A UnicodeDecodeError object without an end attribute
+class NoEndUnicodeDecodeError(UnicodeDecodeError):
+ def __init__(self):
+ UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad")
+ del self.end
+
+# A UnicodeDecodeError object with a bad object attribute
+class BadObjectUnicodeDecodeError(UnicodeDecodeError):
+ def __init__(self):
+ UnicodeDecodeError.__init__(self, "ascii", "", 0, 1, "bad")
+ self.object = []
+
+# A UnicodeTranslateError object without a start attribute
+class NoStartUnicodeTranslateError(UnicodeTranslateError):
+ def __init__(self):
+ UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
+ del self.start
+
+# A UnicodeTranslateError object without an end attribute
+class NoEndUnicodeTranslateError(UnicodeTranslateError):
+ def __init__(self):
+ UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
+ del self.end
+
+# A UnicodeTranslateError object without an object attribute
+class NoObjectUnicodeTranslateError(UnicodeTranslateError):
+ def __init__(self):
+ UnicodeTranslateError.__init__(self, u"", 0, 1, "bad")
+ del self.object
+
class CodecCallbackTest(unittest.TestCase):
def test_xmlcharrefreplace(self):
@@ -417,6 +477,56 @@ class CodecCallbackTest(unittest.TestCase):
codecs.replace_errors,
UnicodeError("ouch")
)
+ self.assertRaises(
+ AttributeError,
+ codecs.replace_errors,
+ NoStartUnicodeEncodeError()
+ )
+ self.assertRaises(
+ TypeError,
+ codecs.replace_errors,
+ BadStartUnicodeEncodeError()
+ )
+ self.assertRaises(
+ AttributeError,
+ codecs.replace_errors,
+ NoEndUnicodeEncodeError()
+ )
+ self.assertRaises(
+ AttributeError,
+ codecs.replace_errors,
+ NoObjectUnicodeEncodeError()
+ )
+ self.assertRaises(
+ TypeError,
+ codecs.replace_errors,
+ BadObjectUnicodeEncodeError()
+ )
+ self.assertRaises(
+ AttributeError,
+ codecs.replace_errors,
+ NoEndUnicodeDecodeError()
+ )
+ self.assertRaises(
+ TypeError,
+ codecs.replace_errors,
+ BadObjectUnicodeDecodeError()
+ )
+ self.assertRaises(
+ AttributeError,
+ codecs.replace_errors,
+ NoStartUnicodeTranslateError()
+ )
+ self.assertRaises(
+ AttributeError,
+ codecs.replace_errors,
+ NoEndUnicodeTranslateError()
+ )
+ self.assertRaises(
+ AttributeError,
+ codecs.replace_errors,
+ NoObjectUnicodeTranslateError()
+ )
# With the correct exception, "replace" returns an "?" or u"\ufffd" replacement
self.assertEquals(
codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
@@ -455,10 +565,29 @@ class CodecCallbackTest(unittest.TestCase):
codecs.xmlcharrefreplace_errors,
UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
)
+ self.assertRaises(
+ AttributeError,
+ codecs.xmlcharrefreplace_errors,
+ NoStartUnicodeEncodeError()
+ )
+ self.assertRaises(
+ AttributeError,
+ codecs.xmlcharrefreplace_errors,
+ NoEndUnicodeEncodeError()
+ )
+ self.assertRaises(
+ AttributeError,
+ codecs.xmlcharrefreplace_errors,
+ NoObjectUnicodeEncodeError()
+ )
# Use the correct exception
+ cs = (0, 1, 9, 10, 99, 100, 999, 1000, 9999, 10000, 0x3042)
+ s = "".join(unichr(c) for c in cs)
self.assertEquals(
- codecs.xmlcharrefreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
- (u"&#%d;" % 0x3042, 1)
+ codecs.xmlcharrefreplace_errors(
+ UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
+ ),
+ (u"".join(u"&#%d;" % ord(c) for c in s), len(s))
)
def test_badandgoodbackslashreplaceexceptions(self):