source: monitoring/plugins/mpi/simplejson/tests/test_recursion.py @ 2028

Last change on this file since 2028 was 2028, checked in by sanmai, 12 years ago
  • Add CLARIN Discojuice JSON monitoring plugin.
  • Add CLARIN Discojuice HTML monitoring plugin.
  • Add CLARIN SAML monitoring plugin.
  • LAT OAI-PMH endpoint monitoring plugin.
  • Add one dependency (SimpleJSON), for the time being, needed for Python 2.5.
File size: 1.6 KB
Line 
1from unittest import TestCase
2
3import simplejson as json
4
5class JSONTestObject:
6    pass
7
8
9class RecursiveJSONEncoder(json.JSONEncoder):
10    recurse = False
11    def default(self, o):
12        if o is JSONTestObject:
13            if self.recurse:
14                return [JSONTestObject]
15            else:
16                return 'JSONTestObject'
17        return json.JSONEncoder.default(o)
18
19
20class TestRecursion(TestCase):
21    def test_listrecursion(self):
22        x = []
23        x.append(x)
24        try:
25            json.dumps(x)
26        except ValueError:
27            pass
28        else:
29            self.fail("didn't raise ValueError on list recursion")
30        x = []
31        y = [x]
32        x.append(y)
33        try:
34            json.dumps(x)
35        except ValueError:
36            pass
37        else:
38            self.fail("didn't raise ValueError on alternating list recursion")
39        y = []
40        x = [y, y]
41        # ensure that the marker is cleared
42        json.dumps(x)
43
44    def test_dictrecursion(self):
45        x = {}
46        x["test"] = x
47        try:
48            json.dumps(x)
49        except ValueError:
50            pass
51        else:
52            self.fail("didn't raise ValueError on dict recursion")
53        x = {}
54        y = {"a": x, "b": x}
55        # ensure that the marker is cleared
56        json.dumps(y)
57
58    def test_defaultrecursion(self):
59        enc = RecursiveJSONEncoder()
60        self.assertEquals(enc.encode(JSONTestObject), '"JSONTestObject"')
61        enc.recurse = True
62        try:
63            enc.encode(JSONTestObject)
64        except ValueError:
65            pass
66        else:
67            self.fail("didn't raise ValueError on default recursion")
Note: See TracBrowser for help on using the repository browser.