-
-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathformat_literals_test.py
More file actions
86 lines (80 loc) · 2.59 KB
/
format_literals_test.py
File metadata and controls
86 lines (80 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from __future__ import annotations
import pytest
from pyupgrade._main import _fix_tokens
@pytest.mark.parametrize(
's',
(
# Don't touch syntax errors
'"{0}"format(1)',
pytest.param("'{}'.format(1)", id='already upgraded'),
# Don't touch invalid format strings
"'{'.format(1)", "'}'.format(1)",
# Don't touch non-format strings
"x = ('{0} {1}',)\n",
# Don't touch non-incrementing integers
"'{0} {0}'.format(1)",
# Formats can be embedded in formats, leave these alone?
"'{0:<{1}}'.format(1, 4)",
# don't attempt to fix this, garbage in garbage out
"'{' '0}'.format(1)",
# comment looks like placeholder but is not!
'("{0}" # {1}\n"{2}").format(1, 2, 3)',
# don't touch f-strings (these are wrong but don't make it worse)
'f"{0}".format(a)',
# shouldn't touch the format spec
r'"{}\N{SNOWMAN}".format("")',
),
)
def test_format_literals_noop(s):
assert _fix_tokens(s) == s
@pytest.mark.parametrize(
('s', 'expected'),
(
# Simplest case
("'{0}'.format(1)", "'{}'.format(1)"),
("'{0:x}'.format(30)", "'{:x}'.format(30)"),
("x = '{0}'.format(1)", "x = '{}'.format(1)"),
# Multiline strings
("'''{0}\n{1}\n'''.format(1, 2)", "'''{}\n{}\n'''.format(1, 2)"),
# Multiple implicitly-joined strings
("'{0}' '{1}'.format(1, 2)", "'{}' '{}'.format(1, 2)"),
# Multiple implicitly-joined strings over lines
(
'print(\n'
" 'foo{0}'\n"
" 'bar{1}'.format(1, 2)\n"
')',
'print(\n'
" 'foo{}'\n"
" 'bar{}'.format(1, 2)\n"
')',
),
# Multiple implicitly-joind strings over lines with comments
(
'print(\n'
" 'foo{0}' # ohai\n"
" 'bar{1}'.format(1, 2)\n"
')',
'print(\n'
" 'foo{}' # ohai\n"
" 'bar{}'.format(1, 2)\n"
')',
),
# joined by backslash
(
'x = "foo {0}" \\\n'
' "bar {1}".format(1, 2)',
'x = "foo {}" \\\n'
' "bar {}".format(1, 2)',
),
# parenthesized string literals
('("{0}").format(1)', '("{}").format(1)'),
pytest.param(
r'"\N{snowman} {0}".format(1)',
r'"\N{snowman} {}".format(1)',
id='named escape sequence',
),
),
)
def test_format_literals(s, expected):
assert _fix_tokens(s) == expected