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

341 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 Attribute, 

7 Var, 

8 Name, 

9 FunctionDef, 

10 Call, 

11 CASTLiteralValue, 

12 RecordDef, 

13 Operator, 

14 ModelReturn 

15) 

16 

17def class1(): 

18 return """ 

19class MyClass: 

20 def __init__(self, a: int, b: int): 

21 self.a = a 

22 self.b = b 

23 self.c = a + b 

24 

25 def get_c(self): 

26 return self.c 

27 

28mc = MyClass(2, 3) 

29x = mc.get_c() 

30 """ 

31 

32def class2(): 

33 return """ 

34class MyClass1: 

35 def __init__(self, a: int): 

36 self.a = a 

37 

38 def get_a(self): 

39 return self.a 

40 

41class MyClass2(MyClass1): 

42 def __init__(self, b: int): 

43 self.b = b 

44 super().__init__(b + 1) 

45 

46 def get_b(self): 

47 return self.b 

48 

49 

50mc = MyClass2(2) 

51x = mc.get_a() 

52y = mc.get_b()  

53 

54 """ 

55 

56def class3(): 

57 return """ 

58class Foo: 

59 def __init__(self, a): 

60 self.a = a 

61 

62 def add1(self): 

63 self.a = self.a + 1 

64 

65 def sub1(self): 

66 self.a = self.a - 1 

67 

68foo = Foo() 

69 

70foo.add1().add1().sub1() 

71 """ 

72 

73def generate_cast(test_file_string): 

74 # use Python to CAST 

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

76 

77 return out_cast 

78 

79def test_class1(): 

80 fun_cast = generate_cast(class1()) 

81 record_def_node = fun_cast.nodes[0].body[0] 

82 assert isinstance(record_def_node, RecordDef) 

83 assert record_def_node.name == "MyClass" 

84 assert len(record_def_node.bases) == 0 

85 assert len(record_def_node.fields) == 3 

86 fields = record_def_node.fields 

87 assert isinstance(fields[0], Name) 

88 assert fields[0].name == "a" 

89 assert fields[0].id == 3 

90 

91 assert isinstance(fields[1], Name) 

92 assert fields[1].name == "b" 

93 assert fields[1].id == 4 

94 

95 assert isinstance(fields[2], Name) 

96 assert fields[2].name == "c" 

97 assert fields[2].id == 5 

98 

99 assert len(record_def_node.funcs) == 2 

100 init_func = record_def_node.funcs[0] 

101 assert isinstance(init_func, FunctionDef) 

102 assert isinstance(init_func.name, Name) 

103 assert init_func.name.name == "__init__" 

104 assert init_func.name.id == 1 

105 

106 func_args = init_func.func_args 

107 assert len(func_args) == 3 

108 assert isinstance(func_args[0], Var) 

109 assert func_args[0].val.name == "self" 

110 assert func_args[0].val.id == 2 

111 

112 assert isinstance(func_args[1], Var) 

113 assert func_args[1].val.name == "a" 

114 assert func_args[1].val.id == 3 

115 

116 assert isinstance(func_args[2], Var) 

117 assert func_args[2].val.name == "b" 

118 assert func_args[2].val.id == 4 

119 

120 func_body = init_func.body 

121 assert len(func_body) == 3 

122 assert isinstance(func_body[0], Assignment) 

123 assert isinstance(func_body[0].left, Attribute) 

124 assert func_body[0].left.value.name == "self" 

125 assert func_body[0].left.attr.name == "a" 

126 

127 assert isinstance(func_body[0].right, Name) 

128 assert func_body[0].right.name == "a" 

129 

130 assert isinstance(func_body[1], Assignment) 

131 assert isinstance(func_body[1].left, Attribute) 

132 assert func_body[1].left.value.name == "self" 

133 assert func_body[1].left.attr.name == "b" 

134 

135 assert isinstance(func_body[1].right, Name) 

136 assert func_body[1].right.name == "b" 

137 

138 assert isinstance(func_body[2], Assignment) 

139 assert isinstance(func_body[2].left, Attribute) 

140 assert func_body[2].left.value.name == "self" 

141 assert func_body[2].left.attr.name == "c" 

142 

143 assert isinstance(func_body[2].right, Operator) 

144 assert len(func_body[2].right.operands) == 2 

145 assert func_body[2].right.op == "ast.Add" 

146 assert func_body[2].right.operands[0].name == "a" 

147 assert func_body[2].right.operands[1].name == "b" 

148 

149 get_func = record_def_node.funcs[1] 

150 assert isinstance(get_func, FunctionDef) 

151 assert get_func.name.name == "get_c" 

152 func_args = get_func.func_args 

153 

154 assert len(func_args) == 1 

155 assert func_args[0].val.name == "self" 

156 

157 func_body = get_func.body 

158 assert len(func_body) == 1 

159 assert isinstance(func_body[0], ModelReturn) 

160 assert isinstance(func_body[0].value, Attribute) 

161 assert func_body[0].value.value.name == "self" 

162 assert func_body[0].value.attr.name == "c" 

163 

164 ####################################################### 

165 asg_node = fun_cast.nodes[0].body[1] 

166 assert isinstance(asg_node, Assignment) 

167 assert isinstance(asg_node.left, Var) 

168 assert asg_node.left.val.name == "mc" 

169 assert asg_node.left.val.id == 9 

170 

171 func_call_node = asg_node.right 

172 assert isinstance(func_call_node, Call) 

173 assert func_call_node.func.name == "MyClass" 

174 

175 assert len(func_call_node.arguments) == 2 

176 assert isinstance(func_call_node.arguments[0], CASTLiteralValue) 

177 assert func_call_node.arguments[0].value_type == "Integer" 

178 assert func_call_node.arguments[0].value == "2" 

179 

180 assert isinstance(func_call_node.arguments[1], CASTLiteralValue) 

181 assert func_call_node.arguments[1].value_type == "Integer" 

182 assert func_call_node.arguments[1].value == "3" 

183 

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

185 asg_node = fun_cast.nodes[0].body[2] 

186 assert isinstance(asg_node, Assignment) 

187 assert isinstance(asg_node.left, Var) 

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

189 assert asg_node.left.val.id == 10 

190 

191 assert isinstance(asg_node.right, Call) 

192 assert isinstance(asg_node.right.func, Attribute) 

193 assert asg_node.right.func.value.name == "mc" 

194 assert asg_node.right.func.attr.name == "get_c" 

195 

196def test_class2(): 

197 fun_cast = generate_cast(class2()) 

198 record_def_node = fun_cast.nodes[0].body[0] 

199 assert isinstance(record_def_node, RecordDef) 

200 assert record_def_node.name == "MyClass1" 

201 assert len(record_def_node.bases) == 0 

202 assert len(record_def_node.fields) == 1 

203 fields = record_def_node.fields 

204 assert isinstance(fields[0], Name) 

205 assert fields[0].name == "a" 

206 assert fields[0].id == 3 

207 

208 assert len(record_def_node.funcs) == 2 

209 init_func = record_def_node.funcs[0] 

210 assert isinstance(init_func, FunctionDef) 

211 assert isinstance(init_func.name, Name) 

212 assert init_func.name.name == "__init__" 

213 assert init_func.name.id == 1 

214 

215 func_args = init_func.func_args 

216 assert len(func_args) == 2 

217 assert isinstance(func_args[0], Var) 

218 assert func_args[0].val.name == "self" 

219 assert func_args[0].val.id == 2 

220 

221 assert isinstance(func_args[1], Var) 

222 assert func_args[1].val.name == "a" 

223 assert func_args[1].val.id == 3 

224 

225 func_body = init_func.body 

226 assert len(func_body) == 1 

227 assert isinstance(func_body[0], Assignment) 

228 assert isinstance(func_body[0].left, Attribute) 

229 assert func_body[0].left.value.name == "self" 

230 assert func_body[0].left.attr.name == "a" 

231 

232 assert isinstance(func_body[0].right, Name) 

233 assert func_body[0].right.name == "a" 

234 

235 get_func = record_def_node.funcs[1] 

236 assert isinstance(get_func, FunctionDef) 

237 assert get_func.name.name == "get_a" 

238 func_args = get_func.func_args 

239 

240 assert len(func_args) == 1 

241 assert func_args[0].val.name == "self" 

242 

243 func_body = get_func.body 

244 assert len(func_body) == 1 

245 assert isinstance(func_body[0], ModelReturn) 

246 assert isinstance(func_body[0].value, Attribute) 

247 assert func_body[0].value.value.name == "self" 

248 assert func_body[0].value.attr.name == "a" 

249 

250 ####################################################### 

251 record_def_node = fun_cast.nodes[0].body[1] 

252 assert isinstance(record_def_node, RecordDef) 

253 assert record_def_node.name == "MyClass2" 

254 assert len(record_def_node.bases) == 0 

255 assert len(record_def_node.fields) == 1 

256 fields = record_def_node.fields 

257 assert isinstance(fields[0], Name) 

258 assert fields[0].name == "b" 

259 assert fields[0].id == 9 

260 

261 assert len(record_def_node.funcs) == 2 

262 init_func = record_def_node.funcs[0] 

263 assert isinstance(init_func, FunctionDef) 

264 assert isinstance(init_func.name, Name) 

265 assert init_func.name.name == "__init__" 

266 assert init_func.name.id == 1 

267 

268 func_args = init_func.func_args 

269 assert len(func_args) == 2 

270 assert isinstance(func_args[0], Var) 

271 assert func_args[0].val.name == "self" 

272 assert func_args[0].val.id == 8 

273 

274 assert isinstance(func_args[1], Var) 

275 assert func_args[1].val.name == "b" 

276 assert func_args[1].val.id == 9 

277 

278 func_body = init_func.body 

279 assert len(func_body) == 2 

280 assert isinstance(func_body[0], Assignment) 

281 assert isinstance(func_body[0].left, Attribute) 

282 assert func_body[0].left.value.name == "self" 

283 assert func_body[0].left.attr.name == "b" 

284 

285 assert isinstance(func_body[0].right, Name) 

286 assert func_body[0].right.name == "b" 

287 

288 assert isinstance(func_body[1], Call) 

289 assert isinstance(func_body[1].func, Attribute) 

290 assert isinstance(func_body[1].func.value, Call) 

291 assert func_body[1].func.value.func.name == "super" 

292 assert func_body[1].func.value.func.id == 10 

293 assert func_body[1].func.attr.name == "__init__" 

294 

295 func_args = func_body[1].arguments 

296 assert isinstance(func_args[0], Operator) 

297 assert func_args[0].op == "ast.Add" 

298 

299 assert len(func_args[0].operands) == 2 

300 assert func_args[0].operands[0].name == "b" 

301 

302 assert isinstance(func_args[0].operands[1], CASTLiteralValue) 

303 assert func_args[0].operands[1].value == "1" 

304 

305 get_func = record_def_node.funcs[1] 

306 assert isinstance(get_func, FunctionDef) 

307 assert get_func.name.name == "get_b" 

308 func_args = get_func.func_args 

309 

310 assert len(func_args) == 1 

311 assert func_args[0].val.name == "self" 

312 

313 func_body = get_func.body 

314 assert len(func_body) == 1 

315 assert isinstance(func_body[0], ModelReturn) 

316 assert isinstance(func_body[0].value, Attribute) 

317 assert func_body[0].value.value.name == "self" 

318 assert func_body[0].value.attr.name == "b" 

319 

320 # ####################################################### 

321 asg_node = fun_cast.nodes[0].body[2] 

322 assert isinstance(asg_node, Assignment) 

323 assert isinstance(asg_node.left, Var) 

324 assert asg_node.left.val.name == "mc" 

325 assert asg_node.left.val.id == 14 

326 

327 assert isinstance(asg_node.right, Call) 

328 init_func = asg_node.right 

329 assert init_func.func.name == "MyClass2" 

330 assert len(init_func.arguments) == 1 

331 assert isinstance(init_func.arguments[0], CASTLiteralValue) 

332 

333 asg_node = fun_cast.nodes[0].body[3] 

334 assert isinstance(asg_node, Assignment) 

335 assert isinstance(asg_node.left, Var) 

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

337 

338 assert isinstance(asg_node.right, Call) 

339 assert isinstance(asg_node.right.func, Attribute) 

340 assert asg_node.right.func.attr.name == "get_a" 

341 assert asg_node.right.func.attr.id == 4 

342 assert asg_node.right.func.value.name == "mc" 

343 assert asg_node.right.func.value.id == 14 

344 

345 

346 asg_node = fun_cast.nodes[0].body[4] 

347 assert isinstance(asg_node, Assignment) 

348 assert isinstance(asg_node.left, Var) 

349 assert asg_node.left.val.name == "y" 

350 assert asg_node.left.val.id == 16 

351 

352 assert isinstance(asg_node.right, Call) 

353 assert isinstance(asg_node.right.func, Attribute) 

354 assert asg_node.right.func.attr.name == "get_b" 

355 assert asg_node.right.func.attr.id == 11 

356 assert asg_node.right.func.value.name == "mc" 

357 assert asg_node.right.func.value.id == 14 

358 

359def test_class3(): 

360 class_cast = generate_cast(class3()) 

361 record_def_node = class_cast.nodes[0].body[0] 

362 assert isinstance(record_def_node, RecordDef) 

363 assert record_def_node.name == "Foo" 

364 assert len(record_def_node.bases) == 0 

365 assert len(record_def_node.fields) == 1 

366 fields = record_def_node.fields 

367 assert isinstance(fields[0], Name) 

368 assert fields[0].name == "a" 

369 assert fields[0].id == 3 

370 

371 assert len(record_def_node.funcs) == 3 

372 init_func = record_def_node.funcs[0] 

373 assert isinstance(init_func, FunctionDef) 

374 assert isinstance(init_func.name, Name) 

375 assert init_func.name.name == "__init__" 

376 assert init_func.name.id == 1 

377 

378 func_args = init_func.func_args 

379 assert len(func_args) == 2 

380 assert isinstance(func_args[0], Var) 

381 assert func_args[0].val.name == "self" 

382 assert func_args[0].val.id == 2 

383 

384 assert isinstance(func_args[1], Var) 

385 assert func_args[1].val.name == "a" 

386 assert func_args[1].val.id == 3 

387 

388 func_body = init_func.body 

389 assert len(func_body) == 1 

390 assert isinstance(func_body[0], Assignment) 

391 assert isinstance(func_body[0].left, Attribute) 

392 assert func_body[0].left.value.name == "self" 

393 assert func_body[0].left.attr.name == "a" 

394 

395 assert isinstance(func_body[0].right, Name) 

396 assert func_body[0].right.name == "a" 

397 

398 add_func = record_def_node.funcs[1] 

399 assert isinstance(add_func, FunctionDef) 

400 assert add_func.name.name == "add1" 

401 assert add_func.name.id == 4 

402 func_args = add_func.func_args 

403 

404 assert len(func_args) == 1 

405 assert func_args[0].val.name == "self" 

406 

407 func_body = add_func.body 

408 asg_stmt = func_body[0] 

409 assert isinstance(asg_stmt, Assignment) 

410 asg_left = asg_stmt.left 

411 assert isinstance(asg_left, Attribute) 

412 assert isinstance(asg_left.attr, Name) 

413 assert asg_left.attr.name == "a" 

414 assert isinstance(asg_left.value, Name) 

415 assert asg_left.value.name == "self" 

416 

417 asg_right = asg_stmt.right 

418 assert isinstance(asg_right, Operator) 

419 assert asg_right.op == "ast.Add" 

420 assert isinstance(asg_right.operands[0], Attribute) 

421 assert isinstance(asg_right.operands[0].attr, Name) 

422 assert asg_right.operands[0].attr.name == "a" 

423 assert isinstance(asg_right.operands[0].value, Name) 

424 assert asg_right.operands[0].value.name == "self" 

425 

426 assert isinstance(asg_right.operands[1], CASTLiteralValue) 

427 

428 sub_func = record_def_node.funcs[2] 

429 assert isinstance(sub_func, FunctionDef) 

430 assert sub_func.name.name == "sub1" 

431 assert sub_func.name.id == 7 

432 func_args = sub_func.func_args 

433 

434 assert len(func_args) == 1 

435 assert func_args[0].val.name == "self" 

436 

437 func_body = sub_func.body 

438 asg_stmt = func_body[0] 

439 assert isinstance(asg_stmt, Assignment) 

440 asg_left = asg_stmt.left 

441 assert isinstance(asg_left, Attribute) 

442 assert isinstance(asg_left.attr, Name) 

443 assert asg_left.attr.name == "a" 

444 assert isinstance(asg_left.value, Name) 

445 assert asg_left.value.name == "self" 

446 

447 asg_right = asg_stmt.right 

448 assert isinstance(asg_right, Operator) 

449 assert asg_right.op == "ast.Sub" 

450 assert isinstance(asg_right.operands[0], Attribute) 

451 assert isinstance(asg_right.operands[0].attr, Name) 

452 assert asg_right.operands[0].attr.name == "a" 

453 assert isinstance(asg_right.operands[0].value, Name) 

454 assert asg_right.operands[0].value.name == "self" 

455 

456 assert isinstance(asg_right.operands[1], CASTLiteralValue) 

457 

458 assignment_node = class_cast.nodes[0].body[1] 

459 assert isinstance(assignment_node, Assignment) 

460 assert isinstance(assignment_node.left, Var) 

461 assert isinstance(assignment_node.left.val, Name) 

462 assert assignment_node.left.val.name == "foo" 

463 assert assignment_node.left.val.id == 10 

464 

465 assert isinstance(assignment_node.right, Call) 

466 assert assignment_node.right.func.name == "Foo" 

467 

468 call_node = class_cast.nodes[0].body[2] 

469 assert isinstance(call_node, Call) 

470 assert isinstance(call_node.func, Attribute) 

471 assert isinstance(call_node.func.value, Call) 

472 assert call_node.func.attr.name == "sub1" 

473 assert call_node.func.attr.id == 7 

474 assert isinstance(call_node.func.value.func, Attribute) 

475 assert isinstance(call_node.func.value.func.value, Call) 

476 assert call_node.func.value.func.attr.name == "add1" 

477 assert call_node.func.value.func.attr.id == 4 

478 assert isinstance(call_node.func.value.func.value.func, Attribute) 

479 assert call_node.func.value.func.value.func.value.name == "foo" 

480 assert call_node.func.value.func.value.func.attr.name == "add1" 

481 assert call_node.func.value.func.value.func.attr.id == 4