xref: /vim-8.2.3635/src/testdir/test_taglist.vim (revision e2e4075f)
1" test taglist(), tagfiles() functions and :tags command
2
3source check.vim
4source view_util.vim
5
6func Test_taglist()
7  call writefile([
8	\ "FFoo\tXfoo\t1",
9	\ "FBar\tXfoo\t2",
10	\ "BFoo\tXbar\t1",
11	\ "BBar\tXbar\t2",
12	\ "Kindly\tXbar\t3;\"\tv\tfile:",
13	\ "Lambda\tXbar\t3;\"\tλ\tfile:",
14	\ "Command\tXbar\tcall cursor(3, 4)|;\"\td",
15	\ ], 'Xtags')
16  set tags=Xtags
17  split Xtext
18
19  call assert_equal(['FFoo', 'BFoo'], map(taglist("Foo"), {i, v -> v.name}))
20  call assert_equal(['FFoo', 'BFoo'], map("Foo"->taglist("Xtext"), {i, v -> v.name}))
21  call assert_equal(['FFoo', 'BFoo'], map(taglist("Foo", "Xfoo"), {i, v -> v.name}))
22  call assert_equal(['BFoo', 'FFoo'], map(taglist("Foo", "Xbar"), {i, v -> v.name}))
23
24  let kindly = taglist("Kindly")
25  call assert_equal(1, len(kindly))
26  call assert_equal('v', kindly[0]['kind'])
27  call assert_equal('3', kindly[0]['cmd'])
28  call assert_equal(1, kindly[0]['static'])
29  call assert_equal('Xbar', kindly[0]['filename'])
30
31  let lambda = taglist("Lambda")
32  call assert_equal(1, len(lambda))
33  call assert_equal('λ', lambda[0]['kind'])
34
35  let cmd = taglist("Command")
36  call assert_equal(1, len(cmd))
37  call assert_equal('d', cmd[0]['kind'])
38  call assert_equal('call cursor(3, 4)', cmd[0]['cmd'])
39
40  call assert_fails("let l=taglist([])", 'E730:')
41
42  call delete('Xtags')
43  set tags&
44  bwipe
45endfunc
46
47func Test_taglist_native_etags()
48  CheckFeature emacs_tags
49
50  call writefile([
51	\ "\x0c",
52	\ "src/os_unix.c,13491",
53	\ "set_signals(\x7f1335,32699",
54	\ "reset_signals(\x7f1407,34136",
55	\ ], 'Xtags')
56
57  set tags=Xtags
58
59  call assert_equal([['set_signals', '1335,32699'], ['reset_signals', '1407,34136']],
60	\ map(taglist('set_signals'), {i, v -> [v.name, v.cmd]}))
61
62  call delete('Xtags')
63  set tags&
64endfunc
65
66func Test_taglist_ctags_etags()
67  CheckFeature emacs_tags
68
69  call writefile([
70	\ "\x0c",
71	\ "src/os_unix.c,13491",
72	\ "set_signals(void)\x7fset_signals\x011335,32699",
73	\ "reset_signals(void)\x7freset_signals\x011407,34136",
74	\ ], 'Xtags')
75
76  set tags=Xtags
77
78  call assert_equal([['set_signals', '1335,32699'], ['reset_signals', '1407,34136']],
79	\ map(taglist('set_signals'), {i, v -> [v.name, v.cmd]}))
80
81  call delete('Xtags')
82  set tags&
83endfunc
84
85func Test_tags_too_long()
86  call assert_fails('tag ' . repeat('x', 1020), ['E433:', 'E426:'])
87  tags
88endfunc
89
90func Test_tagfiles()
91  call assert_equal([], tagfiles())
92
93  call writefile(["FFoo\tXfoo\t1"], 'Xtags1')
94  call writefile(["FBar\tXbar\t1"], 'Xtags2')
95  set tags=Xtags1,Xtags2
96  call assert_equal(['Xtags1', 'Xtags2'], tagfiles())
97
98  help
99  let tf = tagfiles()
100  call assert_equal(1, len(tf))
101  call assert_equal(fnamemodify(expand('$VIMRUNTIME/doc/tags'), ':p:gs?\\?/?'),
102	\           fnamemodify(tf[0], ':p:gs?\\?/?'))
103  helpclose
104  call assert_equal(['Xtags1', 'Xtags2'], tagfiles())
105  set tags&
106  call assert_equal([], tagfiles())
107
108  call delete('Xtags1')
109  call delete('Xtags2')
110  bd
111endfunc
112
113" For historical reasons we support a tags file where the last line is missing
114" the newline.
115func Test_tagsfile_without_trailing_newline()
116  call writefile(["Foo\tfoo\t1"], 'Xtags', 'b')
117  set tags=Xtags
118
119  let tl = taglist('.*')
120  call assert_equal(1, len(tl))
121  call assert_equal('Foo', tl[0].name)
122
123  call delete('Xtags')
124  set tags&
125endfunc
126
127" Test for ignoring comments in a tags file
128func Test_tagfile_ignore_comments()
129  call writefile([
130	\ "!_TAG_PROGRAM_NAME	/Test tags generator/",
131	\ "FBar\tXfoo\t2" .. ';"' .. "\textrafield\tf",
132	\ "!_TAG_FILE_FORMAT	2	/extended format/",
133	\ ], 'Xtags')
134  set tags=Xtags
135
136  let l = taglist('.*')
137  call assert_equal(1, len(l))
138  call assert_equal('FBar', l[0].name)
139
140  set tags&
141  call delete('Xtags')
142endfunc
143
144" Test for using an excmd in a tags file to position the cursor (instead of a
145" search pattern or a line number)
146func Test_tagfile_excmd()
147  call writefile([
148	\ "vFoo\tXfoo\tcall cursor(3, 4)" .. '|;"' .. "\tv",
149	\ ], 'Xtags')
150  set tags=Xtags
151
152  let l = taglist('.*')
153  call assert_equal([{
154	      \ 'cmd' : 'call cursor(3, 4)',
155	      \ 'static' : 0,
156	      \ 'name' : 'vFoo',
157	      \ 'kind' : 'v',
158	      \ 'filename' : 'Xfoo'}], l)
159
160  set tags&
161  call delete('Xtags')
162endfunc
163
164" Test for duplicate fields in a tag in a tags file
165func Test_duplicate_field()
166  call writefile([
167	\ "vFoo\tXfoo\t4" .. ';"' .. "\ttypename:int\ttypename:int\tv",
168	\ ], 'Xtags')
169  set tags=Xtags
170
171  let l = taglist('.*')
172  call assert_equal([{
173	      \ 'cmd' : '4',
174	      \ 'static' : 0,
175	      \ 'name' : 'vFoo',
176	      \ 'kind' : 'v',
177	      \ 'typename' : 'int',
178	      \ 'filename' : 'Xfoo'}], l)
179
180  set tags&
181  call delete('Xtags')
182endfunc
183
184" Test for tag address with ;
185func Test_tag_addr_with_semicolon()
186  call writefile([
187	      \ "Func1\tXfoo\t6;/^Func1/" .. ';"' .. "\tf"
188	      \ ], 'Xtags')
189  set tags=Xtags
190
191  let l = taglist('.*')
192  call assert_equal([{
193	      \ 'cmd' : '6;/^Func1/',
194	      \ 'static' : 0,
195	      \ 'name' : 'Func1',
196	      \ 'kind' : 'f',
197	      \ 'filename' : 'Xfoo'}], l)
198
199  set tags&
200  call delete('Xtags')
201endfunc
202
203" Test for format error in a tags file
204func Test_format_error()
205  call writefile(['vFoo-Xfoo-4'], 'Xtags')
206  set tags=Xtags
207
208  let caught_exception = v:false
209  try
210    let l = taglist('.*')
211  catch /E431:/
212    " test succeeded
213    let caught_exception = v:true
214  catch
215    call assert_report('Caught ' . v:exception . ' in ' . v:throwpoint)
216  endtry
217  call assert_true(caught_exception)
218
219  set tags&
220  call delete('Xtags')
221endfunc
222
223" Test for :tag command completion with 'wildoptions' set to 'tagfile'
224func Test_tag_complete_wildoptions()
225  call writefile(["foo\ta.c\t10;\"\tf", "bar\tb.c\t20;\"\td"], 'Xtags')
226  set tags=Xtags
227  set wildoptions=tagfile
228
229  call feedkeys(":tag \<C-D>\<C-R>=Screenline(&lines - 1)\<CR> : "
230        \ .. "\<C-R>=Screenline(&lines - 2)\<CR>\<C-B>\"\<CR>", 'xt')
231
232  call assert_equal('"tag bar d b.c : foo f a.c', @:)
233
234  call delete('Xtags')
235  set wildoptions&
236  set tags&
237endfunc
238
239" vim: shiftwidth=2 sts=2 expandtab
240