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