blob: 2d53474bb38467a522d54066bdd9c64eb8af2950 [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:')
88endfunction
Bram Moolenaar1d669c22017-01-11 22:40:19 +010089
90function Test_Search_history_window()
91 new
92 call setline(1, ['a', 'b', 'a', 'b'])
93 1
94 call feedkeys("/a\<CR>", 'xt')
95 call assert_equal('a', getline('.'))
96 1
97 call feedkeys("/b\<CR>", 'xt')
98 call assert_equal('b', getline('.'))
99 1
100 " select the previous /a command
101 call feedkeys("q/kk\<CR>", 'x!')
102 call assert_equal('a', getline('.'))
103 call assert_equal('a', @/)
104 bwipe!
105endfunc
Bram Moolenaarb513d302018-12-02 14:55:08 +0100106
107function Test_history_completion()
108 call feedkeys(":history \<C-A>\<C-B>\"\<CR>", 'tx')
109 call assert_equal('"history / : = > ? @ all cmd debug expr input search', @:)
110endfunc