-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathtest_definitions.py
111 lines (81 loc) · 3.14 KB
/
test_definitions.py
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
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.
import os
from pylsp import uris
from pylsp.plugins.definition import pylsp_definitions
from pylsp.workspace import Document
DOC_URI = uris.from_fs_path(__file__)
DOC = """def a():
pass
print a()
class Directory(object):
def __init__(self):
self.members = dict()
def add_member(self, id, name):
self.members[id] = name
"""
def test_definitions(config, workspace):
# Over 'a' in print a
cursor_pos = {'line': 3, 'character': 6}
# The definition of 'a'
def_range = {
'start': {'line': 0, 'character': 4},
'end': {'line': 0, 'character': 5}
}
doc = Document(DOC_URI, workspace, DOC)
assert [{'uri': DOC_URI, 'range': def_range}] == pylsp_definitions(config, workspace, doc, cursor_pos)
def test_builtin_definition(config, workspace):
# Over 'i' in dict
cursor_pos = {'line': 8, 'character': 24}
doc = Document(DOC_URI, workspace, DOC)
orig_settings = config.settings()
# Check definition for `dict` goes to `builtins.pyi::dict`
follow_defns_setting = {'follow_builtin_definitions': True}
settings = {'plugins': {'jedi_definition': follow_defns_setting}}
config.update(settings)
defns = pylsp_definitions(config, workspace, doc, cursor_pos)
assert len(defns) == 1
assert defns[0]["uri"].endswith("builtins.pyi")
# Check no definitions for `dict`
follow_defns_setting['follow_builtin_definitions'] = False
config.update(settings)
defns = pylsp_definitions(config, workspace, doc, cursor_pos)
assert not defns
config.update(orig_settings)
def test_assignment(config, workspace):
# Over 's' in self.members[id]
cursor_pos = {'line': 11, 'character': 19}
# The assignment of 'self.members'
def_range = {
'start': {'line': 8, 'character': 13},
'end': {'line': 8, 'character': 20}
}
doc = Document(DOC_URI, workspace, DOC)
assert [{'uri': DOC_URI, 'range': def_range}] == pylsp_definitions(config, workspace, doc, cursor_pos)
def test_document_path_definitions(config, workspace_other_root_path, tmpdir):
# 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():
pass
'''
p = tmpdir.join("mymodule.py")
p.write(module_content)
# Content of doc to test definition
doc_content = """from mymodule import 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)
# The range where is defined in mymodule.py
def_range = {
'start': {'line': 1, 'character': 4},
'end': {'line': 1, 'character': 7}
}
# The position where foo is called in myfile.py
cursor_pos = {'line': 0, 'character': 24}
# The uri for mymodule.py
module_path = str(p)
module_uri = uris.from_fs_path(module_path)
assert [{"uri": module_uri, "range": def_range}] == pylsp_definitions(
config, workspace_other_root_path, doc, cursor_pos
)