Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 1 | " Tests for the terminal window. |
| 2 | |
| 3 | if !exists('*term_start') |
| 4 | finish |
| 5 | endif |
| 6 | |
| 7 | source shared.vim |
| 8 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 9 | " Open a terminal with a shell, assign the job to g:job and return the buffer |
| 10 | " number. |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 11 | func Run_shell_in_terminal() |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 12 | let buf = term_start(&shell) |
| 13 | |
| 14 | let termlist = term_list() |
| 15 | call assert_equal(1, len(termlist)) |
| 16 | call assert_equal(buf, termlist[0]) |
| 17 | |
| 18 | let g:job = term_getjob(buf) |
| 19 | call assert_equal(v:t_job, type(g:job)) |
| 20 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 21 | return buf |
| 22 | endfunc |
| 23 | |
| 24 | " Stops the shell started by Run_shell_in_terminal(). |
| 25 | func Stop_shell_in_terminal(buf) |
| 26 | call term_sendkeys(a:buf, "exit\r") |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 27 | call WaitFor('job_status(g:job) == "dead"') |
| 28 | call assert_equal('dead', job_status(g:job)) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 29 | endfunc |
| 30 | |
| 31 | func Test_terminal_basic() |
| 32 | let buf = Run_shell_in_terminal() |
Bram Moolenaar | 7c9aec4 | 2017-08-03 13:51:25 +0200 | [diff] [blame] | 33 | if has("unix") |
| 34 | call assert_match("^/dev/", job_info(g:job).tty) |
| 35 | call assert_match("^/dev/", term_gettty('')) |
| 36 | else |
Bram Moolenaar | 5be8dd0 | 2017-08-03 20:52:19 +0200 | [diff] [blame] | 37 | call assert_match("^winpty://", job_info(g:job).tty) |
| 38 | call assert_match("^winpty://", term_gettty('')) |
Bram Moolenaar | 7c9aec4 | 2017-08-03 13:51:25 +0200 | [diff] [blame] | 39 | endif |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 40 | call Stop_shell_in_terminal(buf) |
| 41 | call term_wait(buf) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 42 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 43 | " closing window wipes out the terminal buffer a with finished job |
| 44 | close |
| 45 | call assert_equal("", bufname(buf)) |
| 46 | |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 47 | unlet g:job |
| 48 | endfunc |
| 49 | |
| 50 | func Test_terminal_make_change() |
| 51 | let buf = Run_shell_in_terminal() |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 52 | call Stop_shell_in_terminal(buf) |
Bram Moolenaar | 20e6cd0 | 2017-08-01 20:25:22 +0200 | [diff] [blame] | 53 | call term_wait(buf) |
| 54 | |
| 55 | setlocal modifiable |
| 56 | exe "normal Axxx\<Esc>" |
| 57 | call assert_fails(buf . 'bwipe', 'E517') |
| 58 | undo |
| 59 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 60 | exe buf . 'bwipe' |
| 61 | unlet g:job |
| 62 | endfunc |
| 63 | |
Bram Moolenaar | 94053a5 | 2017-08-01 21:44:33 +0200 | [diff] [blame] | 64 | func Test_terminal_wipe_buffer() |
| 65 | let buf = Run_shell_in_terminal() |
| 66 | exe buf . 'bwipe' |
| 67 | call WaitFor('job_status(g:job) == "dead"') |
| 68 | call assert_equal('dead', job_status(g:job)) |
| 69 | call assert_equal("", bufname(buf)) |
| 70 | |
| 71 | unlet g:job |
| 72 | endfunc |
| 73 | |
| 74 | func Test_terminal_hide_buffer() |
| 75 | let buf = Run_shell_in_terminal() |
| 76 | quit |
| 77 | for nr in range(1, winnr('$')) |
| 78 | call assert_notequal(winbufnr(nr), buf) |
| 79 | endfor |
| 80 | call assert_true(bufloaded(buf)) |
| 81 | call assert_true(buflisted(buf)) |
| 82 | |
| 83 | exe 'split ' . buf . 'buf' |
| 84 | call Stop_shell_in_terminal(buf) |
| 85 | exe buf . 'bwipe' |
| 86 | |
| 87 | unlet g:job |
| 88 | endfunc |
| 89 | |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 90 | func! s:Nasty_exit_cb(job, st) |
| 91 | exe g:buf . 'bwipe!' |
| 92 | let g:buf = 0 |
| 93 | endfunc |
| 94 | |
| 95 | func Test_terminal_nasty_cb() |
| 96 | let cmd = Get_cat_cmd() |
| 97 | let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')}) |
| 98 | let g:job = term_getjob(g:buf) |
| 99 | |
| 100 | call WaitFor('job_status(g:job) == "dead"') |
| 101 | call WaitFor('g:buf == 0') |
| 102 | unlet g:buf |
| 103 | unlet g:job |
| 104 | call delete('Xtext') |
| 105 | endfunc |
| 106 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 107 | func Check_123(buf) |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 108 | let l = term_scrape(a:buf, 0) |
| 109 | call assert_true(len(l) == 0) |
| 110 | let l = term_scrape(a:buf, 999) |
| 111 | call assert_true(len(l) == 0) |
Bram Moolenaar | 9c84484 | 2017-08-01 18:41:21 +0200 | [diff] [blame] | 112 | let l = term_scrape(a:buf, 1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 113 | call assert_true(len(l) > 0) |
| 114 | call assert_equal('1', l[0].chars) |
| 115 | call assert_equal('2', l[1].chars) |
| 116 | call assert_equal('3', l[2].chars) |
| 117 | call assert_equal('#00e000', l[0].fg) |
| 118 | if &background == 'light' |
| 119 | call assert_equal('#ffffff', l[0].bg) |
| 120 | else |
| 121 | call assert_equal('#000000', l[0].bg) |
| 122 | endif |
| 123 | |
Bram Moolenaar | 5c838a3 | 2017-08-02 22:10:34 +0200 | [diff] [blame] | 124 | let l = term_getline(a:buf, -1) |
| 125 | call assert_equal('', l) |
| 126 | let l = term_getline(a:buf, 0) |
| 127 | call assert_equal('', l) |
| 128 | let l = term_getline(a:buf, 999) |
| 129 | call assert_equal('', l) |
Bram Moolenaar | 9c84484 | 2017-08-01 18:41:21 +0200 | [diff] [blame] | 130 | let l = term_getline(a:buf, 1) |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 131 | call assert_equal('123', l) |
| 132 | endfunc |
| 133 | |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 134 | func Get_cat_cmd() |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 135 | if has('win32') |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 136 | return 'cmd /c "cls && color 2 && echo 123"' |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 137 | else |
| 138 | call writefile(["\<Esc>[32m123"], 'Xtext') |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 139 | return "cat Xtext" |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 140 | endif |
Bram Moolenaar | 3c3a80d | 2017-08-03 17:06:45 +0200 | [diff] [blame] | 141 | endfunc |
| 142 | |
| 143 | func Test_terminal_scrape() |
| 144 | let cmd = Get_cat_cmd() |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 145 | let buf = term_start(cmd) |
| 146 | |
| 147 | let termlist = term_list() |
| 148 | call assert_equal(1, len(termlist)) |
| 149 | call assert_equal(buf, termlist[0]) |
| 150 | |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 151 | " Nothing happens with invalid buffer number |
| 152 | call term_wait(1234) |
| 153 | |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 154 | call term_wait(buf) |
| 155 | call Check_123(buf) |
| 156 | |
| 157 | " Must still work after the job ended. |
| 158 | let g:job = term_getjob(buf) |
| 159 | call WaitFor('job_status(g:job) == "dead"') |
| 160 | call term_wait(buf) |
| 161 | call Check_123(buf) |
| 162 | |
| 163 | exe buf . 'bwipe' |
Bram Moolenaar | f144a3f | 2017-07-30 18:02:12 +0200 | [diff] [blame] | 164 | call delete('Xtext') |
Bram Moolenaar | c6df10e | 2017-07-29 20:15:08 +0200 | [diff] [blame] | 165 | endfunc |