]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/test/test_rename.vader

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Merge commit 'd49e95aa7ba744f0a7f544aca43afdb6aab41f24' as '.vim/bundle/asyncomplete...
[etc/vim.git] / .vim / bundle / ale / test / test_rename.vader
1 Before:
2   call ale#test#SetDirectory('/testplugin/test')
3   call ale#test#SetFilename('dummy.txt')
4
5   let g:old_filename = expand('%:p')
6   let g:Callback = ''
7   let g:expr_list = []
8   let g:message_list = []
9   let g:handle_code_action_called = 0
10   let g:code_actions = []
11   let g:options = {}
12   let g:capability_checked = ''
13   let g:conn_id = v:null
14   let g:InitCallback = v:null
15
16   runtime autoload/ale/lsp_linter.vim
17   runtime autoload/ale/lsp.vim
18   runtime autoload/ale/util.vim
19   runtime autoload/ale/rename.vim
20   runtime autoload/ale/code_action.vim
21
22   function! ale#lsp_linter#StartLSP(buffer, linter, Callback) abort
23     let g:conn_id = ale#lsp#Register('executable', '/foo/bar', '', {})
24     call ale#lsp#MarkDocumentAsOpen(g:conn_id, a:buffer)
25
26     if a:linter.lsp is# 'tsserver'
27         call ale#lsp#MarkConnectionAsTsserver(g:conn_id)
28     endif
29
30     let l:details = {
31     \ 'command': 'foobar',
32     \ 'buffer': a:buffer,
33     \ 'connection_id': g:conn_id,
34     \ 'project_root': '/foo/bar',
35     \}
36
37     let g:InitCallback = {-> ale#lsp_linter#OnInit(a:linter, l:details, a:Callback)}
38   endfunction
39
40   function! ale#lsp#HasCapability(conn_id, capability) abort
41     let g:capability_checked = a:capability
42
43     return 1
44   endfunction
45
46   function! ale#lsp#RegisterCallback(conn_id, callback) abort
47     let g:Callback = a:callback
48   endfunction
49
50   function! ale#lsp#Send(conn_id, message) abort
51     call add(g:message_list, a:message)
52
53     return 42
54   endfunction
55
56   function! ale#util#Execute(expr) abort
57     call add(g:expr_list, a:expr)
58   endfunction
59
60   function! ale#code_action#HandleCodeAction(code_action, options) abort
61     let g:handle_code_action_called = 1
62     AssertEqual g:ale_save_hidden || !&hidden, get(a:options, 'should_save', 0)
63     call add(g:code_actions, a:code_action)
64   endfunction
65
66   function! ale#util#Input(message, value) abort
67     return 'a-new-name'
68   endfunction
69
70   call ale#rename#SetMap({
71   \  3: {
72   \    'old_name': 'oldName',
73   \    'new_name': 'aNewName',
74   \  },
75   \})
76
77 After:
78   if g:conn_id isnot v:null
79     call ale#lsp#RemoveConnectionWithID(g:conn_id)
80   endif
81
82   call ale#rename#SetMap({})
83   call ale#test#RestoreDirectory()
84   call ale#linter#Reset()
85
86   unlet! g:capability_checked
87   unlet! g:InitCallback
88   unlet! g:old_filename
89   unlet! g:conn_id
90   unlet! g:Callback
91   unlet! g:message_list
92   unlet! g:expr_list
93   unlet! b:ale_linters
94   unlet! g:options
95   unlet! g:code_actions
96   unlet! g:handle_code_action_called
97
98   runtime autoload/ale/lsp_linter.vim
99   runtime autoload/ale/lsp.vim
100   runtime autoload/ale/util.vim
101   runtime autoload/ale/rename.vim
102   runtime autoload/ale/code_action.vim
103
104 Execute(Other messages for the tsserver handler should be ignored):
105   call ale#rename#HandleTSServerResponse(1, {'command': 'foo'})
106   AssertEqual g:handle_code_action_called, 0
107
108 Execute(Failed rename responses should be handled correctly):
109   call ale#rename#SetMap({3: {'old_name': 'oldName', 'new_name': 'a-test'}})
110   call ale#rename#HandleTSServerResponse(
111   \ 1,
112   \ {'command': 'rename', 'request_seq': 3}
113   \)
114   AssertEqual g:handle_code_action_called, 0
115
116 Given typescript(Some typescript file):
117   foo
118   somelongerline
119   bazxyzxyzxyz
120
121 Execute(Code actions from tsserver should be handled):
122   call ale#rename#HandleTSServerResponse(1, {
123   \ 'command': 'rename',
124   \ 'request_seq': 3,
125   \ 'success': v:true,
126   \ 'body': {
127   \   'locs': [
128   \     {
129   \       'file': '/foo/bar/file1.ts',
130   \       'locs': [
131   \          {
132   \            'start': {
133   \              'line': 1,
134   \              'offset': 2,
135   \            },
136   \            'end': {
137   \              'line': 3,
138   \              'offset': 4,
139   \            },
140   \          },
141   \       ],
142   \     },
143   \     {
144   \       'file': '/foo/bar/file2.ts',
145   \       'locs': [
146   \         {
147   \           'start': {
148   \             'line': 10,
149   \             'offset': 20,
150   \           },
151   \            'end': {
152   \              'line': 30,
153   \              'offset': 40,
154   \            },
155   \         },
156   \       ],
157   \     },
158   \   ]
159   \ },
160   \})
161
162   AssertEqual
163   \ [
164   \   {
165   \     'description': 'rename',
166   \     'changes': [
167   \       {
168   \         'fileName': '/foo/bar/file1.ts',
169   \         'textChanges': [
170   \            {
171   \              'start': {
172   \                'line': 1,
173   \                'offset': 2,
174   \              },
175   \              'end': {
176   \                'line': 3,
177   \                'offset': 4,
178   \              },
179   \              'newText': 'aNewName',
180   \            },
181   \         ],
182   \       },
183   \       {
184   \         'fileName': '/foo/bar/file2.ts',
185   \         'textChanges': [
186   \           {
187   \             'start': {
188   \               'line': 10,
189   \               'offset': 20,
190   \             },
191   \              'end': {
192   \                'line': 30,
193   \                'offset': 40,
194   \              },
195   \              'newText': 'aNewName',
196   \           },
197   \         ],
198   \       },
199   \     ],
200   \   }
201   \ ],
202   \ g:code_actions
203
204 Execute(Prints a tsserver error message when unsuccessful):
205   call ale#rename#HandleTSServerResponse(1, {
206   \ 'command': 'rename',
207   \ 'request_seq': 3,
208   \ 'success': v:false,
209   \ 'message': 'This symbol cannot be renamed',
210   \})
211
212   AssertEqual g:handle_code_action_called, 0
213   AssertEqual ['echom ''Error renaming "oldName" to: "aNewName". ' .
214   \ 'Reason: This symbol cannot be renamed'''], g:expr_list
215
216 Execute(HandleTSServerResponse does nothing when no changes):
217   call ale#rename#HandleTSServerResponse(1, {
218   \ 'command': 'rename',
219   \ 'request_seq': 3,
220   \ 'success': v:true,
221   \ 'body': {
222   \   'locs': []
223   \ }
224   \})
225
226   AssertEqual g:handle_code_action_called, 0
227   AssertEqual ['echom ''Error renaming "oldName" to: "aNewName"'''], g:expr_list
228
229 Execute(tsserver rename requests should be sent):
230   call ale#rename#SetMap({})
231   call ale#linter#Reset()
232
233   runtime ale_linters/typescript/tsserver.vim
234   call setpos('.', [bufnr(''), 2, 5, 0])
235
236   ALERename
237
238   " We shouldn't register the callback yet.
239   AssertEqual '''''', string(g:Callback)
240
241   AssertEqual type(function('type')), type(g:InitCallback)
242   call g:InitCallback()
243
244   AssertEqual 'rename', g:capability_checked
245   AssertEqual
246   \ 'function(''ale#rename#HandleTSServerResponse'')',
247   \ string(g:Callback)
248   AssertEqual
249   \ [
250   \   ale#lsp#tsserver_message#Change(bufnr('')),
251   \   [0, 'ts@rename', {
252   \     'file': expand('%:p'),
253   \     'line': 2,
254   \     'offset': 5,
255   \     'arguments': {
256   \         'findInComments': g:ale_rename_tsserver_find_in_comments,
257   \         'findInStrings': g:ale_rename_tsserver_find_in_strings,
258   \     },
259   \   }]
260   \ ],
261   \ g:message_list
262   AssertEqual {'42': {'old_name': 'somelongerline', 'new_name': 'a-new-name'}},
263   \ ale#rename#GetMap()
264
265 Given python(Some Python file):
266   foo
267   somelongerline
268   bazxyzxyzxyz
269
270 Execute(Code actions from LSP should be handled):
271   call ale#rename#HandleLSPResponse(1, {
272   \ 'id': 3,
273   \ 'result': {
274   \   'changes': {
275   \     'file:///foo/bar/file1.ts': [
276   \       {
277   \         'range': {
278   \           'start': {
279   \             'line': 1,
280   \             'character': 2,
281   \           },
282   \           'end': {
283   \             'line': 3,
284   \             'character': 4,
285   \           },
286   \         },
287   \         'newText': 'bla123'
288   \       },
289   \     ],
290   \   },
291   \ },
292   \})
293
294   AssertEqual
295   \ [
296   \   {
297   \     'description': 'rename',
298   \     'changes': [
299   \       {
300   \         'fileName': '/foo/bar/file1.ts',
301   \         'textChanges': [
302   \            {
303   \              'start': {
304   \                'line': 2,
305   \                'offset': 3,
306   \              },
307   \              'end': {
308   \                'line': 4,
309   \                'offset': 5,
310   \              },
311   \              'newText': 'bla123',
312   \            },
313   \         ],
314   \       },
315   \     ],
316   \   }
317   \ ],
318   \ g:code_actions
319
320 Execute(DocumentChanges from LSP should be handled):
321   call ale#rename#HandleLSPResponse(1, {
322   \ 'id': 3,
323   \ 'result': {
324   \   'documentChanges': [
325   \     {
326   \       'textDocument': {
327   \         'version': 1.0,
328   \         'uri': 'file:///foo/bar/file1.ts',
329   \       },
330   \       'edits': [
331   \         {
332   \           'range': {
333   \             'start': {
334   \               'line': 1,
335   \               'character': 2,
336   \             },
337   \             'end': {
338   \               'line': 3,
339   \               'character': 4,
340   \             },
341   \           },
342   \           'newText': 'bla123',
343   \         },
344   \       ],
345   \     },
346   \   ],
347   \ },
348   \})
349
350   AssertEqual
351   \ [
352   \   {
353   \     'description': 'rename',
354   \     'changes': [
355   \       {
356   \         'fileName': '/foo/bar/file1.ts',
357   \         'textChanges': [
358   \            {
359   \              'start': {
360   \                'line': 2,
361   \                'offset': 3,
362   \              },
363   \              'end': {
364   \                'line': 4,
365   \                'offset': 5,
366   \              },
367   \              'newText': 'bla123',
368   \            },
369   \         ],
370   \       },
371   \     ],
372   \   }
373   \ ],
374   \ g:code_actions
375
376 Execute(Single DocumentChange from LSP should be handled):
377   call ale#rename#HandleLSPResponse(1, {
378   \ 'id': 3,
379   \ 'result': {
380   \   'documentChanges': {
381   \     'textDocument': {
382   \       'version': 1.0,
383   \       'uri': 'file:///foo/bar/file1.ts',
384   \     },
385   \     'edits': [
386   \       {
387   \         'range': {
388   \           'start': {
389   \             'line': 1,
390   \             'character': 2,
391   \           },
392   \           'end': {
393   \             'line': 3,
394   \             'character': 4,
395   \           },
396   \         },
397   \         'newText': 'bla123',
398   \       },
399   \     ],
400   \   },
401   \ },
402   \})
403
404   AssertEqual
405   \ [
406   \   {
407   \     'description': 'rename',
408   \     'changes': [
409   \       {
410   \         'fileName': '/foo/bar/file1.ts',
411   \         'textChanges': [
412   \            {
413   \              'start': {
414   \                'line': 2,
415   \                'offset': 3,
416   \              },
417   \              'end': {
418   \                'line': 4,
419   \                'offset': 5,
420   \              },
421   \              'newText': 'bla123',
422   \            },
423   \         ],
424   \       },
425   \     ],
426   \   }
427   \ ],
428   \ g:code_actions
429 Execute(LSP should perform no action when no result):
430   call ale#rename#HandleLSPResponse(1, {
431   \ 'id': 3,
432   \})
433
434   AssertEqual g:handle_code_action_called, 0
435   AssertEqual ['echom ''No rename result received from server'''], g:expr_list
436
437 Execute(LSP should perform no action when no changes):
438   call ale#rename#HandleLSPResponse(1, {
439   \ 'id': 3,
440   \ 'result': {},
441   \})
442
443   AssertEqual g:handle_code_action_called, 0
444   AssertEqual ['echom ''No changes received from server'''], g:expr_list
445
446 Execute(LSP should perform no action when changes is empty):
447   call ale#rename#HandleLSPResponse(1, {
448   \ 'id': 3,
449   \ 'result': {
450   \   'changes': [],
451   \ },
452   \})
453
454   AssertEqual g:handle_code_action_called, 0
455   AssertEqual ['echom ''No changes received from server'''], g:expr_list
456
457 Execute(LSP rename requests should be sent):
458   call ale#rename#SetMap({})
459   runtime ale_linters/python/pylsp.vim
460   let b:ale_linters = ['pylsp']
461   call setpos('.', [bufnr(''), 1, 5, 0])
462
463   ALERename
464
465   " We shouldn't register the callback yet.
466   AssertEqual '''''', string(g:Callback)
467
468   AssertEqual type(function('type')), type(g:InitCallback)
469   call g:InitCallback()
470
471   AssertEqual 'rename', g:capability_checked
472   AssertEqual
473   \ 'function(''ale#rename#HandleLSPResponse'')',
474   \ string(g:Callback)
475
476   AssertEqual
477   \ [
478   \   [1, 'textDocument/didChange', {
479   \     'textDocument': {
480   \         'uri': ale#path#ToFileURI(expand('%:p')),
481   \         'version': g:ale_lsp_next_version_id - 1,
482   \     },
483   \     'contentChanges': [{'text': join(getline(1, '$'), "\n") . "\n"}]
484   \   }],
485   \   [0, 'textDocument/rename', {
486   \   'textDocument': {'uri': ale#path#ToFileURI(expand('%:p'))},
487   \   'position': {'line': 0, 'character': 2},
488   \   'newName': 'a-new-name',
489   \   }],
490   \ ],
491   \ g:message_list
492
493   AssertEqual {'42': {'old_name': 'foo', 'new_name': 'a-new-name'}},
494   \ ale#rename#GetMap()