-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_analyzer_test.py
65 lines (61 loc) · 2.5 KB
/
func_analyzer_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import unittest
import memoization.func_analyzer as func_analyzer
g1 = 1
g2 = 2
g3 = 3
abs = lambda i: i if i >= 0 else -i # hide builtin function
class TestFuncAnalyzer(unittest.TestCase):
a1 = 1
a2 = 2
def test_load_globals(self):
global g1, g2, g3
l1 = 1
def get():
t1 = g1
t3 = g3
self.assertEqual([("g1", 1), ("g3", 3)], func_analyzer.get_load_globals(get))
def get_multiple_times():
t1 = g1
t2 = g1
t3 = g1
self.assertEqual([("g1", 1)], func_analyzer.get_load_globals(get_multiple_times))
def get_local():
t1 = l1 # should not be reported because it is a free variable
self.assertEqual([], func_analyzer.get_load_globals(get_local))
def set():
global g2
g2 = 22 # should not be reported because it is assignment
self.assertEqual([], func_analyzer.get_load_globals(set))
def builtin():
t = sum(g1, g3) # sum should not be reported because it is a built-in function
self.assertEqual([("g1", 1), ("g3", 3)], func_analyzer.get_load_globals(builtin))
def attr():
t1 = TestFuncAnalyzer.a1 # a1 should not be reported because it is an attribute name
self.assertEqual([("TestFuncAnalyzer", TestFuncAnalyzer)], func_analyzer.get_load_globals(attr))
g1 = 11
self.assertEqual([("g1", 11), ("g3", 3)], func_analyzer.get_load_globals(get))
def hide_builtin_function():
return abs(-1)
self.assertEqual([("abs", abs)], func_analyzer.get_load_globals(hide_builtin_function))
def test_load_deref(self):
l1 = 1
l2 = 2
l3 = 3
abs = lambda i: i if i >= 0 else -i # hide builtin function and global function
def get():
t1 = l1
t3 = l3
self.assertEqual([("l1", 1), ("l3", 3)], func_analyzer.get_load_deref(get))
def get_multiple_times():
t1 = l1
t2 = l1
t3 = l1
self.assertEqual([("l1", 1)], func_analyzer.get_load_deref(get_multiple_times))
l1 = 11
self.assertEqual([("l1", 11), ("l3", 3)], func_analyzer.get_load_deref(get))
def set():
l2 = 22 # should not be reported because it is assignment
self.assertEqual([], func_analyzer.get_load_deref(set))
def hide_builtin_function():
return abs(-1)
self.assertEqual([("abs", abs)], func_analyzer.get_load_deref(hide_builtin_function))