diff options
author | Bob Ippolito <bob@redivi.com> | 2008-05-03 17:54:12 +0000 |
---|---|---|
committer | Bob Ippolito <bob@redivi.com> | 2008-05-03 17:54:12 +0000 |
commit | 37594d7c05c0ceb30e3894950e33236aa07ebf73 (patch) | |
tree | 78a5b7e59ce9bd722babdcba44e3b290858a5472 | |
parent | f750b9beae59bcc8b69fece978202f118c01535e (diff) | |
download | simplejson-37594d7c05c0ceb30e3894950e33236aa07ebf73.tar.gz |
convert test suite to unittest, remove jsonfilter, bump version to 0.9
git-svn-id: http://simplejson.googlecode.com/svn/trunk@85 a4795897-2c25-0410-b006-0d3caba88fa1
-rw-r--r-- | setup.py | 7 | ||||
-rw-r--r-- | simplejson/__init__.py | 4 | ||||
-rw-r--r-- | simplejson/jsonfilter.py | 40 | ||||
-rw-r--r-- | simplejson/tests/__init__.py | 22 | ||||
-rw-r--r-- | simplejson/tests/test_decode.py | 22 | ||||
-rw-r--r-- | simplejson/tests/test_default.py | 12 | ||||
-rw-r--r-- | simplejson/tests/test_dump.py | 15 | ||||
-rw-r--r-- | simplejson/tests/test_fail.py | 30 | ||||
-rw-r--r-- | simplejson/tests/test_float.py | 11 | ||||
-rw-r--r-- | simplejson/tests/test_indent.py | 82 | ||||
-rw-r--r-- | simplejson/tests/test_pass1.py | 28 | ||||
-rw-r--r-- | simplejson/tests/test_pass2.py | 15 | ||||
-rw-r--r-- | simplejson/tests/test_pass3.py | 16 | ||||
-rw-r--r-- | simplejson/tests/test_recursion.py | 109 | ||||
-rw-r--r-- | simplejson/tests/test_separators.py | 83 | ||||
-rw-r--r-- | simplejson/tests/test_unicode.py | 59 |
16 files changed, 284 insertions, 271 deletions
@@ -18,7 +18,7 @@ from distutils.command.build_ext import build_ext from distutils.errors import CCompilerError, DistutilsExecError, \ DistutilsPlatformError -VERSION = '1.8.2' +VERSION = '1.9' DESCRIPTION = "Simple, fast, extensible JSON encoder/decoder for Python" LONG_DESCRIPTION = """ simplejson is a simple, fast, complete, correct and extensible @@ -93,11 +93,8 @@ setup( license="MIT License", packages=find_packages(exclude=['ez_setup']), platforms=['any'], - test_suite="nose.collector", + test_suite="simplejson.tests", zip_safe=True, - entry_points={ - 'paste.filter_app_factory': ['json = simplejson.jsonfilter:factory'], - }, features={'speedups': speedups}, cmdclass={'build_ext': ve_build_ext}, ) diff --git a/simplejson/__init__.py b/simplejson/__init__.py index 94b94f8..e06f533 100644 --- a/simplejson/__init__.py +++ b/simplejson/__init__.py @@ -67,7 +67,7 @@ Specializing JSON object decoding:: (1+2j) >>> import decimal >>> simplejson.loads('1.1', parse_float=decimal.Decimal) - decimal.Decimal(1.1) + Decimal("1.1") Extending JSONEncoder:: @@ -99,7 +99,7 @@ pretty-print:: Note that the JSON produced by this module's default settings is a subset of YAML, so it may be used as a serializer for that as well. """ -__version__ = '1.8.2' +__version__ = '1.9' __all__ = [ 'dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONEncoder', diff --git a/simplejson/jsonfilter.py b/simplejson/jsonfilter.py deleted file mode 100644 index 01ca21d..0000000 --- a/simplejson/jsonfilter.py +++ /dev/null @@ -1,40 +0,0 @@ -import simplejson -import cgi - -class JSONFilter(object): - def __init__(self, app, mime_type='text/x-json'): - self.app = app - self.mime_type = mime_type - - def __call__(self, environ, start_response): - # Read JSON POST input to jsonfilter.json if matching mime type - response = {'status': '200 OK', 'headers': []} - def json_start_response(status, headers): - response['status'] = status - response['headers'].extend(headers) - environ['jsonfilter.mime_type'] = self.mime_type - if environ.get('REQUEST_METHOD', '') == 'POST': - if environ.get('CONTENT_TYPE', '') == self.mime_type: - args = [_ for _ in [environ.get('CONTENT_LENGTH')] if _] - data = environ['wsgi.input'].read(*map(int, args)) - environ['jsonfilter.json'] = simplejson.loads(data) - res = simplejson.dumps(self.app(environ, json_start_response)) - jsonp = cgi.parse_qs(environ.get('QUERY_STRING', '')).get('jsonp') - if jsonp: - content_type = 'text/javascript' - res = ''.join(jsonp + ['(', res, ')']) - elif 'Opera' in environ.get('HTTP_USER_AGENT', ''): - # Opera has bunk XMLHttpRequest support for most mime types - content_type = 'text/plain' - else: - content_type = self.mime_type - headers = [ - ('Content-type', content_type), - ('Content-length', len(res)), - ] - headers.extend(response['headers']) - start_response(response['status'], headers) - return [res] - -def factory(app, global_conf, **kw): - return JSONFilter(app, **kw) diff --git a/simplejson/tests/__init__.py b/simplejson/tests/__init__.py index e69de29..b33b654 100644 --- a/simplejson/tests/__init__.py +++ b/simplejson/tests/__init__.py @@ -0,0 +1,22 @@ +import unittest +import doctest + +def additional_tests(): + import simplejson + import simplejson.encoder + import simplejson.decoder + suite = unittest.TestSuite() + for mod in (simplejson, simplejson.encoder, simplejson.decoder): + suite.addTest(doctest.DocTestSuite(mod)) + return suite + +def main(): + suite = additional_tests() + runner = unittest.TextTestRunner() + runner.run(suite) + +if __name__ == '__main__': + import os + import sys + sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) + main()
\ No newline at end of file diff --git a/simplejson/tests/test_decode.py b/simplejson/tests/test_decode.py index 9899ca0..aa0c662 100644 --- a/simplejson/tests/test_decode.py +++ b/simplejson/tests/test_decode.py @@ -1,11 +1,15 @@ -import simplejson as S import decimal -def test_decimal(): - rval = S.loads('1.1', parse_float=decimal.Decimal) - assert isinstance(rval, decimal.Decimal) - assert rval == decimal.Decimal('1.1') +from unittest import TestCase + +import simplejson as S + +class TestDecode(TestCase): + def test_decimal(self): + rval = S.loads('1.1', parse_float=decimal.Decimal) + self.assert_(isinstance(rval, decimal.Decimal)) + self.assertEquals(rval, decimal.Decimal('1.1')) -def test_float(): - rval = S.loads('1', parse_int=float) - assert isinstance(rval, float) - assert rval == 1.0 + def test_float(self): + rval = S.loads('1', parse_int=float) + self.assert_(isinstance(rval, float)) + self.assertEquals(rval, 1.0) diff --git a/simplejson/tests/test_default.py b/simplejson/tests/test_default.py index f62ea8e..d4936e3 100644 --- a/simplejson/tests/test_default.py +++ b/simplejson/tests/test_default.py @@ -1,3 +1,9 @@ -import simplejson -def test_default(): - assert simplejson.dumps(type, default=repr) == simplejson.dumps(repr(type)) +from unittest import TestCase + +import simplejson as S + +class TestDefault(TestCase): + def test_default(self): + self.assertEquals( + S.dumps(type, default=repr), + S.dumps(repr(type))) diff --git a/simplejson/tests/test_dump.py b/simplejson/tests/test_dump.py index b4e236e..04a17bf 100644 --- a/simplejson/tests/test_dump.py +++ b/simplejson/tests/test_dump.py @@ -1,10 +1,13 @@ +from unittest import TestCase from cStringIO import StringIO + import simplejson as S -def test_dump(): - sio = StringIO() - S.dump({}, sio) - assert sio.getvalue() == '{}' +class TestDump(TestCase): + def test_dump(self): + sio = StringIO() + S.dump({}, sio) + self.assertEquals(sio.getvalue(), '{}') -def test_dumps(): - assert S.dumps({}) == '{}' + def test_dumps(self): + self.assertEquals(S.dumps({}), '{}') diff --git a/simplejson/tests/test_fail.py b/simplejson/tests/test_fail.py index 9534a94..fba7449 100644 --- a/simplejson/tests/test_fail.py +++ b/simplejson/tests/test_fail.py @@ -1,3 +1,7 @@ +from unittest import TestCase + +import simplejson as S + # Fri Dec 30 18:57:26 2005 JSONDOCS = [ # http://json.org/JSON_checker/test/fail1.json @@ -57,16 +61,16 @@ SKIPS = { 18: "spec doesn't specify any nesting limitations", } -def test_failures(): - import simplejson - for idx, doc in enumerate(JSONDOCS): - idx = idx + 1 - if idx in SKIPS: - simplejson.loads(doc) - continue - try: - simplejson.loads(doc) - except ValueError: - pass - else: - assert False, "Expected failure for fail%d.json: %r" % (idx, doc) +class TestFail(TestCase): + def test_failures(self): + for idx, doc in enumerate(JSONDOCS): + idx = idx + 1 + if idx in SKIPS: + S.loads(doc) + continue + try: + S.loads(doc) + except ValueError: + pass + else: + self.fail("Expected failure for fail%d.json: %r" % (idx, doc)) diff --git a/simplejson/tests/test_float.py b/simplejson/tests/test_float.py index bcec143..82b2285 100644 --- a/simplejson/tests/test_float.py +++ b/simplejson/tests/test_float.py @@ -1,6 +1,9 @@ -import simplejson import math +from unittest import TestCase -def test_floats(): - for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100]: - assert float(simplejson.dumps(num)) == num +import simplejson as S + +class TestFloat(TestCase): + def test_floats(self): + for num in [1617161771.7650001, math.pi, math.pi**100, math.pi**-100]: + self.assertEquals(float(S.dumps(num)), num) diff --git a/simplejson/tests/test_indent.py b/simplejson/tests/test_indent.py index 47dd4dc..a430e15 100644 --- a/simplejson/tests/test_indent.py +++ b/simplejson/tests/test_indent.py @@ -1,41 +1,41 @@ - - - -def test_indent(): - import simplejson - import textwrap - - h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', - {'nifty': 87}, {'field': 'yes', 'morefield': False} ] - - expect = textwrap.dedent("""\ - [ - [ - "blorpie" - ], - [ - "whoops" - ], - [], - "d-shtaeou", - "d-nthiouh", - "i-vhbjkhnth", - { - "nifty": 87 - }, - { - "field": "yes", - "morefield": false - } - ]""") - - - d1 = simplejson.dumps(h) - d2 = simplejson.dumps(h, indent=2, sort_keys=True, separators=(',', ': ')) - - h1 = simplejson.loads(d1) - h2 = simplejson.loads(d2) - - assert h1 == h - assert h2 == h - assert d2 == expect +from unittest import TestCase + +import simplejson as S +import textwrap + +class TestIndent(TestCase): + def test_indent(self): + h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', + {'nifty': 87}, {'field': 'yes', 'morefield': False} ] + + expect = textwrap.dedent("""\ + [ + [ + "blorpie" + ], + [ + "whoops" + ], + [], + "d-shtaeou", + "d-nthiouh", + "i-vhbjkhnth", + { + "nifty": 87 + }, + { + "field": "yes", + "morefield": false + } + ]""") + + + d1 = S.dumps(h) + d2 = S.dumps(h, indent=2, sort_keys=True, separators=(',', ': ')) + + h1 = S.loads(d1) + h2 = S.loads(d2) + + self.assertEquals(h1, h) + self.assertEquals(h2, h) + self.assertEquals(d2, expect) diff --git a/simplejson/tests/test_pass1.py b/simplejson/tests/test_pass1.py index 4eda192..591a18b 100644 --- a/simplejson/tests/test_pass1.py +++ b/simplejson/tests/test_pass1.py @@ -1,3 +1,7 @@ +from unittest import TestCase + +import simplejson as S + # from http://json.org/JSON_checker/test/pass1.json JSON = r''' [ @@ -58,15 +62,15 @@ JSON = r''' ,"rosebud"] ''' -def test_parse(): - # test in/out equivalence and parsing - import simplejson - res = simplejson.loads(JSON) - out = simplejson.dumps(res) - assert res == simplejson.loads(out) - try: - simplejson.dumps(res, allow_nan=False) - except ValueError: - pass - else: - assert False, "23456789012E666 should be out of range" +class TestPass1(TestCase): + def test_parse(self): + # test in/out equivalence and parsing + res = S.loads(JSON) + out = S.dumps(res) + self.assertEquals(res, S.loads(out)) + try: + S.dumps(res, allow_nan=False) + except ValueError: + pass + else: + self.fail("23456789012E666 should be out of range") diff --git a/simplejson/tests/test_pass2.py b/simplejson/tests/test_pass2.py index ae74abb..8cc74e8 100644 --- a/simplejson/tests/test_pass2.py +++ b/simplejson/tests/test_pass2.py @@ -1,11 +1,14 @@ +from unittest import TestCase +import simplejson as S + # from http://json.org/JSON_checker/test/pass2.json JSON = r''' [[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]] ''' -def test_parse(): - # test in/out equivalence and parsing - import simplejson - res = simplejson.loads(JSON) - out = simplejson.dumps(res) - assert res == simplejson.loads(out) +class TestPass2(TestCase): + def test_parse(self): + # test in/out equivalence and parsing + res = S.loads(JSON) + out = S.dumps(res) + self.assertEquals(res, S.loads(out)) diff --git a/simplejson/tests/test_pass3.py b/simplejson/tests/test_pass3.py index d94893f..017ea8e 100644 --- a/simplejson/tests/test_pass3.py +++ b/simplejson/tests/test_pass3.py @@ -1,3 +1,7 @@ +from unittest import TestCase + +import simplejson as S + # from http://json.org/JSON_checker/test/pass3.json JSON = r''' { @@ -8,9 +12,9 @@ JSON = r''' } ''' -def test_parse(): - # test in/out equivalence and parsing - import simplejson - res = simplejson.loads(JSON) - out = simplejson.dumps(res) - assert res == simplejson.loads(out) +class TestPass3(TestCase): + def test_parse(self): + # test in/out equivalence and parsing + res = S.loads(JSON) + out = S.dumps(res) + self.assertEquals(res, S.loads(out)) diff --git a/simplejson/tests/test_recursion.py b/simplejson/tests/test_recursion.py index 756b066..d8ffbd5 100644 --- a/simplejson/tests/test_recursion.py +++ b/simplejson/tests/test_recursion.py @@ -1,62 +1,65 @@ -import simplejson +from unittest import TestCase -def test_listrecursion(): - x = [] - x.append(x) - try: - simplejson.dumps(x) - except ValueError: - pass - else: - assert False, "didn't raise ValueError on list recursion" - x = [] - y = [x] - x.append(y) - try: - simplejson.dumps(x) - except ValueError: - pass - else: - assert False, "didn't raise ValueError on alternating list recursion" - y = [] - x = [y, y] - # ensure that the marker is cleared - simplejson.dumps(x) +import simplejson as S -def test_dictrecursion(): - x = {} - x["test"] = x - try: - simplejson.dumps(x) - except ValueError: - pass - else: - assert False, "didn't raise ValueError on dict recursion" - x = {} - y = {"a": x, "b": x} - # ensure that the marker is cleared - simplejson.dumps(x) - -class TestObject: +class JSONTestObject: pass -class RecursiveJSONEncoder(simplejson.JSONEncoder): +class RecursiveJSONEncoder(S.JSONEncoder): recurse = False def default(self, o): - if o is TestObject: + if o is JSONTestObject: if self.recurse: - return [TestObject] + return [JSONTestObject] else: - return 'TestObject' - simplejson.JSONEncoder.default(o) + return 'JSONTestObject' + return S.JSONEncoder.default(o) + +class TestRecursion(TestCase): + def test_listrecursion(self): + x = [] + x.append(x) + try: + S.dumps(x) + except ValueError: + pass + else: + self.fail("didn't raise ValueError on list recursion") + x = [] + y = [x] + x.append(y) + try: + S.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 + S.dumps(x) + + def test_dictrecursion(self): + x = {} + x["test"] = x + try: + S.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 + S.dumps(x) -def test_defaultrecursion(): - enc = RecursiveJSONEncoder() - assert enc.encode(TestObject) == '"TestObject"' - enc.recurse = True - try: - enc.encode(TestObject) - except ValueError: - pass - else: - assert False, "didn't raise ValueError on default recursion" + 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") diff --git a/simplejson/tests/test_separators.py b/simplejson/tests/test_separators.py index a615354..4506c95 100644 --- a/simplejson/tests/test_separators.py +++ b/simplejson/tests/test_separators.py @@ -1,41 +1,42 @@ - - - -def test_separators(): - import simplejson - import textwrap - - h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', - {'nifty': 87}, {'field': 'yes', 'morefield': False} ] - - expect = textwrap.dedent("""\ - [ - [ - "blorpie" - ] , - [ - "whoops" - ] , - [] , - "d-shtaeou" , - "d-nthiouh" , - "i-vhbjkhnth" , - { - "nifty" : 87 - } , - { - "field" : "yes" , - "morefield" : false - } - ]""") - - - d1 = simplejson.dumps(h) - d2 = simplejson.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : ')) - - h1 = simplejson.loads(d1) - h2 = simplejson.loads(d2) - - assert h1 == h - assert h2 == h - assert d2 == expect +import textwrap +from unittest import TestCase + +import simplejson as S + + +class TestSeparators(TestCase): + def test_separators(self): + h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', + {'nifty': 87}, {'field': 'yes', 'morefield': False} ] + + expect = textwrap.dedent("""\ + [ + [ + "blorpie" + ] , + [ + "whoops" + ] , + [] , + "d-shtaeou" , + "d-nthiouh" , + "i-vhbjkhnth" , + { + "nifty" : 87 + } , + { + "field" : "yes" , + "morefield" : false + } + ]""") + + + d1 = S.dumps(h) + d2 = S.dumps(h, indent=2, sort_keys=True, separators=(' ,', ' : ')) + + h1 = S.loads(d1) + h2 = S.loads(d2) + + self.assertEquals(h1, h) + self.assertEquals(h2, h) + self.assertEquals(d2, expect) diff --git a/simplejson/tests/test_unicode.py b/simplejson/tests/test_unicode.py index f73b48a..fe5f36e 100644 --- a/simplejson/tests/test_unicode.py +++ b/simplejson/tests/test_unicode.py @@ -1,36 +1,35 @@ +from unittest import TestCase + import simplejson as S -def test_encoding1(): - encoder = S.JSONEncoder(encoding='utf-8') - u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' - s = u.encode('utf-8') - ju = encoder.encode(u) - js = encoder.encode(s) - assert ju == js +class TestUnicode(TestCase): + def test_encoding1(self): + encoder = S.JSONEncoder(encoding='utf-8') + u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' + s = u.encode('utf-8') + ju = encoder.encode(u) + js = encoder.encode(s) + self.assertEquals(ju, js) -def test_encoding2(): - u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' - s = u.encode('utf-8') - ju = S.dumps(u, encoding='utf-8') - js = S.dumps(s, encoding='utf-8') - assert ju == js - -def test_big_unicode_encode(): - u = u'\U0001d120' - assert S.dumps(u) == '"\\ud834\\udd20"' - assert S.dumps(u, ensure_ascii=False) == '"\\ud834\\udd20"' + def test_encoding2(self): + u = u'\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}' + s = u.encode('utf-8') + ju = S.dumps(u, encoding='utf-8') + js = S.dumps(s, encoding='utf-8') + self.assertEquals(ju, js) -def test_big_unicode_decode(): - u = u'z\U0001d120x' - assert S.loads('"' + u + '"') == u - assert S.loads('"z\\ud834\\udd20x"') == u + def test_big_unicode_encode(self): + u = u'\U0001d120' + self.assertEquals(S.dumps(u), '"\\ud834\\udd20"') + self.assertEquals(S.dumps(u, ensure_ascii=False), '"\\ud834\\udd20"') -def test_unicode_decode(): - for i in range(0, 0xd7ff): - u = unichr(i) - json = '"\\u%04x"' % (i,) - res = S.loads(json) - assert res == u, 'S.loads(%r) != %r got %r' % (json, u, res) + def test_big_unicode_decode(self): + u = u'z\U0001d120x' + self.assertEquals(S.loads('"' + u + '"'), u) + self.assertEquals(S.loads('"z\\ud834\\udd20x"'), u) -if __name__ == '__main__': - test_unicode_decode() + def test_unicode_decode(self): + for i in range(0, 0xd7ff): + u = unichr(i) + json = '"\\u%04x"' % (i,) + self.assertEquals(S.loads(json), u)
\ No newline at end of file |