|
1 |
| -"""Tests for the unparse.py script in the Tools/parser directory.""" |
| 1 | +"""Tests for ast.unparse.""" |
2 | 2 |
|
3 | 3 | import unittest
|
4 | 4 | import test.support
|
@@ -625,6 +625,78 @@ def test_star_expr_assign_target_multiple(self):
|
625 | 625 | self.check_src_roundtrip("a, b = [c, d] = e, f = g")
|
626 | 626 |
|
627 | 627 |
|
| 628 | +class ManualASTCreationTestCase(unittest.TestCase): |
| 629 | + """Test that AST nodes created without a type_params field unparse correctly.""" |
| 630 | + |
| 631 | + def test_class(self): |
| 632 | + node = ast.ClassDef(name="X", bases=[], keywords=[], body=[ast.Pass()], decorator_list=[]) |
| 633 | + ast.fix_missing_locations(node) |
| 634 | + self.assertEqual(ast.unparse(node), "class X:\n pass") |
| 635 | + |
| 636 | + def test_class_with_type_params(self): |
| 637 | + node = ast.ClassDef(name="X", bases=[], keywords=[], body=[ast.Pass()], decorator_list=[], |
| 638 | + type_params=[ast.TypeVar("T")]) |
| 639 | + ast.fix_missing_locations(node) |
| 640 | + self.assertEqual(ast.unparse(node), "class X[T]:\n pass") |
| 641 | + |
| 642 | + def test_function(self): |
| 643 | + node = ast.FunctionDef( |
| 644 | + name="f", |
| 645 | + args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), |
| 646 | + body=[ast.Pass()], |
| 647 | + decorator_list=[], |
| 648 | + returns=None, |
| 649 | + ) |
| 650 | + ast.fix_missing_locations(node) |
| 651 | + self.assertEqual(ast.unparse(node), "def f():\n pass") |
| 652 | + |
| 653 | + def test_function_with_type_params(self): |
| 654 | + node = ast.FunctionDef( |
| 655 | + name="f", |
| 656 | + args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), |
| 657 | + body=[ast.Pass()], |
| 658 | + decorator_list=[], |
| 659 | + returns=None, |
| 660 | + type_params=[ast.TypeVar("T")], |
| 661 | + ) |
| 662 | + ast.fix_missing_locations(node) |
| 663 | + self.assertEqual(ast.unparse(node), "def f[T]():\n pass") |
| 664 | + |
| 665 | + def test_function_with_type_params_and_bound(self): |
| 666 | + node = ast.FunctionDef( |
| 667 | + name="f", |
| 668 | + args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), |
| 669 | + body=[ast.Pass()], |
| 670 | + decorator_list=[], |
| 671 | + returns=None, |
| 672 | + type_params=[ast.TypeVar("T", bound=ast.Name("int"))], |
| 673 | + ) |
| 674 | + ast.fix_missing_locations(node) |
| 675 | + self.assertEqual(ast.unparse(node), "def f[T: int]():\n pass") |
| 676 | + |
| 677 | + def test_async_function(self): |
| 678 | + node = ast.AsyncFunctionDef( |
| 679 | + name="f", |
| 680 | + args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), |
| 681 | + body=[ast.Pass()], |
| 682 | + decorator_list=[], |
| 683 | + returns=None, |
| 684 | + ) |
| 685 | + ast.fix_missing_locations(node) |
| 686 | + self.assertEqual(ast.unparse(node), "async def f():\n pass") |
| 687 | + |
| 688 | + def test_async_function_with_type_params(self): |
| 689 | + node = ast.AsyncFunctionDef( |
| 690 | + name="f", |
| 691 | + args=ast.arguments(posonlyargs=[], args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), |
| 692 | + body=[ast.Pass()], |
| 693 | + decorator_list=[], |
| 694 | + returns=None, |
| 695 | + type_params=[ast.TypeVar("T")], |
| 696 | + ) |
| 697 | + ast.fix_missing_locations(node) |
| 698 | + self.assertEqual(ast.unparse(node), "async def f[T]():\n pass") |
| 699 | + |
628 | 700 |
|
629 | 701 | class DirectoryTestCase(ASTTestCase):
|
630 | 702 | """Test roundtrip behaviour on all files in Lib and Lib/test."""
|
|
0 commit comments