From e22d339dc53946f384aec1ba3cb70cc53ef621cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walter=20D=C3=B6rwald?= Date: Thu, 17 Nov 2005 08:52:34 +0000 Subject: Add tests for various error cases and for readbuffer_encode() and charbuffer_encode(). This increases code coverage in Modules/_codecsmodule.c from 83% to 95%. --- Lib/test/test_codeccallbacks.py | 5 +++ Lib/test/test_codecs.py | 69 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_codeccallbacks.py b/Lib/test/test_codeccallbacks.py index f8e59cd..7d32e59 100644 --- a/Lib/test/test_codeccallbacks.py +++ b/Lib/test/test_codeccallbacks.py @@ -577,6 +577,11 @@ class CodecCallbackTest(unittest.TestCase): self.assertRaises(TypeError, codecs.register_error, 42) self.assertRaises(TypeError, codecs.register_error, "test.dummy", 42) + def test_badlookupcall(self): + # enhance coverage of: + # Modules/_codecsmodule.c::lookup_error() + self.assertRaises(TypeError, codecs.lookup_error) + def test_unknownhandler(self): # enhance coverage of: # Modules/_codecsmodule.c::lookup_error() diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py index 74ad83b..336696c 100644 --- a/Lib/test/test_codecs.py +++ b/Lib/test/test_codecs.py @@ -260,6 +260,9 @@ class UTF16Test(ReadTest): ] ) + def test_errors(self): + self.assertRaises(UnicodeDecodeError, codecs.utf_16_decode, "\xff", "strict", True) + class UTF16LETest(ReadTest): encoding = "utf-16-le" @@ -278,6 +281,9 @@ class UTF16LETest(ReadTest): ] ) + def test_errors(self): + self.assertRaises(UnicodeDecodeError, codecs.utf_16_le_decode, "\xff", "strict", True) + class UTF16BETest(ReadTest): encoding = "utf-16-be" @@ -296,6 +302,9 @@ class UTF16BETest(ReadTest): ] ) + def test_errors(self): + self.assertRaises(UnicodeDecodeError, codecs.utf_16_be_decode, "\xff", "strict", True) + class UTF8Test(ReadTest): encoding = "utf-8" @@ -317,8 +326,49 @@ class UTF8Test(ReadTest): ] ) +class UTF7Test(ReadTest): + encoding = "utf-7" + + # No test_partial() yet, because UTF-7 doesn't support it. + +class UTF16ExTest(unittest.TestCase): + + def test_errors(self): + self.assertRaises(UnicodeDecodeError, codecs.utf_16_ex_decode, "\xff", "strict", 0, True) + + def test_bad_args(self): + self.assertRaises(TypeError, codecs.utf_16_ex_decode) + +class ReadBufferTest(unittest.TestCase): + + def test_array(self): + import array + self.assertEqual( + codecs.readbuffer_encode(array.array("c", "spam")), + ("spam", 4) + ) + + def test_empty(self): + self.assertEqual(codecs.readbuffer_encode(""), ("", 0)) + + def test_bad_args(self): + self.assertRaises(TypeError, codecs.readbuffer_encode) + self.assertRaises(TypeError, codecs.readbuffer_encode, 42) + +class CharBufferTest(unittest.TestCase): + + def test_string(self): + self.assertEqual(codecs.charbuffer_encode("spam"), ("spam", 4)) + + def test_empty(self): + self.assertEqual(codecs.charbuffer_encode(""), ("", 0)) + + def test_bad_args(self): + self.assertRaises(TypeError, codecs.charbuffer_encode) + self.assertRaises(TypeError, codecs.charbuffer_encode, 42) + class EscapeDecodeTest(unittest.TestCase): - def test_empty_escape_decode(self): + def test_empty(self): self.assertEquals(codecs.escape_decode(""), ("", 0)) class RecodingTest(unittest.TestCase): @@ -395,7 +445,6 @@ punycode_testcases = [ u"\u0056\u0069\u1EC7\u0074", "TisaohkhngthchnitingVit-kjcr8268qyxafd2f1b9g"), - #(L) 3B (u"\u0033\u5E74\u0042\u7D44\u91D1\u516B\u5148\u751F", "3B-ww4c5e180e575a65lsy2b"), @@ -915,6 +964,18 @@ class BasicUnicodeTest(unittest.TestCase): line = reader.readline() self.assertEqual(s[:len(line)], line) + def test_bad_decode_args(self): + for encoding in all_unicode_encodings: + decoder = codecs.getdecoder(encoding) + self.assertRaises(TypeError, decoder) + if encoding not in ("idna", "punycode"): + self.assertRaises(TypeError, decoder, 42) + + def test_bad_encode_args(self): + for encoding in all_unicode_encodings: + encoder = codecs.getencoder(encoding) + self.assertRaises(TypeError, encoder) + class BasicStrTest(unittest.TestCase): def test_basics(self): s = "abc123" @@ -964,6 +1025,10 @@ def test_main(): UTF16LETest, UTF16BETest, UTF8Test, + UTF7Test, + UTF16ExTest, + ReadBufferTest, + CharBufferTest, EscapeDecodeTest, RecodingTest, PunycodeTest, -- cgit v0.12