forked from emacs-lsp/lsp-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsp-methods-test.el
401 lines (371 loc) · 11.8 KB
/
lsp-methods-test.el
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
;;; lsp-methods-test.el --- Tests for lsp-methods -*- lexical-binding: t; -*-
;; Copyright (C) 2018 Ivan Yonchovski
;; Author: Ivan Yonchovski <yyoncho@gmail.com>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'json)
(require 'lsp-mode)
(defconst lsp-methods-test--changes "
[
{
\"newText\": \"extracted\",
\"range\": {
\"end\": {
\"character\": 6,
\"line\": 4
},
\"start\": {
\"character\": 6,
\"line\": 4
}
}
},
{
\"newText\": \"();\",
\"range\": {
\"end\": {
\"character\": 6,
\"line\": 4
},
\"start\": {
\"character\": 6,
\"line\": 4
}
}
},
{
\"newText\": \"\",
\"range\": {
\"end\": {
\"character\": 30,
\"line\": 4
},
\"start\": {
\"character\": 6,
\"line\": 4
}
}
},
{
\"newText\": \"\\n\\n\",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
},
{
\"newText\": \"private\",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
},
{
\"newText\": \" \",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
},
{
\"newText\": \"void\",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
},
{
\"newText\": \" \",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
},
{
\"newText\": \"extracted\",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
},
{
\"newText\": \"() {\\n\\t\",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
},
{
\"newText\": \"String s = \\\"SomeString\\\";\",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
},
{
\"newText\": \"\\n}\",
\"range\": {
\"end\": {
\"character\": 3,
\"line\": 5
},
\"start\": {
\"character\": 3,
\"line\": 5
}
}
}
]")
(ert-deftest lsp--test-overlapping-updates ()
(let* ((input "package test;
public class Temp {
public void name() {
String s = \"SomeString\";
}
}")
(expected "package test;
public class Temp {
public void name() {
extracted();
}
private void extracted() {
String s = \"SomeString\";
}
}")
(actual (with-temp-buffer
(insert input)
(lsp--apply-text-edits
(lsp--read-json lsp-methods-test--changes))
(buffer-string))))
(should (string= actual expected))))
(ert-deftest lsp-create-test ()
(let ((new-file-name (make-temp-file "new")))
(delete-file new-file-name)
(lsp--apply-workspace-edit
(lsp-make-workspace-edit
:document-changes?
`[,(lsp-make-create-file :uri (lsp--path-to-uri new-file-name)
:kind "create")]))
(should (f-exists? new-file-name)))
(let ((new-file-name (make-temp-file "should be overridden")))
(f-write-text "should be overridden" nil new-file-name)
(lsp--apply-workspace-edit
(lsp-make-workspace-edit
:document-changes?
`[,(lsp-make-create-file :uri (lsp--path-to-uri new-file-name)
:kind "create"
:options? (lsp-make-create-file-options :overwrite? t))]))
(should (equal (f-read-text new-file-name) "")))
(let ((new-file-name (make-temp-file "should not be overridden")))
(f-write-text "text" nil new-file-name)
(lsp--apply-workspace-edit
(lsp-make-workspace-edit
:document-changes?
`[,(lsp-make-create-file :uri (lsp--path-to-uri new-file-name)
:kind "create"
:options? (lsp-make-create-file-options :overwrite? nil))]))
(should (equal (f-read-text new-file-name) "text")))
;; nested file without parent dir
(let ((new-file-name (make-temp-file "new")))
(delete-file new-file-name)
(setq new-file-name (f-join new-file-name "nested"))
(lsp--apply-workspace-edit
(lsp-make-workspace-edit
:document-changes?
`[,(lsp-make-create-file :uri (lsp--path-to-uri new-file-name)
:kind "create")]))
(should (f-exists? new-file-name))))
(ert-deftest lsp-delete-test ()
(let ((delete-file-name (make-temp-file "to-delete")))
(lsp--apply-workspace-edit
(lsp-make-workspace-edit
:document-changes?
`[,(lsp-make-delete-file :uri (lsp--path-to-uri delete-file-name)
:kind "delete")]))
(should-not (f-exists? delete-file-name))))
(ert-deftest lsp-update-test ()
(let ((old-file-name (make-temp-file "old"))
(new-file-name (make-temp-file "new")))
(f-delete new-file-name)
(lsp--apply-workspace-edit
(lsp-make-workspace-edit
:document-changes?
`[,(lsp-make-rename-file :old-uri (lsp--path-to-uri old-file-name)
:new-uri (lsp--path-to-uri new-file-name)
:kind "rename")]))
(should-not (f-exists? old-file-name))
(should (f-exists? new-file-name)))
;; override
(let ((old-file-name (make-temp-file "old"))
(new-file-name (make-temp-file "new")))
(f-write-text "should be overridden" nil new-file-name)
(lsp--apply-workspace-edit
(lsp-make-workspace-edit
:document-changes?
`[,(lsp-make-rename-file :old-uri (lsp--path-to-uri old-file-name)
:new-uri (lsp--path-to-uri new-file-name)
:kind "rename"
:options? (lsp-make-rename-file-options :overwrite? t))]))
(should-not (f-exists? old-file-name))
(should (f-exists? new-file-name)))
;; nested directory
(let* ((old-file-name (make-temp-file "old-"))
(new-file-name (make-temp-file "new-"))
(_ (delete-file new-file-name))
(new-file-name (f-join new-file-name "nested" "nested2")))
(lsp--apply-workspace-edit
(lsp-make-workspace-edit
:document-changes?
`[,(lsp-make-rename-file :old-uri (lsp--path-to-uri old-file-name)
:new-uri (lsp--path-to-uri new-file-name)
:kind "rename")]))
(should-not (f-exists? old-file-name))
(should (f-exists? new-file-name))))
;;; `lsp-rename'
(defmacro lsp-test--simulated-input (keys &rest body)
"Execute body, while simulating the pressing of KEYS.
KEYS is passed to `execute-kbd-macro', after being run trough
`kbd'. Returns the result of the last BODY form."
(declare (indent 1))
`(let (result)
;; This somehow fixes the test, which works without ert-runner just fine.
;; Perhaps `execute-kbd-macro' changes back to the first non-temporary
;; buffer first?
(save-current-buffer
(execute-kbd-macro (kbd ,keys) 1 (lambda () (setq result (progn ,@body)))))
result))
(defun lsp-test--rename-overlays? (pos)
"Return non-nil if there are `lsp-rename' overlays at POS.
POS is a point in the current buffer."
(--any? (equal (overlay-get it 'face) 'lsp-face-rename)
(overlays-at pos)))
(ert-deftest lsp--read-rename ()
"Ensure that `lsp--read-rename' works.
If AT-POINT is nil, it throws a `user-error'.
If a placeholder is given, it shall be the default value,
otherwise the bounds are to be used.
Rename overlays are removed afterwards, even if the user presses
C-g."
(should-error (lsp--read-rename nil) :type 'user-error)
(with-temp-buffer
(insert "identifier")
(should (string= "identifier"
(lsp-test--simulated-input "RET"
(lsp--read-rename '((1 . 11) . nil)))))
(should (string= "ident"
(lsp-test--simulated-input "RET"
(lsp--read-rename '((1 . 10) . "ident")))))
(goto-char 1)
(condition-case nil
(cl-letf (((symbol-function #'read-string)
(lambda (&rest _)
;; NOTE: BEGIN and END means a range [BEGIN;END[, so at
;; point 10, there shouldn't be an overlay anymore. This is
;; consistent with of `bounds-thing-at-point', and it
;; worked during manual testing.
(should (lsp-test--rename-overlays? 1))
(should (lsp-test--rename-overlays? 9))
(should-not (lsp-test--rename-overlays? 10))
(keyboard-quit))))
(lsp--read-rename '((1 . 10) . "id")))
(quit))
;; but not after `lsp--read-rename'
(should-not (lsp-test--rename-overlays? 1))
(should-not (lsp-test--rename-overlays? 9))
(should-not (lsp-test--rename-overlays? 10))))
(ert-deftest lsp--get-symbol-to-rename ()
"Test `lsp--get-symbol-to-rename'.
It should error if renaming cannot be done, make use of
prepareRename as much as possible, with or without bounds, and it
should work without the latter."
;; We don't support rename
(cl-letf (((symbol-function #'lsp-feature?) #'ignore))
(should-error (lsp--get-symbol-to-rename) :type 'error))
(cl-letf (((symbol-function #'lsp--text-document-position-params) #'ignore)
((symbol-function #'lsp--range-to-region) #'identity))
(with-temp-buffer
(insert "identifier")
(goto-char 1)
;; We do support rename, but no prepareRename
(cl-letf (((symbol-function #'lsp-feature?)
(lambda (f) (member f '("textDocument/rename")))))
(should (equal (cons (bounds-of-thing-at-point 'symbol) nil)
(lsp--get-symbol-to-rename)))
(goto-char (point-max))
(insert " ")
;; we are not on an identifier
(should (equal nil (lsp--get-symbol-to-rename))))
;; Do the following tests with an identifier at point
(goto-char 1)
(cl-letf (((symbol-function #'lsp-feature?)
(lambda (f) (member f '("textDocument/rename"
"textDocument/prepareRename")))))
(cl-letf (((symbol-function #'lsp-request)
(lambda (&rest _) (lsp-make-prepare-rename-result
:range '(1 . 12)
:placeholder nil))))
(should (equal '((1 . 12) . nil) (lsp--get-symbol-to-rename))))
(cl-letf (((symbol-function #'lsp-request)
(lambda (&rest _) (lsp-make-prepare-rename-result
:range '(1 . 12)
:placeholder "_"))))
(should (equal '((1 . 12) . "_") (lsp--get-symbol-to-rename))))))))
;;; lsp-methods-test.el ends here