blob: 987b1ee13b0b5ed68e7758a6a5729aa1a59c40e2 [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()
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
72endfunc
73
74func 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
88endfunc
89
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020090func! s:Nasty_exit_cb(job, st)
91 exe g:buf . 'bwipe!'
92 let g:buf = 0
93endfunc
94
95func 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')
105endfunc
106
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200107func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200108 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 Moolenaar9c844842017-08-01 18:41:21 +0200112 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200113 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 Moolenaar5c838a32017-08-02 22:10:34 +0200124 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 Moolenaar9c844842017-08-01 18:41:21 +0200130 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200131 call assert_equal('123', l)
132endfunc
133
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200134func Get_cat_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200135 if has('win32')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200136 return 'cmd /c "cls && color 2 && echo 123"'
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200137 else
138 call writefile(["\<Esc>[32m123"], 'Xtext')
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200139 return "cat Xtext"
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200140 endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200141endfunc
142
143func Test_terminal_scrape()
144 let cmd = Get_cat_cmd()
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200145 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 Moolenaarf144a3f2017-07-30 18:02:12 +0200151 " Nothing happens with invalid buffer number
152 call term_wait(1234)
153
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200154 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 Moolenaarf144a3f2017-07-30 18:02:12 +0200164 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200165endfunc