/
test_cmdline.vim
3380 lines (2925 loc) · 111 KB
/
test_cmdline.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" Tests for editing the command line.
source check.vim
source screendump.vim
source view_util.vim
source shared.vim
import './vim9.vim' as v9
func SetUp()
func SaveLastScreenLine()
let g:Sline = Screenline(&lines - 1)
return ''
endfunc
cnoremap <expr> <F4> SaveLastScreenLine()
endfunc
func TearDown()
delfunc SaveLastScreenLine
cunmap <F4>
endfunc
func Test_complete_tab()
call writefile(['testfile'], 'Xtestfile')
call feedkeys(":e Xtest\t\r", "tx")
call assert_equal('testfile', getline(1))
" Pressing <Tab> after '%' completes the current file, also on MS-Windows
call feedkeys(":e %\t\r", "tx")
call assert_equal('e Xtestfile', @:)
call delete('Xtestfile')
endfunc
func Test_complete_list()
" We can't see the output, but at least we check the code runs properly.
call feedkeys(":e test\<C-D>\r", "tx")
call assert_equal('test', expand('%:t'))
" If a command doesn't support completion, then CTRL-D should be literally
" used.
call feedkeys(":chistory \<C-D>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"chistory \<C-D>", @:)
" Test for displaying the tail of the completion matches
set wildmode=longest,full
call mkdir('Xtest')
call writefile([], 'Xtest/a.c')
call writefile([], 'Xtest/a.h')
let g:Sline = ''
call feedkeys(":e Xtest/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
call assert_equal('a.c a.h', g:Sline)
call assert_equal('"e Xtest/', @:)
if has('win32')
" Test for 'completeslash'
set completeslash=backslash
call feedkeys(":e Xtest\<Tab>\<C-B>\"\<CR>", 'xt')
call assert_equal('"e Xtest\', @:)
call feedkeys(":e Xtest/\<Tab>\<C-B>\"\<CR>", 'xt')
call assert_equal('"e Xtest\a.', @:)
set completeslash=slash
call feedkeys(":e Xtest\<Tab>\<C-B>\"\<CR>", 'xt')
call assert_equal('"e Xtest/', @:)
call feedkeys(":e Xtest\\\<Tab>\<C-B>\"\<CR>", 'xt')
call assert_equal('"e Xtest/a.', @:)
set completeslash&
endif
" Test for displaying the tail with wildcards
let g:Sline = ''
call feedkeys(":e Xtes?/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
call assert_equal('Xtest/a.c Xtest/a.h', g:Sline)
call assert_equal('"e Xtes?/', @:)
let g:Sline = ''
call feedkeys(":e Xtes*/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
call assert_equal('Xtest/a.c Xtest/a.h', g:Sline)
call assert_equal('"e Xtes*/', @:)
let g:Sline = ''
call feedkeys(":e Xtes[/\<C-D>\<F4>\<C-B>\"\<CR>", 'xt')
call assert_equal(':e Xtes[/', g:Sline)
call assert_equal('"e Xtes[/', @:)
call delete('Xtest', 'rf')
set wildmode&
endfunc
func Test_complete_wildmenu()
call mkdir('Xdir1/Xdir2', 'p')
call writefile(['testfile1'], 'Xdir1/Xtestfile1')
call writefile(['testfile2'], 'Xdir1/Xtestfile2')
call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile3')
call writefile(['testfile3'], 'Xdir1/Xdir2/Xtestfile4')
set wildmenu
" Pressing <Tab> completes, and moves to next files when pressing again.
call feedkeys(":e Xdir1/\<Tab>\<Tab>\<CR>", 'tx')
call assert_equal('testfile1', getline(1))
call feedkeys(":e Xdir1/\<Tab>\<Tab>\<Tab>\<CR>", 'tx')
call assert_equal('testfile2', getline(1))
" <S-Tab> is like <Tab> but begin with the last match and then go to
" previous.
call feedkeys(":e Xdir1/Xtest\<S-Tab>\<CR>", 'tx')
call assert_equal('testfile2', getline(1))
call feedkeys(":e Xdir1/Xtest\<S-Tab>\<S-Tab>\<CR>", 'tx')
call assert_equal('testfile1', getline(1))
" <Left>/<Right> to move to previous/next file.
call feedkeys(":e Xdir1/\<Tab>\<Right>\<CR>", 'tx')
call assert_equal('testfile1', getline(1))
call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<CR>", 'tx')
call assert_equal('testfile2', getline(1))
call feedkeys(":e Xdir1/\<Tab>\<Right>\<Right>\<Left>\<CR>", 'tx')
call assert_equal('testfile1', getline(1))
" <Up>/<Down> to go up/down directories.
call feedkeys(":e Xdir1/\<Tab>\<Down>\<CR>", 'tx')
call assert_equal('testfile3', getline(1))
call feedkeys(":e Xdir1/\<Tab>\<Down>\<Up>\<Right>\<CR>", 'tx')
call assert_equal('testfile1', getline(1))
" this fails in some Unix GUIs, not sure why
if !has('unix') || !has('gui_running')
" <C-J>/<C-K> mappings to go up/down directories when 'wildcharm' is
" different than 'wildchar'.
set wildcharm=<C-Z>
cnoremap <C-J> <Down><C-Z>
cnoremap <C-K> <Up><C-Z>
call feedkeys(":e Xdir1/\<Tab>\<C-J>\<CR>", 'tx')
call assert_equal('testfile3', getline(1))
call feedkeys(":e Xdir1/\<Tab>\<C-J>\<C-K>\<CR>", 'tx')
call assert_equal('testfile1', getline(1))
set wildcharm=0
cunmap <C-J>
cunmap <C-K>
endif
" Test for canceling the wild menu by adding a character
redrawstatus
call feedkeys(":e Xdir1/\<Tab>x\<C-B>\"\<CR>", 'xt')
call assert_equal('"e Xdir1/Xdir2/x', @:)
" Completion using a relative path
cd Xdir1/Xdir2
call feedkeys(":e ../\<Tab>\<Right>\<Down>\<C-A>\<C-B>\"\<CR>", 'tx')
call assert_equal('"e Xtestfile3 Xtestfile4', @:)
cd -
" test for wildmenumode()
cnoremap <expr> <F2> wildmenumode()
call feedkeys(":cd Xdir\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
call assert_equal('"cd Xdir1/0', @:)
call feedkeys(":e Xdir1/\<Tab>\<F2>\<C-B>\"\<CR>", 'tx')
call assert_equal('"e Xdir1/Xdir2/1', @:)
cunmap <F2>
" Test for canceling the wild menu by pressing <PageDown> or <PageUp>.
" After this pressing <Left> or <Right> should not change the selection.
call feedkeys(":sign \<Tab>\<PageDown>\<Left>\<Right>\<C-A>\<C-B>\"\<CR>", 'tx')
call assert_equal('"sign define', @:)
call histadd('cmd', 'TestWildMenu')
call feedkeys(":sign \<Tab>\<PageUp>\<Left>\<Right>\<C-A>\<C-B>\"\<CR>", 'tx')
call assert_equal('"TestWildMenu', @:)
" cleanup
%bwipe
call delete('Xdir1', 'rf')
set nowildmenu
endfunc
func Test_wildmenu_screendump()
CheckScreendump
let lines =<< trim [SCRIPT]
set wildmenu hlsearch
[SCRIPT]
call writefile(lines, 'XTest_wildmenu')
let buf = RunVimInTerminal('-S XTest_wildmenu', {'rows': 8})
call term_sendkeys(buf, ":vim\<Tab>")
call VerifyScreenDump(buf, 'Test_wildmenu_1', {})
call term_sendkeys(buf, "\<Tab>")
call VerifyScreenDump(buf, 'Test_wildmenu_2', {})
call term_sendkeys(buf, "\<Tab>")
call VerifyScreenDump(buf, 'Test_wildmenu_3', {})
call term_sendkeys(buf, "\<Tab>\<Tab>")
call VerifyScreenDump(buf, 'Test_wildmenu_4', {})
call term_sendkeys(buf, "\<Esc>")
" clean up
call StopVimInTerminal(buf)
call delete('XTest_wildmenu')
endfunc
func Test_redraw_in_autocmd()
CheckScreendump
let lines =<< trim END
set cmdheight=2
autocmd CmdlineChanged * redraw
END
call writefile(lines, 'XTest_redraw')
let buf = RunVimInTerminal('-S XTest_redraw', {'rows': 8})
call term_sendkeys(buf, ":for i in range(3)\<CR>")
call VerifyScreenDump(buf, 'Test_redraw_in_autocmd_1', {})
call term_sendkeys(buf, "let i =")
call VerifyScreenDump(buf, 'Test_redraw_in_autocmd_2', {})
" clean up
call term_sendkeys(buf, "\<CR>")
call StopVimInTerminal(buf)
call delete('XTest_redraw')
endfunc
func Test_map_completion()
call feedkeys(":map <unique> <si\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <unique> <silent>', getreg(':'))
call feedkeys(":map <script> <un\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <script> <unique>', getreg(':'))
call feedkeys(":map <expr> <sc\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <expr> <script>', getreg(':'))
call feedkeys(":map <buffer> <e\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <buffer> <expr>', getreg(':'))
call feedkeys(":map <nowait> <b\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <nowait> <buffer>', getreg(':'))
call feedkeys(":map <special> <no\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <special> <nowait>', getreg(':'))
call feedkeys(":map <silent> <sp\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <silent> <special>', getreg(':'))
map <Middle>x middle
map ,f commaf
map ,g commaf
map <Left> left
map <A-Left>x shiftleft
call feedkeys(":map ,\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map ,f', getreg(':'))
call feedkeys(":map ,\<Tab>\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map ,g', getreg(':'))
call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <Left>', getreg(':'))
call feedkeys(":map <A-Left>\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal("\"map <A-Left>\<Tab>", getreg(':'))
unmap ,f
unmap ,g
unmap <Left>
unmap <A-Left>x
set cpo-=< cpo-=B cpo-=k
map <Left> left
call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <Left>', getreg(':'))
call feedkeys(":map <M\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal("\"map <M\<Tab>", getreg(':'))
unmap <Left>
set cpo+=<
map <Left> left
exe "set t_k6=\<Esc>[17~"
call feedkeys(":map \<Esc>[17~x f6x\<CR>", 'xt')
call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <Left>', getreg(':'))
if !has('gui_running')
call feedkeys(":map \<Esc>[17~\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal("\"map <F6>x", getreg(':'))
endif
unmap <Left>
call feedkeys(":unmap \<Esc>[17~x\<CR>", 'xt')
set cpo-=<
set cpo+=B
map <Left> left
call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <Left>', getreg(':'))
unmap <Left>
set cpo-=B
set cpo+=k
map <Left> left
call feedkeys(":map <L\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"map <Left>', getreg(':'))
unmap <Left>
set cpo-=k
call assert_fails('call feedkeys(":map \\\\%(\<Tab>\<Home>\"\<CR>", "xt")', 'E53:')
unmap <Middle>x
set cpo&vim
endfunc
func Test_match_completion()
hi Aardig ctermfg=green
call feedkeys(":match \<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"match Aardig', @:)
call feedkeys(":match \<S-Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"match none', @:)
call feedkeys(":match | chist\<Tab>\<C-B>\"\<CR>", 'xt')
call assert_equal('"match | chistory', @:)
endfunc
func Test_highlight_completion()
hi Aardig ctermfg=green
call feedkeys(":hi \<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"hi Aardig', getreg(':'))
call feedkeys(":hi default \<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"hi default Aardig', getreg(':'))
call feedkeys(":hi clear Aa\<Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"hi clear Aardig', getreg(':'))
call feedkeys(":hi li\<S-Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"hi link', getreg(':'))
call feedkeys(":hi d\<S-Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"hi default', getreg(':'))
call feedkeys(":hi c\<S-Tab>\<Home>\"\<CR>", 'xt')
call assert_equal('"hi clear', getreg(':'))
call feedkeys(":hi clear Aardig Aard\<Tab>\<C-B>\"\<CR>", 'xt')
call assert_equal('"hi clear Aardig Aardig', getreg(':'))
call feedkeys(":hi Aardig \<Tab>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"hi Aardig \t", getreg(':'))
" A cleared group does not show up in completions.
hi Anders ctermfg=green
call assert_equal(['Aardig', 'Anders'], getcompletion('A', 'highlight'))
hi clear Aardig
call assert_equal(['Anders'], getcompletion('A', 'highlight'))
hi clear Anders
call assert_equal([], getcompletion('A', 'highlight'))
endfunc
" Test for command-line expansion of "hi Ni " (easter egg)
func Test_highlight_easter_egg()
call test_override('ui_delay', 1)
call feedkeys(":hi Ni \<Tab>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"hi Ni \<Tab>", @:)
call test_override('ALL', 0)
endfunc
func Test_getcompletion()
let groupcount = len(getcompletion('', 'event'))
call assert_true(groupcount > 0)
let matchcount = len('File'->getcompletion('event'))
call assert_true(matchcount > 0)
call assert_true(groupcount > matchcount)
if has('menu')
source $VIMRUNTIME/menu.vim
let matchcount = len(getcompletion('', 'menu'))
call assert_true(matchcount > 0)
call assert_equal(['File.'], getcompletion('File', 'menu'))
call assert_true(matchcount > 0)
let matchcount = len(getcompletion('File.', 'menu'))
call assert_true(matchcount > 0)
endif
let l = getcompletion('v:n', 'var')
call assert_true(index(l, 'v:null') >= 0)
let l = getcompletion('v:notexists', 'var')
call assert_equal([], l)
args a.c b.c
let l = getcompletion('', 'arglist')
call assert_equal(['a.c', 'b.c'], l)
let l = getcompletion('a.', 'buffer')
call assert_equal(['a.c'], l)
%argdelete
let l = getcompletion('', 'augroup')
call assert_true(index(l, 'END') >= 0)
let l = getcompletion('blahblah', 'augroup')
call assert_equal([], l)
let l = getcompletion('', 'behave')
call assert_true(index(l, 'mswin') >= 0)
let l = getcompletion('not', 'behave')
call assert_equal([], l)
let l = getcompletion('', 'color')
call assert_true(index(l, 'default') >= 0)
let l = getcompletion('dirty', 'color')
call assert_equal([], l)
let l = getcompletion('', 'command')
call assert_true(index(l, 'sleep') >= 0)
let l = getcompletion('awake', 'command')
call assert_equal([], l)
let l = getcompletion('', 'dir')
call assert_true(index(l, 'samples/') >= 0)
let l = getcompletion('NoMatch', 'dir')
call assert_equal([], l)
if glob('~/*') !=# ''
let l = getcompletion('~/', 'dir')
call assert_true(l[0][0] ==# '~')
endif
let l = getcompletion('exe', 'expression')
call assert_true(index(l, 'executable(') >= 0)
let l = getcompletion('kill', 'expression')
call assert_equal([], l)
let l = getcompletion('tag', 'function')
call assert_true(index(l, 'taglist(') >= 0)
let l = getcompletion('paint', 'function')
call assert_equal([], l)
let Flambda = {-> 'hello'}
let l = getcompletion('', 'function')
let l = filter(l, {i, v -> v =~ 'lambda'})
call assert_equal([], l)
let l = getcompletion('run', 'file')
call assert_true(index(l, 'runtest.vim') >= 0)
let l = getcompletion('walk', 'file')
call assert_equal([], l)
set wildignore=*.vim
let l = getcompletion('run', 'file', 1)
call assert_true(index(l, 'runtest.vim') < 0)
set wildignore&
" Directory name with space character
call mkdir('Xdir with space')
call assert_equal(['Xdir with space/'], getcompletion('Xdir\ w', 'shellcmd'))
call assert_equal(['./Xdir with space/'], getcompletion('./Xdir', 'shellcmd'))
call delete('Xdir with space', 'd')
let l = getcompletion('ha', 'filetype')
call assert_true(index(l, 'hamster') >= 0)
let l = getcompletion('horse', 'filetype')
call assert_equal([], l)
let l = getcompletion('z', 'syntax')
call assert_true(index(l, 'zimbu') >= 0)
let l = getcompletion('emacs', 'syntax')
call assert_equal([], l)
let l = getcompletion('jikes', 'compiler')
call assert_true(index(l, 'jikes') >= 0)
let l = getcompletion('break', 'compiler')
call assert_equal([], l)
let l = getcompletion('last', 'help')
call assert_true(index(l, ':tablast') >= 0)
let l = getcompletion('giveup', 'help')
call assert_equal([], l)
let l = getcompletion('time', 'option')
call assert_true(index(l, 'timeoutlen') >= 0)
let l = getcompletion('space', 'option')
call assert_equal([], l)
let l = getcompletion('er', 'highlight')
call assert_true(index(l, 'ErrorMsg') >= 0)
let l = getcompletion('dark', 'highlight')
call assert_equal([], l)
let l = getcompletion('', 'messages')
call assert_true(index(l, 'clear') >= 0)
let l = getcompletion('not', 'messages')
call assert_equal([], l)
let l = getcompletion('', 'mapclear')
call assert_true(index(l, '<buffer>') >= 0)
let l = getcompletion('not', 'mapclear')
call assert_equal([], l)
let l = getcompletion('.', 'shellcmd')
call assert_equal(['./', '../'], filter(l, 'v:val =~ "\\./"'))
call assert_equal(-1, match(l[2:], '^\.\.\?/$'))
let root = has('win32') ? 'C:\\' : '/'
let l = getcompletion(root, 'shellcmd')
let expected = map(filter(glob(root . '*', 0, 1),
\ 'isdirectory(v:val) || executable(v:val)'), 'isdirectory(v:val) ? v:val . ''/'' : v:val')
call assert_equal(expected, l)
if has('cscope')
let l = getcompletion('', 'cscope')
let cmds = ['add', 'find', 'help', 'kill', 'reset', 'show']
call assert_equal(cmds, l)
" using cmdline completion must not change the result
call feedkeys(":cscope find \<c-d>\<c-c>", 'xt')
let l = getcompletion('', 'cscope')
call assert_equal(cmds, l)
let keys = ['a', 'c', 'd', 'e', 'f', 'g', 'i', 's', 't']
let l = getcompletion('find ', 'cscope')
call assert_equal(keys, l)
endif
if has('signs')
sign define Testing linehl=Comment
let l = getcompletion('', 'sign')
let cmds = ['define', 'jump', 'list', 'place', 'undefine', 'unplace']
call assert_equal(cmds, l)
" using cmdline completion must not change the result
call feedkeys(":sign list \<c-d>\<c-c>", 'xt')
let l = getcompletion('', 'sign')
call assert_equal(cmds, l)
let l = getcompletion('list ', 'sign')
call assert_equal(['Testing'], l)
let l = getcompletion('de*', 'sign')
call assert_equal(['define'], l)
let l = getcompletion('p?', 'sign')
call assert_equal(['place'], l)
let l = getcompletion('j.', 'sign')
call assert_equal(['jump'], l)
endif
" Command line completion tests
let l = getcompletion('cd ', 'cmdline')
call assert_true(index(l, 'samples/') >= 0)
let l = getcompletion('cd NoMatch', 'cmdline')
call assert_equal([], l)
let l = getcompletion('let v:n', 'cmdline')
call assert_true(index(l, 'v:null') >= 0)
let l = getcompletion('let v:notexists', 'cmdline')
call assert_equal([], l)
let l = getcompletion('call tag', 'cmdline')
call assert_true(index(l, 'taglist(') >= 0)
let l = getcompletion('call paint', 'cmdline')
call assert_equal([], l)
func T(a, c, p)
let g:cmdline_compl_params = [a:a, a:c, a:p]
return "oneA\noneB\noneC"
endfunc
command -nargs=1 -complete=custom,T MyCmd
let l = getcompletion('MyCmd ', 'cmdline')
call assert_equal(['oneA', 'oneB', 'oneC'], l)
call assert_equal(['', 'MyCmd ', 6], g:cmdline_compl_params)
delcommand MyCmd
delfunc T
unlet g:cmdline_compl_params
" For others test if the name is recognized.
let names = ['buffer', 'environment', 'file_in_path', 'mapping', 'tag', 'tag_listfiles', 'user']
if has('cmdline_hist')
call add(names, 'history')
endif
if has('gettext')
call add(names, 'locale')
endif
if has('profile')
call add(names, 'syntime')
endif
set tags=Xtags
call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//", "word\tfile\tcmd"], 'Xtags')
for name in names
let matchcount = len(getcompletion('', name))
call assert_true(matchcount >= 0, 'No matches for ' . name)
endfor
call delete('Xtags')
set tags&
edit a~b
enew
call assert_equal(['a~b'], getcompletion('a~', 'buffer'))
bw a~b
if has('unix')
edit Xtest\
enew
call assert_equal(['Xtest\'], getcompletion('Xtest\', 'buffer'))
bw Xtest\
endif
call assert_fails("call getcompletion('\\\\@!\\\\@=', 'buffer')", 'E871:')
call assert_fails('call getcompletion("", "burp")', 'E475:')
call assert_fails('call getcompletion("abc", [])', 'E475:')
endfunc
" Test for getcompletion() with "fuzzy" in 'wildoptions'
func Test_getcompletion_wildoptions()
let save_wildoptions = &wildoptions
set wildoptions&
let l = getcompletion('space', 'option')
call assert_equal([], l)
let l = getcompletion('ier', 'command')
call assert_equal([], l)
set wildoptions=fuzzy
let l = getcompletion('space', 'option')
call assert_true(index(l, 'backspace') >= 0)
let l = getcompletion('ier', 'command')
call assert_true(index(l, 'compiler') >= 0)
let &wildoptions = save_wildoptions
endfunc
func Test_complete_autoload_error()
let save_rtp = &rtp
let lines =<< trim END
vim9script
export def Complete(..._): string
return 'match'
enddef
echo this will cause an error
END
call mkdir('Xdir/autoload', 'p')
call writefile(lines, 'Xdir/autoload/script.vim')
exe 'set rtp+=' .. getcwd() .. '/Xdir'
let lines =<< trim END
vim9script
import autoload 'script.vim'
command -nargs=* -complete=custom,script.Complete Cmd eval 0 + 0
&wildcharm = char2nr("\<Tab>")
feedkeys(":Cmd \<Tab>", 'xt')
END
call v9.CheckScriptFailure(lines, 'E121: Undefined variable: this')
let &rtp = save_rtp
call delete('Xdir', 'rf')
endfunc
func Test_fullcommand()
let tests = {
\ '': '',
\ ':': '',
\ ':::': '',
\ ':::5': '',
\ 'not_a_cmd': '',
\ 'Check': '',
\ 'syntax': 'syntax',
\ ':syntax': 'syntax',
\ '::::syntax': 'syntax',
\ 'sy': 'syntax',
\ 'syn': 'syntax',
\ 'synt': 'syntax',
\ ':sy': 'syntax',
\ '::::sy': 'syntax',
\ 'match': 'match',
\ '2match': 'match',
\ '3match': 'match',
\ 'aboveleft': 'aboveleft',
\ 'abo': 'aboveleft',
\ 's': 'substitute',
\ '5s': 'substitute',
\ ':5s': 'substitute',
\ "'<,'>s": 'substitute',
\ ":'<,'>s": 'substitute',
\ 'CheckLin': 'CheckLinux',
\ 'CheckLinux': 'CheckLinux',
\ }
for [in, want] in items(tests)
call assert_equal(want, fullcommand(in))
endfor
call assert_equal('', fullcommand(test_null_string()))
call assert_equal('syntax', 'syn'->fullcommand())
command -buffer BufferLocalCommand :
command GlobalCommand :
call assert_equal('GlobalCommand', fullcommand('GlobalCom'))
call assert_equal('BufferLocalCommand', fullcommand('BufferL'))
delcommand BufferLocalCommand
delcommand GlobalCommand
endfunc
func Test_shellcmd_completion()
let save_path = $PATH
call mkdir('Xpathdir/Xpathsubdir', 'p')
call writefile([''], 'Xpathdir/Xfile.exe')
call setfperm('Xpathdir/Xfile.exe', 'rwx------')
" Set PATH to example directory without trailing slash.
let $PATH = getcwd() . '/Xpathdir'
" Test for the ":!<TAB>" case. Previously, this would include subdirs of
" dirs in the PATH, even though they won't be executed. We check that only
" subdirs of the PWD and executables from the PATH are included in the
" suggestions.
let actual = getcompletion('X', 'shellcmd')
let expected = map(filter(glob('*', 0, 1), 'isdirectory(v:val) && v:val[0] == "X"'), 'v:val . "/"')
call insert(expected, 'Xfile.exe')
call assert_equal(expected, actual)
call delete('Xpathdir', 'rf')
let $PATH = save_path
endfunc
func Test_expand_star_star()
call mkdir('a/b', 'p')
call writefile(['asdfasdf'], 'a/b/fileXname')
call feedkeys(":find **/fileXname\<Tab>\<CR>", 'xt')
call assert_equal('find a/b/fileXname', @:)
bwipe!
call delete('a', 'rf')
endfunc
func Test_cmdline_paste()
let @a = "def"
call feedkeys(":abc \<C-R>a ghi\<C-B>\"\<CR>", 'tx')
call assert_equal('"abc def ghi', @:)
new
call setline(1, 'asdf.x /tmp/some verylongword a;b-c*d ')
call feedkeys(":aaa \<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
call assert_equal('"aaa asdf bbb', @:)
call feedkeys("ft:aaa \<C-R>\<C-F> bbb\<C-B>\"\<CR>", 'tx')
call assert_equal('"aaa /tmp/some bbb', @:)
call feedkeys(":aaa \<C-R>\<C-L> bbb\<C-B>\"\<CR>", 'tx')
call assert_equal('"aaa '.getline(1).' bbb', @:)
set incsearch
call feedkeys("fy:aaa veryl\<C-R>\<C-W> bbb\<C-B>\"\<CR>", 'tx')
call assert_equal('"aaa verylongword bbb', @:)
call feedkeys("f;:aaa \<C-R>\<C-A> bbb\<C-B>\"\<CR>", 'tx')
call assert_equal('"aaa a;b-c*d bbb', @:)
call feedkeys(":\<C-\>etoupper(getline(1))\<CR>\<C-B>\"\<CR>", 'tx')
call assert_equal('"ASDF.X /TMP/SOME VERYLONGWORD A;B-C*D ', @:)
bwipe!
" Error while typing a command used to cause that it was not executed
" in the end.
new
try
call feedkeys(":file \<C-R>%Xtestfile\<CR>", 'tx')
catch /^Vim\%((\a\+)\)\=:E32/
" ignore error E32
endtry
call assert_equal("Xtestfile", bufname("%"))
" Try to paste an invalid register using <C-R>
call feedkeys(":\"one\<C-R>\<C-X>two\<CR>", 'xt')
call assert_equal('"onetwo', @:)
" Test for pasting register containing CTRL-H using CTRL-R and CTRL-R CTRL-R
let @a = "xy\<C-H>z"
call feedkeys(":\"\<C-R>a\<CR>", 'xt')
call assert_equal('"xz', @:)
call feedkeys(":\"\<C-R>\<C-R>a\<CR>", 'xt')
call assert_equal("\"xy\<C-H>z", @:)
call feedkeys(":\"\<C-R>\<C-O>a\<CR>", 'xt')
call assert_equal("\"xy\<C-H>z", @:)
" Test for pasting register containing CTRL-V using CTRL-R and CTRL-R CTRL-R
let @a = "xy\<C-V>z"
call feedkeys(":\"\<C-R>=@a\<CR>\<cr>", 'xt')
call assert_equal('"xyz', @:)
call feedkeys(":\"\<C-R>\<C-R>=@a\<CR>\<cr>", 'xt')
call assert_equal("\"xy\<C-V>z", @:)
call assert_beeps('call feedkeys(":\<C-R>=\<C-R>=\<Esc>", "xt")')
bwipe!
endfunc
func Test_cmdline_remove_char()
let encoding_save = &encoding
for e in ['utf8', 'latin1']
exe 'set encoding=' . e
call feedkeys(":abc def\<S-Left>\<Del>\<C-B>\"\<CR>", 'tx')
call assert_equal('"abc ef', @:, e)
call feedkeys(":abc def\<S-Left>\<BS>\<C-B>\"\<CR>", 'tx')
call assert_equal('"abcdef', @:)
call feedkeys(":abc def ghi\<S-Left>\<C-W>\<C-B>\"\<CR>", 'tx')
call assert_equal('"abc ghi', @:, e)
call feedkeys(":abc def\<S-Left>\<C-U>\<C-B>\"\<CR>", 'tx')
call assert_equal('"def', @:, e)
endfor
let &encoding = encoding_save
endfunc
func Test_cmdline_keymap_ctrl_hat()
CheckFeature keymap
set keymap=esperanto
call feedkeys(":\"Jxauxdo \<C-^>Jxauxdo \<C-^>Jxauxdo\<CR>", 'tx')
call assert_equal('"Jxauxdo Ĵaŭdo Jxauxdo', @:)
set keymap=
endfunc
func Test_illegal_address1()
new
2;'(
2;')
quit
endfunc
func Test_illegal_address2()
call writefile(['c', 'x', ' x', '.', '1;y'], 'Xtest.vim')
new
source Xtest.vim
" Trigger calling validate_cursor()
diffsp Xtest.vim
quit!
bwipe!
call delete('Xtest.vim')
endfunc
func Test_cmdline_complete_wildoptions()
help
call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
let a = join(sort(split(@:)),' ')
set wildoptions=tagfile
call feedkeys(":tag /\<c-a>\<c-b>\"\<cr>", 'tx')
let b = join(sort(split(@:)),' ')
call assert_equal(a, b)
bw!
endfunc
func Test_cmdline_complete_user_cmd()
command! -complete=color -nargs=1 Foo :
call feedkeys(":Foo \<Tab>\<Home>\"\<cr>", 'tx')
call assert_equal('"Foo blue', @:)
call feedkeys(":Foo b\<Tab>\<Home>\"\<cr>", 'tx')
call assert_equal('"Foo blue', @:)
call feedkeys(":Foo a b\<Tab>\<Home>\"\<cr>", 'tx')
call assert_equal('"Foo a blue', @:)
call feedkeys(":Foo b\\\<Tab>\<Home>\"\<cr>", 'tx')
call assert_equal('"Foo b\', @:)
call feedkeys(":Foo b\\x\<Tab>\<Home>\"\<cr>", 'tx')
call assert_equal('"Foo b\x', @:)
delcommand Foo
endfunc
func s:ScriptLocalFunction()
echo 'yes'
endfunc
func Test_cmdline_complete_user_func()
call feedkeys(":func Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
call assert_match('"func Test_cmdline_complete_user_', @:)
call feedkeys(":func s:ScriptL\<Tab>\<Home>\"\<cr>", 'tx')
call assert_match('"func <SNR>\d\+_ScriptLocalFunction', @:)
" g: prefix also works
call feedkeys(":echo g:Test_cmdline_complete_user_f\<Tab>\<Home>\"\<cr>", 'tx')
call assert_match('"echo g:Test_cmdline_complete_user_func', @:)
" using g: prefix does not result in just "g:" matches from a lambda
let Fx = { a -> a }
call feedkeys(":echo g:\<Tab>\<Home>\"\<cr>", 'tx')
call assert_match('"echo g:[A-Z]', @:)
" existence of script-local dict function does not break user function name
" completion
function s:a_dict_func() dict
endfunction
call feedkeys(":call Test_cmdline_complete_user\<Tab>\<Home>\"\<cr>", 'tx')
call assert_match('"call Test_cmdline_complete_user_', @:)
delfunction s:a_dict_func
endfunc
func Test_cmdline_complete_user_names()
if has('unix') && executable('whoami')
let whoami = systemlist('whoami')[0]
let first_letter = whoami[0]
if len(first_letter) > 0
" Trying completion of :e ~x where x is the first letter of
" the user name should complete to at least the user name.
call feedkeys(':e ~' . first_letter . "\<c-a>\<c-B>\"\<cr>", 'tx')
call assert_match('^"e \~.*\<' . whoami . '\>', @:)
endif
elseif has('win32')
" Just in case: check that the system has an Administrator account.
let names = system('net user')
if names =~ 'Administrator'
" Trying completion of :e ~A should complete to Administrator.
" There could be other names starting with "A" before Administrator.
call feedkeys(':e ~A' . "\<c-a>\<c-B>\"\<cr>", 'tx')
call assert_match('^"e \~.*Administrator', @:)
endif
else
throw 'Skipped: does not work on this platform'
endif
endfunc
func Test_cmdline_complete_bang()
CheckExecutable whoami
call feedkeys(":!whoam\<C-A>\<C-B>\"\<CR>", 'tx')
call assert_match('^".*\<whoami\>', @:)
endfunc
func Test_cmdline_complete_languages()
let lang = substitute(execute('language time'), '.*"\(.*\)"$', '\1', '')
call assert_equal(lang, v:lc_time)
let lang = substitute(execute('language ctype'), '.*"\(.*\)"$', '\1', '')
call assert_equal(lang, v:ctype)
let lang = substitute(execute('language collate'), '.*"\(.*\)"$', '\1', '')
call assert_equal(lang, v:collate)
let lang = substitute(execute('language messages'), '.*"\(.*\)"$', '\1', '')
call assert_equal(lang, v:lang)
call feedkeys(":language \<c-a>\<c-b>\"\<cr>", 'tx')
call assert_match('^"language .*\<collate\>.*\<ctype\>.*\<messages\>.*\<time\>', @:)
call assert_match('^"language .*\<' . lang . '\>', @:)
call feedkeys(":language messages \<c-a>\<c-b>\"\<cr>", 'tx')
call assert_match('^"language .*\<' . lang . '\>', @:)
call feedkeys(":language ctype \<c-a>\<c-b>\"\<cr>", 'tx')
call assert_match('^"language .*\<' . lang . '\>', @:)
call feedkeys(":language time \<c-a>\<c-b>\"\<cr>", 'tx')
call assert_match('^"language .*\<' . lang . '\>', @:)
call feedkeys(":language collate \<c-a>\<c-b>\"\<cr>", 'tx')
call assert_match('^"language .*\<' . lang . '\>', @:)
endfunc
func Test_cmdline_complete_env_variable()
let $X_VIM_TEST_COMPLETE_ENV = 'foo'
call feedkeys(":edit $X_VIM_TEST_COMPLETE_E\<C-A>\<C-B>\"\<CR>", 'tx')
call assert_match('"edit $X_VIM_TEST_COMPLETE_ENV', @:)
unlet $X_VIM_TEST_COMPLETE_ENV
endfunc
func Test_cmdline_complete_expression()
let g:SomeVar = 'blah'
for cmd in ['exe', 'echo', 'echon', 'echomsg']
call feedkeys(":" .. cmd .. " SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
call assert_match('"' .. cmd .. ' SomeVar', @:)
call feedkeys(":" .. cmd .. " foo SomeV\<Tab>\<C-B>\"\<CR>", 'tx')
call assert_match('"' .. cmd .. ' foo SomeVar', @:)
endfor
unlet g:SomeVar
endfunc
" Unique function name for completion below
func s:WeirdFunc()
echo 'weird'
endfunc
" Test for various command-line completion
func Test_cmdline_complete_various()
" completion for a command starting with a comment
call feedkeys(": :|\"\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\" :|\"\<C-A>", @:)
" completion for a range followed by a comment
call feedkeys(":1,2\"\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"1,2\"\<C-A>", @:)
" completion for :k command
call feedkeys(":ka\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"ka\<C-A>", @:)
" completion for short version of the :s command
call feedkeys(":sI \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"sI \<C-A>", @:)
" completion for :write command
call mkdir('Xdir')
call writefile(['one'], 'Xdir/Xfile1')
let save_cwd = getcwd()
cd Xdir
call feedkeys(":w >> \<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"w >> Xfile1", @:)
call chdir(save_cwd)
call delete('Xdir', 'rf')
" completion for :w ! and :r ! commands
call feedkeys(":w !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"w !invalid_xyz_cmd", @:)
call feedkeys(":r !invalid_xyz_cmd\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"r !invalid_xyz_cmd", @:)
" completion for :>> and :<< commands
call feedkeys(":>>>\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\">>>\<C-A>", @:)
call feedkeys(":<<<\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"<<<\<C-A>", @:)
" completion for command with +cmd argument
call feedkeys(":buffer +/pat Xabc\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"buffer +/pat Xabc", @:)
call feedkeys(":buffer +/pat\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"buffer +/pat\<C-A>", @:)
" completion for a command with a trailing comment
call feedkeys(":ls \" comment\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"ls \" comment\<C-A>", @:)
" completion for a command with a trailing command
call feedkeys(":ls | ls\<C-A>\<C-B>\"\<CR>", 'xt')
call assert_equal("\"ls | ls", @:)
" completion for a command with an CTRL-V escaped argument