summaryrefslogtreecommitdiff
path: root/test/lsp/test_lsp_connections.vader
blob: 8651d8011f26b2fb352517736909ae7e44fb425c (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
Before:
  let g:ale_lsp_next_message_id = 1

After:
  unlet! b:data
  unlet! b:conn

Execute(GetNextMessageID() should increment appropriately):
  " We should get the initial ID, and increment a bit.
  AssertEqual 1, ale#lsp#GetNextMessageID()
  AssertEqual 2, ale#lsp#GetNextMessageID()
  AssertEqual 3, ale#lsp#GetNextMessageID()

  " Set the maximum ID.
  let g:ale_lsp_next_message_id = 9223372036854775807

  " When we hit the maximum ID, the next ID afterwards should be 1.
  AssertEqual 9223372036854775807, ale#lsp#GetNextMessageID()
  AssertEqual 1, ale#lsp#GetNextMessageID()

Execute(ale#lsp#CreateMessageData() should create an appropriate message):
    " NeoVim outputs JSON with spaces, so the output is a little different.
    if has('nvim')
      " 79 is the size in bytes for UTF-8, not the number of characters.
      AssertEqual
      \ [
      \   1,
      \   "Content-Length: 79\r\n\r\n"
      \   . '{"method": "someMethod", "jsonrpc": "2.0", "id": 1, "params": {"foo": "barÜ"}}',
      \ ],
      \ ale#lsp#CreateMessageData([0, 'someMethod', {'foo': 'barÜ'}])
      " Check again to ensure that we use the next ID.
      AssertEqual
      \ [
      \   2,
      \   "Content-Length: 79\r\n\r\n"
      \   . '{"method": "someMethod", "jsonrpc": "2.0", "id": 2, "params": {"foo": "barÜ"}}',
      \ ],
      \ ale#lsp#CreateMessageData([0, 'someMethod', {'foo': 'barÜ'}])
    else
      AssertEqual
      \ [
      \   1,
      \   "Content-Length: 71\r\n\r\n"
      \   . '{"method":"someMethod","jsonrpc":"2.0","id":1,"params":{"foo":"barÜ"}}',
      \ ],
      \ ale#lsp#CreateMessageData([0, 'someMethod', {'foo': 'barÜ'}])
      AssertEqual
      \ [
      \   2,
      \   "Content-Length: 71\r\n\r\n"
      \   . '{"method":"someMethod","jsonrpc":"2.0","id":2,"params":{"foo":"barÜ"}}',
      \ ],
      \ ale#lsp#CreateMessageData([0, 'someMethod', {'foo': 'barÜ'}])
    endif

Execute(ale#lsp#CreateMessageData() should create messages without params):
    if has('nvim')
      AssertEqual
      \ [
      \   1,
      \   "Content-Length: 56\r\n\r\n"
      \   . '{"method": "someOtherMethod", "jsonrpc": "2.0", "id": 1}',
      \ ],
      \ ale#lsp#CreateMessageData([0, 'someOtherMethod'])
    else
      AssertEqual
      \ [
      \   1,
      \   "Content-Length: 51\r\n\r\n"
      \   . '{"method":"someOtherMethod","jsonrpc":"2.0","id":1}',
      \ ],
      \ ale#lsp#CreateMessageData([0, 'someOtherMethod'])
    endif

Execute(ale#lsp#CreateMessageData() should create notifications):
    if has('nvim')
      AssertEqual
      \ [
      \   0,
      \   "Content-Length: 48\r\n\r\n"
      \   . '{"method": "someNotification", "jsonrpc": "2.0"}',
      \ ],
      \ ale#lsp#CreateMessageData([1, 'someNotification'])
      AssertEqual
      \ [
      \   0,
      \   "Content-Length: 74\r\n\r\n"
      \   . '{"method": "someNotification", "jsonrpc": "2.0", "params": {"foo": "bar"}}',
      \ ],
      \ ale#lsp#CreateMessageData([1, 'someNotification', {'foo': 'bar'}])
    else
      AssertEqual
      \ [
      \   0,
      \   "Content-Length: 45\r\n\r\n"
      \   . '{"method":"someNotification","jsonrpc":"2.0"}',
      \ ],
      \ ale#lsp#CreateMessageData([1, 'someNotification'])
      AssertEqual
      \ [
      \   0,
      \   "Content-Length: 68\r\n\r\n"
      \   . '{"method":"someNotification","jsonrpc":"2.0","params":{"foo":"bar"}}',
      \ ],
      \ ale#lsp#CreateMessageData([1, 'someNotification', {'foo': 'bar'}])
    endif

Execute(ale#lsp#CreateMessageData() should create tsserver notification messages):
    if has('nvim')
      AssertEqual
      \ [
      \   0,
      \   '{"seq": null, "type": "request", "command": "someNotification"}'
      \   . "\n",
      \ ],
      \ ale#lsp#CreateMessageData([1, 'ts@someNotification'])
      AssertEqual
      \ [
      \   0,
      \   '{"seq": null, "arguments": {"foo": "bar"}, "type": "request", "command": "someNotification"}'
      \   . "\n",
      \ ],
      \ ale#lsp#CreateMessageData([1, 'ts@someNotification', {'foo': 'bar'}])
    else
      AssertEqual
      \ [
      \   0,
      \   '{"seq":null,"type":"request","command":"someNotification"}'
      \   . "\n",
      \ ],
      \ ale#lsp#CreateMessageData([1, 'ts@someNotification'])
      AssertEqual
      \ [
      \   0,
      \   '{"seq":null,"arguments":{"foo":"bar"},"type":"request","command":"someNotification"}'
      \   . "\n",
      \ ],
      \ ale#lsp#CreateMessageData([1, 'ts@someNotification', {'foo': 'bar'}])
    endif

Execute(ale#lsp#CreateMessageData() should create tsserver messages expecting responses):
    if has('nvim')
      AssertEqual
      \ [
      \   1,
      \   '{"seq": 1, "type": "request", "command": "someMessage"}'
      \   . "\n",
      \ ],
      \ ale#lsp#CreateMessageData([0, 'ts@someMessage'])
      AssertEqual
      \ [
      \   2,
      \   '{"seq": 2, "arguments": {"foo": "bar"}, "type": "request", "command": "someMessage"}'
      \   . "\n",
      \ ],
      \ ale#lsp#CreateMessageData([0, 'ts@someMessage', {'foo': 'bar'}])
    else
      AssertEqual
      \ [
      \   1,
      \   '{"seq":1,"type":"request","command":"someMessage"}'
      \   . "\n",
      \ ],
      \ ale#lsp#CreateMessageData([0, 'ts@someMessage'])
      AssertEqual
      \ [
      \   2,
      \   '{"seq":2,"arguments":{"foo":"bar"},"type":"request","command":"someMessage"}'
      \   . "\n",
      \ ],
      \ ale#lsp#CreateMessageData([0, 'ts@someMessage', {'foo': 'bar'}])
    endif

Execute(ale#lsp#ReadMessageData() should read single whole messages):
  AssertEqual
  \  ['', [{'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}}]],
  \ ale#lsp#ReadMessageData(
  \   "Content-Length: 49\r\n\r\n"
  \   . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}'
  \ )

Execute(ale#lsp#ReadMessageData() should ignore other headers):
  AssertEqual
  \  ['', [{'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}}]],
  \ ale#lsp#ReadMessageData(
  \   "First-Header: 49\r\n"
  \   . "Content-Length: 49\r\n"
  \   . "Other-Header: 49\r\n"
  \   . "\r\n"
  \   . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}'
  \ )

Execute(ale#lsp#ReadMessageData() should handle partial messages):
  let b:data = "Content-Length: 49\r\n\r\n" . '{"id":2,"jsonrpc":"2.0","result":'

  AssertEqual [b:data, []], ale#lsp#ReadMessageData(b:data)

Execute(ale#lsp#ReadMessageData() should handle multiple messages):
  AssertEqual
  \  ['', [
  \   {'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}},
  \   {'id': 2, 'jsonrpc': '2.0', 'result': {'foo123': 'barÜ'}},
  \ ]],
  \ ale#lsp#ReadMessageData(
  \   "Content-Length: 49\r\n\r\n"
  \   . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}'
  \   . "Content-Length: 52\r\n\r\n"
  \   . '{"id":2,"jsonrpc":"2.0","result":{"foo123":"barÜ"}}'
  \ )

Execute(ale#lsp#ReadMessageData() should handle a message with part of a second message):
  let b:data = "Content-Length: 52\r\n\r\n" . '{"id":2,"jsonrpc":"2.'

  AssertEqual
  \  [b:data, [
  \   {'id': 2, 'jsonrpc': '2.0', 'result': {'foo': 'barÜ'}},
  \ ]],
  \ ale#lsp#ReadMessageData(
  \   "Content-Length: 49\r\n\r\n"
  \   . '{"id":2,"jsonrpc":"2.0","result":{"foo":"barÜ"}}'
  \   . b:data
  \ )

Execute(Projects with regular project roots should be registered correctly):
  let b:conn = {'projects': {}}

  call ale#lsp#RegisterProject(b:conn, '/foo/bar')

  AssertEqual
  \ {
  \   'projects': {
  \     '/foo/bar': {'initialized': 0, 'message_queue': [], 'init_request_id': 0},
  \   },
  \ },
  \ b:conn

Execute(Projects with regular project roots should be fetched correctly):
  let b:conn = {
  \ 'projects': {
  \   '/foo/bar': {'initialized': 0, 'message_queue': [], 'init_request_id': 0},
  \ },
  \}

  AssertEqual
  \ {'initialized': 0, 'message_queue': [], 'init_request_id': 0},
  \ ale#lsp#GetProject(b:conn, '/foo/bar')

Execute(Projects with empty project roots should be registered correctly):
  let b:conn = {'projects': {}}

  call ale#lsp#RegisterProject(b:conn, '')

  AssertEqual
  \ {
  \   'projects': {
  \     '<<EMPTY>>': {'initialized': 1, 'message_queue': [], 'init_request_id': 0},
  \   },
  \ },
  \ b:conn

Execute(Projects with empty project roots should be fetched correctly):
  let b:conn = {
  \ 'projects': {
  \   '<<EMPTY>>': {'initialized': 1, 'message_queue': [], 'init_request_id': 0},
  \ },
  \}

  AssertEqual
  \ {'initialized': 1, 'message_queue': [], 'init_request_id': 0},
  \ ale#lsp#GetProject(b:conn, '')