Skip to content

Commit 26c9d79

Browse files
committed
Moves definition unittests to separate file
1 parent 1ad492f commit 26c9d79

File tree

3 files changed

+233
-84
lines changed

3 files changed

+233
-84
lines changed

test/test_server.py

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
# from types import NoneType
2-
from setup_tests import (
3-
path_to_uri,
4-
run_request,
5-
test_dir,
6-
write_rpc_notification,
7-
write_rpc_request,
8-
)
1+
from setup_tests import run_request, test_dir, write_rpc_notification, write_rpc_request
92

103

114
def test_init():
@@ -257,79 +250,3 @@ def sig_request(file_path, line, char):
257250
assert len(exp_results) + 1 == len(results)
258251
for i in range(len(exp_results)):
259252
check_return(results[i + 1], exp_results[i])
260-
261-
262-
def test_def():
263-
def check_return(result_array, checks):
264-
# If no definition is given result is None
265-
if result_array is None:
266-
assert not checks[0]
267-
return None
268-
assert result_array["uri"] == path_to_uri(checks[2])
269-
assert result_array["range"]["start"]["line"] == checks[0]
270-
assert result_array["range"]["start"]["line"] == checks[1]
271-
272-
def def_request(file_path, line, char):
273-
return write_rpc_request(
274-
1,
275-
"textDocument/definition",
276-
{
277-
"textDocument": {"uri": str(file_path)},
278-
"position": {"line": line, "character": char},
279-
},
280-
)
281-
282-
#
283-
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
284-
file_path = test_dir / "test_prog.f08"
285-
string += def_request(file_path, 12, 6)
286-
string += def_request(file_path, 13, 6)
287-
string += def_request(file_path, 20, 7)
288-
string += def_request(file_path, 21, 20)
289-
string += def_request(file_path, 21, 42)
290-
string += def_request(file_path, 23, 26)
291-
file_path = test_dir / "subdir" / "test_submod.F90"
292-
string += def_request(file_path, 30, 12)
293-
string += def_request(file_path, 35, 12)
294-
file_path = test_dir / "test_inc.f90"
295-
string += def_request(file_path, 2, 15)
296-
string += def_request(file_path, 10, 2)
297-
string += def_request(file_path, 12, 13)
298-
file_path = test_dir / "subdir" / "test_inc2.f90"
299-
string += def_request(file_path, 3, 2)
300-
file_path = test_dir / "subdir" / "test_rename.F90"
301-
string += def_request(file_path, 13, 5)
302-
string += def_request(file_path, 14, 5)
303-
file_path = test_dir / "hover" / "functions.f90"
304-
string += def_request(file_path, 3, 17)
305-
errcode, results = run_request(string)
306-
assert errcode == 0
307-
#
308-
fixed_path = str(test_dir / "subdir" / "test_fixed.f")
309-
free_path = str(test_dir / "subdir" / "test_free.f90")
310-
exp_results = (
311-
# test_prog.f08
312-
[0, 0, fixed_path],
313-
[22, 22, fixed_path],
314-
[10, 10, str(test_dir / "test_prog.f08")],
315-
[21, 21, free_path],
316-
[14, 14, free_path],
317-
[5, 5, free_path],
318-
# subdir/test_submod.F90
319-
[1, 1, str(test_dir / "subdir" / "test_submod.F90")],
320-
[1, 1, str(test_dir / "subdir" / "test_submod.F90")],
321-
# test_inc.f90
322-
[2, 2, str(test_dir / "subdir" / "test_inc2.f90")],
323-
[0, 0, str(test_dir / "subdir" / "test_inc2.f90")],
324-
[None],
325-
# subdir/test_inc2.f90
326-
[4, 4, str(test_dir / "test_inc.f90")],
327-
# subdir/test_rename.F90
328-
[6, 6, str(test_dir / "subdir" / "test_rename.F90")],
329-
[1, 1, str(test_dir / "subdir" / "test_rename.F90")],
330-
# hover/functions.f90
331-
[3, 3, str(test_dir / "hover" / "functions.f90")],
332-
)
333-
assert len(exp_results) + 1 == len(results)
334-
for i in range(len(exp_results)):
335-
check_return(results[i + 1], exp_results[i])

test/test_server_definitions.py

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
from pathlib import Path
2+
3+
from setup_tests import path_to_uri, run_request, test_dir, write_rpc_request
4+
5+
6+
def validate_def(result_array, checks):
7+
# If no definition is given result is None
8+
if result_array is None:
9+
assert not checks[0]
10+
return None
11+
assert result_array["uri"] == path_to_uri(checks[2])
12+
assert result_array["range"]["start"]["line"] == checks[0]
13+
assert result_array["range"]["start"]["line"] == checks[1]
14+
15+
16+
def def_request(uri: Path, line, char):
17+
return write_rpc_request(
18+
1,
19+
"textDocument/definition",
20+
{
21+
"textDocument": {"uri": str(uri)},
22+
"position": {"line": line - 1, "character": char - 1},
23+
},
24+
)
25+
26+
27+
def test_def_fun_sub_fixed():
28+
"""Test that going to definition of a function or submodule works."""
29+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
30+
file_path = test_dir / "test_prog.f08"
31+
string += def_request(file_path, 13, 7)
32+
string += def_request(file_path, 14, 7)
33+
errcode, results = run_request(string)
34+
assert errcode == 0
35+
fixed_path = str(test_dir / "subdir" / "test_fixed.f")
36+
ref_res = [[0, 0, fixed_path], [22, 22, fixed_path]]
37+
assert len(ref_res) == len(results) - 1
38+
for i in range(len(ref_res)):
39+
validate_def(results[i + 1], ref_res[i])
40+
41+
42+
def test_def_variable():
43+
"""Test that going to definition of a variable works."""
44+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
45+
file_path = test_dir / "test_prog.f08"
46+
string += def_request(file_path, 21, 8)
47+
errcode, results = run_request(string)
48+
assert errcode == 0
49+
ref_res = [[10, 10, str(test_dir / "test_prog.f08")]]
50+
assert len(ref_res) == len(results) - 1
51+
for i in range(len(ref_res)):
52+
validate_def(results[i + 1], ref_res[i])
53+
54+
55+
def test_def_type_bound_procedure1():
56+
"""Test that going to definition of type bound procedure works."""
57+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
58+
file_path = test_dir / "test_prog.f08"
59+
string += def_request(file_path, 22, 21)
60+
errcode, results = run_request(string)
61+
assert errcode == 0
62+
ref_res = [[21, 21, str(test_dir / "subdir" / "test_free.f90")]]
63+
assert len(ref_res) == len(results) - 1
64+
for i in range(len(ref_res)):
65+
validate_def(results[i + 1], ref_res[i])
66+
67+
68+
def test_def_type_bound_procedure2():
69+
"""Test that going to definition of type bound procedure works."""
70+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
71+
file_path = test_dir / "test_prog.f08"
72+
string += def_request(file_path, 22, 43)
73+
errcode, results = run_request(string)
74+
assert errcode == 0
75+
ref_res = [[14, 14, str(test_dir / "subdir" / "test_free.f90")]]
76+
assert len(ref_res) == len(results) - 1
77+
for i in range(len(ref_res)):
78+
validate_def(results[i + 1], ref_res[i])
79+
80+
81+
def test_def_type_nested_variable():
82+
"""Test that going to definition of type bound nested variables works."""
83+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
84+
file_path = test_dir / "test_prog.f08"
85+
string += def_request(file_path, 24, 27)
86+
errcode, results = run_request(string)
87+
assert errcode == 0
88+
ref_res = [[5, 5, str(test_dir / "subdir" / "test_free.f90")]]
89+
assert len(ref_res) == len(results) - 1
90+
for i in range(len(ref_res)):
91+
validate_def(results[i + 1], ref_res[i])
92+
93+
94+
def test_def_type_in_submod_function():
95+
"""Test that going into the definition of a type bound function in a submodule"""
96+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
97+
file_path = test_dir / "subdir" / "test_submod.F90"
98+
string += def_request(file_path, 31, 13)
99+
errcode, results = run_request(string)
100+
assert errcode == 0
101+
ref_res = [[1, 1, str(test_dir / "subdir" / "test_submod.F90")]]
102+
assert len(ref_res) == len(results) - 1
103+
for i in range(len(ref_res)):
104+
validate_def(results[i + 1], ref_res[i])
105+
106+
107+
def test_def_type_in_submod_procedure():
108+
"""Test that going into the definition of a type bound procedure in a submodule"""
109+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
110+
file_path = test_dir / "subdir" / "test_submod.F90"
111+
string += def_request(file_path, 36, 13)
112+
errcode, results = run_request(string)
113+
assert errcode == 0
114+
ref_res = [[1, 1, str(test_dir / "subdir" / "test_submod.F90")]]
115+
assert len(ref_res) == len(results) - 1
116+
for i in range(len(ref_res)):
117+
validate_def(results[i + 1], ref_res[i])
118+
119+
120+
def test_def_include_file():
121+
"""Test that going into the location of an include file works."""
122+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
123+
file_path = test_dir / "test_inc.f90"
124+
string += def_request(file_path, 3, 16)
125+
errcode, results = run_request(string)
126+
assert errcode == 0
127+
ref_res = [[2, 2, str(test_dir / "subdir" / "test_inc2.f90")]]
128+
assert len(ref_res) == len(results) - 1
129+
for i in range(len(ref_res)):
130+
validate_def(results[i + 1], ref_res[i])
131+
132+
133+
def test_def_include_variable1():
134+
"""Test that going to definition of a variable in an include file works."""
135+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
136+
file_path = test_dir / "test_inc.f90"
137+
string += def_request(file_path, 11, 3)
138+
errcode, results = run_request(string)
139+
assert errcode == 0
140+
ref_res = [[0, 0, str(test_dir / "subdir" / "test_inc2.f90")]]
141+
assert len(ref_res) == len(results) - 1
142+
for i in range(len(ref_res)):
143+
validate_def(results[i + 1], ref_res[i])
144+
145+
146+
def test_def_include_variable2():
147+
"""Test that going to definition of a variable in an include file works."""
148+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
149+
file_path = test_dir / "subdir" / "test_inc2.f90"
150+
string += def_request(file_path, 4, 3)
151+
errcode, results = run_request(string)
152+
assert errcode == 0
153+
ref_res = [[4, 4, str(test_dir / "test_inc.f90")]]
154+
assert len(ref_res) == len(results) - 1
155+
for i in range(len(ref_res)):
156+
validate_def(results[i + 1], ref_res[i])
157+
158+
159+
def test_def_include_file_missing():
160+
"""Test that going to the definition of a missing file will not break fortls"""
161+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
162+
file_path = test_dir / "test_inc.f90"
163+
string += def_request(file_path, 13, 14)
164+
errcode, results = run_request(string)
165+
assert errcode == 0
166+
ref_res = [[None]]
167+
assert len(ref_res) == len(results) - 1
168+
for i in range(len(ref_res)):
169+
validate_def(results[i + 1], ref_res[i])
170+
171+
172+
def test_def_rename_only_variable():
173+
"""Test that going to definition of a renamed list variable will take you
174+
to the original definition.
175+
"""
176+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
177+
file_path = test_dir / "subdir" / "test_rename.F90"
178+
string += def_request(file_path, 14, 6)
179+
errcode, results = run_request(string)
180+
assert errcode == 0
181+
ref_res = [[6, 6, str(test_dir / "subdir" / "test_rename.F90")]]
182+
assert len(ref_res) == len(results) - 1
183+
for i in range(len(ref_res)):
184+
validate_def(results[i + 1], ref_res[i])
185+
186+
187+
def test_def_rename_only_variable_nested():
188+
"""Test that going to definition of a renamed list variable will take you
189+
to the original definition, tests the multiply renamed case.
190+
"""
191+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
192+
file_path = test_dir / "subdir" / "test_rename.F90"
193+
string += def_request(file_path, 15, 6)
194+
errcode, results = run_request(string)
195+
assert errcode == 0
196+
ref_res = [[1, 1, str(test_dir / "subdir" / "test_rename.F90")]]
197+
assert len(ref_res) == len(results) - 1
198+
for i in range(len(ref_res)):
199+
validate_def(results[i + 1], ref_res[i])
200+
201+
202+
def test_def_function_implicit_result_variable():
203+
"""Test that going to definition on the implicitly defined variable RESULT
204+
works.
205+
"""
206+
string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
207+
file_path = test_dir / "hover" / "functions.f90"
208+
string += def_request(file_path, 4, 18)
209+
errcode, results = run_request(string)
210+
assert errcode == 0
211+
ref_res = [[3, 3, str(test_dir / "hover" / "functions.f90")]]
212+
assert len(ref_res) == len(results) - 1
213+
for i in range(len(ref_res)):
214+
validate_def(results[i + 1], ref_res[i])

test/test_source/tmp.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import os
2+
3+
from fparser.common.readfortran import FortranFileReader
4+
from fparser.two.parser import ParserFactory
5+
from fparser.two.utils import Base, walk
6+
7+
os.chdir("/home/gn/Code/Python/fortls/test/test_source")
8+
9+
reader = FortranFileReader("test.f90", include_dirs=["subdir"], ignore_comments=False)
10+
parser = ParserFactory().create(std="f2008")
11+
parse_tree = parser(reader)
12+
13+
for i, node in enumerate(walk(parse_tree)):
14+
if node is None:
15+
continue
16+
if not isinstance(node, Base):
17+
continue
18+
print(i, type(node), ": ", node)

0 commit comments

Comments
 (0)