annotate Lib/json/tests/test_tool.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 4102c1a02417
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
80635
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
1 import os
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
2 import sys
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
3 import textwrap
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
4 import unittest
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
5 import subprocess
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
6 from test import test_support
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
7 from test.script_helper import assert_python_ok
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
8
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
9 class TestTool(unittest.TestCase):
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
10 data = """
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
11
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
12 [["blorpie"],[ "whoops" ] , [
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
13 ],\t"d-shtaeou",\r"d-nthiouh",
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
14 "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
15 :"yes"} ]
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
16 """
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
17
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
18 expect = textwrap.dedent("""\
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
19 [
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
20 [
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
21 "blorpie"
80639
fa277ab588de #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80635
diff changeset
22 ],
80635
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
23 [
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
24 "whoops"
80639
fa277ab588de #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80635
diff changeset
25 ],
fa277ab588de #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80635
diff changeset
26 [],
fa277ab588de #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80635
diff changeset
27 "d-shtaeou",
fa277ab588de #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80635
diff changeset
28 "d-nthiouh",
fa277ab588de #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80635
diff changeset
29 "i-vhbjkhnth",
80635
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
30 {
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
31 "nifty": 87
80639
fa277ab588de #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80635
diff changeset
32 },
80635
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
33 {
80639
fa277ab588de #16476: Fix json.tool to avoid including trailing whitespace.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80635
diff changeset
34 "field": "yes",
80635
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
35 "morefield": false
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
36 }
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
37 ]
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
38 """)
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
39
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
40 def test_stdin_stdout(self):
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
41 proc = subprocess.Popen(
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
42 (sys.executable, '-m', 'json.tool'),
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
43 stdin=subprocess.PIPE, stdout=subprocess.PIPE)
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
44 out, err = proc.communicate(self.data.encode())
80644
4102c1a02417 #16549: fix test failures on Windows.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80639
diff changeset
45 self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
80635
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
46 self.assertEqual(err, None)
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
47
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
48 def _create_infile(self):
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
49 infile = test_support.TESTFN
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
50 with open(infile, "w") as fp:
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
51 self.addCleanup(os.remove, infile)
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
52 fp.write(self.data)
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
53 return infile
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
54
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
55 def test_infile_stdout(self):
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
56 infile = self._create_infile()
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
57 rc, out, err = assert_python_ok('-m', 'json.tool', infile)
80644
4102c1a02417 #16549: fix test failures on Windows.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 80639
diff changeset
58 self.assertEqual(out.splitlines(), self.expect.encode().splitlines())
80635
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
59 self.assertEqual(err, b'')
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
60
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
61 def test_infile_outfile(self):
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
62 infile = self._create_infile()
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
63 outfile = test_support.TESTFN + '.out'
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
64 rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile)
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
65 self.addCleanup(os.remove, outfile)
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
66 with open(outfile, "r") as fp:
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
67 self.assertEqual(fp.read(), self.expect)
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
68 self.assertEqual(out, b'')
08e5973fdc7e #16549: Add tests for json.tools. Initial patch by Berker Peksag and Serhiy Storchaka.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
diff changeset
69 self.assertEqual(err, b'')