blob: 48ee1c4eca7a93c1d420df8e6a8f4873931a01f2 [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 Moolenaar94053a52017-08-01 21:44:33 +020033 call Stop_shell_in_terminal(buf)
34 call term_wait(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020035
Bram Moolenaar94053a52017-08-01 21:44:33 +020036 " closing window wipes out the terminal buffer a with finished job
37 close
38 call assert_equal("", bufname(buf))
39
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020040 unlet g:job
41endfunc
42
43func Test_terminal_make_change()
44 let buf = Run_shell_in_terminal()
Bram Moolenaar94053a52017-08-01 21:44:33 +020045 call Stop_shell_in_terminal(buf)
Bram Moolenaar20e6cd02017-08-01 20:25:22 +020046 call term_wait(buf)
47
48 setlocal modifiable
49 exe "normal Axxx\<Esc>"
50 call assert_fails(buf . 'bwipe', 'E517')
51 undo
52
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020053 exe buf . 'bwipe'
54 unlet g:job
55endfunc
56
Bram Moolenaar94053a52017-08-01 21:44:33 +020057func Test_terminal_wipe_buffer()
58 let buf = Run_shell_in_terminal()
59 exe buf . 'bwipe'
60 call WaitFor('job_status(g:job) == "dead"')
61 call assert_equal('dead', job_status(g:job))
62 call assert_equal("", bufname(buf))
63
64 unlet g:job
65endfunc
66
67func Test_terminal_hide_buffer()
68 let buf = Run_shell_in_terminal()
69 quit
70 for nr in range(1, winnr('$'))
71 call assert_notequal(winbufnr(nr), buf)
72 endfor
73 call assert_true(bufloaded(buf))
74 call assert_true(buflisted(buf))
75
76 exe 'split ' . buf . 'buf'
77 call Stop_shell_in_terminal(buf)
78 exe buf . 'bwipe'
79
80 unlet g:job
81endfunc
82
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020083func Check_123(buf)
Bram Moolenaar5c838a32017-08-02 22:10:34 +020084 let l = term_scrape(a:buf, 0)
85 call assert_true(len(l) == 0)
86 let l = term_scrape(a:buf, 999)
87 call assert_true(len(l) == 0)
Bram Moolenaar9c844842017-08-01 18:41:21 +020088 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020089 call assert_true(len(l) > 0)
90 call assert_equal('1', l[0].chars)
91 call assert_equal('2', l[1].chars)
92 call assert_equal('3', l[2].chars)
93 call assert_equal('#00e000', l[0].fg)
94 if &background == 'light'
95 call assert_equal('#ffffff', l[0].bg)
96 else
97 call assert_equal('#000000', l[0].bg)
98 endif
99
Bram Moolenaar5c838a32017-08-02 22:10:34 +0200100 let l = term_getline(a:buf, -1)
101 call assert_equal('', l)
102 let l = term_getline(a:buf, 0)
103 call assert_equal('', l)
104 let l = term_getline(a:buf, 999)
105 call assert_equal('', l)
Bram Moolenaar9c844842017-08-01 18:41:21 +0200106 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200107 call assert_equal('123', l)
108endfunc
109
110func Test_terminal_scrape()
111 if has('win32')
112 let cmd = 'cmd /c "cls && color 2 && echo 123"'
113 else
114 call writefile(["\<Esc>[32m123"], 'Xtext')
115 let cmd = "cat Xtext"
116 endif
117 let buf = term_start(cmd)
118
119 let termlist = term_list()
120 call assert_equal(1, len(termlist))
121 call assert_equal(buf, termlist[0])
122
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200123 " Nothing happens with invalid buffer number
124 call term_wait(1234)
125
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200126 call term_wait(buf)
127 call Check_123(buf)
128
129 " Must still work after the job ended.
130 let g:job = term_getjob(buf)
131 call WaitFor('job_status(g:job) == "dead"')
132 call term_wait(buf)
133 call Check_123(buf)
134
135 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200136 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200137endfunc