robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqMerge pull request #39689 from zeertzjq/vim-9.2.045191b9516d1b

master
28.3 KiB907 linesraw
1" Test findfile() and finddir()
2
3source check.vim
4source vim9.vim
5
6let s:files = [ 'Xfinddir1/foo',
7      \         'Xfinddir1/bar',
8      \         'Xfinddir1/Xdir2/foo',
9      \         'Xfinddir1/Xdir2/foobar',
10      \         'Xfinddir1/Xdir2/Xdir3/bar',
11      \         'Xfinddir1/Xdir2/Xdir3/barfoo' ]
12
13func CreateFiles()
14  call mkdir('Xfinddir1/Xdir2/Xdir3/Xdir2', 'p')
15  for f in s:files
16    call writefile([], f)
17  endfor
18endfunc
19
20func CleanFiles()
21  " Safer to delete each file even if it's more verbose
22  " than doing a recursive delete('Xfinddir1', 'rf').
23  for f in s:files
24    call delete(f)
25  endfor
26
27  call delete('Xfinddir1/Xdir2/Xdir3/Xdir2', 'd')
28  call delete('Xfinddir1/Xdir2/Xdir3', 'd')
29  call delete('Xfinddir1/Xdir2', 'd')
30  call delete('Xfinddir1', 'd')
31endfunc
32
33" Test findfile({name} [, {path} [, {count}]])
34func Test_findfile()
35  let save_path = &path
36  let save_shellslash = &shellslash
37  let save_dir = getcwd()
38  set shellslash
39  call CreateFiles()
40  cd Xfinddir1
41  e Xdir2/foo
42
43  " With ,, in path, findfile() searches in current directory.
44  set path=,,
45  call assert_equal('foo', findfile('foo'))
46  call assert_equal('bar', findfile('bar'))
47  call assert_equal('',    findfile('foobar'))
48
49  " Directories should not be found (finddir() finds them).
50  call assert_equal('', findfile('Xdir2'))
51
52  " With . in 'path', findfile() searches relatively to current file.
53  set path=.
54  call assert_equal('Xdir2/foo',    findfile('foo'))
55  call assert_equal('',             findfile('bar'))
56  call assert_equal('Xdir2/foobar', 'foobar'->findfile())
57
58  " Empty {path} 2nd argument is the same as no 2nd argument.
59  call assert_equal('Xdir2/foo', findfile('foo', ''))
60  call assert_equal('',          findfile('bar', ''))
61
62  " Test with *
63  call assert_equal('Xdir2/foo',       findfile('foo', '*'))
64  call assert_equal('',                findfile('bar', '*'))
65  call assert_equal('Xdir2/Xdir3/bar', findfile('bar', '*/*'))
66  call assert_equal('Xdir2/Xdir3/bar', findfile('bar', 'Xdir2/*'))
67  call assert_equal('Xdir2/Xdir3/bar', findfile('bar', 'Xdir*/Xdir3'))
68  call assert_equal('Xdir2/Xdir3/bar', findfile('bar', '*2/*3'))
69
70  " Test with **
71  call assert_equal('bar',             findfile('bar', '**'))
72  call assert_equal('Xdir2/Xdir3/bar', findfile('bar', '**/Xdir3'))
73  call assert_equal('Xdir2/Xdir3/bar', findfile('bar', 'Xdir2/**'))
74
75  call assert_equal('Xdir2/Xdir3/barfoo', findfile('barfoo', '**2'))
76  call assert_equal('',                   findfile('barfoo', '**1'))
77  call assert_equal('Xdir2/foobar',       findfile('foobar', '**1'))
78
79  " Test with {count} 3rd argument.
80  call assert_equal('bar',                      findfile('bar', '**', 0))
81  call assert_equal('bar',                      findfile('bar', '**', 1))
82  call assert_equal('Xdir2/Xdir3/bar',          findfile('bar', '**', 2))
83  call assert_equal('',                         findfile('bar', '**', 3))
84  call assert_equal(['bar', 'Xdir2/Xdir3/bar'], findfile('bar', '**', -1))
85
86  " Test upwards search.
87  cd Xdir2/Xdir3
88  call assert_equal('bar',                findfile('bar', ';'))
89  call assert_match('.*/Xfinddir1/Xdir2/foo', findfile('foo', ';'))
90  call assert_match('.*/Xfinddir1/Xdir2/foo', findfile('foo', ';', 1))
91  call assert_match('.*/Xfinddir1/foo',       findfile('foo', ';', 2))
92  call assert_match('.*/Xfinddir1/foo',       findfile('foo', ';', 2))
93  call assert_match('.*/Xfinddir1/Xdir2/foo', findfile('foo', 'Xdir2;', 1))
94  call assert_equal('',                   findfile('foo', 'Xdir2;', 2))
95
96  " List l should have at least 2 values (possibly more if foo file
97  " happens to be found upwards above Xfinddir1).
98  let l = findfile('foo', ';', -1)
99  call assert_match('.*/Xfinddir1/Xdir2/foo', l[0])
100  call assert_match('.*/Xfinddir1/foo',       l[1])
101
102  " Test upwards search with stop-directory.
103  cd Xdir2
104  let l = findfile('bar', ';' . save_dir . '/Xfinddir1/Xdir2/Xdir3/', -1)
105  call assert_equal(1, len(l))
106  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
107  let l = findfile('bar', ';' . save_dir . '/Xfinddir1/Xdir2/Xdir3', -1)
108  call assert_equal(1, len(l))
109  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
110  let l = findfile('bar', ';../', -1)
111  call assert_equal(1, len(l))
112  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
113  let l = findfile('bar', ';..', -1)
114  call assert_equal(1, len(l))
115  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
116
117  let l = findfile('bar', ';' . save_dir . '/Xfinddir1/Xdir2/', -1)
118  call assert_equal(1, len(l))
119  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
120  let l = findfile('bar', ';' . save_dir . '/Xfinddir1/Xdir2', -1)
121  call assert_equal(1, len(l))
122  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
123  let l = findfile('bar', ';../../', -1)
124  call assert_equal(1, len(l))
125  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
126  let l = findfile('bar', ';../..', -1)
127  call assert_equal(1, len(l))
128  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
129
130  let l = findfile('bar', ';' . save_dir . '/Xfinddir1/', -1)
131  call assert_equal(2, len(l))
132  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
133  call assert_match('.*/Xfinddir1/bar',             l[1])
134  let l = findfile('bar', ';' . save_dir . '/Xfinddir1', -1)
135  call assert_equal(2, len(l))
136  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
137  call assert_match('.*/Xfinddir1/bar',             l[1])
138  let l = findfile('bar', ';../../../', -1)
139  call assert_equal(2, len(l))
140  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
141  call assert_match('.*/Xfinddir1/bar',             l[1])
142  let l = findfile('bar', ';../../..', -1)
143  call assert_equal(2, len(l))
144  call assert_match('.*/Xfinddir1/Xdir2/Xdir3/bar', l[0])
145  call assert_match('.*/Xfinddir1/bar',             l[1])
146
147  " Test combined downwards and upwards search from Xdir2/.
148  cd ../..
149  call assert_equal('Xdir3/bar',    findfile('bar', '**;', 1))
150  call assert_match('.*/Xfinddir1/bar', findfile('bar', '**;', 2))
151
152  bwipe!
153  call chdir(save_dir)
154  call CleanFiles()
155  let &path = save_path
156  let &shellslash = save_shellslash
157endfunc
158
159func Test_findfile_error()
160  call assert_fails('call findfile([])', 'E730:')
161  call assert_fails('call findfile("x", [])', 'E730:')
162  call assert_fails('call findfile("x", "", [])', 'E745:')
163  call assert_fails('call findfile("x", "**x")', 'E343:')
164  call assert_fails('call findfile("x", repeat("x", 5000))', 'E854:')
165endfunc
166
167" Test finddir({name} [, {path} [, {count}]])
168func Test_finddir()
169  let save_path = &path
170  let save_shellslash = &shellslash
171  let save_dir = getcwd()
172  set path=,,
173  set shellslash
174  call CreateFiles()
175  cd Xfinddir1
176
177  call assert_equal('Xdir2', finddir('Xdir2'))
178  call assert_equal('',      'Xdir3'->finddir())
179
180  " Files should not be found (findfile() finds them).
181  call assert_equal('', finddir('foo'))
182
183  call assert_equal('Xdir2',       finddir('Xdir2', '**'))
184  call assert_equal('Xdir2/Xdir3', finddir('Xdir3', '**'))
185
186  call assert_equal('Xdir2',               finddir('Xdir2', '**', 1))
187  call assert_equal('Xdir2/Xdir3/Xdir2',   finddir('Xdir2', '**', 2))
188  call assert_equal(['Xdir2',
189        \            'Xdir2/Xdir3/Xdir2'], finddir('Xdir2', '**', -1))
190
191  call assert_equal('Xdir2',       finddir('Xdir2', '**1'))
192  call assert_equal('Xdir2',       finddir('Xdir2', '**0'))
193  call assert_equal('Xdir2/Xdir3', finddir('Xdir3', '**1'))
194  call assert_equal('',            finddir('Xdir3', '**0'))
195
196  " Test upwards dir search.
197  cd Xdir2/Xdir3
198  call assert_match('.*/Xfinddir1', finddir('Xfinddir1', ';'))
199
200  " Test upwards search with stop-directory.
201  call assert_match('.*/Xfinddir1', finddir('Xfinddir1', ';' . save_dir . '/'))
202  call assert_equal('',         finddir('Xfinddir1', ';' . save_dir . '/Xfinddir1/'))
203
204  " Test combined downwards and upwards dir search from Xdir2/.
205  cd ..
206  call assert_match('.*/Xfinddir1',       finddir('Xfinddir1', '**;', 1))
207  call assert_equal('Xdir3/Xdir2',    finddir('Xdir2', '**;', 1))
208  call assert_match('.*/Xfinddir1/Xdir2', finddir('Xdir2', '**;', 2))
209  call assert_equal('Xdir3',          finddir('Xdir3', '**;', 1))
210
211  call chdir(save_dir)
212  call CleanFiles()
213  let &path = save_path
214  let &shellslash = save_shellslash
215endfunc
216
217func Test_finddir_error()
218  call assert_fails('call finddir([])', 'E730:')
219  call assert_fails('call finddir("x", [])', 'E730:')
220  call assert_fails('call finddir("x", "", [])', 'E745:')
221  call assert_fails('call finddir("x", "**x")', 'E343:')
222  call assert_fails('call finddir("x", repeat("x", 5000))', 'E854:')
223endfunc
224
225func Test_findfile_with_suffixesadd()
226  let save_path = &path
227  let save_dir = getcwd()
228  set path=,,
229  call mkdir('Xfinddir1', 'pR')
230  cd Xfinddir1
231
232  call writefile([], 'foo.c', 'D')
233  call writefile([], 'bar.cpp', 'D')
234  call writefile([], 'baz.cc', 'D')
235  call writefile([], 'foo.o', 'D')
236  call writefile([], 'bar.o', 'D')
237  call writefile([], 'baz.o', 'D')
238
239  set suffixesadd=.c,.cpp
240  call assert_equal('foo.c', findfile('foo'))
241  call assert_equal('./foo.c', findfile('./foo'))
242  call assert_equal('bar.cpp', findfile('bar'))
243  call assert_equal('./bar.cpp', findfile('./bar'))
244  call assert_equal('', findfile('baz'))
245  call assert_equal('', findfile('./baz'))
246  set suffixesadd+=.cc
247  call assert_equal('baz.cc', findfile('baz'))
248  call assert_equal('./baz.cc', findfile('./baz'))
249
250  set suffixesadd&
251  call chdir(save_dir)
252  let &path = save_path
253endfunc
254
255" Test for the :find, :sfind and :tabfind commands
256func Test_find_cmd()
257  new
258  let save_path = &path
259  let save_dir = getcwd()
260  set path=.,./**/*
261  call CreateFiles()
262  cd Xfinddir1
263
264  " Test for :find
265  find foo
266  call assert_equal('foo', expand('%:.'))
267  2find foo
268  call assert_equal('Xdir2/foo', expand('%:.'))
269  call assert_fails('3find foo', 'E347:')
270
271  " Test for :sfind
272  enew
273  sfind barfoo
274  call assert_equal('Xdir2/Xdir3/barfoo', expand('%:.'))
275  call assert_equal(3, winnr('$'))
276  close
277  call assert_fails('sfind baz', 'E345:')
278  call assert_equal(2, winnr('$'))
279
280  " Test for :tabfind
281  enew
282  tabfind foobar
283  call assert_equal('Xdir2/foobar', expand('%:.'))
284  call assert_equal(2, tabpagenr('$'))
285  tabclose
286  call assert_fails('tabfind baz', 'E345:')
287  call assert_equal(1, tabpagenr('$'))
288
289  call chdir(save_dir)
290  exe 'cd ' . save_dir
291  call CleanFiles()
292  let &path = save_path
293  close
294
295  call assert_fails('find', 'E471:')
296  call assert_fails('sfind', 'E471:')
297  call assert_fails('tabfind', 'E471:')
298endfunc
299
300func Test_find_non_existing_path()
301  new
302  let save_path = &path
303  let save_dir = getcwd()
304  call mkdir('dir1/dir2', 'p')
305  call writefile([], 'dir1/file.txt')
306  call writefile([], 'dir1/dir2/base.txt')
307  call chdir('dir1/dir2')
308  e base.txt
309  set path=../include
310
311  call assert_fails(':find file.txt', 'E345:')
312
313  call chdir(save_dir)
314  bw!
315  call delete('dir1/dir2/base.txt', 'rf')
316  call delete('dir1/dir2', 'rf')
317  call delete('dir1/file.txt', 'rf')
318  call delete('dir1', 'rf')
319  let &path = save_path
320endfunc
321
322" Test for 'findfunc'
323func Test_findfunc()
324  CheckUnix
325  call assert_equal('', &findfunc)
326  call writefile(['aFile'], 'Xfindfunc1.c', 'D')
327  call writefile(['bFile'], 'Xfindfunc2.c', 'D')
328  call writefile(['cFile'], 'Xfindfunc3.c', 'D')
329
330  " basic tests
331  func FindFuncBasic(pat, cmdcomplete)
332    let fnames = ['Xfindfunc1.c', 'Xfindfunc2.c', 'Xfindfunc3.c']
333    return fnames->copy()->filter('v:val =~? a:pat')
334  endfunc
335
336  set findfunc=FindFuncBasic
337  find Xfindfunc3
338  call assert_match('Xfindfunc3\.c', @%)
339  bw!
340  2find Xfind
341  call assert_match('Xfindfunc2\.c', @%)
342  bw!
343  call assert_fails('4find Xfind', 'E347: No more file "Xfind" found in path')
344  call assert_fails('find foobar', 'E345: Can''t find file "foobar" in path')
345
346  sfind Xfindfunc2.c
347  call assert_match('Xfindfunc2\.c', @%)
348  call assert_equal(2, winnr('$'))
349  %bw!
350  call assert_fails('sfind foobar', 'E345: Can''t find file "foobar" in path')
351
352  tabfind Xfindfunc3.c
353  call assert_match('Xfindfunc3\.c', @%)
354  call assert_equal(2, tabpagenr())
355  %bw!
356  call assert_fails('tabfind foobar', 'E345: Can''t find file "foobar" in path')
357
358  " Test garbage collection
359  call test_garbagecollect_now()
360  find Xfindfunc2
361  call assert_match('Xfindfunc2\.c', @%)
362  bw!
363  delfunc FindFuncBasic
364  call test_garbagecollect_now()
365  call assert_fails('find Xfindfunc2', 'E117: Unknown function: FindFuncBasic')
366
367  " 'findfunc' with dicts in the returned list
368  func FindFuncDict(pat, cmdcomplete)
369    return [
370          \ #{word: 'Xfindfunc1.c', abbr: 'Xff1.c'},
371          \ #{word: 'Xfindfunc2.c'},
372          \ 'Xfindfunc3.c',
373          "\ invalid values
374          \ #{abbr: 'XXX'},
375          \ v:_null_dict,
376          \ v:_null_string,
377          \ ]
378  endfunc
379
380  set findfunc=FindFuncDict
381  find Xfind
382  call assert_match('Xfindfunc1\.c', @%)
383  bw!
384  2find Xfind
385  call assert_match('Xfindfunc2\.c', @%)
386  bw!
387  3find Xfind
388  call assert_match('Xfindfunc3\.c', @%)
389  bw!
390  " These invalid values should not crash
391  4find Xfind
392  5find Xfind
393  6find Xfind
394  call assert_fails('7find Xfind', 'E347: No more file "Xfind" found in path')
395  call assert_equal('', @%)
396  %bw!
397  delfunc FindFuncDict
398
399  " Buffer-local option
400  func GlobalFindFunc(pat, cmdcomplete)
401    return ['global']
402  endfunc
403  func LocalFindFunc(pat, cmdcomplete)
404    return ['local']
405  endfunc
406  set findfunc=GlobalFindFunc
407  new
408  setlocal findfunc=LocalFindFunc
409  find xxxx
410  call assert_equal('local', @%)
411  wincmd w
412  find xxxx
413  call assert_equal('global', @%)
414  aboveleft new
415  call assert_equal("GlobalFindFunc", &findfunc)
416  wincmd k
417  aboveleft new
418  call assert_equal("GlobalFindFunc", &findfunc)
419  %bw!
420  delfunc GlobalFindFunc
421  delfunc LocalFindFunc
422
423  " Assign an expression
424  set findfunc=[]
425  call assert_fails('find xxxx', 'E117: Unknown function: []')
426
427  " Error cases
428
429  " Function that doesn't take any arguments
430  func FindFuncNoArg()
431  endfunc
432  set findfunc=FindFuncNoArg
433  call assert_fails('find Xfindfunc1.c', 'E118: Too many arguments for function: FindFuncNoArg')
434  delfunc FindFuncNoArg
435
436  " Syntax error in the function
437  func FindFuncSyntaxError(pat, cmdcomplete)
438    return l
439  endfunc
440  set findfunc=FindFuncSyntaxError
441  call assert_fails('find Xfindfunc1.c', 'E121: Undefined variable: l')
442  delfunc FindFuncSyntaxError
443
444  " Find function throws an error
445  func FindFuncWithThrow(pat, cmdcomplete)
446    throw 'find error'
447  endfunc
448  set findfunc=FindFuncWithThrow
449  call assert_fails('find Xfindfunc1.c', 'find error')
450  delfunc FindFuncWithThrow
451
452  " Try using a null function
453  "call assert_fails('let &findfunc = test_null_function()', 'E129: Function name required')
454
455  " Try to create a new window from the find function
456  func FindFuncNewWindow(pat, cmdexpand)
457    new
458    return ["foo"]
459  endfunc
460  set findfunc=FindFuncNewWindow
461  call assert_fails('find Xfindfunc1.c', 'E565: Not allowed to change text or change window')
462  delfunc FindFuncNewWindow
463
464  " Try to modify the current buffer from the find function
465  func FindFuncModifyBuf(pat, cmdexpand)
466    call setline(1, ['abc'])
467    return ["foo"]
468  endfunc
469  set findfunc=FindFuncModifyBuf
470  call assert_fails('find Xfindfunc1.c', 'E565: Not allowed to change text or change window')
471  delfunc FindFuncModifyBuf
472
473  " Return the wrong type from the function
474  func FindFuncWrongRet(pat, cmdexpand)
475    return 'foo'
476  endfunc
477  set findfunc=FindFuncWrongRet
478  call assert_fails('find Xfindfunc1.c', "E1514: 'findfunc' did not return a List type")
479  delfunc FindFuncWrongRet
480
481  set findfunc&
482endfunc
483
484" Test for using a script-local function for 'findfunc'
485func Test_findfunc_scriptlocal_func()
486  func! s:FindFuncScript(pat, cmdexpand)
487    let g:FindFuncArg = a:pat
488    return ['xxx']
489  endfunc
490
491  set findfunc=s:FindFuncScript
492  call assert_equal(expand('<SID>') .. 'FindFuncScript', &findfunc)
493  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
494  new | only
495  let g:FindFuncArg = ''
496  find abc
497  call assert_equal('abc', g:FindFuncArg)
498  bw!
499
500  set findfunc=<SID>FindFuncScript
501  call assert_equal(expand('<SID>') .. 'FindFuncScript', &findfunc)
502  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
503  new | only
504  let g:FindFuncArg = ''
505  find abc
506  call assert_equal('abc', g:FindFuncArg)
507  bw!
508
509  let &findfunc = 's:FindFuncScript'
510  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
511  new | only
512  let g:FindFuncArg = ''
513  find abc
514  call assert_equal('abc', g:FindFuncArg)
515  bw!
516
517  let &findfunc = '<SID>FindFuncScript'
518  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
519  new | only
520  let g:FindFuncArg = ''
521  find abc
522  call assert_equal('abc', g:FindFuncArg)
523  bw!
524
525  set findfunc=
526  setglobal findfunc=s:FindFuncScript
527  setlocal findfunc=
528  call assert_equal(expand('<SID>') .. 'FindFuncScript', &findfunc)
529  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
530  call assert_equal('', &l:findfunc)
531  new | only
532  let g:FindFuncArg = ''
533  find abc
534  call assert_equal('abc', g:FindFuncArg)
535  bw!
536
537  new | only
538  set findfunc=
539  setglobal findfunc=
540  setlocal findfunc=s:FindFuncScript
541  call assert_equal(expand('<SID>') .. 'FindFuncScript', &findfunc)
542  call assert_equal(expand('<SID>') .. 'FindFuncScript', &l:findfunc)
543  call assert_equal('', &g:findfunc)
544  let g:FindFuncArg = ''
545  find abc
546  call assert_equal('abc', g:FindFuncArg)
547  bw!
548
549  new | only
550  set findfunc=
551  setlocal findfunc=NoSuchFunc
552  setglobal findfunc=s:FindFuncScript
553  call assert_equal('NoSuchFunc', &findfunc)
554  call assert_equal('NoSuchFunc', &l:findfunc)
555  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
556  new | only
557  call assert_equal(expand('<SID>') .. 'FindFuncScript', &findfunc)
558  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
559  call assert_equal('', &l:findfunc)
560  let g:FindFuncArg = ''
561  find abc
562  call assert_equal('abc', g:FindFuncArg)
563  bw!
564
565  new | only
566  set findfunc=
567  setlocal findfunc=NoSuchFunc
568  set findfunc=s:FindFuncScript
569  call assert_equal(expand('<SID>') .. 'FindFuncScript', &findfunc)
570  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
571  call assert_equal('', &l:findfunc)
572  let g:FindFuncArg = ''
573  find abc
574  call assert_equal('abc', g:FindFuncArg)
575  new | only
576  call assert_equal(expand('<SID>') .. 'FindFuncScript', &findfunc)
577  call assert_equal(expand('<SID>') .. 'FindFuncScript', &g:findfunc)
578  call assert_equal('', &l:findfunc)
579  let g:FindFuncArg = ''
580  find abc
581  call assert_equal('abc', g:FindFuncArg)
582  bw!
583
584  set findfunc=
585  delfunc s:FindFuncScript
586endfunc
587
588" Test for expanding the argument to the :find command using 'findfunc'
589func Test_findfunc_expand_arg()
590  let s:fnames = ['Xfindfunc1.c', 'Xfindfunc2.c', 'Xfindfunc3.c']
591
592  " 'findfunc' that accepts a regular expression
593  func FindFuncRegexp(pat, cmdcomplete)
594    return s:fnames->copy()->filter('v:val =~? a:pat')
595  endfunc
596
597  " 'findfunc' that accepts a glob
598  func FindFuncGlob(pat_arg, cmdcomplete)
599    let pat = glob2regpat(a:cmdcomplete ? $'*{a:pat_arg}*' : a:pat_arg)
600    return s:fnames->copy()->filter('v:val =~? pat')
601  endfunc
602
603  for regexp in [v:true, v:false]
604    let &findfunc = regexp ? 'FindFuncRegexp' : 'FindFuncGlob'
605
606    call feedkeys(":find \<Tab>\<C-B>\"\<CR>", "xt")
607    call assert_equal('"find Xfindfunc1.c', @:)
608
609    call feedkeys(":find Xfind\<Tab>\<Tab>\<C-B>\"\<CR>", "xt")
610    call assert_equal('"find Xfindfunc2.c', @:)
611
612    call assert_equal(s:fnames, getcompletion('find ', 'cmdline'))
613    call assert_equal(s:fnames, getcompletion('find Xfind', 'cmdline'))
614
615    let pat = regexp ? 'X.*1\.c' : 'X*1.c'
616    call feedkeys($":find {pat}\<Tab>\<C-B>\"\<CR>", "xt")
617    call assert_equal('"find Xfindfunc1.c', @:)
618    call assert_equal(['Xfindfunc1.c'], getcompletion($'find {pat}', 'cmdline'))
619
620    call feedkeys(":find 3\<Tab>\<C-B>\"\<CR>", "xt")
621    call assert_equal('"find Xfindfunc3.c', @:)
622    call assert_equal(['Xfindfunc3.c'], getcompletion($'find 3', 'cmdline'))
623
624    call feedkeys(":find Xfind\<C-A>\<C-B>\"\<CR>", "xt")
625    call assert_equal('"find Xfindfunc1.c Xfindfunc2.c Xfindfunc3.c', @:)
626
627    call feedkeys(":find abc\<Tab>\<C-B>\"\<CR>", "xt")
628    call assert_equal('"find abc', @:)
629    call assert_equal([], getcompletion('find abc', 'cmdline'))
630  endfor
631
632  set findfunc&
633  delfunc! FindFuncRegexp
634  delfunc! FindFuncGlob
635  unlet s:fnames
636endfunc
637
638" Test for different ways of setting the 'findfunc' option
639func Test_findfunc_callback()
640  new
641  func FindFunc1(pat, cmdexpand)
642    let g:FindFunc1Args = [a:pat, a:cmdexpand]
643    return ['findfunc1']
644  endfunc
645
646  let lines =<< trim END
647    #" Test for using a function name
648    LET &findfunc = 'g:FindFunc1'
649    LET g:FindFunc1Args = []
650    find abc1
651    call assert_equal(['abc1', v:false], g:FindFunc1Args)
652
653    #" Test for using a function()
654    set findfunc=function('g:FindFunc1')
655    LET g:FindFunc1Args = []
656    find abc2
657    call assert_equal(['abc2', v:false], g:FindFunc1Args)
658
659    #" Using a funcref variable to set 'findfunc'
660    VAR Fn = function('g:FindFunc1')
661    LET &findfunc = Fn
662    LET g:FindFunc1Args = []
663    find abc3
664    call assert_equal(['abc3', v:false], g:FindFunc1Args)
665
666    #" Using a string(funcref_variable) to set 'findfunc'
667    LET Fn = function('g:FindFunc1')
668    LET &findfunc = string(Fn)
669    LET g:FindFunc1Args = []
670    find abc4
671    call assert_equal(['abc4', v:false], g:FindFunc1Args)
672
673    #" Test for using a funcref()
674    set findfunc=funcref('g:FindFunc1')
675    LET g:FindFunc1Args = []
676    find abc5
677    call assert_equal(['abc5', v:false], g:FindFunc1Args)
678
679    #" Using a funcref variable to set 'findfunc'
680    LET Fn = funcref('g:FindFunc1')
681    LET &findfunc = Fn
682    LET g:FindFunc1Args = []
683    find abc6
684    call assert_equal(['abc6', v:false], g:FindFunc1Args)
685
686    #" Using a string(funcref_variable) to set 'findfunc'
687    LET Fn = funcref('g:FindFunc1')
688    LET &findfunc = string(Fn)
689    LET g:FindFunc1Args = []
690    find abc7
691    call assert_equal(['abc7', v:false], g:FindFunc1Args)
692
693    #" Test for using a lambda function using set
694    VAR optval = "LSTART pat, cmdexpand LMIDDLE FindFunc1(pat, cmdexpand) LEND"
695    LET optval = substitute(optval, ' ', '\\ ', 'g')
696    exe "set findfunc=" .. optval
697    LET g:FindFunc1Args = []
698    find abc8
699    call assert_equal(['abc8', v:false], g:FindFunc1Args)
700
701    #" Test for using a lambda function using LET
702    LET &findfunc = LSTART pat, _ LMIDDLE FindFunc1(pat, v:false) LEND
703    LET g:FindFunc1Args = []
704    find abc9
705    call assert_equal(['abc9', v:false], g:FindFunc1Args)
706
707    #" Set 'findfunc' to a string(lambda expression)
708    LET &findfunc = 'LSTART pat, _ LMIDDLE FindFunc1(pat, v:false) LEND'
709    LET g:FindFunc1Args = []
710    find abc10
711    call assert_equal(['abc10', v:false], g:FindFunc1Args)
712
713    #" Set 'findfunc' to a variable with a lambda expression
714    VAR Lambda = LSTART pat, _ LMIDDLE FindFunc1(pat, v:false) LEND
715    LET &findfunc = Lambda
716    LET g:FindFunc1Args = []
717    find abc11
718    call assert_equal(['abc11', v:false], g:FindFunc1Args)
719
720    #" Set 'findfunc' to a string(variable with a lambda expression)
721    LET Lambda = LSTART pat, _ LMIDDLE FindFunc1(pat, v:false) LEND
722    LET &findfunc = string(Lambda)
723    LET g:FindFunc1Args = []
724    find abc12
725    call assert_equal(['abc12', v:false], g:FindFunc1Args)
726
727    #" Try to use 'findfunc' after the function is deleted
728    func g:TmpFindFunc(pat, cmdexpand)
729      let g:TmpFindFunc1Args = [a:pat, a:cmdexpand]
730    endfunc
731    LET &findfunc = function('g:TmpFindFunc')
732    delfunc g:TmpFindFunc
733    call test_garbagecollect_now()
734    LET g:TmpFindFunc1Args = []
735    call assert_fails('find abc13', 'E117:')
736    call assert_equal([], g:TmpFindFunc1Args)
737
738    #" Try to use a function with three arguments for 'findfunc'
739    func g:TmpFindFunc2(x, y, z)
740      let g:TmpFindFunc2Args = [a:x, a:y, a:z]
741    endfunc
742    set findfunc=TmpFindFunc2
743    LET g:TmpFindFunc2Args = []
744    call assert_fails('find abc14', 'E119:')
745    call assert_equal([], g:TmpFindFunc2Args)
746    delfunc TmpFindFunc2
747
748    #" Try to use a function with zero arguments for 'findfunc'
749    func g:TmpFindFunc3()
750      let g:TmpFindFunc3Called = v:true
751    endfunc
752    set findfunc=TmpFindFunc3
753    LET g:TmpFindFunc3Called = v:false
754    call assert_fails('find abc15', 'E118:')
755    call assert_equal(v:false, g:TmpFindFunc3Called)
756    delfunc TmpFindFunc3
757
758    #" Try to use a lambda function with three arguments for 'findfunc'
759    LET &findfunc = LSTART a, b, c LMIDDLE FindFunc1(a, v:false) LEND
760    LET g:FindFunc1Args = []
761    call assert_fails('find abc16', 'E119:')
762    call assert_equal([], g:FindFunc1Args)
763
764    #" Test for clearing the 'findfunc' option
765    set findfunc=''
766    set findfunc&
767    call assert_fails("set findfunc=function('abc')", "E700:")
768    call assert_fails("set findfunc=funcref('abc')", "E700:")
769
770    #" set 'findfunc' to a non-existing function
771    LET &findfunc = function('g:FindFunc1')
772    call assert_fails("set findfunc=function('NonExistingFunc')", 'E700:')
773    call assert_fails("LET &findfunc = function('NonExistingFunc')", 'E700:')
774    LET g:FindFunc1Args = []
775    find abc17
776    call assert_equal(['abc17', v:false], g:FindFunc1Args)
777  END
778  call CheckTransLegacySuccess(lines)
779
780  " Test for using a script-local function name
781  func s:FindFunc2(pat, cmdexpand)
782    let g:FindFunc2Args = [a:pat, a:cmdexpand]
783    return ['findfunc2']
784  endfunc
785  set findfunc=s:FindFunc2
786  let g:FindFunc2Args = []
787  find abc18
788  call assert_equal(['abc18', v:false], g:FindFunc2Args)
789
790  let &findfunc = 's:FindFunc2'
791  let g:FindFunc2Args = []
792  find abc19
793  call assert_equal(['abc19', v:false], g:FindFunc2Args)
794  delfunc s:FindFunc2
795
796  " Using Vim9 lambda expression in legacy context should fail
797  set findfunc=(pat,\ cmdexpand)\ =>\ FindFunc1(pat,\ v:false)
798  let g:FindFunc1Args = []
799  call assert_fails('find abc20', 'E117:')
800  call assert_equal([], g:FindFunc1Args)
801
802  " set 'findfunc' to a partial with dict.
803  func SetFindFunc()
804    let operator = {'execute': function('FindFuncExecute')}
805    let &findfunc = operator.execute
806  endfunc
807  func FindFuncExecute(pat, cmdexpand) dict
808    return ['findfuncexecute']
809  endfunc
810  call SetFindFunc()
811  call test_garbagecollect_now()
812  set findfunc=
813  delfunc SetFindFunc
814  delfunc FindFuncExecute
815
816  func FindFunc2(pat, cmdexpand)
817    let g:FindFunc2Args = [a:pat, a:cmdexpand]
818    return ['findfunc2']
819  endfunc
820
821  " Vim9 tests
822  let lines =<< trim END
823    vim9script
824
825    def g:Vim9findFunc(pat: string, cmdexpand: bool): list<string>
826      g:FindFunc1Args = [pat, cmdexpand]
827      return ['vim9findfunc']
828    enddef
829
830    # Test for using a def function with findfunc
831    set findfunc=function('g:Vim9findFunc')
832    g:FindFunc1Args = []
833    find abc21
834    assert_equal(['abc21', false], g:FindFunc1Args)
835
836    # Test for using a global function name
837    &findfunc = g:FindFunc2
838    g:FindFunc2Args = []
839    find abc22
840    assert_equal(['abc22', false], g:FindFunc2Args)
841    bw!
842
843    # Test for using a script-local function name
844    def LocalFindFunc(pat: string, cmdexpand: bool): list<string>
845      g:LocalFindFuncArgs = [pat, cmdexpand]
846      return ['localfindfunc']
847    enddef
848    &findfunc = LocalFindFunc
849    g:LocalFindFuncArgs = []
850    find abc23
851    assert_equal(['abc23', false], g:LocalFindFuncArgs)
852    bw!
853  END
854  call CheckScriptSuccess(lines)
855
856  " setting 'findfunc' to a script local function outside of a script context
857  " should fail
858  let cleanup =<< trim END
859    call writefile([execute('messages')], 'Xtest.out')
860    qall
861  END
862  call writefile(cleanup, 'Xverify.vim', 'D')
863  call RunVim([], [], "-c \"set findfunc=s:abc\" -S Xverify.vim")
864  call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0])
865  call delete('Xtest.out')
866
867  " cleanup
868  set findfunc&
869  delfunc FindFunc1
870  delfunc FindFunc2
871  unlet g:FindFunc1Args g:FindFunc2Args
872  %bw!
873endfunc
874
875" Test using environment variables with spaces
876func Test_path_env_variable_with_whitespaces()
877    let save_path = &path
878    defer execute('let &path = save_path')
879
880    let $testdir = 'Xpath with some whites'
881    call mkdir($testdir, 'R')
882
883    " Check direct usage yields the same result that autocomplete
884    call feedkeys(':set path=$testdir' .. "\<C-A>\<CR>", 'xt')
885    let auto_testpath = &path
886    " include autocomplete suffix
887    exe "set path=$testdir" .. "/"
888    call assert_equal(auto_testpath, &path)
889
890    " Check a file can be found using environment variables
891    let expanded_test_path = expand('$testdir/test.txt')
892    call writefile(['testing...'], expanded_test_path)
893
894    " hinting an environment variable path
895    call assert_equal(expanded_test_path, findfile('test.txt', $test_dir))
896
897    " using 'path' option with an environment variable
898    set path=$testdir
899    call assert_equal(expanded_test_path, findfile('test.txt'))
900
901    " using :find instead of findfile()
902    find test.txt
903    call assert_equal(expanded_test_path, expand('%:.'))
904    enew
905endfunc
906
907" vim: shiftwidth=2 sts=2 expandtab