blob: 19490f252856b6dcf6e912910a2ca3c1e4f0c3aa [file] [log] [blame]
Bram Moolenaar119d4692016-03-05 21:21:24 +01001" Tests for the history functions
2
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
4CheckFeature cmdline_hist
Bram Moolenaar119d4692016-03-05 21:21:24 +01005
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
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +020015 call assert_true('ls'->histadd(a:hist))
Bram Moolenaar119d4692016-03-05 21:21:24 +010016 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))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +020024 call assert_false(a:hist->histdel(7))
Bram Moolenaar119d4692016-03-05 21:21:24 +010025 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'))
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +020030 call assert_equal('ls', a:hist->histget(-1))
31 call assert_equal(4, a:hist->histnr())
Bram Moolenaar119d4692016-03-05 21:21:24 +010032
Bram Moolenaareebd84e2016-12-01 17:57:44 +010033 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
Bram Moolenaar119d4692016-03-05 21:21:24 +010057 " 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
Bram Moolenaar578fe942020-02-27 21:32:51 +010073
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))
Bram Moolenaar119d4692016-03-05 21:21:24 +010081endfunction
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:')
Bram Moolenaar8d588cc2020-02-25 21:47:45 +010096 call assert_fails('history xyz', 'E488:')
97 call assert_fails('history ,abc', 'E488:')
Bram Moolenaar531be472020-09-23 22:38:05 +020098 call assert_fails('call histdel(":", "\\%(")', 'E53:')
Bram Moolenaar119d4692016-03-05 21:21:24 +010099endfunction
Bram Moolenaar1d669c22017-01-11 22:40:19 +0100100
Dominique Pelled176ca32021-09-09 20:45:34 +0200101function 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
Bram Moolenaar1d669c22017-01-11 22:40:19 +0100117function 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
Bram Moolenaarb513d302018-12-02 14:55:08 +0100133
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100134" Test for :history command option completion
Bram Moolenaarb513d302018-12-02 14:55:08 +0100135function 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
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100139
140" Test for increasing the 'history' option value
141func Test_history_size()
142 let save_histsz = &history
Bram Moolenaar578fe942020-02-27 21:32:51 +0100143 set history=10
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100144 call histadd(':', 'ls')
145 call histdel(':')
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100146 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
Bram Moolenaar578fe942020-02-27 21:32:51 +0100152 set history=15
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100153 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
Bram Moolenaar578fe942020-02-27 21:32:51 +0100167 " 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
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100176 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
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100195
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100196 " Search for a pattern that is not present in the history
197 call assert_beeps('call feedkeys("/a1b2\<Up>\<CR>", "xt")')
198
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100199 " 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&
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100205
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&
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100242endfunc
243
Bram Moolenaar578fe942020-02-27 21:32:51 +0100244" Test for making sure the key value is not stored in history
245func Test_history_crypt_key()
246 CheckFeature cryptv
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +0200247
Bram Moolenaar578fe942020-02-27 21:32:51 +0100248 call feedkeys(":set bs=2 key=abc ts=8\<CR>", 'xt')
249 call assert_equal('set bs=2 key= ts=8', histget(':'))
Yee Cheng Chin6ee7b522023-10-01 09:13:22 +0200250
251 call assert_fails("call feedkeys(':set bs=2 key-=abc ts=8\<CR>', 'xt')")
252 call assert_equal('set bs=2 key-= ts=8', histget(':'))
253
Bram Moolenaar578fe942020-02-27 21:32:51 +0100254 set key& bs& ts&
255endfunc
256
zeertzjqc029c132024-03-28 11:37:26 +0100257" The following used to overflow and causing a use-after-free
Christian Brabandt9198c1f2023-10-26 21:29:32 +0200258func Test_history_max_val()
259
260 set history=10
261 call assert_fails(':history 2147483648', 'E1510:')
262 set history&
263endfunc
264
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100265" vim: shiftwidth=2 sts=2 expandtab