Skip to content

Commit 8fc953f

Browse files
hugovkencukou
andauthored
gh-115692: Add tests to increase json coverage (#115693)
Co-authored-by: Petr Viktorin <[email protected]>
1 parent 9c93b74 commit 8fc953f

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

Lib/test/test_json/test_decode.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,34 @@
88
class TestDecode:
99
def test_decimal(self):
1010
rval = self.loads('1.1', parse_float=decimal.Decimal)
11-
self.assertTrue(isinstance(rval, decimal.Decimal))
11+
self.assertIsInstance(rval, decimal.Decimal)
1212
self.assertEqual(rval, decimal.Decimal('1.1'))
1313

1414
def test_float(self):
1515
rval = self.loads('1', parse_int=float)
16-
self.assertTrue(isinstance(rval, float))
16+
self.assertIsInstance(rval, float)
1717
self.assertEqual(rval, 1.0)
1818

19+
def test_bytes(self):
20+
self.assertEqual(self.loads(b"1"), 1)
21+
22+
def test_parse_constant(self):
23+
for constant, expected in [
24+
("Infinity", "INFINITY"),
25+
("-Infinity", "-INFINITY"),
26+
("NaN", "NAN"),
27+
]:
28+
self.assertEqual(
29+
self.loads(constant, parse_constant=str.upper), expected
30+
)
31+
32+
def test_constant_invalid_case(self):
33+
for constant in [
34+
"nan", "NAN", "naN", "infinity", "INFINITY", "inFiniTy"
35+
]:
36+
with self.assertRaises(self.JSONDecodeError):
37+
self.loads(constant)
38+
1939
def test_empty_objects(self):
2040
self.assertEqual(self.loads('{}'), {})
2141
self.assertEqual(self.loads('[]'), [])
@@ -88,7 +108,8 @@ def test_string_with_utf8_bom(self):
88108
self.json.load(StringIO(bom_json))
89109
self.assertIn('BOM', str(cm.exception))
90110
# make sure that the BOM is not detected in the middle of a string
91-
bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8'))
111+
bom = ''.encode('utf-8-sig').decode('utf-8')
112+
bom_in_str = f'"{bom}"'
92113
self.assertEqual(self.loads(bom_in_str), '\ufeff')
93114
self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')
94115

Lib/test/test_json/test_encode_basestring_ascii.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def test_encode_basestring_ascii(self):
2323
for input_string, expect in CASES:
2424
result = self.json.encoder.encode_basestring_ascii(input_string)
2525
self.assertEqual(result, expect,
26-
'{0!r} != {1!r} for {2}({3!r})'.format(
27-
result, expect, fname, input_string))
26+
f'{result!r} != {expect!r} for {fname}({input_string!r})')
2827

2928
def test_ordered_dict(self):
3029
# See issue 6105

Lib/test/test_json/test_fail.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def test_failures(self):
8989
except self.JSONDecodeError:
9090
pass
9191
else:
92-
self.fail("Expected failure for fail{0}.json: {1!r}".format(idx, doc))
92+
self.fail(f"Expected failure for fail{idx}.json: {doc!r}")
9393

9494
def test_non_string_keys_dict(self):
9595
data = {'a' : 1, (1, 2) : 2}

Lib/test/test_json/test_unicode.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ def test_encoding4(self):
2020
def test_encoding5(self):
2121
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
2222
j = self.dumps(u, ensure_ascii=False)
23-
self.assertEqual(j, '"{0}"'.format(u))
23+
self.assertEqual(j, f'"{u}"')
2424

2525
def test_encoding6(self):
2626
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
2727
j = self.dumps([u], ensure_ascii=False)
28-
self.assertEqual(j, '["{0}"]'.format(u))
28+
self.assertEqual(j, f'["{u}"]')
29+
30+
def test_encoding7(self):
31+
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
32+
j = self.dumps(u + "\n", ensure_ascii=False)
33+
self.assertEqual(j, f'"{u}\\n"')
2934

3035
def test_big_unicode_encode(self):
3136
u = '\U0001d120'
@@ -34,13 +39,13 @@ def test_big_unicode_encode(self):
3439

3540
def test_big_unicode_decode(self):
3641
u = 'z\U0001d120x'
37-
self.assertEqual(self.loads('"' + u + '"'), u)
42+
self.assertEqual(self.loads(f'"{u}"'), u)
3843
self.assertEqual(self.loads('"z\\ud834\\udd20x"'), u)
3944

4045
def test_unicode_decode(self):
4146
for i in range(0, 0xd7ff):
4247
u = chr(i)
43-
s = '"\\u{0:04x}"'.format(i)
48+
s = f'"\\u{i:04x}"'
4449
self.assertEqual(self.loads(s), u)
4550

4651
def test_unicode_preservation(self):

0 commit comments

Comments
 (0)