File: test_server_documentation.py

package info (click to toggle)
fortran-language-server 3.2.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,268 kB
  • sloc: python: 9,688; f90: 1,195; fortran: 30; makefile: 28; ansic: 20
file content (372 lines) | stat: -rw-r--r-- 11,628 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
from setup_tests import run_request, test_dir, write_rpc_request


def check_return(result_array, checks, only_docs=False):
    comm_lines = []
    found_docs = False
    idx = 0
    for i, hover_line in enumerate(result_array["contents"]["value"].splitlines()):
        if hover_line == "-----":
            found_docs = True
        if found_docs and only_docs:
            comm_lines.append((idx, hover_line))
            idx += 1
        elif not only_docs:
            comm_lines.append((i, hover_line))
    assert len(comm_lines) == len(checks)
    for i in range(len(checks)):
        assert comm_lines[i][0] == checks[i][0]
        assert comm_lines[i][1] == checks[i][1]


def hover_request(file_path, line, char):
    return write_rpc_request(
        1,
        "textDocument/hover",
        {
            "textDocument": {"uri": str(file_path)},
            "position": {"line": line, "character": char},
        },
    )


def test_doxygen():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "docs")})
    file_path = test_dir / "docs" / "test_doxygen.f90"
    string += hover_request(file_path, 15, 17)
    errcode, results = run_request(string, ["-n1"])
    assert errcode == 0
    ref = (
        (0, "```fortran90"),
        (1, "SUBROUTINE insert(list, n, max_size, new_entry)"),
        (2, " REAL, DIMENSION(:), INTENT(INOUT) :: list"),
        (3, " INTEGER, INTENT(IN) :: n"),
        (4, " INTEGER, INTENT(IN) :: max_size"),
        (5, " REAL, INTENT(IN) :: new_entry"),
        (6, "```"),
        (7, "-----"),
        (8, "inserts a value into an ordered array"),
        (9, ""),
        (
            10,
            (
                'An array "list" consisting of n ascending ordered values. The method'
                " insert a"
            ),
        ),
        (11, '"new_entry" into the array.'),
        (12, "hint: use cshift and eo-shift"),
        (13, ""),
        (14, ""),
        (15, "**Parameters:**  "),
        (16, "`list` - a real array, size: max_size   "),
        (17, "`n` - current values in the array   "),
        (18, "`max_size` - size if the array   "),
        (19, "`new_entry` - the value to insert "),
    )
    check_return(results[1], ref)


def test_ford():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "docs")})
    file_path = test_dir / "docs" / "test_ford.f90"
    string += hover_request(file_path, 5, 20)
    errcode, results = run_request(string)
    assert errcode == 0
    ref = (
        (0, "```fortran90"),
        (1, "SUBROUTINE feed_pets(cats, dogs, food, angry)"),
        (2, " INTEGER, INTENT(IN) :: cats"),
        (3, " INTEGER, INTENT(IN) :: dogs"),
        (4, " REAL, INTENT(INOUT) :: food"),
        (5, " INTEGER, INTENT(OUT) :: angry"),
        (6, "```"),
        (7, "-----"),
        (8, "Feeds your cats and dogs, if enough food is available. If not enough"),
        (9, "food is available, some of your pets will get angry."),
        (10, "   "),
        (11, ""),
        (12, "**Parameters:**     "),
        (13, "`cats` The number of cats to keep track of.   "),
        (14, "`dogs` The number of dogs to keep track of.   "),
        (15, "`food` The amount of pet food (in kilograms) which you have on hand.   "),
        (16, "`angry` The number of pets angry because they weren't fed."),
    )
    check_return(results[1], ref)


def test_doc_overwrite_type_bound_procedure_sub():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
    file_path = test_dir / "subdir" / "test_free.f90"
    # Test we can override method docstring e.g.
    # procedure :: name => name_imp !< Doc override
    # We want to preserve the argument list docstring
    string += hover_request(file_path, 13, 19)
    errcode, results = run_request(string)
    assert errcode == 0
    check_return(
        results[1],
        (
            (0, "-----"),
            (1, "Doc 1   "),
            (2, ""),
            (3, "**Parameters:**     "),
            (4, "`n` Doc 5"),
        ),
        True,
    )


def test_doc_type_bound_procedure_sub_implementation():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
    file_path = test_dir / "subdir" / "test_free.f90"
    # procedure :: name => name_imp !< Doc override
    # Test that name_imp will yield the full docstring present in the implementation
    string += hover_request(file_path, 13, 31)
    errcode, results = run_request(string)
    assert errcode == 0
    check_return(
        results[1],
        (
            (0, "-----"),
            (1, "Doc 4   "),
            (2, ""),
            (3, "**Parameters:**     "),
            (4, "`n` Doc 5"),
        ),
        True,
    )


def test_doc_variable():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
    file_path = test_dir / "subdir" / "test_free.f90"
    # n !! Doc 5
    # Test that a variable can carry over documentation
    string += hover_request(file_path, 37, 26)
    errcode, results = run_request(string)
    assert errcode == 0
    check_return(
        results[1],
        (
            (0, "-----"),
            (1, "Doc 5"),
        ),
        True,
    )


def test_doc_overwrite_type_bound_procedure_fun():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
    file_path = test_dir / "subdir" / "test_free.f90"
    # Test we can override function docstring e.g.
    # procedure :: name => name_imp !< Doc override
    # We want to preserve the argument list docstring
    string += hover_request(file_path, 14, 17)
    errcode, results = run_request(string)
    assert errcode == 0
    check_return(
        results[1],
        (
            (0, "-----"),
            (1, "Doc 2"),
        ),
        True,
    )


def test_doc_type_bound_procedure_fun_implementation():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
    file_path = test_dir / "subdir" / "test_free.f90"
    # procedure :: name => name_imp !< Doc override
    # Test that name_imp will yield the full docstring present in the implementation
    string += hover_request(file_path, 14, 28)
    errcode, results = run_request(string)
    assert errcode == 0
    check_return(
        results[1],
        (
            (0, "-----"),
            (1, "Doc 6"),
        ),
        True,
    )


def test_doc_empty_overwrite_type_bound_procedure_sub():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
    file_path = test_dir / "subdir" / "test_free.f90"
    # Test we can ignore overriding method docstring  and return the original e.g.
    # procedure :: name => name_imp !<
    # We want to preserve the argument list docstring
    # the self argument in the second request is not included because it is
    # missing a doc string
    string += hover_request(file_path, 21, 18)
    string += hover_request(file_path, 21, 37)
    errcode, results = run_request(string)
    assert errcode == 0
    check_return(
        results[1],
        (
            (0, "-----"),
            (1, "Doc 7   "),
            (2, ""),
            (3, "**Parameters:**     "),
            (4, "`scale` Doc 8"),
        ),
        True,
    )
    check_return(
        results[2],
        (
            (0, "-----"),
            (1, "Doc 7   "),
            (2, ""),
            (3, "**Parameters:**     "),
            (4, "`scale` Doc 8"),
        ),
        True,
    )


def test_doc_empty_overwrite_type_bound_procedure_fun():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
    file_path = test_dir / "subdir" / "test_free.f90"
    # procedure :: name => name_imp !< Doc
    # We want to preserve the procedure docstring but also fetch the empty
    # docs for the implementation
    string += hover_request(file_path, 22, 17)
    string += hover_request(file_path, 22, 32)
    errcode, results = run_request(string)
    assert errcode == 0
    check_return(
        results[1],
        (
            (0, "-----"),
            (1, "Doc 3  "),
            (2, ""),
            (3, "**Return:**  "),
            (4, "`norm`return value docstring"),
        ),
        True,
    )
    check_return(
        results[2],
        (
            (0, "-----"),
            (1, "Top level docstring  "),
            (2, ""),
            (3, "**Parameters:**    "),
            (4, "`self` self value docstring  "),
            (5, ""),
            (6, "**Return:**  "),
            (7, "`norm`return value docstring"),
        ),
        True,
    )


def test_doc_multiline_type_bound_procedure_arg_list():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir)})
    file_path = test_dir / "subdir" / "test_free.f90"
    # Check that inline docstrings can be input and carried over in multiple lines
    # for both the procedure pointer and the implementation
    string += hover_request(file_path, 15, 32)
    string += hover_request(file_path, 15, 47)
    errcode, results = run_request(string)
    assert errcode == 0
    check_return(
        results[1],
        (
            (0, "-----"),
            (1, "Doc 3  "),
            (2, ""),
            (3, "**Parameters:**     "),
            (4, "`arg1` Doc 9"),
            (5, "Doc 10"),
        ),
        True,
    )
    check_return(
        results[2],
        (
            (0, "-----"),
            (1, ""),
            (2, "**Parameters:**     "),
            (3, "`arg1` Doc 9"),
            (4, "Doc 10   "),
            (5, "`self` Doc 11"),
            (6, "Doc 12"),
        ),
        True,
    )


def test_doxygen_doc_for_module_use():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "docs")})
    file_path = test_dir / "docs" / "test_module_and_type_doc.f90"
    string += hover_request(file_path, 24, 14)
    errcode, results = run_request(string)
    assert errcode == 0

    ref = (
        (0, "```fortran90"),
        (1, "MODULE doxygen_doc_mod"),
        (2, "```"),
        (3, "-----"),
        (4, "module doc for doxygen_doc_mod"),
        (5, ""),
        (6, "with info"),
    )
    check_return(results[1], ref)


def test_ford_doc_for_module_use():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "docs")})
    file_path = test_dir / "docs" / "test_module_and_type_doc.f90"
    string += hover_request(file_path, 25, 14)
    errcode, results = run_request(string)
    assert errcode == 0

    ref = (
        (0, "```fortran90"),
        (1, "MODULE ford_doc_mod"),
        (2, "```"),
        (3, "-----"),
        (4, "Doc for ford_doc_mod"),
    )
    check_return(results[1], ref)


def test_doxygen_doc_for_type():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "docs")})
    file_path = test_dir / "docs" / "test_module_and_type_doc.f90"
    string += hover_request(file_path, 27, 11)
    errcode, results = run_request(string)
    assert errcode == 0

    ref = (
        (0, "```fortran90"),
        (1, "TYPE :: a_t"),
        (2, "```"),
        (3, "-----"),
        (4, "Doc for a_t"),
    )
    check_return(results[1], ref)


def test_ford_doc_for_type():
    string = write_rpc_request(1, "initialize", {"rootPath": str(test_dir / "docs")})
    file_path = test_dir / "docs" / "test_module_and_type_doc.f90"
    string += hover_request(file_path, 28, 11)
    errcode, results = run_request(string)
    assert errcode == 0

    ref = (
        (0, "```fortran90"),
        (1, "TYPE :: b_t"),
        (2, "```"),
        (3, "-----"),
        (4, "Doc for b_t"),
    )
    check_return(results[1], ref)