| 56 | self.assertAstMatches(node, expected_node_src) |
| 57 | |
| 58 | def test_load_ast(self): |
| 59 | node = gast.FunctionDef( |
| 60 | name="f", |
| 61 | args=gast.arguments( |
| 62 | args=[gast.Name("a", ctx=gast.Param(), annotation=None, type_comment=None)], |
| 63 | posonlyargs=[], |
| 64 | vararg=None, |
| 65 | kwonlyargs=[], |
| 66 | kw_defaults=[], |
| 67 | kwarg=None, |
| 68 | defaults=[], |
| 69 | ), |
| 70 | body=[ |
| 71 | gast.Return( |
| 72 | gast.BinOp( |
| 73 | op=gast.Add(), |
| 74 | left=gast.Name("a", ctx=gast.Load(), annotation=None, type_comment=None), |
| 75 | right=gast.Constant(1, kind=None), |
| 76 | ) |
| 77 | ) |
| 78 | ], |
| 79 | decorator_list=[], |
| 80 | returns=None, |
| 81 | type_comment=None, |
| 82 | **{"type_params": []} if gast_util.get_gast_version() >= Version("0.5.5") else {}, |
| 83 | ) |
| 84 | |
| 85 | module, source, _ = loader.load_ast(node) |
| 86 | |
| 87 | expected_node_src = """ |
| 88 | # coding=utf-8 |
| 89 | def f(a): |
| 90 | return (a + 1) |
| 91 | """ |
| 92 | expected_node_src = textwrap.dedent(expected_node_src) |
| 93 | |
| 94 | self.assertAstMatches(node, source) |
| 95 | self.assertAstMatches(node, expected_node_src) |
| 96 | |
| 97 | self.assertEqual(2, module.f(1)) |
| 98 | with open(module.__file__, "r") as temp_output: |
| 99 | self.assertAstMatches(node, temp_output.read()) |
| 100 | |
| 101 | def test_load_source(self): |
| 102 | test_source = textwrap.dedent(""" |