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-1.2.1 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-1.2.0 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-1.1.2 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-1.1.1 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-1.1.0 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-1.0.0 vendor/simplejson/simplejson/tests/test_recursion.py
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/pygments.rb-0.6.3/vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.6.3 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.6.2 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.6.1 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.6.0 vendor/simplejson/simplejson/tests/test_recursion.py
mortar-pygments.rb-0.5.7 vendor/simplejson/simplejson/tests/test_recursion.py
mortar-pygments.rb-0.5.6 vendor/simplejson/simplejson/tests/test_recursion.py
mortar-pygments.rb-0.5.5 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-jruby-0.5.4.2 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-jruby-0.5.4.1 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-jruby-0.5.4 vendor/simplejson/simplejson/tests/test_recursion.py
gitlab-pygments.rb-0.5.4 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.5.4 vendor/simplejson/simplejson/tests/test_recursion.py
pygments.rb-0.5.2 vendor/simplejson/simplejson/tests/test_recursion.py