robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
4f431bb632
master
1" Tests for the history functions 2 3source check.vim 4CheckFeature cmdline_hist 5 6set history = 7 7 8function History_Tests ( hist ) 9" First clear the history 10call histadd ( a: hist , 'dummy' ) 11call assert_true ( histdel ( a: hist )) 12call assert_equal ( -1 , histnr ( a: hist )) 13call assert_equal ( '' , histget ( a: hist )) 14 15call assert_true ( 'ls' ->histadd ( a: hist )) 16call assert_true ( histadd ( a: hist , 'buffers' )) 17call assert_equal ( 'buffers' , histget ( a: hist )) 18call assert_equal ( 'ls' , histget ( a: hist , -2 )) 19call assert_equal ( 'ls' , histget ( a: hist , 1 )) 20call assert_equal ( '' , histget ( a: hist , 5 )) 21call assert_equal ( '' , histget ( a: hist , -5 )) 22call assert_equal ( 2 , histnr ( a: hist )) 23call assert_true ( histdel ( a: hist , 2 )) 24call assert_false ( a: hist ->histdel ( 7 )) 25call assert_equal ( 1 , histnr ( a: hist )) 26call assert_equal ( 'ls' , histget ( a: hist , -1 )) 27 28call assert_true ( histadd ( a: hist , 'buffers' )) 29call assert_true ( histadd ( a: hist , 'ls' )) 30call assert_equal ( 'ls' , a: hist ->histget ( -1 )) 31call assert_equal ( 4 , a: hist ->histnr ()) 32 33let a = execute ( 'history ' . a: hist ) 34call assert_match ( "^\n # \\S* history\n 3 buffers\n> 4 ls$" , a ) 35let a = execute ( 'history all' ) 36call assert_match ( "^\n # .* history\n 3 buffers\n> 4 ls" , a ) 37 38if len ( a: hist ) > 0 39let a = execute ( 'history ' . a: hist . ' 2' ) 40call assert_match ( "^\n # \\S* history$" , a ) 41let a = execute ( 'history ' . a: hist . ' 3' ) 42call assert_match ( "^\n # \\S* history\n 3 buffers$" , a ) 43let a = execute ( 'history ' . a: hist . ' 4' ) 44call assert_match ( "^\n # \\S* history\n> 4 ls$" , a ) 45let a = execute ( 'history ' . a: hist . ' 3,4' ) 46call assert_match ( "^\n # \\S* history\n 3 buffers\n> 4 ls$" , a ) 47let a = execute ( 'history ' . a: hist . ' -1' ) 48call assert_match ( "^\n # \\S* history\n> 4 ls$" , a ) 49let a = execute ( 'history ' . a: hist . ' -2' ) 50call assert_match ( "^\n # \\S* history\n 3 buffers$" , a ) 51let a = execute ( 'history ' . a: hist . ' -2,' ) 52call assert_match ( "^\n # \\S* history\n 3 buffers\n> 4 ls$" , a ) 53let a = execute ( 'history ' . a: hist . ' -3' ) 54call assert_match ( "^\n # \\S* history$" , a ) 55endif 56 57" Test for removing entries matching a pattern 58for i in range ( 1 , 3 ) 59call histadd ( a: hist , 'text_' . i ) 60endfor 61call assert_true ( histdel ( a: hist , 'text_\d\+' )) 62call assert_equal ( 'ls' , histget ( a: hist , -1 )) 63 64" Test for freeing the entire history list 65for i in range ( 1 , 7 ) 66call histadd ( a: hist , 'text_' . i ) 67endfor 68call histdel ( a: hist ) 69for i in range ( 1 , 7 ) 70call assert_equal ( '' , histget ( a: hist , i )) 71call assert_equal ( '' , histget ( a: hist , i - 7 - 1 )) 72endfor 73 74" Test for freeing an entry at the beginning of the history list 75for i in range ( 1 , 4 ) 76call histadd ( a: hist , 'text_' . i ) 77endfor 78call histdel ( a: hist , 1 ) 79call assert_equal ( '' , histget ( a: hist , 1 )) 80call assert_equal ( 'text_4' , histget ( a: hist , 4 )) 81endfunction 82 83function Test_History () 84for h in [ 'cmd' , ':' , '' , 'search' , '/' , '?' , 'expr' , '=' , 'input' , '@' , 'debug' , '>' ] 85call History_Tests ( h ) 86endfor 87 88" Negative tests 89call assert_false ( histdel ( 'abc' )) 90call assert_equal ( '' , histget ( 'abc' )) 91call assert_fails ( 'call histdel([])' , 'E730:' ) 92call assert_equal ( '' , histget ( 10 )) 93call assert_fails ( 'call histget([])' , 'E730:' ) 94call assert_equal ( -1 , histnr ( 'abc' )) 95call assert_fails ( 'call histnr([])' , 'E730:' ) 96call assert_fails ( 'history xyz' , 'E488:' ) 97call assert_fails ( 'history ,abc' , 'E488:' ) 98call assert_fails ( 'call histdel(":", "\\%(")' , 'E53:' ) 99 100" Test for filtering the history list 101let hist_filter = execute ( ':filter /_\d/ :history all' ) ->split ( '\n' ) 102call assert_equal ( 20 , len ( hist_filter )) 103let expected = [ ' # cmd history' , 104 \' 2 text_2' , 105 \' 3 text_3' , 106 \'> 4 text_4' , 107 \' # search history' , 108 \' 2 text_2' , 109 \' 3 text_3' , 110 \'> 4 text_4' , 111 \' # expr history' , 112 \' 2 text_2' , 113 \' 3 text_3' , 114 \'> 4 text_4' , 115 \' # input history' , 116 \' 2 text_2' , 117 \' 3 text_3' , 118 \'> 4 text_4' , 119 \' # debug history' , 120 \' 2 text_2' , 121 \' 3 text_3' , 122 \'> 4 text_4' ] 123call assert_equal ( expected , hist_filter ) 124 125let cmds = { 'c' : 'cmd' , 's' : 'search' , 'e' : 'expr' , 'i' : 'input' , 'd' : 'debug' } 126for h in sort ( keys ( cmds )) 127" find some items 128let hist_filter = execute ( ':filter /_\d/ :history ' .. h ) ->split ( '\n' ) 129call assert_equal ( 4 , len ( hist_filter )) 130 131let expected = [ ' # ' .. cmds [ h ] .. ' history' , 132 \' 2 text_2' , 133 \' 3 text_3' , 134 \'> 4 text_4' ] 135call assert_equal ( expected , hist_filter ) 136 137" Search for an item that is not there 138let hist_filter = execute ( ':filter /XXXX/ :history ' .. h ) ->split ( '\n' ) 139call assert_equal ( 1 , len ( hist_filter )) 140 141let expected = [ ' # ' .. cmds [ h ] .. ' history' ] 142call assert_equal ( expected , hist_filter ) 143 144" Invert the filter condition, find non-matches 145let hist_filter = execute ( ':filter! /_3$/ :history ' .. h ) ->split ( '\n' ) 146call assert_equal ( 3 , len ( hist_filter )) 147 148let expected = [ ' # ' .. cmds [ h ] .. ' history' , 149 \' 2 text_2' , 150 \'> 4 text_4' ] 151call assert_equal ( expected , hist_filter ) 152endfor 153endfunction 154 155function Test_history_truncates_long_entry () 156" History entry short enough to fit on the screen should not be truncated. 157call histadd ( ':' , 'echo x' .. repeat ( 'y' , & columns - 17 ) .. 'z' ) 158let a = execute ( 'history : -1' ) 159 160call assert_match ( "^\n # cmd history\n" 161 \.. "> *\\d\\+ echo x" .. repeat ( 'y' , & columns - 17 ) .. 'z$' , a ) 162 163" Long history entry should be truncated to fit on the screen, with, '...' 164" inserted in the string to indicate the that there is truncation. 165call histadd ( ':' , 'echo x' .. repeat ( 'y' , & columns - 16 ) .. 'z' ) 166let a = execute ( 'history : -1' ) 167call assert_match ( "^\n # cmd history\n" 168 \.. "> *\\d\\+ echo xy\\+\.\.\.y\\+z$" , a ) 169endfunction 170 171function Test_Search_history_window () 172new 173call setline ( 1 , [ 'a' , 'b' , 'a' , 'b' ]) 1741 175call feedkeys ( "/a\<CR>" , 'xt' ) 176call assert_equal ( 'a' , getline ( '.' )) 1771 178call feedkeys ( "/b\<CR>" , 'xt' ) 179call assert_equal ( 'b' , getline ( '.' )) 1801 181" select the previous /a command 182call feedkeys ( "q/kk\<CR>" , 'x!' ) 183call assert_equal ( 'a' , getline ( '.' )) 184call assert_equal ( 'a' , @/) 185bwipe ! 186endfunc 187 188" Test for :history command option completion 189function Test_history_completion () 190call feedkeys ( ":history \<C-A>\<C-B>\"\<CR>" , 'tx' ) 191call assert_equal ( '"history / : = > ? @ all cmd debug expr input search' , @:) 192endfunc 193 194" Test for increasing the 'history' option value 195func Test_history_size () 196let save_histsz = & history 197set history = 10 198call histadd ( ':' , 'ls' ) 199call histdel ( ':' ) 200for i in range ( 1 , 5 ) 201call histadd ( ':' , 'cmd' .. i ) 202endfor 203call assert_equal ( 5 , histnr ( ':' )) 204call assert_equal ( 'cmd5' , histget ( ':' , -1 )) 205 206set history = 15 207for i in range ( 6 , 10 ) 208call histadd ( ':' , 'cmd' .. i ) 209endfor 210call assert_equal ( 10 , histnr ( ':' )) 211call assert_equal ( 'cmd1' , histget ( ':' , 1 )) 212call assert_equal ( 'cmd10' , histget ( ':' , -1 )) 213 214set history = 5 215call histadd ( ':' , 'abc' ) 216call assert_equal ( '' , histget ( ':' , 6 )) 217call assert_equal ( '' , histget ( ':' , 12 )) 218call assert_equal ( 'cmd7' , histget ( ':' , 7 )) 219call assert_equal ( 'abc' , histget ( ':' , -1 )) 220 221" This test works only when the language is English 222if v: lang == "C" || v: lang =~ '^[Ee]n' 223set history = 0 224redir => v 225call feedkeys ( ":history\<CR>" , 'xt' ) 226redir END 227call assert_equal ([ "'history' option is zero" ], split ( v , "\n" )) 228endif 229 230let & history = save_histsz 231endfunc 232 233" Test for recalling old search patterns in / 234func Test_history_search () 235call histdel ( '/' ) 236let g: pat = [] 237func SavePat () 238call add ( g: pat , getcmdline ()) 239return '' 240endfunc 241cnoremap < F2 > < C-\ > eSavePat()< CR > 242call histadd ( '/' , 'pat1' ) 243call histadd ( '/' , 'pat2' ) 244let @/= '' 245call feedkeys ( "/\<Up>\<F2>\<Up>\<F2>\<Down>\<Down>\<F2>\<Esc>" , 'xt' ) 246call assert_equal ([ 'pat2' , 'pat1' , '' ], g: pat ) 247cunmap <F2> 248delfunc SavePat 249 250" Search for a pattern that is not present in the history 251call assert_beeps ( 'call feedkeys("/a1b2\<Up>\<CR>", "xt")' ) 252 253" Recall patterns with 'history' set to 0 254set history = 0 255let @/= 'abc' 256let cmd = 'call feedkeys("/\<Up>\<Down>\<S-Up>\<S-Down>\<CR>", "xt")' 257call assert_fails ( cmd , 'E486:' ) 258set history & 259 260" Recall patterns till the end of history 261set history = 4 262call histadd ( '/' , 'pat' ) 263call histdel ( '/' ) 264call histadd ( '/' , 'pat1' ) 265call histadd ( '/' , 'pat2' ) 266call assert_beeps ( 'call feedkeys("/\<Up>\<Up>\<Up>\<C-U>\<cr>", "xt")' ) 267call assert_beeps ( 'call feedkeys("/\<Down><cr>", "xt")' ) 268 269" Test for wrapping around the history list 270for i in range ( 3 , 7 ) 271call histadd ( '/' , 'pat' .. i ) 272endfor 273let upcmd = "\<up>\<up>\<up>\<up>\<up>" 274let downcmd = "\<down>\<down>\<down>\<down>\<down>" 275try 276call feedkeys ( "/" .. upcmd .. "\<cr>" , 'xt' ) 277catch /E486:/ 278endtry 279call assert_equal ( 'pat4' , @/) 280try 281call feedkeys ( "/" .. upcmd .. downcmd .. "\<cr>" , 'xt' ) 282catch /E486:/ 283endtry 284call assert_equal ( 'pat4' , @/) 285 286" Test for changing the search command separator in the history 287call assert_fails ( 'call feedkeys("/def/\<cr>", "xt")' , 'E486:' ) 288call assert_fails ( 'call feedkeys("?\<up>\<cr>", "xt")' , 'E486:' ) 289call assert_equal ( 'def?' , histget ( '/' , -1 )) 290 291call assert_fails ( 'call feedkeys("/ghi?\<cr>", "xt")' , 'E486:' ) 292call assert_fails ( 'call feedkeys("?\<up>\<cr>", "xt")' , 'E486:' ) 293call assert_equal ( 'ghi\?' , histget ( '/' , -1 )) 294 295set history & 296endfunc 297 298" Test for making sure the key value is not stored in history 299func Test_history_crypt_key () 300CheckFeature cryptv 301 302call feedkeys ( ":set bs=2 key=abc ts=8\<CR>" , 'xt' ) 303call assert_equal ( 'set bs=2 key= ts=8' , histget ( ':' )) 304 305call assert_fails ( "call feedkeys(':set bs=2 key-=abc ts=8\<CR>', 'xt')" ) 306call assert_equal ( 'set bs=2 key-= ts=8' , histget ( ':' )) 307 308set key & bs & ts & 309endfunc 310 311" The following used to overflow and causing a use-after-free 312func Test_history_max_val () 313 314set history = 10 315call assert_fails ( ':history 2147483648' , 'E1510:' ) 316set history & 317endfunc 318 319" vim: shiftwidth=2 sts=2 expandtab