1source screendump.vim
2source check.vim
3
4" Test for insert expansion
5func Test_ins_complete()
6  edit test_ins_complete.vim
7  " The files in the current directory interferes with the files
8  " used by this test. So use a separate directory for the test.
9  call mkdir('Xdir')
10  cd Xdir
11
12  set ff=unix
13  call writefile(["test11\t36Gepeto\t/Tag/",
14	      \ "asd\ttest11file\t36G",
15	      \ "Makefile\tto\trun"], 'Xtestfile')
16  call writefile(['', 'start of testfile',
17	      \ 'ru',
18	      \ 'run1',
19	      \ 'run2',
20	      \ 'STARTTEST',
21	      \ 'ENDTEST',
22	      \ 'end of testfile'], 'Xtestdata')
23  set ff&
24
25  enew!
26  edit Xtestdata
27  new
28  call append(0, ['#include "Xtestfile"', ''])
29  call cursor(2, 1)
30
31  set cot=
32  set cpt=.,w
33  " add-expands (word from next line) from other window
34  exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
35  call assert_equal('run1 run3', getline('.'))
36  " add-expands (current buffer first)
37  exe "normal o\<C-P>\<C-X>\<C-N>"
38  call assert_equal('run3 run3', getline('.'))
39  " Local expansion, ends in an empty line (unless it becomes a global
40  " expansion)
41  exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
42  call assert_equal('', getline('.'))
43  " starts Local and switches to global add-expansion
44  exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
45  call assert_equal('run1 run2', getline('.'))
46
47  set cpt=.,w,i
48  " i-add-expands and switches to local
49  exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
50  call assert_equal("Makefile\tto\trun3", getline('.'))
51  " add-expands lines (it would end in an empty line if it didn't ignored
52  " itself)
53  exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
54  call assert_equal("Makefile\tto\trun3", getline('.'))
55  call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
56
57  set cpt=kXtestfile
58  " checks k-expansion, and file expansion (use Xtest11 instead of test11,
59  " because TEST11.OUT may match first on DOS)
60  write Xtest11.one
61  write Xtest11.two
62  exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
63  call assert_equal('Xtest11.two', getline('.'))
64
65  " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
66  " CTRL-F again to verify this doesn't cause trouble.
67  exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
68  call assert_equal('Xtest11.one', getline('.'))
69  normal ddk
70
71  set cpt=w
72  " checks make_cyclic in other window
73  exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
74  call assert_equal('STARTTEST', getline('.'))
75
76  set cpt=u nohid
77  " checks unloaded buffer expansion
78  only
79  exe "normal oEN\<C-N>"
80  call assert_equal('ENDTEST', getline('.'))
81  " checks adding mode abortion
82  exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
83  call assert_equal('unless', getline('.'))
84
85  set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
86  " tag expansion, define add-expansion interrupted
87  exe "normal o\<C-X>\<C-]>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>\<C-X>\<C-D>"
88  call assert_equal('test11file	36Gepeto	/Tag/ asd', getline('.'))
89  " t-expansion
90  exe "normal oa\<C-N>\<Esc>"
91  call assert_equal('asd', getline('.'))
92
93  %bw!
94  call delete('Xtestfile')
95  call delete('Xtest11.one')
96  call delete('Xtest11.two')
97  call delete('Xtestdata')
98  set cpt& cot& def& tags& tagbsearch& hidden&
99  cd ..
100  call delete('Xdir', 'rf')
101endfunc
102
103func Test_omni_dash()
104  func Omni(findstart, base)
105    if a:findstart
106        return 5
107    else
108        echom a:base
109	return ['-help', '-v']
110    endif
111  endfunc
112  set omnifunc=Omni
113  new
114  exe "normal Gofind -\<C-x>\<C-o>"
115  call assert_equal("\n-\nmatch 1 of 2", execute(':2mess'))
116
117  bwipe!
118  delfunc Omni
119  set omnifunc=
120endfunc
121
122func Test_completefunc_args()
123  let s:args = []
124  func! CompleteFunc(findstart, base)
125    let s:args += [[a:findstart, empty(a:base)]]
126  endfunc
127  new
128
129  set completefunc=CompleteFunc
130  call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
131  call assert_equal([1, 1], s:args[0])
132  call assert_equal(0, s:args[1][0])
133  set completefunc=
134
135  let s:args = []
136  set omnifunc=CompleteFunc
137  call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
138  call assert_equal([1, 1], s:args[0])
139  call assert_equal(0, s:args[1][0])
140  set omnifunc=
141
142  bwipe!
143  unlet s:args
144  delfunc CompleteFunc
145endfunc
146
147func s:CompleteDone_CompleteFuncNone( findstart, base )
148  if a:findstart
149    return 0
150  endif
151
152  return v:none
153endfunc
154
155func s:CompleteDone_CompleteFuncDict( findstart, base )
156  if a:findstart
157    return 0
158  endif
159
160  return {
161	  \ 'words': [
162	    \ {
163	      \ 'word': 'aword',
164	      \ 'abbr': 'wrd',
165	      \ 'menu': 'extra text',
166	      \ 'info': 'words are cool',
167	      \ 'kind': 'W',
168	      \ 'user_data': 'test'
169	    \ }
170	  \ ]
171	\ }
172endfunc
173
174func s:CompleteDone_CheckCompletedItemNone()
175  let s:called_completedone = 1
176endfunc
177
178func s:CompleteDone_CheckCompletedItemDict(pre)
179  call assert_equal( 'aword',          v:completed_item[ 'word' ] )
180  call assert_equal( 'wrd',            v:completed_item[ 'abbr' ] )
181  call assert_equal( 'extra text',     v:completed_item[ 'menu' ] )
182  call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
183  call assert_equal( 'W',              v:completed_item[ 'kind' ] )
184  call assert_equal( 'test',           v:completed_item[ 'user_data' ] )
185
186  if a:pre
187    call assert_equal('function', complete_info().mode)
188  endif
189
190  let s:called_completedone = 1
191endfunc
192
193func Test_CompleteDoneNone()
194  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
195  let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
196
197  set completefunc=<SID>CompleteDone_CompleteFuncNone
198  execute "normal a\<C-X>\<C-U>\<C-Y>"
199  set completefunc&
200  let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
201
202  call assert_true(s:called_completedone)
203  call assert_equal(oldline, newline)
204
205  let s:called_completedone = 0
206  au! CompleteDone
207endfunc
208
209func Test_CompleteDoneDict()
210  au CompleteDonePre * :call <SID>CompleteDone_CheckCompletedItemDict(1)
211  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict(0)
212
213  set completefunc=<SID>CompleteDone_CompleteFuncDict
214  execute "normal a\<C-X>\<C-U>\<C-Y>"
215  set completefunc&
216
217  call assert_equal('test', v:completed_item[ 'user_data' ])
218  call assert_true(s:called_completedone)
219
220  let s:called_completedone = 0
221  au! CompleteDone
222endfunc
223
224func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
225  if a:findstart
226    return 0
227  endif
228
229  return {
230	  \ 'words': [
231	    \ {
232	      \ 'word': 'aword',
233	      \ 'abbr': 'wrd',
234	      \ 'menu': 'extra text',
235	      \ 'info': 'words are cool',
236	      \ 'kind': 'W',
237	      \ 'user_data': ['one', 'two'],
238	    \ }
239	  \ ]
240	\ }
241endfunc
242
243func s:CompleteDone_CheckCompletedItemDictNoUserData()
244  call assert_equal( 'aword',          v:completed_item[ 'word' ] )
245  call assert_equal( 'wrd',            v:completed_item[ 'abbr' ] )
246  call assert_equal( 'extra text',     v:completed_item[ 'menu' ] )
247  call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
248  call assert_equal( 'W',              v:completed_item[ 'kind' ] )
249  call assert_equal( ['one', 'two'],   v:completed_item[ 'user_data' ] )
250
251  let s:called_completedone = 1
252endfunc
253
254func Test_CompleteDoneDictNoUserData()
255  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
256
257  set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
258  execute "normal a\<C-X>\<C-U>\<C-Y>"
259  set completefunc&
260
261  call assert_equal(['one', 'two'], v:completed_item[ 'user_data' ])
262  call assert_true(s:called_completedone)
263
264  let s:called_completedone = 0
265  au! CompleteDone
266endfunc
267
268func s:CompleteDone_CompleteFuncList(findstart, base)
269  if a:findstart
270    return 0
271  endif
272
273  return [ 'aword' ]
274endfunc
275
276func s:CompleteDone_CheckCompletedItemList()
277  call assert_equal( 'aword', v:completed_item[ 'word' ] )
278  call assert_equal( '',      v:completed_item[ 'abbr' ] )
279  call assert_equal( '',      v:completed_item[ 'menu' ] )
280  call assert_equal( '',      v:completed_item[ 'info' ] )
281  call assert_equal( '',      v:completed_item[ 'kind' ] )
282  call assert_equal( '',      v:completed_item[ 'user_data' ] )
283
284  let s:called_completedone = 1
285endfunc
286
287func Test_CompleteDoneList()
288  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
289
290  set completefunc=<SID>CompleteDone_CompleteFuncList
291  execute "normal a\<C-X>\<C-U>\<C-Y>"
292  set completefunc&
293
294  call assert_equal('', v:completed_item[ 'user_data' ])
295  call assert_true(s:called_completedone)
296
297  let s:called_completedone = 0
298  au! CompleteDone
299endfunc
300
301func Test_CompleteDone_undo()
302  au CompleteDone * call append(0, "prepend1")
303  new
304  call setline(1, ["line1", "line2"])
305  call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
306  call assert_equal(["prepend1", "line1", "line2", "line1", ""],
307              \     getline(1, '$'))
308  undo
309  call assert_equal(["line1", "line2"], getline(1, '$'))
310  bwipe!
311  au! CompleteDone
312endfunc
313
314" Check that when using feedkeys() typeahead does not interrupt searching for
315" completions.
316func Test_compl_feedkeys()
317  new
318  set completeopt=menuone,noselect
319  call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
320  call assert_equal("jump jump", getline(1))
321  bwipe!
322  set completeopt&
323endfunc
324
325func Test_compl_in_cmdwin()
326  set wildmenu wildchar=<Tab>
327  com! -nargs=1 -complete=command GetInput let input = <q-args>
328  com! -buffer TestCommand echo 'TestCommand'
329
330  let input = ''
331  call feedkeys("q:iGetInput T\<C-x>\<C-v>\<CR>", 'tx!')
332  call assert_equal('TestCommand', input)
333
334  let input = ''
335  call feedkeys("q::GetInput T\<Tab>\<CR>:q\<CR>", 'tx!')
336  call assert_equal('T', input)
337
338  delcom TestCommand
339  delcom GetInput
340  set wildmenu& wildchar&
341endfunc
342
343" Test for insert path completion with completeslash option
344func Test_ins_completeslash()
345  CheckMSWindows
346
347  call mkdir('Xdir')
348  let orig_shellslash = &shellslash
349  set cpt&
350  new
351
352  set noshellslash
353
354  set completeslash=
355  exe "normal oXd\<C-X>\<C-F>"
356  call assert_equal('Xdir\', getline('.'))
357
358  set completeslash=backslash
359  exe "normal oXd\<C-X>\<C-F>"
360  call assert_equal('Xdir\', getline('.'))
361
362  set completeslash=slash
363  exe "normal oXd\<C-X>\<C-F>"
364  call assert_equal('Xdir/', getline('.'))
365
366  set shellslash
367
368  set completeslash=
369  exe "normal oXd\<C-X>\<C-F>"
370  call assert_equal('Xdir/', getline('.'))
371
372  set completeslash=backslash
373  exe "normal oXd\<C-X>\<C-F>"
374  call assert_equal('Xdir\', getline('.'))
375
376  set completeslash=slash
377  exe "normal oXd\<C-X>\<C-F>"
378  call assert_equal('Xdir/', getline('.'))
379  %bw!
380  call delete('Xdir', 'rf')
381
382  set noshellslash
383  set completeslash=slash
384  call assert_true(stridx(globpath(&rtp, 'syntax/*.vim', 1, 1)[0], '\') != -1)
385
386  let &shellslash = orig_shellslash
387  set completeslash=
388endfunc
389
390func Test_pum_with_folds_two_tabs()
391  CheckScreendump
392
393  let lines =<< trim END
394    set fdm=marker
395    call setline(1, ['" x {{{1', '" a some text'])
396    call setline(3, range(&lines)->map({_, val -> '" a' .. val}))
397    norm! zm
398    tab sp
399    call feedkeys('2Gzv', 'xt')
400    call feedkeys("0fa", 'xt')
401  END
402
403  call writefile(lines, 'Xpumscript')
404  let buf = RunVimInTerminal('-S Xpumscript', #{rows: 10})
405  call TermWait(buf, 50)
406  call term_sendkeys(buf, "a\<C-N>")
407  call VerifyScreenDump(buf, 'Test_pum_with_folds_two_tabs', {})
408
409  call term_sendkeys(buf, "\<Esc>")
410  call StopVimInTerminal(buf)
411  call delete('Xpumscript')
412endfunc
413
414func Test_pum_with_preview_win()
415  CheckScreendump
416
417  let lines =<< trim END
418      funct Omni_test(findstart, base)
419	if a:findstart
420	  return col(".") - 1
421	endif
422	return [#{word: "one", info: "1info"}, #{word: "two", info: "2info"}, #{word: "three", info: "3info"}]
423      endfunc
424      set omnifunc=Omni_test
425      set completeopt+=longest
426  END
427
428  call writefile(lines, 'Xpreviewscript')
429  let buf = RunVimInTerminal('-S Xpreviewscript', #{rows: 12})
430  call TermWait(buf, 50)
431  call term_sendkeys(buf, "Gi\<C-X>\<C-O>")
432  call TermWait(buf, 100)
433  call term_sendkeys(buf, "\<C-N>")
434  call VerifyScreenDump(buf, 'Test_pum_with_preview_win', {})
435
436  call term_sendkeys(buf, "\<Esc>")
437  call StopVimInTerminal(buf)
438  call delete('Xpreviewscript')
439endfunc
440
441" Test for inserting the tag search pattern in insert mode
442func Test_ins_compl_tag_sft()
443  call writefile([
444        \ "!_TAG_FILE_ENCODING\tutf-8\t//",
445        \ "first\tXfoo\t/^int first() {}$/",
446        \ "second\tXfoo\t/^int second() {}$/",
447        \ "third\tXfoo\t/^int third() {}$/"],
448        \ 'Xtags')
449  set tags=Xtags
450  let code =<< trim [CODE]
451    int first() {}
452    int second() {}
453    int third() {}
454  [CODE]
455  call writefile(code, 'Xfoo')
456
457  enew
458  set showfulltag
459  exe "normal isec\<C-X>\<C-]>\<C-N>\<CR>"
460  call assert_equal('int second() {}', getline(1))
461  set noshowfulltag
462
463  call delete('Xtags')
464  call delete('Xfoo')
465  set tags&
466  %bwipe!
467endfunc
468
469" Test for 'completefunc' deleting text
470func Test_completefunc_error()
471  new
472  func CompleteFunc(findstart, base)
473    if a:findstart == 1
474      normal dd
475      return col('.') - 1
476    endif
477    return ['a', 'b']
478  endfunc
479  set completefunc=CompleteFunc
480  call setline(1, ['', 'abcd', ''])
481  call assert_fails('exe "normal 2G$a\<C-X>\<C-U>"', 'E840:')
482  set completefunc&
483  delfunc CompleteFunc
484  close!
485endfunc
486
487" vim: shiftwidth=2 sts=2 expandtab
488