1 | n/a | import os |
---|
2 | n/a | import sys |
---|
3 | n/a | import textwrap |
---|
4 | n/a | import unittest |
---|
5 | n/a | import subprocess |
---|
6 | n/a | from test import support |
---|
7 | n/a | from test.support.script_helper import assert_python_ok |
---|
8 | n/a | |
---|
9 | n/a | |
---|
10 | n/a | class TestTool(unittest.TestCase): |
---|
11 | n/a | data = """ |
---|
12 | n/a | |
---|
13 | n/a | [["blorpie"],[ "whoops" ] , [ |
---|
14 | n/a | ],\t"d-shtaeou",\r"d-nthiouh", |
---|
15 | n/a | "i-vhbjkhnth", {"nifty":87}, {"morefield" :\tfalse,"field" |
---|
16 | n/a | :"yes"} ] |
---|
17 | n/a | """ |
---|
18 | n/a | |
---|
19 | n/a | expect_without_sort_keys = textwrap.dedent("""\ |
---|
20 | n/a | [ |
---|
21 | n/a | [ |
---|
22 | n/a | "blorpie" |
---|
23 | n/a | ], |
---|
24 | n/a | [ |
---|
25 | n/a | "whoops" |
---|
26 | n/a | ], |
---|
27 | n/a | [], |
---|
28 | n/a | "d-shtaeou", |
---|
29 | n/a | "d-nthiouh", |
---|
30 | n/a | "i-vhbjkhnth", |
---|
31 | n/a | { |
---|
32 | n/a | "nifty": 87 |
---|
33 | n/a | }, |
---|
34 | n/a | { |
---|
35 | n/a | "field": "yes", |
---|
36 | n/a | "morefield": false |
---|
37 | n/a | } |
---|
38 | n/a | ] |
---|
39 | n/a | """) |
---|
40 | n/a | |
---|
41 | n/a | expect = textwrap.dedent("""\ |
---|
42 | n/a | [ |
---|
43 | n/a | [ |
---|
44 | n/a | "blorpie" |
---|
45 | n/a | ], |
---|
46 | n/a | [ |
---|
47 | n/a | "whoops" |
---|
48 | n/a | ], |
---|
49 | n/a | [], |
---|
50 | n/a | "d-shtaeou", |
---|
51 | n/a | "d-nthiouh", |
---|
52 | n/a | "i-vhbjkhnth", |
---|
53 | n/a | { |
---|
54 | n/a | "nifty": 87 |
---|
55 | n/a | }, |
---|
56 | n/a | { |
---|
57 | n/a | "morefield": false, |
---|
58 | n/a | "field": "yes" |
---|
59 | n/a | } |
---|
60 | n/a | ] |
---|
61 | n/a | """) |
---|
62 | n/a | |
---|
63 | n/a | def test_stdin_stdout(self): |
---|
64 | n/a | with subprocess.Popen( |
---|
65 | n/a | (sys.executable, '-m', 'json.tool'), |
---|
66 | n/a | stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc: |
---|
67 | n/a | out, err = proc.communicate(self.data.encode()) |
---|
68 | n/a | self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) |
---|
69 | n/a | self.assertEqual(err, None) |
---|
70 | n/a | |
---|
71 | n/a | def _create_infile(self): |
---|
72 | n/a | infile = support.TESTFN |
---|
73 | n/a | with open(infile, "w") as fp: |
---|
74 | n/a | self.addCleanup(os.remove, infile) |
---|
75 | n/a | fp.write(self.data) |
---|
76 | n/a | return infile |
---|
77 | n/a | |
---|
78 | n/a | def test_infile_stdout(self): |
---|
79 | n/a | infile = self._create_infile() |
---|
80 | n/a | rc, out, err = assert_python_ok('-m', 'json.tool', infile) |
---|
81 | n/a | self.assertEqual(rc, 0) |
---|
82 | n/a | self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) |
---|
83 | n/a | self.assertEqual(err, b'') |
---|
84 | n/a | |
---|
85 | n/a | def test_infile_outfile(self): |
---|
86 | n/a | infile = self._create_infile() |
---|
87 | n/a | outfile = support.TESTFN + '.out' |
---|
88 | n/a | rc, out, err = assert_python_ok('-m', 'json.tool', infile, outfile) |
---|
89 | n/a | self.addCleanup(os.remove, outfile) |
---|
90 | n/a | with open(outfile, "r") as fp: |
---|
91 | n/a | self.assertEqual(fp.read(), self.expect) |
---|
92 | n/a | self.assertEqual(rc, 0) |
---|
93 | n/a | self.assertEqual(out, b'') |
---|
94 | n/a | self.assertEqual(err, b'') |
---|
95 | n/a | |
---|
96 | n/a | def test_help_flag(self): |
---|
97 | n/a | rc, out, err = assert_python_ok('-m', 'json.tool', '-h') |
---|
98 | n/a | self.assertEqual(rc, 0) |
---|
99 | n/a | self.assertTrue(out.startswith(b'usage: ')) |
---|
100 | n/a | self.assertEqual(err, b'') |
---|
101 | n/a | |
---|
102 | n/a | def test_sort_keys_flag(self): |
---|
103 | n/a | infile = self._create_infile() |
---|
104 | n/a | rc, out, err = assert_python_ok('-m', 'json.tool', '--sort-keys', infile) |
---|
105 | n/a | self.assertEqual(rc, 0) |
---|
106 | n/a | self.assertEqual(out.splitlines(), |
---|
107 | n/a | self.expect_without_sort_keys.encode().splitlines()) |
---|
108 | n/a | self.assertEqual(err, b'') |
---|