blob: 2407f8eb1ee6066a28a0a1c7371152a0e94a58e2 [file] [log] [blame]
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02001" Tests for various Ex commands.
2
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01003source check.vim
4
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02005func Test_ex_delete()
6 new
7 call setline(1, ['a', 'b', 'c'])
8 2
9 " :dl is :delete with the "l" flag, not :dlist
10 .dl
11 call assert_equal(['a', 'c'], getline(1, 2))
12endfunc
Bram Moolenaar0acae7a2019-08-06 21:29:29 +020013
14func Test_range_error()
15 call assert_fails(':.echo 1', 'E481:')
16 call assert_fails(':$echo 1', 'E481:')
17 call assert_fails(':1,2echo 1', 'E481:')
18 call assert_fails(':+1echo 1', 'E481:')
19 call assert_fails(':/1/echo 1', 'E481:')
20 call assert_fails(':\/echo 1', 'E481:')
21 normal vv
22 call assert_fails(":'<,'>echo 1", 'E481:')
Bram Moolenaar9f6277b2020-02-11 22:04:02 +010023 call assert_fails(":\\xcenter", 'E10:')
Bram Moolenaar0acae7a2019-08-06 21:29:29 +020024endfunc
Bram Moolenaar52410572019-10-27 05:12:45 +010025
26func Test_buffers_lastused()
27 call test_settime(localtime() - 2000) " middle
28 edit bufa
29 enew
30 call test_settime(localtime() - 10) " newest
31 edit bufb
32 enew
33 call test_settime(1550010000) " oldest
34 edit bufc
35 enew
36 call test_settime(0)
37 enew
38
39 let ls = split(execute('buffers t', 'silent!'), '\n')
40 let bufs = ls->map({i,v->split(v, '"\s*')[1:2]})
41 call assert_equal(['bufb', 'bufa', 'bufc'], bufs[1:]->map({i,v->v[0]}))
42 call assert_match('1[0-3] seconds ago', bufs[1][1])
43 call assert_match('\d\d:\d\d:\d\d', bufs[2][1])
44 call assert_match('2019/02/1\d \d\d:\d\d:00', bufs[3][1])
45
46 bwipeout bufa
47 bwipeout bufb
48 bwipeout bufc
49endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010050
51" Test for the :copy command
52func Test_copy()
53 new
54
55 call setline(1, ['L1', 'L2', 'L3', 'L4'])
56 " copy lines in a range to inside the range
57 1,3copy 2
58 call assert_equal(['L1', 'L2', 'L1', 'L2', 'L3', 'L3', 'L4'], getline(1, 7))
59
60 close!
61endfunc
62
63" Test for the :file command
64func Test_file_cmd()
65 call assert_fails('3file', 'E474:')
66 call assert_fails('0,0file', 'E474:')
67 call assert_fails('0file abc', 'E474:')
68endfunc
69
70" Test for the :drop command
71func Test_drop_cmd()
72 call writefile(['L1', 'L2'], 'Xfile')
73 enew | only
74 drop Xfile
75 call assert_equal('L2', getline(2))
76 " Test for switching to an existing window
77 below new
78 drop Xfile
79 call assert_equal(1, winnr())
80 " Test for splitting the current window
81 enew | only
82 set modified
83 drop Xfile
84 call assert_equal(2, winnr('$'))
85 " Check for setting the argument list
86 call assert_equal(['Xfile'], argv())
87 enew | only!
88 call delete('Xfile')
89endfunc
90
91" Test for the :append command
92func Test_append_cmd()
93 new
94 call setline(1, [' L1'])
95 call feedkeys(":append\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
96 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
97 %delete _
98 " append after a specific line
99 call setline(1, [' L1', ' L2', ' L3'])
100 call feedkeys(":2append\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
101 call assert_equal([' L1', ' L2', ' L4', ' L5', ' L3'], getline(1, '$'))
102 %delete _
103 " append with toggling 'autoindent'
104 call setline(1, [' L1'])
105 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
106 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
107 call assert_false(&autoindent)
108 %delete _
109 " append with 'autoindent' set and toggling 'autoindent'
110 set autoindent
111 call setline(1, [' L1'])
112 call feedkeys(":append!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
113 call assert_equal([' L1', ' L2', ' L3'], getline(1, '$'))
114 call assert_true(&autoindent)
115 set autoindent&
116 close!
117endfunc
118
119" Test for the :insert command
120func Test_insert_cmd()
121 new
122 call setline(1, [' L1'])
123 call feedkeys(":insert\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
124 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
125 %delete _
126 " insert before a specific line
127 call setline(1, [' L1', ' L2', ' L3'])
128 call feedkeys(":2insert\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
129 call assert_equal([' L1', ' L4', ' L5', ' L2', ' L3'], getline(1, '$'))
130 %delete _
131 " insert with toggling 'autoindent'
132 call setline(1, [' L1'])
133 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
134 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
135 call assert_false(&autoindent)
136 %delete _
137 " insert with 'autoindent' set and toggling 'autoindent'
138 set autoindent
139 call setline(1, [' L1'])
140 call feedkeys(":insert!\<CR> L2\<CR> L3\<CR>.\<CR>", 'xt')
141 call assert_equal([' L2', ' L3', ' L1'], getline(1, '$'))
142 call assert_true(&autoindent)
143 set autoindent&
144 close!
145endfunc
146
147" Test for the :change command
148func Test_change_cmd()
149 new
150 call setline(1, [' L1', 'L2', 'L3'])
151 call feedkeys(":change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
152 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
153 %delete _
154 " change a specific line
155 call setline(1, [' L1', ' L2', ' L3'])
156 call feedkeys(":2change\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
157 call assert_equal([' L1', ' L4', ' L5', ' L3'], getline(1, '$'))
158 %delete _
159 " change with toggling 'autoindent'
160 call setline(1, [' L1', 'L2', 'L3'])
161 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
162 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
163 call assert_false(&autoindent)
164 %delete _
165 " change with 'autoindent' set and toggling 'autoindent'
166 set autoindent
167 call setline(1, [' L1', 'L2', 'L3'])
168 call feedkeys(":change!\<CR> L4\<CR> L5\<CR>.\<CR>", 'xt')
169 call assert_equal([' L4', ' L5', 'L2', 'L3'], getline(1, '$'))
170 call assert_true(&autoindent)
171 set autoindent&
172 close!
173endfunc
174
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100175" Test for the :language command
176func Test_language_cmd()
177 CheckFeature multi_lang
178
179 call assert_fails('language ctype non_existing_lang', 'E197:')
180 call assert_fails('language time non_existing_lang', 'E197:')
181endfunc
182
183" Test for the :confirm command dialog
184func Test_confirm_cmd()
185 CheckNotGui
186 CheckRunVimInTerminal
187
188 call writefile(['foo1'], 'foo')
189 call writefile(['bar1'], 'bar')
190
191 " Test for saving all the modified buffers
192 let buf = RunVimInTerminal('', {'rows': 20})
193 call term_sendkeys(buf, ":set nomore\n")
194 call term_sendkeys(buf, ":new foo\n")
195 call term_sendkeys(buf, ":call setline(1, 'foo2')\n")
196 call term_sendkeys(buf, ":new bar\n")
197 call term_sendkeys(buf, ":call setline(1, 'bar2')\n")
198 call term_sendkeys(buf, ":wincmd b\n")
199 call term_sendkeys(buf, ":confirm qall\n")
200 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
201 call term_sendkeys(buf, "A")
202 call StopVimInTerminal(buf)
203
204 call assert_equal(['foo2'], readfile('foo'))
205 call assert_equal(['bar2'], readfile('bar'))
206
207 " Test for discarding all the changes to modified buffers
208 let buf = RunVimInTerminal('', {'rows': 20})
209 call term_sendkeys(buf, ":set nomore\n")
210 call term_sendkeys(buf, ":new foo\n")
211 call term_sendkeys(buf, ":call setline(1, 'foo3')\n")
212 call term_sendkeys(buf, ":new bar\n")
213 call term_sendkeys(buf, ":call setline(1, 'bar3')\n")
214 call term_sendkeys(buf, ":wincmd b\n")
215 call term_sendkeys(buf, ":confirm qall\n")
216 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
217 call term_sendkeys(buf, "D")
218 call StopVimInTerminal(buf)
219
220 call assert_equal(['foo2'], readfile('foo'))
221 call assert_equal(['bar2'], readfile('bar'))
222
223 " Test for saving and discarding changes to some buffers
224 let buf = RunVimInTerminal('', {'rows': 20})
225 call term_sendkeys(buf, ":set nomore\n")
226 call term_sendkeys(buf, ":new foo\n")
227 call term_sendkeys(buf, ":call setline(1, 'foo4')\n")
228 call term_sendkeys(buf, ":new bar\n")
229 call term_sendkeys(buf, ":call setline(1, 'bar4')\n")
230 call term_sendkeys(buf, ":wincmd b\n")
231 call term_sendkeys(buf, ":confirm qall\n")
232 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, Save (A)ll, (D)iscard All, (C)ancel: ', term_getline(buf, 20))}, 1000)
233 call term_sendkeys(buf, "N")
234 call WaitForAssert({-> assert_match('\[Y\]es, (N)o, (C)ancel: ', term_getline(buf, 20))}, 1000)
235 call term_sendkeys(buf, "Y")
236 call StopVimInTerminal(buf)
237
238 call assert_equal(['foo4'], readfile('foo'))
239 call assert_equal(['bar2'], readfile('bar'))
240
241 call delete('foo')
242 call delete('bar')
243endfunc
244
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100245" Test for the :print command
246func Test_print_cmd()
247 call assert_fails('print', 'E749:')
248endfunc
249
250" Test for the :winsize command
251func Test_winsize_cmd()
252 call assert_fails('winsize 1', 'E465:')
253endfunc
254
255" Test for the :redir command
256func Test_redir_cmd()
257 call assert_fails('redir @@', 'E475:')
258 call assert_fails('redir abc', 'E475:')
259 if has('unix')
260 call mkdir('Xdir')
261 call assert_fails('redir > Xdir', 'E17:')
262 call delete('Xdir', 'd')
263 endif
264 if !has('bsd')
265 call writefile([], 'Xfile')
266 call setfperm('Xfile', 'r--r--r--')
267 call assert_fails('redir! > Xfile', 'E190:')
268 call delete('Xfile')
269 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100270
271 " Test for redirecting to a register
272 redir @q> | echon 'clean ' | redir END
273 redir @q>> | echon 'water' | redir END
274 call assert_equal('clean water', @q)
275
276 " Test for redirecting to a variable
277 redir => color | echon 'blue ' | redir END
278 redir =>> color | echon 'sky' | redir END
279 call assert_equal('blue sky', color)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100280endfunc
281
282" Test for the :filetype command
283func Test_filetype_cmd()
284 call assert_fails('filetype abc', 'E475:')
285endfunc
286
287" Test for the :mode command
288func Test_mode_cmd()
289 call assert_fails('mode abc', 'E359:')
290endfunc
291
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100292" Test for the :sleep command
293func Test_sleep_cmd()
294 call assert_fails('sleep x', 'E475:')
295endfunc
296
297" Test for the :read command
298func Test_read_cmd()
299 call writefile(['one'], 'Xfile')
300 new
301 call assert_fails('read', 'E32:')
302 edit Xfile
303 read
304 call assert_equal(['one', 'one'], getline(1, '$'))
305 close!
306 new
307 read Xfile
308 call assert_equal(['', 'one'], getline(1, '$'))
309 call deletebufline('', 1, '$')
310 call feedkeys("Qr Xfile\<CR>visual\<CR>", 'xt')
311 call assert_equal(['one'], getline(1, '$'))
312 close!
313 call delete('Xfile')
314endfunc
315
316" Test for running Ex commands when text is locked.
317" <C-\>e in the command line is used to lock the text
318func Test_run_excmd_with_text_locked()
319 " :quit
320 let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
321 call assert_fails("call feedkeys(cmd, 'xt')", 'E523:')
322
323 " :qall
324 let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
325 call assert_fails("call feedkeys(cmd, 'xt')", 'E523:')
326
327 " :exit
328 let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
329 call assert_fails("call feedkeys(cmd, 'xt')", 'E523:')
330
331 " :close - should be ignored
332 new
333 let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
334 call assert_equal(2, winnr('$'))
335 close
336endfunc
337
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100338" vim: shiftwidth=2 sts=2 expandtab