]>
Commit | Line | Data |
---|---|---|
3257aa99 DM |
1 | import os\r |
2 | import sys\r | |
3 | import textwrap\r | |
4 | import unittest\r | |
5 | import subprocess\r | |
6 | from test import test_support\r | |
7 | from test.script_helper import assert_python_ok\r | |
8 | \r | |
9 | class TestTool(unittest.TestCase):\r | |
10 | data = """\r | |
11 | \r | |
12 | [["blorpie"],[ "whoops" ] , [\r | |
13 | ],\t"d-shtaeou",\r"d-nthiouh",\r | |
14 | "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field"\r | |
15 | :"yes"} ]\r | |
16 | """\r | |
17 | \r | |
18 | expect = textwrap.dedent("""\\r | |
19 | [\r | |
20 | [\r | |
21 | "blorpie"\r | |
22 | ],\r | |
23 | [\r | |
24 | "whoops"\r | |
25 | ],\r | |
26 | [],\r | |
27 | "d-shtaeou",\r | |
28 | "d-nthiouh",\r | |
29 | "i-vhbjkhnth",\r | |
30 | {\r | |
31 | "nifty": 87\r | |
32 | },\r | |
33 | {\r | |
34 | "field": "yes",\r | |
35 | "morefield": false\r | |
36 | }\r | |
37 | ]\r | |
38 | """)\r | |
39 | \r | |
40 | def test_stdin_stdout(self):\r | |
41 | proc = subprocess.Popen(\r | |
42 | (sys.executable, '-m', 'json.tool'),\r | |
43 | stdin=subprocess.PIPE, stdout=subprocess.PIPE)\r | |
44 | out, err = proc.communicate(self.data.encode())\r | |
45 | self.assertEqual(out.splitlines(), self.expect.encode().splitlines())\r | |
46 | self.assertEqual(err, None)\r | |
47 | \r | |
48 | def _create_infile(self):\r | |
49 | infile = test_support.TESTFN\r | |
50 | with open(infile, "w") as fp:\r | |
51 | self.addCleanup(os.remove, infile)\r | |
52 | fp.write(self.data)\r | |
53 | return infile\r | |
54 | \r | |
55 | def test_infile_stdout(self):\r | |
56 | infile = self._create_infile()\r | |
57 | rc, out, err = assert_python_ok('-m', 'json.tool', infile)\r | |
58 | self.assertEqual(out.splitlines(), self.expect.encode().splitlines())\r | |
59 | self.assertEqual(err, b'')\r | |
60 | \r | |
61 | def test_infile_outfile(self):\r | |
62 | infile = self._create_infile()\r | |
63 | outfile = test_support.TESTFN + '.out'\r | |
64 | rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile)\r | |
65 | self.addCleanup(os.remove, outfile)\r | |
66 | with open(outfile, "r") as fp:\r | |
67 | self.assertEqual(fp.read(), self.expect)\r | |
68 | self.assertEqual(out, b'')\r | |
69 | self.assertEqual(err, b'')\r |