Skip to content

Commit da83383

Browse files
committed
chore: add tests for multi-line fetches
1 parent ff6d62c commit da83383

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

test/test_parser.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pytest
12
from setup_tests import test_dir
23

34
from fortls.parsers.internal.parser import FortranFile
@@ -57,3 +58,41 @@ def test_weird_parser_bug():
5758
ast = file.parse()
5859
assert err_str is None
5960
assert not ast.end_errors
61+
62+
63+
@pytest.mark.parametrize(
64+
"ln_no, pp_defs, reference",
65+
[
66+
(6, {}, 6),
67+
(7, {}, 6),
68+
(8, {}, 6),
69+
(11, {"TEST": True}, 60), # not entirely correct ref vals
70+
(23, {"MULT": True}, 90), # not entirely correct ref vals
71+
(32, {"TEST": True, "MULT": True}, 130), # not entirely correct ref vals
72+
(39, {"TEST": True, "MULT": True}, 2400), # not entirely correct ref vals
73+
],
74+
)
75+
def test_get_code_line_multilines(ln_no: int, pp_defs: dict, reference: int):
76+
"""Tests how the get_code_line performs with multi-line and preprocessor
77+
78+
Not all the results are correct, since get_code_line is not aware of the
79+
preprocessor skips. Instead what it does is it evaluates all the line
80+
continuations and appends them in post.
81+
"""
82+
83+
def calc_result(res: tuple):
84+
pre, cur, post = res
85+
res = "".join(pre + [cur] + post).replace(" ", "")
86+
assert "result" in res, "Fortran variable `result` not found in results"
87+
loc = {}
88+
exec(res, None, loc)
89+
return loc["result"]
90+
91+
file_path = test_dir / "parse" / "mixed" / "multilines.F90"
92+
file = FortranFile(str(file_path))
93+
file.load_from_disk()
94+
file.preprocess(pp_defs=pp_defs)
95+
pp = bool(pp_defs)
96+
res = file.get_code_line(line_no=ln_no, pp_content=pp)
97+
result = calc_result(res)
98+
assert result == reference
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
program multiline_tests
2+
implicit none
3+
integer :: result
4+
character(len=100) :: str
5+
6+
! Test: Simple multi-line continuation
7+
result = 1 + &
8+
2 + &
9+
3
10+
11+
! Test: Multi-line continuation with a preprocessor directive
12+
result = 10 + &
13+
#ifdef TEST
14+
20 + &
15+
#endif
16+
30
17+
18+
! Test: Multi-line continuation with string concatenation
19+
str = 'Hello' // &
20+
& ' ' // &
21+
& 'World'
22+
23+
! Test: Multi-line continuation with mixed preprocessor and arithmetic operations
24+
result = &
25+
#ifdef MULT
26+
(10*2) + &
27+
#else
28+
(10 * 3) + &
29+
#endif
30+
& 10 * 4
31+
32+
! Test: Multi-line continuation with C preprocessor && sequence
33+
result = 100 + &
34+
#if defined(TEST) && defined(MULT)
35+
&(20) + &
36+
#endif
37+
&10
38+
39+
! Test: multiplee Multi-line continuation with C preprocessor and comments
40+
result = 1000 + & ! Comment 0
41+
#if defined( TEST ) && defined( MULT )
42+
&100 + & ! Comment 1
43+
&200+& !! Comment 2
44+
#else
45+
500 + & !!! Comment 3
46+
#endif
47+
&600
48+
49+
end program multiline_tests

0 commit comments

Comments
 (0)