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

140 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 Var, 

7 Name, 

8 Operator, 

9 CASTLiteralValue 

10) 

11 

12def binop1(): 

13 return """ 

14x = 2 + 3 

15 """ 

16 

17def binop2(): 

18 return """ 

19x = 2 

20y = x + 3 

21 """ 

22 

23def binop3(): 

24 return """ 

25x = 1  

26y = 2 

27z = x + y - (y * x) / x 

28 """ 

29 

30def unary1(): 

31 return """ 

32x = -1  

33 """ 

34 

35def unary2(): 

36 return """ 

37x = 1 

38y = -x  

39 """ 

40 

41 

42def generate_cast(test_file_string): 

43 # use Python to CAST 

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

45 

46 return out_cast 

47 

48def test_binop1(): 

49 exp_cast = generate_cast(binop1()) 

50 # Test basic properties of assignment node 

51 binop_node = exp_cast.nodes[0].body[0] 

52 

53 assert isinstance(binop_node, Assignment) 

54 assert isinstance(binop_node.left, Var) 

55 assert isinstance(binop_node.left.val, Name) 

56 assert binop_node.left.val.name == "x" 

57 

58 assert isinstance(binop_node.right, Operator) 

59 assert binop_node.right.op == "ast.Add" 

60 

61 assert isinstance(binop_node.right.operands[0], CASTLiteralValue) 

62 assert binop_node.right.operands[0].value == '2' 

63 assert binop_node.right.operands[0].value_type == 'Integer' 

64 

65 assert isinstance(binop_node.right.operands[1], CASTLiteralValue) 

66 assert binop_node.right.operands[1].value == '3' 

67 assert binop_node.right.operands[1].value_type == 'Integer' 

68 

69def test_binop2(): 

70 exp_cast = generate_cast(binop2()) 

71 

72 # Test basic properties of two assignment nodes 

73 binop_node = exp_cast.nodes[0].body[0] 

74 assert isinstance(binop_node, Assignment) 

75 assert isinstance(binop_node.left, Var) 

76 assert isinstance(binop_node.left.val, Name) 

77 assert binop_node.left.val.name == "x" 

78 assert binop_node.left.val.id == 0 

79 

80 assert isinstance(binop_node.right, CASTLiteralValue) 

81 assert binop_node.right.value_type == "Integer" 

82 assert binop_node.right.value == '2' 

83 

84 # ------ 

85 binop_node = exp_cast.nodes[0].body[1] 

86 assert isinstance(binop_node, Assignment) 

87 assert isinstance(binop_node.left, Var) 

88 assert isinstance(binop_node.left.val, Name) 

89 assert binop_node.left.val.name == "y" 

90 assert binop_node.left.val.id == 1 

91 

92 assert isinstance(binop_node.right, Operator) 

93 assert binop_node.right.op == "ast.Add" 

94 

95 assert isinstance(binop_node.right.operands[0], Name) 

96 assert binop_node.right.operands[0].name == 'x' 

97 assert binop_node.right.operands[0].id == 0 

98 

99 assert isinstance(binop_node.right.operands[1], CASTLiteralValue) 

100 assert binop_node.right.operands[1].value == '3' 

101 assert binop_node.right.operands[1].value_type == 'Integer' 

102 

103def test_binop3(): 

104 exp_cast = generate_cast(binop3()) 

105 

106 # Test basic properties of two assignment nodes 

107 binop_node = exp_cast.nodes[0].body[0] 

108 assert isinstance(binop_node, Assignment) 

109 assert isinstance(binop_node.left, Var) 

110 assert isinstance(binop_node.left.val, Name) 

111 assert binop_node.left.val.name == "x" 

112 assert binop_node.left.val.id == 0 

113 

114 assert isinstance(binop_node.right, CASTLiteralValue) 

115 assert binop_node.right.value_type == "Integer" 

116 assert binop_node.right.value == '1' 

117 

118 # ------ 

119 binop_node = exp_cast.nodes[0].body[1] 

120 assert isinstance(binop_node, Assignment) 

121 assert isinstance(binop_node.left, Var) 

122 assert isinstance(binop_node.left.val, Name) 

123 assert binop_node.left.val.name == "y" 

124 assert binop_node.left.val.id == 1 

125 

126 assert isinstance(binop_node.right, CASTLiteralValue) 

127 assert binop_node.right.value_type == "Integer" 

128 assert binop_node.right.value == '2' 

129 

130 # ------ 

131 binop_node = exp_cast.nodes[0].body[2] 

132 assert isinstance(binop_node, Assignment) 

133 assert isinstance(binop_node.left, Var) 

134 assert isinstance(binop_node.left.val, Name) 

135 assert binop_node.left.val.name == "z" 

136 assert binop_node.left.val.id == 2 

137 

138 assert isinstance(binop_node.right, Operator) 

139 assert binop_node.right.op == "ast.Sub" 

140 

141 binop_node_1 = binop_node.right.operands[0] 

142 assert binop_node_1.op == "ast.Add" 

143 

144 assert isinstance(binop_node_1.operands[0], Name) 

145 assert binop_node_1.operands[0].name == "x" 

146 assert binop_node_1.operands[0].id == 0 

147 

148 assert isinstance(binop_node_1.operands[1], Name) 

149 assert binop_node_1.operands[1].name == "y" 

150 assert binop_node_1.operands[1].id == 1 

151 

152 binop_node_2 = binop_node.right.operands[1] 

153 assert binop_node_2.op == "ast.Div" 

154 assert isinstance(binop_node_2.operands[0], Operator) 

155 

156 assert isinstance(binop_node_2.operands[1], Name) 

157 assert binop_node_2.operands[1].name == "x" 

158 assert binop_node_2.operands[1].id == 0 

159 

160 binop_node_3 = binop_node_2.operands[0] 

161 assert binop_node_3.op == "ast.Mult" 

162 

163 assert isinstance(binop_node_3.operands[0], Name) 

164 assert binop_node_3.operands[0].name == "y" 

165 assert binop_node_3.operands[0].id == 1 

166 

167 assert isinstance(binop_node_3.operands[1], Name) 

168 assert binop_node_3.operands[1].name == "x" 

169 assert binop_node_3.operands[1].id == 0 

170 

171def test_unary1(): 

172 exp_cast = generate_cast(unary1()) 

173 # Test basic properties of assignment node 

174 unary_node = exp_cast.nodes[0].body[0] 

175 

176 assert isinstance(unary_node, Assignment) 

177 assert isinstance(unary_node.left, Var) 

178 assert isinstance(unary_node.left.val, Name) 

179 assert unary_node.left.val.name == "x" 

180 assert unary_node.left.val.id == 0 

181 

182 assert isinstance(unary_node.right, Operator) 

183 assert unary_node.right.op == "ast.USub" 

184 

185 assert isinstance(unary_node.right.operands[0], CASTLiteralValue) 

186 assert unary_node.right.operands[0].value == '1' 

187 assert unary_node.right.operands[0].value_type == 'Integer' 

188 

189def test_unary2(): 

190 exp_cast = generate_cast(unary2()) 

191 # Test basic properties of assignment node 

192 unary_node = exp_cast.nodes[0].body[0] 

193 

194 assert isinstance(unary_node, Assignment) 

195 assert isinstance(unary_node.left, Var) 

196 assert isinstance(unary_node.left.val, Name) 

197 assert unary_node.left.val.name == "x" 

198 assert unary_node.left.val.id == 0 

199 

200 assert isinstance(unary_node.right, CASTLiteralValue) 

201 assert unary_node.right.value == '1' 

202 assert unary_node.right.value_type == 'Integer' 

203 

204 unary_node = exp_cast.nodes[0].body[1] 

205 

206 assert isinstance(unary_node, Assignment) 

207 assert isinstance(unary_node.left, Var) 

208 assert isinstance(unary_node.left.val, Name) 

209 assert unary_node.left.val.name == "y" 

210 assert unary_node.left.val.id == 1 

211 

212 assert isinstance(unary_node.right, Operator) 

213 assert unary_node.right.op == "ast.USub" 

214 

215 assert isinstance(unary_node.right.operands[0], Name) 

216 assert unary_node.right.operands[0].name == 'x' 

217 assert unary_node.right.operands[0].id == 0