Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 1 | " Tests for the history functions |
| 2 | |
Bram Moolenaar | b46fecd | 2019-06-15 17:58:09 +0200 | [diff] [blame] | 3 | source check.vim |
| 4 | CheckFeature cmdline_hist |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 5 | |
| 6 | set history=7 |
| 7 | |
| 8 | function 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 Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 15 | call assert_true('ls'->histadd(a:hist)) |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 16 | 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 Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 24 | call assert_false(a:hist->histdel(7)) |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 25 | 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 Moolenaar | f9f24ce | 2019-08-31 21:17:39 +0200 | [diff] [blame] | 30 | call assert_equal('ls', a:hist->histget(-1)) |
| 31 | call assert_equal(4, a:hist->histnr()) |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 32 | |
Bram Moolenaar | eebd84e | 2016-12-01 17:57:44 +0100 | [diff] [blame] | 33 | 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 Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 57 | " 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 Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 73 | |
| 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 Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 81 | endfunction |
| 82 | |
| 83 | function 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 Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 96 | call assert_fails('history xyz', 'E488:') |
| 97 | call assert_fails('history ,abc', 'E488:') |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 98 | endfunction |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 99 | |
| 100 | function 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! |
| 115 | endfunc |
Bram Moolenaar | b513d30 | 2018-12-02 14:55:08 +0100 | [diff] [blame] | 116 | |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame^] | 117 | " Test for :history command option completion |
Bram Moolenaar | b513d30 | 2018-12-02 14:55:08 +0100 | [diff] [blame] | 118 | function Test_history_completion() |
| 119 | call feedkeys(":history \<C-A>\<C-B>\"\<CR>", 'tx') |
| 120 | call assert_equal('"history / : = > ? @ all cmd debug expr input search', @:) |
| 121 | endfunc |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 122 | |
| 123 | " Test for increasing the 'history' option value |
| 124 | func Test_history_size() |
| 125 | let save_histsz = &history |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 126 | set history=10 |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame^] | 127 | call histadd(':', 'ls') |
| 128 | call histdel(':') |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 129 | for i in range(1, 5) |
| 130 | call histadd(':', 'cmd' .. i) |
| 131 | endfor |
| 132 | call assert_equal(5, histnr(':')) |
| 133 | call assert_equal('cmd5', histget(':', -1)) |
| 134 | |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 135 | set history=15 |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 136 | for i in range(6, 10) |
| 137 | call histadd(':', 'cmd' .. i) |
| 138 | endfor |
| 139 | call assert_equal(10, histnr(':')) |
| 140 | call assert_equal('cmd1', histget(':', 1)) |
| 141 | call assert_equal('cmd10', histget(':', -1)) |
| 142 | |
| 143 | set history=5 |
| 144 | call histadd(':', 'abc') |
| 145 | call assert_equal('', histget(':', 6)) |
| 146 | call assert_equal('', histget(':', 12)) |
| 147 | call assert_equal('cmd7', histget(':', 7)) |
| 148 | call assert_equal('abc', histget(':', -1)) |
| 149 | |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 150 | " This test works only when the language is English |
| 151 | if v:lang == "C" || v:lang =~ '^[Ee]n' |
| 152 | set history=0 |
| 153 | redir => v |
| 154 | call feedkeys(":history\<CR>", 'xt') |
| 155 | redir END |
| 156 | call assert_equal(["'history' option is zero"], split(v, "\n")) |
| 157 | endif |
| 158 | |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 159 | let &history=save_histsz |
| 160 | endfunc |
| 161 | |
| 162 | " Test for recalling old search patterns in / |
| 163 | func Test_history_search() |
| 164 | call histdel('/') |
| 165 | let g:pat = [] |
| 166 | func SavePat() |
| 167 | call add(g:pat, getcmdline()) |
| 168 | return '' |
| 169 | endfunc |
| 170 | cnoremap <F2> <C-\>eSavePat()<CR> |
| 171 | call histadd('/', 'pat1') |
| 172 | call histadd('/', 'pat2') |
| 173 | let @/ = '' |
| 174 | call feedkeys("/\<Up>\<F2>\<Up>\<F2>\<Down>\<Down>\<F2>\<Esc>", 'xt') |
| 175 | call assert_equal(['pat2', 'pat1', ''], g:pat) |
| 176 | cunmap <F2> |
| 177 | delfunc SavePat |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame^] | 178 | |
| 179 | " Recall patterns with 'history' set to 0 |
| 180 | set history=0 |
| 181 | let @/ = 'abc' |
| 182 | let cmd = 'call feedkeys("/\<Up>\<Down>\<S-Up>\<S-Down>\<CR>", "xt")' |
| 183 | call assert_fails(cmd, 'E486:') |
| 184 | set history& |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 185 | endfunc |
| 186 | |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 187 | " Test for making sure the key value is not stored in history |
| 188 | func Test_history_crypt_key() |
| 189 | CheckFeature cryptv |
| 190 | call feedkeys(":set bs=2 key=abc ts=8\<CR>", 'xt') |
| 191 | call assert_equal('set bs=2 key= ts=8', histget(':')) |
| 192 | set key& bs& ts& |
| 193 | endfunc |
| 194 | |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 195 | " vim: shiftwidth=2 sts=2 expandtab |