1scriptencoding utf-8
2
3function! Test_Compl_After_Trigger()
4  call youcompleteme#test#setup#OpenFile(
5        \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
6
7  call setpos( '.', [ 0, 11, 6 ] )
8
9  " Required to trigger TextChangedI
10  " https://github.com/vim/vim/issues/4665#event-2480928194
11  call test_override( 'char_avail', 1 )
12
13  " Must do the checks in a timer callback because we need to stay in insert
14  " mode until done.
15  function! Check( id ) closure
16    call WaitForCompletion()
17    call feedkeys( "\<ESC>" )
18  endfunction
19
20  call FeedAndCheckMain( 'cl.', funcref( 'Check' ) )
21  " Checks run in insert mode, then exit insert mode.
22  call assert_false( pumvisible(), 'pumvisible()' )
23
24  call test_override( 'ALL', 0 )
25endfunctio
26
27function! Test_Force_Semantic_TopLevel()
28  call youcompleteme#test#setup#OpenFile(
29        \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
30
31  call setpos( '.', [ 0, 17, 5 ] )
32
33  function! Check( id )
34    cal WaitForCompletion()
35    let items = complete_info( [ 'items' ] )[ 'items' ]
36    call assert_equal( 1, len( filter( items, 'v:val.abbr ==# "Foo"' ) ),
37          \ 'Foo should be in the suggestions' )
38
39    let items = complete_info( [ 'items' ] )[ 'items' ]
40    call assert_equal( 1,
41          \ len( filter( items, 'v:val.word ==# "__FUNCTION__"' ) ),
42          \ '__FUNCTION__ should be in the suggestions' )
43
44    call feedkeys( "\<ESC>" )
45  endfunction
46
47  call FeedAndCheckMain( "i\<C-Space>", funcref( 'Check' ) )
48  " Checks run in insert mode, then exit insert mode.
49  call assert_false( pumvisible(), 'pumvisible()' )
50
51  call test_override( 'ALL', 0 )
52endfunction
53
54function! Test_Select_Next_Previous()
55  call youcompleteme#test#setup#OpenFile(
56        \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
57
58  call setpos( '.', [ 0, 11, 6 ] )
59
60  " Required to trigger TextChangedI
61  " https://github.com/vim/vim/issues/4665#event-2480928194
62  call test_override( 'char_avail', 1 )
63
64  function! Check( id )
65    call WaitForCompletion()
66
67    call CheckCurrentLine( '  foo.' )
68    call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
69
70    call FeedAndCheckAgain( "\<Tab>", funcref( 'Check2' ) )
71  endfunction
72
73  function! Check2( id )
74    call WaitForCompletion()
75    call CheckCurrentLine( '  foo.c' )
76    call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
77
78    call FeedAndCheckAgain( "\<Tab>", funcref( 'Check3' ) )
79  endfunction
80
81  function! Check3( id )
82    call WaitForCompletion()
83    call CheckCurrentLine( '  foo.x' )
84    call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
85
86    call FeedAndCheckAgain( "\<BS>y", funcref( 'Check4' ) )
87  endfunction
88
89  function! Check4( id )
90    call WaitForCompletion()
91    call CheckCurrentLine( '  foo.y' )
92    call CheckCompletionItemsContainsExactly( [ 'y' ] )
93    call feedkeys( "\<ESC>" )
94  endfunction
95
96  call FeedAndCheckMain( 'cl.', funcref( 'Check' ) )
97  " Checks run in insert mode, then exit insert mode.
98  call assert_false( pumvisible(), 'pumvisible()' )
99
100  call test_override( 'ALL', 0 )
101endfunction
102
103function! Test_Enter_Delete_Chars_Updates_Filter()
104  call youcompleteme#test#setup#OpenFile(
105        \ 'test/testdata/cpp/completion.cc', {} )
106
107  call setpos( '.', [ 0, 23, 31 ] )
108
109  " Required to trigger TextChangedI
110  " https://github.com/vim/vim/issues/4665#event-2480928194
111  call test_override( 'char_avail', 1 )
112
113  function! Check1( id )
114    call WaitForCompletion()
115    call CheckCompletionItemsContainsExactly( [ 'colourOfLine', 'lengthOfLine' ] )
116    call FeedAndCheckAgain( "\<BS>", funcref( 'Check2' ) )
117  endfunction
118
119  function! Check2( id )
120    call WaitForCompletion()
121    call CheckCompletionItemsContainsExactly( [
122          \ 'operator=(…)',
123          \ 'colourOfLine',
124          \ 'lengthOfLine',
125          \ 'RED_AND_YELLOW' ] )
126    call FeedAndCheckAgain( 'w', funcref( 'Check3' ) )
127  endfunction
128
129  function! Check3( id )
130    call WaitForCompletion()
131    call CheckCompletionItemsContainsExactly( [ 'RED_AND_YELLOW' ] )
132    " now type something that doesnt match
133    call FeedAndCheckAgain( 'this_does_not_match', funcref( 'Check4' ) )
134  endfunction
135
136  function! Check4( id )
137    call WaitForAssert( { -> assert_false( pumvisible() ) } )
138    call CheckCurrentLine(
139          \ '  p->line.colourOfLine = Line::owthis_does_not_match' )
140    call CheckCompletionItemsContainsExactly( [] )
141    call feedkeys( "\<Esc>" )
142  endfunction
143
144  call FeedAndCheckMain( 'cl:ol', funcref( 'Check1' ) )
145  " Checks run in insert mode, then exit insert mode.
146  call assert_false( pumvisible(), 'pumvisible()' )
147
148  call test_override( 'ALL', 0 )
149endfunction
150
151function! SetUp_Test_Compl_No_Filetype()
152  call youcompleteme#test#setup#PushGlobal( 'ycm_filetype_whitelist', {
153        \ '*': 1,
154        \ 'ycm_nofiletype': 1
155        \ } )
156  call youcompleteme#test#setup#PushGlobal( 'ycm_filetype_blacklist', {} )
157endfunction
158
159function! Test_Compl_No_Filetype()
160  call assert_false( has_key( g:ycm_filetype_blacklist, 'ycm_nofiletype' ) )
161  enew
162  call setline( '.', 'hello this is some text ' )
163
164  " Even when fileytpe is set to '', the filetype autocommand is triggered, but
165  " apparently, _not_ within this function.
166  doautocmd FileType
167  call assert_equal( 1, b:ycm_completing )
168
169  " Required to trigger TextChangedI
170  " https://github.com/vim/vim/issues/4665#event-2480928194
171  call test_override( 'char_avail', 1 )
172
173  " Must do the checks in a timer callback because we need to stay in insert
174  " mode until done.
175  function! Check( id ) closure
176    call assert_equal( getline( '2' ), 'hell' )
177    call WaitForCompletion()
178    let items = complete_info().items
179    call map( items, {index, value -> value.word} )
180    call assert_equal( [ 'hello' ], items )
181    call feedkeys( "\<ESC>" )
182  endfunction
183
184  call FeedAndCheckMain( 'ohell', funcref( 'Check' ) )
185  " Checks run in insert mode, then exit insert mode.
186  call assert_false( pumvisible(), 'pumvisible()' )
187
188  call test_override( 'ALL', 0 )
189  delfunc! Check
190endfunction
191
192function! TearDown_Test_Compl_No_Filetype()
193  call youcompleteme#test#setup#PopGlobal( 'ycm_filetype_whitelist' )
194  call youcompleteme#test#setup#PopGlobal( 'ycm_filetype_blacklist' )
195endfunction
196
197function! Test_Compl_No_Filetype_Blacklisted()
198  call assert_true( has_key( g:ycm_filetype_blacklist, 'ycm_nofiletype' ) )
199
200  enew
201  call setline( '.', 'hello this is some text ' )
202
203  " Even when fileytpe is set to '', the filetype autocommand is triggered, but
204  " apparently, _not_ within this function.
205  doautocmd FileType
206  call assert_false( exists( 'b:ycm_completing' ) )
207
208  " Required to trigger TextChangedI
209  " https://github.com/vim/vim/issues/4665#event-2480928194
210  call test_override( 'char_avail', 1 )
211
212  " Must do the checks in a timer callback because we need to stay in insert
213  " mode until done.
214  function! Check( id ) closure
215    call assert_false( pumvisible() )
216    call feedkeys( "\<ESC>" )
217  endfunction
218
219  call FeedAndCheckMain( 'ohell', funcref( 'Check' ) )
220  " Checks run in insert mode, then exit insert mode.
221  call assert_false( pumvisible(), 'pumvisible()' )
222
223  call test_override( 'ALL', 0 )
224  delfunc! Check
225endfunction
226
227function! OmniFuncTester( findstart, query )
228  if a:findstart
229    return s:omnifunc_start_col
230  endif
231  return s:omnifunc_items
232endfunction
233
234function! SetUp_Test_OmniComplete_Filter()
235  call youcompleteme#test#setup#PushGlobal( 'ycm_semantic_triggers', {
236        \ 'omnifunc_test': [ ':', '.' ]
237        \ } )
238endfunction
239
240function! Test_OmniComplete_Filter()
241  enew
242  setf omnifunc_test
243  set omnifunc=OmniFuncTester
244
245  let s:omnifunc_start_col = 3
246  let s:omnifunc_items = [ 'test', 'testing', 'testy' ]
247
248  function! Check1( id )
249    call WaitForCompletion()
250    call CheckCurrentLine( 'te:te' )
251    call CheckCompletionItemsContainsExactly( [ 'test', 'testy', 'testing' ],
252                                            \ 'word' )
253    call FeedAndCheckAgain( 'y', funcref( 'Check2' ) )
254  endfunction
255
256  function! Check2( id )
257    call WaitForCompletion()
258    call CheckCurrentLine( 'te:tey' )
259    call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
260    call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
261  endfunction
262
263  function! Check3( id )
264    call WaitForCompletion()
265    call CheckCurrentLine( 'te:testy' )
266    call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
267    call FeedAndCheckAgain( "\<C-p>", funcref( 'Check4' ) )
268  endfunction
269
270  function! Check4( id )
271    call WaitForCompletion()
272    call CheckCurrentLine( 'te:tey' )
273    call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
274    call feedkeys( "\<Esc>" )
275  endfunction
276
277  call setline(1, 'te:' )
278  call setpos( '.', [ 0, 1, 3 ] )
279  call FeedAndCheckMain( 'ate', 'Check1' )
280endfunction
281
282function! TearDown_Test_OmniComplete_Filter()
283  call youcompleteme#test#setup#PopGlobal( 'ycm_semantic_triggers' )
284endfunction
285
286function! Test_OmniComplete_Force()
287  enew
288  setf omnifunc_test
289  set omnifunc=OmniFuncTester
290
291  let s:omnifunc_start_col = 0
292  let s:omnifunc_items = [ 'test', 'testing', 'testy' ]
293
294  function! Check1( id )
295    call WaitForCompletion()
296    call CheckCurrentLine( 'te' )
297    call CheckCompletionItemsContainsExactly( [ 'test', 'testy', 'testing' ],
298                                            \ 'word' )
299    call FeedAndCheckAgain( 'y', funcref( 'Check2' ) )
300  endfunction
301
302  function! Check2( id )
303    call WaitForCompletion()
304    call CheckCurrentLine( 'tey' )
305    call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
306    call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
307  endfunction
308
309  function! Check3( id )
310    call WaitForCompletion()
311    call CheckCurrentLine( 'testy' )
312    call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
313    call FeedAndCheckAgain( "\<C-p>", funcref( 'Check4' ) )
314  endfunction
315
316  function! Check4( id )
317    call WaitForCompletion()
318    call CheckCurrentLine( 'tey' )
319    call CheckCompletionItemsContainsExactly( [ 'testy' ], 'word' )
320    call feedkeys( "\<Esc>" )
321  endfunction
322
323  call setline(1, 'te' )
324  call setpos( '.', [ 0, 1, 3 ] )
325  call FeedAndCheckMain( "a\<C-Space>", 'Check1' )
326endfunction
327
328function! Test_Completion_FixIt()
329  " There's a bug in clangd where you have to open a file which includes the
330  " file you want to auto-include before it will actually auto-include that
331  " file, auto_include_workaround #includes auto_include.h, so that clangd knows
332  " about it
333  call youcompleteme#test#setup#OpenFile(
334        \ 'test/testdata/cpp/auto_include_workaround.cc', {} )
335
336  call youcompleteme#test#setup#OpenFile(
337        \ 'test/testdata/cpp/auto_include.cc', {} )
338
339  function Check1( id )
340    call WaitForCompletion()
341    call CheckCurrentLine( 'do_a' )
342    call CheckCompletionItemsContainsExactly( [ 'do_a_thing(Thing thing)',
343                                 \ 'do_another_thing()' ] )
344    call FeedAndCheckAgain( "\<Tab>" , funcref( 'Check2' ) )
345  endfunction
346
347  function Check2( id )
348    call WaitForCompletion()
349    call CheckCurrentLine( 'do_a_thing' )
350    call CheckCompletionItemsContainsExactly( [ 'do_a_thing(Thing thing)',
351                                 \ 'do_another_thing()' ] )
352    call FeedAndCheckAgain( '(' , funcref( 'Check3' ) )
353  endfunction
354
355
356  function Check3( id )
357    call WaitForAssert( {-> assert_false( pumvisible(), 'pumvisible()' ) } )
358    call CheckCurrentLine( 'do_a_thing(' )
359    call assert_equal( '#include "auto_include.h"', getline( 1 ) )
360    call feedkeys( "\<Esc>" )
361  endfunction
362
363  call setpos( '.', [ 0, 3, 1 ] )
364  call FeedAndCheckMain( "Ado_a\<C-Space>", funcref( 'Check1' ) )
365endfunction
366
367function! Test_Select_Next_Previous_InsertModeMapping()
368  call youcompleteme#test#setup#OpenFile(
369        \ '/third_party/ycmd/ycmd/tests/clangd/testdata/basic.cpp', {} )
370
371  call setpos( '.', [ 0, 11, 6 ] )
372
373  inoremap <C-n> <Down>
374
375  " Required to trigger TextChangedI
376  " https://github.com/vim/vim/issues/4665#event-2480928194
377  call test_override( 'char_avail', 1 )
378
379  function! Check( id )
380    call WaitForCompletion()
381
382    call CheckCurrentLine( '  foo.' )
383    call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
384
385    call FeedAndCheckAgain( "\<C-n>", funcref( 'Check2' ) )
386  endfunction
387
388  function! Check2( id )
389    call WaitForCompletion()
390    call CheckCurrentLine( '  foo.c' )
391    call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
392
393    call FeedAndCheckAgain( "\<C-n>", funcref( 'Check3' ) )
394  endfunction
395
396  function! Check3( id )
397    call WaitForCompletion()
398    call CheckCurrentLine( '  foo.x' )
399    call CheckCompletionItemsContainsExactly( [ 'c', 'x', 'y' ] )
400
401    call FeedAndCheckAgain( "\<BS>a", funcref( 'Check4' ) )
402  endfunction
403
404  function! Check4( id )
405    call CheckCurrentLine( '  foo.a' )
406    call CheckCompletionItemsContainsExactly( [] )
407    call FeedAndCheckAgain( "\<C-n>", funcref( 'Check5' ) )
408  endfunction
409
410  function! Check5( id )
411    " The last ctrl-n moved to the next line
412    call CheckCurrentLine( '}' )
413    call assert_equal( [ 0, 12, 2, 0 ], getpos( '.' ) )
414    call CheckCompletionItemsContainsExactly( [] )
415    call feedkeys( "\<Esc>" )
416  endfunction
417
418
419  call FeedAndCheckMain( 'cl.', funcref( 'Check' ) )
420  " Checks run in insert mode, then exit insert mode.
421  call assert_false( pumvisible(), 'pumvisible()' )
422
423  call test_override( 'ALL', 0 )
424  iunmap <C-n>
425endfunction
426