]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-115692: Add tests to increase `json` coverage (#115693)
authorHugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Sun, 14 Apr 2024 12:11:06 +0000 (15:11 +0300)
committerGitHub <noreply@github.com>
Sun, 14 Apr 2024 12:11:06 +0000 (06:11 -0600)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
Lib/test/test_json/test_decode.py
Lib/test/test_json/test_encode_basestring_ascii.py
Lib/test/test_json/test_fail.py
Lib/test/test_json/test_unicode.py

index 124045b13184b3e31ab378e02f3ff340e700b90a..79fb239b35d3f2e3df568f38675aa573024c369f 100644 (file)
@@ -8,14 +8,34 @@ from test import support
 class TestDecode:
     def test_decimal(self):
         rval = self.loads('1.1', parse_float=decimal.Decimal)
-        self.assertTrue(isinstance(rval, decimal.Decimal))
+        self.assertIsInstance(rval, decimal.Decimal)
         self.assertEqual(rval, decimal.Decimal('1.1'))
 
     def test_float(self):
         rval = self.loads('1', parse_int=float)
-        self.assertTrue(isinstance(rval, float))
+        self.assertIsInstance(rval, float)
         self.assertEqual(rval, 1.0)
 
+    def test_bytes(self):
+        self.assertEqual(self.loads(b"1"), 1)
+
+    def test_parse_constant(self):
+        for constant, expected in [
+            ("Infinity", "INFINITY"),
+            ("-Infinity", "-INFINITY"),
+            ("NaN", "NAN"),
+        ]:
+            self.assertEqual(
+                self.loads(constant, parse_constant=str.upper), expected
+            )
+
+    def test_constant_invalid_case(self):
+        for constant in [
+            "nan", "NAN", "naN", "infinity", "INFINITY", "inFiniTy"
+        ]:
+            with self.assertRaises(self.JSONDecodeError):
+                self.loads(constant)
+
     def test_empty_objects(self):
         self.assertEqual(self.loads('{}'), {})
         self.assertEqual(self.loads('[]'), [])
@@ -88,7 +108,8 @@ class TestDecode:
             self.json.load(StringIO(bom_json))
         self.assertIn('BOM', str(cm.exception))
         # make sure that the BOM is not detected in the middle of a string
-        bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8'))
+        bom = ''.encode('utf-8-sig').decode('utf-8')
+        bom_in_str = f'"{bom}"'
         self.assertEqual(self.loads(bom_in_str), '\ufeff')
         self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')
 
index 4bbc6c71489a83839f2e69e97e6546f150edaed2..6a39b72a09df3571ab0c754c303f19204505f208 100644 (file)
@@ -23,8 +23,7 @@ class TestEncodeBasestringAscii:
         for input_string, expect in CASES:
             result = self.json.encoder.encode_basestring_ascii(input_string)
             self.assertEqual(result, expect,
-                '{0!r} != {1!r} for {2}({3!r})'.format(
-                    result, expect, fname, input_string))
+                f'{result!r} != {expect!r} for {fname}({input_string!r})')
 
     def test_ordered_dict(self):
         # See issue 6105
index d6bce605e21463c2c903cff95d7fd62548d5ab06..a74240f1107de348084eb1baebd84be6ae56ce20 100644 (file)
@@ -89,7 +89,7 @@ class TestFail:
             except self.JSONDecodeError:
                 pass
             else:
-                self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
+                self.fail(f"Expected failure for fail{idx}.json: {doc!r}")
 
     def test_non_string_keys_dict(self):
         data = {'a' : 1, (1, 2) : 2}
index 2e8bba2775256ae5f61b2c0b923db76f4a5a3505..68629cceeb9be94e32a0c243fed4bfde7ba4f2dd 100644 (file)
@@ -20,12 +20,17 @@ class TestUnicode:
     def test_encoding5(self):
         u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
         j = self.dumps(u, ensure_ascii=False)
-        self.assertEqual(j, '"{0}"'.format(u))
+        self.assertEqual(j, f'"{u}"')
 
     def test_encoding6(self):
         u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
         j = self.dumps([u], ensure_ascii=False)
-        self.assertEqual(j, '["{0}"]'.format(u))
+        self.assertEqual(j, f'["{u}"]')
+
+    def test_encoding7(self):
+        u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
+        j = self.dumps(u + "\n", ensure_ascii=False)
+        self.assertEqual(j, f'"{u}\\n"')
 
     def test_big_unicode_encode(self):
         u = '\U0001d120'
@@ -34,13 +39,13 @@ class TestUnicode:
 
     def test_big_unicode_decode(self):
         u = 'z\U0001d120x'
-        self.assertEqual(self.loads('"' + u + '"'), u)
+        self.assertEqual(self.loads(f'"{u}"'), u)
         self.assertEqual(self.loads('"z\\ud834\\udd20x"'), u)
 
     def test_unicode_decode(self):
         for i in range(0, 0xd7ff):
             u = chr(i)
-            s = '"\\u{0:04x}"'.format(i)
+            s = f'"\\u{i:04x}"'
             self.assertEqual(self.loads(s), u)
 
     def test_unicode_preservation(self):