Debian Codegraph @

Lib/test/test_json/test_encode_basestring_ascii.py

raw
1 from collections import OrderedDict
2 from test.test_json import PyTest, CTest
3 from test.support import bigaddrspacetest
4
5
6 CASES = [
7 ('/\\"\ucafe\ubabe\uab98\ufcde\ubcda\uef4a\x08\x0c\n\r\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?', '"/\\\\\\"\\ucafe\\ubabe\\uab98\\ufcde\\ubcda\\uef4a\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"'),
8 ('\u0123\u4567\u89ab\ucdef\uabcd\uef4a', '"\\u0123\\u4567\\u89ab\\ucdef\\uabcd\\uef4a"'),
9 ('controls', '"controls"'),
10 ('\x08\x0c\n\r\t', '"\\b\\f\\n\\r\\t"'),
11 ('\x00\x1f\x7f', '"\\u0000\\u001f\\u007f"'),
12 ('{"object with 1 member":["array with 1 element"]}', '"{\\"object with 1 member\\":[\\"array with 1 element\\"]}"'),
13 (' s p a c e d ', '" s p a c e d "'),
14 ('\U0001d120', '"\\ud834\\udd20"'),
15 ('\u03b1\u03a9', '"\\u03b1\\u03a9"'),
16 ("`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'),
17 ]
18
19 class TestEncodeBasestringAscii:
20 def test_encode_basestring_ascii(self):
21 fname = self.json.encoder.encode_basestring_ascii.__name__
22 for input_string, expect in CASES:
23 result = self.json.encoder.encode_basestring_ascii(input_string)
24 self.assertEqual(result, expect,
25 f'{result!r} != {expect!r} for {fname}({input_string!r})')
26
27 def test_ordered_dict(self):
28 # See issue 6105
29 items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
30 s = self.dumps(OrderedDict(items))
31 self.assertEqual(s, '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}')
32
33 def test_sorted_dict(self):
34 items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
35 s = self.dumps(dict(items), sort_keys=True)
36 self.assertEqual(s, '{"five": 5, "four": 4, "one": 1, "three": 3, "two": 2}')
37
38
39 class TestPyEncodeBasestringAscii(TestEncodeBasestringAscii, PyTest): pass
40 class TestCEncodeBasestringAscii(TestEncodeBasestringAscii, CTest):
41 @bigaddrspacetest
42 def test_overflow(self):
43 size = (2**32)//6 + 1
44 s = "\x00"*size
45 with self.assertRaises(OverflowError):
46 self.json.encoder.encode_basestring_ascii(s)