Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 1 | " Tests for the terminal window. |
| 2 | |
Bram Moolenaar | ea5d6fa | 2017-08-18 21:07:11 +0200 | [diff] [blame] | 3 | if !has('terminal') |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 4 | finish |
| 5 | endif |
| 6 | |
| 7 | source shared.vim |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 8 | source screendump.vim |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 9 | |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 10 | let s:python = PythonProg() |
| 11 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 12 | " Open a terminal with a shell, assign the job to g:job and return the buffer |
| 13 | " number. |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 14 | func Run_shell_in_terminal(options) |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 15 | 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 Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 20 | |
| 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 Moolenaar | 35422f4 | 2017-08-05 16:33:56 +0200 | [diff] [blame] | 28 | let string = string({'job': term_getjob(buf)}) |
| 29 | call assert_match("{'job': 'process \\d\\+ run'}", string) |
| 30 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 31 | return buf |
| 32 | endfunc |
| 33 | |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 34 | func Test_terminal_basic() |
Bram Moolenaar | b00fdf6 | 2017-09-21 22:16:21 +0200 | [diff] [blame] | 35 | au BufWinEnter * if &buftype == 'terminal' | let b:done = 'yes' | endif |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 36 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | b00fdf6 | 2017-09-21 22:16:21 +0200 | [diff] [blame] | 37 | |
Bram Moolenaar | 7c9aec4 | 2017-08-03 13:51:25 +0200 | [diff] [blame] | 38 | if has("unix") |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 39 | call assert_match('^/dev/', job_info(g:job).tty_out) |
| 40 | call assert_match('^/dev/', term_gettty('')) |
Bram Moolenaar | 7c9aec4 | 2017-08-03 13:51:25 +0200 | [diff] [blame] | 41 | else |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 42 | call assert_match('^\\\\.\\pipe\\', job_info(g:job).tty_out) |
| 43 | call assert_match('^\\\\.\\pipe\\', term_gettty('')) |
Bram Moolenaar | 7c9aec4 | 2017-08-03 13:51:25 +0200 | [diff] [blame] | 44 | endif |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 45 | call assert_equal('t', mode()) |
Bram Moolenaar | b00fdf6 | 2017-09-21 22:16:21 +0200 | [diff] [blame] | 46 | call assert_equal('yes', b:done) |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 47 | call assert_match('%aR[^\n]*running]', execute('ls')) |
Bram Moolenaar | 0751f51 | 2018-03-29 16:37:16 +0200 | [diff] [blame] | 48 | call assert_match('%aR[^\n]*running]', execute('ls R')) |
| 49 | call assert_notmatch('%[^\n]*running]', execute('ls F')) |
| 50 | call assert_notmatch('%[^\n]*running]', execute('ls ?')) |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 51 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 52 | call Stop_shell_in_terminal(buf) |
| 53 | call term_wait(buf) |
Bram Moolenaar | 2bb7b6b | 2017-08-13 20:58:33 +0200 | [diff] [blame] | 54 | call assert_equal('n', mode()) |
| 55 | call assert_match('%aF[^\n]*finished]', execute('ls')) |
Bram Moolenaar | 0751f51 | 2018-03-29 16:37:16 +0200 | [diff] [blame] | 56 | call assert_match('%aF[^\n]*finished]', execute('ls F')) |
| 57 | call assert_notmatch('%[^\n]*finished]', execute('ls R')) |
| 58 | call assert_notmatch('%[^\n]*finished]', execute('ls ?')) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 59 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 60 | " closing window wipes out the terminal buffer a with finished job |
| 61 | close |
| 62 | call assert_equal("", bufname(buf)) |
| 63 | |
Bram Moolenaar | b00fdf6 | 2017-09-21 22:16:21 +0200 | [diff] [blame] | 64 | au! BufWinEnter |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 65 | unlet g:job |
| 66 | endfunc |
| 67 | |
| 68 | func Test_terminal_make_change() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 69 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 70 | call Stop_shell_in_terminal(buf) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 71 | call term_wait(buf) |
| 72 | |
| 73 | setlocal modifiable |
| 74 | exe "normal Axxx\<Esc>" |
| 75 | call assert_fails(buf . 'bwipe', 'E517') |
| 76 | undo |
| 77 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 78 | exe buf . 'bwipe' |
| 79 | unlet g:job |
| 80 | endfunc |
| 81 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 82 | func Test_terminal_wipe_buffer() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 83 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | eb44a68 | 2017-08-03 22:44:55 +0200 | [diff] [blame] | 84 | call assert_fails(buf . 'bwipe', 'E517') |
| 85 | exe buf . 'bwipe!' |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 86 | call WaitFor('job_status(g:job) == "dead"') |
| 87 | call assert_equal('dead', job_status(g:job)) |
| 88 | call assert_equal("", bufname(buf)) |
| 89 | |
| 90 | unlet g:job |
| 91 | endfunc |
| 92 | |
Bram Moolenaar | 8adb0d0 | 2017-09-17 19:08:02 +0200 | [diff] [blame] | 93 | func Test_terminal_split_quit() |
| 94 | let buf = Run_shell_in_terminal({}) |
| 95 | call term_wait(buf) |
| 96 | split |
| 97 | quit! |
| 98 | call term_wait(buf) |
| 99 | sleep 50m |
| 100 | call assert_equal('run', job_status(g:job)) |
| 101 | |
| 102 | quit! |
| 103 | call WaitFor('job_status(g:job) == "dead"') |
| 104 | call assert_equal('dead', job_status(g:job)) |
| 105 | |
| 106 | exe buf . 'bwipe' |
| 107 | unlet g:job |
| 108 | endfunc |
| 109 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 110 | func Test_terminal_hide_buffer() |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 111 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 112 | setlocal bufhidden=hide |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 113 | quit |
| 114 | for nr in range(1, winnr('$')) |
| 115 | call assert_notequal(winbufnr(nr), buf) |
| 116 | endfor |
| 117 | call assert_true(bufloaded(buf)) |
| 118 | call assert_true(buflisted(buf)) |
| 119 | |
| 120 | exe 'split ' . buf . 'buf' |
| 121 | call Stop_shell_in_terminal(buf) |
| 122 | exe buf . 'bwipe' |
| 123 | |
| 124 | unlet g:job |
| 125 | endfunc |
| 126 | |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 127 | func! s:Nasty_exit_cb(job, st) |
| 128 | exe g:buf . 'bwipe!' |
| 129 | let g:buf = 0 |
| 130 | endfunc |
| 131 | |
Bram Moolenaar | 9d18961 | 2017-09-09 18:11:00 +0200 | [diff] [blame] | 132 | func Get_cat_123_cmd() |
| 133 | if has('win32') |
| 134 | return 'cmd /c "cls && color 2 && echo 123"' |
| 135 | else |
| 136 | call writefile(["\<Esc>[32m123"], 'Xtext') |
| 137 | return "cat Xtext" |
| 138 | endif |
| 139 | endfunc |
| 140 | |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 141 | func Test_terminal_nasty_cb() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 142 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 143 | let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')}) |
| 144 | let g:job = term_getjob(g:buf) |
| 145 | |
| 146 | call WaitFor('job_status(g:job) == "dead"') |
| 147 | call WaitFor('g:buf == 0') |
| 148 | unlet g:buf |
| 149 | unlet g:job |
| 150 | call delete('Xtext') |
| 151 | endfunc |
| 152 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 153 | func Check_123(buf) |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 154 | let l = term_scrape(a:buf, 0) |
| 155 | call assert_true(len(l) == 0) |
| 156 | let l = term_scrape(a:buf, 999) |
| 157 | call assert_true(len(l) == 0) |
Bram Moolenaar | 9c84484 | 2017-08-01 18:41:21 +0200 | [diff] [blame] | 158 | let l = term_scrape(a:buf, 1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 159 | call assert_true(len(l) > 0) |
| 160 | call assert_equal('1', l[0].chars) |
| 161 | call assert_equal('2', l[1].chars) |
| 162 | call assert_equal('3', l[2].chars) |
| 163 | call assert_equal('#00e000', l[0].fg) |
| 164 | if &background == 'light' |
| 165 | call assert_equal('#ffffff', l[0].bg) |
| 166 | else |
| 167 | call assert_equal('#000000', l[0].bg) |
| 168 | endif |
| 169 | |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 170 | let l = term_getline(a:buf, -1) |
| 171 | call assert_equal('', l) |
| 172 | let l = term_getline(a:buf, 0) |
| 173 | call assert_equal('', l) |
| 174 | let l = term_getline(a:buf, 999) |
| 175 | call assert_equal('', l) |
Bram Moolenaar | 9c84484 | 2017-08-01 18:41:21 +0200 | [diff] [blame] | 176 | let l = term_getline(a:buf, 1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 177 | call assert_equal('123', l) |
| 178 | endfunc |
| 179 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 180 | func Test_terminal_scrape_123() |
| 181 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 182 | let buf = term_start(cmd) |
| 183 | |
| 184 | let termlist = term_list() |
| 185 | call assert_equal(1, len(termlist)) |
| 186 | call assert_equal(buf, termlist[0]) |
| 187 | |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 188 | " Nothing happens with invalid buffer number |
| 189 | call term_wait(1234) |
| 190 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 191 | call term_wait(buf) |
Bram Moolenaar | 1783337 | 2017-09-04 22:23:19 +0200 | [diff] [blame] | 192 | " On MS-Windows we first get a startup message of two lines, wait for the |
Bram Moolenaar | 1bfdc07 | 2017-09-05 20:19:42 +0200 | [diff] [blame] | 193 | " "cls" to happen, after that we have one line with three characters. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 194 | call WaitFor({-> len(term_scrape(buf, 1)) == 3}) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 195 | call Check_123(buf) |
| 196 | |
| 197 | " Must still work after the job ended. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 198 | let job = term_getjob(buf) |
| 199 | call WaitFor({-> job_status(job) == "dead"}) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 200 | call term_wait(buf) |
| 201 | call Check_123(buf) |
| 202 | |
| 203 | exe buf . 'bwipe' |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 204 | call delete('Xtext') |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 205 | endfunc |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 206 | |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 207 | func Test_terminal_scrape_multibyte() |
| 208 | if !has('multi_byte') |
| 209 | return |
| 210 | endif |
| 211 | call writefile(["léttまrs"], 'Xtext') |
| 212 | if has('win32') |
Bram Moolenaar | 3678393 | 2017-08-14 23:07:30 +0200 | [diff] [blame] | 213 | " Run cmd with UTF-8 codepage to make the type command print the expected |
| 214 | " multibyte characters. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 215 | let buf = term_start("cmd /K chcp 65001") |
| 216 | call term_sendkeys(buf, "type Xtext\<CR>") |
| 217 | call term_sendkeys(buf, "exit\<CR>") |
| 218 | let line = 4 |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 219 | else |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 220 | let buf = term_start("cat Xtext") |
| 221 | let line = 1 |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 222 | endif |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 223 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 224 | call WaitFor({-> len(term_scrape(buf, line)) >= 7 && term_scrape(buf, line)[0].chars == "l"}) |
| 225 | let l = term_scrape(buf, line) |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 226 | call assert_true(len(l) >= 7) |
| 227 | call assert_equal('l', l[0].chars) |
| 228 | call assert_equal('é', l[1].chars) |
| 229 | call assert_equal(1, l[1].width) |
| 230 | call assert_equal('t', l[2].chars) |
| 231 | call assert_equal('t', l[3].chars) |
| 232 | call assert_equal('ま', l[4].chars) |
| 233 | call assert_equal(2, l[4].width) |
| 234 | call assert_equal('r', l[5].chars) |
| 235 | call assert_equal('s', l[6].chars) |
| 236 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 237 | let job = term_getjob(buf) |
| 238 | call WaitFor({-> job_status(job) == "dead"}) |
| 239 | call term_wait(buf) |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 240 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 241 | exe buf . 'bwipe' |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 242 | call delete('Xtext') |
| 243 | endfunc |
| 244 | |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 245 | func Test_terminal_scroll() |
| 246 | call writefile(range(1, 200), 'Xtext') |
| 247 | if has('win32') |
| 248 | let cmd = 'cmd /c "type Xtext"' |
| 249 | else |
| 250 | let cmd = "cat Xtext" |
| 251 | endif |
| 252 | let buf = term_start(cmd) |
| 253 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 254 | let job = term_getjob(buf) |
| 255 | call WaitFor({-> job_status(job) == "dead"}) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 256 | call term_wait(buf) |
| 257 | if has('win32') |
| 258 | " TODO: this should not be needed |
| 259 | sleep 100m |
| 260 | endif |
| 261 | |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 262 | let scrolled = term_getscrolled(buf) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 263 | call assert_equal('1', getline(1)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 264 | call assert_equal('1', term_getline(buf, 1 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 265 | call assert_equal('49', getline(49)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 266 | call assert_equal('49', term_getline(buf, 49 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 267 | call assert_equal('200', getline(200)) |
Bram Moolenaar | 82b9ca0 | 2017-08-08 23:06:46 +0200 | [diff] [blame] | 268 | call assert_equal('200', term_getline(buf, 200 - scrolled)) |
Bram Moolenaar | f8d57a5 | 2017-08-07 20:38:42 +0200 | [diff] [blame] | 269 | |
| 270 | exe buf . 'bwipe' |
| 271 | call delete('Xtext') |
| 272 | endfunc |
| 273 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 274 | func Test_terminal_size() |
Bram Moolenaar | 33a43be | 2017-08-06 21:36:22 +0200 | [diff] [blame] | 275 | let cmd = Get_cat_123_cmd() |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 276 | |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 277 | exe 'terminal ++rows=5 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 278 | let size = term_getsize('') |
| 279 | bwipe! |
| 280 | call assert_equal(5, size[0]) |
| 281 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 282 | call term_start(cmd, {'term_rows': 6}) |
| 283 | let size = term_getsize('') |
| 284 | bwipe! |
| 285 | call assert_equal(6, size[0]) |
| 286 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 287 | vsplit |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 288 | exe 'terminal ++rows=5 ++cols=33 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 289 | let size = term_getsize('') |
| 290 | bwipe! |
| 291 | call assert_equal([5, 33], size) |
| 292 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 293 | call term_start(cmd, {'term_rows': 6, 'term_cols': 36}) |
| 294 | let size = term_getsize('') |
| 295 | bwipe! |
| 296 | call assert_equal([6, 36], size) |
| 297 | |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 298 | exe 'vertical terminal ++cols=20 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 299 | let size = term_getsize('') |
| 300 | bwipe! |
| 301 | call assert_equal(20, size[1]) |
| 302 | |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 303 | call term_start(cmd, {'vertical': 1, 'term_cols': 26}) |
| 304 | let size = term_getsize('') |
| 305 | bwipe! |
| 306 | call assert_equal(26, size[1]) |
| 307 | |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 308 | split |
Bram Moolenaar | b241208 | 2017-08-20 18:09:14 +0200 | [diff] [blame] | 309 | exe 'vertical terminal ++rows=6 ++cols=20 ' . cmd |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 310 | let size = term_getsize('') |
| 311 | bwipe! |
| 312 | call assert_equal([6, 20], size) |
Bram Moolenaar | 08d384f | 2017-08-11 21:51:23 +0200 | [diff] [blame] | 313 | |
| 314 | call term_start(cmd, {'vertical': 1, 'term_rows': 7, 'term_cols': 27}) |
| 315 | let size = term_getsize('') |
| 316 | bwipe! |
| 317 | call assert_equal([7, 27], size) |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 318 | |
| 319 | call delete('Xtext') |
Bram Moolenaar | da43b61 | 2017-08-11 22:27:50 +0200 | [diff] [blame] | 320 | endfunc |
| 321 | |
| 322 | func Test_terminal_curwin() |
| 323 | let cmd = Get_cat_123_cmd() |
| 324 | call assert_equal(1, winnr('$')) |
| 325 | |
| 326 | split dummy |
| 327 | exe 'terminal ++curwin ' . cmd |
| 328 | call assert_equal(2, winnr('$')) |
| 329 | bwipe! |
| 330 | |
| 331 | split dummy |
| 332 | call term_start(cmd, {'curwin': 1}) |
| 333 | call assert_equal(2, winnr('$')) |
| 334 | bwipe! |
| 335 | |
| 336 | split dummy |
| 337 | call setline(1, 'change') |
| 338 | call assert_fails('terminal ++curwin ' . cmd, 'E37:') |
| 339 | call assert_equal(2, winnr('$')) |
| 340 | exe 'terminal! ++curwin ' . cmd |
| 341 | call assert_equal(2, winnr('$')) |
| 342 | bwipe! |
| 343 | |
| 344 | split dummy |
| 345 | call setline(1, 'change') |
| 346 | call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:') |
| 347 | call assert_equal(2, winnr('$')) |
| 348 | bwipe! |
| 349 | |
| 350 | split dummy |
| 351 | bwipe! |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 352 | call delete('Xtext') |
Bram Moolenaar | cfcc022 | 2017-08-05 17:13:48 +0200 | [diff] [blame] | 353 | endfunc |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 354 | |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 355 | func s:get_sleep_cmd() |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 356 | if s:python != '' |
| 357 | let cmd = s:python . " test_short_sleep.py" |
| 358 | let waittime = 500 |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 359 | else |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 360 | echo 'This will take five seconds...' |
| 361 | let waittime = 2000 |
| 362 | if has('win32') |
| 363 | let cmd = $windir . '\system32\timeout.exe 1' |
| 364 | else |
| 365 | let cmd = 'sleep 1' |
| 366 | endif |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 367 | endif |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 368 | return [cmd, waittime] |
| 369 | endfunc |
| 370 | |
| 371 | func Test_terminal_finish_open_close() |
| 372 | call assert_equal(1, winnr('$')) |
| 373 | |
| 374 | let [cmd, waittime] = s:get_sleep_cmd() |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 375 | |
Bram Moolenaar | 1dd9833 | 2018-03-16 22:54:53 +0100 | [diff] [blame] | 376 | " shell terminal closes automatically |
| 377 | terminal |
| 378 | let buf = bufnr('%') |
| 379 | call assert_equal(2, winnr('$')) |
| 380 | " Wait for the shell to display a prompt |
| 381 | call WaitFor({-> term_getline(buf, 1) != ""}) |
| 382 | call Stop_shell_in_terminal(buf) |
| 383 | call WaitFor("winnr('$') == 1", waittime) |
| 384 | |
| 385 | " shell terminal that does not close automatically |
| 386 | terminal ++noclose |
| 387 | let buf = bufnr('%') |
| 388 | call assert_equal(2, winnr('$')) |
| 389 | " Wait for the shell to display a prompt |
| 390 | call WaitFor({-> term_getline(buf, 1) != ""}) |
| 391 | call Stop_shell_in_terminal(buf) |
| 392 | call assert_equal(2, winnr('$')) |
| 393 | quit |
| 394 | call assert_equal(1, winnr('$')) |
| 395 | |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 396 | exe 'terminal ++close ' . cmd |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 397 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 398 | wincmd p |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 399 | call WaitFor("winnr('$') == 1", waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 400 | |
| 401 | call term_start(cmd, {'term_finish': 'close'}) |
| 402 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 403 | wincmd p |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 404 | call WaitFor("winnr('$') == 1", waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 405 | call assert_equal(1, winnr('$')) |
| 406 | |
| 407 | exe 'terminal ++open ' . cmd |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 408 | close! |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 409 | call WaitFor("winnr('$') == 2", waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 410 | call assert_equal(2, winnr('$')) |
| 411 | bwipe |
| 412 | |
| 413 | call term_start(cmd, {'term_finish': 'open'}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 414 | close! |
Bram Moolenaar | b81bc77 | 2017-08-11 22:45:01 +0200 | [diff] [blame] | 415 | call WaitFor("winnr('$') == 2", waittime) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 416 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 417 | bwipe |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 418 | |
Bram Moolenaar | 8cad930 | 2017-08-12 14:32:32 +0200 | [diff] [blame] | 419 | exe 'terminal ++hidden ++open ' . cmd |
| 420 | call assert_equal(1, winnr('$')) |
| 421 | call WaitFor("winnr('$') == 2", waittime) |
| 422 | call assert_equal(2, winnr('$')) |
| 423 | bwipe |
| 424 | |
| 425 | call term_start(cmd, {'term_finish': 'open', 'hidden': 1}) |
| 426 | call assert_equal(1, winnr('$')) |
| 427 | call WaitFor("winnr('$') == 2", waittime) |
| 428 | call assert_equal(2, winnr('$')) |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 429 | bwipe |
Bram Moolenaar | 37c4583 | 2017-08-12 16:01:04 +0200 | [diff] [blame] | 430 | |
| 431 | call assert_fails("call term_start(cmd, {'term_opencmd': 'open'})", 'E475:') |
| 432 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split %x'})", 'E475:') |
| 433 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split %d and %s'})", 'E475:') |
| 434 | call assert_fails("call term_start(cmd, {'term_opencmd': 'split % and %d'})", 'E475:') |
| 435 | |
| 436 | call term_start(cmd, {'term_finish': 'open', 'term_opencmd': '4split | buffer %d'}) |
Bram Moolenaar | 97a80e4 | 2017-08-30 13:31:49 +0200 | [diff] [blame] | 437 | close! |
Bram Moolenaar | 37c4583 | 2017-08-12 16:01:04 +0200 | [diff] [blame] | 438 | call WaitFor("winnr('$') == 2", waittime) |
| 439 | call assert_equal(2, winnr('$')) |
| 440 | call assert_equal(4, winheight(0)) |
| 441 | bwipe |
Bram Moolenaar | dd693ce | 2017-08-10 23:15:19 +0200 | [diff] [blame] | 442 | endfunc |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 443 | |
| 444 | func Test_terminal_cwd() |
Bram Moolenaar | e9f6fd2 | 2017-09-10 14:25:49 +0200 | [diff] [blame] | 445 | if !executable('pwd') |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 446 | return |
| 447 | endif |
| 448 | call mkdir('Xdir') |
| 449 | let buf = term_start('pwd', {'cwd': 'Xdir'}) |
Bram Moolenaar | e9f6fd2 | 2017-09-10 14:25:49 +0200 | [diff] [blame] | 450 | call WaitFor('"Xdir" == fnamemodify(getline(1), ":t")') |
| 451 | call assert_equal('Xdir', fnamemodify(getline(1), ":t")) |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 452 | |
| 453 | exe buf . 'bwipe' |
| 454 | call delete('Xdir', 'rf') |
| 455 | endfunc |
| 456 | |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 457 | func Test_terminal_servername() |
| 458 | if !has('clientserver') |
| 459 | return |
| 460 | endif |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 461 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 462 | " Wait for the shell to display a prompt |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 463 | call WaitFor({-> term_getline(buf, 1) != ""}) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 464 | if has('win32') |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 465 | call term_sendkeys(buf, "echo %VIM_SERVERNAME%\r") |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 466 | else |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 467 | call term_sendkeys(buf, "echo $VIM_SERVERNAME\r") |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 468 | endif |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 469 | call term_wait(buf) |
| 470 | call Stop_shell_in_terminal(buf) |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 471 | call WaitFor('getline(2) == v:servername') |
| 472 | call assert_equal(v:servername, getline(2)) |
| 473 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 474 | exe buf . 'bwipe' |
| 475 | unlet buf |
Bram Moolenaar | 52dbb5e | 2017-11-21 18:11:27 +0100 | [diff] [blame] | 476 | endfunc |
| 477 | |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 478 | func Test_terminal_env() |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 479 | let buf = Run_shell_in_terminal({'env': {'TESTENV': 'correct'}}) |
Bram Moolenaar | 51c2368 | 2017-08-14 21:45:00 +0200 | [diff] [blame] | 480 | " Wait for the shell to display a prompt |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 481 | call WaitFor({-> term_getline(buf, 1) != ""}) |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 482 | if has('win32') |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 483 | call term_sendkeys(buf, "echo %TESTENV%\r") |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 484 | else |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 485 | call term_sendkeys(buf, "echo $TESTENV\r") |
Bram Moolenaar | ba6febd | 2017-10-30 21:56:23 +0100 | [diff] [blame] | 486 | endif |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 487 | call term_wait(buf) |
| 488 | call Stop_shell_in_terminal(buf) |
Bram Moolenaar | 51c2368 | 2017-08-14 21:45:00 +0200 | [diff] [blame] | 489 | call WaitFor('getline(2) == "correct"') |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 490 | call assert_equal('correct', getline(2)) |
| 491 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 492 | exe buf . 'bwipe' |
Bram Moolenaar | 05aafed | 2017-08-11 19:12:11 +0200 | [diff] [blame] | 493 | endfunc |
Bram Moolenaar | 679653e | 2017-08-13 14:13:19 +0200 | [diff] [blame] | 494 | |
| 495 | " must be last, we can't go back from GUI to terminal |
| 496 | func Test_zz_terminal_in_gui() |
Bram Moolenaar | 9f0139a | 2017-08-13 20:26:20 +0200 | [diff] [blame] | 497 | if !CanRunGui() |
Bram Moolenaar | 679653e | 2017-08-13 14:13:19 +0200 | [diff] [blame] | 498 | return |
| 499 | endif |
Bram Moolenaar | 97f65fa | 2017-08-29 20:42:07 +0200 | [diff] [blame] | 500 | |
| 501 | " Ignore the "failed to create input context" error. |
| 502 | call test_ignore_error('E285:') |
| 503 | |
Bram Moolenaar | 679653e | 2017-08-13 14:13:19 +0200 | [diff] [blame] | 504 | gui -f |
| 505 | |
| 506 | call assert_equal(1, winnr('$')) |
| 507 | let buf = Run_shell_in_terminal({'term_finish': 'close'}) |
| 508 | call Stop_shell_in_terminal(buf) |
| 509 | call term_wait(buf) |
| 510 | |
| 511 | " closing window wipes out the terminal buffer a with finished job |
| 512 | call WaitFor("winnr('$') == 1") |
| 513 | call assert_equal(1, winnr('$')) |
| 514 | call assert_equal("", bufname(buf)) |
| 515 | |
| 516 | unlet g:job |
| 517 | endfunc |
Bram Moolenaar | dcaa613 | 2017-08-13 17:13:09 +0200 | [diff] [blame] | 518 | |
| 519 | func Test_terminal_list_args() |
| 520 | let buf = term_start([&shell, &shellcmdflag, 'echo "123"']) |
| 521 | call assert_fails(buf . 'bwipe', 'E517') |
| 522 | exe buf . 'bwipe!' |
| 523 | call assert_equal("", bufname(buf)) |
| 524 | endfunction |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 525 | |
| 526 | func Test_terminal_noblock() |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 527 | let buf = term_start(&shell) |
Bram Moolenaar | d8d85bf | 2017-09-03 18:08:00 +0200 | [diff] [blame] | 528 | if has('mac') |
| 529 | " The shell or something else has a problem dealing with more than 1000 |
| 530 | " characters at the same time. |
| 531 | let len = 1000 |
| 532 | else |
| 533 | let len = 5000 |
| 534 | endif |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 535 | |
| 536 | for c in ['a','b','c','d','e','f','g','h','i','j','k'] |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 537 | call term_sendkeys(buf, 'echo ' . repeat(c, len) . "\<cr>") |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 538 | endfor |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 539 | call term_sendkeys(buf, "echo done\<cr>") |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 540 | |
| 541 | " On MS-Windows there is an extra empty line below "done". Find "done" in |
| 542 | " the last-but-one or the last-but-two line. |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 543 | let lnum = term_getsize(buf)[0] - 1 |
Bram Moolenaar | 2181014 | 2018-02-02 18:30:36 +0100 | [diff] [blame] | 544 | call WaitFor({-> term_getline(buf, lnum) =~ "done" || term_getline(buf, lnum - 1) =~ "done"}, 10000) |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 545 | let line = term_getline(buf, lnum) |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 546 | if line !~ 'done' |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 547 | let line = term_getline(buf, lnum - 1) |
Bram Moolenaar | eef0531 | 2017-08-20 20:21:23 +0200 | [diff] [blame] | 548 | endif |
| 549 | call assert_match('done', line) |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 550 | |
Bram Moolenaar | ab8b1c1 | 2017-11-04 19:24:31 +0100 | [diff] [blame] | 551 | let g:job = term_getjob(buf) |
| 552 | call Stop_shell_in_terminal(buf) |
| 553 | call term_wait(buf) |
Bram Moolenaar | d21f8b5 | 2017-08-19 15:40:01 +0200 | [diff] [blame] | 554 | unlet g:job |
Bram Moolenaar | 97bd5e6 | 2017-08-18 20:50:30 +0200 | [diff] [blame] | 555 | bwipe |
| 556 | endfunc |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 557 | |
| 558 | func Test_terminal_write_stdin() |
Bram Moolenaar | 3346cc4 | 2017-09-02 14:54:21 +0200 | [diff] [blame] | 559 | if !executable('wc') |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 560 | throw 'skipped: wc command not available' |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 561 | endif |
| 562 | new |
| 563 | call setline(1, ['one', 'two', 'three']) |
| 564 | %term wc |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 565 | call WaitFor('getline("$") =~ "3"') |
Bram Moolenaar | 3346cc4 | 2017-09-02 14:54:21 +0200 | [diff] [blame] | 566 | let nrs = split(getline('$')) |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 567 | call assert_equal(['3', '3', '14'], nrs) |
| 568 | bwipe |
| 569 | |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 570 | new |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 571 | call setline(1, ['one', 'two', 'three', 'four']) |
| 572 | 2,3term wc |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 573 | call WaitFor('getline("$") =~ "2"') |
Bram Moolenaar | 3346cc4 | 2017-09-02 14:54:21 +0200 | [diff] [blame] | 574 | let nrs = split(getline('$')) |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 575 | call assert_equal(['2', '2', '10'], nrs) |
| 576 | bwipe |
| 577 | |
Bram Moolenaar | dada6d2 | 2017-09-02 17:18:35 +0200 | [diff] [blame] | 578 | if executable('python') |
| 579 | new |
| 580 | call setline(1, ['print("hello")']) |
| 581 | 1term ++eof=exit() python |
| 582 | " MS-Windows echoes the input, Unix doesn't. |
| 583 | call WaitFor('getline("$") =~ "exit" || getline(1) =~ "hello"') |
| 584 | if getline(1) =~ 'hello' |
| 585 | call assert_equal('hello', getline(1)) |
| 586 | else |
| 587 | call assert_equal('hello', getline(line('$') - 1)) |
| 588 | endif |
| 589 | bwipe |
| 590 | |
| 591 | if has('win32') |
| 592 | new |
| 593 | call setline(1, ['print("hello")']) |
| 594 | 1term ++eof=<C-Z> python |
| 595 | call WaitFor('getline("$") =~ "Z"') |
| 596 | call assert_equal('hello', getline(line('$') - 1)) |
| 597 | bwipe |
| 598 | endif |
| 599 | endif |
| 600 | |
Bram Moolenaar | 37819ed | 2017-08-20 19:33:47 +0200 | [diff] [blame] | 601 | bwipe! |
| 602 | endfunc |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 603 | |
| 604 | func Test_terminal_no_cmd() |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 605 | " Todo: make this work in the GUI |
| 606 | if !has('gui_running') |
| 607 | return |
| 608 | endif |
| 609 | let buf = term_start('NONE', {}) |
| 610 | call assert_notequal(0, buf) |
| 611 | |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 612 | let pty = job_info(term_getjob(buf))['tty_out'] |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 613 | call assert_notequal('', pty) |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 614 | if has('win32') |
Bram Moolenaar | e738a1a | 2017-09-16 17:42:41 +0200 | [diff] [blame] | 615 | silent exe '!start cmd /c "echo look here > ' . pty . '"' |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 616 | else |
| 617 | call system('echo "look here" > ' . pty) |
| 618 | endif |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 619 | call WaitFor({-> term_getline(buf, 1) =~ "look here"}) |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 620 | |
Bram Moolenaar | e738a1a | 2017-09-16 17:42:41 +0200 | [diff] [blame] | 621 | call assert_match('look here', term_getline(buf, 1)) |
Bram Moolenaar | 13ebb03 | 2017-08-26 22:02:51 +0200 | [diff] [blame] | 622 | bwipe! |
| 623 | endfunc |
Bram Moolenaar | 9d654a8 | 2017-09-03 19:52:17 +0200 | [diff] [blame] | 624 | |
| 625 | func Test_terminal_special_chars() |
| 626 | " this file name only works on Unix |
| 627 | if !has('unix') |
| 628 | return |
| 629 | endif |
| 630 | call mkdir('Xdir with spaces') |
| 631 | call writefile(['x'], 'Xdir with spaces/quoted"file') |
| 632 | term ls Xdir\ with\ spaces/quoted\"file |
| 633 | call WaitFor('term_getline("", 1) =~ "quoted"') |
| 634 | call assert_match('quoted"file', term_getline('', 1)) |
| 635 | call term_wait('') |
| 636 | |
| 637 | call delete('Xdir with spaces', 'rf') |
| 638 | bwipe |
| 639 | endfunc |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 640 | |
| 641 | func Test_terminal_wrong_options() |
| 642 | call assert_fails('call term_start(&shell, { |
| 643 | \ "in_io": "file", |
| 644 | \ "in_name": "xxx", |
| 645 | \ "out_io": "file", |
| 646 | \ "out_name": "xxx", |
| 647 | \ "err_io": "file", |
| 648 | \ "err_name": "xxx" |
| 649 | \ })', 'E474:') |
| 650 | call assert_fails('call term_start(&shell, { |
| 651 | \ "out_buf": bufnr("%") |
| 652 | \ })', 'E474:') |
| 653 | call assert_fails('call term_start(&shell, { |
| 654 | \ "err_buf": bufnr("%") |
| 655 | \ })', 'E474:') |
| 656 | endfunc |
| 657 | |
| 658 | func Test_terminal_redir_file() |
Bram Moolenaar | 1783337 | 2017-09-04 22:23:19 +0200 | [diff] [blame] | 659 | " TODO: this should work on MS-Window |
| 660 | if has('unix') |
| 661 | let cmd = Get_cat_123_cmd() |
| 662 | let buf = term_start(cmd, {'out_io': 'file', 'out_name': 'Xfile'}) |
| 663 | call term_wait(buf) |
| 664 | call WaitFor('len(readfile("Xfile")) > 0') |
| 665 | call assert_match('123', readfile('Xfile')[0]) |
Bram Moolenaar | e9f6fd2 | 2017-09-10 14:25:49 +0200 | [diff] [blame] | 666 | let g:job = term_getjob(buf) |
| 667 | call WaitFor('job_status(g:job) == "dead"') |
Bram Moolenaar | 1783337 | 2017-09-04 22:23:19 +0200 | [diff] [blame] | 668 | call delete('Xfile') |
Bram Moolenaar | 2dc9d26 | 2017-09-08 14:39:30 +0200 | [diff] [blame] | 669 | bwipe |
Bram Moolenaar | 1783337 | 2017-09-04 22:23:19 +0200 | [diff] [blame] | 670 | endif |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 671 | |
| 672 | if has('unix') |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 673 | call writefile(['one line'], 'Xfile') |
| 674 | let buf = term_start('cat', {'in_io': 'file', 'in_name': 'Xfile'}) |
| 675 | call term_wait(buf) |
| 676 | call WaitFor('term_getline(' . buf . ', 1) == "one line"') |
| 677 | call assert_equal('one line', term_getline(buf, 1)) |
Bram Moolenaar | 8b53b79 | 2017-09-05 20:29:25 +0200 | [diff] [blame] | 678 | let g:job = term_getjob(buf) |
| 679 | call WaitFor('job_status(g:job) == "dead"') |
Bram Moolenaar | e88fc7a | 2017-09-03 20:59:40 +0200 | [diff] [blame] | 680 | bwipe |
| 681 | call delete('Xfile') |
| 682 | endif |
| 683 | endfunc |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 684 | |
| 685 | func TerminalTmap(remap) |
| 686 | let buf = Run_shell_in_terminal({}) |
| 687 | call assert_equal('t', mode()) |
| 688 | |
| 689 | if a:remap |
| 690 | tmap 123 456 |
| 691 | else |
| 692 | tnoremap 123 456 |
| 693 | endif |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 694 | " don't use abcde, it's an existing command |
| 695 | tmap 456 abxde |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 696 | call assert_equal('456', maparg('123', 't')) |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 697 | call assert_equal('abxde', maparg('456', 't')) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 698 | call feedkeys("123", 'tx') |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 699 | call WaitFor({-> term_getline(buf, term_getcursor(buf)[0]) =~ 'abxde\|456'}) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 700 | let lnum = term_getcursor(buf)[0] |
| 701 | if a:remap |
Bram Moolenaar | 461fe50 | 2017-12-05 12:30:03 +0100 | [diff] [blame] | 702 | call assert_match('abxde', term_getline(buf, lnum)) |
Bram Moolenaar | 69fbc9e | 2017-09-14 20:37:57 +0200 | [diff] [blame] | 703 | else |
| 704 | call assert_match('456', term_getline(buf, lnum)) |
| 705 | endif |
| 706 | |
| 707 | call term_sendkeys(buf, "\r") |
| 708 | call Stop_shell_in_terminal(buf) |
| 709 | call term_wait(buf) |
| 710 | |
| 711 | tunmap 123 |
| 712 | tunmap 456 |
| 713 | call assert_equal('', maparg('123', 't')) |
| 714 | close |
| 715 | unlet g:job |
| 716 | endfunc |
| 717 | |
| 718 | func Test_terminal_tmap() |
| 719 | call TerminalTmap(1) |
| 720 | call TerminalTmap(0) |
| 721 | endfunc |
Bram Moolenaar | 059db5c | 2017-10-15 22:42:23 +0200 | [diff] [blame] | 722 | |
| 723 | func Test_terminal_wall() |
| 724 | let buf = Run_shell_in_terminal({}) |
| 725 | wall |
| 726 | call Stop_shell_in_terminal(buf) |
| 727 | call term_wait(buf) |
| 728 | exe buf . 'bwipe' |
| 729 | unlet g:job |
| 730 | endfunc |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 731 | |
Bram Moolenaar | 7a76092 | 2018-02-19 23:10:02 +0100 | [diff] [blame] | 732 | func Test_terminal_wqall() |
| 733 | let buf = Run_shell_in_terminal({}) |
| 734 | call assert_fails('wqall', 'E948') |
| 735 | call Stop_shell_in_terminal(buf) |
| 736 | call term_wait(buf) |
| 737 | exe buf . 'bwipe' |
| 738 | unlet g:job |
| 739 | endfunc |
| 740 | |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 741 | func Test_terminal_composing_unicode() |
| 742 | let save_enc = &encoding |
| 743 | set encoding=utf-8 |
| 744 | |
| 745 | if has('win32') |
| 746 | let cmd = "cmd /K chcp 65001" |
| 747 | let lnum = [3, 6, 9] |
| 748 | else |
| 749 | let cmd = &shell |
| 750 | let lnum = [1, 3, 5] |
| 751 | endif |
| 752 | |
| 753 | enew |
| 754 | let buf = term_start(cmd, {'curwin': bufnr('')}) |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 755 | let g:job = term_getjob(buf) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 756 | call term_wait(buf, 50) |
| 757 | |
| 758 | " ascii + composing |
| 759 | let txt = "a\u0308bc" |
| 760 | call term_sendkeys(buf, "echo " . txt . "\r") |
| 761 | call term_wait(buf, 50) |
| 762 | call assert_match("echo " . txt, term_getline(buf, lnum[0])) |
| 763 | call assert_equal(txt, term_getline(buf, lnum[0] + 1)) |
| 764 | let l = term_scrape(buf, lnum[0] + 1) |
| 765 | call assert_equal("a\u0308", l[0].chars) |
| 766 | call assert_equal("b", l[1].chars) |
| 767 | call assert_equal("c", l[2].chars) |
| 768 | |
| 769 | " multibyte + composing |
| 770 | let txt = "\u304b\u3099\u304e\u304f\u3099\u3052\u3053\u3099" |
| 771 | call term_sendkeys(buf, "echo " . txt . "\r") |
| 772 | call term_wait(buf, 50) |
| 773 | call assert_match("echo " . txt, term_getline(buf, lnum[1])) |
| 774 | call assert_equal(txt, term_getline(buf, lnum[1] + 1)) |
| 775 | let l = term_scrape(buf, lnum[1] + 1) |
| 776 | call assert_equal("\u304b\u3099", l[0].chars) |
| 777 | call assert_equal("\u304e", l[1].chars) |
| 778 | call assert_equal("\u304f\u3099", l[2].chars) |
| 779 | call assert_equal("\u3052", l[3].chars) |
| 780 | call assert_equal("\u3053\u3099", l[4].chars) |
| 781 | |
| 782 | " \u00a0 + composing |
| 783 | let txt = "abc\u00a0\u0308" |
| 784 | call term_sendkeys(buf, "echo " . txt . "\r") |
| 785 | call term_wait(buf, 50) |
| 786 | call assert_match("echo " . txt, term_getline(buf, lnum[2])) |
| 787 | call assert_equal(txt, term_getline(buf, lnum[2] + 1)) |
| 788 | let l = term_scrape(buf, lnum[2] + 1) |
| 789 | call assert_equal("\u00a0\u0308", l[3].chars) |
| 790 | |
| 791 | call term_sendkeys(buf, "exit\r") |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 792 | call WaitFor('job_status(g:job) == "dead"') |
| 793 | call assert_equal('dead', job_status(g:job)) |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 794 | bwipe! |
Bram Moolenaar | 3e1c617 | 2017-11-02 16:58:00 +0100 | [diff] [blame] | 795 | unlet g:job |
Bram Moolenaar | 6daeef1 | 2017-10-15 22:56:49 +0200 | [diff] [blame] | 796 | let &encoding = save_enc |
| 797 | endfunc |
Bram Moolenaar | ff54679 | 2017-11-21 14:47:57 +0100 | [diff] [blame] | 798 | |
| 799 | func Test_terminal_aucmd_on_close() |
| 800 | fun Nop() |
| 801 | let s:called = 1 |
| 802 | endfun |
| 803 | |
| 804 | aug repro |
| 805 | au! |
| 806 | au BufWinLeave * call Nop() |
| 807 | aug END |
| 808 | |
| 809 | let [cmd, waittime] = s:get_sleep_cmd() |
| 810 | |
| 811 | call assert_equal(1, winnr('$')) |
| 812 | new |
| 813 | call setline(1, ['one', 'two']) |
| 814 | exe 'term ++close ' . cmd |
| 815 | wincmd p |
| 816 | call WaitFor("winnr('$') == 2", waittime) |
| 817 | call assert_equal(1, s:called) |
| 818 | bwipe! |
| 819 | |
| 820 | unlet s:called |
| 821 | au! repro |
| 822 | delfunc Nop |
| 823 | endfunc |
Bram Moolenaar | ede35bb | 2018-01-26 20:05:18 +0100 | [diff] [blame] | 824 | |
| 825 | func Test_terminal_term_start_empty_command() |
| 826 | let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})" |
| 827 | call assert_fails(cmd, 'E474') |
| 828 | let cmd = "call term_start('', {'curwin' : 1, 'term_finish' : 'close'})" |
| 829 | call assert_fails(cmd, 'E474') |
| 830 | let cmd = "call term_start({}, {'curwin' : 1, 'term_finish' : 'close'})" |
| 831 | call assert_fails(cmd, 'E474') |
| 832 | let cmd = "call term_start(0, {'curwin' : 1, 'term_finish' : 'close'})" |
| 833 | call assert_fails(cmd, 'E474') |
| 834 | endfunc |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 835 | |
| 836 | func Test_terminal_response_to_control_sequence() |
| 837 | if !has('unix') |
| 838 | return |
| 839 | endif |
| 840 | |
| 841 | let buf = Run_shell_in_terminal({}) |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 842 | call WaitFor({-> term_getline(buf, 1) != ''}) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 843 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 844 | call term_sendkeys(buf, "cat\<CR>") |
| 845 | call WaitFor({-> term_getline(buf, 1) =~ 'cat'}) |
Bram Moolenaar | d4a282f | 2018-02-02 18:22:31 +0100 | [diff] [blame] | 846 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 847 | " Request the cursor position. |
| 848 | call term_sendkeys(buf, "\x1b[6n\<CR>") |
Bram Moolenaar | d4a282f | 2018-02-02 18:22:31 +0100 | [diff] [blame] | 849 | |
| 850 | " Wait for output from tty to display, below an empty line. |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 851 | call WaitFor({-> term_getline(buf, 4) =~ '3;1R'}) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 852 | |
Bram Moolenaar | 086eb87 | 2018-03-25 21:24:12 +0200 | [diff] [blame] | 853 | " End "cat" gently. |
| 854 | call term_sendkeys(buf, "\<CR>\<C-D>") |
| 855 | |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 856 | call Stop_shell_in_terminal(buf) |
Bram Moolenaar | b50773c | 2018-01-30 22:31:19 +0100 | [diff] [blame] | 857 | exe buf . 'bwipe' |
| 858 | unlet g:job |
| 859 | endfunc |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 860 | |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 861 | " Run Vim, start a terminal in that Vim with the kill argument, |
| 862 | " :qall works. |
| 863 | func Run_terminal_qall_kill(line1, line2) |
| 864 | " 1. Open a terminal window and wait for the prompt to appear |
| 865 | " 2. set kill using term_setkill() |
| 866 | " 3. make Vim exit, it will kill the shell |
| 867 | let after = [ |
| 868 | \ a:line1, |
| 869 | \ 'let buf = bufnr("%")', |
| 870 | \ 'while term_getline(buf, 1) =~ "^\\s*$"', |
| 871 | \ ' sleep 10m', |
| 872 | \ 'endwhile', |
| 873 | \ a:line2, |
| 874 | \ 'au VimLeavePre * call writefile(["done"], "Xdone")', |
| 875 | \ 'qall', |
| 876 | \ ] |
| 877 | if !RunVim([], after, '') |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 878 | return |
| 879 | endif |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 880 | call assert_equal("done", readfile("Xdone")[0]) |
| 881 | call delete("Xdone") |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 882 | endfunc |
| 883 | |
| 884 | " Run Vim in a terminal, then start a terminal in that Vim with a kill |
| 885 | " argument, check that :qall works. |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 886 | func Test_terminal_qall_kill_arg() |
| 887 | call Run_terminal_qall_kill('term ++kill=kill', '') |
| 888 | endfunc |
| 889 | |
| 890 | " Run Vim, start a terminal in that Vim, set the kill argument with |
| 891 | " term_setkill(), check that :qall works. |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 892 | func Test_terminal_qall_kill_func() |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 893 | call Run_terminal_qall_kill('term', 'call term_setkill(buf, "kill")') |
| 894 | endfunc |
| 895 | |
| 896 | " Run Vim, start a terminal in that Vim without the kill argument, |
| 897 | " check that :qall does not exit, :qall! does. |
| 898 | func Test_terminal_qall_exit() |
| 899 | let after = [ |
| 900 | \ 'term', |
| 901 | \ 'let buf = bufnr("%")', |
| 902 | \ 'while term_getline(buf, 1) =~ "^\\s*$"', |
| 903 | \ ' sleep 10m', |
| 904 | \ 'endwhile', |
| 905 | \ 'set nomore', |
| 906 | \ 'au VimLeavePre * call writefile(["too early"], "Xdone")', |
| 907 | \ 'qall', |
| 908 | \ 'au! VimLeavePre * exe buf . "bwipe!" | call writefile(["done"], "Xdone")', |
| 909 | \ 'cquit', |
| 910 | \ ] |
| 911 | if !RunVim([], after, '') |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 912 | return |
| 913 | endif |
Bram Moolenaar | 3e8d385 | 2018-03-20 17:43:01 +0100 | [diff] [blame] | 914 | call assert_equal("done", readfile("Xdone")[0]) |
| 915 | call delete("Xdone") |
Bram Moolenaar | 25cdd9c | 2018-03-10 20:28:12 +0100 | [diff] [blame] | 916 | endfunc |
Bram Moolenaar | 435acdb | 2018-03-10 20:51:25 +0100 | [diff] [blame] | 917 | |
| 918 | " Run Vim in a terminal, then start a terminal in that Vim without a kill |
| 919 | " argument, check that :confirm qall works. |
| 920 | func Test_terminal_qall_prompt() |
| 921 | if !CanRunVimInTerminal() |
| 922 | return |
| 923 | endif |
| 924 | let buf = RunVimInTerminal('', {}) |
| 925 | |
| 926 | " Open a terminal window and wait for the prompt to appear |
| 927 | call term_sendkeys(buf, ":term\<CR>") |
| 928 | call WaitFor({-> term_getline(buf, 10) =~ '\[running]'}) |
| 929 | call WaitFor({-> term_getline(buf, 1) !~ '^\s*$'}) |
| 930 | |
| 931 | " make Vim exit, it will prompt to kill the shell |
| 932 | call term_sendkeys(buf, "\<C-W>:confirm qall\<CR>") |
| 933 | call WaitFor({-> term_getline(buf, 20) =~ 'ancel:'}) |
| 934 | call term_sendkeys(buf, "y") |
| 935 | call WaitFor({-> term_getstatus(buf) == "finished"}) |
| 936 | |
| 937 | " close the terminal window where Vim was running |
| 938 | quit |
| 939 | endfunc |
Bram Moolenaar | b852c3e | 2018-03-11 16:55:36 +0100 | [diff] [blame] | 940 | |
Bram Moolenaar | 012eb66 | 2018-03-13 17:55:27 +0100 | [diff] [blame] | 941 | func Test_terminal_open_autocmd() |
Bram Moolenaar | b852c3e | 2018-03-11 16:55:36 +0100 | [diff] [blame] | 942 | augroup repro |
| 943 | au! |
| 944 | au TerminalOpen * let s:called += 1 |
| 945 | augroup END |
| 946 | |
| 947 | let s:called = 0 |
| 948 | |
| 949 | " Open a terminal window with :terminal |
| 950 | terminal |
| 951 | call assert_equal(1, s:called) |
| 952 | bwipe! |
| 953 | |
| 954 | " Open a terminal window with term_start() |
| 955 | call term_start(&shell) |
| 956 | call assert_equal(2, s:called) |
| 957 | bwipe! |
| 958 | |
| 959 | " Open a hidden terminal buffer with :terminal |
| 960 | terminal ++hidden |
| 961 | call assert_equal(3, s:called) |
| 962 | for buf in term_list() |
| 963 | exe buf . "bwipe!" |
| 964 | endfor |
| 965 | |
| 966 | " Open a hidden terminal buffer with term_start() |
| 967 | let buf = term_start(&shell, {'hidden': 1}) |
| 968 | call assert_equal(4, s:called) |
| 969 | exe buf . "bwipe!" |
| 970 | |
| 971 | unlet s:called |
| 972 | au! repro |
| 973 | endfunction |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 974 | |
| 975 | func Check_dump01(off) |
| 976 | call assert_equal('one two three four five', trim(getline(a:off + 1))) |
| 977 | call assert_equal('~ Select Word', trim(getline(a:off + 7))) |
| 978 | call assert_equal(':popup PopUp :', trim(getline(a:off + 20))) |
| 979 | endfunc |
| 980 | |
Bram Moolenaar | f06b0b6 | 2018-03-29 17:22:24 +0200 | [diff] [blame^] | 981 | func Test_terminal_dumpwrite_composing() |
| 982 | if !CanRunVimInTerminal() |
| 983 | return |
| 984 | endif |
| 985 | let save_enc = &encoding |
| 986 | set encoding=utf-8 |
| 987 | call assert_equal(1, winnr('$')) |
| 988 | |
| 989 | let text = " a\u0300 e\u0302 o\u0308" |
| 990 | call writefile([text], 'Xcomposing') |
| 991 | let buf = RunVimInTerminal('Xcomposing', {}) |
| 992 | call WaitFor({-> term_getline(buf, 1) =~ text}) |
| 993 | call term_dumpwrite(buf, 'Xdump') |
| 994 | let dumpline = readfile('Xdump')[0] |
| 995 | call assert_match('|à| |ê| |ö', dumpline) |
| 996 | |
| 997 | call StopVimInTerminal(buf) |
| 998 | call delete('Xcomposing') |
| 999 | call delete('Xdump') |
| 1000 | let &encoding = save_enc |
| 1001 | endfunc |
| 1002 | |
Bram Moolenaar | 45d2a64 | 2018-03-24 14:30:32 +0100 | [diff] [blame] | 1003 | " just testing basic functionality. |
| 1004 | func Test_terminal_dumpload() |
| 1005 | call assert_equal(1, winnr('$')) |
| 1006 | call term_dumpload('dumps/Test_popup_command_01.dump') |
| 1007 | call assert_equal(2, winnr('$')) |
| 1008 | call assert_equal(20, line('$')) |
| 1009 | call Check_dump01(0) |
| 1010 | quit |
| 1011 | endfunc |
| 1012 | |
| 1013 | func Test_terminal_dumpdiff() |
| 1014 | call assert_equal(1, winnr('$')) |
| 1015 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump') |
| 1016 | call assert_equal(2, winnr('$')) |
| 1017 | call assert_equal(62, line('$')) |
| 1018 | call Check_dump01(0) |
| 1019 | call Check_dump01(42) |
| 1020 | call assert_equal(' bbbbbbbbbbbbbbbbbb ', getline(26)[0:29]) |
| 1021 | quit |
| 1022 | endfunc |
Bram Moolenaar | 897e63c | 2018-03-24 17:16:33 +0100 | [diff] [blame] | 1023 | |
| 1024 | func Test_terminal_dumpdiff_options() |
| 1025 | set laststatus=0 |
| 1026 | call assert_equal(1, winnr('$')) |
| 1027 | let height = winheight(0) |
| 1028 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 1, 'term_cols': 33}) |
| 1029 | call assert_equal(2, winnr('$')) |
| 1030 | call assert_equal(height, winheight(winnr())) |
| 1031 | call assert_equal(33, winwidth(winnr())) |
| 1032 | call assert_equal('dump diff dumps/Test_popup_command_01.dump', bufname('%')) |
| 1033 | quit |
| 1034 | |
| 1035 | call assert_equal(1, winnr('$')) |
| 1036 | let width = winwidth(0) |
| 1037 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'vertical': 0, 'term_rows': 13, 'term_name': 'something else'}) |
| 1038 | call assert_equal(2, winnr('$')) |
| 1039 | call assert_equal(width, winwidth(winnr())) |
| 1040 | call assert_equal(13, winheight(winnr())) |
| 1041 | call assert_equal('something else', bufname('%')) |
| 1042 | quit |
| 1043 | |
| 1044 | call assert_equal(1, winnr('$')) |
| 1045 | call term_dumpdiff('dumps/Test_popup_command_01.dump', 'dumps/Test_popup_command_02.dump', {'curwin': 1}) |
| 1046 | call assert_equal(1, winnr('$')) |
| 1047 | bwipe |
| 1048 | |
| 1049 | set laststatus& |
| 1050 | endfunc |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1051 | |
| 1052 | func Test_terminal_api_drop_newwin() |
| 1053 | if !CanRunVimInTerminal() |
| 1054 | return |
| 1055 | endif |
| 1056 | call assert_equal(1, winnr('$')) |
| 1057 | |
| 1058 | " Use the title termcap entries to output the escape sequence. |
| 1059 | call writefile([ |
Bram Moolenaar | cf67a50 | 2018-03-25 20:31:32 +0200 | [diff] [blame] | 1060 | \ 'set title', |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1061 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1062 | \ 'let &titlestring = ''["drop","Xtextfile"]''', |
| 1063 | \ 'redraw', |
| 1064 | \ "set t_ts=", |
| 1065 | \ ], 'Xscript') |
| 1066 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 1067 | call WaitFor({-> bufnr('Xtextfile') > 0}) |
| 1068 | call assert_equal('Xtextfile', expand('%:t')) |
| 1069 | call assert_true(winnr('$') >= 3) |
| 1070 | |
| 1071 | call StopVimInTerminal(buf) |
| 1072 | call delete('Xscript') |
| 1073 | bwipe Xtextfile |
| 1074 | endfunc |
| 1075 | |
| 1076 | func Test_terminal_api_drop_oldwin() |
| 1077 | if !CanRunVimInTerminal() |
| 1078 | return |
| 1079 | endif |
| 1080 | let firstwinid = win_getid() |
| 1081 | split Xtextfile |
| 1082 | let textfile_winid = win_getid() |
| 1083 | call assert_equal(2, winnr('$')) |
| 1084 | call win_gotoid(firstwinid) |
| 1085 | |
| 1086 | " Use the title termcap entries to output the escape sequence. |
| 1087 | call writefile([ |
Bram Moolenaar | cf67a50 | 2018-03-25 20:31:32 +0200 | [diff] [blame] | 1088 | \ 'set title', |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1089 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1090 | \ 'let &titlestring = ''["drop","Xtextfile"]''', |
| 1091 | \ 'redraw', |
| 1092 | \ "set t_ts=", |
| 1093 | \ ], 'Xscript') |
Bram Moolenaar | 15a1c3f | 2018-03-25 18:56:25 +0200 | [diff] [blame] | 1094 | let buf = RunVimInTerminal('-S Xscript', {'rows': 10}) |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1095 | call WaitFor({-> expand('%:t') =='Xtextfile'}) |
| 1096 | call assert_equal(textfile_winid, win_getid()) |
| 1097 | |
| 1098 | call StopVimInTerminal(buf) |
| 1099 | call delete('Xscript') |
| 1100 | bwipe Xtextfile |
| 1101 | endfunc |
| 1102 | |
Bram Moolenaar | 2a77d21 | 2018-03-26 21:38:52 +0200 | [diff] [blame] | 1103 | func Tapi_TryThis(bufnum, arg) |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1104 | let g:called_bufnum = a:bufnum |
| 1105 | let g:called_arg = a:arg |
| 1106 | endfunc |
| 1107 | |
Bram Moolenaar | 2a77d21 | 2018-03-26 21:38:52 +0200 | [diff] [blame] | 1108 | func WriteApiCall(funcname) |
| 1109 | " Use the title termcap entries to output the escape sequence. |
| 1110 | call writefile([ |
| 1111 | \ 'set title', |
| 1112 | \ 'exe "set t_ts=\<Esc>]51; t_fs=\x07"', |
| 1113 | \ 'let &titlestring = ''["call","' . a:funcname . '",["hello",123]]''', |
| 1114 | \ 'redraw', |
| 1115 | \ "set t_ts=", |
| 1116 | \ ], 'Xscript') |
| 1117 | endfunc |
| 1118 | |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1119 | func Test_terminal_api_call() |
| 1120 | if !CanRunVimInTerminal() |
| 1121 | return |
| 1122 | endif |
Bram Moolenaar | 2de50f8 | 2018-03-25 19:09:56 +0200 | [diff] [blame] | 1123 | |
Bram Moolenaar | 2a77d21 | 2018-03-26 21:38:52 +0200 | [diff] [blame] | 1124 | call WriteApiCall('Tapi_TryThis') |
Bram Moolenaar | 8fbaeb1 | 2018-03-25 18:20:17 +0200 | [diff] [blame] | 1125 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 1126 | call WaitFor({-> exists('g:called_bufnum')}) |
| 1127 | call assert_equal(buf, g:called_bufnum) |
| 1128 | call assert_equal(['hello', 123], g:called_arg) |
| 1129 | |
| 1130 | call StopVimInTerminal(buf) |
| 1131 | call delete('Xscript') |
| 1132 | unlet g:called_bufnum |
| 1133 | unlet g:called_arg |
| 1134 | endfunc |
Bram Moolenaar | 2a77d21 | 2018-03-26 21:38:52 +0200 | [diff] [blame] | 1135 | |
| 1136 | func Test_terminal_api_call_fails() |
| 1137 | if !CanRunVimInTerminal() |
| 1138 | return |
| 1139 | endif |
| 1140 | |
| 1141 | call WriteApiCall('TryThis') |
| 1142 | call ch_logfile('Xlog', 'w') |
| 1143 | let buf = RunVimInTerminal('-S Xscript', {}) |
| 1144 | call WaitFor({-> string(readfile('Xlog')) =~ 'Invalid function name: TryThis'}) |
| 1145 | |
| 1146 | call StopVimInTerminal(buf) |
| 1147 | call delete('Xscript') |
| 1148 | call ch_logfile('', '') |
| 1149 | call delete('Xlog') |
| 1150 | endfunc |