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

90 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 Call, 

8 Name, 

9 CASTLiteralValue, 

10 ModelIf, 

11 Loop, 

12 Operator 

13) 

14 

15def while1(): 

16 return """ 

17x = 2 

18while x < 5: 

19 x = x + 1 

20 """ 

21 

22def while2(): 

23 return """ 

24x = 2 

25y = 3 

26 

27while x < 5: 

28 x = x + 1 

29 x = x + y 

30 """ 

31 

32def generate_cast(test_file_string): 

33 # use Python to CAST 

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

35 

36 return out_cast 

37 

38def test_while1(): 

39 cast = generate_cast(while1()) 

40 

41 asg_node = cast.nodes[0].body[0] 

42 loop_node = cast.nodes[0].body[1] 

43 

44 assert isinstance(asg_node, Assignment) 

45 assert isinstance(asg_node.left, Var) 

46 assert isinstance(asg_node.left.val, Name) 

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

48 

49 assert isinstance(asg_node.right, CASTLiteralValue) 

50 assert asg_node.right.value_type == "Integer" 

51 assert asg_node.right.value == '2' 

52 

53 assert isinstance(loop_node, Loop) 

54 assert len(loop_node.pre) == 0 

55 

56 # Loop Test 

57 loop_test = loop_node.expr 

58 assert isinstance(loop_test, Operator) 

59 assert loop_test.op == "ast.Lt" 

60 assert isinstance(loop_test.operands[0], Name) 

61 assert loop_test.operands[0].name == "x" 

62 

63 assert isinstance(loop_test.operands[1], CASTLiteralValue) 

64 assert loop_test.operands[1].value_type == "Integer" 

65 assert loop_test.operands[1].value == "5" 

66 

67 # Loop Body 

68 loop_body = loop_node.body 

69 asg = loop_body[0] 

70 assert isinstance(asg, Assignment) 

71 assert isinstance(asg.left, Var) 

72 assert asg.left.val.name == "x" 

73 

74 assert isinstance(asg.right, Operator) 

75 assert asg.right.op == "ast.Add" 

76 assert isinstance(asg.right.operands[0], Name) 

77 assert isinstance(asg.right.operands[1], CASTLiteralValue) 

78 assert asg.right.operands[1].value == "1" 

79 

80def test_while2(): 

81 cast = generate_cast(while2()) 

82 

83 asg_node = cast.nodes[0].body[0] 

84 asg_node_2 = cast.nodes[0].body[1] 

85 loop_node = cast.nodes[0].body[2] 

86 

87 assert isinstance(asg_node, Assignment) 

88 assert isinstance(asg_node.left, Var) 

89 assert isinstance(asg_node.left.val, Name) 

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

91 

92 assert isinstance(asg_node.right, CASTLiteralValue) 

93 assert asg_node.right.value_type == "Integer" 

94 assert asg_node.right.value == '2' 

95 

96 assert isinstance(asg_node_2, Assignment) 

97 assert isinstance(asg_node_2.left, Var) 

98 assert isinstance(asg_node_2.left.val, Name) 

99 assert asg_node_2.left.val.name == "y" 

100 

101 assert isinstance(asg_node_2.right, CASTLiteralValue) 

102 assert asg_node_2.right.value_type == "Integer" 

103 assert asg_node_2.right.value == '3' 

104 

105 assert isinstance(loop_node, Loop) 

106 assert len(loop_node.pre) == 0 

107 

108 # Loop Test 

109 loop_test = loop_node.expr 

110 assert isinstance(loop_test, Operator) 

111 assert loop_test.op == "ast.Lt" 

112 assert isinstance(loop_test.operands[0], Name) 

113 assert loop_test.operands[0].name == "x" 

114 

115 assert isinstance(loop_test.operands[1], CASTLiteralValue) 

116 assert loop_test.operands[1].value_type == "Integer" 

117 assert loop_test.operands[1].value == "5" 

118 

119 # Loop Body 

120 loop_body = loop_node.body 

121 asg = loop_body[0] 

122 assert isinstance(asg, Assignment) 

123 assert isinstance(asg.left, Var) 

124 assert asg.left.val.name == "x" 

125 

126 assert isinstance(asg.right, Operator) 

127 assert asg.right.op == "ast.Add" 

128 assert isinstance(asg.right.operands[0], Name) 

129 assert asg.right.operands[0].name == "x" 

130 

131 assert isinstance(asg.right.operands[1], CASTLiteralValue) 

132 assert asg.right.operands[1].value == "1" 

133 

134 asg = loop_body[1] 

135 assert isinstance(asg, Assignment) 

136 assert isinstance(asg.left, Var) 

137 assert asg.left.val.name == "x" 

138 

139 assert isinstance(asg.right, Operator) 

140 assert asg.right.op == "ast.Add" 

141 assert isinstance(asg.right.operands[0], Name) 

142 assert asg.right.operands[0].name == "x" 

143 

144 assert isinstance(asg.right.operands[1], Name) 

145 assert asg.right.operands[1].name == "y" 

146