test_history.vim 10 KB

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