ยปCore Development>Code coverage>Lib/test/test_json/test_encode_basestring_ascii.py

Python code coverage for Lib/test/test_json/test_encode_basestring_ascii.py

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