Coverage for skema/program_analysis/tests/test_adv_comprehension_lambda_cast.py: 99%
486 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 Attribute,
7 FunctionDef,
8 Call,
9 Var,
10 Loop,
11 ModelReturn,
12 Name,
13 CASTLiteralValue,
14 ModelIf,
15 StructureType,
16 ScalarType,
17 Operator
18)
20def comp1():
21 return """
22L = [a*b for a in range(10) for b in range(10)]
23 """
25def comp2():
26 return """
27L = [a for a in range(10) if a % 2 == 0]
28 """
30def comp3():
31 return """
32L = [a*b for a in range(10) if a % 2 == 0 for b in range(10) if b % 2 == 1]
33 """
35def lambda1():
36 return """
37y = 2
38F = lambda x : x * y
39 """
41def generate_cast(test_file_string):
42 # use Python to CAST
43 out_cast = TS2CAST(test_file_string, from_file=False).out_cast
45 return out_cast
47def test_comp1():
48 comp_cast = generate_cast(comp1())
50 # Test list comprehension with double for loops
51 func_node = comp_cast.nodes[0].body[0]
52 assert isinstance(func_node, FunctionDef)
53 assert "%comprehension_list_0" == func_node.name.name
54 assert len(func_node.func_args) == 0
56 func_node_body = func_node.body
57 func_asg = func_node.body[0]
58 func_ret = func_node.body[2]
59 assert isinstance(func_asg, Assignment)
60 assert isinstance(func_asg.left, Var)
61 assert isinstance(func_asg.left.val, Name)
62 assert "list__temp_" == func_asg.left.val.name
64 assert isinstance(func_asg.right, CASTLiteralValue)
65 assert func_asg.right.value_type == StructureType.LIST
66 assert func_asg.right.value == []
68 assert isinstance(func_ret, ModelReturn)
69 assert isinstance(func_ret.value, Name)
70 assert "list__temp_" == func_ret.value.name
72 func_loop = func_node_body[1]
73 assert isinstance(func_loop, Loop)
74 assert func_loop.post == []
76 # Loop Pre
77 #############
78 func_loop_pre = func_loop.pre
79 loop_pre_iter = func_loop_pre[0]
80 assert isinstance(loop_pre_iter, Assignment)
81 assert isinstance(loop_pre_iter.left, Var)
82 assert isinstance(loop_pre_iter.left.val, Name)
83 assert "generated_iter_0" == loop_pre_iter.left.val.name
85 assert isinstance(loop_pre_iter.right, Call)
86 assert isinstance(loop_pre_iter.right.func, Name)
87 assert loop_pre_iter.right.func.name == "iter"
89 assert len(loop_pre_iter.right.arguments) == 1
90 range_call = loop_pre_iter.right.arguments[0]
91 assert isinstance(range_call, Call)
92 assert isinstance(range_call.func, Name)
93 assert range_call.func.name == "range"
94 assert len(range_call.arguments) == 3
95 assert isinstance(range_call.arguments[0], CASTLiteralValue)
96 assert isinstance(range_call.arguments[1], CASTLiteralValue)
97 assert isinstance(range_call.arguments[2], CASTLiteralValue)
99 loop_pre_next = func_loop_pre[1]
100 assert isinstance(loop_pre_next, Assignment)
101 assert isinstance(loop_pre_next.left, CASTLiteralValue)
102 assert loop_pre_next.left.value_type == StructureType.TUPLE
103 assert isinstance(loop_pre_next.left.value[0], Var)
104 assert isinstance(loop_pre_next.left.value[0].val, Name)
105 assert loop_pre_next.left.value[0].val.name == "a"
107 assert isinstance(loop_pre_next.left.value[1], Var)
108 assert isinstance(loop_pre_next.left.value[1].val, Name)
109 assert "generated_iter_0" == loop_pre_next.left.value[1].val.name
111 assert isinstance(loop_pre_next.left.value[2], Var)
112 assert isinstance(loop_pre_next.left.value[2].val, Name)
113 assert "sc_" in loop_pre_next.left.value[2].val.name
115 assert isinstance(loop_pre_next.right, Call)
116 assert isinstance(loop_pre_next.right.func, Name)
117 assert loop_pre_next.right.func.name == "next"
118 assert len(loop_pre_next.right.arguments) == 1
119 assert isinstance(loop_pre_next.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
120 assert "generated_iter_0" == loop_pre_next.right.arguments[0].val.name
123 # Loop Test
124 #############
125 func_loop_test = func_loop.expr
126 assert isinstance(func_loop_test, Operator)
127 assert func_loop_test.op == "ast.Eq"
129 assert isinstance(func_loop_test.operands[0], Name)
130 assert "sc_" in func_loop_test.operands[0].name
132 assert isinstance(func_loop_test.operands[1], CASTLiteralValue)
133 assert func_loop_test.operands[1].value == False
134 assert func_loop_test.operands[1].value_type == ScalarType.BOOLEAN
137 # Loop Body
138 #############
139 inner_loop_body = func_loop.body[0]
140 assert isinstance(inner_loop_body, Loop)
142 def test_inner_loop(inner_loop: Loop):
143 # Inner Loop Pre
144 ##################
145 inner_loop_pre = inner_loop.pre
146 inner_loop_iter = inner_loop_pre[0]
147 assert isinstance(inner_loop_iter, Assignment)
148 assert isinstance(inner_loop_iter.left, Var)
149 assert isinstance(inner_loop_iter.left.val, Name)
150 assert "generated_iter_1" == inner_loop_iter.left.val.name
152 assert isinstance(inner_loop_iter.right, Call)
153 assert isinstance(inner_loop_iter.right.func, Name)
154 assert inner_loop_iter.right.func.name == "iter"
156 assert len(inner_loop_iter.right.arguments) == 1
157 range_call = inner_loop_iter.right.arguments[0]
158 assert isinstance(range_call, Call)
159 assert isinstance(range_call.func, Name)
160 assert range_call.func.name == "range"
161 assert len(range_call.arguments) == 3
162 assert isinstance(range_call.arguments[0], CASTLiteralValue)
163 assert isinstance(range_call.arguments[1], CASTLiteralValue)
164 assert isinstance(range_call.arguments[2], CASTLiteralValue)
166 inner_loop_pre_next = inner_loop_pre[1]
167 assert isinstance(inner_loop_pre_next, Assignment)
168 assert isinstance(inner_loop_pre_next.left, CASTLiteralValue)
169 assert inner_loop_pre_next.left.value_type == StructureType.TUPLE
170 assert isinstance(inner_loop_pre_next.left.value[0], Var)
171 assert isinstance(inner_loop_pre_next.left.value[0].val, Name)
172 assert inner_loop_pre_next.left.value[0].val.name == "b" # NOTE
174 assert isinstance(inner_loop_pre_next.left.value[1], Var)
175 assert isinstance(inner_loop_pre_next.left.value[1].val, Name)
176 assert "generated_iter_1" == inner_loop_pre_next.left.value[1].val.name
178 assert isinstance(inner_loop_pre_next.left.value[2], Var)
179 assert isinstance(inner_loop_pre_next.left.value[2].val, Name)
180 assert "sc_" in inner_loop_pre_next.left.value[2].val.name
182 assert isinstance(inner_loop_pre_next.right, Call)
183 assert isinstance(inner_loop_pre_next.right.func, Name)
184 assert inner_loop_pre_next.right.func.name == "next"
185 assert len(inner_loop_pre_next.right.arguments) == 1
186 assert isinstance(inner_loop_pre_next.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
187 assert "generated_iter_1" == inner_loop_pre_next.right.arguments[0].val.name
189 # Inner Loop Test
190 ##################
191 inner_loop_test = inner_loop.expr
192 assert isinstance(inner_loop_test, Operator)
193 assert inner_loop_test.op == "ast.Eq"
195 assert isinstance(inner_loop_test.operands[0], Name)
196 assert "sc_" in inner_loop_test.operands[0].name
198 assert isinstance(inner_loop_test.operands[1], CASTLiteralValue)
199 assert inner_loop_test.operands[1].value == False
200 assert inner_loop_test.operands[1].value_type == ScalarType.BOOLEAN
202 # Inner Loop Body
203 ##################
204 loop_body_append = inner_loop.body[0]
205 assert isinstance(loop_body_append, Call)
206 assert isinstance(loop_body_append.func, Attribute)
207 assert isinstance(loop_body_append.func.attr, Name)
208 assert loop_body_append.func.attr.name == "append"
210 assert isinstance(loop_body_append.func.value, Name)
211 assert "list__temp_" == loop_body_append.func.value.name
213 assert len(loop_body_append.arguments) == 1
214 assert isinstance(loop_body_append.arguments[0], Operator)
215 assert loop_body_append.arguments[0].op == "ast.Mult"
217 assert isinstance(loop_body_append.arguments[0].operands[0], Name)
218 assert loop_body_append.arguments[0].operands[0].name == "a"
220 assert isinstance(loop_body_append.arguments[0].operands[1], Name)
221 assert loop_body_append.arguments[0].operands[1].name == "b"
223 loop_body_next = inner_loop.body[1]
224 assert isinstance(loop_body_next, Assignment)
225 assert isinstance(loop_body_next.left, CASTLiteralValue)
226 assert loop_body_next.left.value_type == StructureType.TUPLE
227 assert isinstance(loop_body_next.left.value[0], Var)
228 assert isinstance(loop_body_next.left.value[0].val, Name)
229 assert loop_body_next.left.value[0].val.name == "b"
231 assert isinstance(loop_body_next.left.value[1], Var)
232 assert isinstance(loop_body_next.left.value[1].val, Name)
233 assert "generated_iter_1" == loop_body_next.left.value[1].val.name
235 assert isinstance(loop_body_next.left.value[2], Var)
236 assert isinstance(loop_body_next.left.value[2].val, Name)
237 assert "sc_" in loop_body_next.left.value[2].val.name
239 assert isinstance(loop_body_next.right, Call)
240 assert isinstance(loop_body_next.right.func, Name)
241 assert loop_body_next.right.func.name == "next"
242 assert len(loop_body_next.right.arguments) == 1
243 assert isinstance(loop_body_next.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
244 assert "generated_iter_1" == loop_body_next.right.arguments[0].val.name
246 test_inner_loop(inner_loop_body)
247 # Loop Call
248 #############
250 call_node = comp_cast.nodes[0].body[1]
251 assert isinstance(call_node, Assignment)
252 assert isinstance(call_node.left, Var)
253 assert isinstance(call_node.left.val, Name)
254 assert call_node.left.val.name == "L"
256 assert isinstance(call_node.right, Call)
257 assert isinstance(call_node.right.func, Name)
258 assert "%comprehension_list_0" == call_node.right.func.name
259 assert len(call_node.right.arguments) == 0
261def test_comp2():
262 comp_cast = generate_cast(comp2())
264 # Test list comprehension with conditional
265 func_node = comp_cast.nodes[0].body[0]
266 assert isinstance(func_node, FunctionDef)
267 assert "%comprehension_list_0" == func_node.name.name
268 assert len(func_node.func_args) == 0
270 func_node_body = func_node.body
271 func_asg = func_node.body[0]
272 func_ret = func_node.body[2]
273 assert isinstance(func_asg, Assignment)
274 assert isinstance(func_asg.left, Var)
275 assert isinstance(func_asg.left.val, Name)
276 assert "list__temp_" == func_asg.left.val.name
278 assert isinstance(func_asg.right, CASTLiteralValue)
279 assert func_asg.right.value_type == StructureType.LIST
280 assert func_asg.right.value == []
282 assert isinstance(func_ret, ModelReturn)
283 assert isinstance(func_ret.value, Name)
284 assert "list__temp_" == func_ret.value.name
286 func_loop = func_node_body[1]
287 assert isinstance(func_loop, Loop)
288 assert func_loop.post == []
290 # Loop Pre
291 #############
292 func_loop_pre = func_loop.pre
293 loop_pre_iter = func_loop_pre[0]
294 assert isinstance(loop_pre_iter, Assignment)
295 assert isinstance(loop_pre_iter.left, Var)
296 assert isinstance(loop_pre_iter.left.val, Name)
297 assert "generated_iter_0" == loop_pre_iter.left.val.name
299 assert isinstance(loop_pre_iter.right, Call)
300 assert isinstance(loop_pre_iter.right.func, Name)
301 assert loop_pre_iter.right.func.name == "iter"
303 assert len(loop_pre_iter.right.arguments) == 1
304 range_call = loop_pre_iter.right.arguments[0]
305 assert isinstance(range_call, Call)
306 assert isinstance(range_call.func, Name)
307 assert range_call.func.name == "range"
308 assert len(range_call.arguments) == 3
309 assert isinstance(range_call.arguments[0], CASTLiteralValue)
310 assert isinstance(range_call.arguments[1], CASTLiteralValue)
311 assert isinstance(range_call.arguments[2], CASTLiteralValue)
313 loop_pre_next = func_loop_pre[1]
314 assert isinstance(loop_pre_next, Assignment)
315 assert isinstance(loop_pre_next.left, CASTLiteralValue)
316 assert loop_pre_next.left.value_type == StructureType.TUPLE
317 assert isinstance(loop_pre_next.left.value[0], Var)
318 assert isinstance(loop_pre_next.left.value[0].val, Name)
319 assert loop_pre_next.left.value[0].val.name == "a"
321 assert isinstance(loop_pre_next.left.value[1], Var)
322 assert isinstance(loop_pre_next.left.value[1].val, Name)
323 assert "generated_iter_0" == loop_pre_next.left.value[1].val.name
325 assert isinstance(loop_pre_next.left.value[2], Var)
326 assert isinstance(loop_pre_next.left.value[2].val, Name)
327 assert "sc_" in loop_pre_next.left.value[2].val.name
329 assert isinstance(loop_pre_next.right, Call)
330 assert isinstance(loop_pre_next.right.func, Name)
331 assert loop_pre_next.right.func.name == "next"
332 assert len(loop_pre_next.right.arguments) == 1
333 assert isinstance(loop_pre_next.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
334 assert "generated_iter_0" == loop_pre_next.right.arguments[0].val.name
337 # Loop Test
338 #############
339 func_loop_test = func_loop.expr
340 assert isinstance(func_loop_test, Operator)
341 assert func_loop_test.op == "ast.Eq"
343 assert isinstance(func_loop_test.operands[0], Name)
344 assert "sc_" in func_loop_test.operands[0].name
346 assert isinstance(func_loop_test.operands[1], CASTLiteralValue)
347 assert func_loop_test.operands[1].value == False
348 assert func_loop_test.operands[1].value_type == ScalarType.BOOLEAN
351 # Loop Body
352 #############
353 func_loop_body = func_loop.body
354 loop_body_if = func_loop_body[0]
355 assert isinstance(loop_body_if, ModelIf)
356 body_if_test = loop_body_if.expr
357 assert isinstance(body_if_test, Operator)
358 assert body_if_test.op == "ast.Eq"
359 assert isinstance(body_if_test.operands[0], Operator)
360 assert body_if_test.operands[0].op == "ast.Mod"
361 assert isinstance(body_if_test.operands[0].operands[0], Name)
362 assert body_if_test.operands[0].operands[0].name == "a"
364 assert isinstance(body_if_test.operands[0].operands[1], CASTLiteralValue)
365 assert isinstance(body_if_test.operands[1], CASTLiteralValue)
367 body_if_body = loop_body_if.body[0]
369 assert isinstance(body_if_body, Call)
370 assert isinstance(body_if_body.func, Attribute)
371 assert isinstance(body_if_body.func.attr, Name)
372 assert body_if_body.func.attr.name == "append"
374 assert isinstance(body_if_body.func.value, Name)
375 assert "list__temp_" in body_if_body.func.value.name
377 assert len(body_if_body.arguments) == 1
378 assert isinstance(body_if_body.arguments[0], Var)
379 assert body_if_body.arguments[0].val.name == "a"
381 loop_body_next = func_loop_body[1]
382 assert isinstance(loop_body_next, Assignment)
383 assert isinstance(loop_body_next.left, CASTLiteralValue)
384 assert loop_body_next.left.value_type == StructureType.TUPLE
385 assert isinstance(loop_body_next.left.value[0], Var)
386 assert isinstance(loop_body_next.left.value[0].val, Name)
387 assert loop_body_next.left.value[0].val.name == "a"
389 assert isinstance(loop_body_next.left.value[1], Var)
390 assert isinstance(loop_body_next.left.value[1].val, Name)
391 assert "generated_iter_0" == loop_body_next.left.value[1].val.name
393 assert isinstance(loop_body_next.left.value[2], Var)
394 assert isinstance(loop_body_next.left.value[2].val, Name)
395 assert "sc_" in loop_body_next.left.value[2].val.name
397 assert isinstance(loop_body_next.right, Call)
398 assert isinstance(loop_body_next.right.func, Name)
399 assert loop_body_next.right.func.name == "next"
400 assert len(loop_body_next.right.arguments) == 1
401 assert isinstance(loop_body_next.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
402 assert "generated_iter_0" == loop_body_next.right.arguments[0].val.name
404 call_node = comp_cast.nodes[0].body[1]
405 assert isinstance(call_node, Assignment)
406 assert isinstance(call_node.left, Var)
407 assert isinstance(call_node.left.val, Name)
408 assert call_node.left.val.name == "L"
410 assert isinstance(call_node.right, Call)
411 assert isinstance(call_node.right.func, Name)
412 assert "%comprehension_list_0" == call_node.right.func.name
413 assert len(call_node.right.arguments) == 0
415def test_comp3():
416 comp_cast = generate_cast(comp3())
418 # Test list comprehension with conditional
419 func_node = comp_cast.nodes[0].body[0]
420 assert isinstance(func_node, FunctionDef)
421 assert "%comprehension_list_0" == func_node.name.name
422 assert len(func_node.func_args) == 0
424 func_node_body = func_node.body
425 func_asg = func_node.body[0]
426 func_ret = func_node.body[2]
427 assert isinstance(func_asg, Assignment)
428 assert isinstance(func_asg.left, Var)
429 assert isinstance(func_asg.left.val, Name)
430 assert "list__temp_" == func_asg.left.val.name
432 assert isinstance(func_asg.right, CASTLiteralValue)
433 assert func_asg.right.value_type == StructureType.LIST
434 assert func_asg.right.value == []
436 assert isinstance(func_ret, ModelReturn)
437 assert isinstance(func_ret.value, Name)
438 assert "list__temp_" == func_ret.value.name
440 func_loop = func_node_body[1]
441 assert isinstance(func_loop, Loop)
442 assert func_loop.post == []
444 # Loop Pre
445 #############
446 func_loop_pre = func_loop.pre
447 loop_pre_iter = func_loop_pre[0]
448 assert isinstance(loop_pre_iter, Assignment)
449 assert isinstance(loop_pre_iter.left, Var)
450 assert isinstance(loop_pre_iter.left.val, Name)
451 assert "generated_iter_0" == loop_pre_iter.left.val.name
453 assert isinstance(loop_pre_iter.right, Call)
454 assert isinstance(loop_pre_iter.right.func, Name)
455 assert loop_pre_iter.right.func.name == "iter"
457 assert len(loop_pre_iter.right.arguments) == 1
458 range_call = loop_pre_iter.right.arguments[0]
459 assert isinstance(range_call, Call)
460 assert isinstance(range_call.func, Name)
461 assert range_call.func.name == "range"
462 assert len(range_call.arguments) == 3
463 assert isinstance(range_call.arguments[0], CASTLiteralValue)
464 assert isinstance(range_call.arguments[1], CASTLiteralValue)
465 assert isinstance(range_call.arguments[2], CASTLiteralValue)
467 loop_pre_next = func_loop_pre[1]
468 assert isinstance(loop_pre_next, Assignment)
469 assert isinstance(loop_pre_next.left, CASTLiteralValue)
470 assert loop_pre_next.left.value_type == StructureType.TUPLE
471 assert isinstance(loop_pre_next.left.value[0], Var)
472 assert isinstance(loop_pre_next.left.value[0].val, Name)
473 assert loop_pre_next.left.value[0].val.name == "a"
475 assert isinstance(loop_pre_next.left.value[1], Var)
476 assert isinstance(loop_pre_next.left.value[1].val, Name)
477 assert "generated_iter_0" == loop_pre_next.left.value[1].val.name
479 assert isinstance(loop_pre_next.left.value[2], Var)
480 assert isinstance(loop_pre_next.left.value[2].val, Name)
481 assert "sc_" in loop_pre_next.left.value[2].val.name
483 assert isinstance(loop_pre_next.right, Call)
484 assert isinstance(loop_pre_next.right.func, Name)
485 assert loop_pre_next.right.func.name == "next"
486 assert len(loop_pre_next.right.arguments) == 1
487 assert isinstance(loop_pre_next.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
488 assert "generated_iter_0" == loop_pre_next.right.arguments[0].val.name
491 # Loop Test
492 #############
493 func_loop_test = func_loop.expr
494 assert isinstance(func_loop_test, Operator)
495 assert func_loop_test.op == "ast.Eq"
497 assert isinstance(func_loop_test.operands[0], Name)
498 assert "sc_" in func_loop_test.operands[0].name
500 assert isinstance(func_loop_test.operands[1], CASTLiteralValue)
501 assert func_loop_test.operands[1].value == False
502 assert func_loop_test.operands[1].value_type == ScalarType.BOOLEAN
505 # Loop Body
506 #############
507 func_loop_body = func_loop.body
508 loop_body_if = func_loop_body[0]
509 assert isinstance(loop_body_if, ModelIf)
511 body_if_test = loop_body_if.expr
512 assert isinstance(body_if_test, Operator)
513 assert body_if_test.op == "ast.Eq"
514 assert isinstance(body_if_test.operands[0], Operator)
515 assert body_if_test.operands[0].op == "ast.Mod"
516 assert isinstance(body_if_test.operands[0].operands[0], Name)
517 assert body_if_test.operands[0].operands[0].name == "a"
518 assert isinstance(body_if_test.operands[0].operands[1], CASTLiteralValue)
519 assert isinstance(body_if_test.operands[1], CASTLiteralValue)
521 body_if_body = loop_body_if.body[0]
522 assert isinstance(body_if_body, Loop)
524 def test_inner_body_inner_loop(node: Loop):
525 pre = node.pre
527 loop_pre_iter = pre[0]
528 assert isinstance(loop_pre_iter, Assignment)
529 assert isinstance(loop_pre_iter.left, Var)
530 assert isinstance(loop_pre_iter.left.val, Name)
531 assert "generated_iter_1" == loop_pre_iter.left.val.name
533 assert isinstance(loop_pre_iter.right, Call)
534 assert isinstance(loop_pre_iter.right.func, Name)
535 assert loop_pre_iter.right.func.name == "iter"
537 assert len(loop_pre_iter.right.arguments) == 1
538 range_call = loop_pre_iter.right.arguments[0]
539 assert isinstance(range_call, Call)
540 assert isinstance(range_call.func, Name)
541 assert range_call.func.name == "range"
542 assert len(range_call.arguments) == 3
543 assert isinstance(range_call.arguments[0], CASTLiteralValue)
544 assert isinstance(range_call.arguments[1], CASTLiteralValue)
545 assert isinstance(range_call.arguments[2], CASTLiteralValue)
547 loop_pre_next = pre[1]
548 assert isinstance(loop_pre_next, Assignment)
549 assert isinstance(loop_pre_next.left, CASTLiteralValue)
550 assert loop_pre_next.left.value_type == StructureType.TUPLE
551 assert isinstance(loop_pre_next.left.value[0], Var)
552 assert isinstance(loop_pre_next.left.value[0].val, Name)
553 assert loop_pre_next.left.value[0].val.name == "b"
555 assert isinstance(loop_pre_next.left.value[1], Var)
556 assert isinstance(loop_pre_next.left.value[1].val, Name)
557 assert "generated_iter_1" == loop_pre_next.left.value[1].val.name
559 assert isinstance(loop_pre_next.left.value[2], Var)
560 assert isinstance(loop_pre_next.left.value[2].val, Name)
561 assert "sc_" in loop_pre_next.left.value[2].val.name
563 assert isinstance(loop_pre_next.right, Call)
564 assert isinstance(loop_pre_next.right.func, Name)
565 assert loop_pre_next.right.func.name == "next"
566 assert len(loop_pre_next.right.arguments) == 1
567 assert isinstance(loop_pre_next.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
568 assert "generated_iter_1" == loop_pre_next.right.arguments[0].val.name
570 test = node.expr
571 assert isinstance(test, Operator)
572 assert test.op =="ast.Eq"
573 assert isinstance(test.operands[0], Name)
574 assert "sc_" in test.operands[0].name
575 assert isinstance(test.operands[1], CASTLiteralValue)
576 assert test.operands[1].value_type == ScalarType.BOOLEAN
577 assert test.operands[1].value == False
579 body = node.body[0]
580 assert isinstance(body, ModelIf)
581 assert isinstance(body.expr, Operator)
582 assert body.expr.op == "ast.Eq"
583 assert isinstance(body.expr.operands[0], Operator)
584 assert body.expr.operands[0].op == "ast.Mod"
585 assert isinstance(body.expr.operands[0].operands[0], Name)
586 assert body.expr.operands[0].operands[0].name == "b"
587 assert isinstance(body.expr.operands[0].operands[1], CASTLiteralValue)
589 assert isinstance(body.expr.operands[1], CASTLiteralValue)
591 assert isinstance(body.body[0], Call)
592 assert isinstance(body.body[0].func, Attribute)
593 assert isinstance(body.body[0].func.attr, Name)
594 assert body.body[0].func.attr.name == "append"
596 assert isinstance(body.body[0].func.value, Name)
597 assert "list__temp_" in body.body[0].func.value.name
599 assert len(body.body[0].arguments) == 1
600 assert isinstance(body.body[0].arguments[0], Operator)
601 assert body.body[0].arguments[0].op == "ast.Mult"
603 assert isinstance(body.body[0].arguments[0].operands[0], Name)
604 assert body.body[0].arguments[0].operands[0].name == "a"
606 assert isinstance(body.body[0].arguments[0].operands[1], Name)
607 assert body.body[0].arguments[0].operands[1].name == "b"
609 body_2 = node.body[1]
610 assert isinstance(body_2, Assignment)
611 assert isinstance(body_2.left, CASTLiteralValue)
612 assert body_2.left.value_type == StructureType.TUPLE
613 assert isinstance(body_2.left.value[0], Var)
614 assert isinstance(body_2.left.value[0].val, Name)
615 assert body_2.left.value[0].val.name == "b"
617 assert isinstance(body_2.left.value[1], Var)
618 assert isinstance(body_2.left.value[1].val, Name)
619 assert "generated_iter_1" == body_2.left.value[1].val.name
621 assert isinstance(body_2.left.value[2], Var)
622 assert isinstance(body_2.left.value[2].val, Name)
623 assert "sc_" in body_2.left.value[2].val.name
625 assert isinstance(body_2.right, Call)
626 assert isinstance(body_2.right.func, Name)
627 assert body_2.right.func.name == "next"
628 assert len(body_2.right.arguments) == 1
629 assert isinstance(body_2.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
630 assert "generated_iter_1" == body_2.right.arguments[0].val.name
632 test_inner_body_inner_loop(body_if_body)
634 loop_body_next = func_loop_body[1]
635 assert isinstance(loop_body_next, Assignment)
636 assert isinstance(loop_body_next.left, CASTLiteralValue)
637 assert loop_body_next.left.value_type == StructureType.TUPLE
638 assert isinstance(loop_body_next.left.value[0], Var)
639 assert isinstance(loop_body_next.left.value[0].val, Name)
640 assert loop_body_next.left.value[0].val.name == "a"
642 assert isinstance(loop_body_next.left.value[1], Var)
643 assert isinstance(loop_body_next.left.value[1].val, Name)
644 assert "generated_iter_0" == loop_body_next.left.value[1].val.name
646 assert isinstance(loop_body_next.left.value[2], Var)
647 assert isinstance(loop_body_next.left.value[2].val, Name)
648 assert "sc_" in loop_body_next.left.value[2].val.name
650 assert isinstance(loop_body_next.right, Call)
651 assert isinstance(loop_body_next.right.func, Name)
652 assert loop_body_next.right.func.name == "next"
653 assert len(loop_body_next.right.arguments) == 1
654 assert isinstance(loop_body_next.right.arguments[0], Var) # NOTE: This should be Name, not Var, but it's ok for now
655 assert "generated_iter_0" == loop_body_next.right.arguments[0].val.name
658 # Calling the comprehension node
659 call_node = comp_cast.nodes[0].body[1]
660 assert isinstance(call_node, Assignment)
661 assert isinstance(call_node.left, Var)
662 assert isinstance(call_node.left.val, Name)
663 assert call_node.left.val.name == "L"
665 assert isinstance(call_node.right, Call)
666 assert isinstance(call_node.right.func, Name)
667 assert "%comprehension_list_0" == call_node.right.func.name
668 assert len(call_node.right.arguments) == 0