Coverage for tests/test_allocate_stat_checked.py: 100%

1# pylint: disable=W0621,C0116,C0114

2 import pytest

3

4 from castep_linter import tests

5 from castep_linter.fortran.fortran_node import Fortran, WrongNodeError

6 from castep_linter.fortran.parser import get_fortran_parser

7 from castep_linter.scan_files import run_tests_on_code

8 from unittest import mock

9

10 from castep_linter.tests.allocate_stat_checked import check_allocate_has_stat

11

12

13@pytest.fixture

14 def test_list():

15 return {"call_expression": [tests.check_allocate_has_stat]}

16

17

18@pytest.fixture

19 def parser():

20 return get_fortran_parser()

21

22

23 def subroutine_wrapper(code):

24 return (

25 b"""module foo

26 subroutine x(y)

27 """

28 + code

29 + b"""

30 end subroutine x

31 end module foo"""

32 )

33

34

35 def test_wrong_node():

36 mock_node = mock.Mock(**{"is_type.return_value": False})

37 err_log = mock.MagicMock()

38 with pytest.raises(WrongNodeError):

39 check_allocate_has_stat(mock_node, err_log)

40

41

42 def test_allocate_stat_correct(parser, test_list):

43 code = b"""

44 allocate(stat_checked_var(x,y,z), stat=u)

45 if (u/=0) STOP 'err'

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

52 def test_allocate_stat_correct_wrong_way(parser, test_list):

53 code = b"""

54 allocate(stat_checked_var(x,y,z), stat=u)

55 if (0/=u) STOP 'err'

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

62 def test_allocate_stat_correct_if_but_not_checked(parser, test_list):

63 code = b"""

64 allocate(stat_checked_var(x,y,z), stat=u)

65 if (0/=z) STOP 'err'

66 """

67 wrapped_code = subroutine_wrapper(code)

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

69 assert len(error_log.errors) == 1

70

71

72 def test_allocate_stat_correct_mixed_caps(parser, test_list):

73 code = b"""

74 allocate(stat_checked_var(x,y,z), stat=u)

75 if (U/=0) STOP 'err'

76 """

77 wrapped_code = subroutine_wrapper(code)

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

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

80

81

82 def test_allocate_stat_correct_mixed_caps2(parser, test_list):

83 code = b"""

84 allocate(stat_checked_var(x,y,z), stat=U)

85 if (u/=0) STOP 'err'

86 """

87 wrapped_code = subroutine_wrapper(code)

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

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

90

91

92 def test_allocate_stat_correct_comment(parser, test_list):

93 code = b"""

94 allocate(stat_checked_var(x,y,z), stat=u)

95 ! comment

96 if (u/=0) STOP 'err'

97 """

98 wrapped_code = subroutine_wrapper(code)

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

100 assert len(error_log.errors) == 0

101

102

103 def test_allocate_no_stat(parser, test_list):

104 code = b"""

105 allocate(stat_checked_var(x,y,z))

106 """

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) == 1

110

111

112 def test_allocate_stat_not_checked(parser, test_list):

113 code = b"""

114 allocate(stat_checked_var(x,y,z), stat=u)

115 """

116 wrapped_code = subroutine_wrapper(code)

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

118 assert len(error_log.errors) == 1

119

120

121 def test_allocate_stat_not_checked_with_line_after(parser, test_list):

122 code = b"""

123 allocate(stat_checked_var(x,y,z), stat=u)

124 x = 5

125 """

126 wrapped_code = subroutine_wrapper(code)

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

128 assert len(error_log.errors) == 1