blob: 07386a318abfdd1f8a09f59f8fb4f96506857dc3 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
Bram Moolenaarea5d6fa2017-08-18 21:07:11 +02003if !has('terminal')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02004 finish
5endif
6
7source shared.vim
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01008source screendump.vim
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02009
Bram Moolenaarb81bc772017-08-11 22:45:01 +020010let s:python = PythonProg()
11
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 Moolenaar35422f42017-08-05 16:33:56 +020028 let string = string({'job': term_getjob(buf)})
29 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 Moolenaarb00fdf62017-09-21 22:16:21 +020035 au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif
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 Moolenaar2dc9d262017-09-08 14:39:30 +020042 call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out)
43 call assert_match('^\\\\.\\pipe\\', term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 endif
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020045 call assert_equal('t', mode())
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020046 call assert_equal('yes', b:done)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020047 call assert_match('%aR[^\n]*running]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020048 call assert_match('%aR[^\n]*running]', execute('ls R'))
49 call assert_notmatch('%[^\n]*running]', execute('ls F'))
50 call assert_notmatch('%[^\n]*running]', execute('ls ?'))
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020051
Bram Moolenaar94053a52017-08-01 21:44:33 +020052 call Stop_shell_in_terminal(buf)
53 call term_wait(buf)
Bram Moolenaar2bb7b6b2017-08-13 20:58:33 +020054 call assert_equal('n', mode())
55 call assert_match('%aF[^\n]*finished]', execute('ls'))
Bram Moolenaar0751f512018-03-29 16:37:16 +020056 call assert_match('%aF[^\n]*finished]', execute('ls F'))
57 call assert_notmatch('%[^\n]*finished]', execute('ls R'))
58 call assert_notmatch('%[^\n]*finished]', execute('ls ?'))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020059
Bram Moolenaar94053a52017-08-01 21:44:33 +020060 " closing window wipes out the terminal buffer a with finished job
61 close
62 call assert_equal("", bufname(buf))
63
Bram Moolenaarb00fdf62017-09-21 22:16:21 +020064 au! BufWinEnter
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020065 unlet g:job
66endfunc
67
68func Test_terminal_make_change()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020069 let buf = Run_shell_in_terminal({})
Bram Moolenaar94053a52017-08-01 21:44:33 +020070 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020071 call term_wait(buf)
72
73 setlocal modifiable
74 exe "normal Axxx\<Esc>"
75 call assert_fails(buf . 'bwipe', 'E517')
76 undo
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078 exe buf . 'bwipe'
79 unlet g:job
80endfunc
81
Bram Moolenaar94053a52017-08-01 21:44:33 +020082func Test_terminal_wipe_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +020083 let buf = Run_shell_in_terminal({})
Bram Moolenaareb44a682017-08-03 22:44:55 +020084 call assert_fails(buf . 'bwipe', 'E517')
85 exe buf . 'bwipe!'
Bram Moolenaar50182fa2018-04-28 21:34:40 +020086 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar94053a52017-08-01 21:44:33 +020087 call assert_equal("", bufname(buf))
88
89 unlet g:job
90endfunc
91
Bram Moolenaar8adb0d02017-09-17 19:08:02 +020092func Test_terminal_split_quit()
93 let buf = Run_shell_in_terminal({})
94 call term_wait(buf)
95 split
96 quit!
97 call term_wait(buf)
98 sleep 50m
99 call assert_equal('run', job_status(g:job))
100
101 quit!
Bram Moolenaar50182fa2018-04-28 21:34:40 +0200102 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200103 call assert_equal('dead', job_status(g:job))
104
105 exe buf . 'bwipe'
106 unlet g:job
107endfunc
108
Bram Moolenaar94053a52017-08-01 21:44:33 +0200109func Test_terminal_hide_buffer()
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200110 let buf = Run_shell_in_terminal({})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200111 setlocal bufhidden=hide
Bram Moolenaar94053a52017-08-01 21:44:33 +0200112 quit
113 for nr in range(1, winnr('$'))
114 call assert_notequal(winbufnr(nr), buf)
115 endfor
116 call assert_true(bufloaded(buf))
117 call assert_true(buflisted(buf))
118
119 exe 'split ' . buf . 'buf'
120 call Stop_shell_in_terminal(buf)
121 exe buf . 'bwipe'
122
123 unlet g:job
124endfunc
125
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200126func! s:Nasty_exit_cb(job, st)
127 exe g:buf . 'bwipe!'
128 let g:buf = 0
129endfunc
130
Bram Moolenaar9d189612017-09-09 18:11:00 +0200131func Get_cat_123_cmd()
132 if has('win32')
133 return 'cmd /c "cls && color 2 && echo 123"'
134 else
135 call writefile(["\<Esc>[32m123"], 'Xtext')
136 return "cat Xtext"
137 endif
138endfunc
139
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200140func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200141 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200142 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
143 let g:job = term_getjob(g:buf)
144
145 call WaitFor('job_status(g:job) == "dead"')
146 call WaitFor('g:buf == 0')
147 unlet g:buf
148 unlet g:job
149 call delete('Xtext')
150endfunc
151
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200152func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200153 let l = term_scrape(a:buf, 0)
154 call assert_true(len(l) == 0)
155 let l = term_scrape(a:buf, 999)
156 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200157 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200158 call assert_true(len(l) > 0)
159 call assert_equal('1', l[0].chars)
160 call assert_equal('2', l[1].chars)
161 call assert_equal('3', l[2].chars)
162 call assert_equal('#00e000', l[0].fg)
163 if &background == 'light'
164 call assert_equal('#ffffff', l[0].bg)
165 else
166 call assert_equal('#000000', l[0].bg)
167 endif
168
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200169 let l = term_getline(a:buf, -1)
170 call assert_equal('', l)
171 let l = term_getline(a:buf, 0)
172 call assert_equal('', l)
173 let l = term_getline(a:buf, 999)
174 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200175 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200176 call assert_equal('123', l)
177endfunc
178
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200179func Test_terminal_scrape_123()
180 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200181 let buf = term_start(cmd)
182
183 let termlist = term_list()
184 call assert_equal(1, len(termlist))
185 call assert_equal(buf, termlist[0])
186
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200187 " Nothing happens with invalid buffer number
188 call term_wait(1234)
189
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200190 call term_wait(buf)
Bram Moolenaar17833372017-09-04 22:23:19 +0200191 " On MS-Windows we first get a startup message of two lines, wait for the
Bram Moolenaar1bfdc072017-09-05 20:19:42 +0200192 " "cls" to happen, after that we have one line with three characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100193 call WaitFor({-> len(term_scrape(buf, 1)) == 3})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200194 call Check_123(buf)
195
196 " Must still work after the job ended.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100197 let job = term_getjob(buf)
198 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200199 call term_wait(buf)
200 call Check_123(buf)
201
202 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200203 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200204endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200205
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200206func Test_terminal_scrape_multibyte()
207 if !has('multi_byte')
208 return
209 endif
210 call writefile(["léttまrs"], 'Xtext')
211 if has('win32')
Bram Moolenaar36783932017-08-14 23:07:30 +0200212 " Run cmd with UTF-8 codepage to make the type command print the expected
213 " multibyte characters.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100214 let buf = term_start("cmd /K chcp 65001")
215 call term_sendkeys(buf, "type Xtext\<CR>")
216 call term_sendkeys(buf, "exit\<CR>")
217 let line = 4
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200218 else
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100219 let buf = term_start("cat Xtext")
220 let line = 1
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200221 endif
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200222
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100223 call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"})
224 let l = term_scrape(buf, line)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200225 call assert_true(len(l) >= 7)
226 call assert_equal('l', l[0].chars)
227 call assert_equal('é', l[1].chars)
228 call assert_equal(1, l[1].width)
229 call assert_equal('t', l[2].chars)
230 call assert_equal('t', l[3].chars)
231 call assert_equal('ま', l[4].chars)
232 call assert_equal(2, l[4].width)
233 call assert_equal('r', l[5].chars)
234 call assert_equal('s', l[6].chars)
235
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100236 let job = term_getjob(buf)
237 call WaitFor({-> job_status(job) == "dead"})
238 call term_wait(buf)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200239
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100240 exe buf . 'bwipe'
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200241 call delete('Xtext')
242endfunc
243
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200244func Test_terminal_scroll()
245 call writefile(range(1, 200), 'Xtext')
246 if has('win32')
247 let cmd = 'cmd /c "type Xtext"'
248 else
249 let cmd = "cat Xtext"
250 endif
251 let buf = term_start(cmd)
252
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100253 let job = term_getjob(buf)
254 call WaitFor({-> job_status(job) == "dead"})
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200255 call term_wait(buf)
256 if has('win32')
257 " TODO: this should not be needed
258 sleep 100m
259 endif
260
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200261 let scrolled = term_getscrolled(buf)
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200262 call assert_equal('1', getline(1))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200263 call assert_equal('1', term_getline(buf, 1 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200264 call assert_equal('49', getline(49))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200265 call assert_equal('49', term_getline(buf, 49 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200266 call assert_equal('200', getline(200))
Bram Moolenaar82b9ca02017-08-08 23:06:46 +0200267 call assert_equal('200', term_getline(buf, 200 - scrolled))
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200268
269 exe buf . 'bwipe'
270 call delete('Xtext')
271endfunc
272
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200273func Test_terminal_scrollback()
274 let buf = Run_shell_in_terminal({})
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200275 set termwinscroll=100
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200276 call writefile(range(150), 'Xtext')
277 if has('win32')
278 call term_sendkeys(buf, "type Xtext\<CR>")
279 else
280 call term_sendkeys(buf, "cat Xtext\<CR>")
281 endif
282 let rows = term_getsize(buf)[0]
Bram Moolenaar6c672192018-04-15 13:28:42 +0200283 " On MS-Windows there is an empty line, check both last line and above it.
284 call WaitFor({-> term_getline(buf, rows - 1) . term_getline(buf, rows - 2) =~ '149'})
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200285 let lines = line('$')
Bram Moolenaarac3e8302018-04-15 13:10:44 +0200286 call assert_inrange(91, 100, lines)
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200287
288 call Stop_shell_in_terminal(buf)
289 call term_wait(buf)
290 exe buf . 'bwipe'
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200291 set termwinscroll&
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200292endfunc
293
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200294func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200295 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200296
Bram Moolenaarb2412082017-08-20 18:09:14 +0200297 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200298 let size = term_getsize('')
299 bwipe!
300 call assert_equal(5, size[0])
301
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200302 call term_start(cmd, {'term_rows': 6})
303 let size = term_getsize('')
304 bwipe!
305 call assert_equal(6, size[0])
306
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200307 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200308 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaara42d3632018-04-14 17:05:38 +0200309 call assert_equal([5, 33], term_getsize(''))
310
311 call term_setsize('', 6, 0)
312 call assert_equal([6, 33], term_getsize(''))
313
314 call term_setsize('', 0, 35)
315 call assert_equal([6, 35], term_getsize(''))
316
317 call term_setsize('', 7, 30)
318 call assert_equal([7, 30], term_getsize(''))
319
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200320 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200321 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200322
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200323 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
324 let size = term_getsize('')
325 bwipe!
326 call assert_equal([6, 36], size)
327
Bram Moolenaarb2412082017-08-20 18:09:14 +0200328 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200329 let size = term_getsize('')
330 bwipe!
331 call assert_equal(20, size[1])
332
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200333 call term_start(cmd, {'vertical': 1, 'term_cols': 26})
334 let size = term_getsize('')
335 bwipe!
336 call assert_equal(26, size[1])
337
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200338 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200339 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200340 let size = term_getsize('')
341 bwipe!
342 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200343
344 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
345 let size = term_getsize('')
346 bwipe!
347 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200348
349 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200350endfunc
351
352func Test_terminal_curwin()
353 let cmd = Get_cat_123_cmd()
354 call assert_equal(1, winnr('$'))
355
356 split dummy
357 exe 'terminal ++curwin ' . cmd
358 call assert_equal(2, winnr('$'))
359 bwipe!
360
361 split dummy
362 call term_start(cmd, {'curwin': 1})
363 call assert_equal(2, winnr('$'))
364 bwipe!
365
366 split dummy
367 call setline(1, 'change')
368 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
369 call assert_equal(2, winnr('$'))
370 exe 'terminal! ++curwin ' . cmd
371 call assert_equal(2, winnr('$'))
372 bwipe!
373
374 split dummy
375 call setline(1, 'change')
376 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
377 call assert_equal(2, winnr('$'))
378 bwipe!
379
380 split dummy
381 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200382 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200383endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200384
Bram Moolenaarff546792017-11-21 14:47:57 +0100385func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200386 if s:python != ''
387 let cmd = s:python . " test_short_sleep.py"
388 let waittime = 500
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200389 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200390 echo 'This will take five seconds...'
391 let waittime = 2000
392 if has('win32')
393 let cmd = $windir . '\system32\timeout.exe 1'
394 else
395 let cmd = 'sleep 1'
396 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200397 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100398 return [cmd, waittime]
399endfunc
400
401func Test_terminal_finish_open_close()
402 call assert_equal(1, winnr('$'))
403
404 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200405
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100406 " shell terminal closes automatically
407 terminal
408 let buf = bufnr('%')
409 call assert_equal(2, winnr('$'))
410 " Wait for the shell to display a prompt
411 call WaitFor({-> term_getline(buf, 1) != ""})
412 call Stop_shell_in_terminal(buf)
413 call WaitFor("winnr('$') == 1", waittime)
414
415 " shell terminal that does not close automatically
416 terminal ++noclose
417 let buf = bufnr('%')
418 call assert_equal(2, winnr('$'))
419 " Wait for the shell to display a prompt
420 call WaitFor({-> term_getline(buf, 1) != ""})
421 call Stop_shell_in_terminal(buf)
422 call assert_equal(2, winnr('$'))
423 quit
424 call assert_equal(1, winnr('$'))
425
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200426 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200427 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200428 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200429 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200430
431 call term_start(cmd, {'term_finish': 'close'})
432 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200433 wincmd p
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200434 call WaitFor("winnr('$') == 1", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200435 call assert_equal(1, winnr('$'))
436
437 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200438 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200439 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200440 call assert_equal(2, winnr('$'))
441 bwipe
442
443 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200444 close!
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200445 call WaitFor("winnr('$') == 2", waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200446 call assert_equal(2, winnr('$'))
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200447 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200448
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200449 exe 'terminal ++hidden ++open ' . cmd
450 call assert_equal(1, winnr('$'))
451 call WaitFor("winnr('$') == 2", waittime)
452 call assert_equal(2, winnr('$'))
453 bwipe
454
455 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
456 call assert_equal(1, winnr('$'))
457 call WaitFor("winnr('$') == 2", waittime)
458 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200459 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200460
461 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
462 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
463 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
464 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
465
466 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200467 close!
Bram Moolenaar37c45832017-08-12 16:01:04 +0200468 call WaitFor("winnr('$') == 2", waittime)
469 call assert_equal(2, winnr('$'))
470 call assert_equal(4, winheight(0))
471 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200472endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200473
474func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200475 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200476 return
477 endif
478 call mkdir('Xdir')
479 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200480 call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")')
481 call assert_equal('Xdir', fnamemodify(getline(1), ":t"))
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200482
483 exe buf . 'bwipe'
484 call delete('Xdir', 'rf')
485endfunc
486
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100487func Test_terminal_servername()
488 if !has('clientserver')
489 return
490 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100491 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100492 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100493 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100494 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100495 call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100496 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100497 call term_sendkeys(buf, "echo $VIM_SERVERNAME\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100498 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100499 call term_wait(buf)
500 call Stop_shell_in_terminal(buf)
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100501 call WaitFor('getline(2) == v:servername')
502 call assert_equal(v:servername, getline(2))
503
Bram Moolenaar012eb662018-03-13 17:55:27 +0100504 exe buf . 'bwipe'
505 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100506endfunc
507
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200508func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100509 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200510 " Wait for the shell to display a prompt
Bram Moolenaar012eb662018-03-13 17:55:27 +0100511 call WaitFor({-> term_getline(buf, 1) != ""})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100512 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100513 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100514 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100515 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100516 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100517 call term_wait(buf)
518 call Stop_shell_in_terminal(buf)
Bram Moolenaar51c23682017-08-14 21:45:00 +0200519 call WaitFor('getline(2) == "correct"')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200520 call assert_equal('correct', getline(2))
521
Bram Moolenaar012eb662018-03-13 17:55:27 +0100522 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200523endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200524
525" must be last, we can't go back from GUI to terminal
526func Test_zz_terminal_in_gui()
Bram Moolenaar9f0139a2017-08-13 20:26:20 +0200527 if !CanRunGui()
Bram Moolenaar679653e2017-08-13 14:13:19 +0200528 return
529 endif
Bram Moolenaar97f65fa2017-08-29 20:42:07 +0200530
531 " Ignore the "failed to create input context" error.
532 call test_ignore_error('E285:')
533
Bram Moolenaar679653e2017-08-13 14:13:19 +0200534 gui -f
535
536 call assert_equal(1, winnr('$'))
537 let buf = Run_shell_in_terminal({'term_finish': 'close'})
538 call Stop_shell_in_terminal(buf)
539 call term_wait(buf)
540
541 " closing window wipes out the terminal buffer a with finished job
542 call WaitFor("winnr('$') == 1")
543 call assert_equal(1, winnr('$'))
544 call assert_equal("", bufname(buf))
545
546 unlet g:job
547endfunc
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200548
549func Test_terminal_list_args()
550 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
551 call assert_fails(buf . 'bwipe', 'E517')
552 exe buf . 'bwipe!'
553 call assert_equal("", bufname(buf))
554endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200555
556func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100557 let buf = term_start(&shell)
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200558 if has('mac')
559 " The shell or something else has a problem dealing with more than 1000
560 " characters at the same time.
561 let len = 1000
562 else
563 let len = 5000
564 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200565
566 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100567 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200568 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100569 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200570
571 " On MS-Windows there is an extra empty line below "done". Find "done" in
572 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100573 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100574 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100575 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200576 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100577 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200578 endif
579 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200580
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100581 let g:job = term_getjob(buf)
582 call Stop_shell_in_terminal(buf)
583 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200584 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200585 bwipe
586endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200587
588func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200589 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200590 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200591 endif
592 new
593 call setline(1, ['one', 'two', 'three'])
594 %term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200595 call WaitFor('getline("$") =~ "3"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200596 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200597 call assert_equal(['3', '3', '14'], nrs)
598 bwipe
599
Bram Moolenaardada6d22017-09-02 17:18:35 +0200600 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200601 call setline(1, ['one', 'two', 'three', 'four'])
602 2,3term wc
Bram Moolenaardada6d22017-09-02 17:18:35 +0200603 call WaitFor('getline("$") =~ "2"')
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200604 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200605 call assert_equal(['2', '2', '10'], nrs)
606 bwipe
607
Bram Moolenaardada6d22017-09-02 17:18:35 +0200608 if executable('python')
609 new
610 call setline(1, ['print("hello")'])
611 1term ++eof=exit() python
612 " MS-Windows echoes the input, Unix doesn't.
613 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
614 if getline(1) =~ 'hello'
615 call assert_equal('hello', getline(1))
616 else
617 call assert_equal('hello', getline(line('$') - 1))
618 endif
619 bwipe
620
621 if has('win32')
622 new
623 call setline(1, ['print("hello")'])
624 1term ++eof=<C-Z> python
625 call WaitFor('getline("$") =~ "Z"')
626 call assert_equal('hello', getline(line('$') - 1))
627 bwipe
628 endif
629 endif
630
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200631 bwipe!
632endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200633
634func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200635 " Todo: make this work in the GUI
636 if !has('gui_running')
637 return
638 endif
639 let buf = term_start('NONE', {})
640 call assert_notequal(0, buf)
641
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200642 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200643 call assert_notequal('', pty)
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200644 if has('win32')
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200645 silent exe '!start cmd /c "echo look here > ' . pty . '"'
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200646 else
647 call system('echo "look here" > ' . pty)
648 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100649 call WaitFor({-> term_getline(buf, 1) =~ "look here"})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200650
Bram Moolenaare738a1a2017-09-16 17:42:41 +0200651 call assert_match('look here', term_getline(buf, 1))
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200652 bwipe!
653endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200654
655func Test_terminal_special_chars()
656 " this file name only works on Unix
657 if !has('unix')
658 return
659 endif
660 call mkdir('Xdir with spaces')
661 call writefile(['x'], 'Xdir with spaces/quoted"file')
662 term ls Xdir\ with\ spaces/quoted\"file
663 call WaitFor('term_getline("", 1) =~ "quoted"')
664 call assert_match('quoted"file', term_getline('', 1))
665 call term_wait('')
666
667 call delete('Xdir with spaces', 'rf')
668 bwipe
669endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200670
671func Test_terminal_wrong_options()
672 call assert_fails('call term_start(&shell, {
673 \ "in_io": "file",
674 \ "in_name": "xxx",
675 \ "out_io": "file",
676 \ "out_name": "xxx",
677 \ "err_io": "file",
678 \ "err_name": "xxx"
679 \ })', 'E474:')
680 call assert_fails('call term_start(&shell, {
681 \ "out_buf": bufnr("%")
682 \ })', 'E474:')
683 call assert_fails('call term_start(&shell, {
684 \ "err_buf": bufnr("%")
685 \ })', 'E474:')
686endfunc
687
688func Test_terminal_redir_file()
Bram Moolenaar17833372017-09-04 22:23:19 +0200689 " TODO: this should work on MS-Window
690 if has('unix')
691 let cmd = Get_cat_123_cmd()
692 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
693 call term_wait(buf)
694 call WaitFor('len(readfile("Xfile")) > 0')
695 call assert_match('123', readfile('Xfile')[0])
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200696 let g:job = term_getjob(buf)
697 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaar17833372017-09-04 22:23:19 +0200698 call delete('Xfile')
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200699 bwipe
Bram Moolenaar17833372017-09-04 22:23:19 +0200700 endif
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200701
702 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200703 call writefile(['one line'], 'Xfile')
704 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
705 call term_wait(buf)
706 call WaitFor('term_getline(' . buf . ', 1) == "one line"')
707 call assert_equal('one line', term_getline(buf, 1))
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200708 let g:job = term_getjob(buf)
709 call WaitFor('job_status(g:job) == "dead"')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200710 bwipe
711 call delete('Xfile')
712 endif
713endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200714
715func TerminalTmap(remap)
716 let buf = Run_shell_in_terminal({})
717 call assert_equal('t', mode())
718
719 if a:remap
720 tmap 123 456
721 else
722 tnoremap 123 456
723 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100724 " don't use abcde, it's an existing command
725 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200726 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100727 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200728 call feedkeys("123", 'tx')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100729 call WaitFor({-> term_getline(buf, term_getcursor(buf)[0]) =~ 'abxde\|456'})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200730 let lnum = term_getcursor(buf)[0]
731 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100732 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200733 else
734 call assert_match('456', term_getline(buf, lnum))
735 endif
736
737 call term_sendkeys(buf, "\r")
738 call Stop_shell_in_terminal(buf)
739 call term_wait(buf)
740
741 tunmap 123
742 tunmap 456
743 call assert_equal('', maparg('123', 't'))
744 close
745 unlet g:job
746endfunc
747
748func Test_terminal_tmap()
749 call TerminalTmap(1)
750 call TerminalTmap(0)
751endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200752
753func Test_terminal_wall()
754 let buf = Run_shell_in_terminal({})
755 wall
756 call Stop_shell_in_terminal(buf)
757 call term_wait(buf)
758 exe buf . 'bwipe'
759 unlet g:job
760endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200761
Bram Moolenaar7a760922018-02-19 23:10:02 +0100762func Test_terminal_wqall()
763 let buf = Run_shell_in_terminal({})
764 call assert_fails('wqall', 'E948')
765 call Stop_shell_in_terminal(buf)
766 call term_wait(buf)
767 exe buf . 'bwipe'
768 unlet g:job
769endfunc
770
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200771func Test_terminal_composing_unicode()
772 let save_enc = &encoding
773 set encoding=utf-8
774
775 if has('win32')
776 let cmd = "cmd /K chcp 65001"
777 let lnum = [3, 6, 9]
778 else
779 let cmd = &shell
780 let lnum = [1, 3, 5]
781 endif
782
783 enew
784 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100785 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200786 call term_wait(buf, 50)
787
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200788 if has('win32')
789 call assert_equal('cmd', job_info(g:job).cmd[0])
790 else
791 call assert_equal(&shell, job_info(g:job).cmd[0])
792 endif
793
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200794 " ascii + composing
795 let txt = "a\u0308bc"
796 call term_sendkeys(buf, "echo " . txt . "\r")
797 call term_wait(buf, 50)
798 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
799 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
800 let l = term_scrape(buf, lnum[0] + 1)
801 call assert_equal("a\u0308", l[0].chars)
802 call assert_equal("b", l[1].chars)
803 call assert_equal("c", l[2].chars)
804
805 " multibyte + composing
806 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
807 call term_sendkeys(buf, "echo " . txt . "\r")
808 call term_wait(buf, 50)
809 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
810 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
811 let l = term_scrape(buf, lnum[1] + 1)
812 call assert_equal("\u304b\u3099", l[0].chars)
813 call assert_equal("\u304e", l[1].chars)
814 call assert_equal("\u304f\u3099", l[2].chars)
815 call assert_equal("\u3052", l[3].chars)
816 call assert_equal("\u3053\u3099", l[4].chars)
817
818 " \u00a0 + composing
819 let txt = "abc\u00a0\u0308"
820 call term_sendkeys(buf, "echo " . txt . "\r")
821 call term_wait(buf, 50)
822 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
823 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
824 let l = term_scrape(buf, lnum[2] + 1)
825 call assert_equal("\u00a0\u0308", l[3].chars)
826
827 call term_sendkeys(buf, "exit\r")
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100828 call WaitFor('job_status(g:job) == "dead"')
829 call assert_equal('dead', job_status(g:job))
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200830 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100831 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200832 let &encoding = save_enc
833endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100834
835func Test_terminal_aucmd_on_close()
836 fun Nop()
837 let s:called = 1
838 endfun
839
840 aug repro
841 au!
842 au BufWinLeave * call Nop()
843 aug END
844
845 let [cmd, waittime] = s:get_sleep_cmd()
846
847 call assert_equal(1, winnr('$'))
848 new
849 call setline(1, ['one', 'two'])
850 exe 'term ++close ' . cmd
851 wincmd p
852 call WaitFor("winnr('$') == 2", waittime)
853 call assert_equal(1, s:called)
854 bwipe!
855
856 unlet s:called
857 au! repro
858 delfunc Nop
859endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100860
861func Test_terminal_term_start_empty_command()
862 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
863 call assert_fails(cmd, 'E474')
864 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
865 call assert_fails(cmd, 'E474')
866 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
867 call assert_fails(cmd, 'E474')
868 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
869 call assert_fails(cmd, 'E474')
870endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100871
872func Test_terminal_response_to_control_sequence()
873 if !has('unix')
874 return
875 endif
876
877 let buf = Run_shell_in_terminal({})
Bram Moolenaar086eb872018-03-25 21:24:12 +0200878 call WaitFor({-> term_getline(buf, 1) != ''})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100879
Bram Moolenaar086eb872018-03-25 21:24:12 +0200880 call term_sendkeys(buf, "cat\<CR>")
881 call WaitFor({-> term_getline(buf, 1) =~ 'cat'})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100882
Bram Moolenaar086eb872018-03-25 21:24:12 +0200883 " Request the cursor position.
884 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100885
886 " Wait for output from tty to display, below an empty line.
Bram Moolenaar086eb872018-03-25 21:24:12 +0200887 call WaitFor({-> term_getline(buf, 4) =~ '3;1R'})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100888
Bram Moolenaar086eb872018-03-25 21:24:12 +0200889 " End "cat" gently.
890 call term_sendkeys(buf, "\<CR>\<C-D>")
891
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100892 call Stop_shell_in_terminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100893 exe buf . 'bwipe'
894 unlet g:job
895endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100896
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100897" Run Vim, start a terminal in that Vim with the kill argument,
898" :qall works.
899func Run_terminal_qall_kill(line1, line2)
900 " 1. Open a terminal window and wait for the prompt to appear
901 " 2. set kill using term_setkill()
902 " 3. make Vim exit, it will kill the shell
903 let after = [
904 \ a:line1,
905 \ 'let buf = bufnr("%")',
906 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
907 \ ' sleep 10m',
908 \ 'endwhile',
909 \ a:line2,
910 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
911 \ 'qall',
912 \ ]
913 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100914 return
915 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100916 call assert_equal("done", readfile("Xdone")[0])
917 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100918endfunc
919
920" Run Vim in a terminal, then start a terminal in that Vim with a kill
921" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100922func Test_terminal_qall_kill_arg()
923 call Run_terminal_qall_kill('term ++kill=kill', '')
924endfunc
925
926" Run Vim, start a terminal in that Vim, set the kill argument with
927" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100928func Test_terminal_qall_kill_func()
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100929 call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")')
930endfunc
931
932" Run Vim, start a terminal in that Vim without the kill argument,
933" check that :qall does not exit, :qall! does.
934func Test_terminal_qall_exit()
935 let after = [
936 \ 'term',
937 \ 'let buf = bufnr("%")',
938 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
939 \ ' sleep 10m',
940 \ 'endwhile',
941 \ 'set nomore',
942 \ 'au VimLeavePre * call writefile(["too early"], "Xdone")',
943 \ 'qall',
944 \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")',
945 \ 'cquit',
946 \ ]
947 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100948 return
949 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100950 call assert_equal("done", readfile("Xdone")[0])
951 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100952endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +0100953
954" Run Vim in a terminal, then start a terminal in that Vim without a kill
955" argument, check that :confirm qall works.
956func Test_terminal_qall_prompt()
957 if !CanRunVimInTerminal()
958 return
959 endif
960 let buf = RunVimInTerminal('', {})
961
962 " Open a terminal window and wait for the prompt to appear
963 call term_sendkeys(buf, ":term\<CR>")
964 call WaitFor({-> term_getline(buf, 10) =~ '\[running]'})
965 call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'})
966
967 " make Vim exit, it will prompt to kill the shell
968 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
969 call WaitFor({-> term_getline(buf, 20) =~ 'ancel:'})
970 call term_sendkeys(buf, "y")
971 call WaitFor({-> term_getstatus(buf) == "finished"})
972
973 " close the terminal window where Vim was running
974 quit
975endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100976
Bram Moolenaar012eb662018-03-13 17:55:27 +0100977func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100978 augroup repro
979 au!
980 au TerminalOpen * let s:called += 1
981 augroup END
982
983 let s:called = 0
984
985 " Open a terminal window with :terminal
986 terminal
987 call assert_equal(1, s:called)
988 bwipe!
989
990 " Open a terminal window with term_start()
991 call term_start(&shell)
992 call assert_equal(2, s:called)
993 bwipe!
994
995 " Open a hidden terminal buffer with :terminal
996 terminal ++hidden
997 call assert_equal(3, s:called)
998 for buf in term_list()
999 exe buf . "bwipe!"
1000 endfor
1001
1002 " Open a hidden terminal buffer with term_start()
1003 let buf = term_start(&shell, {'hidden': 1})
1004 call assert_equal(4, s:called)
1005 exe buf . "bwipe!"
1006
1007 unlet s:called
1008 au! repro
1009endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001010
1011func Check_dump01(off)
1012 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1013 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001014 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001015endfunc
1016
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001017func Test_terminal_dumpwrite_composing()
1018 if !CanRunVimInTerminal()
1019 return
1020 endif
1021 let save_enc = &encoding
1022 set encoding=utf-8
1023 call assert_equal(1, winnr('$'))
1024
1025 let text = " a\u0300 e\u0302 o\u0308"
1026 call writefile([text], 'Xcomposing')
1027 let buf = RunVimInTerminal('Xcomposing', {})
1028 call WaitFor({-> term_getline(buf, 1) =~ text})
1029 call term_dumpwrite(buf, 'Xdump')
1030 let dumpline = readfile('Xdump')[0]
1031 call assert_match('|à| |ê| |ö', dumpline)
1032
1033 call StopVimInTerminal(buf)
1034 call delete('Xcomposing')
1035 call delete('Xdump')
1036 let &encoding = save_enc
1037endfunc
1038
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001039" just testing basic functionality.
1040func Test_terminal_dumpload()
1041 call assert_equal(1, winnr('$'))
1042 call term_dumpload('dumps/Test_popup_command_01.dump')
1043 call assert_equal(2, winnr('$'))
1044 call assert_equal(20, line('$'))
1045 call Check_dump01(0)
1046 quit
1047endfunc
1048
1049func Test_terminal_dumpdiff()
1050 call assert_equal(1, winnr('$'))
1051 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump')
1052 call assert_equal(2, winnr('$'))
1053 call assert_equal(62, line('$'))
1054 call Check_dump01(0)
1055 call Check_dump01(42)
1056 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1057 quit
1058endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001059
1060func Test_terminal_dumpdiff_options()
1061 set laststatus=0
1062 call assert_equal(1, winnr('$'))
1063 let height = winheight(0)
1064 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1065 call assert_equal(2, winnr('$'))
1066 call assert_equal(height, winheight(winnr()))
1067 call assert_equal(33, winwidth(winnr()))
1068 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1069 quit
1070
1071 call assert_equal(1, winnr('$'))
1072 let width = winwidth(0)
1073 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1074 call assert_equal(2, winnr('$'))
1075 call assert_equal(width, winwidth(winnr()))
1076 call assert_equal(13, winheight(winnr()))
1077 call assert_equal('something else', bufname('%'))
1078 quit
1079
1080 call assert_equal(1, winnr('$'))
1081 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1082 call assert_equal(1, winnr('$'))
1083 bwipe
1084
1085 set laststatus&
1086endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001087
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001088func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001089 call assert_equal(1, winnr('$'))
1090
1091 " Use the title termcap entries to output the escape sequence.
1092 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001093 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001094 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001095 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001096 \ 'redraw',
1097 \ "set t_ts=",
1098 \ ], 'Xscript')
1099 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001100 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001101 call assert_equal('Xtextfile', expand('%:t'))
1102 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001103 return buf
1104endfunc
1105
1106func Test_terminal_api_drop_newwin()
1107 if !CanRunVimInTerminal()
1108 return
1109 endif
1110 let buf = Api_drop_common('')
1111 call assert_equal(0, &bin)
1112 call assert_equal('', &fenc)
1113
1114 call StopVimInTerminal(buf)
1115 call delete('Xscript')
1116 bwipe Xtextfile
1117endfunc
1118
1119func Test_terminal_api_drop_newwin_bin()
1120 if !CanRunVimInTerminal()
1121 return
1122 endif
1123 let buf = Api_drop_common(',{"bin":1}')
1124 call assert_equal(1, &bin)
1125
1126 call StopVimInTerminal(buf)
1127 call delete('Xscript')
1128 bwipe Xtextfile
1129endfunc
1130
1131func Test_terminal_api_drop_newwin_binary()
1132 if !CanRunVimInTerminal()
1133 return
1134 endif
1135 let buf = Api_drop_common(',{"binary":1}')
1136 call assert_equal(1, &bin)
1137
1138 call StopVimInTerminal(buf)
1139 call delete('Xscript')
1140 bwipe Xtextfile
1141endfunc
1142
1143func Test_terminal_api_drop_newwin_nobin()
1144 if !CanRunVimInTerminal()
1145 return
1146 endif
1147 set binary
1148 let buf = Api_drop_common(',{"nobin":1}')
1149 call assert_equal(0, &bin)
1150
1151 call StopVimInTerminal(buf)
1152 call delete('Xscript')
1153 bwipe Xtextfile
1154 set nobinary
1155endfunc
1156
1157func Test_terminal_api_drop_newwin_nobinary()
1158 if !CanRunVimInTerminal()
1159 return
1160 endif
1161 set binary
1162 let buf = Api_drop_common(',{"nobinary":1}')
1163 call assert_equal(0, &bin)
1164
1165 call StopVimInTerminal(buf)
1166 call delete('Xscript')
1167 bwipe Xtextfile
1168 set nobinary
1169endfunc
1170
1171func Test_terminal_api_drop_newwin_ff()
1172 if !CanRunVimInTerminal()
1173 return
1174 endif
1175 let buf = Api_drop_common(',{"ff":"dos"}')
1176 call assert_equal("dos", &ff)
1177
1178 call StopVimInTerminal(buf)
1179 call delete('Xscript')
1180 bwipe Xtextfile
1181endfunc
1182
1183func Test_terminal_api_drop_newwin_fileformat()
1184 if !CanRunVimInTerminal()
1185 return
1186 endif
1187 let buf = Api_drop_common(',{"fileformat":"dos"}')
1188 call assert_equal("dos", &ff)
1189
1190 call StopVimInTerminal(buf)
1191 call delete('Xscript')
1192 bwipe Xtextfile
1193endfunc
1194
1195func Test_terminal_api_drop_newwin_enc()
1196 if !CanRunVimInTerminal()
1197 return
1198 endif
1199 let buf = Api_drop_common(',{"enc":"utf-16"}')
1200 call assert_equal("utf-16", &fenc)
1201
1202 call StopVimInTerminal(buf)
1203 call delete('Xscript')
1204 bwipe Xtextfile
1205endfunc
1206
1207func Test_terminal_api_drop_newwin_encoding()
1208 if !CanRunVimInTerminal()
1209 return
1210 endif
1211 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1212 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001213
1214 call StopVimInTerminal(buf)
1215 call delete('Xscript')
1216 bwipe Xtextfile
1217endfunc
1218
1219func Test_terminal_api_drop_oldwin()
1220 if !CanRunVimInTerminal()
1221 return
1222 endif
1223 let firstwinid = win_getid()
1224 split Xtextfile
1225 let textfile_winid = win_getid()
1226 call assert_equal(2, winnr('$'))
1227 call win_gotoid(firstwinid)
1228
1229 " Use the title termcap entries to output the escape sequence.
1230 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001231 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001232 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1233 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1234 \ 'redraw',
1235 \ "set t_ts=",
1236 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001237 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001238 call WaitFor({-> expand('%:t') =='Xtextfile'})
1239 call assert_equal(textfile_winid, win_getid())
1240
1241 call StopVimInTerminal(buf)
1242 call delete('Xscript')
1243 bwipe Xtextfile
1244endfunc
1245
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001246func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001247 let g:called_bufnum = a:bufnum
1248 let g:called_arg = a:arg
1249endfunc
1250
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001251func WriteApiCall(funcname)
1252 " Use the title termcap entries to output the escape sequence.
1253 call writefile([
1254 \ 'set title',
1255 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1256 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1257 \ 'redraw',
1258 \ "set t_ts=",
1259 \ ], 'Xscript')
1260endfunc
1261
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001262func Test_terminal_api_call()
1263 if !CanRunVimInTerminal()
1264 return
1265 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001266
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001267 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001268 let buf = RunVimInTerminal('-S Xscript', {})
1269 call WaitFor({-> exists('g:called_bufnum')})
1270 call assert_equal(buf, g:called_bufnum)
1271 call assert_equal(['hello', 123], g:called_arg)
1272
1273 call StopVimInTerminal(buf)
1274 call delete('Xscript')
1275 unlet g:called_bufnum
1276 unlet g:called_arg
1277endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001278
1279func Test_terminal_api_call_fails()
1280 if !CanRunVimInTerminal()
1281 return
1282 endif
1283
1284 call WriteApiCall('TryThis')
1285 call ch_logfile('Xlog', 'w')
1286 let buf = RunVimInTerminal('-S Xscript', {})
1287 call WaitFor({-> string(readfile('Xlog')) =~ 'Invalid function name: TryThis'})
1288
1289 call StopVimInTerminal(buf)
1290 call delete('Xscript')
1291 call ch_logfile('', '')
1292 call delete('Xlog')
1293endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001294
Bram Moolenaara997b452018-04-17 23:24:06 +02001295let s:caught_e937 = 0
1296
1297func Tapi_Delete(bufnum, arg)
1298 try
1299 execute 'bdelete!' a:bufnum
1300 catch /E937:/
1301 let s:caught_e937 = 1
1302 endtry
1303endfunc
1304
1305func Test_terminal_api_call_fail_delete()
1306 if !CanRunVimInTerminal()
1307 return
1308 endif
1309
1310 call WriteApiCall('Tapi_Delete')
1311 let buf = RunVimInTerminal('-S Xscript', {})
1312 call WaitFor({-> s:caught_e937 == 1})
1313
1314 call StopVimInTerminal(buf)
1315 call delete('Xscript')
1316 call ch_logfile('', '')
1317endfunc
1318
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001319func Test_terminal_ansicolors_default()
1320 let colors = [
1321 \ '#000000', '#e00000',
1322 \ '#00e000', '#e0e000',
1323 \ '#0000e0', '#e000e0',
1324 \ '#00e0e0', '#e0e0e0',
1325 \ '#808080', '#ff4040',
1326 \ '#40ff40', '#ffff40',
1327 \ '#4040ff', '#ff40ff',
1328 \ '#40ffff', '#ffffff',
1329 \]
1330
1331 let buf = Run_shell_in_terminal({})
1332 call assert_equal(colors, term_getansicolors(buf))
1333 call Stop_shell_in_terminal(buf)
1334 call term_wait(buf)
1335
1336 exe buf . 'bwipe'
1337endfunc
1338
1339let s:test_colors = [
1340 \ '#616e64', '#0d0a79',
1341 \ '#6d610d', '#0a7373',
1342 \ '#690d0a', '#6d696e',
1343 \ '#0d0a6f', '#616e0d',
1344 \ '#0a6479', '#6d0d0a',
1345 \ '#617373', '#0d0a69',
1346 \ '#6d690d', '#0a6e6f',
1347 \ '#610d0a', '#6e6479',
1348 \]
1349
1350func Test_terminal_ansicolors_global()
1351 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1352 let buf = Run_shell_in_terminal({})
1353 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1354 call Stop_shell_in_terminal(buf)
1355 call term_wait(buf)
1356
1357 exe buf . 'bwipe'
1358 unlet g:terminal_ansi_colors
1359endfunc
1360
1361func Test_terminal_ansicolors_func()
1362 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1363 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1364 call assert_equal(s:test_colors, term_getansicolors(buf))
1365
1366 call term_setansicolors(buf, g:terminal_ansi_colors)
1367 call assert_equal(g:terminal_ansi_colors, term_getansicolors(buf))
1368
1369 let colors = [
1370 \ 'ivory', 'AliceBlue',
1371 \ 'grey67', 'dark goldenrod',
1372 \ 'SteelBlue3', 'PaleVioletRed4',
1373 \ 'MediumPurple2', 'yellow2',
1374 \ 'RosyBrown3', 'OrangeRed2',
1375 \ 'white smoke', 'navy blue',
1376 \ 'grey47', 'gray97',
1377 \ 'MistyRose2', 'DodgerBlue4',
1378 \]
1379 call term_setansicolors(buf, colors)
1380
1381 let colors[4] = 'Invalid'
1382 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1383
1384 call Stop_shell_in_terminal(buf)
1385 call term_wait(buf)
1386 exe buf . 'bwipe'
1387endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001388
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001389func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001390 if !CanRunVimInTerminal()
1391 return
1392 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001393 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001394 let text = []
1395 for n in range(10)
1396 call add(text, repeat(n, 50))
1397 endfor
1398 call writefile(text, 'Xwinsize')
1399 let buf = RunVimInTerminal('Xwinsize', {})
1400 let win = bufwinid(buf)
1401 call assert_equal([6, 40], term_getsize(buf))
1402 call assert_equal(6, winheight(win))
1403 call assert_equal(40, winwidth(win))
1404
1405 " resizing the window doesn't resize the terminal.
1406 resize 10
1407 vertical resize 60
1408 call assert_equal([6, 40], term_getsize(buf))
1409 call assert_equal(10, winheight(win))
1410 call assert_equal(60, winwidth(win))
1411
1412 call StopVimInTerminal(buf)
1413 call delete('Xwinsize')
1414
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001415 call assert_fails('set termwinsize=40', 'E474')
1416 call assert_fails('set termwinsize=10+40', 'E474')
1417 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001418
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001419 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001420endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001421
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001422func Test_terminal_termwinsize_option_zero()
1423 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001424 let buf = Run_shell_in_terminal({})
1425 let win = bufwinid(buf)
1426 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1427 call Stop_shell_in_terminal(buf)
1428 call term_wait(buf)
1429 exe buf . 'bwipe'
1430
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001431 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001432 let buf = Run_shell_in_terminal({})
1433 let win = bufwinid(buf)
1434 call assert_equal([7, winwidth(win)], term_getsize(buf))
1435 call Stop_shell_in_terminal(buf)
1436 call term_wait(buf)
1437 exe buf . 'bwipe'
1438
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001439 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001440 let buf = Run_shell_in_terminal({})
1441 let win = bufwinid(buf)
1442 call assert_equal([winheight(win), 33], term_getsize(buf))
1443 call Stop_shell_in_terminal(buf)
1444 call term_wait(buf)
1445 exe buf . 'bwipe'
1446
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001447 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001448endfunc
1449
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001450func Test_terminal_termwinsize_mininmum()
1451 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001452 vsplit
1453 let buf = Run_shell_in_terminal({})
1454 let win = bufwinid(buf)
1455 call assert_inrange(10, 1000, winheight(win))
1456 call assert_inrange(50, 1000, winwidth(win))
1457 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1458
1459 resize 15
1460 vertical resize 60
1461 redraw
1462 call assert_equal([15, 60], term_getsize(buf))
1463 call assert_equal(15, winheight(win))
1464 call assert_equal(60, winwidth(win))
1465
1466 resize 7
1467 vertical resize 30
1468 redraw
1469 call assert_equal([10, 50], term_getsize(buf))
1470 call assert_equal(7, winheight(win))
1471 call assert_equal(30, winwidth(win))
1472
1473 call Stop_shell_in_terminal(buf)
1474 call term_wait(buf)
1475 exe buf . 'bwipe'
1476
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001477 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001478 let buf = Run_shell_in_terminal({})
1479 let win = bufwinid(buf)
1480 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1481 call Stop_shell_in_terminal(buf)
1482 call term_wait(buf)
1483 exe buf . 'bwipe'
1484
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001485 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001486endfunc