]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/json/tests/test_decode.py
2 from StringIO
import StringIO
3 from collections
import OrderedDict
4 from json
.tests
import PyTest
, CTest
7 class TestDecode(object):
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'))
14 rval
= self
.loads('1', parse_int
=float)
15 self
.assertTrue(isinstance(rval
, float))
16 self
.assertEqual(rval
, 1.0)
18 def test_decoder_optimizations(self
):
19 # Several optimizations were made that skip over calls to
20 # the whitespace regex, so this test is designed to try and
21 # exercise the uncommon cases. The array cases are already covered.
22 rval
= self
.loads('{ "key" : "value" , "k":"v" }')
23 self
.assertEqual(rval
, {"key":"value", "k":"v"})
25 def test_empty_objects(self
):
26 self
.assertEqual(self
.loads('{}'), {})
27 self
.assertEqual(self
.loads('[]'), [])
28 self
.assertEqual(self
.loads('""'), u
"")
29 self
.assertIsInstance(self
.loads('""'), unicode)
31 def test_object_pairs_hook(self
):
32 s
= '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
33 p
= [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
34 ("qrt", 5), ("pad", 6), ("hoy", 7)]
35 self
.assertEqual(self
.loads(s
), eval(s
))
36 self
.assertEqual(self
.loads(s
, object_pairs_hook
=lambda x
: x
), p
)
37 self
.assertEqual(self
.json
.load(StringIO(s
),
38 object_pairs_hook
=lambda x
: x
), p
)
39 od
= self
.loads(s
, object_pairs_hook
=OrderedDict
)
40 self
.assertEqual(od
, OrderedDict(p
))
41 self
.assertEqual(type(od
), OrderedDict
)
42 # the object_pairs_hook takes priority over the object_hook
43 self
.assertEqual(self
.loads(s
,
44 object_pairs_hook
=OrderedDict
,
45 object_hook
=lambda x
: None),
49 class TestPyDecode(TestDecode
, PyTest
): pass
50 class TestCDecode(TestDecode
, CTest
): pass