forked from dflook/python-minifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_fstring.py
More file actions
106 lines (79 loc) · 3.09 KB
/
test_fstring.py
File metadata and controls
106 lines (79 loc) · 3.09 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import ast
import sys
import pytest
from python_minifier import unparse
from python_minifier.ast_compare import compare_ast
@pytest.mark.parametrize(
'statement', [
'f"{1=!r:.4}"',
'f"{1=:.4}"',
'f"{1=!s:.4}"',
'f"{1}"',
'f"{1=}"',
'f"{1=!s}"',
'f"{1=!a}"'
]
)
def test_fstring_statement(statement):
if sys.version_info < (3, 8):
pytest.skip('f-string debug specifier added in python 3.8')
assert unparse(ast.parse(statement)) == statement
def test_pep0701():
if sys.version_info < (3, 12):
pytest.skip('f-string syntax is bonkers before python 3.12')
statement = 'f"{f"{f"{f"{"hello"}"}"}"}"'
assert unparse(ast.parse(statement)) == statement
statement = 'f"This is the playlist: {", ".join([])}"'
assert unparse(ast.parse(statement)) == statement
statement = 'f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"'
assert unparse(ast.parse(statement)) == statement
statement = '''
f"This is the playlist: {", ".join([
'Take me back to Eden', # My, my, those eyes like fire
'Alkaline', # Not acid nor alkaline
'Ascensionism' # Take to the broken skies at last
])}"
'''
assert unparse(ast.parse(statement)) == 'f"This is the playlist: {", ".join(["Take me back to Eden","Alkaline","Ascensionism"])}"'
# statement = '''print(f"This is the playlist: {"\N{BLACK HEART SUIT}".join(songs)}")'''
# assert unparse(ast.parse(statement)) == statement
statement = '''f"Magic wand: {bag["wand"]}"'''
assert unparse(ast.parse(statement)) == statement
statement = """
f'''A complex trick: {
bag['bag'] # recursive bags!
}'''
"""
assert unparse(ast.parse(statement)) == 'f"A complex trick: {bag["bag"]}"'
statement = '''f"These are the things: {", ".join(things)}"'''
assert unparse(ast.parse(statement)) == statement
statement = '''f"{source.removesuffix(".py")}.c: $(srcdir)/{source}"'''
assert unparse(ast.parse(statement)) == statement
statement = '''f"{f"{f"infinite"}"}"+' '+f"{f"nesting!!!"}"'''
assert unparse(ast.parse(statement)) == statement
statement = '''f"{"\\n".join(a)}"'''
assert unparse(ast.parse(statement)) == statement
statement = '''f"{f"{f"{f"{f"{f"{1+1}"}"}"}"}"}"'''
assert unparse(ast.parse(statement)) == statement
statement = '''f"{"":*^{1:{1}}}"'''
assert unparse(ast.parse(statement)) == statement
# statement = '''f"{"":*^{1:{1:{1}}}}"'''
# assert unparse(ast.parse(statement)) == statement
# SyntaxError: f-string: expressions nested too deeply
statement = '''f"___{
x
}___"'''
assert unparse(ast.parse(statement)) == '''f"___{x}___"'''
statement = '''f"Useless use of lambdas: {(lambda x:x*2)}"'''
assert unparse(ast.parse(statement)) == statement
def test_fstring_empty_str():
if sys.version_info < (3, 6):
pytest.skip('f-string expressions not allowed in python < 3.6')
source = r'''
f"""\
{fg_br}"""
'''
print(source)
expected_ast = ast.parse(source)
actual_ast = unparse(expected_ast)
compare_ast(expected_ast, ast.parse(actual_ast))