Sha256: 18939c914e8c182dc3d9ac21ba33644618ea932e8ec0f0bd7012ede0dbd5e6b2

Contents?: true

Size: 1.64 KB

Versions: 33

Compression:

Stored size: 1.64 KB

Contents

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.assertEquals(enc.encode(JSONTestObject), '"JSONTestObject"')
        enc.recurse = True
        try:
            enc.encode(JSONTestObject)
        except ValueError:
            pass
        else:
            self.fail("didn't raise ValueError on default recursion")

Version data entries

33 entries across 33 versions & 5 rubygems

Version Path
pygments.rb-0.5.1 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.5.0 vendor/simplejson/simplejson/tests/test_recursion.py
gitlab-pygments.rb-0.3.2 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.4.2 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.4.1 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.3.7 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.3.6 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.3.5 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.3.4 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.3.3 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.3.2 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.3.1 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.3.0 vendor/simplejson/simplejson/tests/test_recursion.py