Coverage for src/castep_linter/tests/number_literal_correct_kind.py: 91%
20 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-23 18:07 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-11-23 18:07 +0000
1"""Test that a number literal has a dp (if real) or no dp if of any other type"""
2from castep_linter.error_logging import ErrorLogger
3from castep_linter.fortran.fortran_node import Fortran, FortranNode, WrongNodeError
4from castep_linter.tests import castep_identifiers
7def 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"""
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)
14 literal_string = node.raw.lower()
16 if "_" in literal_string:
17 value, kind = literal_string.split("_", maxsplit=1)
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=}")
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")
30def is_int(x: str) -> bool:
31 return not any(c in x.lower() for c in [".", "e", "d"])