Coverage for tests/test_trace_entry_exit.py: 100%

97 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-23 18:07 +0000

1# pylint: disable=W0621,C0116,C0114 

2import pytest 

3 

4from castep_linter import tests 

5from castep_linter.fortran.parser import get_fortran_parser 

6from castep_linter.scan_files import run_tests_on_code 

7 

8 

9@pytest.fixture 

10def test_list(): 

11 return {"subroutine": [tests.test_trace_entry_exit], "function": [tests.test_trace_entry_exit]} 

12 

13 

14@pytest.fixture 

15def parser(): 

16 return get_fortran_parser() 

17 

18 

19def subroutine_wrapper(code): 

20 return ( 

21 b"""module foo 

22 subroutine x(y) 

23 """ 

24 + code 

25 + b""" 

26 end subroutine x 

27 end module foo""" 

28 ) 

29 

30 

31def test_trace_entry_exit_correct(parser, test_list): 

32 code = b""" 

33 call trace_entry("x", stat) 

34 call trace_exit("x", stat) 

35 """ 

36 wrapped_code = subroutine_wrapper(code) 

37 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

38 assert len(error_log.errors) == 0, error_log.errors 

39 

40 

41def test_trace_entry_exit_correct_extra(parser, test_list): 

42 code = b""" 

43 call trace_entry("x", stat) 

44 call bleh() 

45 call trace_exit("x", stat) 

46 """ 

47 wrapped_code = subroutine_wrapper(code) 

48 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

49 assert len(error_log.errors) == 0 

50 

51 

52def test_trace_entry_exit_correct_keyword(parser, test_list): 

53 code = b""" 

54 call trace_entry(string="x", status=stat) 

55 call trace_exit(string="x", status=stat) 

56 """ 

57 wrapped_code = subroutine_wrapper(code) 

58 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

59 assert len(error_log.errors) == 0 

60 

61 

62def test_trace_entry_exit_correct_by_param(parser, test_list): 

63 code = b""" 

64 character(len=100), parameter :: sub_name = "x" 

65 call trace_entry(sub_name, stat) 

66 call trace_exit(sub_name, stat) 

67 """ 

68 wrapped_code = subroutine_wrapper(code) 

69 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

70 assert len(error_log.errors) == 0 

71 

72 

73def test_trace_entry_exit_correct_by_param_extra(parser, test_list): 

74 code = b""" 

75 character(len=100), parameter :: sub_name = "x" 

76 character(len=100), parameter :: bleh = othervar 

77 integer :: p 

78 type(myvar) :: z 

79 call trace_entry(sub_name, stat) 

80 call trace_exit(sub_name, stat) 

81 """ 

82 wrapped_code = subroutine_wrapper(code) 

83 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

84 assert len(error_log.errors) == 0 

85 

86 

87def test_trace_entry_missing(parser, test_list): 

88 code = b""" 

89 call trace_exit("x", stat) 

90 """ 

91 wrapped_code = subroutine_wrapper(code) 

92 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

93 assert len(error_log.errors) == 1 

94 

95 

96def test_trace_exit_missing(parser, test_list): 

97 code = b""" 

98 call trace_entry("x", stat) 

99 """ 

100 wrapped_code = subroutine_wrapper(code) 

101 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

102 assert len(error_log.errors) == 1 

103 

104 

105def test_trace_entry_exit_missing(parser, test_list): 

106 code = b"" 

107 wrapped_code = subroutine_wrapper(code) 

108 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

109 assert len(error_log.errors) == 2 

110 

111 

112def test_trace_entry_exit_wrong_name(parser, test_list): 

113 code = b""" 

114 call trace_entry("y", stat) 

115 call trace_exit("y", stat) 

116 """ 

117 wrapped_code = subroutine_wrapper(code) 

118 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

119 assert len(error_log.errors) == 2 

120 

121 

122def test_trace_entry_exit_wrong_name_keyword(parser, test_list): 

123 code = b""" 

124 call trace_entry(string="y", status=stat) 

125 call trace_exit(string="y", status=stat) 

126 """ 

127 wrapped_code = subroutine_wrapper(code) 

128 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

129 assert len(error_log.errors) == 2 

130 

131 

132def test_trace_entry_exit_wrong_name_by_param(parser, test_list): 

133 code = b""" 

134 character(len=100), parameter :: sub_name = "y" 

135 call trace_entry(sub_name, stat) 

136 call trace_exit(sub_name, stat) 

137 """ 

138 wrapped_code = subroutine_wrapper(code) 

139 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

140 assert len(error_log.errors) == 2 

141 

142 

143def test_trace_entry_exit_no_name(parser, test_list): 

144 code = b""" 

145 call trace_entry() 

146 call trace_exit() 

147 """ 

148 wrapped_code = subroutine_wrapper(code) 

149 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

150 assert len(error_log.errors) == 2 

151 

152 

153def test_trace_entry_exit_unknown_name(parser, test_list): 

154 code = b""" 

155 call trace_entry(other_var) 

156 call trace_exit(other_var) 

157 """ 

158 wrapped_code = subroutine_wrapper(code) 

159 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

160 assert len(error_log.errors) == 2 

161 

162 

163def test_trace_entry_exit_correct_caps(parser, test_list): 

164 wrapped_code = b""" 

165 module foo 

166 SUBROUTINE X(Y) 

167 CALL TRACE_ENTRY("X", STAT) 

168 CALL TRACE_EXIT("X", STAT) 

169 END SUBROUTINE X 

170 end module foo 

171 """ 

172 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

173 assert len(error_log.errors) == 0, error_log.errors[0].message 

174 

175 

176def test_trace_entry_exit_correct_by_param_mixed_caps(parser, test_list): 

177 wrapped_code = b""" 

178 module foo 

179 SUBROUTINE X(Y) 

180 CHARACTER(len=100), PARAMETER :: sub_name = "X" 

181 CALL TRACE_ENTRY(SUB_NAME, STAT) 

182 CALL TRACE_EXIT(SUB_NAME, STAT) 

183 END SUBROUTINE X 

184 end module foo 

185 """ 

186 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

187 assert len(error_log.errors) == 0 

188 

189 

190def test_trace_entry_exit_correct_by_param_all_caps(parser, test_list): 

191 wrapped_code = b""" 

192 module foo 

193 SUBROUTINE X(Y) 

194 CHARACTER(len=100), PARAMETER :: SUB_NAME = "X" 

195 CALL TRACE_ENTRY(SUB_NAME, STAT) 

196 CALL TRACE_EXIT(SUB_NAME, STAT) 

197 END SUBROUTINE X 

198 end module foo 

199 """ 

200 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

201 assert len(error_log.errors) == 0 

202 

203 

204def test_trace_entry_exit_correct_function(parser, test_list): 

205 wrapped_code = b""" 

206 module foo 

207 function X(Y) 

208 CALL TRACE_ENTRY("x", STAT) 

209 CALL TRACE_EXIT("x", STAT) 

210 end function X 

211 end module foo 

212 """ 

213 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

214 assert len(error_log.errors) == 0 

215 

216 

217def test_trace_entry_exit_missing_function(parser, test_list): 

218 wrapped_code = b""" 

219 module foo 

220 function X(Y) 

221 end function X 

222 end module foo 

223 """ 

224 error_log = run_tests_on_code(parser, wrapped_code, test_list, "filename") 

225 assert len(error_log.errors) == 2