-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtests.py
183 lines (147 loc) · 6.04 KB
/
tests.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import unittest
import zsc.prepass as prepass
import zsc.compiler as compiler
import ast
import inspect
class TestPrepass(unittest.TestCase):
def test_get_call_name_zfunc(self):
p = prepass.Prepass()
example = ast.parse('''zbrush.IClick("a:b:c")''', mode='eval')
assert (p.get_call_name(example.body)) == ('zbrush', 'IClick')
def test_get_call_name_raw(self):
p = prepass.Prepass()
example = ast.parse('''somefunction()''', mode='eval')
assert (p.get_call_name(example.body)) == ('', 'somefunction')
def test_get_is_zfunc_normal(self):
p = prepass.Prepass()
raw = '''import zbrush\nzbrush.IClick()'''
p.visit(ast.parse(raw))
example = ast.parse('''zbrush.IClick("A")''', mode='eval')
assert (p.is_zbrush_function(example.body))
def test_get_is_zfunc_fail_unrecognized(self):
p = prepass.Prepass()
raw = '''import zbrush\nzbrush.IClick()'''
p.visit(ast.parse(raw))
example = ast.parse('''dummy("A")''', mode='eval')
assert (not p.is_zbrush_function(example.body))
def test_get_zfunc_mod_alias(self):
p = prepass.Prepass()
raw = '''import zbrush as zb\nzb.IClick()'''
p.visit(ast.parse(raw))
example = ast.parse('''zb.IClick("A")''', mode='eval')
assert (p.is_zbrush_function(example.body))
def test_get_zfunc_func_alias(self):
p = prepass.Prepass()
raw = '''from zbrush import IClick as blah'''
p.visit(ast.parse(raw))
example = ast.parse('''blah("A")''', mode='eval')
assert (p.is_zbrush_function(example.body))
def test_get_zfunc_signature(self):
p = prepass.Prepass()
raw = '''from zbrush import IClick as blah'''
p.visit(ast.parse(raw))
assert isinstance(p.get_signature('IClick'), inspect.FullArgSpec)
def test_allows_zbrush_import(self):
p = prepass.Prepass()
raw = '''import zbrush'''
p.visit(ast.parse(raw))
def test_allows_math_import(self):
p = prepass.Prepass()
raw = '''import math'''
p.visit(ast.parse(raw))
def test_allows_random_import(self):
p = prepass.Prepass()
raw = '''import random'''
p.visit(ast.parse(raw))
def test_no_other_import_plain(self):
p = prepass.Prepass()
raw = '''import csv'''
def bad(): return p.visit(ast.parse(raw))
self.assertRaises(prepass.ParseError, bad)
def test_no_other_math_import_aliased(self):
p = prepass.Prepass()
raw = '''import csv as xyz'''
def bad(): return p.visit(ast.parse(raw))
self.assertRaises(prepass.ParseError, bad)
def test_user_function(self):
p = prepass.Prepass()
raw = '''def userfunc():\n return 1'''
p.visit(ast.parse(raw))
call = ast.Call(ast.Name("userfunc"))
assert p.is_user_function(call)
def test_user_function_negative(self):
p = prepass.Prepass()
raw = '''def userfunc():\n return 1'''
p.visit(ast.parse(raw))
call = ast.Call(ast.Name("not_userfunc"))
assert not p.is_user_function(call)
def test_has_return_type(self):
p = prepass.Prepass()
raw = '''import zbrush, math, random'''
p.visit(ast.parse(raw))
call = ast.Call(ast.Name("GetActiveToolPath"))
assert p.has_return_type(call)
def test_has_return_type_neg(self):
p = prepass.Prepass()
raw = '''import zbrush, math, random'''
p.visit(ast.parse(raw))
call = ast.Call(ast.Name("MTransformSet"))
assert not p.has_return_type(call)
def test_return_type_math(self):
p = prepass.Prepass()
raw = '''import zbrush, math, random'''
p.visit(ast.parse(raw))
call = ast.Call(ast.Name("sin"))
assert p.has_return_type(call)
def test_return_type_random(self):
p = prepass.Prepass()
raw = '''import zbrush, math, random'''
p.visit(ast.parse(raw))
call = ast.Call(ast.Name("randint"))
assert p.has_return_type(call)
def test_return_type_user(self):
# note that user functions _CANT_ have return types
# in zbrush, so this ignores the python. The compiler
# raises an error for this
p = prepass.Prepass()
raw = '''def userfunc():\n return 1'''
p.visit(ast.parse(raw))
call = ast.Call(ast.Name("usefunc"))
assert not p.has_return_type(call)
class TestAnalyzer(unittest.TestCase):
def parse (self, stringval):
tree = ast.parse(stringval)
p = prepass.Prepass()
p.visit(tree)
analyzer = compiler.Analyzer(0, input_file="dummy", prepass=p)
return analyzer, tree
def test_assign_int(self):
analyzer, tree = self.parse('''a = 1''')
analyzer.visit(tree)
assert analyzer.format() == '[VarDef, a, 1]'
def test_assign_float(self):
analyzer, tree = self.parse('''a = -1.0''')
analyzer.visit(tree)
assert analyzer.format() == '[VarDef, a, -1.0]'
def test_assign_str(self):
analyzer, tree = self.parse('''a = "fred"''')
analyzer.visit(tree)
assert analyzer.format() == '[VarDef, a, "fred"]'
def test_assign_var(self):
analyzer, tree = self.parse('''a = 1\nb = a''')
analyzer.visit(tree)
assert analyzer.format() == '[VarDef, a, 1]\n[VarDef, b, #a]'
def test_assign_var_neg(self):
analyzer, tree = self.parse('''a = 1\nb = -a''')
analyzer.visit(tree)
assert analyzer.format() == '[VarDef, a, 1]\n[VarDef, b, [NEG, #a]]'
def test_assign_def_vs_set(self):
# use VarDef at the top level but VarSet inside of functions
analyzer, tree = self.parse('''a = 1\ndef func(input, output):\n temp = input + 1\n output = temp''')
analyzer.visit(tree)
result = analyzer.format()
assert '[VarDef, a, 1]' in result
assert '[VarSet, temp, ' in result
assert '[VarSet, output' in result
if __name__ == '__main__':
unittest.main()