blob: 96006ac7e797117b5d14a65613c1424ef8d9ddce [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 Moolenaar119d4692016-03-05 21:21:24 +010098endfunction
Bram Moolenaar1d669c22017-01-11 22:40:19 +010099
100function Test_Search_history_window()
101 new
102 call setline(1, ['a', 'b', 'a', 'b'])
103 1
104 call feedkeys("/a\<CR>", 'xt')
105 call assert_equal('a', getline('.'))
106 1
107 call feedkeys("/b\<CR>", 'xt')
108 call assert_equal('b', getline('.'))
109 1
110 " select the previous /a command
111 call feedkeys("q/kk\<CR>", 'x!')
112 call assert_equal('a', getline('.'))
113 call assert_equal('a', @/)
114 bwipe!
115endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +0100116
117function Test_history_completion()
118 call feedkeys(":history \<C-A>\<C-B>\"\<CR>", 'tx')
119 call assert_equal('"history / : = > ? @ all cmd debug expr input search', @:)
120endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100121
122" Test for increasing the 'history' option value
123func Test_history_size()
124 let save_histsz = &history
125 call histdel(':')
Bram Moolenaar578fe942020-02-27 21:32:51 +0100126 set history=10
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100127 for i in range(1, 5)
128 call histadd(':', 'cmd' .. i)
129 endfor
130 call assert_equal(5, histnr(':'))
131 call assert_equal('cmd5', histget(':', -1))
132
Bram Moolenaar578fe942020-02-27 21:32:51 +0100133 set history=15
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100134 for i in range(6, 10)
135 call histadd(':', 'cmd' .. i)
136 endfor
137 call assert_equal(10, histnr(':'))
138 call assert_equal('cmd1', histget(':', 1))
139 call assert_equal('cmd10', histget(':', -1))
140
141 set history=5
142 call histadd(':', 'abc')
143 call assert_equal('', histget(':', 6))
144 call assert_equal('', histget(':', 12))
145 call assert_equal('cmd7', histget(':', 7))
146 call assert_equal('abc', histget(':', -1))
147
Bram Moolenaar578fe942020-02-27 21:32:51 +0100148 " This test works only when the language is English
149 if v:lang == "C" || v:lang =~ '^[Ee]n'
150 set history=0
151 redir => v
152 call feedkeys(":history\<CR>", 'xt')
153 redir END
154 call assert_equal(["'history' option is zero"], split(v, "\n"))
155 endif
156
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100157 let &history=save_histsz
158endfunc
159
160" Test for recalling old search patterns in /
161func Test_history_search()
162 call histdel('/')
163 let g:pat = []
164 func SavePat()
165 call add(g:pat, getcmdline())
166 return ''
167 endfunc
168 cnoremap <F2> <C-\>eSavePat()<CR>
169 call histadd('/', 'pat1')
170 call histadd('/', 'pat2')
171 let @/ = ''
172 call feedkeys("/\<Up>\<F2>\<Up>\<F2>\<Down>\<Down>\<F2>\<Esc>", 'xt')
173 call assert_equal(['pat2', 'pat1', ''], g:pat)
174 cunmap <F2>
175 delfunc SavePat
176endfunc
177
Bram Moolenaar578fe942020-02-27 21:32:51 +0100178" Test for making sure the key value is not stored in history
179func Test_history_crypt_key()
180 CheckFeature cryptv
181 call feedkeys(":set bs=2 key=abc ts=8\<CR>", 'xt')
182 call assert_equal('set bs=2 key= ts=8', histget(':'))
183 set key& bs& ts&
184endfunc
185
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100186" vim: shiftwidth=2 sts=2 expandtab