xref: /vim-8.2.3635/src/testdir/test_tagfunc.vim (revision 2387773d)
1" Test 'tagfunc'
2
3func TagFunc(pat, flag, info)
4  let g:tagfunc_args = [a:pat, a:flag, a:info]
5  let tags = []
6  for num in range(1,10)
7    let tags += [{
8          \ 'cmd': '2', 'name': 'nothing'.num, 'kind': 'm',
9          \ 'filename': 'Xfile1', 'user_data': 'somedata'.num,
10          \}]
11  endfor
12  return tags
13endfunc
14
15func Test_tagfunc()
16  set tagfunc=TagFunc
17  new Xfile1
18  call setline(1, ['empty', 'one()', 'empty'])
19  write
20
21  call assert_equal({'cmd': '2', 'static': 0,
22        \ 'name': 'nothing2', 'user_data': 'somedata2',
23        \ 'kind': 'm', 'filename': 'Xfile1'}, taglist('.')[1])
24
25  call settagstack(win_getid(), {'items': []})
26
27  tag arbitrary
28  call assert_equal('arbitrary', g:tagfunc_args[0])
29  call assert_equal('', g:tagfunc_args[1])
30  call assert_equal('somedata1', gettagstack().items[0].user_data)
31  5tag arbitrary
32  call assert_equal('arbitrary', g:tagfunc_args[0])
33  call assert_equal('', g:tagfunc_args[1])
34  call assert_equal('somedata5', gettagstack().items[1].user_data)
35  pop
36  tag
37  call assert_equal('arbitrary', g:tagfunc_args[0])
38  call assert_equal('', g:tagfunc_args[1])
39  call assert_equal('somedata5', gettagstack().items[1].user_data)
40
41  let g:tagfunc_args=[]
42  execute "normal! \<c-]>"
43  call assert_equal('one', g:tagfunc_args[0])
44  call assert_equal('c', g:tagfunc_args[1])
45
46  set cpt=t
47  let g:tagfunc_args=[]
48  execute "normal! i\<c-n>\<c-y>"
49  call assert_equal('ci', g:tagfunc_args[1])
50  call assert_equal('nothing1', getline('.')[0:7])
51
52  func BadTagFunc1(...)
53    return 0
54  endfunc
55  func BadTagFunc2(...)
56    return [1]
57  endfunc
58  func BadTagFunc3(...)
59    return [{'name': 'foo'}]
60  endfunc
61
62  for &tagfunc in ['BadTagFunc1', 'BadTagFunc2', 'BadTagFunc3']
63    try
64      tag nothing
65      call assert_false(1, 'tag command should have failed')
66    catch
67      call assert_exception('E987:')
68    endtry
69    exe 'delf' &tagfunc
70  endfor
71
72  func NullTagFunc(...)
73    return v:null
74  endfunc
75  set tags= tfu=NullTagFunc
76  call assert_fails('tag nothing', 'E426')
77  delf NullTagFunc
78
79  bwipe!
80  set tags& tfu& cpt&
81  call delete('Xfile1')
82endfunc
83
84" Test for modifying the tag stack from a tag function and jumping to a tag
85" from a tag function
86func Test_tagfunc_settagstack()
87  func Mytagfunc1(pat, flags, info)
88    call settagstack(1, {'tagname' : 'mytag', 'from' : [0, 10, 1, 0]})
89    return [{'name' : 'mytag', 'filename' : 'Xtest', 'cmd' : '1'}]
90  endfunc
91  set tagfunc=Mytagfunc1
92  call writefile([''], 'Xtest')
93  call assert_fails('tag xyz', 'E986:')
94
95  func Mytagfunc2(pat, flags, info)
96    tag test_tag
97    return [{'name' : 'mytag', 'filename' : 'Xtest', 'cmd' : '1'}]
98  endfunc
99  set tagfunc=Mytagfunc2
100  call assert_fails('tag xyz', 'E986:')
101
102  call delete('Xtest')
103  set tagfunc&
104  delfunc Mytagfunc1
105  delfunc Mytagfunc2
106endfunc
107
108" vim: shiftwidth=2 sts=2 expandtab
109