blob: dac153d863654e9d495c4c14f512d1b8200f49bd [file] [log] [blame]
Bram Moolenaar343b8c02017-02-17 12:04:56 +01001" Tests for :help
2
Christian Brabandteb380b92025-07-07 20:53:55 +02003import './util/vim9.vim' as v9
Bram Moolenaar17709e22021-03-19 14:38:12 +01004
Bram Moolenaar343b8c02017-02-17 12:04:56 +01005func Test_help_restore_snapshot()
6 help
7 set buftype=
8 help
9 edit x
10 help
11 helpclose
12endfunc
Bram Moolenaar751ba612017-03-16 22:26:44 +010013
LemonBoy2a2707d2022-05-04 22:13:47 +010014func Test_help_restore_snapshot_split()
15 " Squeeze the unnamed buffer, Xfoo and the help one side-by-side and focus
16 " the first one before calling :help.
17 let bnr = bufnr()
18 botright vsp Xfoo
19 wincmd h
20 help
21 wincmd L
22 let g:did_bufenter = v:false
23 augroup T
24 au!
25 au BufEnter Xfoo let g:did_bufenter = v:true
26 augroup END
27 helpclose
28 augroup! T
29 " We're back to the unnamed buffer.
30 call assert_equal(bnr, bufnr())
31 " No BufEnter was triggered for Xfoo.
32 call assert_equal(v:false, g:did_bufenter)
33
34 close!
35 bwipe!
36endfunc
37
Bram Moolenaar751ba612017-03-16 22:26:44 +010038func Test_help_errors()
39 call assert_fails('help doesnotexist', 'E149:')
40 call assert_fails('help!', 'E478:')
Bram Moolenaarea3db912020-02-02 15:32:13 +010041 if has('multi_lang')
42 call assert_fails('help help@xy', 'E661:')
43 endif
44
45 let save_hf = &helpfile
46 set helpfile=help_missing
47 help
48 call assert_equal(1, winnr('$'))
49 call assert_notequal('help', &buftype)
50 let &helpfile = save_hf
51
52 call assert_fails('help ' . repeat('a', 1048), 'E149:')
Bram Moolenaara4f99f52017-08-26 16:25:32 +020053
54 new
55 set keywordprg=:help
56 call setline(1, " ")
57 call assert_fails('normal VK', 'E349:')
58 bwipe!
59endfunc
60
Bram Moolenaar9ca25082019-10-05 11:30:09 +020061func Test_help_expr()
62 help expr-!~?
63 call assert_equal('eval.txt', expand('%:t'))
64 close
65endfunc
66
Bram Moolenaara4f99f52017-08-26 16:25:32 +020067func Test_help_keyword()
68 new
69 set keywordprg=:help
70 call setline(1, " Visual ")
71 normal VK
72 call assert_match('^Visual mode', getline('.'))
73 call assert_equal('help', &ft)
74 close
75 bwipe!
Bram Moolenaar751ba612017-03-16 22:26:44 +010076endfunc
Bram Moolenaar35c5e812017-12-09 21:10:13 +010077
78func Test_help_local_additions()
Bram Moolenaar572a4432022-09-28 21:07:03 +010079 call mkdir('Xruntime/doc', 'pR')
Bram Moolenaar35c5e812017-12-09 21:10:13 +010080 call writefile(['*mydoc.txt* my awesome doc'], 'Xruntime/doc/mydoc.txt')
81 call writefile(['*mydoc-ext.txt* my extended awesome doc'], 'Xruntime/doc/mydoc-ext.txt')
82 let rtp_save = &rtp
83 set rtp+=./Xruntime
h-east0e2508d2022-01-03 12:53:24 +000084 help local-additions
85 let lines = getline(line(".") + 1, search("^$") - 1)
86 call assert_equal([
87 \ '|mydoc-ext.txt| my extended awesome doc',
88 \ '|mydoc.txt| my awesome doc'
89 \ ], lines)
90 call delete('Xruntime/doc/mydoc-ext.txt')
91 close
92
Bram Moolenaar572a4432022-09-28 21:07:03 +010093 call mkdir('Xruntime-ja/doc', 'pR')
h-east0e2508d2022-01-03 12:53:24 +000094 call writefile(["local-additions\thelp.jax\t/*local-additions*"], 'Xruntime-ja/doc/tags-ja')
95 call writefile(['*help.txt* This is jax file', '',
96 \ 'LOCAL ADDITIONS: *local-additions*', ''], 'Xruntime-ja/doc/help.jax')
97 call writefile(['*work.txt* This is jax file'], 'Xruntime-ja/doc/work.jax')
98 call writefile(['*work2.txt* This is jax file'], 'Xruntime-ja/doc/work2.jax')
99 set rtp+=./Xruntime-ja
100
101 help local-additions@en
102 let lines = getline(line(".") + 1, search("^$") - 1)
103 call assert_equal([
104 \ '|mydoc.txt| my awesome doc'
105 \ ], lines)
106 close
107
108 help local-additions@ja
109 let lines = getline(line(".") + 1, search("^$") - 1)
110 call assert_equal([
111 \ '|mydoc.txt| my awesome doc',
112 \ '|help.txt| This is jax file',
113 \ '|work.txt| This is jax file',
114 \ '|work2.txt| This is jax file',
115 \ ], lines)
Bram Moolenaar35c5e812017-12-09 21:10:13 +0100116 close
117
Bram Moolenaar35c5e812017-12-09 21:10:13 +0100118 let &rtp = rtp_save
119endfunc
Bram Moolenaar297610b2019-12-27 17:20:55 +0100120
121func Test_help_completion()
122 call feedkeys(":help :undo\<C-A>\<C-B>\"\<CR>", 'tx')
123 call assert_equal('"help :undo :undoj :undol :undojoin :undolist', @:)
124endfunc
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100125
126" Test for the :helptags command
Bram Moolenaarf9a65502021-03-05 20:47:44 +0100127" NOTE: if you run tests as root this will fail. Don't run tests as root!
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100128func Test_helptag_cmd()
Bram Moolenaar572a4432022-09-28 21:07:03 +0100129 call mkdir('Xtagdir/a/doc', 'pR')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100130
131 " No help file to process in the directory
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100132 call assert_fails('helptags Xtagdir', 'E151:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100133
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100134 call writefile([], 'Xtagdir/a/doc/sample.txt')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100135
136 " Test for ++t argument
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100137 helptags ++t Xtagdir
138 call assert_equal(["help-tags\ttags\t1"], readfile('Xtagdir/tags'))
139 call delete('Xtagdir/tags')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100140
Carlo Teubnerddab3ce2022-07-30 12:03:16 +0100141 " Test parsing tags
142 call writefile(['*tag1*', 'Example: >', ' *notag*', 'Example end: *tag2*'],
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100143 \ 'Xtagdir/a/doc/sample.txt')
144 helptags Xtagdir
Carlo Teubnerddab3ce2022-07-30 12:03:16 +0100145 call assert_equal(["tag1\ta/doc/sample.txt\t/*tag1*",
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100146 \ "tag2\ta/doc/sample.txt\t/*tag2*"], readfile('Xtagdir/tags'))
Carlo Teubnerddab3ce2022-07-30 12:03:16 +0100147
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100148 " Duplicate tags in the help file
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100149 call writefile(['*tag1*', '*tag1*', '*tag2*'], 'Xtagdir/a/doc/sample.txt')
150 call assert_fails('helptags Xtagdir', 'E154:')
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100151endfunc
152
Bram Moolenaar17709e22021-03-19 14:38:12 +0100153func Test_helptag_cmd_readonly()
154 CheckUnix
155 CheckNotRoot
Bram Moolenaar17709e22021-03-19 14:38:12 +0100156
157 " Read-only tags file
Bram Moolenaar572a4432022-09-28 21:07:03 +0100158 call mkdir('Xrodir/doc', 'pR')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100159 call writefile([''], 'Xrodir/doc/tags')
160 call writefile([], 'Xrodir/doc/sample.txt')
161 call setfperm('Xrodir/doc/tags', 'r-xr--r--')
162 call assert_fails('helptags Xrodir/doc', 'E152:', getfperm('Xrodir/doc/tags'))
Bram Moolenaar17709e22021-03-19 14:38:12 +0100163
164 let rtp = &rtp
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100165 let &rtp = 'Xrodir'
Bram Moolenaar17709e22021-03-19 14:38:12 +0100166 helptags ALL
167 let &rtp = rtp
168
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100169 call delete('Xrodir/doc/tags')
Bram Moolenaar17709e22021-03-19 14:38:12 +0100170
171 " No permission to read the help file
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100172 call mkdir('Xrodir/b/doc', 'p')
173 call writefile([], 'Xrodir/b/doc/sample.txt')
174 call setfperm('Xrodir/b/doc/sample.txt', '-w-------')
175 call assert_fails('helptags Xrodir', 'E153:', getfperm('Xrodir/b/doc/sample.txt'))
Bram Moolenaar17709e22021-03-19 14:38:12 +0100176endfunc
177
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200178" Test for setting the 'helpheight' option in the help window
179func Test_help_window_height()
Bram Moolenaare4e1a1e2022-03-02 20:49:50 +0000180 let &cmdheight = &lines - 23
Yegappan Lakshmanan59585492021-06-12 13:46:41 +0200181 set helpheight=10
182 help
183 set helpheight=14
184 call assert_equal(14, winheight(0))
185 set helpheight& cmdheight=1
186 close
187endfunc
Bram Moolenaar17709e22021-03-19 14:38:12 +0100188
Bram Moolenaarbd228fd2021-11-25 10:50:12 +0000189func Test_help_long_argument()
190 try
191 exe 'help \%' .. repeat('0', 1021)
192 catch
193 call assert_match("E149:", v:exception)
194 endtry
195endfunc
196
Bram Moolenaar679d66c2022-01-30 16:42:56 +0000197func Test_help_using_visual_match()
198 let lines =<< trim END
199 call setline(1, ' ')
200 /^
201 exe "normal \<C-V>\<C-V>"
202 h5\%V€]
203 END
204 call v9.CheckScriptFailure(lines, 'E149:')
205endfunc
206
Phạm Bình An6af20a92025-05-01 17:30:58 +0200207func Test_helptag_navigation()
208 let helpdir = tempname()
209 let tempfile = helpdir . '/test.txt'
210 call mkdir(helpdir, 'pR')
211 call writefile(['', '*[tag*', '', '|[tag|'], tempfile)
212 exe 'helptags' helpdir
213 exe 'sp' tempfile
214 exe 'lcd' helpdir
215 setl ft=help
216 let &l:iskeyword='!-~,^*,^|,^",192-255'
217 call cursor(4, 2)
218 " Vim must not escape `[` when expanding the tag
219 exe "normal! \<C-]>"
220 call assert_equal(2, line('.'))
221 bw
222endfunc
223
Bram Moolenaarbd228fd2021-11-25 10:50:12 +0000224
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100225" vim: shiftwidth=2 sts=2 expandtab