Coverage for src/castep_linter/tests/number_literal_correct_kind.py: 91%

1"""Test that a number literal has a dp (if real) or no dp if of any other type"""

2 from castep_linter.error_logging import ErrorLogger

3 from castep_linter.fortran.fortran_node import Fortran, FortranNode, WrongNodeError

4 from castep_linter.tests import castep_identifiers

5

6

7 def test_number_literal(node: FortranNode, error_log: ErrorLogger) -> None:

8 """Test that a number literal has a dp (if real) or no dp if of any other type"""

9

10 if not node.is_type(Fortran.NUMBER_LITERAL): 10 ↛ 11line 10 didn't jump to line 11, because the condition on line 10 was never true

11 err = "Expected number literal node"

12 raise WrongNodeError(err)

13

14 literal_string = node.raw.lower()

15

16 if "_" in literal_string:

17 value, kind = literal_string.split("_", maxsplit=1)

18

19 if is_int(value):

20 error_log.add_msg("Error", node, f"Integer literal with {kind=}")

21 elif kind not in castep_identifiers.DP_ALL:

22 error_log.add_msg("Error", node, f"Float literal with {kind=}")

23

24 elif "d" in literal_string:

25 pass # eg 5.0d4

26 elif not is_int(literal_string):

27 error_log.add_msg("Error", node, "Float literal without kind")

28

29

30 def is_int(x: str) -> bool:

31 return not any(c in x.lower() for c in [".", "e", "d"])