robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40325 from justinmk/cmdwindd5ebae7ac

master
7.9 KiB277 linesraw
1" Tests for editing the command line.
2
3source check.vim
4source screendump.vim
5
6
7func Test_cant_open_cmdwin_in_cmdwin()
8  try
9    call feedkeys("q:q::q\<CR>", "x!")
10  catch
11    let caught = v:exception
12  endtry
13  call assert_match('E1292:', caught)
14endfunc
15
16func Test_cmdwin_virtual_edit()
17  enew!
18  set ve=all cpo+=$
19  silent normal q/s
20
21  set ve= cpo-=$
22endfunc
23
24" Check that a :normal command can be used to stop Visual mode without side
25" effects.
26func Test_normal_escape()
27  throw 'Skipped: Nvim supports cmdwin freedom #40312'
28  call feedkeys("q:i\" foo\<Esc>:normal! \<C-V>\<Esc>\<CR>:\" bar\<CR>", 'ntx')
29  call assert_equal('" bar', @:)
30endfunc
31
32" This was using a pointer to a freed buffer
33func Test_cmdwin_freed_buffer_ptr()
34  throw "Skipped: Nvim supports cmdwin freedom #40312"
35  " this does not work on MS-Windows because renaming an open file fails
36  CheckNotMSWindows
37
38  au BufEnter * next 0| file 
39  edit 0
40  silent! norm q/
41
42  au! BufEnter
43  bwipe!
44endfunc
45
46" This was resulting in a window with negative width.
47" The test doesn't reproduce the illegal memory access though...
48func Test_cmdwin_split_often()
49  let lines = &lines
50  let columns = &columns
51  set t_WS=
52
53  try
54    " set encoding=iso8859
55    set ruler
56    winsize 0 0
57    noremap 0 H
58    sil norm 0000000q:
59  catch /E36:/
60  endtry
61
62  bwipe!
63  set encoding=utf8
64  let &lines = lines
65  let &columns = columns
66endfunc
67
68func Test_cmdwin_restore_heights()
69  set showtabline=0 cmdheight=2 laststatus=0
70  call feedkeys("q::set cmdheight=1\<CR>:q\<CR>", 'ntx')
71  call assert_equal(&lines - 1, winheight(0))
72
73  set showtabline=2 cmdheight=3
74  call feedkeys("q::set showtabline=0\<CR>:q\<CR>", 'ntx')
75  call assert_equal(&lines - 3, winheight(0))
76
77  set cmdheight=1 laststatus=2
78  call feedkeys("q::set laststatus=0\<CR>:q\<CR>", 'ntx')
79  call assert_equal(&lines - 1, winheight(0))
80
81  set laststatus=2
82  call feedkeys("q::set laststatus=1\<CR>:q\<CR>", 'ntx')
83  call assert_equal(&lines - 1, winheight(0))
84
85  set laststatus=2
86  belowright vsplit
87  wincmd _
88  let restcmds = winrestcmd()
89  call feedkeys("q::set laststatus=1\<CR>:q\<CR>", 'ntx')
90  " As we have 2 windows, &ls = 1 should still have a statusline on the last
91  " window. As such, the number of available rows hasn't changed and the window
92  " sizes should be restored.
93  call assert_equal(restcmds, winrestcmd())
94
95  set cmdheight& showtabline& laststatus&
96endfunc
97
98func Test_cmdwin_temp_curwin()
99  throw "Skipped: Nvim supports cmdwin freedom #40312"
100  func CheckWraps(expect_wrap)
101    setlocal textwidth=0 wrapmargin=1
102
103    call deletebufline('', 1, '$')
104    let as = repeat('a', winwidth(0) - 2 - &wrapmargin)
105    call setline(1, as .. ' b')
106    normal! gww
107
108    setlocal textwidth& wrapmargin&
109    call assert_equal(a:expect_wrap ? [as, 'b'] : [as .. ' b'], getline(1, '$'))
110  endfunc
111
112  func CheckCmdWin()
113    call assert_equal('command', win_gettype())
114    " textoff and &wrapmargin formatting considers the cmdwin_type char.
115    call assert_equal(1, getwininfo(win_getid())[0].textoff)
116    call CheckWraps(1)
117  endfunc
118
119  func CheckOtherWin()
120    call assert_equal('', win_gettype())
121    call assert_equal(0, getwininfo(win_getid())[0].textoff)
122    call CheckWraps(0)
123  endfunc
124
125  call feedkeys("q::call CheckCmdWin()\<CR>:call win_execute(win_getid(winnr('#')), 'call CheckOtherWin()')\<CR>:q<CR>", 'ntx')
126
127  %bwipe!
128  delfunc CheckWraps
129  delfunc CheckCmdWin
130  delfunc CheckOtherWin
131endfunc
132
133func Test_cmdwin_interrupted()
134  throw "Skipped: Nvim supports cmdwin freedom #40312"
135  func CheckInterrupted()
136    call feedkeys("q::call assert_equal('', getcmdwintype())\<CR>:call assert_equal('', getcmdtype())\<CR>:q<CR>", 'ntx')
137  endfunc
138
139  augroup CmdWin
140
141  " While opening the cmdwin's split:
142  " Close the cmdwin's window.
143  au WinEnter * ++once quit
144  call CheckInterrupted()
145
146  " Close the old window.
147  au WinEnter * ++once execute winnr('#') 'quit'
148  call CheckInterrupted()
149
150  " Switch back to the old window.
151  au WinEnter * ++once wincmd p
152  call CheckInterrupted()
153
154  " Change the old window's buffer.
155  au WinEnter * ++once call win_execute(win_getid(winnr('#')), 'enew')
156  call CheckInterrupted()
157
158  " Using BufLeave autocmds as cmdwin restrictions do not apply to them when
159  " fired from opening the cmdwin...
160  " After opening the cmdwin's split, while creating the cmdwin's buffer:
161  " Delete the cmdwin's buffer.
162  au BufLeave * ++once bwipe
163  call CheckInterrupted()
164
165  " Close the cmdwin's window.
166  au BufLeave * ++once quit
167  call CheckInterrupted()
168
169  " Close the old window.
170  au BufLeave * ++once execute winnr('#') 'quit'
171  call CheckInterrupted()
172
173  " Switch to a different window.
174  au BufLeave * ++once split
175  call CheckInterrupted()
176
177  " Change the old window's buffer.
178  au BufLeave * ++once call win_execute(win_getid(winnr('#')), 'enew')
179  call CheckInterrupted()
180
181  " However, changing the current buffer is OK and does not interrupt.
182  au BufLeave * ++once edit other
183  call feedkeys("q::let t=getcmdwintype()\<CR>:let b=bufnr()\<CR>:clo<CR>", 'ntx')
184  call assert_equal(':', t)
185  call assert_equal(1, bufloaded('other'))
186  call assert_notequal(b, bufnr('other'))
187
188  augroup END
189
190  " No autocmds should remain, but clear the augroup to be sure.
191  augroup CmdWin
192    au!
193  augroup END
194
195  %bwipe!
196  delfunc CheckInterrupted
197endfunc
198
199func Test_cmdwin_existing_bufname()
200  throw "Skipped: Nvim supports cmdwin freedom #40312"
201  func CheckName()
202    call assert_equal(1, getbufinfo('')[0].command)
203    call assert_equal(0, getbufinfo('[Command Line]')[0].command)
204    call assert_match('#a\s*"\[Command Line\]"', execute('ls'))
205    call assert_match('%a\s*"\[Command Line\]"', execute('ls'))
206  endfunc
207  file [Command Line]
208  call feedkeys("q::call CheckName()\<CR>:q\<CR>", 'ntx')
209  0file
210  delfunc CheckName
211endfunc
212
213func Test_cmdwin_showcmd()
214  CheckScreendump
215
216  let lines =<< trim [SCRIPT]
217    augroup vimHints | au! | augroup END
218    set showcmd
219  [SCRIPT]
220  call writefile(lines, 'XTest_cmdwin_showcmd', 'D')
221  let buf = RunVimInTerminal('-S XTest_cmdwin_showcmd', {'rows': 18})
222
223  for keys in ['q:', ":\<C-F>"]
224    call term_sendkeys(buf, keys)
225    call VerifyScreenDump(buf, 'Test_cmdwin_showcmd_1', {})
226    call term_sendkeys(buf, '"')
227    call WaitForAssert({-> assert_match('^: \+" *$', term_getline(buf, 18))})
228    call term_sendkeys(buf, 'x')
229    call WaitForAssert({-> assert_match('^: \+"x *$', term_getline(buf, 18))})
230    call term_sendkeys(buf, 'y')
231    call WaitForAssert({-> assert_match('^: \+"xy *$', term_getline(buf, 18))})
232    call term_sendkeys(buf, 'y')
233    call WaitForAssert({-> assert_match('^: \+$', term_getline(buf, 18))})
234    call term_sendkeys(buf, "\<C-C>\<C-C>")
235    call VerifyScreenDump(buf, 'Test_cmdwin_showcmd_2', {})
236  endfor
237
238  " clean up
239  call StopVimInTerminal(buf)
240endfunc
241
242func Test_cmdwin_cursor_position()
243  throw "Skipped: Nvim supports cmdwin freedom #40312"
244  " When the cmdline fills the screen width exactly, pressing CTRL-F to open
245  " the cmdwin should place the cursor on the last character, not past it.
246  let cmd = 'echo "' .. repeat('a', &columns - 8) .. '"'
247  call assert_equal(&columns - 1, len(cmd))
248  let g:cmdwin_col = 0
249  let g:cmdwin_line = ''
250  call feedkeys(':' .. cmd .. "\<C-F>" ..
251        \ ":let g:cmdwin_col = col('.')\<CR>" ..
252        \ ":let g:cmdwin_line = getline('.')\<CR>" ..
253        \ ":q\<CR>", 'x!')
254  call assert_equal(len(cmd), g:cmdwin_col)
255  call assert_equal(cmd, g:cmdwin_line)
256  unlet g:cmdwin_col g:cmdwin_line
257endfunc
258
259func Test_cmdwin_no_prefix_on_wrapped_line()
260  CheckScreendump
261
262  let lines =<< trim END
263    augroup vimHints | au! | augroup END
264  END
265  call writefile(lines, 'Xcmdwin_wrap', 'D')
266
267  let buf = RunVimInTerminal('-S Xcmdwin_wrap', #{rows: 12, cols: 40})
268  let cmd = 'echo "' .. repeat('a', 40 - 8) .. '"XYZ'
269  call term_sendkeys(buf, ':' .. cmd .. "\<C-F>")
270  call TermWait(buf, 100)
271  call VerifyScreenDump(buf, 'Test_cmdwin_wrap_prefix', {})
272
273  call term_sendkeys(buf, ":q\<CR>")
274  call StopVimInTerminal(buf)
275endfunc
276
277" vim: shiftwidth=2 sts=2 expandtab