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