summaryrefslogtreecommitdiffstats
path: root/simplejson/tests/test_recursion.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2013-09-29 05:12:56 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2013-09-29 05:12:56 -0400
commit19346fa9068878af516cdb670bea4f791337507b (patch)
tree54d4fa5a82b2e0305f3b050dc1ebb53ec9d82a5d /simplejson/tests/test_recursion.py
downloadlastfm-19346fa9068878af516cdb670bea4f791337507b.tar.gz
Initial commit
Diffstat (limited to 'simplejson/tests/test_recursion.py')
-rw-r--r--simplejson/tests/test_recursion.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/simplejson/tests/test_recursion.py b/simplejson/tests/test_recursion.py
new file mode 100644
index 0000000..662eb66
--- /dev/null
+++ b/simplejson/tests/test_recursion.py
@@ -0,0 +1,67 @@
+from unittest import TestCase
+
+import simplejson as json
+
+class JSONTestObject:
+ pass
+
+
+class RecursiveJSONEncoder(json.JSONEncoder):
+ recurse = False
+ def default(self, o):
+ if o is JSONTestObject:
+ if self.recurse:
+ return [JSONTestObject]
+ else:
+ return 'JSONTestObject'
+ return json.JSONEncoder.default(o)
+
+
+class TestRecursion(TestCase):
+ def test_listrecursion(self):
+ x = []
+ x.append(x)
+ try:
+ json.dumps(x)
+ except ValueError:
+ pass
+ else:
+ self.fail("didn't raise ValueError on list recursion")
+ x = []
+ y = [x]
+ x.append(y)
+ try:
+ json.dumps(x)
+ except ValueError:
+ pass
+ else:
+ self.fail("didn't raise ValueError on alternating list recursion")
+ y = []
+ x = [y, y]
+ # ensure that the marker is cleared
+ json.dumps(x)
+
+ def test_dictrecursion(self):
+ x = {}
+ x["test"] = x
+ try:
+ json.dumps(x)
+ except ValueError:
+ pass
+ else:
+ self.fail("didn't raise ValueError on dict recursion")
+ x = {}
+ y = {"a": x, "b": x}
+ # ensure that the marker is cleared
+ json.dumps(y)
+
+ def test_defaultrecursion(self):
+ enc = RecursiveJSONEncoder()
+ self.assertEqual(enc.encode(JSONTestObject), '"JSONTestObject"')
+ enc.recurse = True
+ try:
+ enc.encode(JSONTestObject)
+ except ValueError:
+ pass
+ else:
+ self.fail("didn't raise ValueError on default recursion")