]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Use non-zero and non-last positions in error handler tests.
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 16 Mar 2015 06:31:38 +0000 (08:31 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 16 Mar 2015 06:31:38 +0000 (08:31 +0200)
1  2 
Lib/test/test_codeccallbacks.py

index b52e1f6d1cfbe613819237ad338a579759dc94ad,cacdfae7d291d084a6c4a7f687566dd68a19c916..4cfb88e99b673fc73c4f4c1e635ba89acc5202ca
@@@ -590,72 -581,10 +591,76 @@@ class CodecCallbackTest(unittest.TestCa
              with self.subTest(str=s):
                  self.assertEqual(
                      codecs.backslashreplace_errors(
-                         UnicodeEncodeError("ascii", s, 0, len(s), "ouch")),
-                     (r, len(s))
+                         UnicodeEncodeError("ascii", "a" + s + "b",
+                                            1, 1 + len(s), "ouch")),
+                     (r, 1 + len(s))
                  )
-                         UnicodeTranslateError(s, 0, len(s), "ouch")),
-                     (r, len(s))
 +                self.assertEqual(
 +                    codecs.backslashreplace_errors(
-                         UnicodeDecodeError("ascii", bytearray(b), 0, 1, "ouch")),
-                     (r, 1)
++                        UnicodeTranslateError("a" + s + "b",
++                                              1, 1 + len(s), "ouch")),
++                    (r, 1 + len(s))
 +                )
 +        tests = [
 +            (b"a", "\\x61"),
 +            (b"\n", "\\x0a"),
 +            (b"\x00", "\\x00"),
 +            (b"\xff", "\\xff"),
 +        ]
 +        for b, r in tests:
 +            with self.subTest(bytes=b):
 +                self.assertEqual(
 +                    codecs.backslashreplace_errors(
-                         UnicodeEncodeError("ascii", s, 0, len(s), "ouch")),
-                     (r, len(s))
++                        UnicodeDecodeError("ascii", bytearray(b"a" + b + b"b"),
++                                           1, 2, "ouch")),
++                    (r, 2)
 +                )
 +
 +    def test_badandgoodnamereplaceexceptions(self):
 +        # "namereplace" complains about a non-exception passed in
 +        self.assertRaises(
 +           TypeError,
 +           codecs.namereplace_errors,
 +           42
 +        )
 +        # "namereplace" complains about the wrong exception types
 +        self.assertRaises(
 +           TypeError,
 +           codecs.namereplace_errors,
 +           UnicodeError("ouch")
 +        )
 +        # "namereplace" can only be used for encoding
 +        self.assertRaises(
 +            TypeError,
 +            codecs.namereplace_errors,
 +            UnicodeDecodeError("ascii", bytearray(b"\xff"), 0, 1, "ouch")
 +        )
 +        self.assertRaises(
 +            TypeError,
 +            codecs.namereplace_errors,
 +            UnicodeTranslateError("\u3042", 0, 1, "ouch")
 +        )
 +        # Use the correct exception
 +        tests = [
 +            ("\u3042", "\\N{HIRAGANA LETTER A}"),
 +            ("\x00", "\\x00"),
 +            ("\ufbf9", "\\N{ARABIC LIGATURE UIGHUR KIRGHIZ YEH WITH "
 +                       "HAMZA ABOVE WITH ALEF MAKSURA ISOLATED FORM}"),
 +            ("\U000e007f", "\\N{CANCEL TAG}"),
 +            ("\U0010ffff", "\\U0010ffff"),
 +            # Lone surrogates
 +            ("\ud800", "\\ud800"),
 +            ("\udfff", "\\udfff"),
 +            ("\ud800\udfff", "\\ud800\\udfff"),
 +        ]
 +        for s, r in tests:
 +            with self.subTest(str=s):
 +                self.assertEqual(
 +                    codecs.namereplace_errors(
++                        UnicodeEncodeError("ascii", "a" + s + "b",
++                                           1, 1 + len(s), "ouch")),
++                    (r, 1 + len(s))
 +                )
  
      def test_badandgoodsurrogateescapeexceptions(self):
          surrogateescape_errors = codecs.lookup_error('surrogateescape')
                  )
                  self.assertEqual(
                      surrogatepass_errors(
-                         UnicodeDecodeError(enc, bytearray(b[:n]), 0, n, "ouch")),
-                     (s[:1], n)
+                         UnicodeDecodeError(enc, bytearray(b"a" + b[:n] + b"b"),
 -                                           1, n, "ouch")),
++                                           1, 1 + n, "ouch")),
+                     (s[:1], 1 + n)
                  )
  
      def test_badhandlerresults(self):