Mercurial > cpython-lru_cache-weakref
annotate Lib/json/tests/__init__.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 5b0fecd2eba0 |
| children |
| rev | line source |
|---|---|
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
1 import os |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
2 import sys |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
3 import json |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
4 import doctest |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
5 import unittest |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
6 |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
7 from test import test_support |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
8 |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
9 # import json with and without accelerations |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
10 cjson = test_support.import_fresh_module('json', fresh=['_json']) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
11 pyjson = test_support.import_fresh_module('json', blocked=['_json']) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
12 |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
13 # create two base classes that will be used by the other tests |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
14 class PyTest(unittest.TestCase): |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
15 json = pyjson |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
16 loads = staticmethod(pyjson.loads) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
17 dumps = staticmethod(pyjson.dumps) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
18 |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
19 @unittest.skipUnless(cjson, 'requires _json') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
20 class CTest(unittest.TestCase): |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
21 if cjson is not None: |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
22 json = cjson |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
23 loads = staticmethod(cjson.loads) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
24 dumps = staticmethod(cjson.dumps) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
25 |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
26 # test PyTest and CTest checking if the functions come from the right module |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
27 class TestPyTest(PyTest): |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
28 def test_pyjson(self): |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
29 self.assertEqual(self.json.scanner.make_scanner.__module__, |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
30 'json.scanner') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
31 self.assertEqual(self.json.decoder.scanstring.__module__, |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
32 'json.decoder') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
33 self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__, |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
34 'json.encoder') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
35 |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
36 class TestCTest(CTest): |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
37 def test_cjson(self): |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
38 self.assertEqual(self.json.scanner.make_scanner.__module__, '_json') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
39 self.assertEqual(self.json.decoder.scanstring.__module__, '_json') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
40 self.assertEqual(self.json.encoder.c_make_encoder.__module__, '_json') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
41 self.assertEqual(self.json.encoder.encode_basestring_ascii.__module__, |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
42 '_json') |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
43 |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
44 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
45 here = os.path.dirname(__file__) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
46 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
47 def test_suite(): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
48 suite = additional_tests() |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
49 loader = unittest.TestLoader() |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
50 for fn in os.listdir(here): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
51 if fn.startswith("test") and fn.endswith(".py"): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
52 modname = "json.tests." + fn[:-3] |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
53 __import__(modname) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
54 module = sys.modules[modname] |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
55 suite.addTests(loader.loadTestsFromModule(module)) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
56 return suite |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
57 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
58 def additional_tests(): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
59 suite = unittest.TestSuite() |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
60 for mod in (json, json.encoder, json.decoder): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
61 suite.addTest(doctest.DocTestSuite(mod)) |
|
70084
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
62 suite.addTest(TestPyTest('test_pyjson')) |
|
5b0fecd2eba0
#5723: Improve json tests to be executed with and without accelerations.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
46685
diff
changeset
|
63 suite.addTest(TestCTest('test_cjson')) |
|
46676
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
64 return suite |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
65 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
66 def main(): |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
67 suite = test_suite() |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
68 runner = unittest.TextTestRunner() |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
69 runner.run(suite) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
70 |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
71 if __name__ == '__main__': |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
72 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) |
|
4ded35754cb7
Add the 'json' package. Code taken from simplejson 1.9 and contributed by Bob
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
73 main() |
