blob: c594548647e31dc8819b7a4000eda8982964774b [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
Bram Moolenaar406cd902020-02-18 21:54:41 +0100241 " Test for closing a window with a modified buffer
242 let buf = RunVimInTerminal('', {'rows': 20})
243 call term_sendkeys(buf, ":set nomore\n")
244 call term_sendkeys(buf, ":new\n")
245 call term_sendkeys(buf, ":call setline(1, 'abc')\n")
246 call term_sendkeys(buf, ":confirm close\n")
247 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
248 \ term_getline(buf, 20))}, 1000)
249 call term_sendkeys(buf, "C")
250 call term_sendkeys(buf, ":confirm close\n")
251 call WaitForAssert({-> assert_match('^\[Y\]es, (N)o, (C)ancel: *$',
252 \ term_getline(buf, 20))}, 1000)
253 call term_sendkeys(buf, "N")
254 call term_sendkeys(buf, ":quit\n")
255 call StopVimInTerminal(buf)
256
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100257 call delete('foo')
258 call delete('bar')
259endfunc
260
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100261" Test for the :print command
262func Test_print_cmd()
263 call assert_fails('print', 'E749:')
264endfunc
265
266" Test for the :winsize command
267func Test_winsize_cmd()
268 call assert_fails('winsize 1', 'E465:')
269endfunc
270
271" Test for the :redir command
272func Test_redir_cmd()
273 call assert_fails('redir @@', 'E475:')
274 call assert_fails('redir abc', 'E475:')
275 if has('unix')
276 call mkdir('Xdir')
277 call assert_fails('redir > Xdir', 'E17:')
278 call delete('Xdir', 'd')
279 endif
280 if !has('bsd')
281 call writefile([], 'Xfile')
282 call setfperm('Xfile', 'r--r--r--')
283 call assert_fails('redir! > Xfile', 'E190:')
284 call delete('Xfile')
285 endif
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100286
287 " Test for redirecting to a register
288 redir @q> | echon 'clean ' | redir END
289 redir @q>> | echon 'water' | redir END
290 call assert_equal('clean water', @q)
291
292 " Test for redirecting to a variable
293 redir => color | echon 'blue ' | redir END
294 redir =>> color | echon 'sky' | redir END
295 call assert_equal('blue sky', color)
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100296endfunc
297
298" Test for the :filetype command
299func Test_filetype_cmd()
300 call assert_fails('filetype abc', 'E475:')
301endfunc
302
303" Test for the :mode command
304func Test_mode_cmd()
305 call assert_fails('mode abc', 'E359:')
306endfunc
307
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100308" Test for the :sleep command
309func Test_sleep_cmd()
310 call assert_fails('sleep x', 'E475:')
311endfunc
312
313" Test for the :read command
314func Test_read_cmd()
315 call writefile(['one'], 'Xfile')
316 new
317 call assert_fails('read', 'E32:')
318 edit Xfile
319 read
320 call assert_equal(['one', 'one'], getline(1, '$'))
321 close!
322 new
323 read Xfile
324 call assert_equal(['', 'one'], getline(1, '$'))
325 call deletebufline('', 1, '$')
326 call feedkeys("Qr Xfile\<CR>visual\<CR>", 'xt')
327 call assert_equal(['one'], getline(1, '$'))
328 close!
329 call delete('Xfile')
330endfunc
331
332" Test for running Ex commands when text is locked.
333" <C-\>e in the command line is used to lock the text
334func Test_run_excmd_with_text_locked()
335 " :quit
336 let cmd = ":\<C-\>eexecute('quit')\<CR>\<C-C>"
337 call assert_fails("call feedkeys(cmd, 'xt')", 'E523:')
338
339 " :qall
340 let cmd = ":\<C-\>eexecute('qall')\<CR>\<C-C>"
341 call assert_fails("call feedkeys(cmd, 'xt')", 'E523:')
342
343 " :exit
344 let cmd = ":\<C-\>eexecute('exit')\<CR>\<C-C>"
345 call assert_fails("call feedkeys(cmd, 'xt')", 'E523:')
346
347 " :close - should be ignored
348 new
349 let cmd = ":\<C-\>eexecute('close')\<CR>\<C-C>"
350 call assert_equal(2, winnr('$'))
351 close
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100352
353 call assert_fails("call feedkeys(\":\<C-R>=execute('bnext')\<CR>\", 'xt')", 'E523:')
354endfunc
355
356" Test for the :verbose command
357func Test_verbose_cmd()
358 call assert_equal([' verbose=1'], split(execute('verbose set vbs'), "\n"))
359 call assert_equal([' verbose=0'], split(execute('0verbose set vbs'), "\n"))
360 let l = execute("4verbose set verbose | set verbose")
361 call assert_equal([' verbose=4', ' verbose=0'], split(l, "\n"))
362endfunc
363
364" Test for the :delete command and the related abbreviated commands
365func Test_excmd_delete()
366 new
367 call setline(1, ['foo', "\tbar"])
368 call assert_equal(['^Ibar$'], split(execute('dl'), "\n"))
369 call setline(1, ['foo', "\tbar"])
370 call assert_equal(['^Ibar$'], split(execute('dell'), "\n"))
371 call setline(1, ['foo', "\tbar"])
372 call assert_equal(['^Ibar$'], split(execute('delel'), "\n"))
373 call setline(1, ['foo', "\tbar"])
374 call assert_equal(['^Ibar$'], split(execute('deletl'), "\n"))
375 call setline(1, ['foo', "\tbar"])
376 call assert_equal(['^Ibar$'], split(execute('deletel'), "\n"))
377 call setline(1, ['foo', "\tbar"])
378 call assert_equal([' bar'], split(execute('dp'), "\n"))
379 call setline(1, ['foo', "\tbar"])
380 call assert_equal([' bar'], split(execute('dep'), "\n"))
381 call setline(1, ['foo', "\tbar"])
382 call assert_equal([' bar'], split(execute('delp'), "\n"))
383 call setline(1, ['foo', "\tbar"])
384 call assert_equal([' bar'], split(execute('delep'), "\n"))
385 call setline(1, ['foo', "\tbar"])
386 call assert_equal([' bar'], split(execute('deletp'), "\n"))
387 call setline(1, ['foo', "\tbar"])
388 call assert_equal([' bar'], split(execute('deletep'), "\n"))
389 close!
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100390endfunc
391
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100392" vim: shiftwidth=2 sts=2 expandtab