jcgregorio@wpgntai-ubiq72.hot.corp.google.com | ed13252 | 2010-04-19 11:12:52 -0700 | [diff] [blame^] | 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""]) |