File: lsp-test

package info (click to toggle)
ccls 0.20220729-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,340 kB
  • sloc: cpp: 13,395; python: 153; objc: 15; makefile: 15; ansic: 1
file content (170 lines) | stat: -rwxr-xr-x 5,389 bytes parent folder | download
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
#!/usr/bin/python3

import json
import os
import pprint
import re
import shutil
import subprocess
import time
import unittest

ccls = os.getenv("CCLS", "ccls")
path = os.path.dirname(os.path.realpath(__file__))
version = re.findall(
    r"ccls version ([\S]+)",
    subprocess.check_output([ccls, "--version"]).decode().strip(),
)[0]

tests = [
    (
        {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "initialize",
            "params": {"rootUri": "file://" + path},
        },
        {
            "jsonrpc": "2.0",
            "id": 1,
            "result": {
                "capabilities": {
                    "textDocumentSync": {
                        "openClose": True,
                        "change": 2,
                        "willSave": False,
                        "willSaveWaitUntil": False,
                        "save": {"includeText": False},
                    },
                    "hoverProvider": True,
                    "completionProvider": {
                        "resolveProvider": False,
                        "triggerCharacters": [".", ":", ">", "#", "<", '"', "/"],
                    },
                    "signatureHelpProvider": {"triggerCharacters": ["(", ","]},
                    "declarationProvider": True,
                    "definitionProvider": True,
                    "implementationProvider": True,
                    "typeDefinitionProvider": True,
                    "referencesProvider": True,
                    "documentHighlightProvider": True,
                    "documentSymbolProvider": True,
                    "workspaceSymbolProvider": True,
                    "codeActionProvider": {"codeActionKinds": ["quickfix"]},
                    "codeLensProvider": {"resolveProvider": False},
                    "documentFormattingProvider": True,
                    "documentRangeFormattingProvider": True,
                    "documentOnTypeFormattingProvider": {
                        "firstTriggerCharacter": "}",
                        "moreTriggerCharacter": [],
                    },
                    "renameProvider": True,
                    "documentLinkProvider": {"resolveProvider": True},
                    "foldingRangeProvider": True,
                    "executeCommandProvider": {"commands": ["ccls.xref"]},
                    "workspace": {
                        "workspaceFolders": {
                            "supported": True,
                            "changeNotifications": True,
                        }
                    },
                },
                "offsetEncoding": "utf-32",
                "serverInfo": {"name": "ccls", "version": version},
            },
        },
    ),
    (
        {"jsonrpc": "2.0", "id": 1, "method": "$ccls/info"},
        {
            "jsonrpc": "2.0",
            "id": 1,
            "result": {
                "db": {"files": 1, "funcs": 0, "types": 1, "vars": 1},
                "pipeline": {"lastIdle": 0, "completed": 1, "enqueued": 1},
                "project": {"entries": 1},
            },
        },
    ),
    (
        {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "workspace/symbol",
            "params": {"query": "test"},
        },
        {
            "id": 1,
            "jsonrpc": "2.0",
            "result": [
                {
                    "kind": 13,
                    "location": {
                        "range": {
                            "end": {"character": 12, "line": 0},
                            "start": {"character": 4, "line": 0},
                        },
                        "uri": "file://" + path + "/sample.c",
                    },
                    "name": "int test_var",
                }
            ],
        },
    ),
]


def encode(msg):
    msg_str = json.dumps(msg)
    rpc_str = "Content-Length: %d\r\n\r\n%s" % (len(msg_str), msg_str)
    return rpc_str.encode()


def read(f):
    size = int(re.findall(r"Content-Length: (\d+)", f.readline().decode())[0])
    f.readline()
    return json.loads(f.read(size).decode())


class TestLSP(unittest.TestCase):
    maxDiff = None
    timeout = 100

    def tearDown(self):
        shutil.rmtree(path + "/.ccls-cache", ignore_errors=True)

    def test_index(self):
        subprocess.run(
            [ccls, "-index=.", "-v=-1"], stderr=subprocess.STDOUT
        ).check_returncode()

    def test_response(self):
        with subprocess.Popen(
            [ccls, "-v=-1"], stdin=subprocess.PIPE, stdout=subprocess.PIPE
        ) as proc:

            def one(case):
                for i in range(self.timeout):
                    proc.stdin.write(encode(case[0]))
                    proc.stdin.flush()
                    got = read(proc.stdout)
                    try:
                        self.assertEqual(case[1], got)
                        break
                    except:
                        if i < self.timeout - 1:
                            time.sleep(0.1)
                        else:
                            raise

            print()
            for tt in tests:
                msg = tt[0]["method"]
                with self.subTest(msg):
                    print("Running", msg)
                    one(tt)
            proc.stdin.close()


if __name__ == "__main__":
    unittest.main(verbosity=2)