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