Commit 077cb347 authored by Serhiy Storchaka's avatar Serhiy Storchaka

Clean up escape-decode decoder tests.

parent 75fb5ec9
...@@ -808,51 +808,55 @@ class UTF8SigTest(ReadTest): ...@@ -808,51 +808,55 @@ class UTF8SigTest(ReadTest):
class EscapeDecodeTest(unittest.TestCase): class EscapeDecodeTest(unittest.TestCase):
def test_empty(self): def test_empty(self):
self.assertEqual(codecs.escape_decode(""), (b"", 0)) self.assertEqual(codecs.escape_decode(b""), (b"", 0))
def test_raw(self): def test_raw(self):
decode = codecs.escape_decode
for b in range(256): for b in range(256):
if b != b'\\'[0]: b = bytes([b])
self.assertEqual(codecs.escape_decode(bytes([b]) + b'0'), if b != b'\\':
(bytes([b]) + b'0', 2)) self.assertEqual(decode(b + b'0'), (b + b'0', 2))
def test_escape(self): def test_escape(self):
self.assertEqual(codecs.escape_decode(b"[\\\n]"), (b"[]", 4)) decode = codecs.escape_decode
self.assertEqual(codecs.escape_decode(br'[\"]'), (b'["]', 4)) check = coding_checker(self, decode)
self.assertEqual(codecs.escape_decode(br"[\']"), (b"[']", 4)) check(b"[\\\n]", b"[]")
self.assertEqual(codecs.escape_decode(br"[\\]"), (br"[\]", 4)) check(br'[\"]', b'["]')
self.assertEqual(codecs.escape_decode(br"[\a]"), (b"[\x07]", 4)) check(br"[\']", b"[']")
self.assertEqual(codecs.escape_decode(br"[\b]"), (b"[\x08]", 4)) check(br"[\\]", br"[\]")
self.assertEqual(codecs.escape_decode(br"[\t]"), (b"[\x09]", 4)) check(br"[\a]", b"[\x07]")
self.assertEqual(codecs.escape_decode(br"[\n]"), (b"[\x0a]", 4)) check(br"[\b]", b"[\x08]")
self.assertEqual(codecs.escape_decode(br"[\v]"), (b"[\x0b]", 4)) check(br"[\t]", b"[\x09]")
self.assertEqual(codecs.escape_decode(br"[\f]"), (b"[\x0c]", 4)) check(br"[\n]", b"[\x0a]")
self.assertEqual(codecs.escape_decode(br"[\r]"), (b"[\x0d]", 4)) check(br"[\v]", b"[\x0b]")
self.assertEqual(codecs.escape_decode(br"[\7]"), (b"[\x07]", 4)) check(br"[\f]", b"[\x0c]")
self.assertEqual(codecs.escape_decode(br"[\8]"), (br"[\8]", 4)) check(br"[\r]", b"[\x0d]")
self.assertEqual(codecs.escape_decode(br"[\78]"), (b"[\x078]", 5)) check(br"[\7]", b"[\x07]")
self.assertEqual(codecs.escape_decode(br"[\41]"), (b"[!]", 5)) check(br"[\8]", br"[\8]")
self.assertEqual(codecs.escape_decode(br"[\418]"), (b"[!8]", 6)) check(br"[\78]", b"[\x078]")
self.assertEqual(codecs.escape_decode(br"[\101]"), (b"[A]", 6)) check(br"[\41]", b"[!]")
self.assertEqual(codecs.escape_decode(br"[\1010]"), (b"[A0]", 7)) check(br"[\418]", b"[!8]")
self.assertEqual(codecs.escape_decode(br"[\501]"), (b"[A]", 6)) check(br"[\101]", b"[A]")
self.assertEqual(codecs.escape_decode(br"[\x41]"), (b"[A]", 6)) check(br"[\1010]", b"[A0]")
self.assertEqual(codecs.escape_decode(br"[\X41]"), (br"[\X41]", 6)) check(br"[\501]", b"[A]")
self.assertEqual(codecs.escape_decode(br"[\x410]"), (b"[A0]", 7)) check(br"[\x41]", b"[A]")
check(br"[\X41]", br"[\X41]")
check(br"[\x410]", b"[A0]")
for b in range(256): for b in range(256):
if b not in b'\n"\'\\abtnvfr01234567x': if b not in b'\n"\'\\abtnvfr01234567x':
self.assertEqual(codecs.escape_decode(b'\\' + bytes([b])), b = bytes([b])
(b'\\' + bytes([b]), 2)) check(b'\\' + b, b'\\' + b)
def test_errors(self): def test_errors(self):
self.assertRaises(ValueError, codecs.escape_decode, br"\x") decode = codecs.escape_decode
self.assertRaises(ValueError, codecs.escape_decode, br"[\x]") self.assertRaises(ValueError, decode, br"\x")
self.assertEqual(codecs.escape_decode(br"[\x]\x", "ignore"), (b"[]", 6)) self.assertRaises(ValueError, decode, br"[\x]")
self.assertEqual(codecs.escape_decode(br"[\x]\x", "replace"), (b"[?]?", 6)) self.assertEqual(decode(br"[\x]\x", "ignore"), (b"[]", 6))
self.assertRaises(ValueError, codecs.escape_decode, br"\x0") self.assertEqual(decode(br"[\x]\x", "replace"), (b"[?]?", 6))
self.assertRaises(ValueError, codecs.escape_decode, br"[\x0]") self.assertRaises(ValueError, decode, br"\x0")
self.assertEqual(codecs.escape_decode(br"[\x0]\x0", "ignore"), (b"[]", 8)) self.assertRaises(ValueError, decode, br"[\x0]")
self.assertEqual(codecs.escape_decode(br"[\x0]\x0", "replace"), (b"[?]?", 8)) self.assertEqual(decode(br"[\x0]\x0", "ignore"), (b"[]", 8))
self.assertEqual(decode(br"[\x0]\x0", "replace"), (b"[?]?", 8))
class RecodingTest(unittest.TestCase): class RecodingTest(unittest.TestCase):
def test_recoding(self): def test_recoding(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment