Home | History | Annotate | Download | only in tests
      1 import sys
      2 from json.tests import PyTest, CTest
      3 
      4 
      5 class TestScanstring(object):
      6     def test_scanstring(self):
      7         scanstring = self.json.decoder.scanstring
      8         self.assertEqual(
      9             scanstring('"z\\ud834\\udd20x"', 1, None, True),
     10             (u'z\U0001d120x', 16))
     11 
     12         if sys.maxunicode == 65535:
     13             self.assertEqual(
     14                 scanstring(u'"z\U0001d120x"', 1, None, True),
     15                 (u'z\U0001d120x', 6))
     16         else:
     17             self.assertEqual(
     18                 scanstring(u'"z\U0001d120x"', 1, None, True),
     19                 (u'z\U0001d120x', 5))
     20 
     21         self.assertEqual(
     22             scanstring('"\\u007b"', 1, None, True),
     23             (u'{', 8))
     24 
     25         self.assertEqual(
     26             scanstring('"A JSON payload should be an object or array, not a string."', 1, None, True),
     27             (u'A JSON payload should be an object or array, not a string.', 60))
     28 
     29         self.assertEqual(
     30             scanstring('["Unclosed array"', 2, None, True),
     31             (u'Unclosed array', 17))
     32 
     33         self.assertEqual(
     34             scanstring('["extra comma",]', 2, None, True),
     35             (u'extra comma', 14))
     36 
     37         self.assertEqual(
     38             scanstring('["double extra comma",,]', 2, None, True),
     39             (u'double extra comma', 21))
     40 
     41         self.assertEqual(
     42             scanstring('["Comma after the close"],', 2, None, True),
     43             (u'Comma after the close', 24))
     44 
     45         self.assertEqual(
     46             scanstring('["Extra close"]]', 2, None, True),
     47             (u'Extra close', 14))
     48 
     49         self.assertEqual(
     50             scanstring('{"Extra comma": true,}', 2, None, True),
     51             (u'Extra comma', 14))
     52 
     53         self.assertEqual(
     54             scanstring('{"Extra value after close": true} "misplaced quoted value"', 2, None, True),
     55             (u'Extra value after close', 26))
     56 
     57         self.assertEqual(
     58             scanstring('{"Illegal expression": 1 + 2}', 2, None, True),
     59             (u'Illegal expression', 21))
     60 
     61         self.assertEqual(
     62             scanstring('{"Illegal invocation": alert()}', 2, None, True),
     63             (u'Illegal invocation', 21))
     64 
     65         self.assertEqual(
     66             scanstring('{"Numbers cannot have leading zeroes": 013}', 2, None, True),
     67             (u'Numbers cannot have leading zeroes', 37))
     68 
     69         self.assertEqual(
     70             scanstring('{"Numbers cannot be hex": 0x14}', 2, None, True),
     71             (u'Numbers cannot be hex', 24))
     72 
     73         self.assertEqual(
     74             scanstring('[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]', 21, None, True),
     75             (u'Too deep', 30))
     76 
     77         self.assertEqual(
     78             scanstring('{"Missing colon" null}', 2, None, True),
     79             (u'Missing colon', 16))
     80 
     81         self.assertEqual(
     82             scanstring('{"Double colon":: null}', 2, None, True),
     83             (u'Double colon', 15))
     84 
     85         self.assertEqual(
     86             scanstring('{"Comma instead of colon", null}', 2, None, True),
     87             (u'Comma instead of colon', 25))
     88 
     89         self.assertEqual(
     90             scanstring('["Colon instead of comma": false]', 2, None, True),
     91             (u'Colon instead of comma', 25))
     92 
     93         self.assertEqual(
     94             scanstring('["Bad value", truth]', 2, None, True),
     95             (u'Bad value', 12))
     96 
     97     def test_issue3623(self):
     98         self.assertRaises(ValueError, self.json.decoder.scanstring, b"xxx", 1,
     99                           "xxx")
    100         self.assertRaises(UnicodeDecodeError,
    101                           self.json.encoder.encode_basestring_ascii, b"xx\xff")
    102 
    103     def test_overflow(self):
    104         with self.assertRaises(OverflowError):
    105             self.json.decoder.scanstring(b"xxx", sys.maxsize+1)
    106 
    107 
    108 class TestPyScanstring(TestScanstring, PyTest): pass
    109 class TestCScanstring(TestScanstring, CTest): pass
    110