◐ Shell
clean mode source ↗

bpo-27413: add --no-ensure-ascii argument to json.tool by dhimmel · Pull Request #201 · python/cpython

Expand Up @@ -2,7 +2,7 @@ import sys import textwrap import unittest import subprocess from subprocess import Popen, PIPE from test import support from test.support.script_helper import assert_python_ok
Expand Down Expand Up @@ -61,12 +61,11 @@ class TestTool(unittest.TestCase): """)
def test_stdin_stdout(self): with subprocess.Popen( (sys.executable, '-m', 'json.tool'), stdin=subprocess.PIPE, stdout=subprocess.PIPE) as proc: args = sys.executable, '-m', 'json.tool' with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: out, err = proc.communicate(self.data.encode()) self.assertEqual(out.splitlines(), self.expect.encode().splitlines()) self.assertEqual(err, None) self.assertEqual(err, b'')
def _create_infile(self): infile = support.TESTFN Expand Down Expand Up @@ -106,3 +105,42 @@ def test_sort_keys_flag(self): self.assertEqual(out.splitlines(), self.expect_without_sort_keys.encode().splitlines()) self.assertEqual(err, b'')
def test_indent(self): json_stdin = b'[1, 2]' expect = textwrap.dedent('''\ [ 1, 2 ] ''').encode() args = sys.executable, '-m', 'json.tool', '--indent', '2' with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: json_stdout, err = proc.communicate(json_stdin) self.assertEqual(expect.splitlines(), json_stdout.splitlines()) self.assertEqual(err, b'')
def test_no_indent(self): json_stdin = b'[1, 2]' args = sys.executable, '-m', 'json.tool', '--no-indent' with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: json_stdout, err = proc.communicate(json_stdin) self.assertEqual(json_stdin.splitlines(), json_stdout.splitlines()) self.assertEqual(err, b'')
def test_ensure_ascii(self): json_stdin = '"\xA7 \N{snake} \u03B4 \U0001D037"'.encode() expect = b'"\\u00a7 \\ud83d\\udc0d \\u03b4 \\ud834\\udc37"\n' args = sys.executable, '-m', 'json.tool' with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: json_stdout, err = proc.communicate(json_stdin) self.assertEqual(expect.splitlines(), json_stdout.splitlines()) self.assertEqual(err, b'')
def test_no_ensure_ascii(self): json_stdin = '"\xA7 \N{snake} \u03B4 \U0001D037"'.encode() args = sys.executable, '-m', 'json.tool', '--no-ensure-ascii' with Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) as proc: json_stdout, err = proc.communicate(json_stdin) self.assertEqual(json_stdin.splitlines(), json_stdout.splitlines()) self.assertEqual(err, b'')