blob: cc5afc3940f298b21bcce5bfac8c106e03e6044a [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 Moolenaarc6df10e2017-07-29 20:15:08 +020089func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +020090 let l = term_scrape(a:buf, 0)
91 call assert_true(len(l) == 0)
92 let l = term_scrape(a:buf, 999)
93 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +020094 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020095 call assert_true(len(l) > 0)
96 call assert_equal('1', l[0].chars)
97 call assert_equal('2', l[1].chars)
98 call assert_equal('3', l[2].chars)
99 call assert_equal('#00e000', l[0].fg)
100 if &background == 'light'
101 call assert_equal('#ffffff', l[0].bg)
102 else
103 call assert_equal('#000000', l[0].bg)
104 endif
105
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200106 let l = term_getline(a:buf, -1)
107 call assert_equal('', l)
108 let l = term_getline(a:buf, 0)
109 call assert_equal('', l)
110 let l = term_getline(a:buf, 999)
111 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200112 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200113 call assert_equal('123', l)
114endfunc
115
116func Test_terminal_scrape()
117 if has('win32')
118 let cmd = 'cmd /c "cls && color 2 && echo 123"'
119 else
120 call writefile(["\<Esc>[32m123"], 'Xtext')
121 let cmd = "cat Xtext"
122 endif
123 let buf = term_start(cmd)
124
125 let termlist = term_list()
126 call assert_equal(1, len(termlist))
127 call assert_equal(buf, termlist[0])
128
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200129 " Nothing happens with invalid buffer number
130 call term_wait(1234)
131
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200132 call term_wait(buf)
133 call Check_123(buf)
134
135 " Must still work after the job ended.
136 let g:job = term_getjob(buf)
137 call WaitFor('job_status(g:job) == "dead"')
138 call term_wait(buf)
139 call Check_123(buf)
140
141 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200142 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200143endfunc