mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Add tests to increase code coverage in Python/codecs.c and Python/exceptions.c.
This commit is contained in:
parent
a53899272b
commit
690402ff17
2 changed files with 150 additions and 2 deletions
|
@ -18,6 +18,66 @@ class PosReturn:
|
||||||
self.pos = len(exc.object)
|
self.pos = len(exc.object)
|
||||||
return (u"<?>", oldpos)
|
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):
|
class CodecCallbackTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_xmlcharrefreplace(self):
|
def test_xmlcharrefreplace(self):
|
||||||
|
@ -417,6 +477,56 @@ class CodecCallbackTest(unittest.TestCase):
|
||||||
codecs.replace_errors,
|
codecs.replace_errors,
|
||||||
UnicodeError("ouch")
|
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
|
# With the correct exception, "replace" returns an "?" or u"\ufffd" replacement
|
||||||
self.assertEquals(
|
self.assertEquals(
|
||||||
codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
|
codecs.replace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
|
||||||
|
@ -455,10 +565,29 @@ class CodecCallbackTest(unittest.TestCase):
|
||||||
codecs.xmlcharrefreplace_errors,
|
codecs.xmlcharrefreplace_errors,
|
||||||
UnicodeTranslateError(u"\u3042", 0, 1, "ouch")
|
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
|
# 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(
|
self.assertEquals(
|
||||||
codecs.xmlcharrefreplace_errors(UnicodeEncodeError("ascii", u"\u3042", 0, 1, "ouch")),
|
codecs.xmlcharrefreplace_errors(
|
||||||
(u"&#%d;" % 0x3042, 1)
|
UnicodeEncodeError("ascii", s, 0, len(s), "ouch")
|
||||||
|
),
|
||||||
|
(u"".join(u"&#%d;" % ord(c) for c in s), len(s))
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_badandgoodbackslashreplaceexceptions(self):
|
def test_badandgoodbackslashreplaceexceptions(self):
|
||||||
|
|
|
@ -746,15 +746,34 @@ class CodecsModuleTest(unittest.TestCase):
|
||||||
self.assertEquals(codecs.encode(u'\xe4\xf6\xfc', 'latin-1'),
|
self.assertEquals(codecs.encode(u'\xe4\xf6\xfc', 'latin-1'),
|
||||||
'\xe4\xf6\xfc')
|
'\xe4\xf6\xfc')
|
||||||
self.assertRaises(TypeError, codecs.encode)
|
self.assertRaises(TypeError, codecs.encode)
|
||||||
|
self.assertRaises(LookupError, codecs.encode, "foo", "__spam__")
|
||||||
self.assertEquals(codecs.encode(u'abc'), 'abc')
|
self.assertEquals(codecs.encode(u'abc'), 'abc')
|
||||||
self.assertRaises(UnicodeEncodeError, codecs.encode, u'\xffff', 'ascii')
|
self.assertRaises(UnicodeEncodeError, codecs.encode, u'\xffff', 'ascii')
|
||||||
|
|
||||||
def test_register(self):
|
def test_register(self):
|
||||||
self.assertRaises(TypeError, codecs.register)
|
self.assertRaises(TypeError, codecs.register)
|
||||||
|
self.assertRaises(TypeError, codecs.register, 42)
|
||||||
|
|
||||||
def test_lookup(self):
|
def test_lookup(self):
|
||||||
self.assertRaises(TypeError, codecs.lookup)
|
self.assertRaises(TypeError, codecs.lookup)
|
||||||
self.assertRaises(LookupError, codecs.lookup, "__spam__")
|
self.assertRaises(LookupError, codecs.lookup, "__spam__")
|
||||||
|
self.assertRaises(LookupError, codecs.lookup, " ")
|
||||||
|
|
||||||
|
def test_getencoder(self):
|
||||||
|
self.assertRaises(TypeError, codecs.getencoder)
|
||||||
|
self.assertRaises(LookupError, codecs.getencoder, "__spam__")
|
||||||
|
|
||||||
|
def test_getdecoder(self):
|
||||||
|
self.assertRaises(TypeError, codecs.getdecoder)
|
||||||
|
self.assertRaises(LookupError, codecs.getdecoder, "__spam__")
|
||||||
|
|
||||||
|
def test_getreader(self):
|
||||||
|
self.assertRaises(TypeError, codecs.getreader)
|
||||||
|
self.assertRaises(LookupError, codecs.getreader, "__spam__")
|
||||||
|
|
||||||
|
def test_getwriter(self):
|
||||||
|
self.assertRaises(TypeError, codecs.getwriter)
|
||||||
|
self.assertRaises(LookupError, codecs.getwriter, "__spam__")
|
||||||
|
|
||||||
class StreamReaderTest(unittest.TestCase):
|
class StreamReaderTest(unittest.TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue