This repository was archived by the owner on Mar 27, 2019. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtest_langserver.py
More file actions
139 lines (118 loc) · 3.89 KB
/
Copy pathtest_langserver.py
File metadata and controls
139 lines (118 loc) · 3.89 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
#!/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
from langserver.langserver import LangServer
from langserver.symbols import extract_symbols
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*"))
''',
})
def test_extract_symbols():
import json
example_file = FS.open("/example_file.py", parent_span=None)
want = '''{"name": "MyClass", "kind": 5, "location": {"uri": "file://example_file.py", "range": {"start": {"line": 1, "character": 0}, "end": {"line": 1, "character": 0}}}}
{"name": "__init__", "kind": 6, "location": {"uri": "file://example_file.py", "range": {"start": {"line": 2, "character": 4}, "end": {"line": 2, "character": 4}}}, "containerName": "MyClass"}
{"name": "foo", "kind": 6, "location": {"uri": "file://example_file.py", "range": {"start": {"line": 5, "character": 4}, "end": {"line": 5, "character": 4}}}, "containerName": "MyClass"}
{"name": "_private", "kind": 6, "location": {"uri": "file://example_file.py", "range": {"start": {"line": 10, "character": 4}, "end": {"line": 10, "character": 4}}}, "containerName": "MyClass"}
{"name": "baz", "kind": 12, "location": {"uri": "file://example_file.py", "range": {"start": {"line": 13, "character": 0}, "end": {"line": 13, "character": 0}}}}
{"name": "X", "kind": 13, "location": {"uri": "file://example_file.py", "range": {"start": {"line": 16, "character": 0}, "end": {"line": 16, "character": 0}}}}'''
symbols = extract_symbols(example_file, 'example_file.py')
got = "\n".join(json.dumps(s.json_object()) 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']
}
@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']
}
#@pytest.mark.skip(reason="Failing")
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\nits 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