1" Tests for the terminal window.
2" This is split in two, because it can take a lot of time.
3" See test_terminal2.vim and test_terminal3.vim for further tests.
4
5source check.vim
6CheckFeature terminal
7
8source shared.vim
9source screendump.vim
10source mouse.vim
11source term_util.vim
12
13let s:python = PythonProg()
14let $PROMPT_COMMAND=''
15
16func Test_terminal_basic()
17  au TerminalOpen * let b:done = 'yes'
18  let buf = Run_shell_in_terminal({})
19
20  call assert_equal('t', mode())
21  call assert_equal('yes', b:done)
22  call assert_match('%aR[^\n]*running]', execute('ls'))
23  call assert_match('%aR[^\n]*running]', execute('ls R'))
24  call assert_notmatch('%[^\n]*running]', execute('ls F'))
25  call assert_notmatch('%[^\n]*running]', execute('ls ?'))
26  call assert_fails('set modifiable', 'E946:')
27
28  call StopShellInTerminal(buf)
29  call TermWait(buf)
30  call assert_equal('n', mode())
31  call assert_match('%aF[^\n]*finished]', execute('ls'))
32  call assert_match('%aF[^\n]*finished]', execute('ls F'))
33  call assert_notmatch('%[^\n]*finished]', execute('ls R'))
34  call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
35
36  " closing window wipes out the terminal buffer a with finished job
37  close
38  call assert_equal("", bufname(buf))
39
40  au! TerminalOpen
41  unlet g:job
42endfunc
43
44func Test_terminal_no_name()
45  let buf = Run_shell_in_terminal({})
46  call assert_match('^!', bufname(buf))
47  0file
48  call assert_equal("", bufname(buf))
49  call assert_match('\[No Name\]', execute('file'))
50  call StopShellInTerminal(buf)
51  call TermWait(buf)
52endfunc
53
54func Test_terminal_TerminalWinOpen()
55  au TerminalWinOpen * let b:done = 'yes'
56  let buf = Run_shell_in_terminal({})
57  call assert_equal('yes', b:done)
58  call StopShellInTerminal(buf)
59  " closing window wipes out the terminal buffer with the finished job
60  close
61
62  if has("unix")
63    terminal ++hidden ++open sleep 1
64    sleep 1
65    call assert_fails("echo b:done", 'E121:')
66  endif
67
68  au! TerminalWinOpen
69endfunc
70
71func Test_terminal_make_change()
72  let buf = Run_shell_in_terminal({})
73  call StopShellInTerminal(buf)
74  call TermWait(buf)
75
76  setlocal modifiable
77  exe "normal Axxx\<Esc>"
78  call assert_fails(buf . 'bwipe', 'E89:')
79  undo
80
81  exe buf . 'bwipe'
82  unlet g:job
83endfunc
84
85func Test_terminal_paste_register()
86  let @" = "text to paste"
87
88  let buf = Run_shell_in_terminal({})
89  " Wait for the shell to display a prompt
90  call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
91
92  call feedkeys("echo \<C-W>\"\" \<C-W>\"=37 + 5\<CR>\<CR>", 'xt')
93  call WaitForAssert({-> assert_match("echo text to paste 42$", getline(1))})
94  call WaitForAssert({-> assert_equal('text to paste 42',       2->getline())})
95
96  exe buf . 'bwipe!'
97  unlet g:job
98endfunc
99
100func Test_terminal_wipe_buffer()
101  let buf = Run_shell_in_terminal({})
102  call assert_fails(buf . 'bwipe', 'E89:')
103  exe buf . 'bwipe!'
104  call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
105  call assert_equal("", bufname(buf))
106
107  unlet g:job
108endfunc
109
110func Test_terminal_split_quit()
111  let buf = Run_shell_in_terminal({})
112  call TermWait(buf)
113  split
114  quit!
115  call TermWait(buf)
116  sleep 50m
117  call assert_equal('run', job_status(g:job))
118
119  quit!
120  call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
121
122  exe buf . 'bwipe'
123  unlet g:job
124endfunc
125
126func Test_terminal_hide_buffer_job_running()
127  let buf = Run_shell_in_terminal({})
128  setlocal bufhidden=hide
129  quit
130  for nr in range(1, winnr('$'))
131    call assert_notequal(winbufnr(nr), buf)
132  endfor
133  call assert_true(bufloaded(buf))
134  call assert_true(buflisted(buf))
135
136  exe 'split ' . buf . 'buf'
137  call StopShellInTerminal(buf)
138  exe buf . 'bwipe'
139
140  unlet g:job
141endfunc
142
143func Test_terminal_hide_buffer_job_finished()
144  term echo hello
145  let buf = bufnr()
146  setlocal bufhidden=hide
147  call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
148  call assert_true(bufloaded(buf))
149  call assert_true(buflisted(buf))
150  edit Xasdfasdf
151  call assert_true(bufloaded(buf))
152  call assert_true(buflisted(buf))
153  exe buf .. 'buf'
154  call assert_equal(buf, bufnr())
155  setlocal bufhidden=
156  edit Xasdfasdf
157  call assert_false(bufloaded(buf))
158  call assert_false(buflisted(buf))
159  bwipe Xasdfasdf
160endfunc
161
162func s:Nasty_exit_cb(job, st)
163  exe g:buf . 'bwipe!'
164  let g:buf = 0
165endfunc
166
167func Get_cat_123_cmd()
168  if has('win32')
169    if !has('conpty')
170      return 'cmd /c "cls && color 2 && echo 123"'
171    else
172      " When clearing twice, extra sequence is not output.
173      return 'cmd /c "cls && cls && color 2 && echo 123"'
174    endif
175  else
176    call writefile(["\<Esc>[32m123"], 'Xtext')
177    return "cat Xtext"
178  endif
179endfunc
180
181func Test_terminal_nasty_cb()
182  let cmd = Get_cat_123_cmd()
183  let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
184  let g:job = term_getjob(g:buf)
185
186  call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
187  call WaitForAssert({-> assert_equal(0, g:buf)})
188  unlet g:job
189  unlet g:buf
190  call delete('Xtext')
191endfunc
192
193func Check_123(buf)
194  let l = term_scrape(a:buf, 0)
195  call assert_true(len(l) == 0)
196  let l = term_scrape(a:buf, 999)
197  call assert_true(len(l) == 0)
198  let l = a:buf->term_scrape(1)
199  call assert_true(len(l) > 0)
200  call assert_equal('1', l[0].chars)
201  call assert_equal('2', l[1].chars)
202  call assert_equal('3', l[2].chars)
203  call assert_equal('#00e000', l[0].fg)
204  call assert_equal(0, term_getattr(l[0].attr, 'bold'))
205  call assert_equal(0, l[0].attr->term_getattr('italic'))
206  if has('win32')
207    " On Windows 'background' always defaults to dark, even though the terminal
208    " may use a light background.  Therefore accept both white and black.
209    call assert_match('#ffffff\|#000000', l[0].bg)
210  else
211    if &background == 'light'
212      call assert_equal('#ffffff', l[0].bg)
213    else
214      call assert_equal('#000000', l[0].bg)
215    endif
216  endif
217
218  let l = term_getline(a:buf, -1)
219  call assert_equal('', l)
220  let l = term_getline(a:buf, 0)
221  call assert_equal('', l)
222  let l = term_getline(a:buf, 999)
223  call assert_equal('', l)
224  let l = term_getline(a:buf, 1)
225  call assert_equal('123', l)
226endfunc
227
228func Test_terminal_scrape_123()
229  let cmd = Get_cat_123_cmd()
230  let buf = term_start(cmd)
231
232  let termlist = term_list()
233  call assert_equal(1, len(termlist))
234  call assert_equal(buf, termlist[0])
235
236  " Nothing happens with invalid buffer number
237  call term_wait(1234)
238
239  call TermWait(buf)
240  " On MS-Windows we first get a startup message of two lines, wait for the
241  " "cls" to happen, after that we have one line with three characters.
242  call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
243  call Check_123(buf)
244
245  " Must still work after the job ended.
246  let job = term_getjob(buf)
247  call WaitForAssert({-> assert_equal("dead", job_status(job))})
248  call TermWait(buf)
249  call Check_123(buf)
250
251  exe buf . 'bwipe'
252  call delete('Xtext')
253endfunc
254
255func Test_terminal_scrape_multibyte()
256  call writefile(["léttまrs"], 'Xtext')
257  if has('win32')
258    " Run cmd with UTF-8 codepage to make the type command print the expected
259    " multibyte characters.
260    let buf = term_start("cmd /K chcp 65001")
261    call term_sendkeys(buf, "type Xtext\<CR>")
262    eval buf->term_sendkeys("exit\<CR>")
263    let line = 4
264  else
265    let buf = term_start("cat Xtext")
266    let line = 1
267  endif
268
269  call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
270  let l = term_scrape(buf, line)
271  call assert_true(len(l) >= 7)
272  call assert_equal('l', l[0].chars)
273  call assert_equal('é', l[1].chars)
274  call assert_equal(1, l[1].width)
275  call assert_equal('t', l[2].chars)
276  call assert_equal('t', l[3].chars)
277  call assert_equal('ま', l[4].chars)
278  call assert_equal(2, l[4].width)
279  call assert_equal('r', l[5].chars)
280  call assert_equal('s', l[6].chars)
281
282  let job = term_getjob(buf)
283  call WaitForAssert({-> assert_equal("dead", job_status(job))})
284  call TermWait(buf)
285
286  exe buf . 'bwipe'
287  call delete('Xtext')
288endfunc
289
290func Test_terminal_one_column()
291  " This creates a terminal, displays a double-wide character and makes the
292  " window one column wide.  This used to cause a crash.
293  let width = &columns
294  botright vert term
295  let buf = bufnr('$')
296  call TermWait(buf, 100)
297  exe "set columns=" .. (width / 2)
298  redraw
299  call term_sendkeys(buf, "キ")
300  call TermWait(buf, 10)
301  exe "set columns=" .. width
302  exe buf . 'bwipe!'
303endfunc
304
305func Test_terminal_scroll()
306  call writefile(range(1, 200), 'Xtext')
307  if has('win32')
308    let cmd = 'cmd /c "type Xtext"'
309  else
310    let cmd = "cat Xtext"
311  endif
312  let buf = term_start(cmd)
313
314  let job = term_getjob(buf)
315  call WaitForAssert({-> assert_equal("dead", job_status(job))})
316  call TermWait(buf)
317
318  " wait until the scrolling stops
319  while 1
320    let scrolled = buf->term_getscrolled()
321    sleep 20m
322    if scrolled == buf->term_getscrolled()
323      break
324    endif
325  endwhile
326
327  call assert_equal('1', getline(1))
328  call assert_equal('1', term_getline(buf, 1 - scrolled))
329  call assert_equal('49', getline(49))
330  call assert_equal('49', term_getline(buf, 49 - scrolled))
331  call assert_equal('200', getline(200))
332  call assert_equal('200', term_getline(buf, 200 - scrolled))
333
334  exe buf . 'bwipe'
335  call delete('Xtext')
336endfunc
337
338func Test_terminal_scrollback()
339  let buf = Run_shell_in_terminal({'term_rows': 15})
340  set termwinscroll=100
341  call writefile(range(150), 'Xtext')
342  if has('win32')
343    call term_sendkeys(buf, "type Xtext\<CR>")
344  else
345    call term_sendkeys(buf, "cat Xtext\<CR>")
346  endif
347  let rows = term_getsize(buf)[0]
348  " On MS-Windows there is an empty line, check both last line and above it.
349  call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
350  let lines = line('$')
351  call assert_inrange(91, 100, lines)
352
353  call StopShellInTerminal(buf)
354  call TermWait(buf)
355  exe buf . 'bwipe'
356  set termwinscroll&
357  call delete('Xtext')
358endfunc
359
360func Test_terminal_postponed_scrollback()
361  " tail -f only works on Unix
362  CheckUnix
363
364  call writefile(range(50), 'Xtext')
365  call writefile([
366	\ 'set shell=/bin/sh noruler',
367	\ 'terminal',
368	\ 'sleep 200m',
369	\ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
370	\ 'sleep 100m',
371	\ 'call feedkeys("\<C-W>N", "xt")',
372	\ ], 'XTest_postponed')
373  let buf = RunVimInTerminal('-S XTest_postponed', {})
374  " Check that the Xtext lines are displayed and in Terminal-Normal mode
375  call VerifyScreenDump(buf, 'Test_terminal_scrollback_1', {})
376
377  silent !echo 'one more line' >>Xtext
378  " Screen will not change, move cursor to get a different dump
379  call term_sendkeys(buf, "k")
380  call VerifyScreenDump(buf, 'Test_terminal_scrollback_2', {})
381
382  " Back to Terminal-Job mode, text will scroll and show the extra line.
383  call term_sendkeys(buf, "a")
384  call VerifyScreenDump(buf, 'Test_terminal_scrollback_3', {})
385
386  " stop "tail -f"
387  call term_sendkeys(buf, "\<C-C>")
388  call TermWait(buf, 25)
389  " stop shell
390  call term_sendkeys(buf, "exit\<CR>")
391  call TermWait(buf, 50)
392  " close terminal window
393  let tsk_ret = term_sendkeys(buf, ":q\<CR>")
394
395  " check type of term_sendkeys() return value
396  echo type(tsk_ret)
397
398  call StopVimInTerminal(buf)
399  call delete('XTest_postponed')
400  call delete('Xtext')
401endfunc
402
403" Run diff on two dumps with different size.
404func Test_terminal_dumpdiff_size()
405  call assert_equal(1, winnr('$'))
406  call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
407  call assert_equal(2, winnr('$'))
408  call assert_match('Test_incsearch_search_01.dump', getline(10))
409  call assert_match('      +++++$', getline(11))
410  call assert_match('Test_popup_command_01.dump', getline(31))
411  call assert_equal(repeat('+', 75), getline(30))
412  quit
413endfunc
414
415func Test_terminal_size()
416  let cmd = Get_cat_123_cmd()
417
418  exe 'terminal ++rows=5 ' . cmd
419  let size = term_getsize('')
420  bwipe!
421  call assert_equal(5, size[0])
422
423  call term_start(cmd, {'term_rows': 6})
424  let size = term_getsize('')
425  bwipe!
426  call assert_equal(6, size[0])
427
428  vsplit
429  exe 'terminal ++rows=5 ++cols=33 ' . cmd
430  call assert_equal([5, 33], ''->term_getsize())
431
432  call term_setsize('', 6, 0)
433  call assert_equal([6, 33], term_getsize(''))
434
435  eval ''->term_setsize(0, 35)
436  call assert_equal([6, 35], term_getsize(''))
437
438  call term_setsize('', 7, 30)
439  call assert_equal([7, 30], term_getsize(''))
440
441  bwipe!
442  call assert_fails("call term_setsize('', 7, 30)", "E955:")
443
444  call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
445  let size = term_getsize('')
446  bwipe!
447  call assert_equal([6, 36], size)
448
449  exe 'vertical terminal ++cols=20 ' . cmd
450  let size = term_getsize('')
451  bwipe!
452  call assert_equal(20, size[1])
453
454  eval cmd->term_start({'vertical': 1, 'term_cols': 26})
455  let size = term_getsize('')
456  bwipe!
457  call assert_equal(26, size[1])
458
459  split
460  exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
461  let size = term_getsize('')
462  bwipe!
463  call assert_equal([6, 20], size)
464
465  call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
466  let size = term_getsize('')
467  bwipe!
468  call assert_equal([7, 27], size)
469
470  call delete('Xtext')
471endfunc
472
473func Test_terminal_zero_height()
474  split
475  wincmd j
476  anoremenu 1.1 WinBar.test :
477  terminal ++curwin
478  wincmd k
479  wincmd _
480  redraw
481
482  call term_sendkeys(bufnr(), "exit\r")
483  bwipe!
484endfunc
485
486func Test_terminal_curwin()
487  let cmd = Get_cat_123_cmd()
488  call assert_equal(1, winnr('$'))
489
490  split Xdummy
491  call setline(1, 'dummy')
492  write
493  call assert_equal(1, getbufinfo('Xdummy')[0].loaded)
494  exe 'terminal ++curwin ' . cmd
495  call assert_equal(2, winnr('$'))
496  call assert_equal(0, getbufinfo('Xdummy')[0].loaded)
497  bwipe!
498
499  split Xdummy
500  call term_start(cmd, {'curwin': 1})
501  call assert_equal(2, winnr('$'))
502  bwipe!
503
504  split Xdummy
505  call setline(1, 'change')
506  call assert_fails('terminal ++curwin ' . cmd, 'E37:')
507  call assert_equal(2, winnr('$'))
508  exe 'terminal! ++curwin ' . cmd
509  call assert_equal(2, winnr('$'))
510  bwipe!
511
512  split Xdummy
513  call setline(1, 'change')
514  call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
515  call assert_equal(2, winnr('$'))
516  bwipe!
517
518  split Xdummy
519  bwipe!
520  call delete('Xtext')
521  call delete('Xdummy')
522endfunc
523
524func s:get_sleep_cmd()
525  if s:python != ''
526    let cmd = s:python . " test_short_sleep.py"
527    " 500 was not enough for Travis
528    let waittime = 900
529  else
530    echo 'This will take five seconds...'
531    let waittime = 2000
532    if has('win32')
533      let cmd = $windir . '\system32\timeout.exe 1'
534    else
535      let cmd = 'sleep 1'
536    endif
537  endif
538  return [cmd, waittime]
539endfunc
540
541func Test_terminal_finish_open_close()
542  call assert_equal(1, winnr('$'))
543
544  let [cmd, waittime] = s:get_sleep_cmd()
545
546  " shell terminal closes automatically
547  terminal
548  let buf = bufnr('%')
549  call assert_equal(2, winnr('$'))
550  " Wait for the shell to display a prompt
551  call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
552  call StopShellInTerminal(buf)
553  call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
554
555  " shell terminal that does not close automatically
556  terminal ++noclose
557  let buf = bufnr('%')
558  call assert_equal(2, winnr('$'))
559  " Wait for the shell to display a prompt
560  call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
561  call StopShellInTerminal(buf)
562  call assert_equal(2, winnr('$'))
563  quit
564  call assert_equal(1, winnr('$'))
565
566  exe 'terminal ++close ' . cmd
567  call assert_equal(2, winnr('$'))
568  wincmd p
569  call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
570
571  call term_start(cmd, {'term_finish': 'close'})
572  call assert_equal(2, winnr('$'))
573  wincmd p
574  call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
575  call assert_equal(1, winnr('$'))
576
577  exe 'terminal ++open ' . cmd
578  close!
579  call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
580  bwipe
581
582  call term_start(cmd, {'term_finish': 'open'})
583  close!
584  call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
585  bwipe
586
587  exe 'terminal ++hidden ++open ' . cmd
588  call assert_equal(1, winnr('$'))
589  call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
590  bwipe
591
592  call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
593  call assert_equal(1, winnr('$'))
594  call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
595  bwipe
596
597  call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
598  call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
599  call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
600  call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
601
602  call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d | let g:result = "opened the buffer in a window"'})
603  close!
604  call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
605  call assert_equal(4, winheight(0))
606  call assert_equal('opened the buffer in a window', g:result)
607  unlet g:result
608  bwipe
609endfunc
610
611func Test_terminal_cwd()
612  if has('win32')
613    let cmd = 'cmd /c cd'
614  else
615    CheckExecutable pwd
616    let cmd = 'pwd'
617  endif
618  call mkdir('Xdir')
619  let buf = term_start(cmd, {'cwd': 'Xdir'})
620  call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
621
622  exe buf . 'bwipe'
623  call delete('Xdir', 'rf')
624endfunc
625
626func Test_terminal_cwd_failure()
627  " Case 1: Provided directory is not actually a directory.  Attempt to make
628  " the file executable as well.
629  call writefile([], 'Xfile')
630  call setfperm('Xfile', 'rwx------')
631  call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
632  call delete('Xfile')
633
634  " Case 2: Directory does not exist.
635  call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
636
637  " Case 3: Directory exists but is not accessible.
638  " Skip this for root, it will be accessible anyway.
639  if !IsRoot()
640    call mkdir('XdirNoAccess', '', '0600')
641    " return early if the directory permissions could not be set properly
642    if getfperm('XdirNoAccess')[2] == 'x'
643      call delete('XdirNoAccess', 'rf')
644      return
645    endif
646    call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
647    call delete('XdirNoAccess', 'rf')
648  endif
649endfunc
650
651func Test_terminal_servername()
652  CheckFeature clientserver
653  call s:test_environment("VIM_SERVERNAME", v:servername)
654endfunc
655
656func Test_terminal_version()
657  call s:test_environment("VIM_TERMINAL", string(v:version))
658endfunc
659
660func s:test_environment(name, value)
661  let buf = Run_shell_in_terminal({})
662  " Wait for the shell to display a prompt
663  call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
664  if has('win32')
665    call term_sendkeys(buf, "echo %" . a:name . "%\r")
666  else
667    call term_sendkeys(buf, "echo $" . a:name . "\r")
668  endif
669  call TermWait(buf)
670  call StopShellInTerminal(buf)
671  call WaitForAssert({-> assert_equal(a:value, getline(2))})
672
673  exe buf . 'bwipe'
674  unlet buf
675endfunc
676
677func Test_terminal_env()
678  let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
679  " Wait for the shell to display a prompt
680  call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
681  if has('win32')
682    call term_sendkeys(buf, "echo %TESTENV%\r")
683  else
684    call term_sendkeys(buf, "echo $TESTENV\r")
685  endif
686  eval buf->TermWait()
687  call StopShellInTerminal(buf)
688  call WaitForAssert({-> assert_equal('correct', getline(2))})
689
690  exe buf . 'bwipe'
691endfunc
692
693func Test_terminal_list_args()
694  let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
695  call assert_fails(buf . 'bwipe', 'E89:')
696  exe buf . 'bwipe!'
697  call assert_equal("", bufname(buf))
698endfunction
699
700func Test_terminal_noblock()
701  let buf = term_start(&shell)
702  let wait_time = 5000
703  let letters = 'abcdefghijklmnopqrstuvwxyz'
704  if has('bsd') || has('mac') || has('sun')
705    " The shell or something else has a problem dealing with more than 1000
706    " characters at the same time.  It's very slow too.
707    let len = 1000
708    let wait_time = 15000
709    let letters = 'abcdefghijklm'
710  " NPFS is used in Windows, nonblocking mode does not work properly.
711  elseif has('win32')
712    let len = 1
713  else
714    let len = 5000
715  endif
716
717  " Send a lot of text lines, should be buffered properly.
718  for c in split(letters, '\zs')
719    call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
720  endfor
721  call term_sendkeys(buf, "echo done\<cr>")
722
723  " On MS-Windows there is an extra empty line below "done".  Find "done" in
724  " the last-but-one or the last-but-two line.
725  let lnum = term_getsize(buf)[0] - 1
726  call WaitForAssert({-> assert_match('done', term_getline(buf, lnum - 1) .. '//' .. term_getline(buf, lnum))}, wait_time)
727  let line = term_getline(buf, lnum)
728  if line !~ 'done'
729    let line = term_getline(buf, lnum - 1)
730  endif
731  call assert_match('done', line)
732
733  let g:job = term_getjob(buf)
734  call StopShellInTerminal(buf)
735  call TermWait(buf)
736  unlet g:job
737  bwipe
738endfunc
739
740func Test_terminal_write_stdin()
741  " TODO: enable once writing to stdin works on MS-Windows
742  CheckNotMSWindows
743  CheckExecutable wc
744
745  call setline(1, ['one', 'two', 'three'])
746  %term wc
747  call WaitForAssert({-> assert_match('3', getline("$"))})
748  let nrs = split(getline('$'))
749  call assert_equal(['3', '3', '14'], nrs)
750  %bwipe!
751
752  call setline(1, ['one', 'two', 'three', 'four'])
753  2,3term wc
754  call WaitForAssert({-> assert_match('2', getline("$"))})
755  let nrs = split(getline('$'))
756  call assert_equal(['2', '2', '10'], nrs)
757  %bwipe!
758endfunc
759
760func Test_terminal_eof_arg()
761  call CheckPython(s:python)
762
763  call setline(1, ['print("hello")'])
764  exe '1term ++eof=exit(123) ' .. s:python
765  " MS-Windows echoes the input, Unix doesn't.
766  if has('win32')
767    call WaitFor({-> getline('$') =~ 'exit(123)'})
768    call assert_equal('hello', getline(line('$') - 1))
769  else
770    call WaitFor({-> getline('$') =~ 'hello'})
771    call assert_equal('hello', getline('$'))
772  endif
773  call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
774  %bwipe!
775endfunc
776
777func Test_terminal_eof_arg_win32_ctrl_z()
778  CheckMSWindows
779  call CheckPython(s:python)
780
781  call setline(1, ['print("hello")'])
782  exe '1term ++eof=<C-Z> ' .. s:python
783  call WaitForAssert({-> assert_match('\^Z', getline(line('$') - 1))})
784  call assert_match('\^Z', getline(line('$') - 1))
785  %bwipe!
786endfunc
787
788func Test_terminal_duplicate_eof_arg()
789  call CheckPython(s:python)
790
791  " Check the last specified ++eof arg is used and should not memory leak.
792  new
793  call setline(1, ['print("hello")'])
794  exe '1term ++eof=<C-Z> ++eof=exit(123) ' .. s:python
795  " MS-Windows echoes the input, Unix doesn't.
796  if has('win32')
797    call WaitFor({-> getline('$') =~ 'exit(123)'})
798    call assert_equal('hello', getline(line('$') - 1))
799  else
800    call WaitFor({-> getline('$') =~ 'hello'})
801    call assert_equal('hello', getline('$'))
802  endif
803  call assert_equal(123, bufnr()->term_getjob()->job_info().exitval)
804  %bwipe!
805endfunc
806
807func Test_terminal_no_cmd()
808  let buf = term_start('NONE', {})
809  call assert_notequal(0, buf)
810
811  let pty = job_info(term_getjob(buf))['tty_out']
812  call assert_notequal('', pty)
813  if has('gui_running') && !has('win32')
814    " In the GUI job_start() doesn't work, it does not read from the pty.
815    call system('echo "look here" > ' . pty)
816  else
817    " Otherwise using a job works on all systems.
818    call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
819  endif
820  call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
821
822  bwipe!
823endfunc
824
825func Test_terminal_special_chars()
826  " this file name only works on Unix
827  CheckUnix
828
829  call mkdir('Xdir with spaces')
830  call writefile(['x'], 'Xdir with spaces/quoted"file')
831  term ls Xdir\ with\ spaces/quoted\"file
832  call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
833  " make sure the job has finished
834  call WaitForAssert({-> assert_match('finish', term_getstatus(bufnr()))})
835
836  call delete('Xdir with spaces', 'rf')
837  bwipe
838endfunc
839
840func Test_terminal_wrong_options()
841  call assert_fails('call term_start(&shell, {
842	\ "in_io": "file",
843	\ "in_name": "xxx",
844	\ "out_io": "file",
845	\ "out_name": "xxx",
846	\ "err_io": "file",
847	\ "err_name": "xxx"
848	\ })', 'E474:')
849  call assert_fails('call term_start(&shell, {
850	\ "out_buf": bufnr("%")
851	\ })', 'E474:')
852  call assert_fails('call term_start(&shell, {
853	\ "err_buf": bufnr("%")
854	\ })', 'E474:')
855endfunc
856
857func Test_terminal_redir_file()
858  let cmd = Get_cat_123_cmd()
859  let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
860  call TermWait(buf)
861  " ConPTY may precede escape sequence. There are things that are not so.
862  if !has('conpty')
863    call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
864    call assert_match('123', readfile('Xfile')[0])
865  endif
866  let g:job = term_getjob(buf)
867  call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
868
869  if has('win32')
870    " On Windows we cannot delete a file being used by a process.  When
871    " job_status() returns "dead", the process remains for a short time.
872    " Just wait for a moment.
873    sleep 50m
874  endif
875  call delete('Xfile')
876  bwipe
877
878  if has('unix')
879    call writefile(['one line'], 'Xfile')
880    let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
881    call TermWait(buf)
882    call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
883    let g:job = term_getjob(buf)
884    call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
885    bwipe
886    call delete('Xfile')
887  endif
888endfunc
889
890func TerminalTmap(remap)
891  let buf = Run_shell_in_terminal({})
892  call assert_equal('t', mode())
893
894  if a:remap
895    tmap 123 456
896  else
897    tnoremap 123 456
898  endif
899  " don't use abcde, it's an existing command
900  tmap 456 abxde
901  call assert_equal('456', maparg('123', 't'))
902  call assert_equal('abxde', maparg('456', 't'))
903  call feedkeys("123", 'tx')
904  call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
905  let lnum = term_getcursor(buf)[0]
906  if a:remap
907    call assert_match('abxde', term_getline(buf, lnum))
908  else
909    call assert_match('456', term_getline(buf, lnum))
910  endif
911
912  call term_sendkeys(buf, "\r")
913  call StopShellInTerminal(buf)
914  call TermWait(buf)
915
916  tunmap 123
917  tunmap 456
918  call assert_equal('', maparg('123', 't'))
919  close
920  unlet g:job
921endfunc
922
923func Test_terminal_tmap()
924  call TerminalTmap(1)
925  call TerminalTmap(0)
926endfunc
927
928func Test_terminal_wall()
929  let buf = Run_shell_in_terminal({})
930  wall
931  call StopShellInTerminal(buf)
932  call TermWait(buf)
933  exe buf . 'bwipe'
934  unlet g:job
935endfunc
936
937func Test_terminal_wqall()
938  let buf = Run_shell_in_terminal({})
939  call assert_fails('wqall', 'E948:')
940  call StopShellInTerminal(buf)
941  call TermWait(buf)
942  exe buf . 'bwipe'
943  unlet g:job
944endfunc
945
946func Test_terminal_composing_unicode()
947  let save_enc = &encoding
948  set encoding=utf-8
949
950  if has('win32')
951    let cmd = "cmd /K chcp 65001"
952    let lnum = [3, 6, 9]
953  else
954    let cmd = &shell
955    let lnum = [1, 3, 5]
956  endif
957
958  enew
959  let buf = term_start(cmd, {'curwin': 1})
960  let g:job = term_getjob(buf)
961  call WaitFor({-> term_getline(buf, 1) !=# ''}, 1000)
962
963  if has('win32')
964    call assert_equal('cmd', job_info(g:job).cmd[0])
965  else
966    call assert_equal(&shell, job_info(g:job).cmd[0])
967  endif
968
969  " ascii + composing
970  let txt = "a\u0308bc"
971  call term_sendkeys(buf, "echo " . txt)
972  call TermWait(buf, 25)
973  call assert_match("echo " . txt, term_getline(buf, lnum[0]))
974  call term_sendkeys(buf, "\<cr>")
975  call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[0] + 1))}, 1000)
976  let l = term_scrape(buf, lnum[0] + 1)
977  call assert_equal("a\u0308", l[0].chars)
978  call assert_equal("b", l[1].chars)
979  call assert_equal("c", l[2].chars)
980
981  " multibyte + composing: がぎぐげご
982  let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
983  call term_sendkeys(buf, "echo " . txt)
984  call TermWait(buf, 25)
985  call assert_match("echo " . txt, term_getline(buf, lnum[1]))
986  call term_sendkeys(buf, "\<cr>")
987  call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[1] + 1))}, 1000)
988  let l = term_scrape(buf, lnum[1] + 1)
989  call assert_equal("\u304b\u3099", l[0].chars)
990  call assert_equal(2, l[0].width)
991  call assert_equal("\u304e", l[1].chars)
992  call assert_equal(2, l[1].width)
993  call assert_equal("\u304f\u3099", l[2].chars)
994  call assert_equal(2, l[2].width)
995  call assert_equal("\u3052", l[3].chars)
996  call assert_equal(2, l[3].width)
997  call assert_equal("\u3053\u3099", l[4].chars)
998  call assert_equal(2, l[4].width)
999
1000  " \u00a0 + composing
1001  let txt = "abc\u00a0\u0308"
1002  call term_sendkeys(buf, "echo " . txt)
1003  call TermWait(buf, 25)
1004  call assert_match("echo " . txt, term_getline(buf, lnum[2]))
1005  call term_sendkeys(buf, "\<cr>")
1006  call WaitForAssert({-> assert_equal(txt, term_getline(buf, lnum[2] + 1))}, 1000)
1007  let l = term_scrape(buf, lnum[2] + 1)
1008  call assert_equal("\u00a0\u0308", l[3].chars)
1009
1010  call term_sendkeys(buf, "exit\r")
1011  call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
1012  bwipe!
1013  unlet g:job
1014  let &encoding = save_enc
1015endfunc
1016
1017func Test_terminal_aucmd_on_close()
1018  fun Nop()
1019    let s:called = 1
1020  endfun
1021
1022  aug repro
1023      au!
1024      au BufWinLeave * call Nop()
1025  aug END
1026
1027  let [cmd, waittime] = s:get_sleep_cmd()
1028
1029  call assert_equal(1, winnr('$'))
1030  new
1031  call setline(1, ['one', 'two'])
1032  exe 'term ++close ' . cmd
1033  wincmd p
1034  call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
1035  call assert_equal(1, s:called)
1036  bwipe!
1037
1038  unlet s:called
1039  au! repro
1040  delfunc Nop
1041endfunc
1042
1043func Test_terminal_term_start_empty_command()
1044  let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
1045  call assert_fails(cmd, 'E474:')
1046  let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
1047  call assert_fails(cmd, 'E474:')
1048  let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
1049  call assert_fails(cmd, 'E474:')
1050  let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
1051  call assert_fails(cmd, 'E474:')
1052  let cmd = "call term_start('', {'term_name' : []})"
1053  call assert_fails(cmd, 'E730:')
1054  let cmd = "call term_start('', {'term_finish' : 'axby'})"
1055  call assert_fails(cmd, 'E475:')
1056  let cmd = "call term_start('', {'eof_chars' : []})"
1057  call assert_fails(cmd, 'E730:')
1058  let cmd = "call term_start('', {'term_kill' : []})"
1059  call assert_fails(cmd, 'E730:')
1060  let cmd = "call term_start('', {'tty_type' : []})"
1061  call assert_fails(cmd, 'E730:')
1062  let cmd = "call term_start('', {'tty_type' : 'abc'})"
1063  call assert_fails(cmd, 'E475:')
1064  let cmd = "call term_start('', {'term_highlight' : []})"
1065  call assert_fails(cmd, 'E730:')
1066  if has('gui') || has('termguicolors')
1067    let cmd = "call term_start('', {'ansi_colors' : 'abc'})"
1068    call assert_fails(cmd, 'E475:')
1069    let cmd = "call term_start('', {'ansi_colors' : [[]]})"
1070    call assert_fails(cmd, 'E730:')
1071    let cmd = "call term_start('', {'ansi_colors' : repeat(['blue'], 18)})"
1072    if has('gui_running') || has('termguicolors')
1073      call assert_fails(cmd, 'E475:')
1074    else
1075      call assert_fails(cmd, 'E254:')
1076    endif
1077  endif
1078endfunc
1079
1080func Test_terminal_response_to_control_sequence()
1081  CheckUnix
1082
1083  let buf = Run_shell_in_terminal({})
1084  call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
1085
1086  call term_sendkeys(buf, "cat\<CR>")
1087  call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
1088
1089  " Request the cursor position.
1090  call term_sendkeys(buf, "\x1b[6n\<CR>")
1091
1092  " Wait for output from tty to display, below an empty line.
1093  call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
1094
1095  " End "cat" gently.
1096  call term_sendkeys(buf, "\<CR>\<C-D>")
1097
1098  call StopShellInTerminal(buf)
1099  exe buf . 'bwipe'
1100  unlet g:job
1101endfunc
1102
1103" Run Vim, start a terminal in that Vim with the kill argument,
1104" :qall works.
1105func Run_terminal_qall_kill(line1, line2)
1106  " 1. Open a terminal window and wait for the prompt to appear
1107  " 2. set kill using term_setkill()
1108  " 3. make Vim exit, it will kill the shell
1109  let after = [
1110	\ a:line1,
1111	\ 'let buf = bufnr("%")',
1112	\ 'while term_getline(buf, 1) =~ "^\\s*$"',
1113	\ '  sleep 10m',
1114	\ 'endwhile',
1115	\ a:line2,
1116	\ 'au VimLeavePre * call writefile(["done"], "Xdone")',
1117	\ 'qall',
1118	\ ]
1119  if !RunVim([], after, '')
1120    return
1121  endif
1122  call assert_equal("done", readfile("Xdone")[0])
1123  call delete("Xdone")
1124endfunc
1125
1126" Run Vim in a terminal, then start a terminal in that Vim with a kill
1127" argument, check that :qall works.
1128func Test_terminal_qall_kill_arg()
1129  call Run_terminal_qall_kill('term ++kill=kill', '')
1130endfunc
1131
1132" Run Vim, start a terminal in that Vim, set the kill argument with
1133" term_setkill(), check that :qall works.
1134func Test_terminal_qall_kill_func()
1135  call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
1136endfunc
1137
1138" Run Vim, start a terminal in that Vim without the kill argument,
1139" check that :qall does not exit, :qall! does.
1140func Test_terminal_qall_exit()
1141  let after =<< trim [CODE]
1142    term
1143    let buf = bufnr("%")
1144    while term_getline(buf, 1) =~ "^\\s*$"
1145      sleep 10m
1146    endwhile
1147    set nomore
1148    au VimLeavePre * call writefile(["too early"], "Xdone")
1149    qall
1150    au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1151    cquit
1152  [CODE]
1153
1154  if !RunVim([], after, '')
1155    return
1156  endif
1157  call assert_equal("done", readfile("Xdone")[0])
1158  call delete("Xdone")
1159endfunc
1160
1161" Run Vim in a terminal, then start a terminal in that Vim without a kill
1162" argument, check that :confirm qall works.
1163func Test_terminal_qall_prompt()
1164  CheckRunVimInTerminal
1165  let buf = RunVimInTerminal('', {})
1166
1167  " Open a terminal window and wait for the prompt to appear
1168  call term_sendkeys(buf, ":term\<CR>")
1169  call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1170  call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
1171
1172  " make Vim exit, it will prompt to kill the shell
1173  call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
1174  call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
1175  call term_sendkeys(buf, "y")
1176  call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
1177
1178  " close the terminal window where Vim was running
1179  quit
1180endfunc
1181
1182" Run Vim in a terminal, then start a terminal window with a shell and check
1183" that Vim exits if it is closed.
1184func Test_terminal_exit()
1185  CheckRunVimInTerminal
1186
1187  let lines =<< trim END
1188     let winid = win_getid()
1189     help
1190     term
1191     let termid = win_getid()
1192     call win_gotoid(winid)
1193     close
1194     call win_gotoid(termid)
1195  END
1196  call writefile(lines, 'XtermExit')
1197  let buf = RunVimInTerminal('-S XtermExit', #{rows: 10})
1198  let job = term_getjob(buf)
1199  call WaitForAssert({-> assert_equal("run", job_status(job))})
1200
1201  " quit the shell, it will make Vim exit
1202  call term_sendkeys(buf, "exit\<CR>")
1203  call WaitForAssert({-> assert_equal("dead", job_status(job))})
1204
1205  call delete('XtermExit')
1206endfunc
1207
1208func Test_terminal_open_autocmd()
1209  augroup repro
1210    au!
1211    au TerminalOpen * let s:called += 1
1212  augroup END
1213
1214  let s:called = 0
1215
1216  " Open a terminal window with :terminal
1217  terminal
1218  call assert_equal(1, s:called)
1219  bwipe!
1220
1221  " Open a terminal window with term_start()
1222  call term_start(&shell)
1223  call assert_equal(2, s:called)
1224  bwipe!
1225
1226  " Open a hidden terminal buffer with :terminal
1227  terminal ++hidden
1228  call assert_equal(3, s:called)
1229  for buf in term_list()
1230    exe buf . "bwipe!"
1231  endfor
1232
1233  " Open a hidden terminal buffer with term_start()
1234  let buf = term_start(&shell, {'hidden': 1})
1235  call assert_equal(4, s:called)
1236  exe buf . "bwipe!"
1237
1238  unlet s:called
1239  au! repro
1240endfunc
1241
1242func Test_open_term_from_cmd()
1243  CheckUnix
1244  CheckRunVimInTerminal
1245
1246  let lines =<< trim END
1247      call setline(1, ['a', 'b', 'c'])
1248      3
1249      set incsearch
1250      cnoremap <F3> <Cmd>call term_start(['/bin/sh', '-c', ':'])<CR>
1251  END
1252  call writefile(lines, 'Xopenterm')
1253  let buf = RunVimInTerminal('-S Xopenterm', {})
1254
1255  " this opens a window, incsearch should not use the old cursor position
1256  call term_sendkeys(buf, "/\<F3>")
1257  call VerifyScreenDump(buf, 'Test_terminal_from_cmd', {})
1258  call term_sendkeys(buf, "\<Esc>")
1259  call term_sendkeys(buf, ":q\<CR>")
1260
1261  call StopVimInTerminal(buf)
1262  call delete('Xopenterm')
1263endfunc
1264
1265func Test_combining_double_width()
1266  CheckUnix
1267  CheckRunVimInTerminal
1268
1269  call writefile(["\xe3\x83\x9b\xe3\x82\x9a"], 'Xonedouble')
1270  let lines =<< trim END
1271      call term_start(['/bin/sh', '-c', 'cat Xonedouble'])
1272  END
1273  call writefile(lines, 'Xcombining')
1274  let buf = RunVimInTerminal('-S Xcombining', #{rows: 9})
1275
1276  " this opens a window, incsearch should not use the old cursor position
1277  call VerifyScreenDump(buf, 'Test_terminal_combining', {})
1278  call term_sendkeys(buf, ":q\<CR>")
1279
1280  call StopVimInTerminal(buf)
1281  call delete('Xonedouble')
1282  call delete('Xcombining')
1283endfunc
1284
1285func Test_terminal_popup_with_cmd()
1286  " this was crashing
1287  let buf = term_start(&shell, #{hidden: v:true})
1288  let s:winid = popup_create(buf, {})
1289  tnoremap <F3> <Cmd>call popup_close(s:winid)<CR>
1290  call feedkeys("\<F3>", 'xt')
1291
1292  tunmap  <F3>
1293  exe 'bwipe! ' .. buf
1294  unlet s:winid
1295endfunc
1296
1297func Test_terminal_popup_bufload()
1298  let termbuf = term_start(&shell, #{hidden: v:true, term_finish: 'close'})
1299  let winid = popup_create(termbuf, {})
1300  sleep 50m
1301
1302  let newbuf = bufadd('')
1303  call bufload(newbuf)
1304  call setbufline(newbuf, 1, 'foobar')
1305
1306  " must not have switched to another window
1307  call assert_equal(winid, win_getid())
1308
1309  call StopShellInTerminal(termbuf)
1310  call WaitFor({-> win_getid() != winid})
1311  exe 'bwipe! ' .. newbuf
1312endfunc
1313
1314func Test_terminal_popup_insert_cmd()
1315  CheckUnix
1316
1317  inoremap <F3> <Cmd>call StartTermInPopup()<CR>
1318  func StartTermInPopup()
1319    call term_start(['/bin/sh', '-c', 'cat'], #{hidden: v:true, term_finish: 'close'})->popup_create(#{highlight: 'Pmenu'})
1320  endfunc
1321  call feedkeys("i\<F3>")
1322  sleep 10m
1323  call assert_equal('n', mode())
1324
1325  call feedkeys("\<C-D>", 'xt')
1326  call WaitFor({-> popup_list() == []})
1327  delfunc StartTermInPopup
1328  iunmap <F3>
1329endfunc
1330
1331func Check_dump01(off)
1332  call assert_equal('one two three four five', trim(getline(a:off + 1)))
1333  call assert_equal('~           Select Word', trim(getline(a:off + 7)))
1334  call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
1335endfunc
1336
1337func Test_terminal_dumpwrite_composing()
1338  CheckRunVimInTerminal
1339  let save_enc = &encoding
1340  set encoding=utf-8
1341  call assert_equal(1, winnr('$'))
1342
1343  let text = " a\u0300 e\u0302 o\u0308"
1344  call writefile([text], 'Xcomposing')
1345  let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
1346  call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
1347  eval 'Xdump'->term_dumpwrite(buf)
1348  let dumpline = readfile('Xdump')[0]
1349  call assert_match('|à| |ê| |ö', dumpline)
1350
1351  call StopVimInTerminal(buf)
1352  call delete('Xcomposing')
1353  call delete('Xdump')
1354  let &encoding = save_enc
1355endfunc
1356
1357" Tests for failures in the term_dumpwrite() function
1358func Test_terminal_dumpwrite_errors()
1359  CheckRunVimInTerminal
1360  call assert_fails("call term_dumpwrite({}, 'Xtest.dump')", 'E728:')
1361  let buf = RunVimInTerminal('', {})
1362  call TermWait(buf)
1363  call assert_fails("call term_dumpwrite(buf, 'Xtest.dump', '')", 'E715:')
1364  call assert_fails("call term_dumpwrite(buf, [])", 'E730:')
1365  call writefile([], 'Xtest.dump')
1366  call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E953:')
1367  call delete('Xtest.dump')
1368  call assert_fails("call term_dumpwrite(buf, '')", 'E482:')
1369  call assert_fails("call term_dumpwrite(buf, test_null_string())", 'E482:')
1370  call test_garbagecollect_now()
1371  call StopVimInTerminal(buf, 0)
1372  call TermWait(buf)
1373  call assert_fails("call term_dumpwrite(buf, 'Xtest.dump')", 'E958:')
1374  call assert_fails('call term_sendkeys([], ":q\<CR>")', 'E745:')
1375  call assert_equal(0, term_sendkeys(buf, ":q\<CR>"))
1376endfunc
1377
1378" just testing basic functionality.
1379func Test_terminal_dumpload()
1380  let curbuf = winbufnr('')
1381  call assert_equal(1, winnr('$'))
1382  let buf = term_dumpload('dumps/Test_popup_command_01.dump')
1383  call assert_equal(2, winnr('$'))
1384  call assert_equal(20, line('$'))
1385  call Check_dump01(0)
1386
1387  " Load another dump in the same window
1388  let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
1389  call assert_equal(buf, buf2)
1390  call assert_notequal('one two three four five', trim(getline(1)))
1391
1392  " Load the first dump again in the same window
1393  let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1394  call assert_equal(buf, buf2)
1395  call Check_dump01(0)
1396
1397  call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1398  call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1399  new
1400  let closedbuf = winbufnr('')
1401  quit
1402  call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1403  call assert_fails('call term_dumpload([])', 'E730:')
1404  call assert_fails('call term_dumpload("xabcy.dump")', 'E485:')
1405
1406  quit
1407endfunc
1408
1409func Test_terminal_dumpload_dump()
1410  CheckRunVimInTerminal
1411
1412  let lines =<< trim END
1413     call term_dumpload('dumps/Test_popupwin_22.dump', #{term_rows: 12})
1414  END
1415  call writefile(lines, 'XtermDumpload')
1416  let buf = RunVimInTerminal('-S XtermDumpload', #{rows: 15})
1417  call VerifyScreenDump(buf, 'Test_terminal_dumpload', {})
1418
1419  call StopVimInTerminal(buf)
1420  call delete('XtermDumpload')
1421endfunc
1422
1423func Test_terminal_dumpdiff()
1424  call assert_equal(1, winnr('$'))
1425  eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
1426  call assert_equal(2, winnr('$'))
1427  call assert_equal(62, line('$'))
1428  call Check_dump01(0)
1429  call Check_dump01(42)
1430  call assert_equal('           bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1431  quit
1432
1433  call assert_fails('call term_dumpdiff("X1.dump", [])', 'E730:')
1434  call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1435  call writefile([], 'X1.dump')
1436  call assert_fails('call term_dumpdiff("X1.dump", "X2.dump")', 'E485:')
1437  call delete('X1.dump')
1438endfunc
1439
1440func Test_terminal_dumpdiff_swap()
1441  call assert_equal(1, winnr('$'))
1442  call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1443  call assert_equal(2, winnr('$'))
1444  call assert_equal(62, line('$'))
1445  call assert_match('Test_popup_command_01.dump', getline(21))
1446  call assert_match('Test_popup_command_03.dump', getline(42))
1447  call assert_match('Undo', getline(3))
1448  call assert_match('three four five', getline(45))
1449
1450  normal s
1451  call assert_match('Test_popup_command_03.dump', getline(21))
1452  call assert_match('Test_popup_command_01.dump', getline(42))
1453  call assert_match('three four five', getline(3))
1454  call assert_match('Undo', getline(45))
1455  quit
1456
1457  " Diff two terminal dump files with different number of rows
1458  " Swap the diffs
1459  call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_winline_rnu.dump')
1460  call assert_match('Test_popup_command_01.dump', getline(21))
1461  call assert_match('Test_winline_rnu.dump', getline(42))
1462  normal s
1463  call assert_match('Test_winline_rnu.dump', getline(6))
1464  call assert_match('Test_popup_command_01.dump', getline(27))
1465  quit
1466endfunc
1467
1468func Test_terminal_dumpdiff_options()
1469  set laststatus=0
1470  call assert_equal(1, winnr('$'))
1471  let height = winheight(0)
1472  call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1473  call assert_equal(2, winnr('$'))
1474  call assert_equal(height, winheight(winnr()))
1475  call assert_equal(33, winwidth(winnr()))
1476  call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1477  quit
1478
1479  call assert_equal(1, winnr('$'))
1480  call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1481  call assert_equal(2, winnr('$'))
1482  call assert_equal(&columns, winwidth(0))
1483  call assert_equal(13, winheight(0))
1484  call assert_equal('something else', bufname('%'))
1485  quit
1486
1487  call assert_equal(1, winnr('$'))
1488  call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1489  call assert_equal(1, winnr('$'))
1490  call assert_fails("call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'bufnr': -1})", 'E475:')
1491  bwipe
1492
1493  set laststatus&
1494endfunc
1495
1496" When drawing the statusline the cursor position may not have been updated
1497" yet.
1498" 1. create a terminal, make it show 2 lines
1499" 2. 0.5 sec later: leave terminal window, execute "i"
1500" 3. 0.5 sec later: clear terminal window, now it's 1 line
1501" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
1502" 4. 0.5 sec later: should be done, clean up
1503func Test_terminal_statusline()
1504  CheckUnix
1505
1506  set statusline=x
1507  terminal
1508  let tbuf = bufnr('')
1509  call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
1510  call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
1511  call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
1512  au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
1513
1514  sleep 2
1515  exe tbuf . 'bwipe!'
1516  au! BufLeave
1517  set statusline=
1518endfunc
1519
1520func Test_terminal_window_focus()
1521  let winid1 = win_getid()
1522  terminal
1523  let winid2 = win_getid()
1524  call feedkeys("\<C-W>j", 'xt')
1525  call assert_equal(winid1, win_getid())
1526  call feedkeys("\<C-W>k", 'xt')
1527  call assert_equal(winid2, win_getid())
1528  " can use a cursor key here
1529  call feedkeys("\<C-W>\<Down>", 'xt')
1530  call assert_equal(winid1, win_getid())
1531  call feedkeys("\<C-W>\<Up>", 'xt')
1532  call assert_equal(winid2, win_getid())
1533
1534  bwipe!
1535endfunc
1536
1537func Api_drop_common(options)
1538  call assert_equal(1, winnr('$'))
1539
1540  " Use the title termcap entries to output the escape sequence.
1541  call writefile([
1542	\ 'set title',
1543	\ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1544	\ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
1545	\ 'redraw',
1546	\ "set t_ts=",
1547	\ ], 'Xscript')
1548  let buf = RunVimInTerminal('-S Xscript', {})
1549  call WaitFor({-> bufnr('Xtextfile') > 0})
1550  call assert_equal('Xtextfile', expand('%:t'))
1551  call assert_true(winnr('$') >= 3)
1552  return buf
1553endfunc
1554
1555func Test_terminal_api_drop_newwin()
1556  CheckRunVimInTerminal
1557  let buf = Api_drop_common('')
1558  call assert_equal(0, &bin)
1559  call assert_equal('', &fenc)
1560
1561  call StopVimInTerminal(buf)
1562  call delete('Xscript')
1563  bwipe Xtextfile
1564endfunc
1565
1566func Test_terminal_api_drop_newwin_bin()
1567  CheckRunVimInTerminal
1568  let buf = Api_drop_common(',{"bin":1}')
1569  call assert_equal(1, &bin)
1570
1571  call StopVimInTerminal(buf)
1572  call delete('Xscript')
1573  bwipe Xtextfile
1574endfunc
1575
1576func Test_terminal_api_drop_newwin_binary()
1577  CheckRunVimInTerminal
1578  let buf = Api_drop_common(',{"binary":1}')
1579  call assert_equal(1, &bin)
1580
1581  call StopVimInTerminal(buf)
1582  call delete('Xscript')
1583  bwipe Xtextfile
1584endfunc
1585
1586func Test_terminal_api_drop_newwin_nobin()
1587  CheckRunVimInTerminal
1588  set binary
1589  let buf = Api_drop_common(',{"nobin":1}')
1590  call assert_equal(0, &bin)
1591
1592  call StopVimInTerminal(buf)
1593  call delete('Xscript')
1594  bwipe Xtextfile
1595  set nobinary
1596endfunc
1597
1598func Test_terminal_api_drop_newwin_nobinary()
1599  CheckRunVimInTerminal
1600  set binary
1601  let buf = Api_drop_common(',{"nobinary":1}')
1602  call assert_equal(0, &bin)
1603
1604  call StopVimInTerminal(buf)
1605  call delete('Xscript')
1606  bwipe Xtextfile
1607  set nobinary
1608endfunc
1609
1610func Test_terminal_api_drop_newwin_ff()
1611  CheckRunVimInTerminal
1612  let buf = Api_drop_common(',{"ff":"dos"}')
1613  call assert_equal("dos", &ff)
1614
1615  call StopVimInTerminal(buf)
1616  call delete('Xscript')
1617  bwipe Xtextfile
1618endfunc
1619
1620func Test_terminal_api_drop_newwin_fileformat()
1621  CheckRunVimInTerminal
1622  let buf = Api_drop_common(',{"fileformat":"dos"}')
1623  call assert_equal("dos", &ff)
1624
1625  call StopVimInTerminal(buf)
1626  call delete('Xscript')
1627  bwipe Xtextfile
1628endfunc
1629
1630func Test_terminal_api_drop_newwin_enc()
1631  CheckRunVimInTerminal
1632  let buf = Api_drop_common(',{"enc":"utf-16"}')
1633  call assert_equal("utf-16", &fenc)
1634
1635  call StopVimInTerminal(buf)
1636  call delete('Xscript')
1637  bwipe Xtextfile
1638endfunc
1639
1640func Test_terminal_api_drop_newwin_encoding()
1641  CheckRunVimInTerminal
1642  let buf = Api_drop_common(',{"encoding":"utf-16"}')
1643  call assert_equal("utf-16", &fenc)
1644
1645  call StopVimInTerminal(buf)
1646  call delete('Xscript')
1647  bwipe Xtextfile
1648endfunc
1649
1650func Test_terminal_api_drop_oldwin()
1651  CheckRunVimInTerminal
1652  let firstwinid = win_getid()
1653  split Xtextfile
1654  let textfile_winid = win_getid()
1655  call assert_equal(2, winnr('$'))
1656  call win_gotoid(firstwinid)
1657
1658  " Use the title termcap entries to output the escape sequence.
1659  call writefile([
1660	\ 'set title',
1661	\ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1662	\ 'let &titlestring = ''["drop","Xtextfile"]''',
1663	\ 'redraw',
1664	\ "set t_ts=",
1665	\ ], 'Xscript')
1666  let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
1667  call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
1668  call assert_equal(textfile_winid, win_getid())
1669
1670  call StopVimInTerminal(buf)
1671  call delete('Xscript')
1672  bwipe Xtextfile
1673endfunc
1674
1675func Tapi_TryThis(bufnum, arg)
1676  let g:called_bufnum = a:bufnum
1677  let g:called_arg = a:arg
1678endfunc
1679
1680func WriteApiCall(funcname)
1681  " Use the title termcap entries to output the escape sequence.
1682  call writefile([
1683	\ 'set title',
1684	\ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1685	\ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1686	\ 'redraw',
1687	\ "set t_ts=",
1688	\ ], 'Xscript')
1689endfunc
1690
1691func Test_terminal_api_call()
1692  CheckRunVimInTerminal
1693
1694  unlet! g:called_bufnum
1695  unlet! g:called_arg
1696
1697  call WriteApiCall('Tapi_TryThis')
1698
1699  " Default
1700  let buf = RunVimInTerminal('-S Xscript', {})
1701  call WaitFor({-> exists('g:called_bufnum')})
1702  call assert_equal(buf, g:called_bufnum)
1703  call assert_equal(['hello', 123], g:called_arg)
1704  call StopVimInTerminal(buf)
1705
1706  unlet! g:called_bufnum
1707  unlet! g:called_arg
1708
1709  " Enable explicitly
1710  let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1711  call WaitFor({-> exists('g:called_bufnum')})
1712  call assert_equal(buf, g:called_bufnum)
1713  call assert_equal(['hello', 123], g:called_arg)
1714  call StopVimInTerminal(buf)
1715
1716  unlet! g:called_bufnum
1717  unlet! g:called_arg
1718
1719  func! ApiCall_TryThis(bufnum, arg)
1720    let g:called_bufnum2 = a:bufnum
1721    let g:called_arg2 = a:arg
1722  endfunc
1723
1724  call WriteApiCall('ApiCall_TryThis')
1725
1726  " Use prefix match
1727  let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1728  call WaitFor({-> exists('g:called_bufnum2')})
1729  call assert_equal(buf, g:called_bufnum2)
1730  call assert_equal(['hello', 123], g:called_arg2)
1731  call StopVimInTerminal(buf)
1732
1733  call assert_fails("call term_start('ls', {'term_api' : []})", 'E730:')
1734
1735  unlet! g:called_bufnum2
1736  unlet! g:called_arg2
1737
1738  call delete('Xscript')
1739  delfunction! ApiCall_TryThis
1740  unlet! g:called_bufnum2
1741  unlet! g:called_arg2
1742endfunc
1743
1744func Test_terminal_api_call_fails()
1745  CheckRunVimInTerminal
1746
1747  func! TryThis(bufnum, arg)
1748    let g:called_bufnum3 = a:bufnum
1749    let g:called_arg3 = a:arg
1750  endfunc
1751
1752  call WriteApiCall('TryThis')
1753
1754  unlet! g:called_bufnum3
1755  unlet! g:called_arg3
1756
1757  " Not permitted
1758  call ch_logfile('Xlog', 'w')
1759  let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1760  call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1761  call assert_false(exists('g:called_bufnum3'))
1762  call assert_false(exists('g:called_arg3'))
1763  call StopVimInTerminal(buf)
1764
1765  " No match
1766  call ch_logfile('Xlog', 'w')
1767  let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1768  call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1769  call assert_false(exists('g:called_bufnum3'))
1770  call assert_false(exists('g:called_arg3'))
1771  call StopVimInTerminal(buf)
1772
1773  call delete('Xscript')
1774  call ch_logfile('')
1775  call delete('Xlog')
1776  delfunction! TryThis
1777  unlet! g:called_bufnum3
1778  unlet! g:called_arg3
1779endfunc
1780
1781let s:caught_e937 = 0
1782
1783func Tapi_Delete(bufnum, arg)
1784  try
1785    execute 'bdelete!' a:bufnum
1786  catch /E937:/
1787    let s:caught_e937 = 1
1788  endtry
1789endfunc
1790
1791func Test_terminal_api_call_fail_delete()
1792  CheckRunVimInTerminal
1793
1794  call WriteApiCall('Tapi_Delete')
1795  let buf = RunVimInTerminal('-S Xscript', {})
1796  call WaitForAssert({-> assert_equal(1, s:caught_e937)})
1797
1798  call StopVimInTerminal(buf)
1799  call delete('Xscript')
1800  call ch_logfile('', '')
1801endfunc
1802
1803func Test_terminal_setapi_and_call()
1804  CheckRunVimInTerminal
1805
1806  call WriteApiCall('Tapi_TryThis')
1807  call ch_logfile('Xlog', 'w')
1808
1809  unlet! g:called_bufnum
1810  unlet! g:called_arg
1811
1812  let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1813  call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1814  call assert_false(exists('g:called_bufnum'))
1815  call assert_false(exists('g:called_arg'))
1816
1817  eval buf->term_setapi('Tapi_')
1818  call term_sendkeys(buf, ":set notitle\<CR>")
1819  call term_sendkeys(buf, ":source Xscript\<CR>")
1820  call WaitFor({-> exists('g:called_bufnum')})
1821  call assert_equal(buf, g:called_bufnum)
1822  call assert_equal(['hello', 123], g:called_arg)
1823
1824  call StopVimInTerminal(buf)
1825
1826  call delete('Xscript')
1827  call ch_logfile('')
1828  call delete('Xlog')
1829  unlet! g:called_bufnum
1830  unlet! g:called_arg
1831endfunc
1832
1833func Test_terminal_api_arg()
1834  CheckRunVimInTerminal
1835
1836  call WriteApiCall('Tapi_TryThis')
1837  call ch_logfile('Xlog', 'w')
1838
1839  unlet! g:called_bufnum
1840  unlet! g:called_arg
1841
1842  execute 'term ++api= ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1843  let buf = bufnr('%')
1844  call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
1845  call assert_false(exists('g:called_bufnum'))
1846  call assert_false(exists('g:called_arg'))
1847
1848  call StopVimInTerminal(buf)
1849
1850  call ch_logfile('Xlog', 'w')
1851
1852  execute 'term ++api=Tapi_ ' .. GetVimCommandCleanTerm() .. '-S Xscript'
1853  let buf = bufnr('%')
1854  call WaitFor({-> exists('g:called_bufnum')})
1855  call assert_equal(buf, g:called_bufnum)
1856  call assert_equal(['hello', 123], g:called_arg)
1857
1858  call StopVimInTerminal(buf)
1859
1860  call delete('Xscript')
1861  call ch_logfile('')
1862  call delete('Xlog')
1863  unlet! g:called_bufnum
1864  unlet! g:called_arg
1865endfunc
1866
1867func Test_terminal_ansicolors_default()
1868  CheckFunction term_getansicolors
1869
1870  let colors = [
1871	\ '#000000', '#e00000',
1872	\ '#00e000', '#e0e000',
1873	\ '#0000e0', '#e000e0',
1874	\ '#00e0e0', '#e0e0e0',
1875	\ '#808080', '#ff4040',
1876	\ '#40ff40', '#ffff40',
1877	\ '#4040ff', '#ff40ff',
1878	\ '#40ffff', '#ffffff',
1879	\]
1880
1881  let buf = Run_shell_in_terminal({})
1882  call assert_equal(colors, term_getansicolors(buf))
1883  call StopShellInTerminal(buf)
1884  call TermWait(buf)
1885  call assert_equal([], term_getansicolors(buf))
1886
1887  exe buf . 'bwipe'
1888endfunc
1889
1890let s:test_colors = [
1891	\ '#616e64', '#0d0a79',
1892	\ '#6d610d', '#0a7373',
1893	\ '#690d0a', '#6d696e',
1894	\ '#0d0a6f', '#616e0d',
1895	\ '#0a6479', '#6d0d0a',
1896	\ '#617373', '#0d0a69',
1897	\ '#6d690d', '#0a6e6f',
1898	\ '#610d0a', '#6e6479',
1899	\]
1900
1901func Test_terminal_ansicolors_global()
1902  CheckFeature termguicolors
1903  CheckFunction term_getansicolors
1904
1905  let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1906  let buf = Run_shell_in_terminal({})
1907  call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1908  call StopShellInTerminal(buf)
1909  call TermWait(buf)
1910
1911  exe buf . 'bwipe'
1912  unlet g:terminal_ansi_colors
1913endfunc
1914
1915func Test_terminal_ansicolors_func()
1916  CheckFeature termguicolors
1917  CheckFunction term_getansicolors
1918
1919  let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1920  let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1921  call assert_equal(s:test_colors, term_getansicolors(buf))
1922
1923  call term_setansicolors(buf, g:terminal_ansi_colors)
1924  call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
1925
1926  let colors = [
1927	\ 'ivory', 'AliceBlue',
1928	\ 'grey67', 'dark goldenrod',
1929	\ 'SteelBlue3', 'PaleVioletRed4',
1930	\ 'MediumPurple2', 'yellow2',
1931	\ 'RosyBrown3', 'OrangeRed2',
1932	\ 'white smoke', 'navy blue',
1933	\ 'grey47', 'gray97',
1934	\ 'MistyRose2', 'DodgerBlue4',
1935	\]
1936  eval buf->term_setansicolors(colors)
1937
1938  let colors[4] = 'Invalid'
1939  call assert_fails('call term_setansicolors(buf, colors)', 'E254:')
1940  call assert_fails('call term_setansicolors(buf, {})', 'E714:')
1941
1942  call StopShellInTerminal(buf)
1943  call TermWait(buf)
1944  call assert_equal(0, term_setansicolors(buf, []))
1945  exe buf . 'bwipe'
1946endfunc
1947
1948func Test_terminal_all_ansi_colors()
1949  CheckRunVimInTerminal
1950
1951  " Use all the ANSI colors.
1952  call writefile([
1953	\ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
1954	\ 'hi Tblack ctermfg=0 ctermbg=8',
1955	\ 'hi Tdarkred ctermfg=1 ctermbg=9',
1956	\ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1957	\ 'hi Tbrown ctermfg=3 ctermbg=11',
1958	\ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1959	\ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1960	\ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1961	\ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1962	\ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1963	\ 'hi Tred ctermfg=9 ctermbg=1',
1964	\ 'hi Tgreen ctermfg=10 ctermbg=2',
1965	\ 'hi Tyellow ctermfg=11 ctermbg=3',
1966	\ 'hi Tblue ctermfg=12 ctermbg=4',
1967	\ 'hi Tmagenta ctermfg=13 ctermbg=5',
1968	\ 'hi Tcyan ctermfg=14 ctermbg=6',
1969	\ 'hi Twhite ctermfg=15 ctermbg=7',
1970	\ 'hi TdarkredBold ctermfg=1 cterm=bold',
1971	\ 'hi TgreenBold ctermfg=10 cterm=bold',
1972	\ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
1973	\ '',
1974	\ 'call  matchadd("Tblack", "A")',
1975	\ 'call  matchadd("Tdarkred", "B")',
1976	\ 'call  matchadd("Tdarkgreen", "C")',
1977	\ 'call  matchadd("Tbrown", "D")',
1978	\ 'call  matchadd("Tdarkblue", "E")',
1979	\ 'call  matchadd("Tdarkmagenta", "F")',
1980	\ 'call  matchadd("Tdarkcyan", "G")',
1981	\ 'call  matchadd("Tlightgrey", "H")',
1982	\ 'call  matchadd("Tdarkgrey", "I")',
1983	\ 'call  matchadd("Tred", "J")',
1984	\ 'call  matchadd("Tgreen", "K")',
1985	\ 'call  matchadd("Tyellow", "L")',
1986	\ 'call  matchadd("Tblue", "M")',
1987	\ 'call  matchadd("Tmagenta", "N")',
1988	\ 'call  matchadd("Tcyan", "O")',
1989	\ 'call  matchadd("Twhite", "P")',
1990	\ 'call  matchadd("TdarkredBold", "X")',
1991	\ 'call  matchadd("TgreenBold", "Y")',
1992	\ 'call  matchadd("TmagentaBold", "Z")',
1993	\ 'redraw',
1994	\ ], 'Xcolorscript')
1995  let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1996  call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1997
1998  call term_sendkeys(buf, ":q\<CR>")
1999  call StopVimInTerminal(buf)
2000  call delete('Xcolorscript')
2001endfunc
2002
2003
2004" vim: shiftwidth=2 sts=2 expandtab
2005