Skip to content

Commit 770299a

Browse files
committed
gh-101821: Cover ast.main function
1 parent d40a23c commit 770299a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Lib/test/test_ast.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from textwrap import dedent
1212

1313
from test import support
14+
from test.support import os_helper, script_helper
1415
from test.support.ast_helper import ASTTestMixin
1516

1617
def to_tuple(t):
@@ -2564,6 +2565,33 @@ def test_subinterpreter(self):
25642565
self.assertEqual(res, 0)
25652566

25662567

2568+
class ASTMainTests(unittest.TestCase):
2569+
"""Tests `ast.main()` function."""
2570+
2571+
def test_cli_empty_input(self):
2572+
expected = ast.dump(ast.parse("")) + "\n"
2573+
2574+
res, _ = script_helper.run_python_until_end("-m", "ast")
2575+
2576+
self.assertEqual(res.err, b"")
2577+
self.assertEqual(res.out.decode("utf8"), expected)
2578+
self.assertEqual(res.rc, 0)
2579+
2580+
def test_cli_file_input(self):
2581+
code = "print(1, 2, 3)"
2582+
expected = ast.dump(ast.parse(code), indent=3) + "\n"
2583+
2584+
with os_helper.temp_dir() as tmp_dir:
2585+
filename = os.path.join(tmp_dir, "test_module.py")
2586+
with open(filename, 'w', encoding='utf-8') as f:
2587+
f.write(code)
2588+
res, _ = script_helper.run_python_until_end("-m", "ast", filename)
2589+
2590+
self.assertEqual(res.err, b"")
2591+
self.assertEqual(res.out.decode("utf8"), expected)
2592+
self.assertEqual(res.rc, 0)
2593+
2594+
25672595
def main():
25682596
if __name__ != '__main__':
25692597
return

0 commit comments

Comments
 (0)