Coverage for skema/program_analysis/tests/test_list_proc_cast.py: 100%

190 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 FunctionDef, 

7 ModelReturn, 

8 Var, 

9 Call, 

10 Name, 

11 CASTLiteralValue, 

12) 

13 

14def list1(): 

15 return """ 

16x = [1,2,3] 

17y = x[0] 

18 """ 

19 

20def list2(): 

21 return """ 

22x = [1,[2,3]] 

23y = x[1][0] 

24 """ 

25 

26def list3(): 

27 return """ 

28x = [1,2,3,4,5] 

29y = x[0:3] 

30z = x[0:3:2] 

31 """ 

32 

33def list4(): 

34 return """ 

35def foo(): 

36 return 2 

37  

38x = [1,2,3,4,5] 

39y = x[0:foo()] 

40 """ 

41 

42 

43def generate_cast(test_file_string): 

44 # use Python to CAST 

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

46 

47 return out_cast 

48 

49def test_list1(): 

50 cast = generate_cast(list1()) 

51 

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

53 index_node = cast.nodes[0].body[1] 

54 

55 assert isinstance(asg_node, Assignment) 

56 assert isinstance(asg_node.left, Var) 

57 assert isinstance(asg_node.left.val, Name) 

58 assert asg_node.left.val.name == "x" 

59 assert asg_node.left.val.id == 0 

60 

61 assert isinstance(asg_node.right, CASTLiteralValue) 

62 assert asg_node.right.value_type == "List" 

63 

64 assert isinstance(index_node, Assignment) 

65 assert isinstance(index_node.left, Var) 

66 assert isinstance(index_node.left.val, Name) 

67 assert index_node.left.val.name == "y" 

68 assert index_node.left.val.id == 2 

69 

70 index_call = index_node.right 

71 assert isinstance(index_call, Call) 

72 assert isinstance(index_call.func, Name) 

73 assert index_call.func.name == "_get" 

74 assert index_call.func.id == 1 

75 assert len(index_call.arguments) == 2 

76 

77 assert isinstance(index_call.arguments[0], Name), f"is type{type(index_call.arguments[0])}" 

78 assert index_call.arguments[0].name == "x" 

79 assert index_call.arguments[0].id == 0 

80 

81 assert isinstance(index_call.arguments[1], CASTLiteralValue) 

82 assert index_call.arguments[1].value_type == "Integer" 

83 assert index_call.arguments[1].value == "0" 

84 

85 

86def test_list2(): 

87 cast = generate_cast(list2()) 

88 

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

90 assert isinstance(asg_node, Assignment) 

91 assert isinstance(asg_node.left, Var) 

92 assert isinstance(asg_node.left.val, Name) 

93 assert asg_node.left.val.name == "x" 

94 assert asg_node.left.val.id == 0 

95 

96 assert isinstance(asg_node.right, CASTLiteralValue) 

97 assert asg_node.right.value_type == "List" 

98 second_list = asg_node.right.value[1] 

99 

100 assert isinstance(second_list, CASTLiteralValue) 

101 assert second_list.value_type == "List" 

102 assert isinstance(second_list.value[0], CASTLiteralValue) 

103 assert second_list.value[0].value == "2" 

104 

105 assert isinstance(second_list.value[1], CASTLiteralValue) 

106 assert second_list.value[1].value == "3" 

107 

108 index_node = cast.nodes[0].body[1] 

109 assert isinstance(index_node, Assignment) 

110 assert isinstance(index_node.left, Var) 

111 assert isinstance(index_node.left.val, Name) 

112 assert index_node.left.val.name == "y" 

113 assert index_node.left.val.id == 2 

114 

115 index_call = index_node.right 

116 assert isinstance(index_call, Call) 

117 assert isinstance(index_call.func, Name) 

118 assert index_call.func.name == "_get" 

119 assert index_call.func.id == 1 

120 assert len(index_call.arguments) == 2 

121 

122 arg_call = index_call.arguments[0] 

123 assert isinstance(arg_call, Call), f"is type{type(index_call.arguments[0])}" 

124 assert arg_call.func.name == "_get" 

125 assert arg_call.func.id == 1 

126 

127 assert len(arg_call.arguments) == 2 

128 assert isinstance(arg_call.arguments[0], Name) 

129 assert arg_call.arguments[0].name == "x" 

130 assert arg_call.arguments[0].id == 0 

131 

132 assert isinstance(arg_call.arguments[1], CASTLiteralValue) 

133 assert arg_call.arguments[1].value == "1" 

134 

135 assert isinstance(index_call.arguments[1], CASTLiteralValue) 

136 assert index_call.arguments[1].value_type == "Integer" 

137 assert index_call.arguments[1].value == "0" 

138 

139 

140def test_list3(): 

141 cast = generate_cast(list3()) 

142 

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

144 assert isinstance(asg_node, Assignment) 

145 assert isinstance(asg_node.left, Var) 

146 assert isinstance(asg_node.left.val, Name) 

147 assert asg_node.left.val.name == "x" 

148 assert asg_node.left.val.id == 0 

149 

150 assert isinstance(asg_node.right, CASTLiteralValue) 

151 assert asg_node.right.value_type == "List" 

152 assert len(asg_node.right.value) == 5 

153 

154 index_node = cast.nodes[0].body[1] 

155 assert isinstance(index_node, Assignment) 

156 assert isinstance(index_node.left, Var) 

157 assert isinstance(index_node.left.val, Name) 

158 assert index_node.left.val.name == "y" 

159 assert index_node.left.val.id == 2 

160 

161 index_call = index_node.right 

162 assert isinstance(index_call, Call) 

163 assert isinstance(index_call.func, Name) 

164 assert index_call.func.name == "_get" 

165 assert index_call.func.id == 1 

166 assert len(index_call.arguments) == 2 

167 

168 slice1 = index_call.arguments[0] 

169 assert isinstance(slice1, Name) 

170 assert slice1.name == "x" 

171 assert slice1.id == 0 

172 

173 slice2 = index_call.arguments[1] 

174 assert isinstance(slice2, CASTLiteralValue) 

175 assert slice2.value_type == "List" 

176 assert len(slice2.value) == 3 

177 

178 assert isinstance(slice2.value[0], CASTLiteralValue) 

179 assert slice2.value[0].value == "0" 

180 

181 assert isinstance(slice2.value[1], CASTLiteralValue) 

182 assert slice2.value[1].value == "3" 

183 

184 assert isinstance(slice2.value[2], CASTLiteralValue) 

185 assert slice2.value[2].value == "1" 

186 

187 second_idx = cast.nodes[0].body[2] 

188 assert isinstance(second_idx, Assignment) 

189 assert isinstance(second_idx.left, Var) 

190 assert second_idx.left.val.name == "z" 

191 assert second_idx.left.val.id == 3 

192 

193 second_call = second_idx.right 

194 assert isinstance(second_call, Call) 

195 assert isinstance(second_call.func, Name) 

196 assert second_call.func.name == "_get" 

197 assert second_call.func.id == 1 

198 

199 second_args = second_call.arguments 

200 assert len(second_args) == 2 

201 assert isinstance(second_args[0], Name) 

202 assert second_args[0].name == "x" 

203 assert second_args[0].id == 0 

204 

205 idx_args = second_args[1] 

206 assert isinstance(idx_args, CASTLiteralValue) 

207 assert idx_args.value_type == "List" 

208 assert len(idx_args.value) == 3 

209 

210 assert isinstance(idx_args.value[0], CASTLiteralValue) 

211 assert idx_args.value[0].value == "0" 

212 

213 assert isinstance(idx_args.value[1], CASTLiteralValue) 

214 assert idx_args.value[1].value == "3" 

215 

216 assert isinstance(idx_args.value[2], CASTLiteralValue) 

217 assert idx_args.value[2].value == "2" 

218 

219 

220def test_list4(): 

221 cast = generate_cast(list4()) 

222 

223 func_def_node = cast.nodes[0].body[0] 

224 assert isinstance(func_def_node, FunctionDef) 

225 assert func_def_node.name.name == "foo" 

226 assert func_def_node.name.id == 0 

227 assert isinstance(func_def_node.body[0], ModelReturn) 

228 assert isinstance(func_def_node.body[0].value, CASTLiteralValue) 

229 assert func_def_node.body[0].value.value == "2" 

230 

231 asg_node = cast.nodes[0].body[1] 

232 assert isinstance(asg_node, Assignment) 

233 assert isinstance(asg_node.left, Var) 

234 assert isinstance(asg_node.left.val, Name) 

235 assert asg_node.left.val.name == "x" 

236 assert asg_node.left.val.id == 1 

237 

238 assert isinstance(asg_node.right, CASTLiteralValue) 

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

240 assert len(asg_node.right.value) == 5 

241 

242 index_node = cast.nodes[0].body[2] 

243 assert isinstance(index_node, Assignment) 

244 assert isinstance(index_node.left, Var) 

245 assert isinstance(index_node.left.val, Name) 

246 assert index_node.left.val.name == "y" 

247 assert index_node.left.val.id == 3 

248 

249 index_call = index_node.right 

250 assert isinstance(index_call, Call) 

251 assert isinstance(index_call.func, Name) 

252 assert index_call.func.name == "_get" 

253 assert index_call.func.id == 2 

254 assert len(index_call.arguments) == 2 

255 

256 slice1 = index_call.arguments[0] 

257 assert isinstance(slice1, Name) 

258 assert slice1.name == "x" 

259 assert slice1.id == 1 

260 

261 slice2 = index_call.arguments[1] 

262 assert isinstance(slice2, CASTLiteralValue) 

263 assert slice2.value_type == "List" 

264 assert len(slice2.value) == 3 

265 

266 assert isinstance(slice2.value[0], CASTLiteralValue) 

267 assert slice2.value[0].value == "0" 

268 

269 assert isinstance(slice2.value[1], Call) 

270 assert slice2.value[1].func.name == "foo" 

271 assert slice2.value[1].func.id == 0 

272 

273 assert isinstance(slice2.value[2], CASTLiteralValue) 

274 assert slice2.value[2].value == "1"