annotate Lib/json/tests/test_decode.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 685f1791fbc5
children 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 import decimal
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2 from unittest import TestCase
52427
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
3 from StringIO import StringIO
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
4
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5 import json
52225
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
6 from collections import OrderedDict
46676
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
7
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
8 class TestDecode(TestCase):
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
9 def test_decimal(self):
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
10 rval = json.loads('1.1', parse_float=decimal.Decimal)
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
11 self.assert_(isinstance(rval, decimal.Decimal))
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
12 self.assertEquals(rval, decimal.Decimal('1.1'))
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
13
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
14 def test_float(self):
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
15 rval = json.loads('1', parse_int=float)
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16 self.assert_(isinstance(rval, float))
4ded35754cb7 Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
17 self.assertEquals(rval, 1.0)
52199
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
18
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
19 def test_decoder_optimizations(self):
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
20 # Several optimizations were made that skip over calls to
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
21 # the whitespace regex, so this test is designed to try and
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
22 # exercise the uncommon cases. The array cases are already covered.
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
23 rval = json.loads('{ "key" : "value" , "k":"v" }')
d95e5add3ca4 merge json library with simplejson 2.0.9 (issue 4136)
Bob Ippolito <bob@redivi.com>
parents: 46676
diff changeset
24 self.assertEquals(rval, {"key":"value", "k":"v"})
52225
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
25
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
26 def test_object_pairs_hook(self):
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
27 s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
28 p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
29 ("qrt", 5), ("pad", 6), ("hoy", 7)]
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
30 self.assertEqual(json.loads(s), eval(s))
52427
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
31 self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p)
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
32 self.assertEqual(json.load(StringIO(s),
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
33 object_pairs_hook=lambda x: x), p)
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
34 od = json.loads(s, object_pairs_hook=OrderedDict)
52225
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
35 self.assertEqual(od, OrderedDict(p))
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
36 self.assertEqual(type(od), OrderedDict)
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
37 # the object_pairs_hook takes priority over the object_hook
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
38 self.assertEqual(json.loads(s,
52427
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
39 object_pairs_hook=OrderedDict,
685f1791fbc5 Issue 5381: fix regression in pure python code path, Issue 5584: fix a decoder bug for unicode float literals outside of a container
Bob Ippolito <bob@redivi.com>
parents: 52225
diff changeset
40 object_hook=lambda x: None),
52225
a11a676fe6c0 Issue 5381: Add object_pairs_hook to the json module.
Raymond Hettinger <python@rcn.com>
parents: 52199
diff changeset
41 OrderedDict(p))