Coverage for skema/program_analysis/tests/test_import_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 Attribute, 

7 Var, 

8 Call, 

9 Name, 

10 CASTLiteralValue, 

11 ModelImport, 

12 StructureType, 

13) 

14 

15def import1(): 

16 return """ 

17import sys 

18  

19sys.exit() 

20 """ 

21 

22def import2(): 

23 return """ 

24import sys as system 

25 

26system.exit() 

27 """ 

28 

29def import3(): 

30 return """ 

31from sys import * 

32  

33exit() 

34 """ 

35 

36def import4(): 

37 return """ 

38import sys 

39import numpy 

40 

41x = numpy.array([1,2,3]) 

42sys.exit() 

43 """ 

44 

45def import5(): 

46 return """ 

47import sys as system, numpy as np 

48 

49x = np.array([1,2,3]) 

50system.exit()  

51 """ 

52 

53def import6(): 

54 return """ 

55from sys import exit, copyright 

56 

57copyright() 

58exit() 

59 """ 

60 

61 

62def generate_cast(test_file_string): 

63 # use Python to CAST 

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

65 

66 return out_cast 

67 

68def test_import1(): 

69 cast = generate_cast(import1()) 

70 

71 import_node = cast.nodes[0].body[0] 

72 exit_node = cast.nodes[0].body[1] 

73 

74 assert isinstance(import_node, ModelImport) 

75 assert import_node.name == "sys" 

76 assert import_node.alias == None 

77 assert import_node.symbol == None 

78 assert import_node.all == False 

79 

80 assert isinstance(exit_node, Call) 

81 assert len(exit_node.arguments) == 0 

82 

83 func_call = exit_node.func 

84 assert isinstance(func_call, Attribute) 

85 assert isinstance(func_call.attr, Name) 

86 assert func_call.attr.name == "exit" 

87 assert func_call.attr.id == 1 

88 

89 assert isinstance(func_call.value, Name) 

90 assert func_call.value.name == "sys" 

91 assert func_call.value.id == 0 

92 

93def test_import2(): 

94 cast = generate_cast(import2()) 

95 

96 import_node = cast.nodes[0].body[0] 

97 exit_node = cast.nodes[0].body[1] 

98 

99 assert isinstance(import_node, ModelImport) 

100 assert import_node.name == "sys" 

101 assert import_node.alias == "system" 

102 assert import_node.symbol == None 

103 assert import_node.all == False 

104 

105 assert isinstance(exit_node, Call) 

106 assert len(exit_node.arguments) == 0 

107 

108 func_call = exit_node.func 

109 assert isinstance(func_call, Attribute) 

110 assert isinstance(func_call.attr, Name) 

111 assert func_call.attr.name == "exit" 

112 assert func_call.attr.id == 2 # the ID being 2 and not 1 implies that 'system' is ID 1 

113 

114 assert isinstance(func_call.value, Name) 

115 assert func_call.value.name == "sys" 

116 assert func_call.value.id == 0 

117 

118def test_import3(): 

119 cast = generate_cast(import3()) 

120 

121 import_node = cast.nodes[0].body[0] 

122 exit_node = cast.nodes[0].body[1] 

123 

124 assert isinstance(import_node, ModelImport) 

125 assert import_node.name == "sys" 

126 assert import_node.alias == None 

127 assert import_node.symbol == None 

128 assert import_node.all == True 

129 

130 assert isinstance(exit_node, Call) 

131 assert len(exit_node.arguments) == 0 

132 

133 func_call = exit_node.func 

134 assert isinstance(func_call, Name) 

135 assert func_call.name == "exit" 

136 assert func_call.id == 0 

137 

138def test_import4(): 

139 cast = generate_cast(import4()) 

140 

141 import_node = cast.nodes[0].body[0] 

142 assert isinstance(import_node, ModelImport) 

143 assert import_node.name == "sys" 

144 assert import_node.alias == None 

145 assert import_node.symbol == None 

146 assert import_node.all == False 

147 

148 import_node = cast.nodes[0].body[1] 

149 assert isinstance(import_node, ModelImport) 

150 assert import_node.name == "numpy" 

151 assert import_node.alias == None 

152 assert import_node.symbol == None 

153 assert import_node.all == False 

154 

155 assign_node = cast.nodes[0].body[2] 

156 assert isinstance(assign_node, Assignment) 

157 var = assign_node.left 

158 assert isinstance(var, Var) 

159 assert isinstance(var.val, Name) 

160 assert var.val.name == "x" 

161 assert var.val.id == 3 

162 

163 call = assign_node.right 

164 assert isinstance(call, Call) 

165 args = call.arguments 

166 assert len(args) == 1 

167 arg1 = args[0] 

168 assert isinstance(arg1, CASTLiteralValue) 

169 assert arg1.value_type == StructureType.LIST 

170 

171 attr = call.func 

172 assert isinstance(attr, Attribute) 

173 assert isinstance(attr.attr, Name) 

174 assert attr.attr.name == "array" 

175 assert attr.attr.id == 2 

176 

177 assert isinstance(attr.value, Name) 

178 assert attr.value.name == "numpy" 

179 assert attr.value.id == 1 

180 

181 exit_node = cast.nodes[0].body[3] 

182 assert isinstance(exit_node, Call) 

183 assert len(exit_node.arguments) == 0 

184 

185 func_call = exit_node.func 

186 assert isinstance(func_call, Attribute) 

187 assert isinstance(func_call.attr, Name) 

188 assert func_call.attr.name == "exit" 

189 assert func_call.attr.id == 4 

190 

191 assert isinstance(func_call.value, Name) 

192 assert func_call.value.name == "sys" 

193 assert func_call.value.id == 0 

194 

195def test_import5(): 

196 cast = generate_cast(import5()) 

197 

198 import_node = cast.nodes[0].body[0] 

199 assert isinstance(import_node, ModelImport) 

200 assert import_node.name == "sys" 

201 assert import_node.alias == "system" 

202 assert import_node.symbol == None 

203 assert import_node.all == False 

204 

205 import_node = cast.nodes[0].body[1] 

206 assert isinstance(import_node, ModelImport) 

207 assert import_node.name == "numpy" 

208 assert import_node.alias == "np" 

209 assert import_node.symbol == None 

210 assert import_node.all == False 

211 

212 assign_node = cast.nodes[0].body[2] 

213 assert isinstance(assign_node, Assignment) 

214 var = assign_node.left 

215 assert isinstance(var, Var) 

216 assert isinstance(var.val, Name) 

217 assert var.val.name == "x" 

218 assert var.val.id == 5 

219 

220 call = assign_node.right 

221 assert isinstance(call, Call) 

222 args = call.arguments 

223 assert len(args) == 1 

224 arg1 = args[0] 

225 assert isinstance(arg1, CASTLiteralValue) 

226 assert arg1.value_type == StructureType.LIST 

227 

228 attr = call.func 

229 assert isinstance(attr, Attribute) 

230 assert isinstance(attr.attr, Name) 

231 assert attr.attr.name == "array" 

232 assert attr.attr.id == 4 

233 

234 assert isinstance(attr.value, Name) 

235 assert attr.value.name == "numpy" 

236 assert attr.value.id == 2 

237 

238 exit_node = cast.nodes[0].body[3] 

239 assert isinstance(exit_node, Call) 

240 assert len(exit_node.arguments) == 0 

241 

242 func_call = exit_node.func 

243 assert isinstance(func_call, Attribute) 

244 assert isinstance(func_call.attr, Name) 

245 assert func_call.attr.name == "exit" 

246 assert func_call.attr.id == 6 

247 

248 assert isinstance(func_call.value, Name) 

249 assert func_call.value.name == "sys" 

250 assert func_call.value.id == 0 

251 

252def test_import6(): 

253 cast = generate_cast(import6()) 

254 

255 import1_node = cast.nodes[0].body[0] 

256 import2_node = cast.nodes[0].body[1] 

257 cr_node = cast.nodes[0].body[2] 

258 exit_node = cast.nodes[0].body[3] 

259 

260 assert isinstance(import1_node, ModelImport) 

261 assert import1_node.name == "sys" 

262 assert import1_node.alias == None 

263 assert import1_node.symbol == "exit" 

264 assert import1_node.all == False 

265 

266 assert isinstance(import2_node, ModelImport) 

267 assert import2_node.name == "sys" 

268 assert import2_node.alias == None 

269 assert import2_node.symbol == "copyright" 

270 assert import2_node.all == False 

271 

272 assert isinstance(cr_node, Call) 

273 assert len(cr_node.arguments) == 0 

274 # assert isinstance(cr_node.func, Var) 

275 assert isinstance(cr_node.func, Name) 

276 assert cr_node.func.name == "copyright" 

277 assert cr_node.func.id == 1 

278 

279 assert isinstance(exit_node, Call) 

280 assert len(exit_node.arguments) == 0 

281 # assert isinstance(exit_node.func, Var) 

282 assert isinstance(exit_node.func, Name) 

283 assert exit_node.func.name == "exit" 

284 assert exit_node.func.id == 0