~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mozilla/python/simplejson-2.1.1/simplejson/tests/test_decode.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import decimal
 
2
from unittest import TestCase
 
3
from StringIO import StringIO
 
4
 
 
5
import simplejson as json
 
6
from simplejson import OrderedDict
 
7
 
 
8
class TestDecode(TestCase):
 
9
    if not hasattr(TestCase, 'assertIs'):
 
10
        def assertIs(self, a, b):
 
11
            self.assertTrue(a is b, '%r is %r' % (a, b))
 
12
 
 
13
    def test_decimal(self):
 
14
        rval = json.loads('1.1', parse_float=decimal.Decimal)
 
15
        self.assertTrue(isinstance(rval, decimal.Decimal))
 
16
        self.assertEquals(rval, decimal.Decimal('1.1'))
 
17
 
 
18
    def test_float(self):
 
19
        rval = json.loads('1', parse_int=float)
 
20
        self.assertTrue(isinstance(rval, float))
 
21
        self.assertEquals(rval, 1.0)
 
22
 
 
23
    def test_decoder_optimizations(self):
 
24
        # Several optimizations were made that skip over calls to
 
25
        # the whitespace regex, so this test is designed to try and
 
26
        # exercise the uncommon cases. The array cases are already covered.
 
27
        rval = json.loads('{   "key"    :    "value"    ,  "k":"v"    }')
 
28
        self.assertEquals(rval, {"key":"value", "k":"v"})
 
29
 
 
30
    def test_empty_objects(self):
 
31
        s = '{}'
 
32
        self.assertEqual(json.loads(s), eval(s))
 
33
        s = '[]'
 
34
        self.assertEqual(json.loads(s), eval(s))
 
35
        s = '""'
 
36
        self.assertEqual(json.loads(s), eval(s))
 
37
 
 
38
    def test_object_pairs_hook(self):
 
39
        s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
 
40
        p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
 
41
             ("qrt", 5), ("pad", 6), ("hoy", 7)]
 
42
        self.assertEqual(json.loads(s), eval(s))
 
43
        self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p)
 
44
        self.assertEqual(json.load(StringIO(s),
 
45
                                   object_pairs_hook=lambda x: x), p)
 
46
        od = json.loads(s, object_pairs_hook=OrderedDict)
 
47
        self.assertEqual(od, OrderedDict(p))
 
48
        self.assertEqual(type(od), OrderedDict)
 
49
        # the object_pairs_hook takes priority over the object_hook
 
50
        self.assertEqual(json.loads(s,
 
51
                                    object_pairs_hook=OrderedDict,
 
52
                                    object_hook=lambda x: None),
 
53
                         OrderedDict(p))
 
54
 
 
55
    def check_keys_reuse(self, source, loads):
 
56
        rval = loads(source)
 
57
        (a, b), (c, d) = sorted(rval[0]), sorted(rval[1])
 
58
        self.assertIs(a, c)
 
59
        self.assertIs(b, d)
 
60
 
 
61
    def test_keys_reuse_str(self):
 
62
        s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'.encode('utf8')
 
63
        self.check_keys_reuse(s, json.loads)
 
64
 
 
65
    def test_keys_reuse_unicode(self):
 
66
        s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'
 
67
        self.check_keys_reuse(s, json.loads)
 
68
 
 
69
    def test_empty_strings(self):
 
70
        self.assertEqual(json.loads('""'), "")
 
71
        self.assertEqual(json.loads(u'""'), u"")
 
72
        self.assertEqual(json.loads('[""]'), [""])
 
73
        self.assertEqual(json.loads(u'[""]'), [u""])