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

86 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 ( 

5 GrometFNModuleCollection, 

6 FunctionType, 

7 TypedValue, 

8) 

9import ast 

10 

11from skema.program_analysis.CAST.pythonAST import py_ast_to_cast 

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

13from skema.program_analysis.CAST2FN import cast 

14from skema.program_analysis.CAST2FN.cast import CAST 

15from skema.program_analysis.run_ann_cast_pipeline import ann_cast_pipeline 

16 

17 

18def return1(): 

19 return """ 

20def return_true(): 

21 return True 

22 """ 

23 

24def return2(): 

25 return """ 

26def return_true(): 

27 return True 

28 

29while (return_true()): 

30 print("Test") 

31 """ 

32 

33 

34def generate_gromet(test_file_string): 

35 # use ast.Parse to get Python AST 

36 contents = ast.parse(test_file_string) 

37 

38 # use Python to CAST 

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

40 convert = py_ast_to_cast.PyASTToCAST("temp") 

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

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

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

44 

45 # use AnnCastPipeline to create GroMEt 

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

47 

48 return gromet 

49 

50def test_return1(): 

51 gromet = generate_gromet(return1()) 

52 

53 base_fn = gromet.fn 

54 

55 assert len(base_fn.b) == 1 

56 

57 func_fn = gromet.fn_array[0] 

58 assert len(func_fn.b) == 1 

59 

60 assert len(func_fn.opo) == 1 

61 assert func_fn.opo[0].box == 1 

62 

63 assert len(func_fn.bf) == 1 

64 assert func_fn.bf[0].function_type == FunctionType.LITERAL 

65 assert func_fn.bf[0].value.value_type == "Boolean" 

66 assert func_fn.bf[0].value.value == "True" 

67 

68 assert len(func_fn.pof) == 1 

69 assert func_fn.pof[0].box == 1 

70 

71 assert len(func_fn.wfopo) == 1 

72 assert func_fn.wfopo[0].src == 1 and func_fn.wfopo[0].tgt == 1 

73 

74 

75def test_return2(): 

76 exp_gromet = generate_gromet(return2()) 

77 

78 base_fn = exp_gromet.fn 

79 assert len(base_fn.bl) == 1 

80 assert base_fn.bl[0].condition == 2 

81 assert base_fn.bl[0].body == 3 

82 

83 func_fn = exp_gromet.fn_array[0] 

84 assert len(func_fn.b) == 1 

85 

86 assert len(func_fn.opo) == 1 

87 assert func_fn.opo[0].box == 1 

88 

89 assert len(func_fn.bf) == 1 

90 assert func_fn.bf[0].function_type == FunctionType.LITERAL 

91 assert func_fn.bf[0].value.value_type == "Boolean" 

92 assert func_fn.bf[0].value.value == "True" 

93 

94 assert len(func_fn.pof) == 1 

95 assert func_fn.pof[0].box == 1 

96 

97 assert len(func_fn.wfopo) == 1 

98 assert func_fn.wfopo[0].src == 1 and func_fn.wfopo[0].tgt == 1 

99 

100 predicate_fn = exp_gromet.fn_array[1] 

101 assert len(predicate_fn.b) == 1 

102 assert len(predicate_fn.opo) == 1 

103 assert predicate_fn.opo[0].box == 1 

104 

105 assert len(predicate_fn.bf) == 1 

106 assert predicate_fn.bf[0].body == 1 

107 

108 assert len(predicate_fn.pof) == 1 

109 assert predicate_fn.pof[0].box == 1 

110 

111 assert len(predicate_fn.wfopo) == 1 

112 assert predicate_fn.wfopo[0].src == 1 

113 assert predicate_fn.wfopo[0].tgt == 1 

114 

115 loop_fn = exp_gromet.fn_array[2] 

116 assert len(loop_fn.bf) == 1 

117 assert loop_fn.bf[0].body == 4 

118 

119 loop_body_fn = exp_gromet.fn_array[3] 

120 assert len(loop_body_fn.opo) == 1 

121 assert loop_body_fn.opo[0].box == 1 

122 

123 assert len(loop_body_fn.bf) == 2 

124 assert loop_body_fn.bf[1].function_type == FunctionType.LITERAL 

125 assert loop_body_fn.bf[1].value.value_type == "List" 

126 

127 assert len(loop_body_fn.pif) == 1 

128 assert loop_body_fn.pif[0].box == 1 

129 

130 assert len(loop_body_fn.pof) == 2 

131 assert loop_body_fn.pof[0].box == 1 

132 assert loop_body_fn.pof[1].box == 2 

133 

134 assert len(loop_body_fn.wff) == 1 

135 assert loop_body_fn.wff[0].src == 1 

136 assert loop_body_fn.wff[0].tgt == 2 

137 

138 assert len(loop_body_fn.wfopo) == 1 

139 assert loop_body_fn.wfopo[0].src == 1 

140 assert loop_body_fn.wfopo[0].tgt == 1 

141