Coverage for skema/program_analysis/tests/test_function_cast.py: 100%
187 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
1# import json NOTE: json and Path aren't used right now,
2# from pathlib import Path but will be used in the future
3from skema.program_analysis.CAST.python.ts2cast import TS2CAST
4from skema.program_analysis.CAST2FN.model.cast import (
5 Assignment,
6 Var,
7 Name,
8 FunctionDef,
9 Call,
10 CASTLiteralValue,
11 Operator,
12 ModelReturn
13)
15def fun1():
16 return """
17def foo(x):
18 return x + 2
19x = foo(2)
20 """
22def fun2():
23 return """
24def foo(x):
25 return x + 3
27x = foo(2)
28y = foo(x)
29 """
31def fun3():
32 return """
33def foo(x):
34 return x + 3
36x = foo(2)
37y = foo(x)
38 """
40def fun4():
41 return """
42def foo(x, a):
43 y = x + 3
44 z = a * y
45 return z
47x = foo(2, 1)
48 """
51def generate_cast(test_file_string):
52 # use Python to CAST
53 out_cast = TS2CAST(test_file_string, from_file=False).out_cast
55 return out_cast
57def test_fun1():
58 fun_cast = generate_cast(fun1())
59 func_def_node = fun_cast.nodes[0].body[0]
60 assert isinstance(func_def_node, FunctionDef)
62 assert len(func_def_node.func_args) == 1
63 assert isinstance(func_def_node.func_args[0], Var)
64 assert func_def_node.func_args[0].val.name == "x"
65 assert func_def_node.func_args[0].val.id == 1
67 func_def_body = func_def_node.body[0]
68 assert isinstance(func_def_body, ModelReturn)
69 assert isinstance(func_def_body.value, Operator)
70 assert isinstance(func_def_body.value.operands[0], Name)
72 assert func_def_body.value.op == "ast.Add"
73 assert func_def_body.value.operands[0].name == "x"
74 assert func_def_body.value.operands[0].id == 1
76 assert isinstance(func_def_body.value.operands[1], CASTLiteralValue)
77 assert func_def_body.value.operands[1].value == "2"
79 #######################################################
81 func_asg_node = fun_cast.nodes[0].body[1]
82 assert isinstance(func_asg_node, Assignment)
83 assert isinstance(func_asg_node.left, Var)
84 assert func_asg_node.left.val.name == "x"
85 assert func_asg_node.left.val.id == 2
87 func_call_node = func_asg_node.right
88 assert isinstance(func_call_node, Call)
89 assert func_call_node.func.name == "foo"
90 assert func_call_node.func.id == 0
92 assert len(func_call_node.arguments) == 1
93 assert isinstance(func_call_node.arguments[0], CASTLiteralValue)
94 assert func_call_node.arguments[0].value_type == "Integer"
95 assert func_call_node.arguments[0].value == "2"
98def test_fun2():
99 fun_cast = generate_cast(fun2())
101 func_def_node = fun_cast.nodes[0].body[0]
102 assert isinstance(func_def_node, FunctionDef)
104 assert len(func_def_node.func_args) == 1
105 assert isinstance(func_def_node.func_args[0], Var)
106 assert func_def_node.func_args[0].val.name == "x"
107 assert func_def_node.func_args[0].val.id == 1
109 func_def_body = func_def_node.body[0]
110 assert isinstance(func_def_body, ModelReturn)
111 assert isinstance(func_def_body.value, Operator)
112 assert isinstance(func_def_body.value.operands[0], Name)
114 assert func_def_body.value.op == "ast.Add"
115 assert func_def_body.value.operands[0].name == "x"
116 assert func_def_body.value.operands[0].id == 1
118 assert isinstance(func_def_body.value.operands[1], CASTLiteralValue)
119 assert func_def_body.value.operands[1].value == "3"
121 #######################################################
123 func_asg_node = fun_cast.nodes[0].body[1]
124 assert isinstance(func_asg_node, Assignment)
125 assert isinstance(func_asg_node.left, Var)
126 assert func_asg_node.left.val.name == "x"
127 assert func_asg_node.left.val.id == 2
129 func_call_node = func_asg_node.right
130 assert isinstance(func_call_node, Call)
131 assert func_call_node.func.name == "foo"
132 assert func_call_node.func.id == 0
134 assert len(func_call_node.arguments) == 1
135 assert isinstance(func_call_node.arguments[0], CASTLiteralValue)
136 assert func_call_node.arguments[0].value_type == "Integer"
137 assert func_call_node.arguments[0].value == "2"
139 #######################################################
141 func_asg_node = fun_cast.nodes[0].body[2]
142 assert isinstance(func_asg_node, Assignment)
143 assert isinstance(func_asg_node.left, Var)
144 assert func_asg_node.left.val.name == "y"
145 assert func_asg_node.left.val.id == 3
147 func_call_node = func_asg_node.right
148 assert isinstance(func_call_node, Call)
149 assert func_call_node.func.name == "foo"
150 assert func_call_node.func.id == 0
152 assert len(func_call_node.arguments) == 1
153 assert isinstance(func_call_node.arguments[0], Name)
154 assert func_call_node.arguments[0].name == "x"
155 assert func_call_node.arguments[0].id == 2
158def test_fun3():
159 fun_cast = generate_cast(fun3())
161 func_def_node = fun_cast.nodes[0].body[0]
162 assert isinstance(func_def_node, FunctionDef)
164 assert len(func_def_node.func_args) == 1
165 assert isinstance(func_def_node.func_args[0], Var)
166 assert func_def_node.func_args[0].val.name == "x"
167 assert func_def_node.func_args[0].val.id == 1
169 func_def_body = func_def_node.body[0]
170 assert isinstance(func_def_body, ModelReturn)
171 assert isinstance(func_def_body.value, Operator)
172 assert isinstance(func_def_body.value.operands[0], Name)
174 assert func_def_body.value.op == "ast.Add"
175 assert func_def_body.value.operands[0].name == "x"
176 assert func_def_body.value.operands[0].id == 1
178 assert isinstance(func_def_body.value.operands[1], CASTLiteralValue)
179 assert func_def_body.value.operands[1].value == "3"
181 #######################################################
183 func_asg_node = fun_cast.nodes[0].body[1]
184 assert isinstance(func_asg_node, Assignment)
185 assert isinstance(func_asg_node.left, Var)
186 assert func_asg_node.left.val.name == "x"
187 assert func_asg_node.left.val.id == 2
189 func_call_node = func_asg_node.right
190 assert isinstance(func_call_node, Call)
191 assert func_call_node.func.name == "foo"
192 assert func_call_node.func.id == 0
194 assert len(func_call_node.arguments) == 1
195 assert isinstance(func_call_node.arguments[0], CASTLiteralValue)
196 assert func_call_node.arguments[0].value_type == "Integer"
197 assert func_call_node.arguments[0].value == "2"
199 #######################################################
201 func_asg_node = fun_cast.nodes[0].body[2]
202 assert isinstance(func_asg_node, Assignment)
203 assert isinstance(func_asg_node.left, Var)
204 assert func_asg_node.left.val.name == "y"
205 assert func_asg_node.left.val.id == 3
207 func_call_node = func_asg_node.right
208 assert isinstance(func_call_node, Call)
209 assert func_call_node.func.name == "foo"
210 assert func_call_node.func.id == 0
212 assert len(func_call_node.arguments) == 1
213 assert isinstance(func_call_node.arguments[0], Name)
214 assert func_call_node.arguments[0].name == "x"
215 assert func_call_node.arguments[0].id == 2
217def test_fun4():
218 fun_cast = generate_cast(fun4())
220 func_def_node = fun_cast.nodes[0].body[0]
221 assert isinstance(func_def_node, FunctionDef)
223 assert len(func_def_node.func_args) == 2
224 assert isinstance(func_def_node.func_args[0], Var)
225 assert func_def_node.func_args[0].val.name == "x"
226 assert func_def_node.func_args[0].val.id == 1
228 assert isinstance(func_def_node.func_args[1], Var)
229 assert func_def_node.func_args[1].val.name == "a"
230 assert func_def_node.func_args[1].val.id == 2
232 func_def_body = func_def_node.body[0]
233 assert isinstance(func_def_body, Assignment)
234 assert isinstance(func_def_body.left, Var)
235 assert func_def_body.left.val.name == "y"
236 assert func_def_body.left.val.id == 3
238 assert isinstance(func_def_body.right, Operator)
239 assert func_def_body.right.op == "ast.Add"
241 assert isinstance(func_def_body.right.operands[0], Name)
242 assert func_def_body.right.operands[0].name == "x"
243 assert func_def_body.right.operands[0].id == 1
245 assert isinstance(func_def_body.right.operands[1], CASTLiteralValue)
246 assert func_def_body.right.operands[1].value == "3"
247 assert func_def_body.right.operands[1].value_type == "Integer"
250 func_def_body = func_def_node.body[1]
251 assert isinstance(func_def_body, Assignment)
252 assert isinstance(func_def_body.left, Var)
253 assert func_def_body.left.val.name == "z"
254 assert func_def_body.left.val.id == 4
256 assert isinstance(func_def_body.right, Operator)
257 assert func_def_body.right.op == "ast.Mult"
259 assert isinstance(func_def_body.right.operands[0], Name)
260 assert func_def_body.right.operands[0].name == "a"
261 assert func_def_body.right.operands[0].id == 2
263 assert isinstance(func_def_body.right.operands[1], Name)
264 assert func_def_body.right.operands[1].name == "y"
265 assert func_def_body.right.operands[1].id == 3
267 func_def_body = func_def_node.body[2]
268 assert isinstance(func_def_body, ModelReturn)
269 assert isinstance(func_def_body.value, Name)
271 assert func_def_body.value.name == "z"
272 assert func_def_body.value.id == 4
274 #######################################################
275 func_asg_node = fun_cast.nodes[0].body[1]
276 assert isinstance(func_asg_node, Assignment)
277 assert isinstance(func_asg_node.left, Var)
278 assert func_asg_node.left.val.name == "x"
279 assert func_asg_node.left.val.id == 5
281 func_call_node = func_asg_node.right
282 assert isinstance(func_call_node, Call)
283 assert func_call_node.func.name == "foo"
284 assert func_call_node.func.id == 0
286 assert len(func_call_node.arguments) == 2
287 assert isinstance(func_call_node.arguments[0], CASTLiteralValue)
288 assert func_call_node.arguments[0].value_type == "Integer"
289 assert func_call_node.arguments[0].value == "2"
291 assert isinstance(func_call_node.arguments[1], CASTLiteralValue)
292 assert func_call_node.arguments[1].value_type == "Integer"
293 assert func_call_node.arguments[1].value == "1"