blob: 2220a164a4baae26c6dc270c3f2b1499ae3536d9 [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 Moolenaar35422f42017-08-05 16:33:56 +020021 let string = string({'job': term_getjob(buf)})
22 call assert_match("{'job': 'process \\d\\+ run'}", string)
23
Bram Moolenaar94053a52017-08-01 21:44:33 +020024 return buf
25endfunc
26
27" Stops the shell started by Run_shell_in_terminal().
28func Stop_shell_in_terminal(buf)
29 call term_sendkeys(a:buf, "exit\r")
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020030 call WaitFor('job_status(g:job) == "dead"')
31 call assert_equal('dead', job_status(g:job))
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020032endfunc
33
34func Test_terminal_basic()
35 let buf = Run_shell_in_terminal()
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020036 if has("unix")
37 call assert_match("^/dev/", job_info(g:job).tty)
38 call assert_match("^/dev/", term_gettty(''))
39 else
Bram Moolenaar5be8dd02017-08-03 20:52:19 +020040 call assert_match("^winpty://", job_info(g:job).tty)
41 call assert_match("^winpty://", term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020042 endif
Bram Moolenaar94053a52017-08-01 21:44:33 +020043 call Stop_shell_in_terminal(buf)
44 call term_wait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020045
Bram Moolenaar94053a52017-08-01 21:44:33 +020046 " closing window wipes out the terminal buffer a with finished job
47 close
48 call assert_equal("", bufname(buf))
49
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020050 unlet g:job
51endfunc
52
53func Test_terminal_make_change()
54 let buf = Run_shell_in_terminal()
Bram Moolenaar94053a52017-08-01 21:44:33 +020055 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020056 call term_wait(buf)
57
58 setlocal modifiable
59 exe "normal Axxx\<Esc>"
60 call assert_fails(buf . 'bwipe', 'E517')
61 undo
62
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020063 exe buf . 'bwipe'
64 unlet g:job
65endfunc
66
Bram Moolenaar94053a52017-08-01 21:44:33 +020067func Test_terminal_wipe_buffer()
68 let buf = Run_shell_in_terminal()
Bram Moolenaareb44a682017-08-03 22:44:55 +020069 call assert_fails(buf . 'bwipe', 'E517')
70 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020071 call WaitFor('job_status(g:job) == "dead"')
72 call assert_equal('dead', job_status(g:job))
73 call assert_equal("", bufname(buf))
74
75 unlet g:job
76endfunc
77
78func Test_terminal_hide_buffer()
79 let buf = Run_shell_in_terminal()
80 quit
81 for nr in range(1, winnr('$'))
82 call assert_notequal(winbufnr(nr), buf)
83 endfor
84 call assert_true(bufloaded(buf))
85 call assert_true(buflisted(buf))
86
87 exe 'split ' . buf . 'buf'
88 call Stop_shell_in_terminal(buf)
89 exe buf . 'bwipe'
90
91 unlet g:job
92endfunc
93
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020094func! s:Nasty_exit_cb(job, st)
95 exe g:buf . 'bwipe!'
96 let g:buf = 0
97endfunc
98
99func Test_terminal_nasty_cb()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200100 let cmd = Get_cat_123_cmd()
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200101 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
102 let g:job = term_getjob(g:buf)
103
104 call WaitFor('job_status(g:job) == "dead"')
105 call WaitFor('g:buf == 0')
106 unlet g:buf
107 unlet g:job
108 call delete('Xtext')
109endfunc
110
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200111func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200112 let l = term_scrape(a:buf, 0)
113 call assert_true(len(l) == 0)
114 let l = term_scrape(a:buf, 999)
115 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200116 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200117 call assert_true(len(l) > 0)
118 call assert_equal('1', l[0].chars)
119 call assert_equal('2', l[1].chars)
120 call assert_equal('3', l[2].chars)
121 call assert_equal('#00e000', l[0].fg)
122 if &background == 'light'
123 call assert_equal('#ffffff', l[0].bg)
124 else
125 call assert_equal('#000000', l[0].bg)
126 endif
127
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200128 let l = term_getline(a:buf, -1)
129 call assert_equal('', l)
130 let l = term_getline(a:buf, 0)
131 call assert_equal('', l)
132 let l = term_getline(a:buf, 999)
133 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200134 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200135 call assert_equal('123', l)
136endfunc
137
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200138func Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200139 if has('win32')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200140 return 'cmd /c "cls && color 2 && echo 123"'
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200141 else
142 call writefile(["\<Esc>[32m123"], 'Xtext')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200143 return "cat Xtext"
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200144 endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200145endfunc
146
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200147func Test_terminal_scrape_123()
148 let cmd = Get_cat_123_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200149 let buf = term_start(cmd)
150
151 let termlist = term_list()
152 call assert_equal(1, len(termlist))
153 call assert_equal(buf, termlist[0])
154
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200155 " Nothing happens with invalid buffer number
156 call term_wait(1234)
157
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200158 call term_wait(buf)
Bram Moolenaar620d0642017-08-03 21:08:05 +0200159 if has('win32')
160 " TODO: this should not be needed
161 sleep 100m
162 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200163 call Check_123(buf)
164
165 " Must still work after the job ended.
166 let g:job = term_getjob(buf)
167 call WaitFor('job_status(g:job) == "dead"')
168 call term_wait(buf)
169 call Check_123(buf)
170
171 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200172 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200173endfunc
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200174
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200175func Test_terminal_scrape_multibyte()
176 if !has('multi_byte')
177 return
178 endif
179 call writefile(["léttまrs"], 'Xtext')
180 if has('win32')
181 let cmd = 'cmd /c "type Xtext"'
182 else
183 let cmd = "cat Xtext"
184 endif
185 let buf = term_start(cmd)
186
187 call term_wait(buf)
188 if has('win32')
189 " TODO: this should not be needed
190 sleep 100m
191 endif
192
193 let l = term_scrape(buf, 1)
194 call assert_true(len(l) >= 7)
195 call assert_equal('l', l[0].chars)
196 call assert_equal('é', l[1].chars)
197 call assert_equal(1, l[1].width)
198 call assert_equal('t', l[2].chars)
199 call assert_equal('t', l[3].chars)
200 call assert_equal('ま', l[4].chars)
201 call assert_equal(2, l[4].width)
202 call assert_equal('r', l[5].chars)
203 call assert_equal('s', l[6].chars)
204
205 let g:job = term_getjob(buf)
206 call WaitFor('job_status(g:job) == "dead"')
207 call term_wait(buf)
208
209 exe buf . 'bwipe'
210 call delete('Xtext')
211endfunc
212
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200213func Test_terminal_scroll()
214 call writefile(range(1, 200), 'Xtext')
215 if has('win32')
216 let cmd = 'cmd /c "type Xtext"'
217 else
218 let cmd = "cat Xtext"
219 endif
220 let buf = term_start(cmd)
221
222 let g:job = term_getjob(buf)
223 call WaitFor('job_status(g:job) == "dead"')
224 call term_wait(buf)
225 if has('win32')
226 " TODO: this should not be needed
227 sleep 100m
228 endif
229
230 call assert_equal('1', getline(1))
231 call assert_equal('49', getline(49))
232 call assert_equal('200', getline(200))
233
234 exe buf . 'bwipe'
235 call delete('Xtext')
236endfunc
237
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200238func Test_terminal_size()
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200239 let cmd = Get_cat_123_cmd()
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200240
241 exe '5terminal ' . cmd
242 let size = term_getsize('')
243 bwipe!
244 call assert_equal(5, size[0])
245
246 vsplit
247 exe '5,33terminal ' . cmd
248 let size = term_getsize('')
249 bwipe!
250 call assert_equal([5, 33], size)
251
252 exe 'vertical 20terminal ' . cmd
253 let size = term_getsize('')
254 bwipe!
255 call assert_equal(20, size[1])
256
257 split
258 exe 'vertical 6,20terminal ' . cmd
259 let size = term_getsize('')
260 bwipe!
261 call assert_equal([6, 20], size)
262endfunc