Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 1 | " Tests for the history functions |
| 2 | |
| 3 | if !has('cmdline_hist') |
| 4 | finish |
| 5 | endif |
| 6 | |
| 7 | set history=7 |
| 8 | |
| 9 | function History_Tests(hist) |
| 10 | " First clear the history |
| 11 | call histadd(a:hist, 'dummy') |
| 12 | call assert_true(histdel(a:hist)) |
| 13 | call assert_equal(-1, histnr(a:hist)) |
| 14 | call assert_equal('', histget(a:hist)) |
| 15 | |
| 16 | call assert_true(histadd(a:hist, 'ls')) |
| 17 | call assert_true(histadd(a:hist, 'buffers')) |
| 18 | call assert_equal('buffers', histget(a:hist)) |
| 19 | call assert_equal('ls', histget(a:hist, -2)) |
| 20 | call assert_equal('ls', histget(a:hist, 1)) |
| 21 | call assert_equal('', histget(a:hist, 5)) |
| 22 | call assert_equal('', histget(a:hist, -5)) |
| 23 | call assert_equal(2, histnr(a:hist)) |
| 24 | call assert_true(histdel(a:hist, 2)) |
| 25 | call assert_false(histdel(a:hist, 7)) |
| 26 | call assert_equal(1, histnr(a:hist)) |
| 27 | call assert_equal('ls', histget(a:hist, -1)) |
| 28 | |
| 29 | call assert_true(histadd(a:hist, 'buffers')) |
| 30 | call assert_true(histadd(a:hist, 'ls')) |
| 31 | call assert_equal('ls', histget(a:hist, -1)) |
| 32 | call assert_equal(4, histnr(a:hist)) |
| 33 | |
Bram Moolenaar | eebd84e | 2016-12-01 17:57:44 +0100 | [diff] [blame] | 34 | let a=execute('history ' . a:hist) |
| 35 | call assert_match("^\n # \\S* history\n 3 buffers\n> 4 ls$", a) |
| 36 | let a=execute('history all') |
| 37 | call assert_match("^\n # .* history\n 3 buffers\n> 4 ls", a) |
| 38 | |
| 39 | if len(a:hist) > 0 |
| 40 | let a=execute('history ' . a:hist . ' 2') |
| 41 | call assert_match("^\n # \\S* history$", a) |
| 42 | let a=execute('history ' . a:hist . ' 3') |
| 43 | call assert_match("^\n # \\S* history\n 3 buffers$", a) |
| 44 | let a=execute('history ' . a:hist . ' 4') |
| 45 | call assert_match("^\n # \\S* history\n> 4 ls$", a) |
| 46 | let a=execute('history ' . a:hist . ' 3,4') |
| 47 | call assert_match("^\n # \\S* history\n 3 buffers\n> 4 ls$", a) |
| 48 | let a=execute('history ' . a:hist . ' -1') |
| 49 | call assert_match("^\n # \\S* history\n> 4 ls$", a) |
| 50 | let a=execute('history ' . a:hist . ' -2') |
| 51 | call assert_match("^\n # \\S* history\n 3 buffers$", a) |
| 52 | let a=execute('history ' . a:hist . ' -2,') |
| 53 | call assert_match("^\n # \\S* history\n 3 buffers\n> 4 ls$", a) |
| 54 | let a=execute('history ' . a:hist . ' -3') |
| 55 | call assert_match("^\n # \\S* history$", a) |
| 56 | endif |
| 57 | |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 58 | " Test for removing entries matching a pattern |
| 59 | for i in range(1, 3) |
| 60 | call histadd(a:hist, 'text_' . i) |
| 61 | endfor |
| 62 | call assert_true(histdel(a:hist, 'text_\d\+')) |
| 63 | call assert_equal('ls', histget(a:hist, -1)) |
| 64 | |
| 65 | " Test for freeing the entire history list |
| 66 | for i in range(1, 7) |
| 67 | call histadd(a:hist, 'text_' . i) |
| 68 | endfor |
| 69 | call histdel(a:hist) |
| 70 | for i in range(1, 7) |
| 71 | call assert_equal('', histget(a:hist, i)) |
| 72 | call assert_equal('', histget(a:hist, i - 7 - 1)) |
| 73 | endfor |
| 74 | endfunction |
| 75 | |
| 76 | function Test_History() |
| 77 | for h in ['cmd', ':', '', 'search', '/', '?', 'expr', '=', 'input', '@', 'debug', '>'] |
| 78 | call History_Tests(h) |
| 79 | endfor |
| 80 | |
| 81 | " Negative tests |
| 82 | call assert_false(histdel('abc')) |
| 83 | call assert_equal('', histget('abc')) |
| 84 | call assert_fails('call histdel([])', 'E730:') |
| 85 | call assert_equal('', histget(10)) |
| 86 | call assert_fails('call histget([])', 'E730:') |
| 87 | call assert_equal(-1, histnr('abc')) |
| 88 | call assert_fails('call histnr([])', 'E730:') |
| 89 | endfunction |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 90 | |
| 91 | function Test_Search_history_window() |
| 92 | new |
| 93 | call setline(1, ['a', 'b', 'a', 'b']) |
| 94 | 1 |
| 95 | call feedkeys("/a\<CR>", 'xt') |
| 96 | call assert_equal('a', getline('.')) |
| 97 | 1 |
| 98 | call feedkeys("/b\<CR>", 'xt') |
| 99 | call assert_equal('b', getline('.')) |
| 100 | 1 |
| 101 | " select the previous /a command |
| 102 | call feedkeys("q/kk\<CR>", 'x!') |
| 103 | call assert_equal('a', getline('.')) |
| 104 | call assert_equal('a', @/) |
| 105 | bwipe! |
| 106 | endfunc |
Bram Moolenaar | b513d30 | 2018-12-02 14:55:08 +0100 | [diff] [blame^] | 107 | |
| 108 | function Test_history_completion() |
| 109 | call feedkeys(":history \<C-A>\<C-B>\"\<CR>", 'tx') |
| 110 | call assert_equal('"history / : = > ? @ all cmd debug expr input search', @:) |
| 111 | endfunc |