Coverage for skema/program_analysis/tests/test_conditional_cast.py: 100%
120 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 CASTLiteralValue,
9 ModelIf,
10 Operator
11)
13def cond1():
14 return """
15x = 2
17if x < 5:
18 x = x + 1
19else:
20 x = x - 3
21 """
23def cond2():
24 return """
25x = 2
26y = 3
28if x < 5:
29 x = 1
30 y = 2
31 x = x * y
32else:
33 x = x - 3
34 """
36def cond3():
37 return """
38x = 2
39y = 4
41if x < 5:
42 x = x + y
43 y = 1
44elif x > 10:
45 y = x + 2
46 x = 1
47elif x == 30:
48 x = 1
49 y = 2
50 z = x * y
51else:
52 x = 0
53 y = x - 2
54 """
56def generate_cast(test_file_string):
57 # use Python to CAST
58 out_cast = TS2CAST(test_file_string, from_file=False).out_cast
60 return out_cast
62def test_cond1():
63 exp_cast = generate_cast(cond1())
65 # Test basic conditional
66 asg_node = exp_cast.nodes[0].body[0]
67 cond_node = exp_cast.nodes[0].body[1]
69 assert isinstance(asg_node, Assignment)
70 assert isinstance(asg_node.left, Var)
71 assert isinstance(asg_node.left.val, Name)
72 assert asg_node.left.val.name == "x"
74 assert isinstance(asg_node.right, CASTLiteralValue)
75 assert asg_node.right.value_type == "Integer"
76 assert asg_node.right.value == '2'
78 assert isinstance(cond_node, ModelIf)
79 cond_expr = cond_node.expr
80 cond_body = cond_node.body
81 cond_else = cond_node.orelse
83 assert isinstance(cond_expr, Operator)
84 assert cond_expr.op == "ast.Lt"
85 assert isinstance(cond_expr.operands[0], Name)
86 assert isinstance(cond_expr.operands[1], CASTLiteralValue)
88 assert len(cond_body) == 1
89 assert isinstance(cond_body[0], Assignment)
90 assert isinstance(cond_body[0].left, Var)
91 assert isinstance(cond_body[0].right, Operator)
92 assert cond_body[0].right.op == "ast.Add"
94 assert len(cond_else) == 1
95 assert isinstance(cond_else[0], Assignment)
96 assert isinstance(cond_else[0].left, Var)
97 assert isinstance(cond_else[0].right, Operator)
98 assert cond_else[0].right.op == "ast.Sub"
101def test_cond2():
102 exp_cast = generate_cast(cond2())
104 # Test multiple variable conditional
105 asg_node = exp_cast.nodes[0].body[0]
106 cond_node = exp_cast.nodes[0].body[2]
108 assert isinstance(asg_node, Assignment)
109 assert isinstance(asg_node.left, Var)
110 assert isinstance(asg_node.left.val, Name)
111 assert asg_node.left.val.name == "x"
112 assert asg_node.left.val.id == 0
114 assert isinstance(asg_node.right, CASTLiteralValue)
115 assert asg_node.right.value_type == "Integer"
116 assert asg_node.right.value == '2'
118 asg_node = exp_cast.nodes[0].body[1]
119 assert isinstance(asg_node, Assignment)
120 assert isinstance(asg_node.left, Var)
121 assert isinstance(asg_node.left.val, Name)
122 assert asg_node.left.val.name == "y"
123 assert asg_node.left.val.id == 1
125 assert isinstance(asg_node.right, CASTLiteralValue)
126 assert asg_node.right.value_type == "Integer"
127 assert asg_node.right.value == '3'
129 assert isinstance(cond_node, ModelIf)
130 cond_expr = cond_node.expr
131 cond_body = cond_node.body
132 cond_else = cond_node.orelse
134 assert isinstance(cond_expr, Operator)
135 assert cond_expr.op == "ast.Lt"
136 assert isinstance(cond_expr.operands[0], Name)
137 assert cond_expr.operands[0].name == "x"
138 assert isinstance(cond_expr.operands[1], CASTLiteralValue)
139 assert cond_expr.operands[1].value_type == "Integer"
140 assert cond_expr.operands[1].value == "5"
142 assert len(cond_body) == 3
143 assert isinstance(cond_body[0], Assignment)
144 assert isinstance(cond_body[0].left, Var)
145 assert cond_body[0].left.val.name == "x"
146 assert isinstance(cond_body[0].right, CASTLiteralValue)
147 assert cond_body[0].right.value == "1"
149 assert isinstance(cond_body[1], Assignment)
150 assert isinstance(cond_body[1].left, Var)
151 assert cond_body[1].left.val.name == "y"
152 assert isinstance(cond_body[1].right, CASTLiteralValue)
153 assert cond_body[1].right.value == "2"
155 assert isinstance(cond_body[2], Assignment)
156 assert isinstance(cond_body[2].left, Var)
157 assert isinstance(cond_body[2].right, Operator)
159 assert cond_body[2].right.op == "ast.Mult"
161 assert isinstance(cond_body[2].right.operands[0], Name)
162 assert cond_body[2].right.operands[0].name == "x"
163 assert cond_body[2].right.operands[0].id == 0
164 assert isinstance(cond_body[2].right.operands[1], Name)
165 assert cond_body[2].right.operands[1].name == "y"
166 assert cond_body[2].right.operands[1].id == 1
168 assert len(cond_else) == 1
169 assert isinstance(cond_else[0], Assignment)
170 assert isinstance(cond_else[0].left, Var)
171 assert isinstance(cond_else[0].right, Operator)
172 assert cond_else[0].right.op == "ast.Sub"
174def test_cond3():
175 exp_cast = generate_cast(cond3())
177 # Test nested ifs
178 cond_node = exp_cast.nodes[0].body[2]
180 assert isinstance(cond_node, ModelIf)
181 cond_body = cond_node.body
182 cond_else = cond_node.orelse
184 assert len(cond_body) == 2
185 assert len(cond_else) == 1
186 assert isinstance(cond_else[0], ModelIf)
187 nested_if = cond_else[0]
188 cond_body = nested_if.body
189 cond_else = nested_if.orelse
191 assert len(cond_body) == 2
192 assert len(cond_else) == 1
193 assert isinstance(cond_else[0], ModelIf)
194 nested_if = cond_else[0]
195 cond_body = nested_if.body
196 cond_else = nested_if.orelse
198 assert len(cond_body) == 3
199 assert len(cond_else) == 2
200 assert isinstance(cond_else[0], Assignment)
201 assert isinstance(cond_else[1], Assignment)