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

219 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.multi_file_ingester import process_file_system 

4from skema.gromet.fn import GrometFNModuleCollection 

5from skema.gromet.fn import FunctionType 

6import ast 

7 

8from skema.program_analysis.CAST.pythonAST import py_ast_to_cast 

9from skema.program_analysis.CAST2FN.model.cast import SourceRef 

10from skema.program_analysis.CAST2FN import cast 

11from skema.program_analysis.CAST2FN.cast import CAST 

12from skema.program_analysis.run_ann_cast_pipeline import ann_cast_pipeline 

13 

14def fun_arg_fun_call(): 

15 return """ 

16def foo(x,y,z): 

17 b = x(10) * 2 

18 c = b * y(3) 

19 a = x(z) + y(z) 

20 return a 

21 

22def a(f): return f + 1 

23def b(f): return f + 2 

24 

25foo(x=a,y=b,z=1) 

26 """ 

27 

28def generate_gromet(test_file_string): 

29 # use ast.Parse to get Python AST 

30 contents = ast.parse(test_file_string) 

31 

32 # use Python to CAST 

33 line_count = len(test_file_string.split("\n")) 

34 convert = py_ast_to_cast.PyASTToCAST("temp") 

35 C = convert.visit(contents, {}, {}) 

36 C.source_refs = [SourceRef("temp", None, None, 1, line_count)] 

37 out_cast = cast.CAST([C], "python") 

38 

39 # use AnnCastPipeline to create GroMEt 

40 gromet = ann_cast_pipeline(out_cast, gromet=True, to_file=False, from_obj=True) 

41 

42 return gromet 

43 

44def test_fun_arg_fun_call(): 

45 fun_gromet = generate_gromet(fun_arg_fun_call()) 

46 # Test basic properties of assignment node 

47 base_fn = fun_gromet.fn 

48 

49 assert len(base_fn.bf) == 4 

50 assert base_fn.bf[1].value.value_type == "string" 

51 assert base_fn.bf[1].value.value == "a" 

52 

53 assert base_fn.bf[2].value.value_type == "string" 

54 assert base_fn.bf[2].value.value == "b" 

55 

56 assert len(base_fn.pif) == 3 

57 assert len(base_fn.pof) == 3 

58 assert base_fn.pof[0].name == "x" 

59 assert base_fn.pof[0].box == 2 

60 

61 assert base_fn.pof[1].name == "y" 

62 assert base_fn.pof[1].box == 3 

63 

64 assert base_fn.pof[2].name == "z" 

65 assert base_fn.pof[2].box == 4 

66 

67 assert len(base_fn.wff) == 3 

68 assert base_fn.wff[0].src == 1 

69 assert base_fn.wff[0].tgt == 1 

70 

71 assert base_fn.wff[1].src == 2 

72 assert base_fn.wff[1].tgt == 2 

73 

74 assert base_fn.wff[2].src == 3 

75 assert base_fn.wff[2].tgt == 3 

76 

77 ##################################################################  

78 

79 foo_fn = fun_gromet.fn_array[0] 

80 assert len(foo_fn.opi) == 3 

81 assert foo_fn.opi[0].name == "x" 

82 assert foo_fn.opi[1].name == "y" 

83 assert foo_fn.opi[2].name == "z" 

84 

85 assert len(foo_fn.opo) == 1 

86 assert len(foo_fn.bf) == 3 

87 

88 assert len(foo_fn.pif) == 6 

89 assert foo_fn.pif[0].box == 1 

90 assert foo_fn.pif[1].box == 2 

91 assert foo_fn.pif[2].box == 2 

92 assert foo_fn.pif[3].box == 3 

93 assert foo_fn.pif[4].box == 3 

94 assert foo_fn.pif[5].box == 3 

95 

96 assert len(foo_fn.pof) == 3 

97 assert foo_fn.pof[0].box == 1 

98 assert foo_fn.pof[0].name == "b" 

99 

100 assert foo_fn.pof[1].box == 2 

101 assert foo_fn.pof[1].name == "c" 

102 

103 assert foo_fn.pof[2].box == 3 

104 assert foo_fn.pof[2].name == "a" 

105 

106 assert len(foo_fn.wfopi) == 5 

107 assert foo_fn.wfopi[0].src == 1 and foo_fn.wfopi[0].tgt == 1 

108 assert foo_fn.wfopi[1].src == 2 and foo_fn.wfopi[1].tgt == 2 

109 assert foo_fn.wfopi[2].src == 4 and foo_fn.wfopi[2].tgt == 1 

110 assert foo_fn.wfopi[3].src == 5 and foo_fn.wfopi[3].tgt == 3 

111 assert foo_fn.wfopi[4].src == 6 and foo_fn.wfopi[4].tgt == 2 

112 

113 assert len(foo_fn.wff) == 1 

114 assert foo_fn.wff[0].src == 3 and foo_fn.wff[0].tgt == 1 

115 

116 assert len(foo_fn.wfopo) == 1 

117 assert foo_fn.wfopo[0].src == 1 and foo_fn.wfopo[0].tgt == 3 

118 

119 

120 ##################################################################  

121 first_call_fn = fun_gromet.fn_array[1] 

122 assert len(first_call_fn.opi) == 1 

123 assert len(first_call_fn.opo) == 1 

124 

125 assert len(first_call_fn.bf) == 2 

126 assert first_call_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE 

127 assert first_call_fn.bf[0].name == "_call" 

128 

129 assert first_call_fn.bf[1].function_type == FunctionType.LITERAL 

130 assert first_call_fn.bf[1].value.value == 10 

131 

132 assert len(first_call_fn.pif) == 2 

133 assert first_call_fn.pif[0].box == 1 

134 assert first_call_fn.pif[1].box == 1 

135 

136 assert len(first_call_fn.pof) == 2 

137 assert first_call_fn.pof[0].box == 1 

138 assert first_call_fn.pof[1].box == 2 

139 

140 assert len(first_call_fn.wfopi) == 1 

141 assert first_call_fn.wfopi[0].src == 1 and first_call_fn.wfopi[0].tgt == 1 

142 

143 assert len(first_call_fn.wff) == 1 

144 assert first_call_fn.wff[0].src == 2 and first_call_fn.wff[0].tgt == 2 

145 

146 assert len(first_call_fn.wfopo) == 1 

147 assert first_call_fn.wfopo[0].src == 1 and first_call_fn.wfopo[0].tgt == 1 

148 

149 ##################################################################  

150 first_mult_fn = fun_gromet.fn_array[2] 

151 assert len(first_mult_fn.opi) == 1 

152 assert len(first_mult_fn.opo) == 1 

153 

154 assert len(first_mult_fn.bf) == 3 

155 assert first_mult_fn.bf[0].function_type == FunctionType.EXPRESSION 

156 assert first_mult_fn.bf[0].body == 2 

157 

158 assert first_mult_fn.bf[1].function_type == FunctionType.LITERAL 

159 assert first_mult_fn.bf[1].value.value == 2 

160 

161 assert first_mult_fn.bf[2].function_type == FunctionType.LANGUAGE_PRIMITIVE 

162 assert first_mult_fn.bf[2].name == "ast.Mult" 

163 

164 assert len(first_mult_fn.pif) == 3 

165 assert first_mult_fn.pif[0].box == 1 

166 assert first_mult_fn.pif[1].box == 3 

167 assert first_mult_fn.pif[2].box == 3 

168 

169 assert len(first_mult_fn.pof) == 3 

170 assert first_mult_fn.pof[0].box == 1 

171 assert first_mult_fn.pof[1].box == 2 

172 assert first_mult_fn.pof[2].box == 3 

173 

174 assert len(first_mult_fn.wfopi) == 1 

175 assert first_mult_fn.wfopi[0].src == 1 and first_mult_fn.wfopi[0].tgt == 1 

176 

177 assert len(first_mult_fn.wff) == 2 

178 assert first_mult_fn.wff[0].src == 2 and first_mult_fn.wff[0].tgt == 1 

179 assert first_mult_fn.wff[1].src == 3 and first_mult_fn.wff[1].tgt == 2 

180 

181 assert len(first_mult_fn.wfopo) == 1 

182 assert first_mult_fn.wfopo[0].src == 1 and first_mult_fn.wfopo[0].tgt == 3 

183 

184 ##################################################################  

185 second_call_fn = fun_gromet.fn_array[3] 

186 assert len(second_call_fn.opi) == 1 

187 assert len(second_call_fn.opo) == 1 

188 

189 assert len(second_call_fn.bf) == 2 

190 assert second_call_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE 

191 assert second_call_fn.bf[0].name == "_call" 

192 

193 assert second_call_fn.bf[1].function_type == FunctionType.LITERAL 

194 assert second_call_fn.bf[1].value.value == 3 

195 

196 assert len(second_call_fn.pif) == 2 

197 assert second_call_fn.pif[0].box == 1 

198 assert second_call_fn.pif[1].box == 1 

199 

200 assert len(second_call_fn.pof) == 2 

201 assert second_call_fn.pof[0].box == 1 

202 assert second_call_fn.pof[1].box == 2 

203 

204 assert len(second_call_fn.wfopi) == 1 

205 assert second_call_fn.wfopi[0].src == 1 and second_call_fn.wfopi[0].tgt == 1 

206 

207 assert len(second_call_fn.wff) == 1 

208 assert second_call_fn.wff[0].src == 2 and second_call_fn.wff[0].tgt == 2 

209 

210 assert len(second_call_fn.wfopo) == 1 

211 assert second_call_fn.wfopo[0].src == 1 and second_call_fn.wfopo[0].tgt == 1 

212 

213 ##################################################################  

214 second_mult_fn = fun_gromet.fn_array[4] 

215 assert len(second_mult_fn.opi) == 2 

216 assert len(second_mult_fn.opo) == 1 

217 

218 assert len(second_mult_fn.bf) == 2 

219 assert second_mult_fn.bf[0].function_type == FunctionType.EXPRESSION 

220 assert second_mult_fn.bf[0].body == 4 

221 

222 assert second_mult_fn.bf[1].function_type == FunctionType.LANGUAGE_PRIMITIVE 

223 assert second_mult_fn.bf[1].name == "ast.Mult" 

224 

225 assert len(second_mult_fn.pif) == 3 

226 assert second_mult_fn.pif[0].box == 1 

227 assert second_mult_fn.pif[1].box == 2 

228 assert second_mult_fn.pif[2].box == 2 

229 

230 assert len(second_mult_fn.pof) == 2 

231 assert second_mult_fn.pof[0].box == 1 

232 assert second_mult_fn.pof[1].box == 2 

233 

234 assert len(second_mult_fn.wfopi) == 2 

235 assert second_mult_fn.wfopi[0].src == 1 and second_mult_fn.wfopi[0].tgt == 1 

236 assert second_mult_fn.wfopi[1].src == 2 and second_mult_fn.wfopi[1].tgt == 2 

237 

238 assert len(second_mult_fn.wff) == 1 

239 assert second_mult_fn.wff[0].src == 3 and second_mult_fn.wff[0].tgt == 1 

240 

241 assert len(second_mult_fn.wfopo) == 1 

242 assert second_mult_fn.wfopo[0].src == 1 and second_mult_fn.wfopo[0].tgt == 2 

243 

244 ##################################################################  

245 third_call_fn = fun_gromet.fn_array[5] 

246 assert len(third_call_fn.opi) == 2 

247 assert len(third_call_fn.opo) == 1 

248 

249 assert len(third_call_fn.bf) == 1 

250 assert third_call_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE 

251 assert third_call_fn.bf[0].name == "_call" 

252 

253 assert len(third_call_fn.pif) == 2 

254 assert third_call_fn.pif[0].box == 1 

255 assert third_call_fn.pif[1].box == 1 

256 

257 assert len(third_call_fn.pof) == 1 

258 assert third_call_fn.pof[0].box == 1 

259 

260 assert len(third_call_fn.wfopi) == 2 

261 assert third_call_fn.wfopi[0].src == 1 and third_call_fn.wfopi[0].tgt == 1 

262 assert third_call_fn.wfopi[1].src == 2 and third_call_fn.wfopi[1].tgt == 2 

263 

264 assert len(third_call_fn.wfopo) == 1 

265 assert third_call_fn.wfopo[0].src == 1 and third_call_fn.wfopo[0].tgt == 1 

266 

267 ##################################################################  

268 fourth_call_fn = fun_gromet.fn_array[6] 

269 assert len(fourth_call_fn.opi) == 2 

270 assert len(fourth_call_fn.opo) == 1 

271 

272 assert len(fourth_call_fn.bf) == 1 

273 assert fourth_call_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE 

274 assert fourth_call_fn.bf[0].name == "_call" 

275 

276 assert len(fourth_call_fn.pif) == 2 

277 assert fourth_call_fn.pif[0].box == 1 

278 assert fourth_call_fn.pif[1].box == 1 

279 

280 assert len(fourth_call_fn.pof) == 1 

281 assert fourth_call_fn.pof[0].box == 1 

282 

283 assert len(fourth_call_fn.wfopi) == 2 

284 assert fourth_call_fn.wfopi[0].src == 1 and fourth_call_fn.wfopi[0].tgt == 1 

285 assert fourth_call_fn.wfopi[1].src == 2 and fourth_call_fn.wfopi[1].tgt == 2 

286 

287 assert len(fourth_call_fn.wfopo) == 1 

288 assert fourth_call_fn.wfopo[0].src == 1 and fourth_call_fn.wfopo[0].tgt == 1 

289 

290 ##################################################################  

291 double_call_fn = fun_gromet.fn_array[7] 

292 assert len(double_call_fn.opi) == 3 

293 assert len(double_call_fn.opo) == 1 

294 assert len(double_call_fn.bf) == 3 

295 assert double_call_fn.bf[2].function_type == FunctionType.LANGUAGE_PRIMITIVE 

296 

297 assert len(double_call_fn.pif) == 6 

298 assert double_call_fn.pif[0].box == 1 

299 assert double_call_fn.pif[1].box == 1 

300 assert double_call_fn.pif[2].box == 2 

301 assert double_call_fn.pif[3].box == 2 

302 assert double_call_fn.pif[4].box == 3 

303 assert double_call_fn.pif[5].box == 3 

304 

305 assert len(double_call_fn.pof) == 3 

306 assert double_call_fn.pof[0].box == 1 

307 assert double_call_fn.pof[1].box == 2 

308 assert double_call_fn.pof[2].box == 3 

309 

310 assert len(double_call_fn.wfopi) == 4 

311 assert double_call_fn.wfopi[0].src == 1 and double_call_fn.wfopi[0].tgt == 1 

312 assert double_call_fn.wfopi[1].src == 2 and double_call_fn.wfopi[1].tgt == 2 

313 assert double_call_fn.wfopi[2].src == 3 and double_call_fn.wfopi[2].tgt == 3 

314 assert double_call_fn.wfopi[3].src == 4 and double_call_fn.wfopi[3].tgt == 2 

315 

316 assert len(double_call_fn.wff) == 2 

317 assert double_call_fn.wff[0].src == 5 and double_call_fn.wff[0].tgt == 1 

318 assert double_call_fn.wff[1].src == 6 and double_call_fn.wff[1].tgt == 2 

319 

320 assert len(double_call_fn.wfopo) == 1 

321 assert double_call_fn.wfopo[0].src == 1 and double_call_fn.wfopo[0].tgt == 3 

322 

323