|
3 | 3 |
|
4 | 4 | import pytest |
5 | 5 |
|
| 6 | +import spy.ast as ast |
6 | 7 | from spy.analyze.importing import ImportAnalyzer |
| 8 | +from spy.astcompile import astcompile_interactive |
7 | 9 | from spy.backend.spy import AST_FORMAT, FQN_FORMAT, SPyBackend |
8 | 10 | from spy.fqn import FQN |
| 11 | +from spy.parser import Parser |
| 12 | +from spy.tests.test_parser import assert_node_dump |
9 | 13 | from spy.util import print_diff |
10 | 14 | from spy.vm.function import W_ASTFunc |
11 | 15 | from spy.vm.vm import SPyVM |
@@ -36,6 +40,20 @@ def write_src(self, src: str) -> None: |
36 | 40 | src = textwrap.dedent(src) |
37 | 41 | f.write(src) |
38 | 42 |
|
| 43 | + def compile_interactive(self, src: str) -> ast.Expr: |
| 44 | + """ |
| 45 | + Parse a single expression and astcompile it in interactive mode against the |
| 46 | + symtable of `test::foo`. This is meant to be similar to what SPdb does when |
| 47 | + evaluating interactive exprs. |
| 48 | + """ |
| 49 | + fqn = FQN("test::foo") |
| 50 | + w_foo = self.vm.globals_w[fqn] |
| 51 | + assert isinstance(w_foo, W_ASTFunc) |
| 52 | + parser = Parser(src, "<test>") |
| 53 | + stmt = parser.parse_single_stmt() |
| 54 | + assert isinstance(stmt, ast.StmtExpr) |
| 55 | + return astcompile_interactive(stmt.value, w_foo.funcdef.symtable) |
| 56 | + |
39 | 57 | def assert_dump( |
40 | 58 | self, |
41 | 59 | expected: str, |
@@ -215,3 +233,24 @@ def foo(obj: dynamic, val: i32) -> None: |
215 | 233 | _$aug_target0.x = _$aug_target0.x + val |
216 | 234 | """ |
217 | 235 | self.assert_dump(expected) |
| 236 | + |
| 237 | + def test_NameInteractive(self): |
| 238 | + self.compile_src(""" |
| 239 | + X = 10 |
| 240 | +
|
| 241 | + def foo() -> None: |
| 242 | + Y = 20 |
| 243 | + """) |
| 244 | + expr_X = self.compile_interactive("X") |
| 245 | + expected = """ |
| 246 | + NameInteractive(id='X') |
| 247 | + """ |
| 248 | + assert_node_dump(expr_X, expected) |
| 249 | + |
| 250 | + expr_Y = self.compile_interactive("Y") |
| 251 | + expected = """ |
| 252 | + NameLocalDirect( |
| 253 | + sym=Symbol('Y', 'const', 'direct'), |
| 254 | + ) |
| 255 | + """ |
| 256 | + assert_node_dump(expr_Y, expected) |
0 commit comments