Add --replace-single-quotes to replace single quotes with double ones

This commit is contained in:
Wenjun Si 2024-08-15 13:21:52 +08:00
parent 429455474b
commit 3310eb9734
3 changed files with 62 additions and 18 deletions

View file

@ -8,17 +8,20 @@ from pre_commit_hooks.string_fixer import main
TESTS = (
# Base cases
("''", "''", 0),
('""', "''", 1),
(r'"\'"', r'"\'"', 0),
(r'"\""', r'"\""', 0),
(r"'\"\"'", r"'\"\"'", 0),
("''", "''", False, 0),
("''", '""', True, 1),
('""', "''", False, 1),
('""', '""', True, 0),
(r'"\'"', r'"\'"', False, 0),
(r'"\""', r'"\""', False, 0),
(r"'\"\"'", r"'\"\"'", False, 0),
# String somewhere in the line
('x = "foo"', "x = 'foo'", 1),
('x = "foo"', "x = 'foo'", False, 1),
("x = 'foo'", 'x = "foo"', True, 1),
# Test escaped characters
(r'"\'"', r'"\'"', 0),
(r'"\'"', r'"\'"', False, 0),
# Docstring
('""" Foo """', '""" Foo """', 0),
('""" Foo """', '""" Foo """', False, 0),
(
textwrap.dedent(
"""
@ -34,23 +37,51 @@ TESTS = (
'\n
""",
),
False,
1,
),
('"foo""bar"', "'foo''bar'", 1),
(
textwrap.dedent(
"""
x = ' \\
foo \\
'\n
""",
),
textwrap.dedent(
"""
x = " \\
foo \\
"\n
""",
),
True,
1,
),
('"foo""bar"', "'foo''bar'", False, 1),
("'foo''bar'", '"foo""bar"', True, 1),
pytest.param(
"f'hello{\"world\"}'",
"f'hello{\"world\"}'",
False,
0,
id='ignore nested fstrings',
),
)
@pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS)
def test_rewrite(input_s, output, expected_retval, tmpdir):
@pytest.mark.parametrize(
('input_s', 'output', 'reversed_case', 'expected_retval'), TESTS
)
def test_rewrite(input_s, output, reversed_case, expected_retval, tmpdir):
path = tmpdir.join('file.py')
path.write(input_s)
retval = main([str(path)])
argv = [str(path)]
if reversed_case:
argv.append("--replace-single-quotes")
retval = main(argv)
assert path.read() == output
assert retval == expected_retval