blob: 1bea78d5a8e0a4989560982321cd2f2eb6f94acc [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
101function Test_Search_history_window()
102 new
103 call setline(1, ['a', 'b', 'a', 'b'])
104 1
105 call feedkeys("/a\<CR>", 'xt')
106 call assert_equal('a', getline('.'))
107 1
108 call feedkeys("/b\<CR>", 'xt')
109 call assert_equal('b', getline('.'))
110 1
111 " select the previous /a command
112 call feedkeys("q/kk\<CR>", 'x!')
113 call assert_equal('a', getline('.'))
114 call assert_equal('a', @/)
115 bwipe!
116endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +0100117
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100118" Test for :history command option completion
Bram Moolenaarb513d302018-12-02 14:55:08 +0100119function Test_history_completion()
120 call feedkeys(":history \<C-A>\<C-B>\"\<CR>", 'tx')
121 call assert_equal('"history / : = > ? @ all cmd debug expr input search', @:)
122endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100123
124" Test for increasing the 'history' option value
125func Test_history_size()
126 let save_histsz = &history
Bram Moolenaar578fe942020-02-27 21:32:51 +0100127 set history=10
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100128 call histadd(':', 'ls')
129 call histdel(':')
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100130 for i in range(1, 5)
131 call histadd(':', 'cmd' .. i)
132 endfor
133 call assert_equal(5, histnr(':'))
134 call assert_equal('cmd5', histget(':', -1))
135
Bram Moolenaar578fe942020-02-27 21:32:51 +0100136 set history=15
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100137 for i in range(6, 10)
138 call histadd(':', 'cmd' .. i)
139 endfor
140 call assert_equal(10, histnr(':'))
141 call assert_equal('cmd1', histget(':', 1))
142 call assert_equal('cmd10', histget(':', -1))
143
144 set history=5
145 call histadd(':', 'abc')
146 call assert_equal('', histget(':', 6))
147 call assert_equal('', histget(':', 12))
148 call assert_equal('cmd7', histget(':', 7))
149 call assert_equal('abc', histget(':', -1))
150
Bram Moolenaar578fe942020-02-27 21:32:51 +0100151 " This test works only when the language is English
152 if v:lang == "C" || v:lang =~ '^[Ee]n'
153 set history=0
154 redir => v
155 call feedkeys(":history\<CR>", 'xt')
156 redir END
157 call assert_equal(["'history' option is zero"], split(v, "\n"))
158 endif
159
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100160 let &history=save_histsz
161endfunc
162
163" Test for recalling old search patterns in /
164func Test_history_search()
165 call histdel('/')
166 let g:pat = []
167 func SavePat()
168 call add(g:pat, getcmdline())
169 return ''
170 endfunc
171 cnoremap <F2> <C-\>eSavePat()<CR>
172 call histadd('/', 'pat1')
173 call histadd('/', 'pat2')
174 let @/ = ''
175 call feedkeys("/\<Up>\<F2>\<Up>\<F2>\<Down>\<Down>\<F2>\<Esc>", 'xt')
176 call assert_equal(['pat2', 'pat1', ''], g:pat)
177 cunmap <F2>
178 delfunc SavePat
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100179
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100180 " Search for a pattern that is not present in the history
181 call assert_beeps('call feedkeys("/a1b2\<Up>\<CR>", "xt")')
182
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100183 " Recall patterns with 'history' set to 0
184 set history=0
185 let @/ = 'abc'
186 let cmd = 'call feedkeys("/\<Up>\<Down>\<S-Up>\<S-Down>\<CR>", "xt")'
187 call assert_fails(cmd, 'E486:')
188 set history&
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100189
190 " Recall patterns till the end of history
191 set history=4
192 call histadd('/', 'pat')
193 call histdel('/')
194 call histadd('/', 'pat1')
195 call histadd('/', 'pat2')
196 call assert_beeps('call feedkeys("/\<Up>\<Up>\<Up>\<C-U>\<cr>", "xt")')
197 call assert_beeps('call feedkeys("/\<Down><cr>", "xt")')
198
199 " Test for wrapping around the history list
200 for i in range(3, 7)
201 call histadd('/', 'pat' .. i)
202 endfor
203 let upcmd = "\<up>\<up>\<up>\<up>\<up>"
204 let downcmd = "\<down>\<down>\<down>\<down>\<down>"
205 try
206 call feedkeys("/" .. upcmd .. "\<cr>", 'xt')
207 catch /E486:/
208 endtry
209 call assert_equal('pat4', @/)
210 try
211 call feedkeys("/" .. upcmd .. downcmd .. "\<cr>", 'xt')
212 catch /E486:/
213 endtry
214 call assert_equal('pat4', @/)
215
216 " Test for changing the search command separator in the history
217 call assert_fails('call feedkeys("/def/\<cr>", "xt")', 'E486:')
218 call assert_fails('call feedkeys("?\<up>\<cr>", "xt")', 'E486:')
219 call assert_equal('def?', histget('/', -1))
220
221 call assert_fails('call feedkeys("/ghi?\<cr>", "xt")', 'E486:')
222 call assert_fails('call feedkeys("?\<up>\<cr>", "xt")', 'E486:')
223 call assert_equal('ghi\?', histget('/', -1))
224
225 set history&
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100226endfunc
227
Bram Moolenaar578fe942020-02-27 21:32:51 +0100228" Test for making sure the key value is not stored in history
229func Test_history_crypt_key()
230 CheckFeature cryptv
231 call feedkeys(":set bs=2 key=abc ts=8\<CR>", 'xt')
232 call assert_equal('set bs=2 key= ts=8', histget(':'))
233 set key& bs& ts&
234endfunc
235
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100236" vim: shiftwidth=2 sts=2 expandtab