-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
format_literals_test.py
118 lines (107 loc) · 3.42 KB
/
format_literals_test.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import pytest
from pyupgrade._main import _fix_tokens
from pyupgrade._main import parse_format
from pyupgrade._main import unparse_parsed_string
@pytest.mark.parametrize(
's',
(
'', 'foo', '{}', '{0}', '{named}', '{!r}', '{:>5}', '{{', '}}',
'{0!s:15}',
),
)
def test_roundtrip_text(s):
assert unparse_parsed_string(parse_format(s)) == s
def test_parse_format_starts_with_named():
# technically not possible since our string always starts with quotes
assert parse_format(r'\N{snowman} hi {0} hello') == (
(r'\N{snowman} hi ', '0', '', None),
(' hello', None, None, None),
)
@pytest.mark.parametrize(
('s', 'expected'),
(
('{:}', '{}'),
('{0:}', '{0}'),
('{0!r:}', '{0!r}'),
),
)
def test_intentionally_not_round_trip(s, expected):
# Our unparse simplifies empty parts, whereas stdlib allows them
ret = unparse_parsed_string(parse_format(s))
assert ret == expected
@pytest.mark.parametrize(
's',
(
# Don't touch syntax errors
'"{0}"format(1)',
# Don't touch py27 format strings
"'{}'.format(1)",
# 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)',
),
)
def test_format_literals_noop(s):
assert _fix_tokens(s, min_version=(2, 7)) == 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, min_version=(2, 7)) == expected