ยปCore Development>Code coverage>Lib/test/json_tests/__init__.py

Python code coverage for Lib/test/json_tests/__init__.py

#countcontent
1n/aimport os
2n/aimport sys
3n/aimport json
4n/aimport doctest
5n/aimport unittest
6n/a
7n/afrom test import support
8n/a
9n/a# import json with and without accelerations
10n/acjson = support.import_fresh_module('json', fresh=['_json'])
11n/apyjson = support.import_fresh_module('json', blocked=['_json'])
12n/a
13n/a# create two base classes that will be used by the other tests
14n/aclass PyTest(unittest.TestCase):
15n/a json = pyjson
16n/a loads = staticmethod(pyjson.loads)
17n/a dumps = staticmethod(pyjson.dumps)
18n/a
19n/a@unittest.skipUnless(cjson, 'requires _json')
20n/aclass CTest(unittest.TestCase):
21n/a if cjson is not None:
22n/a json = cjson
23n/a loads = staticmethod(cjson.loads)
24n/a dumps = staticmethod(cjson.dumps)
25n/a
26n/a# test PyTest and CTest checking if the functions come from the right module
27n/aclass TestPyTest(PyTest):
28n/a def test_pyjson(self):
29n/a self.assertEqual(self.json.scanner.make_scanner.__module__,
30n/a 'json.scanner')
31n/a self.assertEqual(self.json.decoder.scanstring.__module__,
32n/a 'json.decoder')
33n/a self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
34n/a 'json.encoder')
35n/a
36n/aclass TestCTest(CTest):
37n/a def test_cjson(self):
38n/a self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
39n/a self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
40n/a self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
41n/a self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
42n/a '_json')
43n/a
44n/a
45n/ahere = os.path.dirname(__file__)
46n/a
47n/adef test_suite():
48n/a suite = additional_tests()
49n/a loader = unittest.TestLoader()
50n/a for fn in os.listdir(here):
51n/a if fn.startswith("test") and fn.endswith(".py"):
52n/a modname = "test.json_tests." + fn[:-3]
53n/a __import__(modname)
54n/a module = sys.modules[modname]
55n/a suite.addTests(loader.loadTestsFromModule(module))
56n/a return suite
57n/a
58n/adef additional_tests():
59n/a suite = unittest.TestSuite()
60n/a for mod in (json, json.encoder, json.decoder):
61n/a suite.addTest(doctest.DocTestSuite(mod))
62n/a suite.addTest(TestPyTest('test_pyjson'))
63n/a suite.addTest(TestCTest('test_cjson'))
64n/a return suite
65n/a
66n/adef main():
67n/a suite = test_suite()
68n/a runner = unittest.TextTestRunner()
69n/a runner.run(suite)
70n/a
71n/aif __name__ == '__main__':
72n/a sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
73n/a main()