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 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)) 81endfunction 82 83function 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:') 99endfunction 100 101function Test_history_truncates_long_entry() 102 " History entry short enough to fit on the screen should not be truncated. 103 call histadd(':', 'echo x' .. repeat('y', &columns - 17) .. 'z') 104 let a = execute('history : -1') 105 106 call assert_match("^\n # cmd history\n" 107 \ .. "> *\\d\\+ echo x" .. repeat('y', &columns - 17) .. 'z$', a) 108 109 " Long history entry should be truncated to fit on the screen, with, '...' 110 " inserted in the string to indicate the that there is truncation. 111 call histadd(':', 'echo x' .. repeat('y', &columns - 16) .. 'z') 112 let a = execute('history : -1') 113 call assert_match("^\n # cmd history\n" 114 \ .. "> *\\d\\+ echo xy\\+\.\.\.y\\+z$", a) 115endfunction 116 117function Test_Search_history_window() 118 new 119 call setline(1, ['a', 'b', 'a', 'b']) 120 1 121 call feedkeys("/a\<CR>", 'xt') 122 call assert_equal('a', getline('.')) 123 1 124 call feedkeys("/b\<CR>", 'xt') 125 call assert_equal('b', getline('.')) 126 1 127 " select the previous /a command 128 call feedkeys("q/kk\<CR>", 'x!') 129 call assert_equal('a', getline('.')) 130 call assert_equal('a', @/) 131 bwipe! 132endfunc 133 134" Test for :history command option completion 135function Test_history_completion() 136 call feedkeys(":history \<C-A>\<C-B>\"\<CR>", 'tx') 137 call assert_equal('"history / : = > ? @ all cmd debug expr input search', @:) 138endfunc 139 140" Test for increasing the 'history' option value 141func Test_history_size() 142 let save_histsz = &history 143 set history=10 144 call histadd(':', 'ls') 145 call histdel(':') 146 for i in range(1, 5) 147 call histadd(':', 'cmd' .. i) 148 endfor 149 call assert_equal(5, histnr(':')) 150 call assert_equal('cmd5', histget(':', -1)) 151 152 set history=15 153 for i in range(6, 10) 154 call histadd(':', 'cmd' .. i) 155 endfor 156 call assert_equal(10, histnr(':')) 157 call assert_equal('cmd1', histget(':', 1)) 158 call assert_equal('cmd10', histget(':', -1)) 159 160 set history=5 161 call histadd(':', 'abc') 162 call assert_equal('', histget(':', 6)) 163 call assert_equal('', histget(':', 12)) 164 call assert_equal('cmd7', histget(':', 7)) 165 call assert_equal('abc', histget(':', -1)) 166 167 " This test works only when the language is English 168 if v:lang == "C" || v:lang =~ '^[Ee]n' 169 set history=0 170 redir => v 171 call feedkeys(":history\<CR>", 'xt') 172 redir END 173 call assert_equal(["'history' option is zero"], split(v, "\n")) 174 endif 175 176 let &history=save_histsz 177endfunc 178 179" Test for recalling old search patterns in / 180func Test_history_search() 181 call histdel('/') 182 let g:pat = [] 183 func SavePat() 184 call add(g:pat, getcmdline()) 185 return '' 186 endfunc 187 cnoremap <F2> <C-\>eSavePat()<CR> 188 call histadd('/', 'pat1') 189 call histadd('/', 'pat2') 190 let @/ = '' 191 call feedkeys("/\<Up>\<F2>\<Up>\<F2>\<Down>\<Down>\<F2>\<Esc>", 'xt') 192 call assert_equal(['pat2', 'pat1', ''], g:pat) 193 cunmap <F2> 194 delfunc SavePat 195 196 " Search for a pattern that is not present in the history 197 call assert_beeps('call feedkeys("/a1b2\<Up>\<CR>", "xt")') 198 199 " Recall patterns with 'history' set to 0 200 set history=0 201 let @/ = 'abc' 202 let cmd = 'call feedkeys("/\<Up>\<Down>\<S-Up>\<S-Down>\<CR>", "xt")' 203 call assert_fails(cmd, 'E486:') 204 set history& 205 206 " Recall patterns till the end of history 207 set history=4 208 call histadd('/', 'pat') 209 call histdel('/') 210 call histadd('/', 'pat1') 211 call histadd('/', 'pat2') 212 call assert_beeps('call feedkeys("/\<Up>\<Up>\<Up>\<C-U>\<cr>", "xt")') 213 call assert_beeps('call feedkeys("/\<Down><cr>", "xt")') 214 215 " Test for wrapping around the history list 216 for i in range(3, 7) 217 call histadd('/', 'pat' .. i) 218 endfor 219 let upcmd = "\<up>\<up>\<up>\<up>\<up>" 220 let downcmd = "\<down>\<down>\<down>\<down>\<down>" 221 try 222 call feedkeys("/" .. upcmd .. "\<cr>", 'xt') 223 catch /E486:/ 224 endtry 225 call assert_equal('pat4', @/) 226 try 227 call feedkeys("/" .. upcmd .. downcmd .. "\<cr>", 'xt') 228 catch /E486:/ 229 endtry 230 call assert_equal('pat4', @/) 231 232 " Test for changing the search command separator in the history 233 call assert_fails('call feedkeys("/def/\<cr>", "xt")', 'E486:') 234 call assert_fails('call feedkeys("?\<up>\<cr>", "xt")', 'E486:') 235 call assert_equal('def?', histget('/', -1)) 236 237 call assert_fails('call feedkeys("/ghi?\<cr>", "xt")', 'E486:') 238 call assert_fails('call feedkeys("?\<up>\<cr>", "xt")', 'E486:') 239 call assert_equal('ghi\?', histget('/', -1)) 240 241 set history& 242endfunc 243 244" Test for making sure the key value is not stored in history 245func Test_history_crypt_key() 246 CheckFeature cryptv 247 call feedkeys(":set bs=2 key=abc ts=8\<CR>", 'xt') 248 call assert_equal('set bs=2 key= ts=8', histget(':')) 249 set key& bs& ts& 250endfunc 251 252" vim: shiftwidth=2 sts=2 expandtab 253