blob: 7e8ef763b4eb8e51b58139f0561ce0f2e17a82fc [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature terminal
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02005
6source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01007source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02008
Bram Moolenaarb81bc772017-08-11 22:45:01 +02009let s:python = PythonProg()
Bram Moolenaarddd33082019-06-03 23:07:25 +020010let $PROMPT_COMMAND=''
Bram Moolenaarb81bc772017-08-11 22:45:01 +020011
Bram Moolenaar94053a52017-08-01 21:44:33 +020012" Open a terminal with a shell, assign the job to g:job and return the buffer
13" number.
Bram Moolenaar05aafed2017-08-11 19:12:11 +020014func Run_shell_in_terminal(options)
Bram Moolenaarba6febd2017-10-30 21:56:23 +010015 if has('win32')
16 let buf = term_start([&shell,'/k'], a:options)
17 else
18 let buf = term_start(&shell, a:options)
19 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020020
21 let termlist = term_list()
22 call assert_equal(1, len(termlist))
23 call assert_equal(buf, termlist[0])
24
25 let g:job = term_getjob(buf)
26 call assert_equal(v:t_job, type(g:job))
27
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020028 let string = string({'job': buf->term_getjob()})
Bram Moolenaar35422f42017-08-05 16:33:56 +020029 call assert_match("{'job': 'process \\d\\+ run'}", string)
30
Bram Moolenaar94053a52017-08-01 21:44:33 +020031 return buf
32endfunc
33
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020034func Test_terminal_basic()
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020035 au TerminalOpen * let b:done = 'yes'
Bram Moolenaar05aafed2017-08-11 19:12:11 +020036 let buf = Run_shell_in_terminal({})
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020037
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020038 if has("unix")
Bram Moolenaar2dc9d262017-09-08 14:39:30 +020039 call assert_match('^/dev/', job_info(g:job).tty_out)
40 call assert_match('^/dev/', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020041 else
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010042 " ConPTY works on anonymous pipe.
43 if !has('conpty')
44 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020045 call assert_match('^\\\\.\\pipe\\', ''->term_gettty())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010046 endif
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020047 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020048 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020049 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020050 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020051 call assert_match('%aR[^\n]*running]', execute('ls R'))
52 call assert_notmatch('%[^\n]*running]', execute('ls F'))
53 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020055 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +020056 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020057 call assert_equal('n', mode())
58 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020059 call assert_match('%aF[^\n]*finished]', execute('ls F'))
60 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
61 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020062
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 " closing window wipes out the terminal buffer a with finished job
64 close
65 call assert_equal("", bufname(buf))
66
Bram Moolenaar606cb8b2018-05-03 20:40:20 +020067 au! TerminalOpen
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020068 unlet g:job
69endfunc
70
71func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020072 let buf = Run_shell_in_terminal({})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020073 call StopShellInTerminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020074 call term_wait(buf)
75
76 setlocal modifiable
77 exe "normal Axxx\<Esc>"
78 call assert_fails(buf . 'bwipe', 'E517')
79 undo
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081 exe buf . 'bwipe'
82 unlet g:job
83endfunc
84
Bram Moolenaar5b868a82019-02-25 06:11:53 +010085func 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))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +020094 call WaitForAssert({-> assert_equal('text to paste 42', 2->getline())})
Bram Moolenaar5b868a82019-02-25 06:11:53 +010095
96 exe buf . 'bwipe!'
97 unlet g:job
98endfunc
99
Bram Moolenaar94053a52017-08-01 21:44:33 +0200100func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200101 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +0200102 call assert_fails(buf . 'bwipe', 'E517')
103 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200104 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +0200105 call assert_equal("", bufname(buf))
106
107 unlet g:job
108endfunc
109
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200110func Test_terminal_split_quit()
111 let buf = Run_shell_in_terminal({})
112 call term_wait(buf)
113 split
114 quit!
115 call term_wait(buf)
116 sleep 50m
117 call assert_equal('run', job_status(g:job))
118
119 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200120 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200121
122 exe buf . 'bwipe'
123 unlet g:job
124endfunc
125
Bram Moolenaar94053a52017-08-01 21:44:33 +0200126func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200127 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200128 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200129 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'
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200137 call StopShellInTerminal(buf)
Bram Moolenaar94053a52017-08-01 21:44:33 +0200138 exe buf . 'bwipe'
139
140 unlet g:job
141endfunc
142
Bram Moolenaar1e115362019-01-09 23:01:02 +0100143func s:Nasty_exit_cb(job, st)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200144 exe g:buf . 'bwipe!'
145 let g:buf = 0
146endfunc
147
Bram Moolenaar9d189612017-09-09 18:11:00 +0200148func Get_cat_123_cmd()
149 if has('win32')
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100150 if !has('conpty')
151 return 'cmd /c "cls && color 2 && echo 123"'
152 else
153 " When clearing twice, extra sequence is not output.
154 return 'cmd /c "cls && cls && color 2 && echo 123"'
155 endif
Bram Moolenaar9d189612017-09-09 18:11:00 +0200156 else
157 call writefile(["\<Esc>[32m123"], 'Xtext')
158 return "cat Xtext"
159 endif
160endfunc
161
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200163 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200164 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
165 let g:job = term_getjob(g:buf)
166
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200167 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
168 call WaitForAssert({-> assert_equal(0, g:buf)})
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200169 unlet g:job
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100170 unlet g:buf
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200171 call delete('Xtext')
172endfunc
173
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200174func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200175 let l = term_scrape(a:buf, 0)
176 call assert_true(len(l) == 0)
177 let l = term_scrape(a:buf, 999)
178 call assert_true(len(l) == 0)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200179 let l = a:buf->term_scrape(1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200180 call assert_true(len(l) > 0)
181 call assert_equal('1', l[0].chars)
182 call assert_equal('2', l[1].chars)
183 call assert_equal('3', l[2].chars)
184 call assert_equal('#00e000', l[0].fg)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200185 call assert_equal(0, term_getattr(l[0].attr, 'bold'))
186 call assert_equal(0, l[0].attr->term_getattr('italic'))
Bram Moolenaar81df6352018-12-22 18:25:30 +0100187 if has('win32')
188 " On Windows 'background' always defaults to dark, even though the terminal
189 " may use a light background. Therefore accept both white and black.
190 call assert_match('#ffffff\|#000000', l[0].bg)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200191 else
Bram Moolenaar81df6352018-12-22 18:25:30 +0100192 if &background == 'light'
193 call assert_equal('#ffffff', l[0].bg)
194 else
195 call assert_equal('#000000', l[0].bg)
196 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200197 endif
198
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200199 let l = term_getline(a:buf, -1)
200 call assert_equal('', l)
201 let l = term_getline(a:buf, 0)
202 call assert_equal('', l)
203 let l = term_getline(a:buf, 999)
204 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200205 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200206 call assert_equal('123', l)
207endfunc
208
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200209func Test_terminal_scrape_123()
210 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200211 let buf = term_start(cmd)
212
213 let termlist = term_list()
214 call assert_equal(1, len(termlist))
215 call assert_equal(buf, termlist[0])
216
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200217 " Nothing happens with invalid buffer number
218 call term_wait(1234)
219
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200220 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200221 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200222 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200223 call WaitForAssert({-> assert_equal(3, len(term_scrape(buf, 1)))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200224 call Check_123(buf)
225
226 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100227 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200228 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200229 call term_wait(buf)
230 call Check_123(buf)
231
232 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200233 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200234endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200235
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200236func Test_terminal_scrape_multibyte()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200237 call writefile(["léttまrs"], 'Xtext')
238 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200239 " Run cmd with UTF-8 codepage to make the type command print the expected
240 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100241 let buf = term_start("cmd /K chcp 65001")
242 call term_sendkeys(buf, "type Xtext\<CR>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200243 eval buf->term_sendkeys("exit\<CR>")
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100244 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200245 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100246 let buf = term_start("cat Xtext")
247 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200248 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200249
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100250 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
251 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200252 call assert_true(len(l) >= 7)
253 call assert_equal('l', l[0].chars)
254 call assert_equal('é', l[1].chars)
255 call assert_equal(1, l[1].width)
256 call assert_equal('t', l[2].chars)
257 call assert_equal('t', l[3].chars)
258 call assert_equal('ま', l[4].chars)
259 call assert_equal(2, l[4].width)
260 call assert_equal('r', l[5].chars)
261 call assert_equal('s', l[6].chars)
262
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100263 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200264 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100265 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200266
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100267 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200268 call delete('Xtext')
269endfunc
270
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200271func Test_terminal_scroll()
272 call writefile(range(1, 200), 'Xtext')
273 if has('win32')
274 let cmd = 'cmd /c "type Xtext"'
275 else
276 let cmd = "cat Xtext"
277 endif
278 let buf = term_start(cmd)
279
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100280 let job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200281 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200282 call term_wait(buf)
283 if has('win32')
284 " TODO: this should not be needed
285 sleep 100m
286 endif
287
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200288 let scrolled = buf->term_getscrolled()
289 call assert_equal(scrolled, term_getscrolled(buf))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200290 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200291 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200292 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200293 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200294 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200295 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200296
297 exe buf . 'bwipe'
298 call delete('Xtext')
299endfunc
300
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200301func Test_terminal_scrollback()
Bram Moolenaar33c5e9f2018-05-26 18:58:51 +0200302 let buf = Run_shell_in_terminal({'term_rows': 15})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200303 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200304 call writefile(range(150), 'Xtext')
305 if has('win32')
306 call term_sendkeys(buf, "type Xtext\<CR>")
307 else
308 call term_sendkeys(buf, "cat Xtext\<CR>")
309 endif
310 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200311 " On MS-Windows there is an empty line, check both last line and above it.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200312 call WaitForAssert({-> assert_match( '149', term_getline(buf, rows - 1) . term_getline(buf, rows - 2))})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200313 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200314 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200315
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200316 call StopShellInTerminal(buf)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200317 call term_wait(buf)
318 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200319 set termwinscroll&
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100320 call delete('Xtext')
321endfunc
322
323func Test_terminal_postponed_scrollback()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200324 " tail -f only works on Unix
325 CheckUnix
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100326
327 call writefile(range(50), 'Xtext')
328 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100329 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100330 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100331 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100332 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
333 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100334 \ 'call feedkeys("\<C-W>N", "xt")',
335 \ ], 'XTest_postponed')
336 let buf = RunVimInTerminal('-S XTest_postponed', {})
337 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar96baf022019-02-14 23:49:38 +0100338 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100339
340 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200341 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100342 call term_sendkeys(buf, "k")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100343 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100344
345 " Back to Terminal-Job mode, text will scroll and show the extra line.
346 call term_sendkeys(buf, "a")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100347 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100348
349 call term_wait(buf)
350 call term_sendkeys(buf, "\<C-C>")
351 call term_wait(buf)
352 call term_sendkeys(buf, "exit\<CR>")
353 call term_wait(buf)
354 call term_sendkeys(buf, ":q\<CR>")
355 call StopVimInTerminal(buf)
356 call delete('XTest_postponed')
357 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200358endfunc
359
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100360" Run diff on two dumps with different size.
361func Test_terminal_dumpdiff_size()
362 call assert_equal(1, winnr('$'))
363 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
364 call assert_equal(2, winnr('$'))
365 call assert_match('Test_incsearch_search_01.dump', getline(10))
366 call assert_match(' +++++$', getline(11))
367 call assert_match('Test_popup_command_01.dump', getline(31))
368 call assert_equal(repeat('+', 75), getline(30))
369 quit
370endfunc
371
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200372func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200373 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200374
Bram Moolenaarb2412082017-08-20 18:09:14 +0200375 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200376 let size = term_getsize('')
377 bwipe!
378 call assert_equal(5, size[0])
379
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200380 call term_start(cmd, {'term_rows': 6})
381 let size = term_getsize('')
382 bwipe!
383 call assert_equal(6, size[0])
384
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200385 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200386 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200387 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200388
389 call term_setsize('', 6, 0)
390 call assert_equal([6, 33], term_getsize(''))
391
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200392 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200393 call assert_equal([6, 35], term_getsize(''))
394
395 call term_setsize('', 7, 30)
396 call assert_equal([7, 30], term_getsize(''))
397
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200398 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200399 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200400
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200401 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
402 let size = term_getsize('')
403 bwipe!
404 call assert_equal([6, 36], size)
405
Bram Moolenaarb2412082017-08-20 18:09:14 +0200406 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200407 let size = term_getsize('')
408 bwipe!
409 call assert_equal(20, size[1])
410
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200411 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200412 let size = term_getsize('')
413 bwipe!
414 call assert_equal(26, size[1])
415
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200416 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200417 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200418 let size = term_getsize('')
419 bwipe!
420 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200421
422 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
423 let size = term_getsize('')
424 bwipe!
425 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200426
427 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200428endfunc
429
430func Test_terminal_curwin()
431 let cmd = Get_cat_123_cmd()
432 call assert_equal(1, winnr('$'))
433
434 split dummy
435 exe 'terminal ++curwin ' . cmd
436 call assert_equal(2, winnr('$'))
437 bwipe!
438
439 split dummy
440 call term_start(cmd, {'curwin': 1})
441 call assert_equal(2, winnr('$'))
442 bwipe!
443
444 split dummy
445 call setline(1, 'change')
446 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
447 call assert_equal(2, winnr('$'))
448 exe 'terminal! ++curwin ' . cmd
449 call assert_equal(2, winnr('$'))
450 bwipe!
451
452 split dummy
453 call setline(1, 'change')
454 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
455 call assert_equal(2, winnr('$'))
456 bwipe!
457
458 split dummy
459 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200460 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200461endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462
Bram Moolenaarff546792017-11-21 14:47:57 +0100463func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200464 if s:python != ''
465 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200466 " 500 was not enough for Travis
467 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200468 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200469 echo 'This will take five seconds...'
470 let waittime = 2000
471 if has('win32')
472 let cmd = $windir . '\system32\timeout.exe 1'
473 else
474 let cmd = 'sleep 1'
475 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200476 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100477 return [cmd, waittime]
478endfunc
479
480func Test_terminal_finish_open_close()
481 call assert_equal(1, winnr('$'))
482
483 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200484
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100485 " shell terminal closes automatically
486 terminal
487 let buf = bufnr('%')
488 call assert_equal(2, winnr('$'))
489 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200490 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200491 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200492 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100493
494 " shell terminal that does not close automatically
495 terminal ++noclose
496 let buf = bufnr('%')
497 call assert_equal(2, winnr('$'))
498 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200499 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200500 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100501 call assert_equal(2, winnr('$'))
502 quit
503 call assert_equal(1, winnr('$'))
504
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200505 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200506 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200507 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200508 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200509
510 call term_start(cmd, {'term_finish': 'close'})
511 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200512 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200513 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200514 call assert_equal(1, winnr('$'))
515
516 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200517 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200518 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200519 bwipe
520
521 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200522 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200523 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200524 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200525
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200526 exe 'terminal ++hidden ++open ' . cmd
527 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200528 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200529 bwipe
530
531 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
532 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200533 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200534 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200535
536 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
537 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
538 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
539 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
540
541 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200542 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200543 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200544 call assert_equal(4, winheight(0))
545 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200546endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200547
548func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200549 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200550 return
551 endif
552 call mkdir('Xdir')
553 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200554 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200555
556 exe buf . 'bwipe'
557 call delete('Xdir', 'rf')
558endfunc
559
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200560func Test_terminal_cwd_failure()
561 " Case 1: Provided directory is not actually a directory. Attempt to make
562 " the file executable as well.
563 call writefile([], 'Xfile')
564 call setfperm('Xfile', 'rwx------')
565 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
566 call delete('Xfile')
567
568 " Case 2: Directory does not exist.
569 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
570
571 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100572 " Skip this for root, it will be accessible anyway.
Bram Moolenaar07282f02019-10-10 16:46:17 +0200573 if !IsRoot()
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100574 call mkdir('XdirNoAccess', '', '0600')
575 " return early if the directory permissions could not be set properly
576 if getfperm('XdirNoAccess')[2] == 'x'
577 call delete('XdirNoAccess', 'rf')
578 return
579 endif
580 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
581 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200582 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200583endfunc
584
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100585func Test_terminal_servername()
586 if !has('clientserver')
587 return
588 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200589 call s:test_environment("VIM_SERVERNAME", v:servername)
590endfunc
591
592func Test_terminal_version()
593 call s:test_environment("VIM_TERMINAL", string(v:version))
594endfunc
595
596func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100597 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100598 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200599 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100600 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200601 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100602 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200603 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100604 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100605 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200606 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200607 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100608
Bram Moolenaar012eb662018-03-13 17:55:27 +0100609 exe buf . 'bwipe'
610 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100611endfunc
612
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200613func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100614 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200615 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200616 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100617 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100618 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100619 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100620 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100621 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200622 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200623 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200624 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200625
Bram Moolenaar012eb662018-03-13 17:55:27 +0100626 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200627endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200628
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200629func Test_terminal_list_args()
630 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
631 call assert_fails(buf . 'bwipe', 'E517')
632 exe buf . 'bwipe!'
633 call assert_equal("", bufname(buf))
634endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200635
636func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100637 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100638 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200639 " The shell or something else has a problem dealing with more than 1000
640 " characters at the same time.
641 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100642 " NPFS is used in Windows, nonblocking mode does not work properly.
643 elseif has('win32')
644 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200645 else
646 let len = 5000
647 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200648
649 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100650 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200651 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100652 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200653
654 " On MS-Windows there is an extra empty line below "done". Find "done" in
655 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100656 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100657 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100658 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200659 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100660 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200661 endif
662 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200663
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100664 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200665 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100666 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200667 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200668 bwipe
669endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200670
671func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200672 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200673 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200674 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200675 if has('win32')
676 " TODO: enable once writing to stdin works on MS-Windows
677 return
678 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200679 new
680 call setline(1, ['one', 'two', 'three'])
681 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200682 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200683 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200684 call assert_equal(['3', '3', '14'], nrs)
685 bwipe
686
Bram Moolenaardada6d22017-09-02 17:18:35 +0200687 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200688 call setline(1, ['one', 'two', 'three', 'four'])
689 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200690 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200691 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200692 call assert_equal(['2', '2', '10'], nrs)
693 bwipe
694
Bram Moolenaardada6d22017-09-02 17:18:35 +0200695 if executable('python')
696 new
697 call setline(1, ['print("hello")'])
698 1term ++eof=exit() python
699 " MS-Windows echoes the input, Unix doesn't.
700 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
701 if getline(1) =~ 'hello'
702 call assert_equal('hello', getline(1))
703 else
704 call assert_equal('hello', getline(line('$') - 1))
705 endif
706 bwipe
707
708 if has('win32')
709 new
710 call setline(1, ['print("hello")'])
711 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200712 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200713 call assert_equal('hello', getline(line('$') - 1))
714 bwipe
715 endif
716 endif
717
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200718 bwipe!
719endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200720
721func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200722 let buf = term_start('NONE', {})
723 call assert_notequal(0, buf)
724
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200725 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200726 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100727 if has('gui_running') && !has('win32')
728 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200729 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100730 else
731 " Otherwise using a job works on all systems.
732 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200733 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200734 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200735
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200736 bwipe!
737endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200738
739func Test_terminal_special_chars()
740 " this file name only works on Unix
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200741 CheckUnix
742
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200743 call mkdir('Xdir with spaces')
744 call writefile(['x'], 'Xdir with spaces/quoted"file')
745 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200746 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200747 call term_wait('')
748
749 call delete('Xdir with spaces', 'rf')
750 bwipe
751endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200752
753func Test_terminal_wrong_options()
754 call assert_fails('call term_start(&shell, {
755 \ "in_io": "file",
756 \ "in_name": "xxx",
757 \ "out_io": "file",
758 \ "out_name": "xxx",
759 \ "err_io": "file",
760 \ "err_name": "xxx"
761 \ })', 'E474:')
762 call assert_fails('call term_start(&shell, {
763 \ "out_buf": bufnr("%")
764 \ })', 'E474:')
765 call assert_fails('call term_start(&shell, {
766 \ "err_buf": bufnr("%")
767 \ })', 'E474:')
768endfunc
769
770func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200771 let cmd = Get_cat_123_cmd()
772 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
773 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100774 " ConPTY may precede escape sequence. There are things that are not so.
775 if !has('conpty')
776 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
777 call assert_match('123', readfile('Xfile')[0])
778 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200779 let g:job = term_getjob(buf)
780 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
781 call delete('Xfile')
782 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200783
784 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200785 call writefile(['one line'], 'Xfile')
786 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
787 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200788 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200789 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200790 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200791 bwipe
792 call delete('Xfile')
793 endif
794endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200795
796func TerminalTmap(remap)
797 let buf = Run_shell_in_terminal({})
798 call assert_equal('t', mode())
799
800 if a:remap
801 tmap 123 456
802 else
803 tnoremap 123 456
804 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100805 " don't use abcde, it's an existing command
806 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200807 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100808 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200809 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200810 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200811 let lnum = term_getcursor(buf)[0]
812 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100813 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200814 else
815 call assert_match('456', term_getline(buf, lnum))
816 endif
817
818 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200819 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200820 call term_wait(buf)
821
822 tunmap 123
823 tunmap 456
824 call assert_equal('', maparg('123', 't'))
825 close
826 unlet g:job
827endfunc
828
829func Test_terminal_tmap()
830 call TerminalTmap(1)
831 call TerminalTmap(0)
832endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200833
834func Test_terminal_wall()
835 let buf = Run_shell_in_terminal({})
836 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200837 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200838 call term_wait(buf)
839 exe buf . 'bwipe'
840 unlet g:job
841endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200842
Bram Moolenaar7a760922018-02-19 23:10:02 +0100843func Test_terminal_wqall()
844 let buf = Run_shell_in_terminal({})
845 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200846 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100847 call term_wait(buf)
848 exe buf . 'bwipe'
849 unlet g:job
850endfunc
851
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200852func Test_terminal_composing_unicode()
853 let save_enc = &encoding
854 set encoding=utf-8
855
856 if has('win32')
857 let cmd = "cmd /K chcp 65001"
858 let lnum = [3, 6, 9]
859 else
860 let cmd = &shell
861 let lnum = [1, 3, 5]
862 endif
863
864 enew
865 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100866 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200867 call term_wait(buf, 50)
868
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200869 if has('win32')
870 call assert_equal('cmd', job_info(g:job).cmd[0])
871 else
872 call assert_equal(&shell, job_info(g:job).cmd[0])
873 endif
874
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200875 " ascii + composing
876 let txt = "a\u0308bc"
877 call term_sendkeys(buf, "echo " . txt . "\r")
878 call term_wait(buf, 50)
879 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
880 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
881 let l = term_scrape(buf, lnum[0] + 1)
882 call assert_equal("a\u0308", l[0].chars)
883 call assert_equal("b", l[1].chars)
884 call assert_equal("c", l[2].chars)
885
886 " multibyte + composing
887 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
888 call term_sendkeys(buf, "echo " . txt . "\r")
889 call term_wait(buf, 50)
890 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
891 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
892 let l = term_scrape(buf, lnum[1] + 1)
893 call assert_equal("\u304b\u3099", l[0].chars)
894 call assert_equal("\u304e", l[1].chars)
895 call assert_equal("\u304f\u3099", l[2].chars)
896 call assert_equal("\u3052", l[3].chars)
897 call assert_equal("\u3053\u3099", l[4].chars)
898
899 " \u00a0 + composing
900 let txt = "abc\u00a0\u0308"
901 call term_sendkeys(buf, "echo " . txt . "\r")
902 call term_wait(buf, 50)
903 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
904 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
905 let l = term_scrape(buf, lnum[2] + 1)
906 call assert_equal("\u00a0\u0308", l[3].chars)
907
908 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200909 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200910 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100911 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200912 let &encoding = save_enc
913endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100914
915func Test_terminal_aucmd_on_close()
916 fun Nop()
917 let s:called = 1
918 endfun
919
920 aug repro
921 au!
922 au BufWinLeave * call Nop()
923 aug END
924
925 let [cmd, waittime] = s:get_sleep_cmd()
926
927 call assert_equal(1, winnr('$'))
928 new
929 call setline(1, ['one', 'two'])
930 exe 'term ++close ' . cmd
931 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200932 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100933 call assert_equal(1, s:called)
934 bwipe!
935
936 unlet s:called
937 au! repro
938 delfunc Nop
939endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100940
941func Test_terminal_term_start_empty_command()
942 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
943 call assert_fails(cmd, 'E474')
944 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
945 call assert_fails(cmd, 'E474')
946 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
947 call assert_fails(cmd, 'E474')
948 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
949 call assert_fails(cmd, 'E474')
950endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100951
952func Test_terminal_response_to_control_sequence()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +0200953 CheckUnix
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100954
955 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200956 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100957
Bram Moolenaar086eb872018-03-25 21:24:12 +0200958 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200959 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100960
Bram Moolenaar086eb872018-03-25 21:24:12 +0200961 " Request the cursor position.
962 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100963
964 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200965 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100966
Bram Moolenaar086eb872018-03-25 21:24:12 +0200967 " End "cat" gently.
968 call term_sendkeys(buf, "\<CR>\<C-D>")
969
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200970 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100971 exe buf . 'bwipe'
972 unlet g:job
973endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100974
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100975" Run Vim, start a terminal in that Vim with the kill argument,
976" :qall works.
977func Run_terminal_qall_kill(line1, line2)
978 " 1. Open a terminal window and wait for the prompt to appear
979 " 2. set kill using term_setkill()
980 " 3. make Vim exit, it will kill the shell
981 let after = [
982 \ a:line1,
983 \ 'let buf = bufnr("%")',
984 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
985 \ ' sleep 10m',
986 \ 'endwhile',
987 \ a:line2,
988 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
989 \ 'qall',
990 \ ]
991 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100992 return
993 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100994 call assert_equal("done", readfile("Xdone")[0])
995 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100996endfunc
997
998" Run Vim in a terminal, then start a terminal in that Vim with a kill
999" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001000func Test_terminal_qall_kill_arg()
1001 call Run_terminal_qall_kill('term ++kill=kill', '')
1002endfunc
1003
1004" Run Vim, start a terminal in that Vim, set the kill argument with
1005" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001006func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001007 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001008endfunc
1009
1010" Run Vim, start a terminal in that Vim without the kill argument,
1011" check that :qall does not exit, :qall! does.
1012func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001013 let after =<< trim [CODE]
1014 term
1015 let buf = bufnr("%")
1016 while term_getline(buf, 1) =~ "^\\s*$"
1017 sleep 10m
1018 endwhile
1019 set nomore
1020 au VimLeavePre * call writefile(["too early"], "Xdone")
1021 qall
1022 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1023 cquit
1024 [CODE]
1025
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001026 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001027 return
1028 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001029 call assert_equal("done", readfile("Xdone")[0])
1030 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001031endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001032
1033" Run Vim in a terminal, then start a terminal in that Vim without a kill
1034" argument, check that :confirm qall works.
1035func Test_terminal_qall_prompt()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001036 CheckRunVimInTerminal
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001037 let buf = RunVimInTerminal('', {})
1038
1039 " Open a terminal window and wait for the prompt to appear
1040 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001041 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1042 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001043
1044 " make Vim exit, it will prompt to kill the shell
1045 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001046 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001047 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001048 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001049
1050 " close the terminal window where Vim was running
1051 quit
1052endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001053
Bram Moolenaar012eb662018-03-13 17:55:27 +01001054func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001055 augroup repro
1056 au!
1057 au TerminalOpen * let s:called += 1
1058 augroup END
1059
1060 let s:called = 0
1061
1062 " Open a terminal window with :terminal
1063 terminal
1064 call assert_equal(1, s:called)
1065 bwipe!
1066
1067 " Open a terminal window with term_start()
1068 call term_start(&shell)
1069 call assert_equal(2, s:called)
1070 bwipe!
1071
1072 " Open a hidden terminal buffer with :terminal
1073 terminal ++hidden
1074 call assert_equal(3, s:called)
1075 for buf in term_list()
1076 exe buf . "bwipe!"
1077 endfor
1078
1079 " Open a hidden terminal buffer with term_start()
1080 let buf = term_start(&shell, {'hidden': 1})
1081 call assert_equal(4, s:called)
1082 exe buf . "bwipe!"
1083
1084 unlet s:called
1085 au! repro
1086endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001087
1088func Check_dump01(off)
1089 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1090 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001091 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001092endfunc
1093
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001094func Test_terminal_dumpwrite_composing()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001095 CheckRunVimInTerminal
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001096 let save_enc = &encoding
1097 set encoding=utf-8
1098 call assert_equal(1, winnr('$'))
1099
1100 let text = " a\u0300 e\u0302 o\u0308"
1101 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001102 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001103 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001104 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001105 let dumpline = readfile('Xdump')[0]
1106 call assert_match('|à| |ê| |ö', dumpline)
1107
1108 call StopVimInTerminal(buf)
1109 call delete('Xcomposing')
1110 call delete('Xdump')
1111 let &encoding = save_enc
1112endfunc
1113
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001114" just testing basic functionality.
1115func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001116 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001117 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001118 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001119 call assert_equal(2, winnr('$'))
1120 call assert_equal(20, line('$'))
1121 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001122
1123 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001124 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001125 call assert_equal(buf, buf2)
1126 call assert_notequal('one two three four five', trim(getline(1)))
1127
1128 " Load the first dump again in the same window
1129 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1130 call assert_equal(buf, buf2)
1131 call Check_dump01(0)
1132
1133 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1134 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1135 new
1136 let closedbuf = winbufnr('')
1137 quit
1138 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1139
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001140 quit
1141endfunc
1142
1143func Test_terminal_dumpdiff()
1144 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001145 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001146 call assert_equal(2, winnr('$'))
1147 call assert_equal(62, line('$'))
1148 call Check_dump01(0)
1149 call Check_dump01(42)
1150 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1151 quit
1152endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001153
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001154func Test_terminal_dumpdiff_swap()
1155 call assert_equal(1, winnr('$'))
1156 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1157 call assert_equal(2, winnr('$'))
1158 call assert_equal(62, line('$'))
1159 call assert_match('Test_popup_command_01.dump', getline(21))
1160 call assert_match('Test_popup_command_03.dump', getline(42))
1161 call assert_match('Undo', getline(3))
1162 call assert_match('three four five', getline(45))
1163
1164 normal s
1165 call assert_match('Test_popup_command_03.dump', getline(21))
1166 call assert_match('Test_popup_command_01.dump', getline(42))
1167 call assert_match('three four five', getline(3))
1168 call assert_match('Undo', getline(45))
1169 quit
1170endfunc
1171
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001172func Test_terminal_dumpdiff_options()
1173 set laststatus=0
1174 call assert_equal(1, winnr('$'))
1175 let height = winheight(0)
1176 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1177 call assert_equal(2, winnr('$'))
1178 call assert_equal(height, winheight(winnr()))
1179 call assert_equal(33, winwidth(winnr()))
1180 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1181 quit
1182
1183 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001184 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1185 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001186 call assert_equal(&columns, winwidth(0))
1187 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001188 call assert_equal('something else', bufname('%'))
1189 quit
1190
1191 call assert_equal(1, winnr('$'))
1192 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1193 call assert_equal(1, winnr('$'))
1194 bwipe
1195
1196 set laststatus&
1197endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001198
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001199func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001200 call assert_equal(1, winnr('$'))
1201
1202 " Use the title termcap entries to output the escape sequence.
1203 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001204 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001205 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001206 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001207 \ 'redraw',
1208 \ "set t_ts=",
1209 \ ], 'Xscript')
1210 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001211 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001212 call assert_equal('Xtextfile', expand('%:t'))
1213 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001214 return buf
1215endfunc
1216
1217func Test_terminal_api_drop_newwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001218 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001219 let buf = Api_drop_common('')
1220 call assert_equal(0, &bin)
1221 call assert_equal('', &fenc)
1222
1223 call StopVimInTerminal(buf)
1224 call delete('Xscript')
1225 bwipe Xtextfile
1226endfunc
1227
1228func Test_terminal_api_drop_newwin_bin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001229 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001230 let buf = Api_drop_common(',{"bin":1}')
1231 call assert_equal(1, &bin)
1232
1233 call StopVimInTerminal(buf)
1234 call delete('Xscript')
1235 bwipe Xtextfile
1236endfunc
1237
1238func Test_terminal_api_drop_newwin_binary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001239 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001240 let buf = Api_drop_common(',{"binary":1}')
1241 call assert_equal(1, &bin)
1242
1243 call StopVimInTerminal(buf)
1244 call delete('Xscript')
1245 bwipe Xtextfile
1246endfunc
1247
1248func Test_terminal_api_drop_newwin_nobin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001249 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001250 set binary
1251 let buf = Api_drop_common(',{"nobin":1}')
1252 call assert_equal(0, &bin)
1253
1254 call StopVimInTerminal(buf)
1255 call delete('Xscript')
1256 bwipe Xtextfile
1257 set nobinary
1258endfunc
1259
1260func Test_terminal_api_drop_newwin_nobinary()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001261 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001262 set binary
1263 let buf = Api_drop_common(',{"nobinary":1}')
1264 call assert_equal(0, &bin)
1265
1266 call StopVimInTerminal(buf)
1267 call delete('Xscript')
1268 bwipe Xtextfile
1269 set nobinary
1270endfunc
1271
1272func Test_terminal_api_drop_newwin_ff()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001273 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001274 let buf = Api_drop_common(',{"ff":"dos"}')
1275 call assert_equal("dos", &ff)
1276
1277 call StopVimInTerminal(buf)
1278 call delete('Xscript')
1279 bwipe Xtextfile
1280endfunc
1281
1282func Test_terminal_api_drop_newwin_fileformat()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001283 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001284 let buf = Api_drop_common(',{"fileformat":"dos"}')
1285 call assert_equal("dos", &ff)
1286
1287 call StopVimInTerminal(buf)
1288 call delete('Xscript')
1289 bwipe Xtextfile
1290endfunc
1291
1292func Test_terminal_api_drop_newwin_enc()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001293 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001294 let buf = Api_drop_common(',{"enc":"utf-16"}')
1295 call assert_equal("utf-16", &fenc)
1296
1297 call StopVimInTerminal(buf)
1298 call delete('Xscript')
1299 bwipe Xtextfile
1300endfunc
1301
1302func Test_terminal_api_drop_newwin_encoding()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001303 CheckRunVimInTerminal
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001304 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1305 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001306
1307 call StopVimInTerminal(buf)
1308 call delete('Xscript')
1309 bwipe Xtextfile
1310endfunc
1311
1312func Test_terminal_api_drop_oldwin()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001313 CheckRunVimInTerminal
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001314 let firstwinid = win_getid()
1315 split Xtextfile
1316 let textfile_winid = win_getid()
1317 call assert_equal(2, winnr('$'))
1318 call win_gotoid(firstwinid)
1319
1320 " Use the title termcap entries to output the escape sequence.
1321 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001322 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001323 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1324 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1325 \ 'redraw',
1326 \ "set t_ts=",
1327 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001328 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001329 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001330 call assert_equal(textfile_winid, win_getid())
1331
1332 call StopVimInTerminal(buf)
1333 call delete('Xscript')
1334 bwipe Xtextfile
1335endfunc
1336
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001337func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001338 let g:called_bufnum = a:bufnum
1339 let g:called_arg = a:arg
1340endfunc
1341
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001342func WriteApiCall(funcname)
1343 " Use the title termcap entries to output the escape sequence.
1344 call writefile([
1345 \ 'set title',
1346 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1347 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1348 \ 'redraw',
1349 \ "set t_ts=",
1350 \ ], 'Xscript')
1351endfunc
1352
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001353func Test_terminal_api_call()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001354 CheckRunVimInTerminal
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001355
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001356 unlet! g:called_bufnum
1357 unlet! g:called_arg
1358
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001359 call WriteApiCall('Tapi_TryThis')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001360
1361 " Default
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001362 let buf = RunVimInTerminal('-S Xscript', {})
1363 call WaitFor({-> exists('g:called_bufnum')})
1364 call assert_equal(buf, g:called_bufnum)
1365 call assert_equal(['hello', 123], g:called_arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001366 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001367
1368 unlet! g:called_bufnum
1369 unlet! g:called_arg
1370
1371 " Enable explicitly
1372 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'Tapi_Try'})
1373 call WaitFor({-> exists('g:called_bufnum')})
1374 call assert_equal(buf, g:called_bufnum)
1375 call assert_equal(['hello', 123], g:called_arg)
1376 call StopVimInTerminal(buf)
1377
1378 unlet! g:called_bufnum
1379 unlet! g:called_arg
1380
1381 func! ApiCall_TryThis(bufnum, arg)
1382 let g:called_bufnum2 = a:bufnum
1383 let g:called_arg2 = a:arg
1384 endfunc
1385
1386 call WriteApiCall('ApiCall_TryThis')
1387
1388 " Use prefix match
1389 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'ApiCall_'})
1390 call WaitFor({-> exists('g:called_bufnum2')})
1391 call assert_equal(buf, g:called_bufnum2)
1392 call assert_equal(['hello', 123], g:called_arg2)
1393 call StopVimInTerminal(buf)
1394
1395 unlet! g:called_bufnum2
1396 unlet! g:called_arg2
1397
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001398 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001399 delfunction! ApiCall_TryThis
1400 unlet! g:called_bufnum2
1401 unlet! g:called_arg2
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001402endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001403
1404func Test_terminal_api_call_fails()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001405 CheckRunVimInTerminal
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001406
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001407 func! TryThis(bufnum, arg)
1408 let g:called_bufnum3 = a:bufnum
1409 let g:called_arg3 = a:arg
1410 endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001411
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001412 call WriteApiCall('TryThis')
1413
1414 unlet! g:called_bufnum3
1415 unlet! g:called_arg3
1416
1417 " Not permitted
1418 call ch_logfile('Xlog', 'w')
1419 let buf = RunVimInTerminal('-S Xscript', {'term_api': ''})
1420 call WaitForAssert({-> assert_match('Unpermitted function: TryThis', string(readfile('Xlog')))})
1421 call assert_false(exists('g:called_bufnum3'))
1422 call assert_false(exists('g:called_arg3'))
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001423 call StopVimInTerminal(buf)
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001424
1425 " No match
1426 call ch_logfile('Xlog', 'w')
1427 let buf = RunVimInTerminal('-S Xscript', {'term_api': 'TryThat'})
1428 call WaitFor({-> string(readfile('Xlog')) =~ 'Unpermitted function: TryThis'})
1429 call assert_false(exists('g:called_bufnum3'))
1430 call assert_false(exists('g:called_arg3'))
1431 call StopVimInTerminal(buf)
1432
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001433 call delete('Xscript')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001434 call ch_logfile('')
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001435 call delete('Xlog')
Bram Moolenaard2842ea2019-09-26 23:08:54 +02001436 delfunction! TryThis
1437 unlet! g:called_bufnum3
1438 unlet! g:called_arg3
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001439endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001440
Bram Moolenaara997b452018-04-17 23:24:06 +02001441let s:caught_e937 = 0
1442
1443func Tapi_Delete(bufnum, arg)
1444 try
1445 execute 'bdelete!' a:bufnum
1446 catch /E937:/
1447 let s:caught_e937 = 1
1448 endtry
1449endfunc
1450
1451func Test_terminal_api_call_fail_delete()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001452 CheckRunVimInTerminal
Bram Moolenaara997b452018-04-17 23:24:06 +02001453
1454 call WriteApiCall('Tapi_Delete')
1455 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001456 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001457
1458 call StopVimInTerminal(buf)
1459 call delete('Xscript')
1460 call ch_logfile('', '')
1461endfunc
1462
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001463func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001464 if !exists('*term_getansicolors')
1465 throw 'Skipped: term_getansicolors() not supported'
1466 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001467 let colors = [
1468 \ '#000000', '#e00000',
1469 \ '#00e000', '#e0e000',
1470 \ '#0000e0', '#e000e0',
1471 \ '#00e0e0', '#e0e0e0',
1472 \ '#808080', '#ff4040',
1473 \ '#40ff40', '#ffff40',
1474 \ '#4040ff', '#ff40ff',
1475 \ '#40ffff', '#ffffff',
1476 \]
1477
1478 let buf = Run_shell_in_terminal({})
1479 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001480 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001481 call term_wait(buf)
1482
1483 exe buf . 'bwipe'
1484endfunc
1485
1486let s:test_colors = [
1487 \ '#616e64', '#0d0a79',
1488 \ '#6d610d', '#0a7373',
1489 \ '#690d0a', '#6d696e',
1490 \ '#0d0a6f', '#616e0d',
1491 \ '#0a6479', '#6d0d0a',
1492 \ '#617373', '#0d0a69',
1493 \ '#6d690d', '#0a6e6f',
1494 \ '#610d0a', '#6e6479',
1495 \]
1496
1497func Test_terminal_ansicolors_global()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001498 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001499 if !exists('*term_getansicolors')
1500 throw 'Skipped: term_getansicolors() not supported'
1501 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001502 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1503 let buf = Run_shell_in_terminal({})
1504 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001505 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001506 call term_wait(buf)
1507
1508 exe buf . 'bwipe'
1509 unlet g:terminal_ansi_colors
1510endfunc
1511
1512func Test_terminal_ansicolors_func()
Bram Moolenaar1e82a782019-09-21 22:57:06 +02001513 CheckFeature termguicolors
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001514 if !exists('*term_getansicolors')
1515 throw 'Skipped: term_getansicolors() not supported'
1516 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001517 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1518 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1519 call assert_equal(s:test_colors, term_getansicolors(buf))
1520
1521 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001522 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001523
1524 let colors = [
1525 \ 'ivory', 'AliceBlue',
1526 \ 'grey67', 'dark goldenrod',
1527 \ 'SteelBlue3', 'PaleVioletRed4',
1528 \ 'MediumPurple2', 'yellow2',
1529 \ 'RosyBrown3', 'OrangeRed2',
1530 \ 'white smoke', 'navy blue',
1531 \ 'grey47', 'gray97',
1532 \ 'MistyRose2', 'DodgerBlue4',
1533 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001534 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001535
1536 let colors[4] = 'Invalid'
1537 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1538
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001539 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001540 call term_wait(buf)
1541 exe buf . 'bwipe'
1542endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001543
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001544func Test_terminal_all_ansi_colors()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001545 CheckRunVimInTerminal
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001546
1547 " Use all the ANSI colors.
1548 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001549 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001550 \ 'hi Tblack ctermfg=0 ctermbg=8',
1551 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1552 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1553 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1554 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1555 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1556 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1557 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1558 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1559 \ 'hi Tred ctermfg=9 ctermbg=1',
1560 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1561 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1562 \ 'hi Tblue ctermfg=12 ctermbg=4',
1563 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1564 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1565 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001566 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1567 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1568 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001569 \ '',
1570 \ 'call matchadd("Tblack", "A")',
1571 \ 'call matchadd("Tdarkred", "B")',
1572 \ 'call matchadd("Tdarkgreen", "C")',
1573 \ 'call matchadd("Tbrown", "D")',
1574 \ 'call matchadd("Tdarkblue", "E")',
1575 \ 'call matchadd("Tdarkmagenta", "F")',
1576 \ 'call matchadd("Tdarkcyan", "G")',
1577 \ 'call matchadd("Tlightgrey", "H")',
1578 \ 'call matchadd("Tdarkgrey", "I")',
1579 \ 'call matchadd("Tred", "J")',
1580 \ 'call matchadd("Tgreen", "K")',
1581 \ 'call matchadd("Tyellow", "L")',
1582 \ 'call matchadd("Tblue", "M")',
1583 \ 'call matchadd("Tmagenta", "N")',
1584 \ 'call matchadd("Tcyan", "O")',
1585 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001586 \ 'call matchadd("TdarkredBold", "X")',
1587 \ 'call matchadd("TgreenBold", "Y")',
1588 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001589 \ 'redraw',
1590 \ ], 'Xcolorscript')
1591 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1592 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1593
1594 call term_sendkeys(buf, ":q\<CR>")
1595 call StopVimInTerminal(buf)
1596 call delete('Xcolorscript')
1597endfunc
1598
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001599func Test_terminal_termwinsize_option_fixed()
Bram Moolenaarc2585492019-09-22 21:29:53 +02001600 CheckRunVimInTerminal
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001601 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001602 let text = []
1603 for n in range(10)
1604 call add(text, repeat(n, 50))
1605 endfor
1606 call writefile(text, 'Xwinsize')
1607 let buf = RunVimInTerminal('Xwinsize', {})
1608 let win = bufwinid(buf)
1609 call assert_equal([6, 40], term_getsize(buf))
1610 call assert_equal(6, winheight(win))
1611 call assert_equal(40, winwidth(win))
1612
1613 " resizing the window doesn't resize the terminal.
1614 resize 10
1615 vertical resize 60
1616 call assert_equal([6, 40], term_getsize(buf))
1617 call assert_equal(10, winheight(win))
1618 call assert_equal(60, winwidth(win))
1619
1620 call StopVimInTerminal(buf)
1621 call delete('Xwinsize')
1622
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001623 call assert_fails('set termwinsize=40', 'E474')
1624 call assert_fails('set termwinsize=10+40', 'E474')
1625 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001626
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001627 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001628endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001629
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001630func Test_terminal_termwinsize_option_zero()
1631 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001632 let buf = Run_shell_in_terminal({})
1633 let win = bufwinid(buf)
1634 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001635 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001636 call term_wait(buf)
1637 exe buf . 'bwipe'
1638
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001639 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001640 let buf = Run_shell_in_terminal({})
1641 let win = bufwinid(buf)
1642 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001643 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001644 call term_wait(buf)
1645 exe buf . 'bwipe'
1646
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001647 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001648 let buf = Run_shell_in_terminal({})
1649 let win = bufwinid(buf)
1650 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001651 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001652 call term_wait(buf)
1653 exe buf . 'bwipe'
1654
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001655 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001656endfunc
1657
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001658func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001659 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001660 vsplit
1661 let buf = Run_shell_in_terminal({})
1662 let win = bufwinid(buf)
1663 call assert_inrange(10, 1000, winheight(win))
1664 call assert_inrange(50, 1000, winwidth(win))
1665 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1666
1667 resize 15
1668 vertical resize 60
1669 redraw
1670 call assert_equal([15, 60], term_getsize(buf))
1671 call assert_equal(15, winheight(win))
1672 call assert_equal(60, winwidth(win))
1673
1674 resize 7
1675 vertical resize 30
1676 redraw
1677 call assert_equal([10, 50], term_getsize(buf))
1678 call assert_equal(7, winheight(win))
1679 call assert_equal(30, winwidth(win))
1680
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001681 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001682 call term_wait(buf)
1683 exe buf . 'bwipe'
1684
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001685 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001686 let buf = Run_shell_in_terminal({})
1687 let win = bufwinid(buf)
1688 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001689 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001690 call term_wait(buf)
1691 exe buf . 'bwipe'
1692
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001693 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001694endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001695
1696func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001697 " make three tabpages, terminal in the middle
1698 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001699 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001700 tabnew
1701 tabprev
1702 call assert_equal(1, winnr('$'))
1703 call assert_equal(2, tabpagenr())
1704 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001705
1706 let buf = Run_shell_in_terminal({})
1707 let termwin = bufwinid(buf)
1708 set termwinkey=<C-L>
1709 call feedkeys("\<C-L>w", 'tx')
1710 call assert_equal(thiswin, win_getid())
1711 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001712 call assert_equal(termwin, win_getid())
1713
Bram Moolenaar997d4242019-09-14 21:23:40 +02001714 if has('langmap')
1715 set langmap=xjyk
1716 call feedkeys("\<C-L>x", 'tx')
1717 call assert_equal(thiswin, win_getid())
1718 call feedkeys("\<C-W>y", 'tx')
1719 call assert_equal(termwin, win_getid())
1720 set langmap=
1721 endif
Bram Moolenaara4b26992019-08-15 20:58:54 +02001722
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001723 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001724 call assert_equal(3, tabpagenr())
1725 tabprev
1726 call assert_equal(2, tabpagenr())
1727 call assert_equal(termwin, win_getid())
1728
1729 call feedkeys("\<C-L>gT", "xt")
1730 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001731 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001732 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001733 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001734
1735 let job = term_getjob(buf)
1736 call feedkeys("\<C-L>\<C-C>", 'tx')
1737 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001738
1739 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001740 tabnext
1741 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001742 tabprev
1743 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001744endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001745
1746func Test_terminal_out_err()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001747 CheckUnix
1748
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001749 call writefile([
1750 \ '#!/bin/sh',
1751 \ 'echo "this is standard error" >&2',
1752 \ 'echo "this is standard out" >&1',
1753 \ ], 'Xechoerrout.sh')
1754 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1755
1756 let outfile = 'Xtermstdout'
1757 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001758
1759 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1760 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001761 call assert_equal('this is standard error', term_getline(buf, 1))
1762
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001763 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001764 exe buf . 'bwipe'
1765 call delete('Xechoerrout.sh')
1766 call delete(outfile)
1767endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001768
1769func Test_terminwinscroll()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001770 CheckUnix
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001771
1772 " Let the terminal output more than 'termwinscroll' lines, some at the start
1773 " will be dropped.
1774 exe 'set termwinscroll=' . &lines
1775 let buf = term_start('/bin/sh')
1776 for i in range(1, &lines)
1777 call feedkeys("echo " . i . "\<CR>", 'xt')
1778 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1779 endfor
1780 " Go to Terminal-Normal mode to update the buffer.
1781 call feedkeys("\<C-W>N", 'xt')
1782 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1783
1784 " Every "echo nr" must only appear once
1785 let lines = getline(1, line('$'))
1786 for i in range(&lines - len(lines) / 2 + 2, &lines)
1787 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1788 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1789 endfor
1790
1791 exe buf . 'bwipe!'
1792endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001793
Bram Moolenaar875cf872018-07-08 20:49:07 +02001794" Resizing the terminal window caused an ml_get error.
1795" TODO: This does not reproduce the original problem.
1796func Test_terminal_resize()
1797 set statusline=x
1798 terminal
1799 call assert_equal(2, winnr('$'))
1800
1801 " Fill the terminal with text.
1802 if has('win32')
1803 call feedkeys("dir\<CR>", 'xt')
1804 else
1805 call feedkeys("ls\<CR>", 'xt')
1806 endif
1807 " Go to Terminal-Normal mode for a moment.
1808 call feedkeys("\<C-W>N", 'xt')
1809 " Open a new window
1810 call feedkeys("i\<C-W>n", 'xt')
1811 call assert_equal(3, winnr('$'))
1812 redraw
1813
1814 close
1815 call assert_equal(2, winnr('$'))
1816 call feedkeys("exit\<CR>", 'xt')
1817 set statusline&
1818endfunc
1819
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001820" must be nearly the last, we can't go back from GUI to terminal
1821func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001822 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001823
1824 " Ignore the "failed to create input context" error.
1825 call test_ignore_error('E285:')
1826
1827 gui -f
1828
1829 call assert_equal(1, winnr('$'))
1830 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001831 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001832 call term_wait(buf)
1833
1834 " closing window wipes out the terminal buffer a with finished job
1835 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1836 call assert_equal("", bufname(buf))
1837
1838 unlet g:job
1839endfunc
1840
1841func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001842 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001843 set guioptions+=!
1844
1845 let filename = 'Xtestscript'
1846 if has('win32')
1847 let filename .= '.bat'
1848 let prefix = ''
1849 let contents = ['@echo off', 'exit %1']
1850 else
1851 let filename .= '.sh'
1852 let prefix = './'
1853 let contents = ['#!/bin/sh', 'exit $1']
1854 endif
1855 call writefile(contents, filename)
1856 call setfperm(filename, 'rwxrwx---')
1857
1858 " Check if v:shell_error is equal to the exit status.
1859 let exitval = 0
1860 execute printf(':!%s%s %d', prefix, filename, exitval)
1861 call assert_equal(exitval, v:shell_error)
1862
1863 let exitval = 9
1864 execute printf(':!%s%s %d', prefix, filename, exitval)
1865 call assert_equal(exitval, v:shell_error)
1866
1867 set guioptions&
1868 call delete(filename)
1869endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001870
1871func Test_terminal_hidden()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001872 CheckUnix
1873
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001874 term ++hidden cat
1875 let bnr = bufnr('$')
1876 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1877 exe 'sbuf ' . bnr
1878 call assert_equal('terminal', &buftype)
1879 call term_sendkeys(bnr, "asdf\<CR>")
1880 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1881 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001882 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001883 bwipe!
1884endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001885
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001886func Test_terminal_switch_mode()
1887 term
1888 let bnr = bufnr('$')
1889 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1890 call feedkeys("\<C-W>N", 'xt')
1891 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1892 call feedkeys("A", 'xt')
1893 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1894 call feedkeys("\<C-W>N", 'xt')
1895 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1896 call feedkeys("I", 'xt')
1897 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1898 call feedkeys("\<C-W>Nv", 'xt')
1899 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1900 call feedkeys("I", 'xt')
1901 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1902 call feedkeys("\<C-W>Nv", 'xt')
1903 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1904 call feedkeys("A", 'xt')
1905 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1906 bwipe!
1907endfunc
1908
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001909func Test_terminal_hidden_and_close()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001910 CheckUnix
1911
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001912 call assert_equal(1, winnr('$'))
1913 term ++hidden ++close ls
1914 let bnr = bufnr('$')
1915 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1916 call WaitForAssert({-> assert_false(bufexists(bnr))})
1917 call assert_equal(1, winnr('$'))
1918endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001919
1920func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001921 " This test does not pass through ConPTY.
1922 if has('conpty')
1923 return
1924 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001925 let contents = [
1926 \ [ 'One', '', 'X' ],
1927 \ [ 'Two', '', '' ],
1928 \ [ 'Three' ] + repeat([''], 30)
1929 \ ]
1930
1931 for c in contents
1932 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001933 if has('win32')
1934 term cmd /c type Xfile
1935 else
1936 term cat Xfile
1937 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001938 let bnr = bufnr('$')
1939 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1940 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001941 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001942 call assert_equal(c, getline(1, line('$')))
1943 quit
1944 endfor
1945
1946 call delete('Xfile')
1947endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001948
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001949func Test_terminal_no_job()
1950 let term = term_start('false', {'term_finish': 'close'})
1951 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1952endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001953
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001954func Test_term_getcursor()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02001955 CheckUnix
1956
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001957 let buf = Run_shell_in_terminal({})
1958
1959 " Wait for the shell to display a prompt.
1960 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
1961
1962 " Hide the cursor.
1963 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
1964 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
1965
1966 " Show the cursor.
1967 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001968 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001969
1970 " Change color of cursor.
1971 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
1972 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
1973 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
1974 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
1975 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
1976
1977 " Make cursor a blinking block.
1978 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
1979 call WaitForAssert({-> assert_equal([1, 1],
1980 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1981
1982 " Make cursor a steady block.
1983 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
1984 call WaitForAssert({-> assert_equal([0, 1],
1985 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1986
1987 " Make cursor a blinking underline.
1988 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
1989 call WaitForAssert({-> assert_equal([1, 2],
1990 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1991
1992 " Make cursor a steady underline.
1993 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
1994 call WaitForAssert({-> assert_equal([0, 2],
1995 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1996
1997 " Make cursor a blinking vertical bar.
1998 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
1999 call WaitForAssert({-> assert_equal([1, 3],
2000 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2001
2002 " Make cursor a steady vertical bar.
2003 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
2004 call WaitForAssert({-> assert_equal([0, 3],
2005 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
2006
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02002007 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02002008endfunc
2009
Bram Moolenaar1e115362019-01-09 23:01:02 +01002010func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01002011 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002012 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002013 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01002014 call assert_equal('', term_gettitle(bufnr('$') + 1))
2015
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02002016 if !has('title') || &title == 0 || empty(&t_ts)
2017 throw "Skipped: can't get/set title"
2018 endif
2019
Bram Moolenaar1e115362019-01-09 23:01:02 +01002020 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002021 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002022 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2023 call term_sendkeys(term, ":e Xfoo\r")
2024 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002025 else
2026 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002027 call term_sendkeys(term, ":e Xfoo\r")
2028 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002029 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002030
Bram Moolenaar1e115362019-01-09 23:01:02 +01002031 call term_sendkeys(term, ":set titlestring=foo\r")
2032 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2033
2034 exe term . 'bwipe!'
2035endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002036
2037" When drawing the statusline the cursor position may not have been updated
2038" yet.
2039" 1. create a terminal, make it show 2 lines
2040" 2. 0.5 sec later: leave terminal window, execute "i"
2041" 3. 0.5 sec later: clear terminal window, now it's 1 line
2042" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2043" 4. 0.5 sec later: should be done, clean up
2044func Test_terminal_statusline()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002045 CheckUnix
2046
Bram Moolenaar10772302019-01-20 18:25:54 +01002047 set statusline=x
2048 terminal
2049 let tbuf = bufnr('')
2050 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2051 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2052 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2053 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2054
2055 sleep 2
2056 exe tbuf . 'bwipe!'
2057 au! BufLeave
2058 set statusline=
2059endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002060
2061func Test_terminal_getwinpos()
Bram Moolenaarc2585492019-09-22 21:29:53 +02002062 CheckRunVimInTerminal
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002063
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002064 " split, go to the bottom-right window
2065 split
2066 wincmd j
2067 set splitright
2068
2069 call writefile([
2070 \ 'echo getwinpos()',
2071 \ ], 'XTest_getwinpos')
2072 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2073 call term_wait(buf)
2074
2075 " Find the output of getwinpos() in the bottom line.
2076 let rows = term_getsize(buf)[0]
2077 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2078 let line = term_getline(buf, rows)
2079 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2080 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2081
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002082 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002083 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002084 " In the GUI it can be more, let's assume a 20 x 14 cell.
2085 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002086 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002087 let winpos = 50->getwinpos()
2088 call assert_equal(xroot, winpos[0])
2089 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002090 let [winrow, wincol] = win_screenpos('.')
2091 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2092 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2093 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2094 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002095
2096 call term_wait(buf)
2097 call term_sendkeys(buf, ":q\<CR>")
2098 call StopVimInTerminal(buf)
2099 call delete('XTest_getwinpos')
2100 exe buf . 'bwipe!'
2101 set splitright&
2102 only!
2103endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002104
2105func Test_terminal_altscreen()
Bram Moolenaaradbde3f2019-09-08 22:57:14 +02002106 " somehow doesn't work on MS-Windows
2107 CheckUnix
2108 let cmd = "cat Xtext\<CR>"
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002109
2110 let buf = term_start(&shell, {})
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002111 call writefile(["\<Esc>[?1047h"], 'Xtext')
2112 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002113 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2114
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002115 call writefile(["\<Esc>[?1047l"], 'Xtext')
2116 call term_sendkeys(buf, cmd)
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002117 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002118
2119 call term_sendkeys(buf, "exit\r")
2120 exe buf . "bwipe!"
Bram Moolenaarbf9a3b02019-09-08 22:35:48 +02002121 call delete('Xtext')
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002122endfunc
Bram Moolenaard2842ea2019-09-26 23:08:54 +02002123
2124func Test_terminal_setapi_and_call()
2125 if !CanRunVimInTerminal()
2126 return
2127 endif
2128
2129 call WriteApiCall('Tapi_TryThis')
2130 call ch_logfile('Xlog', 'w')
2131
2132 unlet! g:called_bufnum
2133 unlet! g:called_arg
2134
2135 let buf = RunVimInTerminal('-S Xscript', {'term_api': 0})
2136 call WaitForAssert({-> assert_match('Unpermitted function: Tapi_TryThis', string(readfile('Xlog')))})
2137 call assert_false(exists('g:called_bufnum'))
2138 call assert_false(exists('g:called_arg'))
2139
2140 call term_setapi(buf, 'Tapi_TryThis')
2141 call term_sendkeys(buf, ":set notitle\<CR>")
2142 call term_sendkeys(buf, ":source Xscript\<CR>")
2143 call WaitFor({-> exists('g:called_bufnum')})
2144 call assert_equal(buf, g:called_bufnum)
2145 call assert_equal(['hello', 123], g:called_arg)
2146 call StopVimInTerminal(buf)
2147
2148 call delete('Xscript')
2149 call ch_logfile('')
2150 call delete('Xlog')
2151 unlet! g:called_bufnum
2152 unlet! g:called_arg
2153endfunc