1
2" Test for insert expansion
3func Test_ins_complete()
4  edit test_ins_complete.vim
5  " The files in the current directory interferes with the files
6  " used by this test. So use a separate directory for the test.
7  call mkdir('Xdir')
8  cd Xdir
9
10  set ff=unix
11  call writefile(["test11\t36Gepeto\t/Tag/",
12	      \ "asd\ttest11file\t36G",
13	      \ "Makefile\tto\trun"], 'Xtestfile')
14  call writefile(['', 'start of testfile',
15	      \ 'ru',
16	      \ 'run1',
17	      \ 'run2',
18	      \ 'STARTTEST',
19	      \ 'ENDTEST',
20	      \ 'end of testfile'], 'Xtestdata')
21  set ff&
22
23  enew!
24  edit Xtestdata
25  new
26  call append(0, ['#include "Xtestfile"', ''])
27  call cursor(2, 1)
28
29  set cot=
30  set cpt=.,w
31  " add-expands (word from next line) from other window
32  exe "normal iru\<C-N>\<C-N>\<C-X>\<C-N>\<Esc>\<C-A>"
33  call assert_equal('run1 run3', getline('.'))
34  " add-expands (current buffer first)
35  exe "normal o\<C-P>\<C-X>\<C-N>"
36  call assert_equal('run3 run3', getline('.'))
37  " Local expansion, ends in an empty line (unless it becomes a global
38  " expansion)
39  exe "normal o\<C-X>\<C-P>\<C-P>\<C-P>\<C-P>\<C-P>"
40  call assert_equal('', getline('.'))
41  " starts Local and switches to global add-expansion
42  exe "normal o\<C-X>\<C-P>\<C-P>\<C-X>\<C-X>\<C-N>\<C-X>\<C-N>\<C-N>"
43  call assert_equal('run1 run2', getline('.'))
44
45  set cpt=.,w,i
46  " i-add-expands and switches to local
47  exe "normal OM\<C-N>\<C-X>\<C-N>\<C-X>\<C-N>\<C-X>\<C-X>\<C-X>\<C-P>"
48  call assert_equal("Makefile\tto\trun3", getline('.'))
49  " add-expands lines (it would end in an empty line if it didn't ignored
50  " itself)
51  exe "normal o\<C-X>\<C-L>\<C-X>\<C-L>\<C-P>\<C-P>"
52  call assert_equal("Makefile\tto\trun3", getline('.'))
53  call assert_equal("Makefile\tto\trun3", getline(line('.') - 1))
54
55  set cpt=kXtestfile
56  " checks k-expansion, and file expansion (use Xtest11 instead of test11,
57  " because TEST11.OUT may match first on DOS)
58  write Xtest11.one
59  write Xtest11.two
60  exe "normal o\<C-N>\<Esc>IX\<Esc>A\<C-X>\<C-F>\<C-N>"
61  call assert_equal('Xtest11.two', getline('.'))
62
63  " use CTRL-X CTRL-F to complete Xtest11.one, remove it and then use CTRL-X
64  " CTRL-F again to verify this doesn't cause trouble.
65  exe "normal oXt\<C-X>\<C-F>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<BS>\<C-X>\<C-F>"
66  call assert_equal('Xtest11.one', getline('.'))
67  normal ddk
68
69  set cpt=w
70  " checks make_cyclic in other window
71  exe "normal oST\<C-N>\<C-P>\<C-P>\<C-P>\<C-P>"
72  call assert_equal('STARTTEST', getline('.'))
73
74  set cpt=u nohid
75  " checks unloaded buffer expansion
76  only
77  exe "normal oEN\<C-N>"
78  call assert_equal('ENDTEST', getline('.'))
79  " checks adding mode abortion
80  exe "normal ounl\<C-N>\<C-X>\<C-X>\<C-P>"
81  call assert_equal('unless', getline('.'))
82
83  set cpt=t,d def=^\\k* tags=Xtestfile notagbsearch
84  " tag expansion, define add-expansion interrupted
85  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>"
86  call assert_equal('test11file	36Gepeto	/Tag/ asd', getline('.'))
87  " t-expansion
88  exe "normal oa\<C-N>\<Esc>"
89  call assert_equal('asd', getline('.'))
90
91  %bw!
92  call delete('Xtestfile')
93  call delete('Xtest11.one')
94  call delete('Xtest11.two')
95  call delete('Xtestdata')
96  set cpt& cot& def& tags& tagbsearch& hidden&
97  cd ..
98  call delete('Xdir', 'rf')
99endfunc
100
101func Test_omni_dash()
102  func Omni(findstart, base)
103    if a:findstart
104        return 5
105    else
106        echom a:base
107	return ['-help', '-v']
108    endif
109  endfunc
110  set omnifunc=Omni
111  new
112  exe "normal Gofind -\<C-x>\<C-o>"
113  call assert_equal("\n-\nmatch 1 of 2", execute(':2mess'))
114
115  bwipe!
116  delfunc Omni
117  set omnifunc=
118endfunc
119
120func Test_completefunc_args()
121  let s:args = []
122  func! CompleteFunc(findstart, base)
123    let s:args += [[a:findstart, empty(a:base)]]
124  endfunc
125  new
126
127  set completefunc=CompleteFunc
128  call feedkeys("i\<C-X>\<C-U>\<Esc>", 'x')
129  call assert_equal([1, 1], s:args[0])
130  call assert_equal(0, s:args[1][0])
131  set completefunc=
132
133  let s:args = []
134  set omnifunc=CompleteFunc
135  call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
136  call assert_equal([1, 1], s:args[0])
137  call assert_equal(0, s:args[1][0])
138  set omnifunc=
139
140  bwipe!
141  unlet s:args
142  delfunc CompleteFunc
143endfunc
144
145func s:CompleteDone_CompleteFuncNone( findstart, base )
146  if a:findstart
147    return 0
148  endif
149
150  return v:none
151endfunc
152
153func s:CompleteDone_CompleteFuncDict( findstart, base )
154  if a:findstart
155    return 0
156  endif
157
158  return {
159          \ 'words': [
160            \ {
161              \ 'word': 'aword',
162              \ 'abbr': 'wrd',
163              \ 'menu': 'extra text',
164              \ 'info': 'words are cool',
165              \ 'kind': 'W',
166              \ 'user_data': 'test'
167            \ }
168          \ ]
169        \ }
170endfunc
171
172func s:CompleteDone_CheckCompletedItemNone()
173  let s:called_completedone = 1
174endfunc
175
176func s:CompleteDone_CheckCompletedItemDict()
177  call assert_equal( 'aword',          v:completed_item[ 'word' ] )
178  call assert_equal( 'wrd',            v:completed_item[ 'abbr' ] )
179  call assert_equal( 'extra text',     v:completed_item[ 'menu' ] )
180  call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
181  call assert_equal( 'W',              v:completed_item[ 'kind' ] )
182  call assert_equal( 'test',           v:completed_item[ 'user_data' ] )
183
184  let s:called_completedone = 1
185endfunc
186
187func Test_CompleteDoneNone()
188  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemNone()
189  let oldline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
190
191  set completefunc=<SID>CompleteDone_CompleteFuncNone
192  execute "normal a\<C-X>\<C-U>\<C-Y>"
193  set completefunc&
194  let newline = join(map(range(&columns), 'nr2char(screenchar(&lines-1, v:val+1))'), '')
195
196  call assert_true(s:called_completedone)
197  call assert_equal(oldline, newline)
198
199  let s:called_completedone = 0
200  au! CompleteDone
201endfunc
202
203func Test_CompleteDoneDict()
204  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict()
205
206  set completefunc=<SID>CompleteDone_CompleteFuncDict
207  execute "normal a\<C-X>\<C-U>\<C-Y>"
208  set completefunc&
209
210  call assert_equal('test', v:completed_item[ 'user_data' ])
211  call assert_true(s:called_completedone)
212
213  let s:called_completedone = 0
214  au! CompleteDone
215endfunc
216
217func s:CompleteDone_CompleteFuncDictNoUserData(findstart, base)
218  if a:findstart
219    return 0
220  endif
221
222  return {
223          \ 'words': [
224            \ {
225              \ 'word': 'aword',
226              \ 'abbr': 'wrd',
227              \ 'menu': 'extra text',
228              \ 'info': 'words are cool',
229              \ 'kind': 'W'
230            \ }
231          \ ]
232        \ }
233endfunc
234
235func s:CompleteDone_CheckCompletedItemDictNoUserData()
236  call assert_equal( 'aword',          v:completed_item[ 'word' ] )
237  call assert_equal( 'wrd',            v:completed_item[ 'abbr' ] )
238  call assert_equal( 'extra text',     v:completed_item[ 'menu' ] )
239  call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
240  call assert_equal( 'W',              v:completed_item[ 'kind' ] )
241  call assert_equal( '',               v:completed_item[ 'user_data' ] )
242
243  let s:called_completedone = 1
244endfunc
245
246func Test_CompleteDoneDictNoUserData()
247  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
248
249  set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
250  execute "normal a\<C-X>\<C-U>\<C-Y>"
251  set completefunc&
252
253  call assert_equal('', v:completed_item[ 'user_data' ])
254  call assert_true(s:called_completedone)
255
256  let s:called_completedone = 0
257  au! CompleteDone
258endfunc
259
260func s:CompleteDone_CompleteFuncList(findstart, base)
261  if a:findstart
262    return 0
263  endif
264
265  return [ 'aword' ]
266endfunc
267
268func s:CompleteDone_CheckCompletedItemList()
269  call assert_equal( 'aword', v:completed_item[ 'word' ] )
270  call assert_equal( '',      v:completed_item[ 'abbr' ] )
271  call assert_equal( '',      v:completed_item[ 'menu' ] )
272  call assert_equal( '',      v:completed_item[ 'info' ] )
273  call assert_equal( '',      v:completed_item[ 'kind' ] )
274  call assert_equal( '',      v:completed_item[ 'user_data' ] )
275
276  let s:called_completedone = 1
277endfunc
278
279func Test_CompleteDoneList()
280  au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
281
282  set completefunc=<SID>CompleteDone_CompleteFuncList
283  execute "normal a\<C-X>\<C-U>\<C-Y>"
284  set completefunc&
285
286  call assert_equal('', v:completed_item[ 'user_data' ])
287  call assert_true(s:called_completedone)
288
289  let s:called_completedone = 0
290  au! CompleteDone
291endfunc
292
293func Test_CompleteDone_undo()
294  au CompleteDone * call append(0, "prepend1")
295  new
296  call setline(1, ["line1", "line2"])
297  call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
298  call assert_equal(["prepend1", "line1", "line2", "line1", ""],
299              \     getline(1, '$'))
300  undo
301  call assert_equal(["line1", "line2"], getline(1, '$'))
302  bwipe!
303  au! CompleteDone
304endfunc
305
306" Check that when using feedkeys() typeahead does not interrupt searching for
307" completions.
308func Test_compl_feedkeys()
309  new
310  set completeopt=menuone,noselect
311  call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
312  call assert_equal("jump jump", getline(1))
313  bwipe!
314  set completeopt&
315endfunc
316