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

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) 

12 

13def cond1(): 

14 return """ 

15x = 2 

16 

17if x < 5: 

18 x = x + 1 

19else: 

20 x = x - 3 

21 """ 

22 

23def cond2(): 

24 return """ 

25x = 2 

26y = 3 

27 

28if x < 5: 

29 x = 1 

30 y = 2 

31 x = x * y 

32else: 

33 x = x - 3  

34 """ 

35 

36def cond3(): 

37 return """ 

38x = 2 

39y = 4 

40 

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 """ 

55 

56def generate_cast(test_file_string): 

57 # use Python to CAST 

58 out_cast = TS2CAST(test_file_string, from_file=False).out_cast 

59 

60 return out_cast 

61 

62def test_cond1(): 

63 exp_cast = generate_cast(cond1()) 

64 

65 # Test basic conditional 

66 asg_node = exp_cast.nodes[0].body[0] 

67 cond_node = exp_cast.nodes[0].body[1] 

68 

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" 

73 

74 assert isinstance(asg_node.right, CASTLiteralValue) 

75 assert asg_node.right.value_type == "Integer" 

76 assert asg_node.right.value == '2' 

77 

78 assert isinstance(cond_node, ModelIf) 

79 cond_expr = cond_node.expr 

80 cond_body = cond_node.body 

81 cond_else = cond_node.orelse 

82 

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) 

87 

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" 

93 

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" 

99 

100 

101def test_cond2(): 

102 exp_cast = generate_cast(cond2()) 

103 

104 # Test multiple variable conditional 

105 asg_node = exp_cast.nodes[0].body[0] 

106 cond_node = exp_cast.nodes[0].body[2] 

107 

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 

113 

114 assert isinstance(asg_node.right, CASTLiteralValue) 

115 assert asg_node.right.value_type == "Integer" 

116 assert asg_node.right.value == '2' 

117 

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 

124 

125 assert isinstance(asg_node.right, CASTLiteralValue) 

126 assert asg_node.right.value_type == "Integer" 

127 assert asg_node.right.value == '3' 

128 

129 assert isinstance(cond_node, ModelIf) 

130 cond_expr = cond_node.expr 

131 cond_body = cond_node.body 

132 cond_else = cond_node.orelse 

133 

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" 

141 

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" 

148 

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" 

154 

155 assert isinstance(cond_body[2], Assignment) 

156 assert isinstance(cond_body[2].left, Var) 

157 assert isinstance(cond_body[2].right, Operator) 

158 

159 assert cond_body[2].right.op == "ast.Mult" 

160 

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 

167 

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" 

173 

174def test_cond3(): 

175 exp_cast = generate_cast(cond3()) 

176 

177 # Test nested ifs 

178 cond_node = exp_cast.nodes[0].body[2] 

179 

180 assert isinstance(cond_node, ModelIf) 

181 cond_body = cond_node.body 

182 cond_else = cond_node.orelse 

183 

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 

190 

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 

197 

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)