blob: 90bc58b8545b78f4c125fe786a30fece6756c214 [file] [log] [blame]
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001" Tests for the terminal window.
2
3if !exists('*term_start')
4 finish
5endif
6
7source shared.vim
8
Bram Moolenaar94053a52017-08-01 21:44:33 +02009" Open a terminal with a shell, assign the job to g:job and return the buffer
10" number.
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020011func Run_shell_in_terminal()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020012 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 Moolenaar94053a52017-08-01 21:44:33 +020021 return buf
22endfunc
23
24" Stops the shell started by Run_shell_in_terminal().
25func Stop_shell_in_terminal(buf)
26 call term_sendkeys(a:buf, "exit\r")
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020027 call WaitFor('job_status(g:job) == "dead"')
28 call assert_equal('dead', job_status(g:job))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020029endfunc
30
31func Test_terminal_basic()
32 let buf = Run_shell_in_terminal()
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020033 if has("unix")
34 call assert_match("^/dev/", job_info(g:job).tty)
35 call assert_match("^/dev/", term_gettty(''))
36 else
37 call assert_equal("", job_info(g:job).tty)
38 endif
Bram Moolenaar94053a52017-08-01 21:44:33 +020039 call Stop_shell_in_terminal(buf)
40 call term_wait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020041
Bram Moolenaar94053a52017-08-01 21:44:33 +020042 " closing window wipes out the terminal buffer a with finished job
43 close
44 call assert_equal("", bufname(buf))
45
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020046 unlet g:job
47endfunc
48
49func Test_terminal_make_change()
50 let buf = Run_shell_in_terminal()
Bram Moolenaar94053a52017-08-01 21:44:33 +020051 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020052 call term_wait(buf)
53
54 setlocal modifiable
55 exe "normal Axxx\<Esc>"
56 call assert_fails(buf . 'bwipe', 'E517')
57 undo
58
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020059 exe buf . 'bwipe'
60 unlet g:job
61endfunc
62
Bram Moolenaar94053a52017-08-01 21:44:33 +020063func Test_terminal_wipe_buffer()
64 let buf = Run_shell_in_terminal()
65 exe buf . 'bwipe'
66 call WaitFor('job_status(g:job) == "dead"')
67 call assert_equal('dead', job_status(g:job))
68 call assert_equal("", bufname(buf))
69
70 unlet g:job
71endfunc
72
73func Test_terminal_hide_buffer()
74 let buf = Run_shell_in_terminal()
75 quit
76 for nr in range(1, winnr('$'))
77 call assert_notequal(winbufnr(nr), buf)
78 endfor
79 call assert_true(bufloaded(buf))
80 call assert_true(buflisted(buf))
81
82 exe 'split ' . buf . 'buf'
83 call Stop_shell_in_terminal(buf)
84 exe buf . 'bwipe'
85
86 unlet g:job
87endfunc
88
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020089func! s:Nasty_exit_cb(job, st)
90 exe g:buf . 'bwipe!'
91 let g:buf = 0
92endfunc
93
94func Test_terminal_nasty_cb()
95 let cmd = Get_cat_cmd()
96 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
97 let g:job = term_getjob(g:buf)
98
99 call WaitFor('job_status(g:job) == "dead"')
100 call WaitFor('g:buf == 0')
101 unlet g:buf
102 unlet g:job
103 call delete('Xtext')
104endfunc
105
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200106func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200107 let l = term_scrape(a:buf, 0)
108 call assert_true(len(l) == 0)
109 let l = term_scrape(a:buf, 999)
110 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200111 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200112 call assert_true(len(l) > 0)
113 call assert_equal('1', l[0].chars)
114 call assert_equal('2', l[1].chars)
115 call assert_equal('3', l[2].chars)
116 call assert_equal('#00e000', l[0].fg)
117 if &background == 'light'
118 call assert_equal('#ffffff', l[0].bg)
119 else
120 call assert_equal('#000000', l[0].bg)
121 endif
122
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200123 let l = term_getline(a:buf, -1)
124 call assert_equal('', l)
125 let l = term_getline(a:buf, 0)
126 call assert_equal('', l)
127 let l = term_getline(a:buf, 999)
128 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200129 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200130 call assert_equal('123', l)
131endfunc
132
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200133func Get_cat_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200134 if has('win32')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200135 return 'cmd /c "cls && color 2 && echo 123"'
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200136 else
137 call writefile(["\<Esc>[32m123"], 'Xtext')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200138 return "cat Xtext"
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200139 endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200140endfunc
141
142func Test_terminal_scrape()
143 let cmd = Get_cat_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200144 let buf = term_start(cmd)
145
146 let termlist = term_list()
147 call assert_equal(1, len(termlist))
148 call assert_equal(buf, termlist[0])
149
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200150 " Nothing happens with invalid buffer number
151 call term_wait(1234)
152
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200153 call term_wait(buf)
154 call Check_123(buf)
155
156 " Must still work after the job ended.
157 let g:job = term_getjob(buf)
158 call WaitFor('job_status(g:job) == "dead"')
159 call term_wait(buf)
160 call Check_123(buf)
161
162 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200163 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200164endfunc