blob: 98bc75843e9b4ceae85c5d19d53d244ef1411f34 [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
Bram Moolenaar5be8dd02017-08-03 20:52:19 +020037 call assert_match("^winpty://", job_info(g:job).tty)
38 call assert_match("^winpty://", term_gettty(''))
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020039 endif
Bram Moolenaar94053a52017-08-01 21:44:33 +020040 call Stop_shell_in_terminal(buf)
41 call term_wait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020042
Bram Moolenaar94053a52017-08-01 21:44:33 +020043 " closing window wipes out the terminal buffer a with finished job
44 close
45 call assert_equal("", bufname(buf))
46
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020047 unlet g:job
48endfunc
49
50func Test_terminal_make_change()
51 let buf = Run_shell_in_terminal()
Bram Moolenaar94053a52017-08-01 21:44:33 +020052 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020053 call term_wait(buf)
54
55 setlocal modifiable
56 exe "normal Axxx\<Esc>"
57 call assert_fails(buf . 'bwipe', 'E517')
58 undo
59
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020060 exe buf . 'bwipe'
61 unlet g:job
62endfunc
63
Bram Moolenaar94053a52017-08-01 21:44:33 +020064func Test_terminal_wipe_buffer()
65 let buf = Run_shell_in_terminal()
Bram Moolenaareb44a682017-08-03 22:44:55 +020066 call assert_fails(buf . 'bwipe', 'E517')
67 exe buf . 'bwipe!'
Bram Moolenaar94053a52017-08-01 21:44:33 +020068 call WaitFor('job_status(g:job) == "dead"')
69 call assert_equal('dead', job_status(g:job))
70 call assert_equal("", bufname(buf))
71
72 unlet g:job
73endfunc
74
75func Test_terminal_hide_buffer()
76 let buf = Run_shell_in_terminal()
77 quit
78 for nr in range(1, winnr('$'))
79 call assert_notequal(winbufnr(nr), buf)
80 endfor
81 call assert_true(bufloaded(buf))
82 call assert_true(buflisted(buf))
83
84 exe 'split ' . buf . 'buf'
85 call Stop_shell_in_terminal(buf)
86 exe buf . 'bwipe'
87
88 unlet g:job
89endfunc
90
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020091func! s:Nasty_exit_cb(job, st)
92 exe g:buf . 'bwipe!'
93 let g:buf = 0
94endfunc
95
96func Test_terminal_nasty_cb()
97 let cmd = Get_cat_cmd()
98 let g:buf = term_start(cmd, {'exit_cb': function('s:Nasty_exit_cb')})
99 let g:job = term_getjob(g:buf)
100
101 call WaitFor('job_status(g:job) == "dead"')
102 call WaitFor('g:buf == 0')
103 unlet g:buf
104 unlet g:job
105 call delete('Xtext')
106endfunc
107
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200108func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200109 let l = term_scrape(a:buf, 0)
110 call assert_true(len(l) == 0)
111 let l = term_scrape(a:buf, 999)
112 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200113 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200114 call assert_true(len(l) > 0)
115 call assert_equal('1', l[0].chars)
116 call assert_equal('2', l[1].chars)
117 call assert_equal('3', l[2].chars)
118 call assert_equal('#00e000', l[0].fg)
119 if &background == 'light'
120 call assert_equal('#ffffff', l[0].bg)
121 else
122 call assert_equal('#000000', l[0].bg)
123 endif
124
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200125 let l = term_getline(a:buf, -1)
126 call assert_equal('', l)
127 let l = term_getline(a:buf, 0)
128 call assert_equal('', l)
129 let l = term_getline(a:buf, 999)
130 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200131 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200132 call assert_equal('123', l)
133endfunc
134
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200135func Get_cat_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200136 if has('win32')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200137 return 'cmd /c "cls && color 2 && echo 123"'
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200138 else
139 call writefile(["\<Esc>[32m123"], 'Xtext')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200140 return "cat Xtext"
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200141 endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200142endfunc
143
144func Test_terminal_scrape()
145 let cmd = Get_cat_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200146 let buf = term_start(cmd)
147
148 let termlist = term_list()
149 call assert_equal(1, len(termlist))
150 call assert_equal(buf, termlist[0])
151
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200152 " Nothing happens with invalid buffer number
153 call term_wait(1234)
154
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200155 call term_wait(buf)
Bram Moolenaar620d0642017-08-03 21:08:05 +0200156 if has('win32')
157 " TODO: this should not be needed
158 sleep 100m
159 endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200160 call Check_123(buf)
161
162 " Must still work after the job ended.
163 let g:job = term_getjob(buf)
164 call WaitFor('job_status(g:job) == "dead"')
165 call term_wait(buf)
166 call Check_123(buf)
167
168 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200169 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200170endfunc