TESTS = [] def add_test(v): # These modules have a few round-tripping problems... try: assert cjson.decode(cjson.encode(v)) == v assert yajl.loads(yajl.dumps(v)) == v except Exception: pass else: TESTS.append((v,tnetstring.dumps(v),cjson.encode(v))) for (k,v) in FORMAT_EXAMPLES.iteritems(): add_test(v) for _ in xrange(20): v = get_random_object(random.Random(0),jsonsafe=True) add_test(v) def thrash_tnetstring(): for obj, tns, json in TESTS: assert tnetstring.loads(tns) == obj assert tnetstring.loads(tnetstring.dumps(obj)) == obj def thrash_cjson(): for obj, tns, json in TESTS: assert cjson.decode(json) == obj assert cjson.decode(cjson.encode(obj)) == obj def thrash_yajl(): for obj, tns, json in TESTS: assert yajl.loads(json) == obj
assert cjson.decode(cjson.encode(v)) == v assert ujson.loads(ujson.dumps(v)) == v except Exception: pass else: TESTS.append((v,tnetstring.dumps(v),cjson.encode(v),marshal.dumps(v))) # Test it on all our format examples. for (k,v) in FORMAT_EXAMPLES.iteritems(): add_test(v) # And on some randomly-generated objects. # Use a fixed random seed for consistency. r = random.Random(7) for _ in xrange(20): v = get_random_object(r) add_test(v) TEST_DUMP_ONLY = False TEST_LOAD_ONLY = False if len(sys.argv) >1 : if sys.argv[1] == "dumps": TEST_DUMP_ONLY = True elif sys.argv[1] == "loads": TEST_LOAD_ONLY = True elif sys.argv[1] == "roundtrip": pass else: raise ValueError("unknown test type: " + sys.argv[1])