blob: 6ce531ed4542c95a347a103f3c077f2b0468c012 [file] [log] [blame]
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02001" Tests for the terminal window.
2" This is split in two, because it can take a lot of time.
Bram Moolenaar18aa13d2020-07-11 13:09:36 +02003" See test_terminal.vim and test_terminal3.vim for further tests.
Bram Moolenaar1112c0f2020-07-01 21:53:50 +02004
5source check.vim
6CheckFeature terminal
7
8source shared.vim
9source screendump.vim
10source mouse.vim
11source term_util.vim
12
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020013let $PROMPT_COMMAND=''
14
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020015func Test_terminal_termwinsize_option_fixed()
16 CheckRunVimInTerminal
17 set termwinsize=6x40
18 let text = []
19 for n in range(10)
20 call add(text, repeat(n, 50))
21 endfor
Bram Moolenaarc4860bd2022-10-15 20:52:26 +010022 call writefile(text, 'Xwinsize', 'D')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020023 let buf = RunVimInTerminal('Xwinsize', {})
24 let win = bufwinid(buf)
25 call assert_equal([6, 40], term_getsize(buf))
26 call assert_equal(6, winheight(win))
27 call assert_equal(40, winwidth(win))
28
29 " resizing the window doesn't resize the terminal.
30 resize 10
31 vertical resize 60
32 call assert_equal([6, 40], term_getsize(buf))
33 call assert_equal(10, winheight(win))
34 call assert_equal(60, winwidth(win))
35
36 call StopVimInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020037
Bram Moolenaare2e40752020-09-04 21:18:46 +020038 call assert_fails('set termwinsize=40', 'E474:')
39 call assert_fails('set termwinsize=10+40', 'E474:')
40 call assert_fails('set termwinsize=abc', 'E474:')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020041
42 set termwinsize=
43endfunc
44
45func Test_terminal_termwinsize_option_zero()
46 set termwinsize=0x0
47 let buf = Run_shell_in_terminal({})
48 let win = bufwinid(buf)
49 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
50 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020051 exe buf . 'bwipe'
52
53 set termwinsize=7x0
54 let buf = Run_shell_in_terminal({})
55 let win = bufwinid(buf)
56 call assert_equal([7, winwidth(win)], term_getsize(buf))
57 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020058 exe buf . 'bwipe'
59
60 set termwinsize=0x33
61 let buf = Run_shell_in_terminal({})
62 let win = bufwinid(buf)
63 call assert_equal([winheight(win), 33], term_getsize(buf))
64 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020065 exe buf . 'bwipe'
66
Christian Brabandtaa64ba12023-09-19 21:05:20 +020067 " This used to crash Vim
68 set termwinsize=10000*10000
69 let buf = Run_shell_in_terminal({})
70 let win = bufwinid(buf)
71 call assert_equal([1000, 1000], term_getsize(buf))
72 call StopShellInTerminal(buf)
73 exe buf . 'bwipe'
74
Bram Moolenaar1112c0f2020-07-01 21:53:50 +020075 set termwinsize=
76endfunc
77
78func Test_terminal_termwinsize_minimum()
79 set termwinsize=10*50
80 vsplit
81 let buf = Run_shell_in_terminal({})
82 let win = bufwinid(buf)
83 call assert_inrange(10, 1000, winheight(win))
84 call assert_inrange(50, 1000, winwidth(win))
85 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
86
87 resize 15
88 vertical resize 60
89 redraw
90 call assert_equal([15, 60], term_getsize(buf))
91 call assert_equal(15, winheight(win))
92 call assert_equal(60, winwidth(win))
93
94 resize 7
95 vertical resize 30
96 redraw
97 call assert_equal([10, 50], term_getsize(buf))
98 call assert_equal(7, winheight(win))
99 call assert_equal(30, winwidth(win))
100
101 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200102 exe buf . 'bwipe'
103
104 set termwinsize=0*0
105 let buf = Run_shell_in_terminal({})
106 let win = bufwinid(buf)
107 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
108 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200109 exe buf . 'bwipe'
110
111 set termwinsize=
112endfunc
113
Bram Moolenaarb936b792020-09-04 18:34:09 +0200114func Test_terminal_termwinsize_overruled()
115 let cmd = GetDummyCmd()
116 set termwinsize=5x43
117 let buf = term_start(cmd, #{term_rows: 7, term_cols: 50})
118 call TermWait(buf)
119 call assert_equal([7, 50], term_getsize(buf))
120 exe "bwipe! " .. buf
121
122 let buf = term_start(cmd, #{term_cols: 50})
123 call TermWait(buf)
124 call assert_equal([5, 50], term_getsize(buf))
125 exe "bwipe! " .. buf
126
127 let buf = term_start(cmd, #{term_rows: 7})
128 call TermWait(buf)
129 call assert_equal([7, 43], term_getsize(buf))
130 exe "bwipe! " .. buf
131
132 set termwinsize=
133endfunc
134
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200135" hidden terminal must not change current window size
136func Test_terminal_hidden_winsize()
137 let cmd = GetDummyCmd()
138 let rows = winheight(0)
139 let buf = term_start(cmd, #{hidden: 1, term_rows: 10})
Yegappan Lakshmanane446a012023-01-19 17:49:58 +0000140 call TermWait(buf)
Bram Moolenaar2ce14582020-09-05 16:08:49 +0200141 call assert_equal(rows, winheight(0))
142 call assert_equal([10, &columns], term_getsize(buf))
143 exe "bwipe! " .. buf
144endfunc
145
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200146func Test_terminal_termwinkey()
147 " make three tabpages, terminal in the middle
148 0tabnew
149 tabnext
150 tabnew
151 tabprev
152 call assert_equal(1, winnr('$'))
153 call assert_equal(2, tabpagenr())
154 let thiswin = win_getid()
155
156 let buf = Run_shell_in_terminal({})
157 let termwin = bufwinid(buf)
158 set termwinkey=<C-L>
159 call feedkeys("\<C-L>w", 'tx')
160 call assert_equal(thiswin, win_getid())
161 call feedkeys("\<C-W>w", 'tx')
162 call assert_equal(termwin, win_getid())
163
164 if has('langmap')
165 set langmap=xjyk
166 call feedkeys("\<C-L>x", 'tx')
167 call assert_equal(thiswin, win_getid())
168 call feedkeys("\<C-W>y", 'tx')
169 call assert_equal(termwin, win_getid())
170 set langmap=
171 endif
172
173 call feedkeys("\<C-L>gt", "xt")
174 call assert_equal(3, tabpagenr())
175 tabprev
176 call assert_equal(2, tabpagenr())
177 call assert_equal(termwin, win_getid())
178
179 call feedkeys("\<C-L>gT", "xt")
180 call assert_equal(1, tabpagenr())
181 tabnext
182 call assert_equal(2, tabpagenr())
183 call assert_equal(termwin, win_getid())
184
185 let job = term_getjob(buf)
186 call feedkeys("\<C-L>\<C-C>", 'tx')
187 call WaitForAssert({-> assert_equal("dead", job_status(job))})
188
189 set termwinkey&
190 tabnext
191 tabclose
192 tabprev
193 tabclose
194endfunc
195
196func Test_terminal_out_err()
197 CheckUnix
198
199 call writefile([
200 \ '#!/bin/sh',
201 \ 'echo "this is standard error" >&2',
202 \ 'echo "this is standard out" >&1',
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100203 \ ], 'Xechoerrout.sh', 'D')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200204 call setfperm('Xechoerrout.sh', 'rwxrwx---')
205
206 let outfile = 'Xtermstdout'
207 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Yegappan Lakshmanane446a012023-01-19 17:49:58 +0000208 call TermWait(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200209
210 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
211 call assert_equal(['this is standard out'], readfile(outfile))
212 call assert_equal('this is standard error', term_getline(buf, 1))
213
214 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
215 exe buf . 'bwipe'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200216 call delete(outfile)
217endfunc
218
219func Test_termwinscroll()
220 CheckUnix
Bram Moolenaarf65927f2020-07-11 14:04:28 +0200221 " TODO: Somehow this test sometimes hangs in the GUI
222 CheckNotGui
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100223 let g:test_is_flaky = 1
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200224
225 " Let the terminal output more than 'termwinscroll' lines, some at the start
226 " will be dropped.
227 exe 'set termwinscroll=' . &lines
228 let buf = term_start('/bin/sh')
Yegappan Lakshmanane446a012023-01-19 17:49:58 +0000229 call TermWait(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200230 for i in range(1, &lines)
231 call feedkeys("echo " . i . "\<CR>", 'xt')
232 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
233 endfor
234 " Go to Terminal-Normal mode to update the buffer.
235 call feedkeys("\<C-W>N", 'xt')
236 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
237
238 " Every "echo nr" must only appear once
239 let lines = getline(1, line('$'))
240 for i in range(&lines - len(lines) / 2 + 2, &lines)
241 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
242 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
243 endfor
244
245 exe buf . 'bwipe!'
246endfunc
247
248" Resizing the terminal window caused an ml_get error.
249" TODO: This does not reproduce the original problem.
250func Test_terminal_resize()
251 set statusline=x
252 terminal
253 call assert_equal(2, winnr('$'))
Bram Moolenaarc54f3472021-03-23 19:22:12 +0100254 let buf = bufnr()
255
256 " Wait for the shell to display a prompt
257 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200258
259 " Fill the terminal with text.
260 if has('win32')
261 call feedkeys("dir\<CR>", 'xt')
262 else
263 call feedkeys("ls\<CR>", 'xt')
264 endif
Bram Moolenaarc54f3472021-03-23 19:22:12 +0100265 " Wait for some output
266 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
267
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200268 " Go to Terminal-Normal mode for a moment.
269 call feedkeys("\<C-W>N", 'xt')
270 " Open a new window
271 call feedkeys("i\<C-W>n", 'xt')
272 call assert_equal(3, winnr('$'))
273 redraw
274
275 close
276 call assert_equal(2, winnr('$'))
277 call feedkeys("exit\<CR>", 'xt')
Bram Moolenaarc54f3472021-03-23 19:22:12 +0100278 call TermWait(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200279 set statusline&
280endfunc
281
Christian Brabandtaa64ba12023-09-19 21:05:20 +0200282func Test_terminal_resize2()
283 CheckNotMSWindows
284 set statusline=x
285 terminal
286 call assert_equal(2, winnr('$'))
287 let buf = bufnr()
288
289 " Wait for the shell to display a prompt
290 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
291
292 " This used to crash Vim
293 call feedkeys("printf '\033[8;99999;99999t'\<CR>", 'xt')
294 redraw
295
296 call feedkeys("exit\<CR>", 'xt')
297 call TermWait(buf)
298 set statusline&
299endfunc
300
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200301" must be nearly the last, we can't go back from GUI to terminal
302func Test_zz1_terminal_in_gui()
303 CheckCanRunGui
304
305 " Ignore the "failed to create input context" error.
306 call test_ignore_error('E285:')
307
308 gui -f
309
310 call assert_equal(1, winnr('$'))
311 let buf = Run_shell_in_terminal({'term_finish': 'close'})
312 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200313
314 " closing window wipes out the terminal buffer a with finished job
315 call WaitForAssert({-> assert_equal(1, winnr('$'))})
316 call assert_equal("", bufname(buf))
317
318 unlet g:job
319endfunc
320
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100321func Test_zz2_terminal_guioptions_bang()
322 CheckGui
323 set guioptions+=!
324
325 let filename = 'Xtestscript'
326 if has('win32')
327 let filename .= '.bat'
328 let prefix = ''
329 let contents = ['@echo off', 'exit %1']
330 else
331 let filename .= '.sh'
332 let prefix = './'
333 let contents = ['#!/bin/sh', 'exit $1']
334 endif
335 call writefile(contents, filename, 'D')
336 call setfperm(filename, 'rwxrwx---')
337
338 " Check if v:shell_error is equal to the exit status.
339 let exitval = 0
340 execute printf(':!%s%s %d', prefix, filename, exitval)
341 call assert_equal(exitval, v:shell_error)
342
343 let exitval = 9
344 execute printf(':!%s%s %d', prefix, filename, exitval)
345 call assert_equal(exitval, v:shell_error)
346
347 set guioptions&
348endfunc
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200349
350func Test_terminal_hidden()
351 CheckUnix
352
353 term ++hidden cat
354 let bnr = bufnr('$')
355 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
356 exe 'sbuf ' . bnr
357 call assert_equal('terminal', &buftype)
358 call term_sendkeys(bnr, "asdf\<CR>")
359 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
360 call term_sendkeys(bnr, "\<C-D>")
361 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
362 bwipe!
363endfunc
364
365func Test_terminal_switch_mode()
366 term
367 let bnr = bufnr('$')
368 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200369 " In the GUI the first switch sometimes doesn't work. Switch twice to avoid
Bram Moolenaar8e7d6222020-12-18 19:49:56 +0100370 " flakiness.
Bram Moolenaarc85156b2020-07-12 14:09:23 +0200371 call feedkeys("\<C-W>N", 'xt')
372 call feedkeys("A", 'xt')
373 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200374 call feedkeys("\<C-W>N", 'xt')
375 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
376 call feedkeys("A", 'xt')
377 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
378 call feedkeys("\<C-\>\<C-N>", 'xt')
379 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
380 call feedkeys("I", 'xt')
381 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
382 call feedkeys("\<C-W>Nv", 'xt')
383 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
384 call feedkeys("I", 'xt')
385 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
386 call feedkeys("\<C-W>Nv", 'xt')
387 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
388 call feedkeys("A", 'xt')
389 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
390 bwipe!
391endfunc
392
393func Test_terminal_normal_mode()
394 CheckRunVimInTerminal
395
396 " Run Vim in a terminal and open a terminal window to run Vim in.
397 let lines =<< trim END
398 call setline(1, range(11111, 11122))
399 3
400 END
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100401 call writefile(lines, 'XtermNormal', 'D')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200402 let buf = RunVimInTerminal('-S XtermNormal', {'rows': 8})
403 call TermWait(buf)
404
405 call term_sendkeys(buf, "\<C-W>N")
406 call term_sendkeys(buf, ":set number cursorline culopt=both\r")
407 call VerifyScreenDump(buf, 'Test_terminal_normal_1', {})
408
409 call term_sendkeys(buf, ":set culopt=number\r")
410 call VerifyScreenDump(buf, 'Test_terminal_normal_2', {})
411
412 call term_sendkeys(buf, ":set culopt=line\r")
413 call VerifyScreenDump(buf, 'Test_terminal_normal_3', {})
414
415 call assert_fails('call term_sendkeys(buf, [])', 'E730:')
416 call term_sendkeys(buf, "a:q!\<CR>:q\<CR>:q\<CR>")
417 call StopVimInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200418endfunc
419
420func Test_terminal_hidden_and_close()
421 CheckUnix
422
423 call assert_equal(1, winnr('$'))
424 term ++hidden ++close ls
425 let bnr = bufnr('$')
426 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
427 call WaitForAssert({-> assert_false(bufexists(bnr))})
428 call assert_equal(1, winnr('$'))
429endfunc
430
431func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200432 if has('conpty')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200433 throw 'Skipped: fail on ConPTY'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200434 endif
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100435 let g:test_is_flaky = 1
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200436 let contents = [
437 \ [ 'One', '', 'X' ],
438 \ [ 'Two', '', '' ],
439 \ [ 'Three' ] + repeat([''], 30)
440 \ ]
441
442 for c in contents
Bram Moolenaarc4860bd2022-10-15 20:52:26 +0100443 call writefile(c, 'Xdntfile', 'D')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200444 if has('win32')
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100445 term cmd /c type Xdntfile
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200446 else
Bram Moolenaarb18b4962022-09-02 21:55:50 +0100447 term cat Xdntfile
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200448 endif
449 let bnr = bufnr('$')
450 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
451 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
452 sleep 100m
453 call assert_equal(c, getline(1, line('$')))
454 quit
455 endfor
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200456endfunc
457
Bram Moolenaarb936b792020-09-04 18:34:09 +0200458func GetDummyCmd()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200459 if has('win32')
Bram Moolenaarb936b792020-09-04 18:34:09 +0200460 return 'cmd /c ""'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200461 else
462 CheckExecutable false
Bram Moolenaarb936b792020-09-04 18:34:09 +0200463 return 'false'
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200464 endif
Bram Moolenaarb936b792020-09-04 18:34:09 +0200465endfunc
466
467func Test_terminal_no_job()
468 let cmd = GetDummyCmd()
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200469 let term = term_start(cmd, {'term_finish': 'close'})
470 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
471endfunc
472
473func Test_term_getcursor()
474 CheckUnix
475
476 let buf = Run_shell_in_terminal({})
477
478 " Wait for the shell to display a prompt.
479 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
480
481 " Hide the cursor.
482 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
483 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
484
485 " Show the cursor.
486 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
487 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
488
489 " Change color of cursor.
490 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
491 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
492 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
493 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
494 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
495
496 " Make cursor a blinking block.
497 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
498 call WaitForAssert({-> assert_equal([1, 1],
499 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
500
501 " Make cursor a steady block.
502 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
503 call WaitForAssert({-> assert_equal([0, 1],
504 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
505
506 " Make cursor a blinking underline.
507 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
508 call WaitForAssert({-> assert_equal([1, 2],
509 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
510
511 " Make cursor a steady underline.
512 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
513 call WaitForAssert({-> assert_equal([0, 2],
514 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
515
516 " Make cursor a blinking vertical bar.
517 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
518 call WaitForAssert({-> assert_equal([1, 3],
519 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
520
521 " Make cursor a steady vertical bar.
522 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
523 call WaitForAssert({-> assert_equal([0, 3],
524 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
525
526 call StopShellInTerminal(buf)
527endfunc
528
529" Test for term_gettitle()
530func Test_term_gettitle()
531 " term_gettitle() returns an empty string for a non-terminal buffer
532 " and for a non-existing buffer.
533 call assert_equal('', bufnr('%')->term_gettitle())
534 call assert_equal('', term_gettitle(bufnr('$') + 1))
535
536 if !has('title') || empty(&t_ts)
537 throw "Skipped: can't get/set title"
538 endif
539
540 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', '-c', 'set title'])
Yegappan Lakshmanane446a012023-01-19 17:49:58 +0000541 call TermWait(term)
Bram Moolenaar3bb79dc2021-12-12 18:50:19 +0000542 " When Vim is running as a server then the title ends in VIM{number}, thus
543 " optionally match a number after "VIM".
544 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d*$', term_gettitle(term)) })
545 call term_sendkeys(term, ":e Xfoo\r")
546 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d*$', term_gettitle(term)) })
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200547
548 call term_sendkeys(term, ":set titlestring=foo\r")
549 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
550
551 exe term . 'bwipe!'
552endfunc
553
554func Test_term_gettty()
555 let buf = Run_shell_in_terminal({})
556 let gettty = term_gettty(buf)
557
558 if has('unix') && executable('tty')
559 " Find tty using the tty shell command.
560 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
561 call term_sendkeys(buf, "tty\r")
562 call WaitForAssert({-> assert_notequal('', term_getline(buf, 3))})
563 let tty = term_getline(buf, 2)
564 call assert_equal(tty, gettty)
565 endif
566
567 let gettty0 = term_gettty(buf, 0)
568 let gettty1 = term_gettty(buf, 1)
569
570 call assert_equal(gettty, gettty0)
571 call assert_equal(job_info(g:job).tty_out, gettty0)
572 call assert_equal(job_info(g:job).tty_in, gettty1)
573
574 if has('unix')
575 " For unix, term_gettty(..., 0) and term_gettty(..., 1)
576 " are identical according to :help term_gettty()
577 call assert_equal(gettty0, gettty1)
578 call assert_match('^/dev/', gettty)
579 else
580 " ConPTY works on anonymous pipe.
581 if !has('conpty')
582 call assert_match('^\\\\.\\pipe\\', gettty0)
583 call assert_match('^\\\\.\\pipe\\', gettty1)
584 endif
585 endif
586
Bram Moolenaarbade44e2020-09-26 22:39:24 +0200587 call assert_fails('call term_gettty(buf, 2)', 'E475:')
588 call assert_fails('call term_gettty(buf, -1)', 'E475:')
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200589
590 call assert_equal('', term_gettty(buf + 1))
591
592 call StopShellInTerminal(buf)
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200593 exe buf . 'bwipe'
594endfunc
595
Bram Moolenaar1112c0f2020-07-01 21:53:50 +0200596
597" vim: shiftwidth=2 sts=2 expandtab