]> git.madduck.net Git - etc/vim.git/blob - .vim/bundle/ale/test/completion/test_completion_events.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 'a39f715c13be3352193ffd9c5b7536b8786eff64' as '.vim/bundle/vim-lsp'
[etc/vim.git] / .vim / bundle / ale / test / completion / test_completion_events.vader
1 Before:
2   Save g:ale_completion_enabled
3   Save g:ale_completion_delay
4   Save g:ale_completion_max_suggestions
5   Save &l:omnifunc
6   Save &l:completeopt
7
8   unlet! b:ale_completion_enabled
9   let g:ale_completion_enabled = 1
10   let g:get_completions_called = 0
11   let g:feedkeys_calls = []
12   let g:fake_mode = 'i'
13
14   let b:ale_linters = {
15   \ 'typescript': ['tsserver'],
16   \}
17
18   let &l:completeopt = 'menu,menuone,preview,noselect,noinsert'
19
20   runtime autoload/ale/util.vim
21
22   function! ale#util#FeedKeys(string) abort
23     call add(g:feedkeys_calls, [a:string])
24   endfunction
25
26   " Pretend we're in insert mode for most tests.
27   function! ale#util#Mode(...) abort
28     return g:fake_mode
29   endfunction
30
31   function! CheckCompletionCalled(expect_success) abort
32     let g:get_completions_called = 0
33
34     " We just want to check if the function is called.
35     function! ale#completion#GetCompletions(source)
36       let g:get_completions_called = 1
37     endfunction
38
39     let g:ale_completion_delay = 0
40
41     " Run this check a few times, as it can fail randomly.
42     for l:i in range(has('nvim-0.3') || has('win32') ? 5 : 1)
43       call ale#completion#Queue()
44       sleep 1m
45
46       if g:get_completions_called is a:expect_success
47         break
48       endif
49     endfor
50
51     AssertEqual a:expect_success, g:get_completions_called
52   endfunction
53
54   let g:handle_code_action_called = 0
55   function! MockHandleCodeAction() abort
56     " delfunction! ale#code_action#HandleCodeAction
57     function! ale#code_action#HandleCodeAction(action, options) abort
58       Assert !get(a:options, 'should_save')
59       let g:handle_code_action_called += 1
60     endfunction
61   endfunction
62
63 After:
64   Restore
65
66   unlet! b:ale_completion_enabled
67   unlet! g:output
68   unlet! g:fake_mode
69   unlet! g:get_completions_called
70   unlet! g:handle_code_action_called
71   unlet! b:ale_old_omnifunc
72   unlet! b:ale_old_completeopt
73   unlet! b:ale_completion_info
74   unlet! b:ale_completion_result
75   unlet! b:ale_complete_done_time
76   unlet! b:ale_linters
77
78   delfunction CheckCompletionCalled
79   delfunction ale#code_action#HandleCodeAction
80   delfunction MockHandleCodeAction
81
82   if exists('*CompleteCallback')
83     delfunction CompleteCallback
84   endif
85
86   " Stop any timers we left behind.
87   " This stops the tests from failing randomly.
88   call ale#completion#StopTimer()
89
90   " Reset the function. The runtime command below should fix this, but doesn't
91   " seem to fix it.
92   function! ale#util#Mode(...) abort
93     return call('mode', a:000)
94   endfunction
95
96   runtime autoload/ale/completion.vim
97   runtime autoload/ale/code_action.vim
98   runtime autoload/ale/util.vim
99
100 Execute(ale#completion#GetCompletions should be called when the cursor position stays the same):
101   call CheckCompletionCalled(1)
102
103 Execute(ale#completion#GetCompletions should not be called if the global setting is disabled):
104   let g:ale_completion_enabled = 0
105   call CheckCompletionCalled(0)
106
107 Execute(ale#completion#GetCompletions should not be called if the buffer setting is disabled):
108   let b:ale_completion_enabled = 0
109   call CheckCompletionCalled(0)
110
111 Given typescript():
112   let abc = y.
113   let foo = ab
114   let foo = (ab)
115
116 Execute(ale#completion#GetCompletions should not be called when the cursor position changes):
117   call setpos('.', [bufnr(''), 1, 2, 0])
118
119   " We just want to check if the function is called.
120   function! ale#completion#GetCompletions(source)
121     let g:get_completions_called = 1
122   endfunction
123
124   let g:ale_completion_delay = 0
125   call ale#completion#Queue()
126
127   " Change the cursor position before the callback is triggered.
128   call setpos('.', [bufnr(''), 2, 2, 0])
129
130   sleep 1m
131
132   Assert !g:get_completions_called
133
134 Execute(ale#completion#GetCompletions should not be called if you switch to normal mode):
135   let &l:completeopt = 'menu,preview'
136   let g:fake_mode = 'n'
137
138   " We just want to check if the function is called.
139   function! ale#completion#GetCompletions(source)
140     let g:get_completions_called = 1
141   endfunction
142
143   let g:ale_completion_delay = 0
144   call ale#completion#Queue()
145
146   sleep 1m
147
148   Assert !g:get_completions_called
149
150 Execute(Completion should not be done shortly after the CompleteDone function):
151   call CheckCompletionCalled(1)
152   call ale#completion#Done()
153   call CheckCompletionCalled(0)
154
155 Execute(ale#completion#Show() should remember the omnifunc setting and replace it):
156   let &l:omnifunc = 'FooBar'
157
158   let b:ale_completion_info = {'source': 'ale-automatic'}
159   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
160
161   AssertEqual 'FooBar', b:ale_old_omnifunc
162   AssertEqual 'ale#completion#AutomaticOmniFunc', &l:omnifunc
163
164   AssertEqual [], g:feedkeys_calls
165   sleep 1ms
166   AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls
167
168 Execute(ale#completion#Show() should remember the completeopt setting and replace it):
169   let &l:completeopt = 'menu'
170
171   let b:ale_completion_info = {'source': 'ale-automatic'}
172   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
173
174   AssertEqual 'menu', b:ale_old_completeopt
175   AssertEqual 'menu,menuone,noinsert', &l:completeopt
176
177   AssertEqual [], g:feedkeys_calls
178   sleep 1ms
179   AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls
180
181 Execute(ale#completion#Show() should set the preview option if it's set):
182   let &l:completeopt = 'menu,preview'
183
184   let b:ale_completion_info = {'source': 'ale-automatic'}
185   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
186
187   AssertEqual 'menu,preview', b:ale_old_completeopt
188   AssertEqual 'menu,menuone,noinsert,preview', &l:completeopt
189
190   AssertEqual [], g:feedkeys_calls
191   sleep 1ms
192   AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls
193
194 Execute(ale#completion#Show() should not replace the completeopt setting for manual completion):
195   let b:ale_completion_info = {'source': 'ale-manual'}
196
197   let &l:completeopt = 'menu,preview'
198
199   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
200
201   Assert !exists('b:ale_old_completeopt')
202
203   AssertEqual [], g:feedkeys_calls
204   sleep 1ms
205   AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls
206
207 Execute(ale#completion#AutomaticOmniFunc() should also remember the completeopt setting and replace it):
208   let &l:completeopt = 'menu,noselect'
209
210   let b:ale_completion_info = {'source': 'ale-automatic'}
211   call ale#completion#AutomaticOmniFunc(0, '')
212
213   AssertEqual 'menu,noselect', b:ale_old_completeopt
214   AssertEqual 'menu,menuone,noinsert,noselect', &l:completeopt
215
216 Execute(ale#completion#AutomaticOmniFunc() should set the preview option if it's set):
217   let &l:completeopt = 'menu,preview'
218
219   let b:ale_completion_info = {'source': 'ale-automatic'}
220   call ale#completion#AutomaticOmniFunc(0, '')
221
222   AssertEqual 'menu,preview', b:ale_old_completeopt
223   AssertEqual 'menu,menuone,noinsert,preview', &l:completeopt
224
225 Execute(ale#completion#Show() should make the correct feedkeys() call for automatic completion):
226   let b:ale_completion_info = {'source': 'ale-automatic'}
227   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
228
229   AssertEqual [], g:feedkeys_calls
230   sleep 1ms
231   AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls
232
233 Execute(ale#completion#Show() should make the correct feedkeys() call for manual completion):
234   let b:ale_completion_info = {'source': 'ale-automatic'}
235   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
236
237   AssertEqual [], g:feedkeys_calls
238   sleep 1ms
239   AssertEqual [["\<Plug>(ale_show_completion_menu)"]], g:feedkeys_calls
240
241 Execute(ale#completion#Show() should not call feedkeys() for other sources):
242   let b:ale_completion_info = {'source': 'other-source'}
243   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
244
245   sleep 1ms
246   AssertEqual [], g:feedkeys_calls
247
248 Execute(ale#completion#Show() shouldn't do anything if you switch back to normal mode):
249   let &l:completeopt = 'menu,preview'
250   let g:fake_mode = 'n'
251
252   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
253
254   AssertEqual 'menu,preview', &l:completeopt
255   Assert !exists('b:ale_old_omnifunc')
256   Assert !exists('b:ale_old_completeopt')
257   Assert !exists('b:ale_completion_result')
258   AssertEqual [], g:feedkeys_calls
259
260 Execute(ale#completion#Show() should save the result it is given):
261   call ale#completion#Show([])
262
263   AssertEqual [], b:ale_completion_result
264
265   call ale#completion#Show([{'word': 'x', 'kind': 'v', 'icase': 1}])
266
267   AssertEqual [{'word': 'x', 'kind': 'v', 'icase': 1}], b:ale_completion_result
268
269 Execute(ale#completion#Done() should restore old omnifunc values):
270   let b:ale_old_omnifunc = 'FooBar'
271
272   call ale#completion#Done()
273
274   " We reset the old omnifunc setting and remove the buffer variable.
275   AssertEqual 'FooBar', &l:omnifunc
276   Assert !has_key(b:, 'ale_old_omnifunc')
277
278 Execute(ale#completion#Done() should restore the old completeopt setting):
279   let b:ale_old_completeopt = 'menu'
280
281   call ale#completion#Done()
282
283   AssertEqual 'menu', &l:completeopt
284   Assert !has_key(b:, 'ale_old_completeopt')
285
286 Execute(ale#completion#Done() should leave settings alone when none were remembered):
287   let &l:omnifunc = 'BazBoz'
288   let &l:completeopt = 'menu'
289
290   call ale#completion#Done()
291
292   AssertEqual 'BazBoz', &l:omnifunc
293   AssertEqual 'menu', &l:completeopt
294
295 Execute(The completion request_id should be reset when queuing again):
296   let b:ale_completion_info = {'request_id': 123}
297
298   let g:ale_completion_delay = 0
299   call ale#completion#Queue()
300   sleep 1m
301
302   AssertEqual 0, b:ale_completion_info.request_id
303
304 Execute(b:ale_completion_info should be set up correctly when requesting completions automatically):
305   let b:ale_completion_result = []
306   call setpos('.', [bufnr(''), 3, 14, 0])
307   call ale#completion#GetCompletions('ale-automatic')
308
309   AssertEqual
310   \ {
311   \   'request_id': 0,
312   \   'conn_id': 0,
313   \   'column': 14,
314   \   'line_length': 14,
315   \   'line': 3,
316   \   'prefix': 'ab',
317   \   'source': 'ale-automatic',
318   \ },
319   \ b:ale_completion_info
320   Assert !exists('b:ale_completion_result')
321
322 Execute(b:ale_completion_info should be set up correctly when requesting completions manually):
323   let b:ale_completion_result = []
324   call setpos('.', [bufnr(''), 3, 14, 0])
325   ALEComplete
326
327   AssertEqual
328   \ {
329   \   'request_id': 0,
330   \   'conn_id': 0,
331   \   'column': 14,
332   \   'line_length': 14,
333   \   'line': 3,
334   \   'prefix': 'ab',
335   \   'source': 'ale-manual',
336   \ },
337   \ b:ale_completion_info
338   Assert !exists('b:ale_completion_result')
339
340 Execute(b:ale_completion_info should be set up correctly for other sources):
341   let b:ale_completion_result = []
342   call setpos('.', [bufnr(''), 3, 14, 0])
343   call ale#completion#GetCompletions('ale-callback')
344
345   AssertEqual
346   \ {
347   \   'request_id': 0,
348   \   'conn_id': 0,
349   \   'column': 14,
350   \   'line_length': 14,
351   \   'line': 3,
352   \   'prefix': 'ab',
353   \   'source': 'ale-callback',
354   \ },
355   \ b:ale_completion_info
356   Assert !exists('b:ale_completion_result')
357
358 Execute(b:ale_completion_info should be set up correctly when requesting completions via callback):
359   let b:ale_completion_result = []
360   call setpos('.', [bufnr(''), 3, 14, 0])
361
362   function! CompleteCallback() abort
363     echo 'Called'
364   endfunction
365
366
367   call ale#completion#GetCompletions('ale-callback', {'callback': funcref('CompleteCallback')})
368
369   AssertEqual
370   \ {
371   \   'request_id': 0,
372   \   'conn_id': 0,
373   \   'column': 14,
374   \   'line_length': 14,
375   \   'line': 3,
376   \   'prefix': 'ab',
377   \   'source': 'ale-callback',
378   \ },
379   \ b:ale_completion_info
380   Assert !exists('b:ale_completion_result')
381
382 Execute(The correct keybinds should be configured):
383   redir => g:output
384     silent map <Plug>(ale_show_completion_menu)
385   redir END
386
387   AssertEqual
388   \ [
389   \   'n  <Plug>(ale_show_completion_menu) * :call ale#completion#RestoreCompletionOptions()<CR>',
390   \   'o  <Plug>(ale_show_completion_menu) * <Nop>',
391   \   'v  <Plug>(ale_show_completion_menu) * <Nop>',
392   \ ],
393   \ sort(split(g:output, "\n"))
394
395 Execute(Running the normal mode <Plug> keybind should reset the settings):
396   let b:ale_old_omnifunc = 'FooBar'
397   let b:ale_old_completeopt = 'menu'
398
399   " We can't run the keybind, but we can call the function.
400   call ale#completion#RestoreCompletionOptions()
401
402   AssertEqual 'FooBar', &l:omnifunc
403   AssertEqual 'menu', &l:completeopt
404   Assert !has_key(b:, 'ale_old_omnifunc')
405   Assert !has_key(b:, 'ale_old_completeopt')
406
407 Execute(HandleUserData should call ale#code_action#HandleCodeAction):
408   let b:ale_completion_info = {'source': 'ale-manual'}
409   call MockHandleCodeAction()
410
411   call ale#completion#HandleUserData({})
412   AssertEqual g:handle_code_action_called, 0
413
414   call ale#completion#HandleUserData({
415   \ 'user_data': ''
416   \})
417   AssertEqual g:handle_code_action_called, 0
418
419   call ale#completion#HandleUserData({
420   \ 'user_data': json_encode({}),
421   \})
422   AssertEqual g:handle_code_action_called, 0
423
424   call ale#completion#HandleUserData({
425   \ 'user_data': json_encode({
426   \   '_ale_completion_item': 1,
427   \   'code_actions': [],
428   \ }),
429   \})
430   AssertEqual g:handle_code_action_called, 0
431
432   call ale#completion#HandleUserData({
433   \ 'user_data': json_encode({
434   \   '_ale_completion_item': 1,
435   \   'code_actions': [
436   \     {'description': '', 'changes': []},
437   \   ],
438   \ }),
439   \})
440   AssertEqual g:handle_code_action_called, 1
441
442   let b:ale_completion_info = {'source': 'ale-automatic'}
443   call ale#completion#HandleUserData({
444   \ 'user_data': json_encode({
445   \   '_ale_completion_item': 1,
446   \   'code_actions': [
447   \     {'description': '', 'changes': []},
448   \   ],
449   \ }),
450   \})
451   AssertEqual g:handle_code_action_called, 2
452
453   let b:ale_completion_info = {'source': 'ale-callback'}
454   call ale#completion#HandleUserData({
455   \ 'user_data': json_encode({
456   \   '_ale_completion_item': 1,
457   \   'code_actions': [
458   \     {'description': '', 'changes': []},
459   \   ],
460   \ }),
461   \})
462   AssertEqual g:handle_code_action_called, 3
463
464   let b:ale_completion_info = {'source': 'ale-omnifunc'}
465   call ale#completion#HandleUserData({
466   \ 'user_data': json_encode({
467   \   '_ale_completion_item': 1,
468   \   'code_actions': [
469   \     {'description': '', 'changes': []},
470   \   ],
471   \ }),
472   \})
473   AssertEqual g:handle_code_action_called, 4
474
475 Execute(ale#code_action#HandleCodeAction should not be called when when source is not ALE):
476   call MockHandleCodeAction()
477   let b:ale_completion_info = {'source': 'syntastic'}
478   call ale#completion#HandleUserData({
479   \ 'user_data': json_encode({
480   \   '_ale_completion_item': 1,
481   \   'code_actions': [
482   \     {'description': '', 'changes': []},
483   \   ],
484   \ }),
485   \})
486   AssertEqual g:handle_code_action_called, 0