blob: e1c733f696cf95eb4aaca5b8383afaad244a6d14 [file] [log] [blame]
Bram Moolenaar15993ce2017-10-26 20:21:44 +02001
2" Test for insert expansion
3func Test_ins_complete()
Bram Moolenaarcf1ba352017-10-27 00:55:04 +02004 edit test_ins_complete.vim
Bram Moolenaarfb094e12017-11-05 20:59:28 +01005 " 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
Bram Moolenaar15993ce2017-10-26 20:21:44 +020010 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&
Bram Moolenaarfb094e12017-11-05 20:59:28 +010097 cd ..
98 call delete('Xdir', 'rf')
Bram Moolenaar15993ce2017-10-26 20:21:44 +020099endfunc
Bram Moolenaarffd99f72017-11-02 15:44:14 +0100100
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
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100119
Bram Moolenaarffa96842018-06-12 22:05:14 +0200120func 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')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200129 call assert_equal([1, 1], s:args[0])
130 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200131 set completefunc=
132
133 let s:args = []
134 set omnifunc=CompleteFunc
135 call feedkeys("i\<C-X>\<C-O>\<Esc>", 'x')
Bram Moolenaar52d3aae2018-06-13 21:27:24 +0200136 call assert_equal([1, 1], s:args[0])
137 call assert_equal(0, s:args[1][0])
Bram Moolenaarffa96842018-06-12 22:05:14 +0200138 set omnifunc=
139
140 bwipe!
141 unlet s:args
142 delfunc CompleteFunc
143endfunc
144
Bram Moolenaar1e115362019-01-09 23:01:02 +0100145func s:CompleteDone_CompleteFuncDict( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100146 if a:findstart
147 return 0
148 endif
149
150 return {
151 \ 'words': [
152 \ {
153 \ 'word': 'aword',
154 \ 'abbr': 'wrd',
155 \ 'menu': 'extra text',
156 \ 'info': 'words are cool',
157 \ 'kind': 'W',
158 \ 'user_data': 'test'
159 \ }
160 \ ]
161 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100162endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100163
Bram Moolenaar1e115362019-01-09 23:01:02 +0100164func s:CompleteDone_CheckCompletedItemDict()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100165 call assert_equal( 'aword', v:completed_item[ 'word' ] )
166 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
167 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
168 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
169 call assert_equal( 'W', v:completed_item[ 'kind' ] )
170 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
171
172 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100173endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100174
175function Test_CompleteDoneDict()
176 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDict()
177
178 set completefunc=<SID>CompleteDone_CompleteFuncDict
179 execute "normal a\<C-X>\<C-U>\<C-Y>"
180 set completefunc&
181
182 call assert_equal( 'test', v:completed_item[ 'user_data' ] )
183 call assert_true( s:called_completedone )
184
185 let s:called_completedone = 0
186 au! CompleteDone
187endfunc
188
Bram Moolenaar1e115362019-01-09 23:01:02 +0100189func s:CompleteDone_CompleteFuncDictNoUserData( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100190 if a:findstart
191 return 0
192 endif
193
194 return {
195 \ 'words': [
196 \ {
197 \ 'word': 'aword',
198 \ 'abbr': 'wrd',
199 \ 'menu': 'extra text',
200 \ 'info': 'words are cool',
201 \ 'kind': 'W'
202 \ }
203 \ ]
204 \ }
Bram Moolenaar1e115362019-01-09 23:01:02 +0100205endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100206
Bram Moolenaar1e115362019-01-09 23:01:02 +0100207func s:CompleteDone_CheckCompletedItemDictNoUserData()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100208 call assert_equal( 'aword', v:completed_item[ 'word' ] )
209 call assert_equal( 'wrd', v:completed_item[ 'abbr' ] )
210 call assert_equal( 'extra text', v:completed_item[ 'menu' ] )
211 call assert_equal( 'words are cool', v:completed_item[ 'info' ] )
212 call assert_equal( 'W', v:completed_item[ 'kind' ] )
213 call assert_equal( '', v:completed_item[ 'user_data' ] )
214
215 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100216endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100217
218function Test_CompleteDoneDictNoUserData()
219 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemDictNoUserData()
220
221 set completefunc=<SID>CompleteDone_CompleteFuncDictNoUserData
222 execute "normal a\<C-X>\<C-U>\<C-Y>"
223 set completefunc&
224
225 call assert_equal( '', v:completed_item[ 'user_data' ] )
226 call assert_true( s:called_completedone )
227
228 let s:called_completedone = 0
229 au! CompleteDone
230endfunc
231
Bram Moolenaar1e115362019-01-09 23:01:02 +0100232func s:CompleteDone_CompleteFuncList( findstart, base )
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100233 if a:findstart
234 return 0
235 endif
236
237 return [ 'aword' ]
Bram Moolenaar1e115362019-01-09 23:01:02 +0100238endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100239
Bram Moolenaar1e115362019-01-09 23:01:02 +0100240func s:CompleteDone_CheckCompletedItemList()
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100241 call assert_equal( 'aword', v:completed_item[ 'word' ] )
242 call assert_equal( '', v:completed_item[ 'abbr' ] )
243 call assert_equal( '', v:completed_item[ 'menu' ] )
244 call assert_equal( '', v:completed_item[ 'info' ] )
245 call assert_equal( '', v:completed_item[ 'kind' ] )
246 call assert_equal( '', v:completed_item[ 'user_data' ] )
247
248 let s:called_completedone = 1
Bram Moolenaar1e115362019-01-09 23:01:02 +0100249endfunc
Bram Moolenaar9b56a572018-02-10 16:19:32 +0100250
251function Test_CompleteDoneList()
252 au CompleteDone * :call <SID>CompleteDone_CheckCompletedItemList()
253
254 set completefunc=<SID>CompleteDone_CompleteFuncList
255 execute "normal a\<C-X>\<C-U>\<C-Y>"
256 set completefunc&
257
258 call assert_equal( '', v:completed_item[ 'user_data' ] )
259 call assert_true( s:called_completedone )
260
261 let s:called_completedone = 0
262 au! CompleteDone
263endfunc
264
Bram Moolenaaraf559d22018-08-08 22:55:41 +0200265func Test_CompleteDone_undo()
266 au CompleteDone * call append(0, "prepend1")
267 new
268 call setline(1, ["line1", "line2"])
269 call feedkeys("Go\<C-X>\<C-N>\<CR>\<ESC>", "tx")
270 call assert_equal(["prepend1", "line1", "line2", "line1", ""],
271 \ getline(1, '$'))
272 undo
273 call assert_equal(["line1", "line2"], getline(1, '$'))
274 bwipe!
275 au! CompleteDone
276endfunc
277
Bram Moolenaar02ae9b42018-02-09 15:06:02 +0100278" Check that when using feedkeys() typeahead does not interrupt searching for
279" completions.
280func Test_compl_feedkeys()
281 new
282 set completeopt=menuone,noselect
283 call feedkeys("ajump ju\<C-X>\<C-N>\<C-P>\<ESC>", "tx")
284 call assert_equal("jump jump", getline(1))
285 bwipe!
286 set completeopt&
287endfunc