forked from python-lsp/python-lsp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_references.py
More file actions
82 lines (61 loc) · 2.42 KB
/
test_references.py
File metadata and controls
82 lines (61 loc) · 2.42 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
# Copyright 2017-2020 Palantir Technologies, Inc.
# Copyright 2021- Python Language Server Contributors.
import os
import pytest
from pylsp import uris
from pylsp.plugins.references import pylsp_references
from pylsp.workspace import Document
DOC1_NAME = "test1.py"
DOC2_NAME = "test2.py"
DOC1 = """class Test1():
pass
"""
DOC2 = """from test1 import Test1
try:
Test1()
except UnicodeError:
pass
"""
@pytest.fixture
def tmp_workspace(temp_workspace_factory):
return temp_workspace_factory(
{
DOC1_NAME: DOC1,
DOC2_NAME: DOC2,
}
)
def test_references(tmp_workspace) -> None:
# Over 'Test1' in class Test1():
position = {"line": 0, "character": 8}
DOC1_URI = uris.from_fs_path(os.path.join(tmp_workspace.root_path, DOC1_NAME))
doc1 = Document(DOC1_URI, tmp_workspace)
refs = pylsp_references(doc1, position, exclude_declaration=False)
# Definition, the import and the instantiation
assert len(refs) == 3
# Briefly check excluding the definitions (also excludes imports, only counts uses)
no_def_refs = pylsp_references(doc1, position, exclude_declaration=True)
assert len(no_def_refs) == 1
# Make sure our definition is correctly located
doc1_ref = [u for u in refs if u["uri"] == DOC1_URI][0]
assert doc1_ref["range"]["start"] == {"line": 0, "character": 6}
assert doc1_ref["range"]["end"] == {"line": 0, "character": 11}
# Make sure our import is correctly located
doc2_import_ref = [u for u in refs if u["uri"] != DOC1_URI][0]
assert doc2_import_ref["range"]["start"] == {"line": 0, "character": 18}
assert doc2_import_ref["range"]["end"] == {"line": 0, "character": 23}
doc2_usage_ref = [u for u in refs if u["uri"] != DOC1_URI][1]
assert doc2_usage_ref["range"]["start"] == {"line": 3, "character": 4}
assert doc2_usage_ref["range"]["end"] == {"line": 3, "character": 9}
def test_references_builtin(tmp_workspace) -> None:
# Over 'UnicodeError':
position = {"line": 4, "character": 7}
doc2_uri = uris.from_fs_path(os.path.join(str(tmp_workspace.root_path), DOC2_NAME))
doc2 = Document(doc2_uri, tmp_workspace)
refs = pylsp_references(doc2, position, exclude_declaration=False)
assert len(refs) >= 1
expected = {
"start": {"line": 4, "character": 7},
"end": {"line": 4, "character": 19},
}
ranges = [r["range"] for r in refs]
assert expected in ranges