Skip to content

Commit 9c94d26

Browse files
authored
Add a test for f-string self-documenting expressions. (#176)
1 parent 75f50d3 commit 9c94d26

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

tests/transformer/test_fstring.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from RestrictedPython import compile_restricted_exec
22
from RestrictedPython._compat import IS_PY36_OR_GREATER
3+
from RestrictedPython._compat import IS_PY38_OR_GREATER
4+
from RestrictedPython.PrintCollector import PrintCollector
35

46
import pytest
57

@@ -33,3 +35,28 @@ def test_visit_invalid_variable_name():
3335
assert result.errors == (
3436
'Line 1: "__init__" is an invalid variable name because it starts with "_"', # NOQA: E501
3537
)
38+
39+
40+
f_string_self_documenting_expressions_example = """
41+
from datetime import date
42+
43+
user = 'eric_idle'
44+
member_since = date(1975, 7, 31)
45+
print(f'{user=} {member_since=}')
46+
"""
47+
48+
49+
@pytest.mark.skipif(
50+
not IS_PY38_OR_GREATER,
51+
reason="f-string self-documenting expressions added in Python 3.8.",
52+
)
53+
def test_f_string_self_documenting_expressions():
54+
"""Checks if f-string self-documenting expressions is checked."""
55+
result = compile_restricted_exec(
56+
f_string_self_documenting_expressions_example,
57+
)
58+
assert result.errors == ()
59+
60+
glb = {'_print_': PrintCollector, '_getattr_': None}
61+
exec(result.code, glb)
62+
assert glb['_print']() == "user='eric_idle' member_since=datetime.date(1975, 7, 31)\n" # NOQA: E501

0 commit comments

Comments
 (0)