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

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) 

14 

15def for1(): 

16 return """ 

17x = 7 

18for i in range(10): 

19 x = x + i 

20 """ 

21 

22def for2(): 

23 return """ 

24x = 1 

25for a,b in range(10): 

26 x = x + a + b 

27 """ 

28 

29def for3(): 

30 return """ 

31x = 1 

32L = [1,2,3] 

33 

34for i in L: 

35 x = x + i 

36 """ 

37 

38 

39def generate_cast(test_file_string): 

40 # use Python to CAST 

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

42 

43 return out_cast 

44 

45def test_for1(): 

46 cast = generate_cast(for1()) 

47 

48 asg_node = cast.nodes[0].body[0] 

49 loop_node = cast.nodes[0].body[1] 

50 

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" 

55 

56 assert isinstance(asg_node.right, CASTLiteralValue) 

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

58 assert asg_node.right.value == '7' 

59 

60 assert isinstance(loop_node, Loop) 

61 assert len(loop_node.pre) == 2 

62 

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" 

68 

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 

72 

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 

77 

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" 

84 

85 assert isinstance(loop_pre[1], Assignment) 

86 assert isinstance(loop_pre[1].left, CASTLiteralValue) 

87 assert loop_pre[1].left.value_type == "Tuple" 

88 

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" 

95 

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" 

100 

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" 

107 

108 assert isinstance(loop_test.operands[1], CASTLiteralValue) 

109 assert loop_test.operands[1].value_type == "Boolean" 

110 

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" 

118 

119 

120def test_for2(): 

121 cast = generate_cast(for2()) 

122 

123 asg_node = cast.nodes[0].body[0] 

124 loop_node = cast.nodes[0].body[1] 

125 

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" 

130 

131 assert isinstance(asg_node.right, CASTLiteralValue) 

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

133 assert asg_node.right.value == '1' 

134 

135 assert isinstance(loop_node, Loop) 

136 assert len(loop_node.pre) == 2 

137 

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" 

143 

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 

147 

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 

152 

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" 

159 

160 assert isinstance(loop_pre[1], Assignment) 

161 assert isinstance(loop_pre[1].left, CASTLiteralValue) 

162 assert loop_pre[1].left.value_type == "Tuple" 

163 

164 assert isinstance(loop_pre[1].left.value[0], CASTLiteralValue) 

165 assert loop_pre[1].left.value[0].value_type == "Tuple" 

166 

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" 

171 

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" 

176 

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" 

181 

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" 

188 

189 assert isinstance(loop_test.operands[1], CASTLiteralValue) 

190 assert loop_test.operands[1].value_type == "Boolean" 

191 

192 # Loop Body 

193 loop_body = loop_node.body 

194 body_asg = loop_body[0] 

195 assert isinstance(body_asg, Assignment) 

196 

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" 

201 

202 assert isinstance(body_asg.right.operands[0].operands[1], Name) 

203 assert body_asg.right.operands[0].operands[1].name == "a" 

204 

205 assert isinstance(body_asg.right.operands[1], Name) 

206 assert body_asg.right.operands[1].name == "b" 

207 

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" 

213 

214 

215def test_for3(): 

216 cast = generate_cast(for3()) 

217 

218 asg_node = cast.nodes[0].body[0] 

219 list_node = cast.nodes[0].body[1] 

220 loop_node = cast.nodes[0].body[2] 

221 

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" 

226 

227 assert isinstance(asg_node.right, CASTLiteralValue) 

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

229 assert asg_node.right.value == '1' 

230 

231 assert isinstance(loop_node, Loop) 

232 assert len(loop_node.pre) == 2 

233 

234 assert isinstance(list_node, Assignment) 

235 assert isinstance(list_node.left, Var) 

236 assert list_node.left.val.name == "L" 

237 

238 assert isinstance(list_node.right, CASTLiteralValue) 

239 assert list_node.right.value_type == "List" 

240 

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" 

246 

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 

250 

251 assert len(iter_args) == 1 

252 assert isinstance(iter_args[0], Var) 

253 assert iter_args[0].val.name == "L" 

254 

255 assert isinstance(loop_pre[1], Assignment) 

256 assert isinstance(loop_pre[1].left, CASTLiteralValue) 

257 assert loop_pre[1].left.value_type == "Tuple" 

258 

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" 

265 

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" 

270 

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" 

277 

278 assert isinstance(loop_test.operands[1], CASTLiteralValue) 

279 assert loop_test.operands[1].value_type == "Boolean" 

280 

281 # Loop Body 

282 loop_body = loop_node.body 

283 next_call = loop_body[-1] 

284 

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"