blob: 97f7d5c2d90d9582ecf93a84ad75a7de95f06af9 [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 Moolenaar9c844842017-08-01 18:41:21 +020084 let l = term_scrape(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020085 call assert_true(len(l) > 0)
86 call assert_equal('1', l[0].chars)
87 call assert_equal('2', l[1].chars)
88 call assert_equal('3', l[2].chars)
89 call assert_equal('#00e000', l[0].fg)
90 if &background == 'light'
91 call assert_equal('#ffffff', l[0].bg)
92 else
93 call assert_equal('#000000', l[0].bg)
94 endif
95
Bram Moolenaar9c844842017-08-01 18:41:21 +020096 let l = term_getline(a:buf, 1)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020097 call assert_equal('123', l)
98endfunc
99
100func Test_terminal_scrape()
101 if has('win32')
102 let cmd = 'cmd /c "cls && color 2 && echo 123"'
103 else
104 call writefile(["\<Esc>[32m123"], 'Xtext')
105 let cmd = "cat Xtext"
106 endif
107 let buf = term_start(cmd)
108
109 let termlist = term_list()
110 call assert_equal(1, len(termlist))
111 call assert_equal(buf, termlist[0])
112
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200113 " Nothing happens with invalid buffer number
114 call term_wait(1234)
115
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200116 call term_wait(buf)
117 call Check_123(buf)
118
119 " Must still work after the job ended.
120 let g:job = term_getjob(buf)
121 call WaitFor('job_status(g:job) == "dead"')
122 call term_wait(buf)
123 call Check_123(buf)
124
125 exe buf . 'bwipe'
Bram Moolenaarf144a3f2017-07-30 18:02:12 +0200126 call delete('Xtext')
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200127endfunc