Coverage for skema/program_analysis/tests/test_for_cast.py: 100%
192 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 Call,
8 Name,
9 CASTLiteralValue,
10 ModelIf,
11 Loop,
12 Operator
13)
15def for1():
16 return """
17x = 7
18for i in range(10):
19 x = x + i
20 """
22def for2():
23 return """
24x = 1
25for a,b in range(10):
26 x = x + a + b
27 """
29def for3():
30 return """
31x = 1
32L = [1,2,3]
34for i in L:
35 x = x + i
36 """
39def generate_cast(test_file_string):
40 # use Python to CAST
41 out_cast = TS2CAST(test_file_string, from_file=False).out_cast
43 return out_cast
45def test_for1():
46 cast = generate_cast(for1())
48 asg_node = cast.nodes[0].body[0]
49 loop_node = cast.nodes[0].body[1]
51 assert isinstance(asg_node, Assignment)
52 assert isinstance(asg_node.left, Var)
53 assert isinstance(asg_node.left.val, Name)
54 assert asg_node.left.val.name == "x"
56 assert isinstance(asg_node.right, CASTLiteralValue)
57 assert asg_node.right.value_type == "Integer"
58 assert asg_node.right.value == '7'
60 assert isinstance(loop_node, Loop)
61 assert len(loop_node.pre) == 2
63 # Loop Pre
64 loop_pre = loop_node.pre
65 assert isinstance(loop_pre[0], Assignment)
66 assert isinstance(loop_pre[0].left, Var)
67 assert loop_pre[0].left.val.name == "generated_iter_0"
69 assert isinstance(loop_pre[0].right, Call)
70 assert loop_pre[0].right.func.name == "iter"
71 iter_args = loop_pre[0].right.arguments
73 assert len(iter_args) == 1
74 assert isinstance(iter_args[0], Call)
75 assert iter_args[0].func.name == "range"
76 assert len(iter_args[0].arguments) == 3
78 assert isinstance(iter_args[0].arguments[0], CASTLiteralValue)
79 assert iter_args[0].arguments[0].value == "1"
80 assert isinstance(iter_args[0].arguments[1], CASTLiteralValue)
81 assert iter_args[0].arguments[1].value == "10"
82 assert isinstance(iter_args[0].arguments[2], CASTLiteralValue)
83 assert iter_args[0].arguments[2].value == "1"
85 assert isinstance(loop_pre[1], Assignment)
86 assert isinstance(loop_pre[1].left, CASTLiteralValue)
87 assert loop_pre[1].left.value_type == "Tuple"
89 assert isinstance(loop_pre[1].left.value[0], Var)
90 assert loop_pre[1].left.value[0].val.name == "i"
91 assert isinstance(loop_pre[1].left.value[1], Var)
92 assert loop_pre[1].left.value[1].val.name == "generated_iter_0"
93 assert isinstance(loop_pre[1].left.value[2], Var)
94 assert loop_pre[1].left.value[2].val.name == "sc_0"
96 assert isinstance(loop_pre[1].right, Call)
97 assert loop_pre[1].right.func.name == "next"
98 assert len(loop_pre[1].right.arguments) == 1
99 assert loop_pre[1].right.arguments[0].val.name == "generated_iter_0"
101 # Loop Test
102 loop_test = loop_node.expr
103 assert isinstance(loop_test, Operator)
104 assert loop_test.op == "ast.Eq"
105 assert isinstance(loop_test.operands[0], Name)
106 assert loop_test.operands[0].name == "sc_0"
108 assert isinstance(loop_test.operands[1], CASTLiteralValue)
109 assert loop_test.operands[1].value_type == "Boolean"
111 # Loop Body
112 loop_body = loop_node.body
113 next_call = loop_body[-1]
114 assert isinstance(next_call, Assignment)
115 assert isinstance(next_call.right, Call)
116 assert next_call.right.func.name == "next"
117 assert next_call.right.arguments[0].val.name == "generated_iter_0"
120def test_for2():
121 cast = generate_cast(for2())
123 asg_node = cast.nodes[0].body[0]
124 loop_node = cast.nodes[0].body[1]
126 assert isinstance(asg_node, Assignment)
127 assert isinstance(asg_node.left, Var)
128 assert isinstance(asg_node.left.val, Name)
129 assert asg_node.left.val.name == "x"
131 assert isinstance(asg_node.right, CASTLiteralValue)
132 assert asg_node.right.value_type == "Integer"
133 assert asg_node.right.value == '1'
135 assert isinstance(loop_node, Loop)
136 assert len(loop_node.pre) == 2
138 # Loop Pre
139 loop_pre = loop_node.pre
140 assert isinstance(loop_pre[0], Assignment)
141 assert isinstance(loop_pre[0].left, Var)
142 assert loop_pre[0].left.val.name == "generated_iter_0"
144 assert isinstance(loop_pre[0].right, Call)
145 assert loop_pre[0].right.func.name == "iter"
146 iter_args = loop_pre[0].right.arguments
148 assert len(iter_args) == 1
149 assert isinstance(iter_args[0], Call)
150 assert iter_args[0].func.name == "range"
151 assert len(iter_args[0].arguments) == 3
153 assert isinstance(iter_args[0].arguments[0], CASTLiteralValue)
154 assert iter_args[0].arguments[0].value == "1"
155 assert isinstance(iter_args[0].arguments[1], CASTLiteralValue)
156 assert iter_args[0].arguments[1].value == "10"
157 assert isinstance(iter_args[0].arguments[2], CASTLiteralValue)
158 assert iter_args[0].arguments[2].value == "1"
160 assert isinstance(loop_pre[1], Assignment)
161 assert isinstance(loop_pre[1].left, CASTLiteralValue)
162 assert loop_pre[1].left.value_type == "Tuple"
164 assert isinstance(loop_pre[1].left.value[0], CASTLiteralValue)
165 assert loop_pre[1].left.value[0].value_type == "Tuple"
167 assert isinstance(loop_pre[1].left.value[0].value[0], Var)
168 assert loop_pre[1].left.value[0].value[0].val.name == "a"
169 assert isinstance(loop_pre[1].left.value[0].value[1], Var)
170 assert loop_pre[1].left.value[0].value[1].val.name == "b"
172 assert isinstance(loop_pre[1].left.value[1], Var)
173 assert loop_pre[1].left.value[1].val.name == "generated_iter_0"
174 assert isinstance(loop_pre[1].left.value[2], Var)
175 assert loop_pre[1].left.value[2].val.name == "sc_0"
177 assert isinstance(loop_pre[1].right, Call)
178 assert loop_pre[1].right.func.name == "next"
179 assert len(loop_pre[1].right.arguments) == 1
180 assert loop_pre[1].right.arguments[0].val.name == "generated_iter_0"
182 # Loop Test
183 loop_test = loop_node.expr
184 assert isinstance(loop_test, Operator)
185 assert loop_test.op == "ast.Eq"
186 assert isinstance(loop_test.operands[0], Name)
187 assert loop_test.operands[0].name == "sc_0"
189 assert isinstance(loop_test.operands[1], CASTLiteralValue)
190 assert loop_test.operands[1].value_type == "Boolean"
192 # Loop Body
193 loop_body = loop_node.body
194 body_asg = loop_body[0]
195 assert isinstance(body_asg, Assignment)
197 assert isinstance(body_asg.right, Operator)
198 assert isinstance(body_asg.right.operands[0], Operator)
199 assert isinstance(body_asg.right.operands[0].operands[0], Name)
200 assert body_asg.right.operands[0].operands[0].name == "x"
202 assert isinstance(body_asg.right.operands[0].operands[1], Name)
203 assert body_asg.right.operands[0].operands[1].name == "a"
205 assert isinstance(body_asg.right.operands[1], Name)
206 assert body_asg.right.operands[1].name == "b"
208 next_call = loop_body[-1]
209 assert isinstance(next_call, Assignment)
210 assert isinstance(next_call.right, Call)
211 assert next_call.right.func.name == "next"
212 assert next_call.right.arguments[0].val.name == "generated_iter_0"
215def test_for3():
216 cast = generate_cast(for3())
218 asg_node = cast.nodes[0].body[0]
219 list_node = cast.nodes[0].body[1]
220 loop_node = cast.nodes[0].body[2]
222 assert isinstance(asg_node, Assignment)
223 assert isinstance(asg_node.left, Var)
224 assert isinstance(asg_node.left.val, Name)
225 assert asg_node.left.val.name == "x"
227 assert isinstance(asg_node.right, CASTLiteralValue)
228 assert asg_node.right.value_type == "Integer"
229 assert asg_node.right.value == '1'
231 assert isinstance(loop_node, Loop)
232 assert len(loop_node.pre) == 2
234 assert isinstance(list_node, Assignment)
235 assert isinstance(list_node.left, Var)
236 assert list_node.left.val.name == "L"
238 assert isinstance(list_node.right, CASTLiteralValue)
239 assert list_node.right.value_type == "List"
241 # Loop Pre
242 loop_pre = loop_node.pre
243 assert isinstance(loop_pre[0], Assignment)
244 assert isinstance(loop_pre[0].left, Var)
245 assert loop_pre[0].left.val.name == "generated_iter_0"
247 assert isinstance(loop_pre[0].right, Call)
248 assert loop_pre[0].right.func.name == "iter"
249 iter_args = loop_pre[0].right.arguments
251 assert len(iter_args) == 1
252 assert isinstance(iter_args[0], Var)
253 assert iter_args[0].val.name == "L"
255 assert isinstance(loop_pre[1], Assignment)
256 assert isinstance(loop_pre[1].left, CASTLiteralValue)
257 assert loop_pre[1].left.value_type == "Tuple"
259 assert isinstance(loop_pre[1].left.value[0], Var)
260 assert loop_pre[1].left.value[0].val.name == "i"
261 assert isinstance(loop_pre[1].left.value[1], Var)
262 assert loop_pre[1].left.value[1].val.name == "generated_iter_0"
263 assert isinstance(loop_pre[1].left.value[2], Var)
264 assert loop_pre[1].left.value[2].val.name == "sc_0"
266 assert isinstance(loop_pre[1].right, Call)
267 assert loop_pre[1].right.func.name == "next"
268 assert len(loop_pre[1].right.arguments) == 1
269 assert loop_pre[1].right.arguments[0].val.name == "generated_iter_0"
271 # Loop Test
272 loop_test = loop_node.expr
273 assert isinstance(loop_test, Operator)
274 assert loop_test.op == "ast.Eq"
275 assert isinstance(loop_test.operands[0], Name)
276 assert loop_test.operands[0].name == "sc_0"
278 assert isinstance(loop_test.operands[1], CASTLiteralValue)
279 assert loop_test.operands[1].value_type == "Boolean"
281 # Loop Body
282 loop_body = loop_node.body
283 next_call = loop_body[-1]
285 assert isinstance(next_call, Assignment)
286 assert isinstance(next_call.right, Call)
287 assert next_call.right.func.name == "next"
288 assert next_call.right.arguments[0].val.name == "generated_iter_0"