Debian Codegraph @

lib-python/3/test/test_json/__init__.py

raw symbols SCIP
1 import os
2 import json
3 import doctest
4 import unittest
5
6 from test import support
7 from test.support import import_helper
8
9
10 # import json with and without accelerations
11 cjson = import_helper.import_fresh_module('json', fresh=['_json'])
12 pyjson = import_helper.import_fresh_module('json', blocked=['_json'])
13 # JSONDecodeError is cached inside the _json module
14 if cjson is not None:
15 cjson.JSONDecodeError = cjson.decoder.JSONDecodeError = json.JSONDecodeError
16
17 # create two base classes that will be used by the other tests
18 class PyTest(unittest.TestCase):
19 json = pyjson
20 loads = staticmethod(pyjson.loads)
21 dumps = staticmethod(pyjson.dumps)
22 JSONDecodeError = staticmethod(pyjson.JSONDecodeError)
23
24 @unittest.skipUnless(cjson, 'requires _json')
25 class CTest(unittest.TestCase):
26 if cjson is not None:
27 json = cjson
28 loads = staticmethod(cjson.loads)
29 dumps = staticmethod(cjson.dumps)
30 JSONDecodeError = staticmethod(cjson.JSONDecodeError)
31
32 # test PyTest and CTest checking if the functions come from the right module
33 class TestPyTest(PyTest):
34 def test_pyjson(self):
35 self.assertEqual(self.json.scanner.make_scanner.__module__,
36 'json.scanner')
37 self.assertEqual(self.json.decoder.scanstring.__module__,
38 'json.decoder')
39 self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
40 'json.encoder')
41
42 class TestCTest(CTest):
43 def test_cjson(self):
44 self.assertEqual(self.json.scanner.make_scanner.__module__, '_json')
45 self.assertEqual(self.json.decoder.scanstring.__module__, '_json')
46 self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json')
47 self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__,
48 '_json')
49
50
51 def load_tests(loader, _, pattern):
52 suite = unittest.TestSuite()
53 for mod in (json, json.encoder, json.decoder):
54 suite.addTest(doctest.DocTestSuite(mod))
55 suite.addTest(TestPyTest('test_pyjson'))
56 suite.addTest(TestCTest('test_cjson'))
57
58 pkg_dir = os.path.dirname(__file__)
59 return support.load_package_tests(pkg_dir, loader, suite, pattern)