blob: 82459f85db8c7b802bea8329130442327c1205f8 [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()
324 if !has('unix')
325 " tail -f only works on Unix
326 return
327 endif
328
329 call writefile(range(50), 'Xtext')
330 call writefile([
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100331 \ 'set shell=/bin/sh noruler',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100332 \ 'terminal',
Bram Moolenaar7e841e32019-02-15 00:26:14 +0100333 \ 'sleep 200m',
Bram Moolenaar5ff7df52019-02-15 01:06:13 +0100334 \ 'call feedkeys("tail -n 100 -f Xtext\<CR>", "xt")',
335 \ 'sleep 100m',
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100336 \ 'call feedkeys("\<C-W>N", "xt")',
337 \ ], 'XTest_postponed')
338 let buf = RunVimInTerminal('-S XTest_postponed', {})
339 " Check that the Xtext lines are displayed and in Terminal-Normal mode
Bram Moolenaar96baf022019-02-14 23:49:38 +0100340 call VerifyScreenDump(buf, 'Test_terminal_01', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100341
342 silent !echo 'one more line' >>Xtext
Bram Moolenaar700dfaa2019-04-13 14:21:19 +0200343 " Screen will not change, move cursor to get a different dump
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100344 call term_sendkeys(buf, "k")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100345 call VerifyScreenDump(buf, 'Test_terminal_02', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100346
347 " Back to Terminal-Job mode, text will scroll and show the extra line.
348 call term_sendkeys(buf, "a")
Bram Moolenaar96baf022019-02-14 23:49:38 +0100349 call VerifyScreenDump(buf, 'Test_terminal_03', {})
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100350
351 call term_wait(buf)
352 call term_sendkeys(buf, "\<C-C>")
353 call term_wait(buf)
354 call term_sendkeys(buf, "exit\<CR>")
355 call term_wait(buf)
356 call term_sendkeys(buf, ":q\<CR>")
357 call StopVimInTerminal(buf)
358 call delete('XTest_postponed')
359 call delete('Xtext')
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200360endfunc
361
Bram Moolenaar81aa0f52019-02-14 23:23:19 +0100362" Run diff on two dumps with different size.
363func Test_terminal_dumpdiff_size()
364 call assert_equal(1, winnr('$'))
365 call term_dumpdiff('dumps/Test_incsearch_search_01.dump', 'dumps/Test_popup_command_01.dump')
366 call assert_equal(2, winnr('$'))
367 call assert_match('Test_incsearch_search_01.dump', getline(10))
368 call assert_match(' +++++$', getline(11))
369 call assert_match('Test_popup_command_01.dump', getline(31))
370 call assert_equal(repeat('+', 75), getline(30))
371 quit
372endfunc
373
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200374func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200375 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200376
Bram Moolenaarb2412082017-08-20 18:09:14 +0200377 exe 'terminal ++rows=5 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200378 let size = term_getsize('')
379 bwipe!
380 call assert_equal(5, size[0])
381
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200382 call term_start(cmd, {'term_rows': 6})
383 let size = term_getsize('')
384 bwipe!
385 call assert_equal(6, size[0])
386
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200387 vsplit
Bram Moolenaarb2412082017-08-20 18:09:14 +0200388 exe 'terminal ++rows=5 ++cols=33 ' . cmd
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200389 call assert_equal([5, 33], ''->term_getsize())
Bram Moolenaara42d3632018-04-14 17:05:38 +0200390
391 call term_setsize('', 6, 0)
392 call assert_equal([6, 33], term_getsize(''))
393
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200394 eval ''->term_setsize(0, 35)
Bram Moolenaara42d3632018-04-14 17:05:38 +0200395 call assert_equal([6, 35], term_getsize(''))
396
397 call term_setsize('', 7, 30)
398 call assert_equal([7, 30], term_getsize(''))
399
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200400 bwipe!
Bram Moolenaar6e72cd02018-04-14 21:31:35 +0200401 call assert_fails("call term_setsize('', 7, 30)", "E955:")
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200402
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200403 call term_start(cmd, {'term_rows': 6, 'term_cols': 36})
404 let size = term_getsize('')
405 bwipe!
406 call assert_equal([6, 36], size)
407
Bram Moolenaarb2412082017-08-20 18:09:14 +0200408 exe 'vertical terminal ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200409 let size = term_getsize('')
410 bwipe!
411 call assert_equal(20, size[1])
412
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200413 eval cmd->term_start({'vertical': 1, 'term_cols': 26})
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200414 let size = term_getsize('')
415 bwipe!
416 call assert_equal(26, size[1])
417
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200418 split
Bram Moolenaarb2412082017-08-20 18:09:14 +0200419 exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200420 let size = term_getsize('')
421 bwipe!
422 call assert_equal([6, 20], size)
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200423
424 call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27})
425 let size = term_getsize('')
426 bwipe!
427 call assert_equal([7, 27], size)
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200428
429 call delete('Xtext')
Bram Moolenaarda43b612017-08-11 22:27:50 +0200430endfunc
431
432func Test_terminal_curwin()
433 let cmd = Get_cat_123_cmd()
434 call assert_equal(1, winnr('$'))
435
436 split dummy
437 exe 'terminal ++curwin ' . cmd
438 call assert_equal(2, winnr('$'))
439 bwipe!
440
441 split dummy
442 call term_start(cmd, {'curwin': 1})
443 call assert_equal(2, winnr('$'))
444 bwipe!
445
446 split dummy
447 call setline(1, 'change')
448 call assert_fails('terminal ++curwin ' . cmd, 'E37:')
449 call assert_equal(2, winnr('$'))
450 exe 'terminal! ++curwin ' . cmd
451 call assert_equal(2, winnr('$'))
452 bwipe!
453
454 split dummy
455 call setline(1, 'change')
456 call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
457 call assert_equal(2, winnr('$'))
458 bwipe!
459
460 split dummy
461 bwipe!
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200462 call delete('Xtext')
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200463endfunc
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200464
Bram Moolenaarff546792017-11-21 14:47:57 +0100465func s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200466 if s:python != ''
467 let cmd = s:python . " test_short_sleep.py"
Bram Moolenaarc8523e22018-06-03 18:22:02 +0200468 " 500 was not enough for Travis
469 let waittime = 900
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200470 else
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200471 echo 'This will take five seconds...'
472 let waittime = 2000
473 if has('win32')
474 let cmd = $windir . '\system32\timeout.exe 1'
475 else
476 let cmd = 'sleep 1'
477 endif
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200478 endif
Bram Moolenaarff546792017-11-21 14:47:57 +0100479 return [cmd, waittime]
480endfunc
481
482func Test_terminal_finish_open_close()
483 call assert_equal(1, winnr('$'))
484
485 let [cmd, waittime] = s:get_sleep_cmd()
Bram Moolenaarb81bc772017-08-11 22:45:01 +0200486
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100487 " shell terminal closes automatically
488 terminal
489 let buf = bufnr('%')
490 call assert_equal(2, winnr('$'))
491 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200492 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200493 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200494 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100495
496 " shell terminal that does not close automatically
497 terminal ++noclose
498 let buf = bufnr('%')
499 call assert_equal(2, winnr('$'))
500 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200501 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200502 call StopShellInTerminal(buf)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100503 call assert_equal(2, winnr('$'))
504 quit
505 call assert_equal(1, winnr('$'))
506
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200507 exe 'terminal ++close ' . cmd
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200508 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200509 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200510 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200511
512 call term_start(cmd, {'term_finish': 'close'})
513 call assert_equal(2, winnr('$'))
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200514 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200515 call WaitForAssert({-> assert_equal(1, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200516 call assert_equal(1, winnr('$'))
517
518 exe 'terminal ++open ' . cmd
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200519 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200520 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200521 bwipe
522
523 call term_start(cmd, {'term_finish': 'open'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200524 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200525 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200526 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200527
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200528 exe 'terminal ++hidden ++open ' . cmd
529 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200530 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200531 bwipe
532
533 call term_start(cmd, {'term_finish': 'open', 'hidden': 1})
534 call assert_equal(1, winnr('$'))
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200535 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200536 bwipe
Bram Moolenaar37c45832017-08-12 16:01:04 +0200537
538 call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:')
539 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:')
540 call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:')
541 call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:')
542
543 call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'})
Bram Moolenaar97a80e42017-08-30 13:31:49 +0200544 close!
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200545 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaar37c45832017-08-12 16:01:04 +0200546 call assert_equal(4, winheight(0))
547 bwipe
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200548endfunc
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200549
550func Test_terminal_cwd()
Bram Moolenaare9f6fd22017-09-10 14:25:49 +0200551 if !executable('pwd')
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200552 return
553 endif
554 call mkdir('Xdir')
555 let buf = term_start('pwd', {'cwd': 'Xdir'})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200556 call WaitForAssert({-> assert_equal('Xdir', fnamemodify(getline(1), ":t"))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200557
558 exe buf . 'bwipe'
559 call delete('Xdir', 'rf')
560endfunc
561
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200562func Test_terminal_cwd_failure()
563 " Case 1: Provided directory is not actually a directory. Attempt to make
564 " the file executable as well.
565 call writefile([], 'Xfile')
566 call setfperm('Xfile', 'rwx------')
567 call assert_fails("call term_start(&shell, {'cwd': 'Xfile'})", 'E475:')
568 call delete('Xfile')
569
570 " Case 2: Directory does not exist.
571 call assert_fails("call term_start(&shell, {'cwd': 'Xdir'})", 'E475:')
572
573 " Case 3: Directory exists but is not accessible.
Bram Moolenaar0b38f542018-11-03 21:47:16 +0100574 " Skip this for root, it will be accessible anyway.
575 if $USER != 'root'
576 call mkdir('XdirNoAccess', '', '0600')
577 " return early if the directory permissions could not be set properly
578 if getfperm('XdirNoAccess')[2] == 'x'
579 call delete('XdirNoAccess', 'rf')
580 return
581 endif
582 call assert_fails("call term_start(&shell, {'cwd': 'XdirNoAccess'})", 'E475:')
583 call delete('XdirNoAccess', 'rf')
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200584 endif
Bram Moolenaar839e81e2018-10-19 16:53:39 +0200585endfunc
586
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100587func Test_terminal_servername()
588 if !has('clientserver')
589 return
590 endif
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200591 call s:test_environment("VIM_SERVERNAME", v:servername)
592endfunc
593
594func Test_terminal_version()
595 call s:test_environment("VIM_TERMINAL", string(v:version))
596endfunc
597
598func s:test_environment(name, value)
Bram Moolenaar012eb662018-03-13 17:55:27 +0100599 let buf = Run_shell_in_terminal({})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100600 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200601 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100602 if has('win32')
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200603 call term_sendkeys(buf, "echo %" . a:name . "%\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100604 else
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200605 call term_sendkeys(buf, "echo $" . a:name . "\r")
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100606 endif
Bram Moolenaar012eb662018-03-13 17:55:27 +0100607 call term_wait(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200608 call StopShellInTerminal(buf)
Bram Moolenaard7a137f2018-06-12 18:05:24 +0200609 call WaitForAssert({-> assert_equal(a:value, getline(2))})
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100610
Bram Moolenaar012eb662018-03-13 17:55:27 +0100611 exe buf . 'bwipe'
612 unlet buf
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +0100613endfunc
614
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200615func Test_terminal_env()
Bram Moolenaar012eb662018-03-13 17:55:27 +0100616 let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}})
Bram Moolenaar51c23682017-08-14 21:45:00 +0200617 " Wait for the shell to display a prompt
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200618 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100619 if has('win32')
Bram Moolenaar012eb662018-03-13 17:55:27 +0100620 call term_sendkeys(buf, "echo %TESTENV%\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100621 else
Bram Moolenaar012eb662018-03-13 17:55:27 +0100622 call term_sendkeys(buf, "echo $TESTENV\r")
Bram Moolenaarba6febd2017-10-30 21:56:23 +0100623 endif
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200624 eval buf->term_wait()
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200625 call StopShellInTerminal(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200626 call WaitForAssert({-> assert_equal('correct', getline(2))})
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200627
Bram Moolenaar012eb662018-03-13 17:55:27 +0100628 exe buf . 'bwipe'
Bram Moolenaar05aafed2017-08-11 19:12:11 +0200629endfunc
Bram Moolenaar679653e2017-08-13 14:13:19 +0200630
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200631func Test_terminal_list_args()
632 let buf = term_start([&shell, &shellcmdflag, 'echo "123"'])
633 call assert_fails(buf . 'bwipe', 'E517')
634 exe buf . 'bwipe!'
635 call assert_equal("", bufname(buf))
636endfunction
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200637
638func Test_terminal_noblock()
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100639 let buf = term_start(&shell)
Bram Moolenaar39536dd2019-01-29 22:58:21 +0100640 if has('bsd') || has('mac') || has('sun')
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200641 " The shell or something else has a problem dealing with more than 1000
642 " characters at the same time.
643 let len = 1000
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100644 " NPFS is used in Windows, nonblocking mode does not work properly.
645 elseif has('win32')
646 let len = 1
Bram Moolenaard8d85bf2017-09-03 18:08:00 +0200647 else
648 let len = 5000
649 endif
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200650
651 for c in ['a','b','c','d','e','f','g','h','i','j','k']
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100652 call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>")
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200653 endfor
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100654 call term_sendkeys(buf, "echo done\<cr>")
Bram Moolenaareef05312017-08-20 20:21:23 +0200655
656 " On MS-Windows there is an extra empty line below "done". Find "done" in
657 " the last-but-one or the last-but-two line.
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100658 let lnum = term_getsize(buf)[0] - 1
Bram Moolenaar21810142018-02-02 18:30:36 +0100659 call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100660 let line = term_getline(buf, lnum)
Bram Moolenaareef05312017-08-20 20:21:23 +0200661 if line !~ 'done'
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100662 let line = term_getline(buf, lnum - 1)
Bram Moolenaareef05312017-08-20 20:21:23 +0200663 endif
664 call assert_match('done', line)
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200665
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100666 let g:job = term_getjob(buf)
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200667 call StopShellInTerminal(buf)
Bram Moolenaarab8b1c12017-11-04 19:24:31 +0100668 call term_wait(buf)
Bram Moolenaard21f8b52017-08-19 15:40:01 +0200669 unlet g:job
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200670 bwipe
671endfunc
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200672
673func Test_terminal_write_stdin()
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200674 if !executable('wc')
Bram Moolenaardada6d22017-09-02 17:18:35 +0200675 throw 'skipped: wc command not available'
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200676 endif
Bram Moolenaar1580f752018-06-03 15:26:36 +0200677 if has('win32')
678 " TODO: enable once writing to stdin works on MS-Windows
679 return
680 endif
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200681 new
682 call setline(1, ['one', 'two', 'three'])
683 %term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200684 call WaitForAssert({-> assert_match('3', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200685 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200686 call assert_equal(['3', '3', '14'], nrs)
687 bwipe
688
Bram Moolenaardada6d22017-09-02 17:18:35 +0200689 new
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200690 call setline(1, ['one', 'two', 'three', 'four'])
691 2,3term wc
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200692 call WaitForAssert({-> assert_match('2', getline("$"))})
Bram Moolenaar3346cc42017-09-02 14:54:21 +0200693 let nrs = split(getline('$'))
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200694 call assert_equal(['2', '2', '10'], nrs)
695 bwipe
696
Bram Moolenaardada6d22017-09-02 17:18:35 +0200697 if executable('python')
698 new
699 call setline(1, ['print("hello")'])
700 1term ++eof=exit() python
701 " MS-Windows echoes the input, Unix doesn't.
702 call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"')
703 if getline(1) =~ 'hello'
704 call assert_equal('hello', getline(1))
705 else
706 call assert_equal('hello', getline(line('$') - 1))
707 endif
708 bwipe
709
710 if has('win32')
711 new
712 call setline(1, ['print("hello")'])
713 1term ++eof=<C-Z> python
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200714 call WaitForAssert({-> assert_match('Z', getline("$"))})
Bram Moolenaardada6d22017-09-02 17:18:35 +0200715 call assert_equal('hello', getline(line('$') - 1))
716 bwipe
717 endif
718 endif
719
Bram Moolenaar37819ed2017-08-20 19:33:47 +0200720 bwipe!
721endfunc
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200722
723func Test_terminal_no_cmd()
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200724 let buf = term_start('NONE', {})
725 call assert_notequal(0, buf)
726
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200727 let pty = job_info(term_getjob(buf))['tty_out']
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200728 call assert_notequal('', pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100729 if has('gui_running') && !has('win32')
730 " In the GUI job_start() doesn't work, it does not read from the pty.
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200731 call system('echo "look here" > ' . pty)
Bram Moolenaarcfc15232019-01-23 22:33:18 +0100732 else
733 " Otherwise using a job works on all systems.
734 call job_start([&shell, &shellcmdflag, 'echo "look here" > ' . pty])
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200735 endif
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200736 call WaitForAssert({-> assert_match('look here', term_getline(buf, 1))})
Bram Moolenaar2dc9d262017-09-08 14:39:30 +0200737
Bram Moolenaar13ebb032017-08-26 22:02:51 +0200738 bwipe!
739endfunc
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200740
741func Test_terminal_special_chars()
742 " this file name only works on Unix
743 if !has('unix')
744 return
745 endif
746 call mkdir('Xdir with spaces')
747 call writefile(['x'], 'Xdir with spaces/quoted"file')
748 term ls Xdir\ with\ spaces/quoted\"file
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200749 call WaitForAssert({-> assert_match('quoted"file', term_getline('', 1))})
Bram Moolenaar9d654a82017-09-03 19:52:17 +0200750 call term_wait('')
751
752 call delete('Xdir with spaces', 'rf')
753 bwipe
754endfunc
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200755
756func Test_terminal_wrong_options()
757 call assert_fails('call term_start(&shell, {
758 \ "in_io": "file",
759 \ "in_name": "xxx",
760 \ "out_io": "file",
761 \ "out_name": "xxx",
762 \ "err_io": "file",
763 \ "err_name": "xxx"
764 \ })', 'E474:')
765 call assert_fails('call term_start(&shell, {
766 \ "out_buf": bufnr("%")
767 \ })', 'E474:')
768 call assert_fails('call term_start(&shell, {
769 \ "err_buf": bufnr("%")
770 \ })', 'E474:')
771endfunc
772
773func Test_terminal_redir_file()
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200774 let cmd = Get_cat_123_cmd()
775 let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'})
776 call term_wait(buf)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100777 " ConPTY may precede escape sequence. There are things that are not so.
778 if !has('conpty')
779 call WaitForAssert({-> assert_notequal(0, len(readfile("Xfile")))})
780 call assert_match('123', readfile('Xfile')[0])
781 endif
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200782 let g:job = term_getjob(buf)
783 call WaitForAssert({-> assert_equal("dead", job_status(g:job))})
784 call delete('Xfile')
785 bwipe
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200786
787 if has('unix')
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200788 call writefile(['one line'], 'Xfile')
789 let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'})
790 call term_wait(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200791 call WaitForAssert({-> assert_equal('one line', term_getline(buf, 1))})
Bram Moolenaar8b53b792017-09-05 20:29:25 +0200792 let g:job = term_getjob(buf)
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200793 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaare88fc7a2017-09-03 20:59:40 +0200794 bwipe
795 call delete('Xfile')
796 endif
797endfunc
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200798
799func TerminalTmap(remap)
800 let buf = Run_shell_in_terminal({})
801 call assert_equal('t', mode())
802
803 if a:remap
804 tmap 123 456
805 else
806 tnoremap 123 456
807 endif
Bram Moolenaar461fe502017-12-05 12:30:03 +0100808 " don't use abcde, it's an existing command
809 tmap 456 abxde
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200810 call assert_equal('456', maparg('123', 't'))
Bram Moolenaar461fe502017-12-05 12:30:03 +0100811 call assert_equal('abxde', maparg('456', 't'))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200812 call feedkeys("123", 'tx')
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200813 call WaitForAssert({-> assert_match('abxde\|456', term_getline(buf, term_getcursor(buf)[0]))})
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200814 let lnum = term_getcursor(buf)[0]
815 if a:remap
Bram Moolenaar461fe502017-12-05 12:30:03 +0100816 call assert_match('abxde', term_getline(buf, lnum))
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200817 else
818 call assert_match('456', term_getline(buf, lnum))
819 endif
820
821 call term_sendkeys(buf, "\r")
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200822 call StopShellInTerminal(buf)
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +0200823 call term_wait(buf)
824
825 tunmap 123
826 tunmap 456
827 call assert_equal('', maparg('123', 't'))
828 close
829 unlet g:job
830endfunc
831
832func Test_terminal_tmap()
833 call TerminalTmap(1)
834 call TerminalTmap(0)
835endfunc
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200836
837func Test_terminal_wall()
838 let buf = Run_shell_in_terminal({})
839 wall
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200840 call StopShellInTerminal(buf)
Bram Moolenaar059db5c2017-10-15 22:42:23 +0200841 call term_wait(buf)
842 exe buf . 'bwipe'
843 unlet g:job
844endfunc
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200845
Bram Moolenaar7a760922018-02-19 23:10:02 +0100846func Test_terminal_wqall()
847 let buf = Run_shell_in_terminal({})
848 call assert_fails('wqall', 'E948')
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200849 call StopShellInTerminal(buf)
Bram Moolenaar7a760922018-02-19 23:10:02 +0100850 call term_wait(buf)
851 exe buf . 'bwipe'
852 unlet g:job
853endfunc
854
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200855func Test_terminal_composing_unicode()
856 let save_enc = &encoding
857 set encoding=utf-8
858
859 if has('win32')
860 let cmd = "cmd /K chcp 65001"
861 let lnum = [3, 6, 9]
862 else
863 let cmd = &shell
864 let lnum = [1, 3, 5]
865 endif
866
867 enew
868 let buf = term_start(cmd, {'curwin': bufnr('')})
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100869 let g:job = term_getjob(buf)
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200870 call term_wait(buf, 50)
871
Bram Moolenaarebe74b72018-04-21 23:34:43 +0200872 if has('win32')
873 call assert_equal('cmd', job_info(g:job).cmd[0])
874 else
875 call assert_equal(&shell, job_info(g:job).cmd[0])
876 endif
877
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200878 " ascii + composing
879 let txt = "a\u0308bc"
880 call term_sendkeys(buf, "echo " . txt . "\r")
881 call term_wait(buf, 50)
882 call assert_match("echo " . txt, term_getline(buf, lnum[0]))
883 call assert_equal(txt, term_getline(buf, lnum[0] + 1))
884 let l = term_scrape(buf, lnum[0] + 1)
885 call assert_equal("a\u0308", l[0].chars)
886 call assert_equal("b", l[1].chars)
887 call assert_equal("c", l[2].chars)
888
889 " multibyte + composing
890 let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099"
891 call term_sendkeys(buf, "echo " . txt . "\r")
892 call term_wait(buf, 50)
893 call assert_match("echo " . txt, term_getline(buf, lnum[1]))
894 call assert_equal(txt, term_getline(buf, lnum[1] + 1))
895 let l = term_scrape(buf, lnum[1] + 1)
896 call assert_equal("\u304b\u3099", l[0].chars)
897 call assert_equal("\u304e", l[1].chars)
898 call assert_equal("\u304f\u3099", l[2].chars)
899 call assert_equal("\u3052", l[3].chars)
900 call assert_equal("\u3053\u3099", l[4].chars)
901
902 " \u00a0 + composing
903 let txt = "abc\u00a0\u0308"
904 call term_sendkeys(buf, "echo " . txt . "\r")
905 call term_wait(buf, 50)
906 call assert_match("echo " . txt, term_getline(buf, lnum[2]))
907 call assert_equal(txt, term_getline(buf, lnum[2] + 1))
908 let l = term_scrape(buf, lnum[2] + 1)
909 call assert_equal("\u00a0\u0308", l[3].chars)
910
911 call term_sendkeys(buf, "exit\r")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200912 call WaitForAssert({-> assert_equal('dead', job_status(g:job))})
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200913 bwipe!
Bram Moolenaar3e1c6172017-11-02 16:58:00 +0100914 unlet g:job
Bram Moolenaar6daeef12017-10-15 22:56:49 +0200915 let &encoding = save_enc
916endfunc
Bram Moolenaarff546792017-11-21 14:47:57 +0100917
918func Test_terminal_aucmd_on_close()
919 fun Nop()
920 let s:called = 1
921 endfun
922
923 aug repro
924 au!
925 au BufWinLeave * call Nop()
926 aug END
927
928 let [cmd, waittime] = s:get_sleep_cmd()
929
930 call assert_equal(1, winnr('$'))
931 new
932 call setline(1, ['one', 'two'])
933 exe 'term ++close ' . cmd
934 wincmd p
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200935 call WaitForAssert({-> assert_equal(2, winnr('$'))}, waittime)
Bram Moolenaarff546792017-11-21 14:47:57 +0100936 call assert_equal(1, s:called)
937 bwipe!
938
939 unlet s:called
940 au! repro
941 delfunc Nop
942endfunc
Bram Moolenaarede35bb2018-01-26 20:05:18 +0100943
944func Test_terminal_term_start_empty_command()
945 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
946 call assert_fails(cmd, 'E474')
947 let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})"
948 call assert_fails(cmd, 'E474')
949 let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})"
950 call assert_fails(cmd, 'E474')
951 let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})"
952 call assert_fails(cmd, 'E474')
953endfunc
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100954
955func Test_terminal_response_to_control_sequence()
956 if !has('unix')
957 return
958 endif
959
960 let buf = Run_shell_in_terminal({})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200961 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100962
Bram Moolenaar086eb872018-03-25 21:24:12 +0200963 call term_sendkeys(buf, "cat\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200964 call WaitForAssert({-> assert_match('cat', term_getline(buf, 1))})
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100965
Bram Moolenaar086eb872018-03-25 21:24:12 +0200966 " Request the cursor position.
967 call term_sendkeys(buf, "\x1b[6n\<CR>")
Bram Moolenaard4a282f2018-02-02 18:22:31 +0100968
969 " Wait for output from tty to display, below an empty line.
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +0200970 call WaitForAssert({-> assert_match('3;1R', term_getline(buf, 4))})
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100971
Bram Moolenaar086eb872018-03-25 21:24:12 +0200972 " End "cat" gently.
973 call term_sendkeys(buf, "\<CR>\<C-D>")
974
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200975 call StopShellInTerminal(buf)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100976 exe buf . 'bwipe'
977 unlet g:job
978endfunc
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100979
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100980" Run Vim, start a terminal in that Vim with the kill argument,
981" :qall works.
982func Run_terminal_qall_kill(line1, line2)
983 " 1. Open a terminal window and wait for the prompt to appear
984 " 2. set kill using term_setkill()
985 " 3. make Vim exit, it will kill the shell
986 let after = [
987 \ a:line1,
988 \ 'let buf = bufnr("%")',
989 \ 'while term_getline(buf, 1) =~ "^\\s*$"',
990 \ ' sleep 10m',
991 \ 'endwhile',
992 \ a:line2,
993 \ 'au VimLeavePre * call writefile(["done"], "Xdone")',
994 \ 'qall',
995 \ ]
996 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100997 return
998 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +0100999 call assert_equal("done", readfile("Xdone")[0])
1000 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001001endfunc
1002
1003" Run Vim in a terminal, then start a terminal in that Vim with a kill
1004" argument, check that :qall works.
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001005func Test_terminal_qall_kill_arg()
1006 call Run_terminal_qall_kill('term ++kill=kill', '')
1007endfunc
1008
1009" Run Vim, start a terminal in that Vim, set the kill argument with
1010" term_setkill(), check that :qall works.
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001011func Test_terminal_qall_kill_func()
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001012 call Run_terminal_qall_kill('term', 'eval buf->term_setkill("kill")')
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001013endfunc
1014
1015" Run Vim, start a terminal in that Vim without the kill argument,
1016" check that :qall does not exit, :qall! does.
1017func Test_terminal_qall_exit()
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001018 let after =<< trim [CODE]
1019 term
1020 let buf = bufnr("%")
1021 while term_getline(buf, 1) =~ "^\\s*$"
1022 sleep 10m
1023 endwhile
1024 set nomore
1025 au VimLeavePre * call writefile(["too early"], "Xdone")
1026 qall
1027 au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")
1028 cquit
1029 [CODE]
1030
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001031 if !RunVim([], after, '')
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001032 return
1033 endif
Bram Moolenaar3e8d3852018-03-20 17:43:01 +01001034 call assert_equal("done", readfile("Xdone")[0])
1035 call delete("Xdone")
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001036endfunc
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001037
1038" Run Vim in a terminal, then start a terminal in that Vim without a kill
1039" argument, check that :confirm qall works.
1040func Test_terminal_qall_prompt()
1041 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001042 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001043 endif
1044 let buf = RunVimInTerminal('', {})
1045
1046 " Open a terminal window and wait for the prompt to appear
1047 call term_sendkeys(buf, ":term\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001048 call WaitForAssert({-> assert_match('\[running]', term_getline(buf, 10))})
1049 call WaitForAssert({-> assert_notmatch('^\s*$', term_getline(buf, 1))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001050
1051 " make Vim exit, it will prompt to kill the shell
1052 call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001053 call WaitForAssert({-> assert_match('ancel:', term_getline(buf, 20))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001054 call term_sendkeys(buf, "y")
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001055 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar435acdb2018-03-10 20:51:25 +01001056
1057 " close the terminal window where Vim was running
1058 quit
1059endfunc
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001060
Bram Moolenaar012eb662018-03-13 17:55:27 +01001061func Test_terminal_open_autocmd()
Bram Moolenaarb852c3e2018-03-11 16:55:36 +01001062 augroup repro
1063 au!
1064 au TerminalOpen * let s:called += 1
1065 augroup END
1066
1067 let s:called = 0
1068
1069 " Open a terminal window with :terminal
1070 terminal
1071 call assert_equal(1, s:called)
1072 bwipe!
1073
1074 " Open a terminal window with term_start()
1075 call term_start(&shell)
1076 call assert_equal(2, s:called)
1077 bwipe!
1078
1079 " Open a hidden terminal buffer with :terminal
1080 terminal ++hidden
1081 call assert_equal(3, s:called)
1082 for buf in term_list()
1083 exe buf . "bwipe!"
1084 endfor
1085
1086 " Open a hidden terminal buffer with term_start()
1087 let buf = term_start(&shell, {'hidden': 1})
1088 call assert_equal(4, s:called)
1089 exe buf . "bwipe!"
1090
1091 unlet s:called
1092 au! repro
1093endfunction
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001094
1095func Check_dump01(off)
1096 call assert_equal('one two three four five', trim(getline(a:off + 1)))
1097 call assert_equal('~ Select Word', trim(getline(a:off + 7)))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001098 call assert_equal(':popup PopUp', trim(getline(a:off + 20)))
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001099endfunc
1100
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001101func Test_terminal_dumpwrite_composing()
1102 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001103 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001104 endif
1105 let save_enc = &encoding
1106 set encoding=utf-8
1107 call assert_equal(1, winnr('$'))
1108
1109 let text = " a\u0300 e\u0302 o\u0308"
1110 call writefile([text], 'Xcomposing')
Bram Moolenaar77bfd752018-04-30 18:03:10 +02001111 let buf = RunVimInTerminal('--cmd "set encoding=utf-8" Xcomposing', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001112 call WaitForAssert({-> assert_match(text, term_getline(buf, 1))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001113 eval 'Xdump'->term_dumpwrite(buf)
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02001114 let dumpline = readfile('Xdump')[0]
1115 call assert_match('|à| |ê| |ö', dumpline)
1116
1117 call StopVimInTerminal(buf)
1118 call delete('Xcomposing')
1119 call delete('Xdump')
1120 let &encoding = save_enc
1121endfunc
1122
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001123" just testing basic functionality.
1124func Test_terminal_dumpload()
Bram Moolenaar87abab92019-06-03 21:14:59 +02001125 let curbuf = winbufnr('')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001126 call assert_equal(1, winnr('$'))
Bram Moolenaar87abab92019-06-03 21:14:59 +02001127 let buf = term_dumpload('dumps/Test_popup_command_01.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001128 call assert_equal(2, winnr('$'))
1129 call assert_equal(20, line('$'))
1130 call Check_dump01(0)
Bram Moolenaar87abab92019-06-03 21:14:59 +02001131
1132 " Load another dump in the same window
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001133 let buf2 = 'dumps/Test_diff_01.dump'->term_dumpload({'bufnr': buf})
Bram Moolenaar87abab92019-06-03 21:14:59 +02001134 call assert_equal(buf, buf2)
1135 call assert_notequal('one two three four five', trim(getline(1)))
1136
1137 " Load the first dump again in the same window
1138 let buf2 = term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': buf})
1139 call assert_equal(buf, buf2)
1140 call Check_dump01(0)
1141
1142 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': curbuf})", 'E475:')
1143 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': 9999})", 'E86:')
1144 new
1145 let closedbuf = winbufnr('')
1146 quit
1147 call assert_fails("call term_dumpload('dumps/Test_popup_command_01.dump', {'bufnr': closedbuf})", 'E475:')
1148
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001149 quit
1150endfunc
1151
1152func Test_terminal_dumpdiff()
1153 call assert_equal(1, winnr('$'))
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001154 eval 'dumps/Test_popup_command_01.dump'->term_dumpdiff('dumps/Test_popup_command_02.dump')
Bram Moolenaar45d2a642018-03-24 14:30:32 +01001155 call assert_equal(2, winnr('$'))
1156 call assert_equal(62, line('$'))
1157 call Check_dump01(0)
1158 call Check_dump01(42)
1159 call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29])
1160 quit
1161endfunc
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001162
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01001163func Test_terminal_dumpdiff_swap()
1164 call assert_equal(1, winnr('$'))
1165 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_03.dump')
1166 call assert_equal(2, winnr('$'))
1167 call assert_equal(62, line('$'))
1168 call assert_match('Test_popup_command_01.dump', getline(21))
1169 call assert_match('Test_popup_command_03.dump', getline(42))
1170 call assert_match('Undo', getline(3))
1171 call assert_match('three four five', getline(45))
1172
1173 normal s
1174 call assert_match('Test_popup_command_03.dump', getline(21))
1175 call assert_match('Test_popup_command_01.dump', getline(42))
1176 call assert_match('three four five', getline(3))
1177 call assert_match('Undo', getline(45))
1178 quit
1179endfunc
1180
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001181func Test_terminal_dumpdiff_options()
1182 set laststatus=0
1183 call assert_equal(1, winnr('$'))
1184 let height = winheight(0)
1185 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33})
1186 call assert_equal(2, winnr('$'))
1187 call assert_equal(height, winheight(winnr()))
1188 call assert_equal(33, winwidth(winnr()))
1189 call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%'))
1190 quit
1191
1192 call assert_equal(1, winnr('$'))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001193 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'})
1194 call assert_equal(2, winnr('$'))
Bram Moolenaare809a4e2019-07-04 17:35:05 +02001195 call assert_equal(&columns, winwidth(0))
1196 call assert_equal(13, winheight(0))
Bram Moolenaar897e63c2018-03-24 17:16:33 +01001197 call assert_equal('something else', bufname('%'))
1198 quit
1199
1200 call assert_equal(1, winnr('$'))
1201 call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1})
1202 call assert_equal(1, winnr('$'))
1203 bwipe
1204
1205 set laststatus&
1206endfunc
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001207
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001208func Api_drop_common(options)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001209 call assert_equal(1, winnr('$'))
1210
1211 " Use the title termcap entries to output the escape sequence.
1212 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001213 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001214 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001215 \ 'let &titlestring = ''["drop","Xtextfile"' . a:options . ']''',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001216 \ 'redraw',
1217 \ "set t_ts=",
1218 \ ], 'Xscript')
1219 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar769e9d22018-04-11 20:53:49 +02001220 call WaitFor({-> bufnr('Xtextfile') > 0})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001221 call assert_equal('Xtextfile', expand('%:t'))
1222 call assert_true(winnr('$') >= 3)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001223 return buf
1224endfunc
1225
1226func Test_terminal_api_drop_newwin()
1227 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001228 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001229 endif
1230 let buf = Api_drop_common('')
1231 call assert_equal(0, &bin)
1232 call assert_equal('', &fenc)
1233
1234 call StopVimInTerminal(buf)
1235 call delete('Xscript')
1236 bwipe Xtextfile
1237endfunc
1238
1239func Test_terminal_api_drop_newwin_bin()
1240 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001241 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001242 endif
1243 let buf = Api_drop_common(',{"bin":1}')
1244 call assert_equal(1, &bin)
1245
1246 call StopVimInTerminal(buf)
1247 call delete('Xscript')
1248 bwipe Xtextfile
1249endfunc
1250
1251func Test_terminal_api_drop_newwin_binary()
1252 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001253 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001254 endif
1255 let buf = Api_drop_common(',{"binary":1}')
1256 call assert_equal(1, &bin)
1257
1258 call StopVimInTerminal(buf)
1259 call delete('Xscript')
1260 bwipe Xtextfile
1261endfunc
1262
1263func Test_terminal_api_drop_newwin_nobin()
1264 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001265 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001266 endif
1267 set binary
1268 let buf = Api_drop_common(',{"nobin":1}')
1269 call assert_equal(0, &bin)
1270
1271 call StopVimInTerminal(buf)
1272 call delete('Xscript')
1273 bwipe Xtextfile
1274 set nobinary
1275endfunc
1276
1277func Test_terminal_api_drop_newwin_nobinary()
1278 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001279 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001280 endif
1281 set binary
1282 let buf = Api_drop_common(',{"nobinary":1}')
1283 call assert_equal(0, &bin)
1284
1285 call StopVimInTerminal(buf)
1286 call delete('Xscript')
1287 bwipe Xtextfile
1288 set nobinary
1289endfunc
1290
1291func Test_terminal_api_drop_newwin_ff()
1292 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001293 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001294 endif
1295 let buf = Api_drop_common(',{"ff":"dos"}')
1296 call assert_equal("dos", &ff)
1297
1298 call StopVimInTerminal(buf)
1299 call delete('Xscript')
1300 bwipe Xtextfile
1301endfunc
1302
1303func Test_terminal_api_drop_newwin_fileformat()
1304 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001305 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001306 endif
1307 let buf = Api_drop_common(',{"fileformat":"dos"}')
1308 call assert_equal("dos", &ff)
1309
1310 call StopVimInTerminal(buf)
1311 call delete('Xscript')
1312 bwipe Xtextfile
1313endfunc
1314
1315func Test_terminal_api_drop_newwin_enc()
1316 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001317 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001318 endif
1319 let buf = Api_drop_common(',{"enc":"utf-16"}')
1320 call assert_equal("utf-16", &fenc)
1321
1322 call StopVimInTerminal(buf)
1323 call delete('Xscript')
1324 bwipe Xtextfile
1325endfunc
1326
1327func Test_terminal_api_drop_newwin_encoding()
1328 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001329 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar333b80a2018-04-04 22:57:29 +02001330 endif
1331 let buf = Api_drop_common(',{"encoding":"utf-16"}')
1332 call assert_equal("utf-16", &fenc)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001333
1334 call StopVimInTerminal(buf)
1335 call delete('Xscript')
1336 bwipe Xtextfile
1337endfunc
1338
1339func Test_terminal_api_drop_oldwin()
1340 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001341 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001342 endif
1343 let firstwinid = win_getid()
1344 split Xtextfile
1345 let textfile_winid = win_getid()
1346 call assert_equal(2, winnr('$'))
1347 call win_gotoid(firstwinid)
1348
1349 " Use the title termcap entries to output the escape sequence.
1350 call writefile([
Bram Moolenaarcf67a502018-03-25 20:31:32 +02001351 \ 'set title',
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001352 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1353 \ 'let &titlestring = ''["drop","Xtextfile"]''',
1354 \ 'redraw',
1355 \ "set t_ts=",
1356 \ ], 'Xscript')
Bram Moolenaar15a1c3f2018-03-25 18:56:25 +02001357 let buf = RunVimInTerminal('-S Xscript', {'rows': 10})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001358 call WaitForAssert({-> assert_equal('Xtextfile', expand('%:t'))})
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001359 call assert_equal(textfile_winid, win_getid())
1360
1361 call StopVimInTerminal(buf)
1362 call delete('Xscript')
1363 bwipe Xtextfile
1364endfunc
1365
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001366func Tapi_TryThis(bufnum, arg)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001367 let g:called_bufnum = a:bufnum
1368 let g:called_arg = a:arg
1369endfunc
1370
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001371func WriteApiCall(funcname)
1372 " Use the title termcap entries to output the escape sequence.
1373 call writefile([
1374 \ 'set title',
1375 \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"',
1376 \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''',
1377 \ 'redraw',
1378 \ "set t_ts=",
1379 \ ], 'Xscript')
1380endfunc
1381
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001382func Test_terminal_api_call()
1383 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001384 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001385 endif
Bram Moolenaar2de50f82018-03-25 19:09:56 +02001386
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001387 call WriteApiCall('Tapi_TryThis')
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02001388 let buf = RunVimInTerminal('-S Xscript', {})
1389 call WaitFor({-> exists('g:called_bufnum')})
1390 call assert_equal(buf, g:called_bufnum)
1391 call assert_equal(['hello', 123], g:called_arg)
1392
1393 call StopVimInTerminal(buf)
1394 call delete('Xscript')
1395 unlet g:called_bufnum
1396 unlet g:called_arg
1397endfunc
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001398
1399func Test_terminal_api_call_fails()
1400 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001401 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001402 endif
1403
1404 call WriteApiCall('TryThis')
1405 call ch_logfile('Xlog', 'w')
1406 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001407 call WaitForAssert({-> assert_match('Invalid function name: TryThis', string(readfile('Xlog')))})
Bram Moolenaar2a77d212018-03-26 21:38:52 +02001408
1409 call StopVimInTerminal(buf)
1410 call delete('Xscript')
1411 call ch_logfile('', '')
1412 call delete('Xlog')
1413endfunc
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001414
Bram Moolenaara997b452018-04-17 23:24:06 +02001415let s:caught_e937 = 0
1416
1417func Tapi_Delete(bufnum, arg)
1418 try
1419 execute 'bdelete!' a:bufnum
1420 catch /E937:/
1421 let s:caught_e937 = 1
1422 endtry
1423endfunc
1424
1425func Test_terminal_api_call_fail_delete()
1426 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001427 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaara997b452018-04-17 23:24:06 +02001428 endif
1429
1430 call WriteApiCall('Tapi_Delete')
1431 let buf = RunVimInTerminal('-S Xscript', {})
Bram Moolenaar0e9d1ae2018-04-30 14:28:24 +02001432 call WaitForAssert({-> assert_equal(1, s:caught_e937)})
Bram Moolenaara997b452018-04-17 23:24:06 +02001433
1434 call StopVimInTerminal(buf)
1435 call delete('Xscript')
1436 call ch_logfile('', '')
1437endfunc
1438
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001439func Test_terminal_ansicolors_default()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001440 if !exists('*term_getansicolors')
1441 throw 'Skipped: term_getansicolors() not supported'
1442 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001443 let colors = [
1444 \ '#000000', '#e00000',
1445 \ '#00e000', '#e0e000',
1446 \ '#0000e0', '#e000e0',
1447 \ '#00e0e0', '#e0e0e0',
1448 \ '#808080', '#ff4040',
1449 \ '#40ff40', '#ffff40',
1450 \ '#4040ff', '#ff40ff',
1451 \ '#40ffff', '#ffffff',
1452 \]
1453
1454 let buf = Run_shell_in_terminal({})
1455 call assert_equal(colors, term_getansicolors(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001456 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001457 call term_wait(buf)
1458
1459 exe buf . 'bwipe'
1460endfunc
1461
1462let s:test_colors = [
1463 \ '#616e64', '#0d0a79',
1464 \ '#6d610d', '#0a7373',
1465 \ '#690d0a', '#6d696e',
1466 \ '#0d0a6f', '#616e0d',
1467 \ '#0a6479', '#6d0d0a',
1468 \ '#617373', '#0d0a69',
1469 \ '#6d690d', '#0a6e6f',
1470 \ '#610d0a', '#6e6479',
1471 \]
1472
1473func Test_terminal_ansicolors_global()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001474 if !exists('*term_getansicolors')
1475 throw 'Skipped: term_getansicolors() not supported'
1476 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001477 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1478 let buf = Run_shell_in_terminal({})
1479 call assert_equal(g:terminal_ansi_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'
1484 unlet g:terminal_ansi_colors
1485endfunc
1486
1487func Test_terminal_ansicolors_func()
Bram Moolenaar981d9dc2019-07-04 22:32:39 +02001488 if !exists('*term_getansicolors')
1489 throw 'Skipped: term_getansicolors() not supported'
1490 endif
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001491 let g:terminal_ansi_colors = reverse(copy(s:test_colors))
1492 let buf = Run_shell_in_terminal({'ansi_colors': s:test_colors})
1493 call assert_equal(s:test_colors, term_getansicolors(buf))
1494
1495 call term_setansicolors(buf, g:terminal_ansi_colors)
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001496 call assert_equal(g:terminal_ansi_colors, buf->term_getansicolors())
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001497
1498 let colors = [
1499 \ 'ivory', 'AliceBlue',
1500 \ 'grey67', 'dark goldenrod',
1501 \ 'SteelBlue3', 'PaleVioletRed4',
1502 \ 'MediumPurple2', 'yellow2',
1503 \ 'RosyBrown3', 'OrangeRed2',
1504 \ 'white smoke', 'navy blue',
1505 \ 'grey47', 'gray97',
1506 \ 'MistyRose2', 'DodgerBlue4',
1507 \]
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001508 eval buf->term_setansicolors(colors)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001509
1510 let colors[4] = 'Invalid'
1511 call assert_fails('call term_setansicolors(buf, colors)', 'E474:')
1512
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001513 call StopShellInTerminal(buf)
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02001514 call term_wait(buf)
1515 exe buf . 'bwipe'
1516endfunc
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001517
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001518func Test_terminal_all_ansi_colors()
1519 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001520 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001521 endif
1522
1523 " Use all the ANSI colors.
1524 call writefile([
Bram Moolenaar9e587872019-05-13 20:27:23 +02001525 \ 'call setline(1, "AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPP XXYYZZ")',
Bram Moolenaara0aaf3c2019-04-13 23:18:21 +02001526 \ 'hi Tblack ctermfg=0 ctermbg=8',
1527 \ 'hi Tdarkred ctermfg=1 ctermbg=9',
1528 \ 'hi Tdarkgreen ctermfg=2 ctermbg=10',
1529 \ 'hi Tbrown ctermfg=3 ctermbg=11',
1530 \ 'hi Tdarkblue ctermfg=4 ctermbg=12',
1531 \ 'hi Tdarkmagenta ctermfg=5 ctermbg=13',
1532 \ 'hi Tdarkcyan ctermfg=6 ctermbg=14',
1533 \ 'hi Tlightgrey ctermfg=7 ctermbg=15',
1534 \ 'hi Tdarkgrey ctermfg=8 ctermbg=0',
1535 \ 'hi Tred ctermfg=9 ctermbg=1',
1536 \ 'hi Tgreen ctermfg=10 ctermbg=2',
1537 \ 'hi Tyellow ctermfg=11 ctermbg=3',
1538 \ 'hi Tblue ctermfg=12 ctermbg=4',
1539 \ 'hi Tmagenta ctermfg=13 ctermbg=5',
1540 \ 'hi Tcyan ctermfg=14 ctermbg=6',
1541 \ 'hi Twhite ctermfg=15 ctermbg=7',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001542 \ 'hi TdarkredBold ctermfg=1 cterm=bold',
1543 \ 'hi TgreenBold ctermfg=10 cterm=bold',
1544 \ 'hi TmagentaBold ctermfg=13 cterm=bold ctermbg=5',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001545 \ '',
1546 \ 'call matchadd("Tblack", "A")',
1547 \ 'call matchadd("Tdarkred", "B")',
1548 \ 'call matchadd("Tdarkgreen", "C")',
1549 \ 'call matchadd("Tbrown", "D")',
1550 \ 'call matchadd("Tdarkblue", "E")',
1551 \ 'call matchadd("Tdarkmagenta", "F")',
1552 \ 'call matchadd("Tdarkcyan", "G")',
1553 \ 'call matchadd("Tlightgrey", "H")',
1554 \ 'call matchadd("Tdarkgrey", "I")',
1555 \ 'call matchadd("Tred", "J")',
1556 \ 'call matchadd("Tgreen", "K")',
1557 \ 'call matchadd("Tyellow", "L")',
1558 \ 'call matchadd("Tblue", "M")',
1559 \ 'call matchadd("Tmagenta", "N")',
1560 \ 'call matchadd("Tcyan", "O")',
1561 \ 'call matchadd("Twhite", "P")',
Bram Moolenaar9e587872019-05-13 20:27:23 +02001562 \ 'call matchadd("TdarkredBold", "X")',
1563 \ 'call matchadd("TgreenBold", "Y")',
1564 \ 'call matchadd("TmagentaBold", "Z")',
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02001565 \ 'redraw',
1566 \ ], 'Xcolorscript')
1567 let buf = RunVimInTerminal('-S Xcolorscript', {'rows': 10})
1568 call VerifyScreenDump(buf, 'Test_terminal_all_ansi_colors', {})
1569
1570 call term_sendkeys(buf, ":q\<CR>")
1571 call StopVimInTerminal(buf)
1572 call delete('Xcolorscript')
1573endfunc
1574
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001575func Test_terminal_termwinsize_option_fixed()
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001576 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02001577 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001578 endif
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001579 set termwinsize=6x40
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001580 let text = []
1581 for n in range(10)
1582 call add(text, repeat(n, 50))
1583 endfor
1584 call writefile(text, 'Xwinsize')
1585 let buf = RunVimInTerminal('Xwinsize', {})
1586 let win = bufwinid(buf)
1587 call assert_equal([6, 40], term_getsize(buf))
1588 call assert_equal(6, winheight(win))
1589 call assert_equal(40, winwidth(win))
1590
1591 " resizing the window doesn't resize the terminal.
1592 resize 10
1593 vertical resize 60
1594 call assert_equal([6, 40], term_getsize(buf))
1595 call assert_equal(10, winheight(win))
1596 call assert_equal(60, winwidth(win))
1597
1598 call StopVimInTerminal(buf)
1599 call delete('Xwinsize')
1600
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001601 call assert_fails('set termwinsize=40', 'E474')
1602 call assert_fails('set termwinsize=10+40', 'E474')
1603 call assert_fails('set termwinsize=abc', 'E474')
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001604
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001605 set termwinsize=
Bram Moolenaara7eef3d2018-04-15 22:25:54 +02001606endfunc
Bram Moolenaar498c2562018-04-15 23:45:15 +02001607
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001608func Test_terminal_termwinsize_option_zero()
1609 set termwinsize=0x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001610 let buf = Run_shell_in_terminal({})
1611 let win = bufwinid(buf)
1612 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001613 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001614 call term_wait(buf)
1615 exe buf . 'bwipe'
1616
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001617 set termwinsize=7x0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001618 let buf = Run_shell_in_terminal({})
1619 let win = bufwinid(buf)
1620 call assert_equal([7, winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001621 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001622 call term_wait(buf)
1623 exe buf . 'bwipe'
1624
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001625 set termwinsize=0x33
Bram Moolenaar498c2562018-04-15 23:45:15 +02001626 let buf = Run_shell_in_terminal({})
1627 let win = bufwinid(buf)
1628 call assert_equal([winheight(win), 33], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001629 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001630 call term_wait(buf)
1631 exe buf . 'bwipe'
1632
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001633 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001634endfunc
1635
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02001636func Test_terminal_termwinsize_minimum()
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001637 set termwinsize=10*50
Bram Moolenaar498c2562018-04-15 23:45:15 +02001638 vsplit
1639 let buf = Run_shell_in_terminal({})
1640 let win = bufwinid(buf)
1641 call assert_inrange(10, 1000, winheight(win))
1642 call assert_inrange(50, 1000, winwidth(win))
1643 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
1644
1645 resize 15
1646 vertical resize 60
1647 redraw
1648 call assert_equal([15, 60], term_getsize(buf))
1649 call assert_equal(15, winheight(win))
1650 call assert_equal(60, winwidth(win))
1651
1652 resize 7
1653 vertical resize 30
1654 redraw
1655 call assert_equal([10, 50], term_getsize(buf))
1656 call assert_equal(7, winheight(win))
1657 call assert_equal(30, winwidth(win))
1658
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001659 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001660 call term_wait(buf)
1661 exe buf . 'bwipe'
1662
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001663 set termwinsize=0*0
Bram Moolenaar498c2562018-04-15 23:45:15 +02001664 let buf = Run_shell_in_terminal({})
1665 let win = bufwinid(buf)
1666 call assert_equal([winheight(win), winwidth(win)], term_getsize(buf))
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001667 call StopShellInTerminal(buf)
Bram Moolenaar498c2562018-04-15 23:45:15 +02001668 call term_wait(buf)
1669 exe buf . 'bwipe'
1670
Bram Moolenaar6d150f72018-04-21 20:03:20 +02001671 set termwinsize=
Bram Moolenaar498c2562018-04-15 23:45:15 +02001672endfunc
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001673
1674func Test_terminal_termwinkey()
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001675 " make three tabpages, terminal in the middle
1676 0tabnew
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001677 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001678 tabnew
1679 tabprev
1680 call assert_equal(1, winnr('$'))
1681 call assert_equal(2, tabpagenr())
1682 let thiswin = win_getid()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001683
1684 let buf = Run_shell_in_terminal({})
1685 let termwin = bufwinid(buf)
1686 set termwinkey=<C-L>
1687 call feedkeys("\<C-L>w", 'tx')
1688 call assert_equal(thiswin, win_getid())
1689 call feedkeys("\<C-W>w", 'tx')
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001690 call assert_equal(termwin, win_getid())
1691
Bram Moolenaara4b26992019-08-15 20:58:54 +02001692 set langmap=xjyk
1693 call feedkeys("\<C-L>x", 'tx')
1694 call assert_equal(thiswin, win_getid())
1695 call feedkeys("\<C-W>y", 'tx')
1696 call assert_equal(termwin, win_getid())
1697 set langmap=
1698
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001699 call feedkeys("\<C-L>gt", "xt")
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001700 call assert_equal(3, tabpagenr())
1701 tabprev
1702 call assert_equal(2, tabpagenr())
1703 call assert_equal(termwin, win_getid())
1704
1705 call feedkeys("\<C-L>gT", "xt")
1706 call assert_equal(1, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001707 tabnext
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001708 call assert_equal(2, tabpagenr())
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001709 call assert_equal(termwin, win_getid())
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001710
1711 let job = term_getjob(buf)
1712 call feedkeys("\<C-L>\<C-C>", 'tx')
1713 call WaitForAssert({-> assert_equal("dead", job_status(job))})
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001714
1715 set termwinkey&
Bram Moolenaar72e83c12019-02-22 16:09:52 +01001716 tabnext
1717 tabclose
Bram Moolenaar882d02e2019-02-22 17:56:43 +01001718 tabprev
1719 tabclose
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001720endfunc
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001721
1722func Test_terminal_out_err()
1723 if !has('unix')
1724 return
1725 endif
1726 call writefile([
1727 \ '#!/bin/sh',
1728 \ 'echo "this is standard error" >&2',
1729 \ 'echo "this is standard out" >&1',
1730 \ ], 'Xechoerrout.sh')
1731 call setfperm('Xechoerrout.sh', 'rwxrwx---')
1732
1733 let outfile = 'Xtermstdout'
1734 let buf = term_start(['./Xechoerrout.sh'], {'out_io': 'file', 'out_name': outfile})
Bram Moolenaar53191912018-06-19 20:08:14 +02001735
1736 call WaitFor({-> !empty(readfile(outfile)) && !empty(term_getline(buf, 1))})
1737 call assert_equal(['this is standard out'], readfile(outfile))
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001738 call assert_equal('this is standard error', term_getline(buf, 1))
1739
Bram Moolenaar54c6baf2018-05-12 21:12:12 +02001740 call WaitForAssert({-> assert_equal('dead', job_status(term_getjob(buf)))})
Bram Moolenaarcd8fb442018-05-12 17:42:42 +02001741 exe buf . 'bwipe'
1742 call delete('Xechoerrout.sh')
1743 call delete(outfile)
1744endfunc
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02001745
1746func Test_terminwinscroll()
1747 if !has('unix')
1748 return
1749 endif
1750
1751 " Let the terminal output more than 'termwinscroll' lines, some at the start
1752 " will be dropped.
1753 exe 'set termwinscroll=' . &lines
1754 let buf = term_start('/bin/sh')
1755 for i in range(1, &lines)
1756 call feedkeys("echo " . i . "\<CR>", 'xt')
1757 call WaitForAssert({-> assert_match(string(i), term_getline(buf, term_getcursor(buf)[0] - 1))})
1758 endfor
1759 " Go to Terminal-Normal mode to update the buffer.
1760 call feedkeys("\<C-W>N", 'xt')
1761 call assert_inrange(&lines, &lines * 110 / 100 + winheight(0), line('$'))
1762
1763 " Every "echo nr" must only appear once
1764 let lines = getline(1, line('$'))
1765 for i in range(&lines - len(lines) / 2 + 2, &lines)
1766 let filtered = filter(copy(lines), {idx, val -> val =~ 'echo ' . i . '\>'})
1767 call assert_equal(1, len(filtered), 'for "echo ' . i . '"')
1768 endfor
1769
1770 exe buf . 'bwipe!'
1771endfunc
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001772
Bram Moolenaar875cf872018-07-08 20:49:07 +02001773" Resizing the terminal window caused an ml_get error.
1774" TODO: This does not reproduce the original problem.
1775func Test_terminal_resize()
1776 set statusline=x
1777 terminal
1778 call assert_equal(2, winnr('$'))
1779
1780 " Fill the terminal with text.
1781 if has('win32')
1782 call feedkeys("dir\<CR>", 'xt')
1783 else
1784 call feedkeys("ls\<CR>", 'xt')
1785 endif
1786 " Go to Terminal-Normal mode for a moment.
1787 call feedkeys("\<C-W>N", 'xt')
1788 " Open a new window
1789 call feedkeys("i\<C-W>n", 'xt')
1790 call assert_equal(3, winnr('$'))
1791 redraw
1792
1793 close
1794 call assert_equal(2, winnr('$'))
1795 call feedkeys("exit\<CR>", 'xt')
1796 set statusline&
1797endfunc
1798
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001799" must be nearly the last, we can't go back from GUI to terminal
1800func Test_zz1_terminal_in_gui()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001801 CheckCanRunGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001802
1803 " Ignore the "failed to create input context" error.
1804 call test_ignore_error('E285:')
1805
1806 gui -f
1807
1808 call assert_equal(1, winnr('$'))
1809 let buf = Run_shell_in_terminal({'term_finish': 'close'})
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001810 call StopShellInTerminal(buf)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001811 call term_wait(buf)
1812
1813 " closing window wipes out the terminal buffer a with finished job
1814 call WaitForAssert({-> assert_equal(1, winnr('$'))})
1815 call assert_equal("", bufname(buf))
1816
1817 unlet g:job
1818endfunc
1819
1820func Test_zz2_terminal_guioptions_bang()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001821 CheckGui
Bram Moolenaarf9c38832018-06-19 19:59:20 +02001822 set guioptions+=!
1823
1824 let filename = 'Xtestscript'
1825 if has('win32')
1826 let filename .= '.bat'
1827 let prefix = ''
1828 let contents = ['@echo off', 'exit %1']
1829 else
1830 let filename .= '.sh'
1831 let prefix = './'
1832 let contents = ['#!/bin/sh', 'exit $1']
1833 endif
1834 call writefile(contents, filename)
1835 call setfperm(filename, 'rwxrwx---')
1836
1837 " Check if v:shell_error is equal to the exit status.
1838 let exitval = 0
1839 execute printf(':!%s%s %d', prefix, filename, exitval)
1840 call assert_equal(exitval, v:shell_error)
1841
1842 let exitval = 9
1843 execute printf(':!%s%s %d', prefix, filename, exitval)
1844 call assert_equal(exitval, v:shell_error)
1845
1846 set guioptions&
1847 call delete(filename)
1848endfunc
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001849
1850func Test_terminal_hidden()
1851 if !has('unix')
1852 return
1853 endif
1854 term ++hidden cat
1855 let bnr = bufnr('$')
1856 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1857 exe 'sbuf ' . bnr
1858 call assert_equal('terminal', &buftype)
1859 call term_sendkeys(bnr, "asdf\<CR>")
1860 call WaitForAssert({-> assert_match('asdf', term_getline(bnr, 2))})
1861 call term_sendkeys(bnr, "\<C-D>")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001862 call WaitForAssert({-> assert_equal('finished', bnr->term_getstatus())})
Bram Moolenaar7da1fb52018-08-04 16:54:11 +02001863 bwipe!
1864endfunc
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001865
Bram Moolenaara4c2a242019-03-25 23:01:38 +01001866func Test_terminal_switch_mode()
1867 term
1868 let bnr = bufnr('$')
1869 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1870 call feedkeys("\<C-W>N", 'xt')
1871 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1872 call feedkeys("A", 'xt')
1873 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1874 call feedkeys("\<C-W>N", 'xt')
1875 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1876 call feedkeys("I", 'xt')
1877 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1878 call feedkeys("\<C-W>Nv", 'xt')
1879 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1880 call feedkeys("I", 'xt')
1881 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1882 call feedkeys("\<C-W>Nv", 'xt')
1883 call WaitForAssert({-> assert_equal('running,normal', term_getstatus(bnr))})
1884 call feedkeys("A", 'xt')
1885 call WaitForAssert({-> assert_equal('running', term_getstatus(bnr))})
1886 bwipe!
1887endfunc
1888
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02001889func Test_terminal_hidden_and_close()
1890 if !has('unix')
1891 return
1892 endif
1893 call assert_equal(1, winnr('$'))
1894 term ++hidden ++close ls
1895 let bnr = bufnr('$')
1896 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1897 call WaitForAssert({-> assert_false(bufexists(bnr))})
1898 call assert_equal(1, winnr('$'))
1899endfunc
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001900
1901func Test_terminal_does_not_truncate_last_newlines()
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001902 " This test does not pass through ConPTY.
1903 if has('conpty')
1904 return
1905 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001906 let contents = [
1907 \ [ 'One', '', 'X' ],
1908 \ [ 'Two', '', '' ],
1909 \ [ 'Three' ] + repeat([''], 30)
1910 \ ]
1911
1912 for c in contents
1913 call writefile(c, 'Xfile')
Bram Moolenaard3471e52018-11-12 21:42:24 +01001914 if has('win32')
1915 term cmd /c type Xfile
1916 else
1917 term cat Xfile
1918 endif
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001919 let bnr = bufnr('$')
1920 call assert_equal('terminal', getbufvar(bnr, '&buftype'))
1921 call WaitForAssert({-> assert_equal('finished', term_getstatus(bnr))})
Bram Moolenaard3471e52018-11-12 21:42:24 +01001922 sleep 100m
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001923 call assert_equal(c, getline(1, line('$')))
1924 quit
1925 endfor
1926
1927 call delete('Xfile')
1928endfunc
Bram Moolenaare751a5f2018-12-16 16:16:10 +01001929
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01001930func Test_terminal_no_job()
1931 let term = term_start('false', {'term_finish': 'close'})
1932 call WaitForAssert({-> assert_equal(v:null, term_getjob(term)) })
1933endfunc
Bram Moolenaar1e115362019-01-09 23:01:02 +01001934
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001935func Test_term_getcursor()
1936 if !has('unix')
1937 return
1938 endif
1939 let buf = Run_shell_in_terminal({})
1940
1941 " Wait for the shell to display a prompt.
1942 call WaitForAssert({-> assert_notequal('', term_getline(buf, 1))})
1943
1944 " Hide the cursor.
1945 call term_sendkeys(buf, "echo -e '\\033[?25l'\r")
1946 call WaitForAssert({-> assert_equal(0, term_getcursor(buf)[2].visible)})
1947
1948 " Show the cursor.
1949 call term_sendkeys(buf, "echo -e '\\033[?25h'\r")
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001950 call WaitForAssert({-> assert_equal(1, buf->term_getcursor()[2].visible)})
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001951
1952 " Change color of cursor.
1953 call WaitForAssert({-> assert_equal('', term_getcursor(buf)[2].color)})
1954 call term_sendkeys(buf, "echo -e '\\033]12;blue\\007'\r")
1955 call WaitForAssert({-> assert_equal('blue', term_getcursor(buf)[2].color)})
1956 call term_sendkeys(buf, "echo -e '\\033]12;green\\007'\r")
1957 call WaitForAssert({-> assert_equal('green', term_getcursor(buf)[2].color)})
1958
1959 " Make cursor a blinking block.
1960 call term_sendkeys(buf, "echo -e '\\033[1 q'\r")
1961 call WaitForAssert({-> assert_equal([1, 1],
1962 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1963
1964 " Make cursor a steady block.
1965 call term_sendkeys(buf, "echo -e '\\033[2 q'\r")
1966 call WaitForAssert({-> assert_equal([0, 1],
1967 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1968
1969 " Make cursor a blinking underline.
1970 call term_sendkeys(buf, "echo -e '\\033[3 q'\r")
1971 call WaitForAssert({-> assert_equal([1, 2],
1972 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1973
1974 " Make cursor a steady underline.
1975 call term_sendkeys(buf, "echo -e '\\033[4 q'\r")
1976 call WaitForAssert({-> assert_equal([0, 2],
1977 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1978
1979 " Make cursor a blinking vertical bar.
1980 call term_sendkeys(buf, "echo -e '\\033[5 q'\r")
1981 call WaitForAssert({-> assert_equal([1, 3],
1982 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1983
1984 " Make cursor a steady vertical bar.
1985 call term_sendkeys(buf, "echo -e '\\033[6 q'\r")
1986 call WaitForAssert({-> assert_equal([0, 3],
1987 \ [term_getcursor(buf)[2].blink, term_getcursor(buf)[2].shape])})
1988
Bram Moolenaar7a39dd72019-06-23 00:50:15 +02001989 call StopShellInTerminal(buf)
Bram Moolenaar74e3d4e2019-04-14 14:16:46 +02001990endfunc
1991
Bram Moolenaar1e115362019-01-09 23:01:02 +01001992func Test_term_gettitle()
Bram Moolenaar1e115362019-01-09 23:01:02 +01001993 " term_gettitle() returns an empty string for a non-terminal buffer
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02001994 " and for a non-existing buffer.
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02001995 call assert_equal('', bufnr('%')->term_gettitle())
Bram Moolenaar1e115362019-01-09 23:01:02 +01001996 call assert_equal('', term_gettitle(bufnr('$') + 1))
1997
Bram Moolenaar4a5711b2019-04-06 12:39:55 +02001998 if !has('title') || &title == 0 || empty(&t_ts)
1999 throw "Skipped: can't get/set title"
2000 endif
2001
Bram Moolenaar1e115362019-01-09 23:01:02 +01002002 let term = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'])
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002003 if has('autoservername')
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002004 call WaitForAssert({-> assert_match('^\[No Name\] - VIM\d\+$', term_gettitle(term)) })
2005 call term_sendkeys(term, ":e Xfoo\r")
2006 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM\d\+$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002007 else
2008 call WaitForAssert({-> assert_equal('[No Name] - VIM', term_gettitle(term)) })
Bram Moolenaar9c35d052019-04-13 20:39:15 +02002009 call term_sendkeys(term, ":e Xfoo\r")
2010 call WaitForAssert({-> assert_match('^Xfoo (.*[/\\]testdir) - VIM$', term_gettitle(term)) })
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002011 endif
Bram Moolenaar1e115362019-01-09 23:01:02 +01002012
Bram Moolenaar1e115362019-01-09 23:01:02 +01002013 call term_sendkeys(term, ":set titlestring=foo\r")
2014 call WaitForAssert({-> assert_equal('foo', term_gettitle(term)) })
2015
2016 exe term . 'bwipe!'
2017endfunc
Bram Moolenaar10772302019-01-20 18:25:54 +01002018
2019" When drawing the statusline the cursor position may not have been updated
2020" yet.
2021" 1. create a terminal, make it show 2 lines
2022" 2. 0.5 sec later: leave terminal window, execute "i"
2023" 3. 0.5 sec later: clear terminal window, now it's 1 line
2024" 4. 0.5 sec later: redraw, including statusline (used to trigger bug)
2025" 4. 0.5 sec later: should be done, clean up
2026func Test_terminal_statusline()
2027 if !has('unix')
2028 return
2029 endif
2030 set statusline=x
2031 terminal
2032 let tbuf = bufnr('')
2033 call term_sendkeys(tbuf, "clear; echo a; echo b; sleep 1; clear\n")
2034 call timer_start(500, { tid -> feedkeys("\<C-w>j", 'tx') })
2035 call timer_start(1500, { tid -> feedkeys("\<C-l>", 'tx') })
2036 au BufLeave * if &buftype == 'terminal' | silent! normal i | endif
2037
2038 sleep 2
2039 exe tbuf . 'bwipe!'
2040 au! BufLeave
2041 set statusline=
2042endfunc
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002043
2044func Test_terminal_getwinpos()
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002045 if !CanRunVimInTerminal()
Bram Moolenaar5d30ff12019-06-06 16:12:12 +02002046 throw 'Skipped: cannot run Vim in a terminal window'
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002047 endif
2048
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002049 " split, go to the bottom-right window
2050 split
2051 wincmd j
2052 set splitright
2053
2054 call writefile([
2055 \ 'echo getwinpos()',
2056 \ ], 'XTest_getwinpos')
2057 let buf = RunVimInTerminal('-S XTest_getwinpos', {'cols': 60})
2058 call term_wait(buf)
2059
2060 " Find the output of getwinpos() in the bottom line.
2061 let rows = term_getsize(buf)[0]
2062 call WaitForAssert({-> assert_match('\[\d\+, \d\+\]', term_getline(buf, rows))})
2063 let line = term_getline(buf, rows)
2064 let xpos = str2nr(substitute(line, '\[\(\d\+\), \d\+\]', '\1', ''))
2065 let ypos = str2nr(substitute(line, '\[\d\+, \(\d\+\)\]', '\1', ''))
2066
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002067 " Position must be bigger than the getwinpos() result of Vim itself.
Bram Moolenaar700dfaa2019-04-13 14:21:19 +02002068 " The calculation in the console assumes a 10 x 7 character cell.
Bram Moolenaar1b557972019-04-09 21:17:32 +02002069 " In the GUI it can be more, let's assume a 20 x 14 cell.
2070 " And then add 100 / 200 tolerance.
Bram Moolenaar616aeef2019-04-06 22:21:22 +02002071 let [xroot, yroot] = getwinpos()
Bram Moolenaar5d69fdb2019-08-31 19:13:58 +02002072 let winpos = 50->getwinpos()
2073 call assert_equal(xroot, winpos[0])
2074 call assert_equal(yroot, winpos[1])
Bram Moolenaar1b557972019-04-09 21:17:32 +02002075 let [winrow, wincol] = win_screenpos('.')
2076 let xoff = wincol * (has('gui_running') ? 14 : 7) + 100
2077 let yoff = winrow * (has('gui_running') ? 20 : 10) + 200
2078 call assert_inrange(xroot + 2, xroot + xoff, xpos)
2079 call assert_inrange(yroot + 2, yroot + yoff, ypos)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02002080
2081 call term_wait(buf)
2082 call term_sendkeys(buf, ":q\<CR>")
2083 call StopVimInTerminal(buf)
2084 call delete('XTest_getwinpos')
2085 exe buf . 'bwipe!'
2086 set splitright&
2087 only!
2088endfunc
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002089
2090func Test_terminal_altscreen()
2091 CheckUnix
2092
2093 let buf = term_start(&shell, {})
2094
2095 call term_sendkeys(buf, 'echo "\e[?1047h"' .. "\r")
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002096 call WaitForAssert({-> assert_equal(1, term_getaltscreen(buf))})
2097
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002098 call term_sendkeys(buf, 'echo "\e[?1047l"' .. "\r")
Bram Moolenaarb9c79cf2019-09-08 22:09:52 +02002099 call WaitForAssert({-> assert_equal(0, term_getaltscreen(buf))})
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02002100
2101 call term_sendkeys(buf, "exit\r")
2102 exe buf . "bwipe!"
2103endfunc