| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import json |
| 4 |
import os |
| 5 |
import pprint |
| 6 |
import re |
| 7 |
import shutil |
| 8 |
import subprocess |
| 9 |
import time |
| 10 |
import unittest |
| 11 |
|
| 12 |
ccls = os.getenv("CCLS", "ccls") |
| 13 |
path = os.path.dirname(os.path.realpath(__file__)) |
| 14 |
version = re.findall( |
| 15 |
r"ccls version ([\S]+)", |
| 16 |
subprocess.check_output([ccls, "--version"]).decode().strip(), |
| 17 |
)[0] |
| 18 |
|
| 19 |
tests = [ |
| 20 |
( |
| 21 |
{ |
| 22 |
"jsonrpc": "2.0", |
| 23 |
"id": 1, |
| 24 |
"method": "initialize", |
| 25 |
"params": {"rootUri": "file://" + path}, |
| 26 |
}, |
| 27 |
{ |
| 28 |
"jsonrpc": "2.0", |
| 29 |
"id": 1, |
| 30 |
"result": { |
| 31 |
"capabilities": { |
| 32 |
"textDocumentSync": { |
| 33 |
"openClose": True, |
| 34 |
"change": 2, |
| 35 |
"willSave": False, |
| 36 |
"willSaveWaitUntil": False, |
| 37 |
"save": {"includeText": False}, |
| 38 |
}, |
| 39 |
"hoverProvider": True, |
| 40 |
"completionProvider": { |
| 41 |
"resolveProvider": False, |
| 42 |
"triggerCharacters": [".", ":", ">", "#", "<", '"', "/"], |
| 43 |
}, |
| 44 |
"signatureHelpProvider": {"triggerCharacters": ["(", "{", ","]}, |
| 45 |
"declarationProvider": True, |
| 46 |
"definitionProvider": True, |
| 47 |
"implementationProvider": True, |
| 48 |
"typeDefinitionProvider": True, |
| 49 |
"referencesProvider": True, |
| 50 |
"documentHighlightProvider": True, |
| 51 |
"documentSymbolProvider": True, |
| 52 |
"workspaceSymbolProvider": True, |
| 53 |
"codeActionProvider": {"codeActionKinds": ["quickfix"]}, |
| 54 |
"codeLensProvider": {"resolveProvider": False}, |
| 55 |
"documentFormattingProvider": True, |
| 56 |
"documentRangeFormattingProvider": True, |
| 57 |
"documentOnTypeFormattingProvider": { |
| 58 |
"firstTriggerCharacter": "}", |
| 59 |
"moreTriggerCharacter": [], |
| 60 |
}, |
| 61 |
"renameProvider": True, |
| 62 |
"documentLinkProvider": {"resolveProvider": True}, |
| 63 |
"foldingRangeProvider": True, |
| 64 |
"executeCommandProvider": {"commands": ["ccls.xref"]}, |
| 65 |
"callHierarchyProvider": True, |
| 66 |
"semanticTokensProvider": { |
| 67 |
"legend": { |
| 68 |
"tokenTypes": [ |
| 69 |
"unknown", |
| 70 |
"file", |
| 71 |
"module", |
| 72 |
"namespace", |
| 73 |
"package", |
| 74 |
"class", |
| 75 |
"method", |
| 76 |
"property", |
| 77 |
"field", |
| 78 |
"constructor", |
| 79 |
"enum", |
| 80 |
"interface", |
| 81 |
"function", |
| 82 |
"variable", |
| 83 |
"constant", |
| 84 |
"string", |
| 85 |
"number", |
| 86 |
"boolean", |
| 87 |
"array", |
| 88 |
"object", |
| 89 |
"key", |
| 90 |
"null", |
| 91 |
"enumMember", |
| 92 |
"struct", |
| 93 |
"event", |
| 94 |
"operator", |
| 95 |
"typeParameter", |
| 96 |
"typeAlias", |
| 97 |
"parameter", |
| 98 |
"staticMethod", |
| 99 |
"macro", |
| 100 |
], |
| 101 |
"tokenModifiers": [ |
| 102 |
"declaration", |
| 103 |
"definition", |
| 104 |
"static", |
| 105 |
"read", |
| 106 |
"write", |
| 107 |
"classScope", |
| 108 |
"functionScope", |
| 109 |
"namespaceScope", |
| 110 |
"id0", |
| 111 |
"id1", |
| 112 |
"id2", |
| 113 |
"id3", |
| 114 |
"id4", |
| 115 |
"id5", |
| 116 |
"id6", |
| 117 |
"id7", |
| 118 |
"id8", |
| 119 |
"id9", |
| 120 |
"id10", |
| 121 |
"id11", |
| 122 |
"id12", |
| 123 |
"id13", |
| 124 |
"id14", |
| 125 |
"id15", |
| 126 |
"id16", |
| 127 |
"id17", |
| 128 |
"id18", |
| 129 |
"id19", |
| 130 |
], |
| 131 |
}, |
| 132 |
"range": True, |
| 133 |
"full": True, |
| 134 |
}, |
| 135 |
"workspace": { |
| 136 |
"workspaceFolders": { |
| 137 |
"supported": True, |
| 138 |
"changeNotifications": True, |
| 139 |
} |
| 140 |
}, |
| 141 |
}, |
| 142 |
"serverInfo": {"name": "ccls", "version": version}, |
| 143 |
"offsetEncoding": "utf-32", |
| 144 |
}, |
| 145 |
}, |
| 146 |
), |
| 147 |
( |
| 148 |
{"jsonrpc": "2.0", "id": 1, "method": "$ccls/info"}, |
| 149 |
{ |
| 150 |
"jsonrpc": "2.0", |
| 151 |
"id": 1, |
| 152 |
"result": { |
| 153 |
"db": {"files": 1, "funcs": 0, "types": 1, "vars": 1}, |
| 154 |
"pipeline": {"lastIdle": 0, "completed": 1, "enqueued": 1, "opened": 0}, |
| 155 |
"project": {"entries": 1}, |
| 156 |
}, |
| 157 |
}, |
| 158 |
), |
| 159 |
( |
| 160 |
{ |
| 161 |
"jsonrpc": "2.0", |
| 162 |
"id": 1, |
| 163 |
"method": "workspace/symbol", |
| 164 |
"params": {"query": "test"}, |
| 165 |
}, |
| 166 |
{ |
| 167 |
"id": 1, |
| 168 |
"jsonrpc": "2.0", |
| 169 |
"result": [ |
| 170 |
{ |
| 171 |
"kind": 13, |
| 172 |
"location": { |
| 173 |
"range": { |
| 174 |
"end": {"character": 12, "line": 0}, |
| 175 |
"start": {"character": 4, "line": 0}, |
| 176 |
}, |
| 177 |
"uri": "file://" + path + "/sample.c", |
| 178 |
}, |
| 179 |
"name": "int test_var", |
| 180 |
} |
| 181 |
], |
| 182 |
}, |
| 183 |
), |
| 184 |
] |
| 185 |
|
| 186 |
|
| 187 |
def encode(msg): |
| 188 |
msg_str = json.dumps(msg) |
| 189 |
rpc_str = "Content-Length: %d\r\n\r\n%s" % (len(msg_str), msg_str) |
| 190 |
return rpc_str.encode() |
| 191 |
|
| 192 |
|
| 193 |
def read(f): |
| 194 |
size = int(re.findall(r"Content-Length: (\d+)", f.readline().decode())[0]) |
| 195 |
f.readline() |
| 196 |
return json.loads(f.read(size).decode()) |
| 197 |
|
| 198 |
|
| 199 |
class TestLSP(unittest.TestCase): |
| 200 |
maxDiff = None |
| 201 |
timeout = 100 |
| 202 |
|
| 203 |
def tearDown(self): |
| 204 |
shutil.rmtree(path + "/.ccls-cache", ignore_errors=True) |
| 205 |
|
| 206 |
def test_index(self): |
| 207 |
subprocess.run( |
| 208 |
[ccls, "-index=.", "-v=-1"], stderr=subprocess.STDOUT |
| 209 |
).check_returncode() |
| 210 |
|
| 211 |
def test_response(self): |
| 212 |
with subprocess.Popen( |
| 213 |
[ccls, "-v=-1"], stdin=subprocess.PIPE, stdout=subprocess.PIPE |
| 214 |
) as proc: |
| 215 |
|
| 216 |
def one(case): |
| 217 |
for i in range(self.timeout): |
| 218 |
proc.stdin.write(encode(case[0])) |
| 219 |
proc.stdin.flush() |
| 220 |
got = read(proc.stdout) |
| 221 |
try: |
| 222 |
self.assertEqual(case[1], got) |
| 223 |
break |
| 224 |
except: |
| 225 |
if i < self.timeout - 1: |
| 226 |
time.sleep(0.1) |
| 227 |
else: |
| 228 |
raise |
| 229 |
|
| 230 |
print() |
| 231 |
for tt in tests: |
| 232 |
msg = tt[0]["method"] |
| 233 |
with self.subTest(msg): |
| 234 |
print("Running", msg) |
| 235 |
one(tt) |
| 236 |
proc.stdin.close() |
| 237 |
|
| 238 |
|
| 239 |
if __name__ == "__main__": |
| 240 |
unittest.main(verbosity=2) |