blob: b12eef008e8ac68a351d07c9d9305ad7667d32a5 [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
73endfunction
74
75function Test_History()
76 for h in ['cmd', ':', '', 'search', '/', '?', 'expr', '=', 'input', '@', 'debug', '>']
77 call History_Tests(h)
78 endfor
79
80 " Negative tests
81 call assert_false(histdel('abc'))
82 call assert_equal('', histget('abc'))
83 call assert_fails('call histdel([])', 'E730:')
84 call assert_equal('', histget(10))
85 call assert_fails('call histget([])', 'E730:')
86 call assert_equal(-1, histnr('abc'))
87 call assert_fails('call histnr([])', 'E730:')
Bram Moolenaar8d588cc2020-02-25 21:47:45 +010088 call assert_fails('history xyz', 'E488:')
89 call assert_fails('history ,abc', 'E488:')
Bram Moolenaar119d4692016-03-05 21:21:24 +010090endfunction
Bram Moolenaar1d669c22017-01-11 22:40:19 +010091
92function Test_Search_history_window()
93 new
94 call setline(1, ['a', 'b', 'a', 'b'])
95 1
96 call feedkeys("/a\<CR>", 'xt')
97 call assert_equal('a', getline('.'))
98 1
99 call feedkeys("/b\<CR>", 'xt')
100 call assert_equal('b', getline('.'))
101 1
102 " select the previous /a command
103 call feedkeys("q/kk\<CR>", 'x!')
104 call assert_equal('a', getline('.'))
105 call assert_equal('a', @/)
106 bwipe!
107endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +0100108
109function Test_history_completion()
110 call feedkeys(":history \<C-A>\<C-B>\"\<CR>", 'tx')
111 call assert_equal('"history / : = > ? @ all cmd debug expr input search', @:)
112endfunc
Bram Moolenaar8d588cc2020-02-25 21:47:45 +0100113
114" Test for increasing the 'history' option value
115func Test_history_size()
116 let save_histsz = &history
117 call histdel(':')
118 set history=5
119 for i in range(1, 5)
120 call histadd(':', 'cmd' .. i)
121 endfor
122 call assert_equal(5, histnr(':'))
123 call assert_equal('cmd5', histget(':', -1))
124
125 set history=10
126 for i in range(6, 10)
127 call histadd(':', 'cmd' .. i)
128 endfor
129 call assert_equal(10, histnr(':'))
130 call assert_equal('cmd1', histget(':', 1))
131 call assert_equal('cmd10', histget(':', -1))
132
133 set history=5
134 call histadd(':', 'abc')
135 call assert_equal('', histget(':', 6))
136 call assert_equal('', histget(':', 12))
137 call assert_equal('cmd7', histget(':', 7))
138 call assert_equal('abc', histget(':', -1))
139
140 let &history=save_histsz
141endfunc
142
143" Test for recalling old search patterns in /
144func Test_history_search()
145 call histdel('/')
146 let g:pat = []
147 func SavePat()
148 call add(g:pat, getcmdline())
149 return ''
150 endfunc
151 cnoremap <F2> <C-\>eSavePat()<CR>
152 call histadd('/', 'pat1')
153 call histadd('/', 'pat2')
154 let @/ = ''
155 call feedkeys("/\<Up>\<F2>\<Up>\<F2>\<Down>\<Down>\<F2>\<Esc>", 'xt')
156 call assert_equal(['pat2', 'pat1', ''], g:pat)
157 cunmap <F2>
158 delfunc SavePat
159endfunc
160
161" vim: shiftwidth=2 sts=2 expandtab