xref: /vim-8.2.3635/src/testdir/test_excmd.vim (revision 3b0ef8cf)
1" Tests for various Ex commands.
2
3source check.vim
4
5func 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
13
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:')
23  call assert_fails(":\\xcenter", 'E10:')
24endfunc
25
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
50
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
175" 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
245" 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
270endfunc
271
272" Test for the :filetype command
273func Test_filetype_cmd()
274  call assert_fails('filetype abc', 'E475:')
275endfunc
276
277" Test for the :mode command
278func Test_mode_cmd()
279  call assert_fails('mode abc', 'E359:')
280endfunc
281
282" vim: shiftwidth=2 sts=2 expandtab
283