~ubuntu-branches/ubuntu/trusty/python3.4/trusty-proposed

« back to all changes in this revision

Viewing changes to Lib/test/test_json/test_decode.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-11-25 09:44:27 UTC
  • Revision ID: package-import@ubuntu.com-20131125094427-lzxj8ap5w01lmo7f
Tags: upstream-3.4~b1
ImportĀ upstreamĀ versionĀ 3.4~b1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import decimal
 
2
from io import StringIO, BytesIO
 
3
from collections import OrderedDict
 
4
from test.test_json import PyTest, CTest
 
5
 
 
6
 
 
7
class TestDecode:
 
8
    def test_decimal(self):
 
9
        rval = self.loads('1.1', parse_float=decimal.Decimal)
 
10
        self.assertTrue(isinstance(rval, decimal.Decimal))
 
11
        self.assertEqual(rval, decimal.Decimal('1.1'))
 
12
 
 
13
    def test_float(self):
 
14
        rval = self.loads('1', parse_int=float)
 
15
        self.assertTrue(isinstance(rval, float))
 
16
        self.assertEqual(rval, 1.0)
 
17
 
 
18
    def test_empty_objects(self):
 
19
        self.assertEqual(self.loads('{}'), {})
 
20
        self.assertEqual(self.loads('[]'), [])
 
21
        self.assertEqual(self.loads('""'), "")
 
22
 
 
23
    def test_object_pairs_hook(self):
 
24
        s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
 
25
        p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
 
26
             ("qrt", 5), ("pad", 6), ("hoy", 7)]
 
27
        self.assertEqual(self.loads(s), eval(s))
 
28
        self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p)
 
29
        self.assertEqual(self.json.load(StringIO(s),
 
30
                                        object_pairs_hook=lambda x: x), p)
 
31
        od = self.loads(s, object_pairs_hook=OrderedDict)
 
32
        self.assertEqual(od, OrderedDict(p))
 
33
        self.assertEqual(type(od), OrderedDict)
 
34
        # the object_pairs_hook takes priority over the object_hook
 
35
        self.assertEqual(self.loads(s, object_pairs_hook=OrderedDict,
 
36
                                    object_hook=lambda x: None),
 
37
                         OrderedDict(p))
 
38
        # check that empty objects literals work (see #17368)
 
39
        self.assertEqual(self.loads('{}', object_pairs_hook=OrderedDict),
 
40
                         OrderedDict())
 
41
        self.assertEqual(self.loads('{"empty": {}}',
 
42
                                    object_pairs_hook=OrderedDict),
 
43
                         OrderedDict([('empty', OrderedDict())]))
 
44
 
 
45
    def test_decoder_optimizations(self):
 
46
        # Several optimizations were made that skip over calls to
 
47
        # the whitespace regex, so this test is designed to try and
 
48
        # exercise the uncommon cases. The array cases are already covered.
 
49
        rval = self.loads('{   "key"    :    "value"    ,  "k":"v"    }')
 
50
        self.assertEqual(rval, {"key":"value", "k":"v"})
 
51
 
 
52
    def check_keys_reuse(self, source, loads):
 
53
        rval = loads(source)
 
54
        (a, b), (c, d) = sorted(rval[0]), sorted(rval[1])
 
55
        self.assertIs(a, c)
 
56
        self.assertIs(b, d)
 
57
 
 
58
    def test_keys_reuse(self):
 
59
        s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'
 
60
        self.check_keys_reuse(s, self.loads)
 
61
        self.check_keys_reuse(s, self.json.decoder.JSONDecoder().decode)
 
62
 
 
63
    def test_extra_data(self):
 
64
        s = '[1, 2, 3]5'
 
65
        msg = 'Extra data'
 
66
        self.assertRaisesRegex(ValueError, msg, self.loads, s)
 
67
 
 
68
    def test_invalid_escape(self):
 
69
        s = '["abc\\y"]'
 
70
        msg = 'escape'
 
71
        self.assertRaisesRegex(ValueError, msg, self.loads, s)
 
72
 
 
73
    def test_invalid_input_type(self):
 
74
        msg = 'the JSON object must be str'
 
75
        for value in [1, 3.14, b'bytes', b'\xff\x00', [], {}, None]:
 
76
            self.assertRaisesRegex(TypeError, msg, self.loads, value)
 
77
        with self.assertRaisesRegex(TypeError, msg):
 
78
            self.json.load(BytesIO(b'[1,2,3]'))
 
79
 
 
80
    def test_string_with_utf8_bom(self):
 
81
        # see #18958
 
82
        bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8')
 
83
        with self.assertRaises(ValueError) as cm:
 
84
            self.loads(bom_json)
 
85
        self.assertIn('BOM', str(cm.exception))
 
86
        with self.assertRaises(ValueError) as cm:
 
87
            self.json.load(StringIO(bom_json))
 
88
        self.assertIn('BOM', str(cm.exception))
 
89
        # make sure that the BOM is not detected in the middle of a string
 
90
        bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8'))
 
91
        self.assertEqual(self.loads(bom_in_str), '\ufeff')
 
92
        self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff')
 
93
 
 
94
class TestPyDecode(TestDecode, PyTest): pass
 
95
class TestCDecode(TestDecode, CTest): pass