This repository was archived by the owner on Mar 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_langserver.py
More file actions
182 lines (155 loc) · 5.19 KB
/
test_langserver.py
File metadata and controls
182 lines (155 loc) · 5.19 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/local/bin/python3
import os.path
import sys
import opentracing
import pytest
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
from langserver.fs import InMemoryFileSystem # noqa: E402
from langserver.langserver import LangServer # noqa: E402
from langserver.symbols import extract_symbols # noqa: E402
FS = InMemoryFileSystem({
'/example_file.py':
'''
class MyClass(object):
def __init__(self):
pass
def foo(self):
def bar():
pass
pass
def _private(self):
pass
def baz():
pass
X = "hi"
''',
'/a.py':
'''
class A:
"A doc string"
pass
''',
'/b.py':
'''
import fnmatch
from .a import A
if __name__ == '__main__':
A()
print(fnmatch.fnmatchcase("test","t*"))
''',
'/c.py':
'''
class C(object):
def bar(self):
foo = ""
printf(foo)
return str("This is a string literal")
''',
})
def test_extract_symbols():
import json
example_file = FS.open("/example_file.py", parent_span=None)
want = '''{"kind": 5, "location": {"range": {"end": {"character": 7, "line": 1}, "start": {"character": 0, "line": 1}}, "uri": "file://example_file.py"}, "name": "MyClass"}
{"containerName": "MyClass", "kind": 6, "location": {"range": {"end": {"character": 12, "line": 2}, "start": {"character": 4, "line": 2}}, "uri": "file://example_file.py"}, "name": "__init__"}
{"containerName": "MyClass", "kind": 6, "location": {"range": {"end": {"character": 7, "line": 5}, "start": {"character": 4, "line": 5}}, "uri": "file://example_file.py"}, "name": "foo"}
{"containerName": "MyClass", "kind": 6, "location": {"range": {"end": {"character": 12, "line": 10}, "start": {"character": 4, "line": 10}}, "uri": "file://example_file.py"}, "name": "_private"}
{"kind": 12, "location": {"range": {"end": {"character": 3, "line": 13}, "start": {"character": 0, "line": 13}}, "uri": "file://example_file.py"}, "name": "baz"}
{"kind": 13, "location": {"range": {"end": {"character": 1, "line": 16}, "start": {"character": 0, "line": 16}}, "uri": "file://example_file.py"}, "name": "X"}'''
symbols = extract_symbols(example_file, 'example_file.py')
got = "\n".join(json.dumps(s.json_object(), sort_keys=True)
for s in symbols)
assert got == want
def test_hover_on_def():
h = hover('/a.py', 1, 7)
assert h == {
'contents': [{
'language': 'python',
'value': 'class A(param type(self))'
}, 'A doc string']
}
def test_hover_on_string_literal():
h = hover('/c.py', 5, 21)
assert h == {"contents": []} # expect no hover on a string literal
def test_hover_on_string_variable():
h = hover('/c.py', 4, 15)
assert h == {'contents': [{'language': 'python', 'value': 'str'}]}
def test_hover_on_str():
h = hover('/c.py', 5, 16)
assert h == {
'contents': [
{
'language': 'python',
'value': 'class str(param object)'
},
"str(object='') -> str\n"
'str(bytes_or_buffer[, encoding[, errors]]) -> str\n'
'\n'
'Create a new string object from the given object. If encoding '
'or\n'
'errors is specified, then the object must expose a data buffer\n'
'that will be decoded using the given encoding and error '
'handler.\n'
'Otherwise, returns the result of object.__str__() (if defined)\n'
'or repr(object).\n'
'encoding defaults to sys.getdefaultencoding().\n'
"errors defaults to 'strict'."]
}
@pytest.mark.skip(reason="Failing")
def test_hover_another_file():
h = hover('/b.py', 4, 5)
assert h == {
'contents': [{
'language': 'python',
'value': 'class A(param type(self))'
}, 'A doc string']
}
def test_hover_stdlib():
h = hover('/b.py', 5, 23)
assert h == {
'contents':
[{
'language': 'python',
'value': 'def fnmatchcase(param name, param pat)'
},
"Test whether FILENAME matches PATTERN, including case.\n"
"\nThis is a version of fnmatch() which doesn't case-normalize\n"
"its arguments."
]
}
def hover(path, line, character):
server = LangServer(conn=None)
server.fs = FS
server.root_path = '/'
return server.serve_hover({
"params": {
"textDocument": {
"uri": "file://" + path
},
"position": {
"line": line,
"character": character,
},
},
"span": opentracing.tracer.start_span()
})
def test_inmemory_fs():
contents = {
"/a": "a",
"/aa": "aa",
"/b/b": "bb",
"/b/c/d/e": "bcde",
"/bb/b": "bbb",
}
fs = InMemoryFileSystem(contents)
for path, v in contents.items():
assert fs.open(path, parent_span=None) == v
dirs = {
"/": ["a", "aa", "b", "bb"],
"/b": ["b", "c"],
"/b/c": ["d"],
"/bb": ["b"],
}
for d, want in dirs.items():
want = sorted(want)
got = sorted([e.name for e in fs.listdir(d, parent_span=None)])
assert got == want