-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathtest_hover.py
More file actions
165 lines (121 loc) · 5.24 KB
/
test_hover.py
File metadata and controls
165 lines (121 loc) · 5.24 KB
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
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.
import os
from pylsp import uris
from pylsp.plugins.hover import pylsp_hover
from pylsp.workspace import Document
DOC_URI = uris.from_fs_path(__file__)
DOC = """
def main(a: float, b: float):
\"\"\"hello world\"\"\"
pass
"""
NUMPY_DOC = """
import numpy as np
np.sin
"""
def test_numpy_hover(workspace) -> None:
# Over the blank line
no_hov_position = {"line": 1, "character": 0}
# Over 'numpy' in import numpy as np
numpy_hov_position_1 = {"line": 2, "character": 8}
# Over 'np' in import numpy as np
numpy_hov_position_2 = {"line": 2, "character": 17}
# Over 'np' in np.sin
numpy_hov_position_3 = {"line": 3, "character": 1}
# Over 'sin' in np.sin
numpy_sin_hov_position = {"line": 3, "character": 4}
doc = Document(DOC_URI, workspace, NUMPY_DOC)
contents = ""
assert contents in pylsp_hover(doc._config, doc, no_hov_position)["contents"]
hover_res = pylsp_hover(doc._config, doc, numpy_hov_position_1)
if hover_res["contents"] == "":
import pytest
pytest.skip("Jedi was unable to find hover information for numpy")
contents = "NumPy\n=====\n\nProvides\n"
if isinstance(hover_res["contents"], dict) and "value" in hover_res["contents"]:
assert contents in hover_res["contents"]["value"]
else:
assert contents in hover_res["contents"]
contents = "NumPy\n=====\n\nProvides\n"
hover_res = pylsp_hover(doc._config, doc, numpy_hov_position_2)
if isinstance(hover_res["contents"], dict) and "value" in hover_res["contents"]:
assert contents in hover_res["contents"]["value"]
else:
assert contents in hover_res["contents"]
contents = "NumPy\n=====\n\nProvides\n"
hover_res = pylsp_hover(doc._config, doc, numpy_hov_position_3)
if isinstance(hover_res["contents"], dict) and "value" in hover_res["contents"]:
assert contents in hover_res["contents"]["value"]
else:
assert contents in hover_res["contents"]
# https://github.com/davidhalter/jedi/issues/1746
import numpy as np
if np.lib.NumpyVersion(np.__version__) < "1.20.0":
contents = "Trigonometric sine, element-wise.\n\n"
hover_res = pylsp_hover(doc._config, doc, numpy_sin_hov_position)
if isinstance(hover_res["contents"], dict) and "value" in hover_res["contents"]:
assert contents in hover_res["contents"]["value"]
else:
assert contents in hover_res["contents"]
def test_hover(workspace) -> None:
# Over 'main' in def main():
hov_position = {"line": 2, "character": 6}
# Over the blank second line
no_hov_position = {"line": 1, "character": 0}
doc = Document(DOC_URI, workspace, DOC)
contents = {
"kind": "markdown",
"value": "```python\nmain(a: float, b: float)\n```\n\n\nhello world",
}
assert {"contents": contents} == pylsp_hover(doc._config, doc, hov_position)
assert {"contents": ""} == pylsp_hover(doc._config, doc, no_hov_position)
def test_hover_signature_formatting(workspace) -> None:
# Over 'main' in def main():
hov_position = {"line": 2, "character": 6}
doc = Document(DOC_URI, workspace, DOC)
# setting low line length should trigger reflow to multiple lines
doc._config.update({"signature": {"line_length": 10}})
contents = {
"kind": "markdown",
"value": "```python\nmain(\n a: float,\n b: float,\n)\n```\n\n\nhello world",
}
assert {"contents": contents} == pylsp_hover(doc._config, doc, hov_position)
def test_hover_signature_formatting_opt_out(workspace) -> None:
# Over 'main' in def main():
hov_position = {"line": 2, "character": 6}
doc = Document(DOC_URI, workspace, DOC)
doc._config.update({"signature": {"line_length": 10, "formatter": None}})
contents = {
"kind": "markdown",
"value": "```python\nmain(a: float, b: float)\n```\n\n\nhello world",
}
assert {"contents": contents} == pylsp_hover(doc._config, doc, hov_position)
def test_document_path_hover(workspace_other_root_path, tmpdir) -> None:
# Create a dummy module out of the workspace's root_path and try to get
# a definition on it in another file placed next to it.
module_content = '''
def foo():
"""A docstring for foo."""
pass
'''
p = tmpdir.join("mymodule.py")
p.write(module_content)
# Content of doc to test definition
doc_content = """from mymodule import foo
foo"""
doc_path = str(tmpdir) + os.path.sep + "myfile.py"
doc_uri = uris.from_fs_path(doc_path)
doc = Document(doc_uri, workspace_other_root_path, doc_content)
cursor_pos = {"line": 1, "character": 3}
contents = pylsp_hover(doc._config, doc, cursor_pos)["contents"]
assert "A docstring for foo." in contents["value"]
def test_hover_without_docstring(workspace_with_signature_docstring_disabled) -> None:
# Over 'main' in def main():
hov_position = {"line": 2, "character": 6}
doc = Document(DOC_URI, workspace_with_signature_docstring_disabled, DOC)
contents = {
"kind": "markdown",
"value": "```python\nmain(a: float, b: float)\n```\n",
}
assert {"contents": contents} == pylsp_hover(doc._config, doc, hov_position)