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