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