mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-04-04 11:16:53 +00:00
Use the tokenizer for great success
This commit is contained in:
parent
5207d1f29a
commit
2983d4478f
2 changed files with 63 additions and 67 deletions
|
|
@ -2,79 +2,49 @@ from __future__ import absolute_import
|
|||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import textwrap
|
||||
|
||||
import pytest
|
||||
|
||||
from pre_commit_hooks.string_fixer import main
|
||||
|
||||
TESTS = (
|
||||
# Base cases
|
||||
(
|
||||
"''",
|
||||
"''",
|
||||
0
|
||||
),
|
||||
(
|
||||
'""',
|
||||
"''",
|
||||
1
|
||||
),
|
||||
(
|
||||
r'"\'"',
|
||||
r'"\'"',
|
||||
0
|
||||
),
|
||||
(
|
||||
r'"\""',
|
||||
r'"\""',
|
||||
0
|
||||
),
|
||||
(
|
||||
r"'\"\"'",
|
||||
r"'\"\"'",
|
||||
0
|
||||
),
|
||||
("''", "''", 0),
|
||||
('""', "''", 1),
|
||||
(r'"\'"', r'"\'"', 0),
|
||||
(r'"\""', r'"\""', 0),
|
||||
(r"'\"\"'", r"'\"\"'", 0),
|
||||
# String somewhere in the line
|
||||
(
|
||||
'x = "foo"',
|
||||
"x = 'foo'",
|
||||
1
|
||||
),
|
||||
('x = "foo"', "x = 'foo'", 1),
|
||||
# Test escaped characters
|
||||
(
|
||||
r'"\'"',
|
||||
r'"\'"',
|
||||
0
|
||||
),
|
||||
(r'"\'"', r'"\'"', 0),
|
||||
# Docstring
|
||||
('""" Foo """', '""" Foo """', 0),
|
||||
(
|
||||
'""" Foo """',
|
||||
'""" Foo """',
|
||||
0
|
||||
),
|
||||
# Fuck it, won't even try to fix
|
||||
(
|
||||
"""
|
||||
x = " \\n
|
||||
foo \\n
|
||||
textwrap.dedent("""
|
||||
x = " \\
|
||||
foo \\
|
||||
"\n
|
||||
""",
|
||||
"""
|
||||
x = " \\n
|
||||
foo \\n
|
||||
"\n
|
||||
""",
|
||||
0
|
||||
"""),
|
||||
textwrap.dedent("""
|
||||
x = ' \\
|
||||
foo \\
|
||||
'\n
|
||||
"""),
|
||||
1,
|
||||
),
|
||||
('"foo""bar"', "'foo''bar'", 1),
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(('input_s', 'expected_output', 'expected_retval'), TESTS)
|
||||
def test_rewrite(input_s, expected_output, expected_retval, tmpdir):
|
||||
@pytest.mark.parametrize(('input_s', 'output', 'expected_retval'), TESTS)
|
||||
def test_rewrite(input_s, output, expected_retval, tmpdir):
|
||||
tmpfile = tmpdir.join('file.txt')
|
||||
|
||||
with open(tmpfile.strpath, 'w') as f:
|
||||
f.write(input_s)
|
||||
|
||||
retval = main([tmpfile.strpath])
|
||||
assert tmpfile.read() == expected_output
|
||||
assert tmpfile.read() == output
|
||||
assert retval == expected_retval
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue