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