13257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport os
23257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport sys
33257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport json
43257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport doctest
53257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielimport unittest
63257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
73257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielfrom test import test_support
83257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
93257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# import json with and without accelerations
103257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielcjson = test_support.import_fresh_module('json', fresh=['_json'])
113257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielpyjson = test_support.import_fresh_module('json', blocked=['_json'])
123257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
133257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# create two base classes that will be used by the other tests
143257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass PyTest(unittest.TestCase):
153257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    json = pyjson
163257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    loads = staticmethod(pyjson.loads)
173257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    dumps = staticmethod(pyjson.dumps)
183257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
193257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel@unittest.skipUnless(cjson, 'requires _json')
203257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass CTest(unittest.TestCase):
213257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    if cjson is not None:
223257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        json = cjson
233257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        loads = staticmethod(cjson.loads)
243257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        dumps = staticmethod(cjson.dumps)
253257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
263257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel# test PyTest and CTest checking if the functions come from the right module
273257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass TestPyTest(PyTest):
283257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    def test_pyjson(self):
293257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        self.assertEqual(self.json.scanner.make_scanner.__module__,
303257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel                         'json.scanner')
313257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        self.assertEqual(self.json.decoder.scanstring.__module__,
323257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel                         'json.decoder')
333257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
343257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel                         'json.encoder')
353257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
363257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielclass TestCTest(CTest):
373257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    def test_cjson(self):
383257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
393257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
403257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
413257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
423257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel                         '_json')
433257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
443257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
453257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielhere = os.path.dirname(__file__)
463257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
473257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef test_suite():
483257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    suite = additional_tests()
493257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    loader = unittest.TestLoader()
503257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    for fn in os.listdir(here):
513257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        if fn.startswith("test") and fn.endswith(".py"):
523257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel            modname = "json.tests." + fn[:-3]
533257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel            __import__(modname)
543257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel            module = sys.modules[modname]
553257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel            suite.addTests(loader.loadTestsFromModule(module))
563257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    return suite
573257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
583257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef additional_tests():
593257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    suite = unittest.TestSuite()
603257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    for mod in (json, json.encoder, json.decoder):
613257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel        suite.addTest(doctest.DocTestSuite(mod))
623257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    suite.addTest(TestPyTest('test_pyjson'))
633257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    suite.addTest(TestCTest('test_cjson'))
643257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    return suite
653257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
663257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanieldef main():
673257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    suite = test_suite()
683257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    runner = unittest.TextTestRunner()
693257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    runner.run(suite)
703257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel
713257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDanielif __name__ == '__main__':
723257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
733257aa99321d745773a6bd1bd4ce7f6fafe74411Daryl McDaniel    main()
74