|
11 | 11 | from textwrap import dedent
|
12 | 12 |
|
13 | 13 | from test import support
|
| 14 | +from test.support import os_helper, script_helper |
14 | 15 | from test.support.ast_helper import ASTTestMixin
|
15 | 16 |
|
16 | 17 | def to_tuple(t):
|
@@ -2564,6 +2565,33 @@ def test_subinterpreter(self):
|
2564 | 2565 | self.assertEqual(res, 0)
|
2565 | 2566 |
|
2566 | 2567 |
|
| 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 | + |
2567 | 2595 | def main():
|
2568 | 2596 | if __name__ != '__main__':
|
2569 | 2597 | return
|
|
0 commit comments