Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

gh-131178: Add tests for ast command-line interface #133329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
May 4, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Accept suggestions
  • Loading branch information
donBarbos committed May 3, 2025
commit de328afc20b10ef45b8cd85488c91d0419c4d49a
21 changes: 10 additions & 11 deletions 21 Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3193,8 +3193,7 @@ def text_normalize(string):
return textwrap.dedent(string).strip()

def set_source(self, content):
with open(self.filename, 'w') as f:
f.write(self.text_normalize(content))
Path(self.filename).write_text(self.text_normalize(content))

def invoke_ast(self, *flags):
output = StringIO()
Expand All @@ -3207,16 +3206,16 @@ def check_output(self, source, expect, *flags):
self.set_source(source)
res = self.invoke_ast(*flags)
expect = self.text_normalize(expect)
self.assertListEqual(res.splitlines(), expect.splitlines())
self.assertEqual(res, expect)

def test_invocation(self):
# test various combinations of parameters
base_flags = [
base_flags = (
('-m=exec', '--mode=exec'),
('--no-type-comments', '--no-type-comments'),
('-a', '--include-attributes'),
('-i=4', '--indent=4'),
]
)
self.set_source('''
print(1, 2, 3)
def f(x):
Expand Down Expand Up @@ -3250,15 +3249,15 @@ def test_mode_flag(self):
Constant(value=2),
Constant(value=3)]))])
'''
for flag in ['-m=exec', '--mode=exec']:
for flag in ('-m=exec', '--mode=exec'):
self.check_output(source, expect, flag)
source = 'pass'
expect = '''
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
Interactive(
body=[
Pass()])
'''
for flag in ['-m=single', '--mode=single']:
for flag in ('-m=single', '--mode=single'):
self.check_output(source, expect, flag)
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
source = 'print(1, 2, 3)'
expect = '''
Expand All @@ -3270,7 +3269,7 @@ def test_mode_flag(self):
Constant(value=2),
Constant(value=3)]))
'''
for flag in ['-m=eval', '--mode=eval']:
for flag in ('-m=eval', '--mode=eval'):
self.check_output(source, expect, flag)
donBarbos marked this conversation as resolved.
Show resolved Hide resolved
source = '(int, str) -> list[int]'
expect = '''
Expand All @@ -3283,7 +3282,7 @@ def test_mode_flag(self):
slice=Name(id='int', ctx=Load()),
ctx=Load()))
'''
for flag in ['-m=func_type', '--mode=func_type']:
for flag in ('-m=func_type', '--mode=func_type'):
self.check_output(source, expect, flag)
donBarbos marked this conversation as resolved.
Show resolved Hide resolved

def test_no_type_comments_flag(self):
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -3312,7 +3311,7 @@ def test_include_attributes_flag(self):
end_lineno=1,
end_col_offset=4)])
'''
for flag in ['-a', '--include-attributes']:
for flag in ('-a', '--include-attributes'):
self.check_output(source, expect, flag)
donBarbos marked this conversation as resolved.
Show resolved Hide resolved

def test_indent_flag(self):
Expand All @@ -3323,7 +3322,7 @@ def test_indent_flag(self):
body=[
Pass()])
'''
for flag in ['-i=0', '--indent=0']:
for flag in ('-i=0', '--indent=0'):
self.check_output(source, expect, flag)
donBarbos marked this conversation as resolved.
Show resolved Hide resolved


Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.