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

126 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 

5import ast 

6 

7from skema.program_analysis.CAST.pythonAST import py_ast_to_cast 

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

9from skema.program_analysis.CAST2FN import cast 

10from skema.program_analysis.CAST2FN.cast import CAST 

11from skema.program_analysis.run_ann_cast_pipeline import ann_cast_pipeline 

12 

13def exp0(): 

14 return """ 

15x = 2 

16 """ 

17 

18def exp1(): 

19 return """ 

20x = 2 + 3 

21 """ 

22 

23def exp2(): 

24 return """ 

25x = 2 

26y = x + 3 

27 """ 

28 

29def exp3(): 

30 return """ 

31x = 2 

32y = x 

33 """ 

34 

35def exp4(): 

36 return """ 

37def exp(a,b,c,d): 

38 s_n = (-a * b * c) + d 

39 """ 

40 

41def generate_gromet(test_file_string): 

42 # use ast.Parse to get Python AST 

43 contents = ast.parse(test_file_string) 

44 

45 # use Python to CAST 

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

47 convert = py_ast_to_cast.PyASTToCAST("temp") 

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

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

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

51 

52 # use AnnCastPipeline to create GroMEt 

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

54 

55 return gromet 

56 

57def test_exp0(): 

58 exp_gromet = generate_gromet(exp0()) 

59 

60 base_fn = exp_gromet.fn 

61 

62 assert len(base_fn.bf) == 1 

63 assert len(base_fn.pof) == 1 

64 

65 assert base_fn.bf[0].body == 1 

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

67 

68 ############################################## 

69 inner_fn = exp_gromet.fn_array[0] 

70 

71 # Check length of elements 

72 assert len(inner_fn.opo) == 1 

73 assert len(inner_fn.bf) == 1 

74 assert len(inner_fn.pof) == 1 

75 assert len(inner_fn.wfopo) == 1 

76 

77 # Check wiring 

78 assert inner_fn.wfopo[0].src == 1 and inner_fn.wfopo[0].tgt == 1 

79 

80def test_exp1(): 

81 exp_gromet = generate_gromet(exp1()) 

82 

83 inner_fn = exp_gromet.fn_array[0] 

84 

85 # Check length of elements 

86 assert len(inner_fn.opo) == 1 

87 assert len(inner_fn.bf) == 3 

88 assert len(inner_fn.wff) == 2 

89 assert len(inner_fn.wfopo) == 1 

90 

91 # Check wiring 

92 assert inner_fn.wff[0].src == 1 and inner_fn.wff[0].tgt == 1 

93 assert inner_fn.wff[1].src == 2 and inner_fn.wff[1].tgt == 2 

94 assert inner_fn.wfopo[0].src == 1 and inner_fn.wfopo[0].tgt == 3 

95 

96 

97def test_exp2(): 

98 exp_gromet = generate_gromet(exp2()) 

99 

100 base_fn = exp_gromet.fn 

101 

102 assert len(base_fn.bf) == 2 

103 assert len(base_fn.pof) == 2 

104 assert base_fn.wff[0].src == 1 and base_fn.wff[0].tgt == 1 

105 

106 # Check calls 

107 assert base_fn.bf[0].body == 1 

108 assert base_fn.bf[1].body == 2 

109 

110 

111 ############################################## 

112 inner_fn = exp_gromet.fn_array[0] 

113 

114 # Check length of elements 

115 assert len(inner_fn.opo) == 1 

116 assert len(inner_fn.bf) == 1 

117 assert len(inner_fn.pof) == 1 

118 assert len(inner_fn.wfopo) == 1 

119 

120 # Check wiring 

121 assert inner_fn.wfopo[0].src == 1 and inner_fn.wfopo[0].tgt == 1 

122 

123 ############################################## 

124 outer_fn = exp_gromet.fn_array[1] 

125 

126 # Check length of elements 

127 assert len(outer_fn.opi) == 1 

128 assert len(outer_fn.opo) == 1 

129 assert len(outer_fn.bf) == 2 

130 assert len(outer_fn.pif) == 2 

131 assert len(outer_fn.pof) == 2 

132 assert len(outer_fn.wfopi) == 1 

133 assert len(outer_fn.wff) == 1 

134 assert len(outer_fn.wfopo) == 1 

135 

136 # Check wiring 

137 assert outer_fn.wfopi[0].src == 1 and outer_fn.wfopi[0].tgt == 1 

138 assert outer_fn.wff[0].src == 2 and outer_fn.wff[0].tgt == 1 

139 assert outer_fn.wfopo[0].src == 1 and outer_fn.wfopo[0].tgt == 2 

140 

141 

142def test_exp3(): 

143 exp_gromet = generate_gromet(exp3()) 

144 base_fn = exp_gromet.fn 

145 

146 assert len(base_fn.bf) == 2 

147 assert len(base_fn.pif) == 1 

148 assert len(base_fn.pof) == 2 

149 assert base_fn.wff[0].src == 1 and base_fn.wff[0].tgt == 1 

150 

151 # Check calls 

152 assert base_fn.bf[0].body == 1 

153 assert base_fn.bf[1].body == 2 

154 

155 ############################################## 

156 inner_fn = exp_gromet.fn_array[0] 

157 

158 # Check length of elements 

159 assert len(inner_fn.opo) == 1 

160 assert len(inner_fn.bf) == 1 

161 assert len(inner_fn.pof) == 1 

162 assert len(inner_fn.wfopo) == 1 

163 

164 # Check wiring 

165 assert inner_fn.wfopo[0].src == 1 and inner_fn.wfopo[0].tgt == 1 

166 

167 ############################################## 

168 outer_fn = exp_gromet.fn_array[1] 

169 

170 # Check length of elements 

171 assert len(outer_fn.opi) == 1 

172 assert len(outer_fn.opo) == 1 

173 assert len(outer_fn.wopio) == 1 

174 

175 # Check wiring 

176 assert outer_fn.wopio[0].src == 1 and outer_fn.wopio[0].tgt == 1 

177 

178 

179def test_exp4(): 

180 exp_gromet = generate_gromet(exp4()) 

181 

182 # FN for outer box 

183 outer_fn = exp_gromet.fn_array[0] 

184 assert len(outer_fn.opi) == 4 

185 

186 # Check wiring to inner expression is correct 

187 assert outer_fn.wfopi[0].src == 1 and outer_fn.wfopi[0].tgt == 1 

188 assert outer_fn.wfopi[1].src == 2 and outer_fn.wfopi[1].tgt == 2 

189 assert outer_fn.wfopi[2].src == 3 and outer_fn.wfopi[2].tgt == 3 

190 assert outer_fn.wfopi[3].src == 4 and outer_fn.wfopi[3].tgt == 4 

191 

192 ############################################## 

193 inner_fn = exp_gromet.fn_array[1] 

194 

195 # Check length of elements 

196 assert len(inner_fn.opi) == 4 

197 assert len(inner_fn.opo) == 1 

198 assert len(inner_fn.bf) == 4 

199 assert len(inner_fn.wfopi) == 4 

200 assert len(inner_fn.wff) == 3 

201 assert len(inner_fn.wfopo) == 1 

202 

203 # Check wiring 

204 assert inner_fn.wfopi[0].src == 1 and inner_fn.wfopi[0].tgt == 1 

205 assert inner_fn.wfopi[1].src == 3 and inner_fn.wfopi[1].tgt == 2 

206 assert inner_fn.wfopi[2].src == 5 and inner_fn.wfopi[2].tgt == 3 

207 assert inner_fn.wfopi[3].src == 7 and inner_fn.wfopi[3].tgt == 4 

208 

209 assert inner_fn.wff[0].src == 2 and inner_fn.wff[0].tgt == 1 

210 assert inner_fn.wff[1].src == 4 and inner_fn.wff[1].tgt == 2 

211 assert inner_fn.wff[2].src == 6 and inner_fn.wff[2].tgt == 3 

212 

213 assert inner_fn.wfopo[0].src == 1 and inner_fn.wfopo[0].tgt == 4 

214 

215 

216def test_expression(): 

217 test_exp0() 

218 test_exp1() 

219 test_exp2() 

220 test_exp3() 

221 test_exp4() 

222 

223 return