-
-
Notifications
You must be signed in to change notification settings - Fork 691
Expand file tree
/
Copy pathtest_autocmd.vim
More file actions
5970 lines (5139 loc) · 176 KB
/
test_autocmd.vim
File metadata and controls
5970 lines (5139 loc) · 176 KB
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 autocommands
source util/screendump.vim
import './util/vim9.vim' as v9
func s:cleanup_buffers() abort
for bnr in range(1, bufnr('$'))
if bufloaded(bnr) && bufnr('%') != bnr
execute 'bd! ' . bnr
endif
endfor
endfunc
func CleanUpTestAuGroup()
augroup testing
au!
augroup END
augroup! testing
endfunc
func Test_vim_did_enter()
call assert_false(v:vim_did_enter)
" This script will never reach the main loop, can't check if v:vim_did_enter
" becomes one.
endfunc
" Test for the CursorHold autocmd
func Test_CursorHold_autocmd()
CheckRunVimInTerminal
call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
let before =<< trim END
set updatetime=10
au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
END
call writefile(before, 'XCHinit', 'D')
let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
call term_sendkeys(buf, "G")
call term_wait(buf, 50)
call term_sendkeys(buf, "gg")
call term_wait(buf)
call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
call term_sendkeys(buf, "j")
call term_wait(buf)
call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
call term_sendkeys(buf, "j")
call term_wait(buf)
call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
call StopVimInTerminal(buf)
call delete('XCHoutput')
endfunc
if has('timers')
func ExitInsertMode(id)
call feedkeys("\<Esc>")
endfunc
func Test_cursorhold_insert()
" depends on timing
let g:test_is_flaky = 1
" Need to move the cursor.
call feedkeys("ggG", "xt")
let g:triggered = 0
au CursorHoldI * let g:triggered += 1
set updatetime=20
call timer_start(200, 'ExitInsertMode')
call feedkeys('a', 'x!')
sleep 30m
call assert_equal(1, g:triggered)
unlet g:triggered
au! CursorHoldI
set updatetime&
endfunc
func Test_cursorhold_insert_with_timer_interrupt()
CheckFeature job
" Need to move the cursor.
call feedkeys("ggG", "xt")
" Confirm the timer invoked in exit_cb of the job doesn't disturb
" CursorHoldI event.
let g:triggered = 0
au CursorHoldI * let g:triggered += 1
set updatetime=100
call job_start(has('win32') ? 'cmd /D /c echo:' : 'echo',
\ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
call feedkeys('a', 'x!')
call assert_equal(1, g:triggered)
unlet g:triggered
au! CursorHoldI
set updatetime&
endfunc
func Test_cursorhold_insert_ctrl_x()
let g:triggered = 0
au CursorHoldI * let g:triggered += 1
set updatetime=20
call timer_start(200, 'ExitInsertMode')
" CursorHoldI does not trigger after CTRL-X
call feedkeys("a\<C-X>", 'x!')
call assert_equal(0, g:triggered)
unlet g:triggered
au! CursorHoldI
set updatetime&
endfunc
func Test_cursorhold_insert_ctrl_g_U()
au CursorHoldI * :
set updatetime=20
new
call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
call feedkeys("i()\<C-g>U", 'tx!')
sleep 200m
call assert_equal('(foo)', getline(1))
undo
call assert_equal('', getline(1))
bwipe!
au! CursorHoldI
set updatetime&
endfunc
func Test_OptionSet_modeline()
call test_override('starting', 1)
au! OptionSet
augroup set_tabstop
au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
augroup END
call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
set modeline
let v:errmsg = ''
call assert_fails('split XoptionsetModeline', 'E12:')
call assert_equal(7, &ts)
call assert_equal('', v:errmsg)
augroup set_tabstop
au!
augroup END
bwipe!
set ts&
call test_override('starting', 0)
endfunc
endif "has('timers')
func Test_bufunload()
augroup test_bufunload_group
autocmd!
autocmd BufUnload * call add(s:li, "bufunload")
autocmd BufDelete * call add(s:li, "bufdelete")
autocmd BufWipeout * call add(s:li, "bufwipeout")
augroup END
let s:li = []
new
setlocal bufhidden=
bunload
call assert_equal(["bufunload", "bufdelete"], s:li)
let s:li = []
new
setlocal bufhidden=delete
bunload
call assert_equal(["bufunload", "bufdelete"], s:li)
let s:li = []
new
setlocal bufhidden=unload
bwipeout
call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
au! test_bufunload_group
augroup! test_bufunload_group
endfunc
" SEGV occurs in older versions. (At least 7.4.2005 or older)
func Test_autocmd_bufunload_with_tabnext()
tabedit
tabfirst
augroup test_autocmd_bufunload_with_tabnext_group
autocmd!
autocmd BufUnload <buffer> tabnext
augroup END
quit
call assert_equal(2, tabpagenr('$'))
autocmd! test_autocmd_bufunload_with_tabnext_group
augroup! test_autocmd_bufunload_with_tabnext_group
tablast
quit
endfunc
func Test_argdelete_in_next()
au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
call assert_fails('next a b', 'E1156:')
au! BufNew,BufEnter,BufLeave,BufWinEnter *
endfunc
func Test_autocmd_bufwinleave_with_tabfirst()
tabedit
augroup sample
autocmd!
autocmd BufWinLeave <buffer> tabfirst
augroup END
call setline(1, ['a', 'b', 'c'])
edit! a.txt
tabclose
endfunc
" SEGV occurs in older versions. (At least 7.4.2321 or older)
func Test_autocmd_bufunload_avoiding_SEGV_01()
split aa.txt
let lastbuf = bufnr('$')
augroup test_autocmd_bufunload
autocmd!
exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
augroup END
call assert_fails('edit bb.txt', 'E937:')
autocmd! test_autocmd_bufunload
augroup! test_autocmd_bufunload
bwipe! aa.txt
bwipe! bb.txt
endfunc
" SEGV occurs in older versions. (At least 7.4.2321 or older)
func Test_autocmd_bufunload_avoiding_SEGV_02()
setlocal buftype=nowrite
let lastbuf = bufnr('$')
augroup test_autocmd_bufunload
autocmd!
exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
augroup END
normal! i1
call assert_fails('edit a.txt', 'E517:')
autocmd! test_autocmd_bufunload
augroup! test_autocmd_bufunload
bwipe! a.txt
endfunc
func Test_autocmd_dummy_wipeout()
" prepare files
call writefile([''], 'Xdummywipetest1.txt', 'D')
call writefile([''], 'Xdummywipetest2.txt', 'D')
augroup test_bufunload_group
autocmd!
autocmd BufUnload * call add(s:li, "bufunload")
autocmd BufDelete * call add(s:li, "bufdelete")
autocmd BufWipeout * call add(s:li, "bufwipeout")
augroup END
let s:li = []
split Xdummywipetest1.txt
silent! vimgrep /notmatched/ Xdummywipetest*
call assert_equal(["bufunload", "bufwipeout"], s:li)
bwipeout
au! test_bufunload_group
augroup! test_bufunload_group
endfunc
func Test_win_tab_autocmd()
let g:record = []
defer CleanUpTestAuGroup()
augroup testing
au WinNewPre * call add(g:record, 'WinNewPre')
au WinNew * call add(g:record, 'WinNew')
au WinClosed * call add(g:record, 'WinClosed')
au WinEnter * call add(g:record, 'WinEnter')
au WinLeave * call add(g:record, 'WinLeave')
au TabNew * call add(g:record, 'TabNew')
au TabClosed * call add(g:record, 'TabClosed')
au TabEnter * call add(g:record, 'TabEnter')
au TabLeave * call add(g:record, 'TabLeave')
augroup END
split
tabnew
close
close
call assert_equal([
\ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
\ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
\ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
\ 'WinLeave', 'WinClosed', 'WinEnter'
\ ], g:record)
let g:record = []
tabnew somefile
tabnext
bwipe somefile
call assert_equal([
\ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
\ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
\ 'WinClosed', 'TabClosed'
\ ], g:record)
let g:record = []
copen
help
tabnext
vnew
call assert_equal([
\ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
\ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter',
\ 'WinNewPre', 'WinLeave', 'WinNew', 'WinEnter'
\ ], g:record)
unlet g:record
endfunc
func Test_WinNewPre()
" Test that the old window layout can be accessed before a new window is created.
let g:layouts_pre = []
let g:layouts_post = []
augroup testing
au WinNewPre * call add(g:layouts_pre, winlayout())
au WinNew * call add(g:layouts_post, winlayout())
augroup END
defer CleanUpTestAuGroup()
split
call assert_notequal(g:layouts_pre[0], g:layouts_post[0])
split
call assert_equal(g:layouts_pre[1], g:layouts_post[0])
call assert_notequal(g:layouts_pre[1], g:layouts_post[1])
" not triggered for tabnew
tabnew
call assert_equal(2, len(g:layouts_pre))
unlet g:layouts_pre
unlet g:layouts_post
" Test modifying window layout during WinNewPre throws.
let g:caught = 0
augroup testing
au!
au WinNewPre * split
augroup END
try
vnew
catch
let g:caught += 1
endtry
augroup testing
au!
au WinNewPre * tabnew
augroup END
try
vnew
catch
let g:caught += 1
endtry
augroup testing
au!
au WinNewPre * close
augroup END
try
vnew
catch
let g:caught += 1
endtry
augroup testing
au!
au WinNewPre * tabclose
augroup END
try
vnew
catch
let g:caught += 1
endtry
call assert_equal(4, g:caught)
unlet g:caught
endfunc
func Test_WinResized()
CheckRunVimInTerminal
let lines =<< trim END
set scrolloff=0
call setline(1, ['111', '222'])
vnew
call setline(1, ['aaa', 'bbb'])
new
call setline(1, ['foo', 'bar'])
let g:resized = 0
au WinResized * let g:resized += 1
func WriteResizedEvent()
call writefile([json_encode(v:event)], 'XresizeEvent')
endfunc
au WinResized * call WriteResizedEvent()
END
call writefile(lines, 'Xtest_winresized', 'D')
let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
" redraw now to avoid a redraw after the :echo command
call term_sendkeys(buf, ":redraw!\<CR>")
call TermWait(buf)
call term_sendkeys(buf, ":echo g:resized\<CR>")
call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
" increase window height, two windows will be reported
call term_sendkeys(buf, "\<C-W>+")
call TermWait(buf)
call term_sendkeys(buf, ":echo g:resized\<CR>")
call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
let event = readfile('XresizeEvent')[0]->json_decode()
call assert_equal({
\ 'windows': [1002, 1001],
\ }, event)
" increase window width, three windows will be reported
call term_sendkeys(buf, "\<C-W>>")
call TermWait(buf)
call term_sendkeys(buf, ":echo g:resized\<CR>")
call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
let event = readfile('XresizeEvent')[0]->json_decode()
call assert_equal({
\ 'windows': [1002, 1001, 1000],
\ }, event)
call delete('XresizeEvent')
call StopVimInTerminal(buf)
endfunc
func Test_WinScrolled()
CheckRunVimInTerminal
let lines =<< trim END
set nowrap scrolloff=0
for ii in range(1, 18)
call setline(ii, repeat(nr2char(96 + ii), ii * 2))
endfor
let win_id = win_getid()
let g:matched = v:false
func WriteScrollEvent()
call writefile([json_encode(v:event)], 'XscrollEvent')
endfunc
execute 'au WinScrolled' win_id 'let g:matched = v:true'
let g:scrolled = 0
au WinScrolled * let g:scrolled += 1
au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
au WinScrolled * let g:afile = str2nr(expand('<afile>'))
au WinScrolled * call WriteScrollEvent()
END
call writefile(lines, 'Xtest_winscrolled', 'D')
let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
" Scroll left/right in Normal mode.
call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
let event = readfile('XscrollEvent')[0]->json_decode()
call assert_equal({
\ 'all': {'leftcol': 1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1000': {'leftcol': -1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
\ }, event)
" Scroll up/down in Normal mode.
call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
let event = readfile('XscrollEvent')[0]->json_decode()
call assert_equal({
\ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
\ }, event)
" Scroll up/down in Insert mode.
call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
let event = readfile('XscrollEvent')[0]->json_decode()
call assert_equal({
\ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
\ }, event)
" Scroll the window horizontally to focus the last letter of the third line
" containing only six characters. Moving to the previous and shorter lines
" should trigger another autocommand as Vim has to make them visible.
call term_sendkeys(buf, "5zl2k")
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
let event = readfile('XscrollEvent')[0]->json_decode()
call assert_equal({
\ 'all': {'leftcol': 5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1000': {'leftcol': -5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
\ }, event)
" Ensure the command was triggered for the specified window ID.
call term_sendkeys(buf, ":echo g:matched\<CR>")
call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
" Ensure the expansion of <amatch> and <afile> matches the window ID.
call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
call delete('XscrollEvent')
call StopVimInTerminal(buf)
endfunc
func Test_WinScrolled_mouse()
CheckRunVimInTerminal
let lines =<< trim END
set nowrap scrolloff=0
set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
call setline(1, ['foo']->repeat(32))
split
let g:scrolled = 0
au WinScrolled * let g:scrolled += 1
END
call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
" With the upper split focused, send a scroll-down event to the unfocused one.
call test_setmouse(7, 1)
call term_sendkeys(buf, "\<ScrollWheelDown>")
call TermWait(buf)
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
" Again, but this time while we're in insert mode.
call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
call TermWait(buf)
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
call StopVimInTerminal(buf)
endfunc
func Test_WinScrolled_close_curwin()
CheckRunVimInTerminal
let lines =<< trim END
set nowrap scrolloff=0
call setline(1, ['aaa', 'bbb'])
vsplit
au WinScrolled * close
au VimLeave * call writefile(['123456'], 'Xtestout')
END
call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
" This was using freed memory
call term_sendkeys(buf, "\<C-E>")
call TermWait(buf)
call StopVimInTerminal(buf)
" check the startup script finished to the end
call assert_equal(['123456'], readfile('Xtestout'))
call delete('Xtestout')
endfunc
func Test_WinScrolled_once_only()
CheckScreendump
CheckRunVimInTerminal
let lines =<< trim END
set cmdheight=2
call setline(1, ['aaa', 'bbb'])
let trigger_count = 0
func ShowInfo(id)
echo g:trigger_count g:winid winlayout()
endfunc
vsplit
split
" use a timer to show the info after a redraw
au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
wincmd j
wincmd l
END
call writefile(lines, 'Xtest_winscrolled_once', 'D')
let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
call term_sendkeys(buf, "\<C-E>")
call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
call StopVimInTerminal(buf)
endfunc
" Check that WinScrolled is not triggered immediately when defined and there
" are split windows.
func Test_WinScrolled_not_when_defined()
CheckScreendump
CheckRunVimInTerminal
let lines =<< trim END
call setline(1, ['aaa', 'bbb'])
echo 'nothing happened'
func ShowTriggered(id)
echo 'triggered'
endfunc
END
call writefile(lines, 'Xtest_winscrolled_not', 'D')
let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
call term_sendkeys(buf, ":split\<CR>")
call TermWait(buf)
" use a timer to show the message after redrawing
call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
call term_sendkeys(buf, "\<C-E>")
call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
call StopVimInTerminal(buf)
endfunc
func Test_WinScrolled_long_wrapped()
CheckRunVimInTerminal
let lines =<< trim END
set scrolloff=0
let height = winheight(0)
let width = winwidth(0)
let g:scrolled = 0
au WinScrolled * let g:scrolled += 1
call setline(1, repeat('foo', height * width))
call cursor(1, height * width)
END
call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
call term_sendkeys(buf, 'gj')
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
call term_sendkeys(buf, '0')
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
call term_sendkeys(buf, '$')
call term_sendkeys(buf, ":echo g:scrolled\<CR>")
call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
call StopVimInTerminal(buf)
endfunc
func Test_WinScrolled_diff()
CheckRunVimInTerminal
let lines =<< trim END
set diffopt+=foldcolumn:0
call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
vnew
call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
windo diffthis
func WriteScrollEvent()
call writefile([json_encode(v:event)], 'XscrollEvent')
endfunc
au WinScrolled * call WriteScrollEvent()
END
call writefile(lines, 'Xtest_winscrolled_diff', 'D')
let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
call term_sendkeys(buf, "\<C-E>")
call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
let event = readfile('XscrollEvent')[0]->json_decode()
call assert_equal({
\ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
\ }, event)
call term_sendkeys(buf, "2\<C-E>")
call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
let event = readfile('XscrollEvent')[0]->json_decode()
call assert_equal({
\ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
\ }, event)
call term_sendkeys(buf, "\<C-E>")
call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
let event = readfile('XscrollEvent')[0]->json_decode()
call assert_equal({
\ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
\ }, event)
call term_sendkeys(buf, "2\<C-Y>")
call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
let event = readfile('XscrollEvent')[0]->json_decode()
call assert_equal({
\ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
\ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
\ }, event)
call StopVimInTerminal(buf)
call delete('XscrollEvent')
endfunc
func Test_WinClosed()
" Test that the pattern is matched against the closed window's ID, and both
" <amatch> and <afile> are set to it.
new
let winid = win_getid()
let g:matched = v:false
augroup test-WinClosed
autocmd!
execute 'autocmd WinClosed' winid 'let g:matched = v:true'
autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
augroup END
close
call assert_true(g:matched)
call assert_equal(winid, g:amatch)
call assert_equal(winid, g:afile)
" Test that WinClosed is non-recursive.
new
new
call assert_equal(3, winnr('$'))
let g:triggered = 0
augroup test-WinClosed
autocmd!
autocmd WinClosed * let g:triggered += 1
autocmd WinClosed * 2 wincmd c
augroup END
close
call assert_equal(1, winnr('$'))
call assert_equal(1, g:triggered)
autocmd! test-WinClosed
augroup! test-WinClosed
unlet g:matched
unlet g:amatch
unlet g:afile
unlet g:triggered
endfunc
func Test_WinClosed_throws()
vnew
let bnr = bufnr()
call assert_equal(1, bufloaded(bnr))
augroup test-WinClosed
autocmd WinClosed * throw 'foo'
augroup END
try
close
catch /.*/
endtry
call assert_equal(0, bufloaded(bnr))
autocmd! test-WinClosed
augroup! test-WinClosed
endfunc
func Test_WinClosed_throws_with_tabs()
tabnew
let bnr = bufnr()
call assert_equal(1, bufloaded(bnr))
augroup test-WinClosed
autocmd WinClosed * throw 'foo'
augroup END
try
close
catch /.*/
endtry
call assert_equal(0, bufloaded(bnr))
autocmd! test-WinClosed
augroup! test-WinClosed
endfunc
" This used to trigger WinClosed twice for the same window, and the window's
" buffer was NULL in the second autocommand.
func Test_WinClosed_switch_tab()
edit Xa
split Xb
split Xc
tab split
new
augroup test-WinClosed
autocmd WinClosed * tabprev | bwipe!
augroup END
close
" Check that the tabline has been fully removed
call assert_equal([1, 1], win_screenpos(0))
autocmd! test-WinClosed
augroup! test-WinClosed
%bwipe!
endfunc
" This used to trigger WinClosed/WinLeave/BufLeave twice for the same window,
" and the window's buffer was NULL in the second autocommand.
func Run_test_BufUnload_close_other(extra_cmd)
let oldtab = tabpagenr()
tabnew Xb1
let g:tab = tabpagenr()
let g:w1 = win_getid()
new Xb2
let g:w2 = win_getid()
let g:log = []
exe a:extra_cmd
augroup test-BufUnload-close-other
autocmd BufUnload * ++nested ++once bwipe! Xb1
for event in ['WinClosed', 'BufLeave', 'WinLeave', 'TabLeave']
exe $'autocmd {event} * call tabpagebuflist(g:tab)'
exe $'autocmd {event} * let g:log += ["{event}:" .. expand("<afile>")]'
endfor
augroup END
close
" WinClosed is triggered once for each of the 2 closed windows.
" Others are only triggered once.
call assert_equal(['BufLeave:Xb2', 'WinLeave:Xb2', $'WinClosed:{g:w2}',
\ $'WinClosed:{g:w1}', 'TabLeave:Xb2'], g:log)
call assert_equal(oldtab, tabpagenr())
call assert_equal([0, 0], win_id2tabwin(g:w1))
call assert_equal([0, 0], win_id2tabwin(g:w2))
unlet g:tab
unlet g:w1
unlet g:w2
unlet g:log
autocmd! test-BufUnload-close-other
augroup! test-BufUnload-close-other
%bwipe!
endfunc
func Test_BufUnload_close_other()
call Run_test_BufUnload_close_other('')
call Run_test_BufUnload_close_other('setlocal bufhidden=wipe')
endfunc
func Run_test_BufUnload_tabonly(first_cmd)
exe a:first_cmd
tabnew Xa
setlocal bufhidden=wipe
tabprevious
autocmd BufWinLeave Xa ++once tabnext
autocmd BufUnload Xa ++once tabonly
tabonly
%bwipe!
endfunc
func Test_BufUnload_tabonly()
" This used to dereference a NULL curbuf.
call Run_test_BufUnload_tabonly('setlocal bufhidden=hide')
" This used to dereference a NULL firstbuf.
call Run_test_BufUnload_tabonly('setlocal bufhidden=wipe')
endfunc
func Run_test_BufUnload_tabonly_nested(second_autocmd)
file Xa
tabnew Xb
setlocal bufhidden=wipe
tabnew Xc
setlocal bufhidden=wipe
autocmd BufUnload Xb ++once ++nested bwipe! Xa
exe $'autocmd BufUnload Xa ++once ++nested {a:second_autocmd}'
autocmd BufWinLeave Xc ++once tabnext
tabfirst
2tabclose
%bwipe!
endfunc
func Test_BufUnload_tabonly_nested()
" These used to cause heap-use-after-free.
call Run_test_BufUnload_tabonly_nested('tabonly')
call Run_test_BufUnload_tabonly_nested('tabonly | tabprevious')
endfunc
func s:AddAnAutocmd()
augroup vimBarTest
au BufReadCmd * echo 'hello'
augroup END
call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
endfunc
func Test_early_bar()
" test that a bar is recognized before the {event}
call s:AddAnAutocmd()
augroup vimBarTest | au! | let done = 77 | augroup END
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
call assert_equal(77, done)
call s:AddAnAutocmd()
augroup vimBarTest| au!| let done = 88 | augroup END
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
call assert_equal(88, done)
" test that a bar is recognized after the {event}
call s:AddAnAutocmd()
augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
call assert_equal(99, done)
" test that a bar is recognized after the {group}
call s:AddAnAutocmd()
au! vimBarTest|echo 'hello'
call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
endfunc
func RemoveGroup()
autocmd! StartOK
augroup! StartOK
endfunc
func Test_augroup_warning()
augroup TheWarning
au VimEnter * echo 'entering'
augroup END
call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
redir => res
augroup! TheWarning
redir END
call assert_match("W19:", res)
call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
" check "Another" does not take the pace of the deleted entry
augroup Another
augroup END
call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
augroup! Another
" no warning for postpone aucmd delete
augroup StartOK
au VimEnter * call RemoveGroup()
augroup END
call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
redir => res
doautocmd VimEnter
redir END
call assert_notmatch("W19:", res)
au! VimEnter
call assert_fails('augroup!', 'E471:')
endfunc
func Test_BufReadCmdHelp()
" This used to cause access to free memory
au BufReadCmd * e +h
help
au! BufReadCmd
endfunc
func Test_BufReadCmdHelpJump()
" This used to cause access to free memory
au BufReadCmd * e +h{
" } to fix highlighting
call assert_fails('help', 'E434:')
au! BufReadCmd
endfunc
" BufReadCmd is triggered for a "nofile" buffer. Check all values.
func Test_BufReadCmdNofile()
for val in ['nofile',
\ 'nowrite',
\ 'acwrite',
\ 'quickfix',
\ 'help',
\ 'terminal',
\ 'prompt',
\ 'popup',
\ ]
new somefile
exe 'set buftype=' .. val