Skip to content
This repository was archived by the owner on Nov 3, 2023. It is now read-only.

Commit 8bfbf34

Browse files
yoseftNurdok
authored andcommitted
Added simple fstring parsing testcase (#425)
* Added simple fstring parsing testcase * Added test case for fstring with args
1 parent 145daec commit 8bfbf34

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

src/tests/parser_test.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,78 @@ def do_something(pos_param0, pos_param1, kw_param0="default"):
4949
assert str(function) == 'in public function `do_something`'
5050

5151

52+
def test_simple_fstring():
53+
"""Test parsing of a function with a simple fstring as a docstring."""
54+
# fstrings are not supported in Python 3.5
55+
if sys.version_info[0:2] == (3, 5):
56+
return
57+
58+
parser = Parser()
59+
code = CodeSnippet("""\
60+
def do_something(pos_param0, pos_param1, kw_param0="default"):
61+
f\"""Do something.\"""
62+
return None
63+
""")
64+
module = parser.parse(code, 'file_path')
65+
assert module.is_public
66+
assert module.dunder_all is None
67+
68+
function, = module.children
69+
assert function.name == 'do_something'
70+
assert function.decorators == []
71+
assert function.children == []
72+
assert function.docstring == 'f"""Do something."""'
73+
assert function.docstring.start == 2
74+
assert function.docstring.end == 2
75+
assert function.kind == 'function'
76+
assert function.parent == module
77+
assert function.start == 1
78+
assert function.end == 3
79+
assert function.error_lineno == 2
80+
assert function.source == code.getvalue()
81+
assert function.is_public
82+
assert str(function) == 'in public function `do_something`'
83+
84+
85+
def test_fstring_with_args():
86+
"""Test parsing of a function with an fstring with args as a docstring."""
87+
# fstrings are not supported in Python 3.5
88+
if sys.version_info[0:2] == (3, 5):
89+
return
90+
91+
parser = Parser()
92+
code = CodeSnippet("""\
93+
foo = "bar"
94+
bar = "baz"
95+
def do_something(pos_param0, pos_param1, kw_param0="default"):
96+
f\"""Do some {foo} and some {bar}.\"""
97+
return None
98+
""")
99+
module = parser.parse(code, 'file_path')
100+
assert module.is_public
101+
assert module.dunder_all is None
102+
103+
function, = module.children
104+
assert function.name == 'do_something'
105+
assert function.decorators == []
106+
assert function.children == []
107+
assert function.docstring == 'f"""Do some {foo} and some {bar}."""'
108+
assert function.docstring.start == 4
109+
assert function.docstring.end == 4
110+
assert function.kind == 'function'
111+
assert function.parent == module
112+
assert function.start == 3
113+
assert function.end == 5
114+
assert function.error_lineno == 4
115+
assert function.source == textwrap.dedent("""\
116+
def do_something(pos_param0, pos_param1, kw_param0="default"):
117+
f\"""Do some {foo} and some {bar}.\"""
118+
return None
119+
""")
120+
assert function.is_public
121+
assert str(function) == 'in public function `do_something`'
122+
123+
52124
def test_decorated_function():
53125
"""Test parsing of a simple function with a decorator."""
54126
parser = Parser()

0 commit comments

Comments
 (0)