1
|
|
|
import ast |
2
|
|
|
from py14.scope import add_scope_context |
3
|
|
|
from py14.context import add_variable_context |
4
|
|
|
from py14.analysis import (FunctionTransformer, CalledWithTransformer, |
5
|
|
|
ImportTransformer, AttributeCallTransformer, |
6
|
|
|
is_void_function) |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
def parse(*args): |
10
|
|
|
source = ast.parse("\n".join(args)) |
11
|
|
|
add_scope_context(source) |
12
|
|
|
add_variable_context(source) |
13
|
|
|
return source |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def test_is_void_for_fun_with_no_return(): |
17
|
|
|
source = parse( |
18
|
|
|
"def foo(x):", |
19
|
|
|
" bar(x)", |
20
|
|
|
) |
21
|
|
|
foo = source.body[0] |
22
|
|
|
assert is_void_function(foo) |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
def test_is_not_void_for_fun_with_return_value(): |
26
|
|
|
source = parse( |
27
|
|
|
"def foo(x):", |
28
|
|
|
" return x", |
29
|
|
|
) |
30
|
|
|
foo = source.body[0] |
31
|
|
|
assert not is_void_function(foo) |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
class TestFunctionTransformer: |
35
|
|
|
def test_nested_functions(self): |
36
|
|
|
source = parse( |
37
|
|
|
"def foo():", |
38
|
|
|
" def bar():", |
39
|
|
|
" def gib():", |
40
|
|
|
" pass", |
41
|
|
|
" def mir():", |
42
|
|
|
" pass", |
43
|
|
|
) |
44
|
|
|
FunctionTransformer().visit(source) |
45
|
|
|
|
46
|
|
|
foo = source.body[0] |
47
|
|
|
bar = foo.body[0] |
48
|
|
|
gib = bar.body[0] |
49
|
|
|
mir = bar.body[1] |
50
|
|
|
|
51
|
|
|
assert len(foo.defined_functions) == 1 |
52
|
|
|
assert len(bar.defined_functions) == 2 |
53
|
|
|
assert len(gib.defined_functions) == 0 |
54
|
|
|
assert len(mir.defined_functions) == 0 |
55
|
|
|
|
56
|
|
|
def test_functions_from_modules(self): |
57
|
|
|
source = parse("from foo import bar, baz") |
58
|
|
|
FunctionTransformer().visit(source) |
59
|
|
|
|
60
|
|
|
module = source |
61
|
|
|
|
62
|
|
|
assert len(module.defined_functions) == 2 |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
class TestCalledWithTransformer: |
66
|
|
|
def test_var_called_with_later_function(self): |
67
|
|
|
source = parse( |
68
|
|
|
"x = 3", |
69
|
|
|
"bar(x)", |
70
|
|
|
"bar(foo(x))", |
71
|
|
|
) |
72
|
|
|
CalledWithTransformer().visit(source) |
73
|
|
|
|
74
|
|
|
x = source.body[0].targets[0] |
75
|
|
|
|
76
|
|
|
assert len(x.called_with) == 2 |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
class TestAttributeCallTransformer: |
80
|
|
|
def test_call_to_attribute_registered(self): |
81
|
|
|
source = parse( |
82
|
|
|
"x = foo()", |
83
|
|
|
"x.bar()", |
84
|
|
|
) |
85
|
|
|
AttributeCallTransformer().visit(source) |
86
|
|
|
|
87
|
|
|
x = source.body[0].targets[0] |
88
|
|
|
|
89
|
|
|
assert len(x.calls) == 1 |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
class TestImportTransformer: |
93
|
|
|
def test_function_knows_from_where_it_is_imported(self): |
94
|
|
|
source = parse( |
95
|
|
|
"from foo import bar", |
96
|
|
|
"bar(x)", |
97
|
|
|
) |
98
|
|
|
ImportTransformer().visit(source) |
99
|
|
|
|
100
|
|
|
module = source |
101
|
|
|
bar_import = source.body[0].names[0] |
102
|
|
|
|
103
|
|
|
assert len(module.imports) == 1 |
104
|
|
|
assert isinstance(bar_import.imported_from, ast.ImportFrom) |
105
|
|
|
|