xref: /vim-8.2.3635/src/testdir/test_cscope.vim (revision f573c6e1)
1" Test for cscope commands.
2
3source check.vim
4CheckFeature cscope
5CheckFeature quickfix
6CheckExecutable cscope
7
8func CscopeSetupOrClean(setup)
9    if a:setup
10      noa sp ../memfile_test.c
11      saveas! Xmemfile_test.c
12      call system('cscope -bk -fXcscope.out Xmemfile_test.c')
13      call system('cscope -bk -fXcscope2.out Xmemfile_test.c')
14      cscope add Xcscope.out
15      set cscopequickfix=s-,g-,d-,c-,t-,e-,f-,i-,a-
16    else
17      cscope kill -1
18      for file in ['Xcscope.out', 'Xcscope2.out', 'Xmemfile_test.c']
19          call delete(file)
20      endfo
21    endif
22endfunc
23
24func Test_cscopeWithCscopeConnections()
25    call CscopeSetupOrClean(1)
26    " Test: E568: duplicate cscope database not added
27    try
28      set nocscopeverbose
29      cscope add Xcscope.out
30      set cscopeverbose
31    catch
32      call assert_report('exception thrown')
33    endtry
34    call assert_fails('cscope add', 'E560:')
35    call assert_fails('cscope add Xcscope.out', 'E568:')
36    call assert_fails('cscope add doesnotexist.out', 'E563:')
37    if has('unix')
38      call assert_fails('cscope add /dev/null', 'E564:')
39    endif
40
41    " Test: Find this C-Symbol
42    for cmd in ['cs find s main', 'cs find 0 main']
43      let a = execute(cmd)
44      " Test where it moves the cursor
45      call assert_equal('main(void)', getline('.'))
46      " Test the output of the :cs command
47      call assert_match('\n(1 of 1): <<main>> main(void )', a)
48    endfor
49
50    " Test: Find this definition
51    for cmd in ['cs find g test_mf_hash',
52          \     'cs find 1 test_mf_hash',
53          \     'cs find 1   test_mf_hash'] " leading space ignored.
54      exe cmd
55      call assert_equal(['', '/*', ' * Test mf_hash_*() functions.', ' */', '    static void', 'test_mf_hash(void)', '{'], getline(line('.')-5, line('.')+1))
56    endfor
57
58    " Test: Find functions called by this function
59    for cmd in ['cs find d test_mf_hash', 'cs find 2 test_mf_hash']
60      let a = execute(cmd)
61      call assert_match('\n(1 of 42): <<mf_hash_init>> mf_hash_init(&ht);', a)
62      call assert_equal('    mf_hash_init(&ht);', getline('.'))
63    endfor
64
65    " Test: Find functions calling this function
66    for cmd in ['cs find c test_mf_hash', 'cs find 3 test_mf_hash']
67      let a = execute(cmd)
68      call assert_match('\n(1 of 1): <<main>> test_mf_hash();', a)
69      call assert_equal('    test_mf_hash();', getline('.'))
70    endfor
71
72    " Test: Find this text string
73    for cmd in ['cs find t Bram', 'cs find 4 Bram']
74      let a = execute(cmd)
75      call assert_match('(1 of 1): <<<unknown>>>  \* VIM - Vi IMproved^Iby Bram Moolenaar', a)
76      call assert_equal(' * VIM - Vi IMproved	by Bram Moolenaar', getline('.'))
77    endfor
78
79    " Test: Find this egrep pattern
80    " test all matches returned by cscope
81    for cmd in ['cs find e ^\#includ.', 'cs find 6 ^\#includ.']
82      let a = execute(cmd)
83      call assert_match('\n(1 of 3): <<<unknown>>> #include <assert.h>', a)
84      call assert_equal('#include <assert.h>', getline('.'))
85      cnext
86      call assert_equal('#include "main.c"', getline('.'))
87      cnext
88      call assert_equal('#include "memfile.c"', getline('.'))
89      call assert_fails('cnext', 'E553:')
90    endfor
91
92    " Test: Find the same egrep pattern using lcscope this time.
93    let a = execute('lcs find e ^\#includ.')
94    call assert_match('\n(1 of 3): <<<unknown>>> #include <assert.h>', a)
95    call assert_equal('#include <assert.h>', getline('.'))
96    lnext
97    call assert_equal('#include "main.c"', getline('.'))
98    lnext
99    call assert_equal('#include "memfile.c"', getline('.'))
100    call assert_fails('lnext', 'E553:')
101
102    " Test: Find this file
103    for cmd in ['cs find f Xmemfile_test.c', 'cs find 7 Xmemfile_test.c']
104      enew
105      let a = execute(cmd)
106      call assert_true(a =~ '"Xmemfile_test.c" \d\+L, \d\+B')
107      call assert_equal('Xmemfile_test.c', @%)
108    endfor
109
110    " Test: Find files #including this file
111    for cmd in ['cs find i assert.h', 'cs find 8 assert.h']
112      enew
113      let a = execute(cmd)
114      let alines = split(a, '\n', 1)
115      call assert_equal('', alines[0])
116      call assert_true(alines[1] =~ '"Xmemfile_test.c" \d\+L, \d\+B')
117      call assert_equal('(1 of 1): <<global>> #include <assert.h>', alines[2])
118      call assert_equal('#include <assert.h>', getline('.'))
119    endfor
120
121    " Test: Invalid find command
122    call assert_fails('cs find', 'E560:')
123    call assert_fails('cs find x', 'E560:')
124
125    if has('float')
126      " Test: Find places where this symbol is assigned a value
127      " this needs a cscope >= 15.8
128      " unfortunately, Travis has cscope version 15.7
129      let cscope_version = systemlist('cscope --version')[0]
130      let cs_version = str2float(matchstr(cscope_version, '\d\+\(\.\d\+\)\?'))
131      if cs_version >= 15.8
132        for cmd in ['cs find a item', 'cs find 9 item']
133          let a = execute(cmd)
134          call assert_equal(['', '(1 of 4): <<test_mf_hash>> item = LALLOC_CLEAR_ONE(mf_hashitem_T);'], split(a, '\n', 1))
135          call assert_equal('	item = LALLOC_CLEAR_ONE(mf_hashitem_T);', getline('.'))
136          cnext
137          call assert_equal('	item = mf_hash_find(&ht, key);', getline('.'))
138          cnext
139          call assert_equal('	    item = mf_hash_find(&ht, key);', getline('.'))
140          cnext
141          call assert_equal('	item = mf_hash_find(&ht, key);', getline('.'))
142        endfor
143      endif
144    endif
145
146    " Test: leading whitespace is not removed for cscope find text
147    let a = execute('cscope find t     test_mf_hash')
148    call assert_equal(['', '(1 of 1): <<<unknown>>>     test_mf_hash();'], split(a, '\n', 1))
149    call assert_equal('    test_mf_hash();', getline('.'))
150
151    " Test: test with scscope
152    let a = execute('scs find t Bram')
153    call assert_match('(1 of 1): <<<unknown>>>  \* VIM - Vi IMproved^Iby Bram Moolenaar', a)
154    call assert_equal(' * VIM - Vi IMproved	by Bram Moolenaar', getline('.'))
155
156    " Test: cscope help
157    for cmd in ['cs', 'cs help', 'cs xxx']
158      let a = execute(cmd)
159      call assert_match('^cscope commands:\n', a)
160      call assert_match('\nadd  :', a)
161      call assert_match('\nfind :', a)
162      call assert_match('\nhelp : Show this message', a)
163      call assert_match('\nkill : Kill a connection', a)
164      call assert_match('\nreset: Reinit all connections', a)
165      call assert_match('\nshow : Show connections', a)
166    endfor
167    let a = execute('scscope help')
168    call assert_match('This cscope command does not support splitting the window\.', a)
169
170    " Test: reset connections
171    let a = execute('cscope reset')
172    call assert_match('\nAdded cscope database.*Xcscope.out (#0)', a)
173    call assert_match('\nAll cscope databases reset', a)
174
175    " Test: cscope show
176    let a = execute('cscope show')
177    call assert_match('\n 0 \d\+.*Xcscope.out\s*<none>', a)
178
179    " Test: cstag and 'csto' option
180    set csto=0
181    let a = execute('cstag TEST_COUNT')
182    call assert_match('(1 of 1): <<TEST_COUNT>> #define TEST_COUNT 50000', a)
183    call assert_equal('#define TEST_COUNT 50000', getline('.'))
184    call assert_fails('cstag DOES_NOT_EXIST', 'E257:')
185    set csto=1
186    let a = execute('cstag index_to_key')
187    call assert_match('(1 of 1): <<index_to_key>> #define index_to_key(i) ((i) ^ 15167)', a)
188    call assert_equal('#define index_to_key(i) ((i) ^ 15167)', getline('.'))
189    call assert_fails('cstag DOES_NOT_EXIST', 'E257:')
190    call assert_fails('cstag', 'E562:')
191    let save_tags = &tags
192    set tags=
193    call assert_fails('cstag DOES_NOT_EXIST', 'E257:')
194    let a = execute('cstag index_to_key')
195    call assert_match('(1 of 1): <<index_to_key>> #define index_to_key(i) ((i) ^ 15167)', a)
196    let &tags = save_tags
197
198    " Test: 'cst' option
199    set nocst
200    call assert_fails('tag TEST_COUNT', 'E433:')
201    set cst
202    let a = execute('tag TEST_COUNT')
203    call assert_match('(1 of 1): <<TEST_COUNT>> #define TEST_COUNT 50000', a)
204    call assert_equal('#define TEST_COUNT 50000', getline('.'))
205    let a = execute('tags')
206    call assert_match('1  1 TEST_COUNT\s\+\d\+\s\+#define index_to_key', a)
207
208    " Test: 'cscoperelative'
209    call mkdir('Xcscoperelative')
210    cd Xcscoperelative
211    let a = execute('cs find g test_mf_hash')
212    call assert_notequal('test_mf_hash(void)', getline('.'))
213    set cscoperelative
214    let a = execute('cs find g test_mf_hash')
215    call assert_equal('test_mf_hash(void)', getline('.'))
216    set nocscoperelative
217    cd ..
218    call delete('Xcscoperelative', 'd')
219
220    " Test: E259: no match found
221    call assert_fails('cscope find g DOES_NOT_EXIST', 'E259:')
222
223    " Test: this should trigger call to cs_print_tags()
224    " Unclear how to check result though, we just exercise the code.
225    set cst cscopequickfix=s0
226    call feedkeys(":cs find s main\<CR>", 't')
227
228    " Test: cscope kill
229    call assert_fails('cscope kill', 'E560:')
230    call assert_fails('cscope kill 2', 'E261:')
231    call assert_fails('cscope kill xxx', 'E261:')
232
233    let a = execute('cscope kill 0')
234    call assert_match('cscope connection 0 closed', a)
235
236    cscope add Xcscope.out
237    let a = execute('cscope kill Xcscope.out')
238    call assert_match('cscope connection Xcscope.out closed', a)
239
240    cscope add Xcscope.out .
241    let a = execute('cscope kill -1')
242    call assert_match('cscope connection .*Xcscope.out closed', a)
243    let a = execute('cscope kill -1')
244    call assert_equal('', a)
245
246    " Test: 'csprg' option
247    call assert_equal('cscope', &csprg)
248    set csprg=doesnotexist
249    call assert_fails('cscope add Xcscope2.out', 'E609:')
250    set csprg=cscope
251
252    " Test: multiple cscope connections
253    cscope add Xcscope.out
254    cscope add Xcscope2.out . -C
255    let a = execute('cscope show')
256    call assert_match('\n 0 \d\+.*Xcscope.out\s*<none>', a)
257    call assert_match('\n 1 \d\+.*Xcscope2.out\s*\.', a)
258
259    " Test: test Ex command line completion
260    call feedkeys(":cs \<C-A>\<C-B>\"\<CR>", 'tx')
261    call assert_equal('"cs add find help kill reset show', @:)
262
263    call feedkeys(":scs \<C-A>\<C-B>\"\<CR>", 'tx')
264    call assert_equal('"scs find', @:)
265
266    call feedkeys(":cs find \<C-A>\<C-B>\"\<CR>", 'tx')
267    call assert_equal('"cs find a c d e f g i s t', @:)
268
269    call feedkeys(":cs kill \<C-A>\<C-B>\"\<CR>", 'tx')
270    call assert_equal('"cs kill -1 0 1', @:)
271
272    call feedkeys(":cs add Xcscope\<C-A>\<C-B>\"\<CR>", 'tx')
273    call assert_equal('"cs add Xcscope.out Xcscope2.out', @:)
274
275    " Test: cscope_connection()
276    call assert_equal(cscope_connection(), 1)
277    call assert_equal(cscope_connection(0, 'out'), 1)
278    call assert_equal(cscope_connection(0, 'xxx'), 1)
279
280    call assert_equal(cscope_connection(1, 'out'), 1)
281    call assert_equal(cscope_connection(1, 'xxx'), 0)
282
283    call assert_equal(cscope_connection(2, 'out'), 0)
284    call assert_equal(cscope_connection(2, getcwd() .. '/Xcscope.out', 1), 1)
285
286    call assert_equal(cscope_connection(3, 'xxx', '..'), 0)
287    call assert_equal(cscope_connection(3, 'out', 'xxx'), 0)
288    call assert_equal(cscope_connection(3, 'out', '.'), 1)
289
290    call assert_equal(cscope_connection(4, 'out', '.'), 0)
291
292    call assert_equal(cscope_connection(5, 'out'), 0)
293    call assert_equal(cscope_connection(-1, 'out'), 0)
294
295    call CscopeSetupOrClean(0)
296endfunc
297
298" Test ":cs add {dir}"  (add the {dir}/cscope.out database)
299func Test_cscope_add_dir()
300  call mkdir('Xcscopedir', 'p')
301
302  " Cscope doesn't handle symlinks, so this needs to be resolved in case a
303  " shadow directory is being used.
304  let memfile = resolve('../memfile_test.c')
305  call system('cscope -bk -fXcscopedir/cscope.out ' . memfile)
306
307  cs add Xcscopedir
308  let a = execute('cscope show')
309  let lines = split(a, "\n", 1)
310  call assert_equal(3, len(lines))
311  call assert_equal(' # pid    database name                       prepend path', lines[0])
312  call assert_equal('', lines[1])
313  call assert_match('^ 0 \d\+.*Xcscopedir/cscope.out\s\+<none>$', lines[2])
314
315  cs kill -1
316  call delete('Xcscopedir/cscope.out')
317  call assert_fails('cs add Xcscopedir', 'E563:')
318
319  call delete('Xcscopedir', 'd')
320endfunc
321
322func Test_cscopequickfix()
323  set cscopequickfix=s-,g-,d+,c-,t+,e-,f0,i-,a-
324  call assert_equal('s-,g-,d+,c-,t+,e-,f0,i-,a-', &cscopequickfix)
325
326  call assert_fails('set cscopequickfix=x-', 'E474:')
327  call assert_fails('set cscopequickfix=s', 'E474:')
328  call assert_fails('set cscopequickfix=s7', 'E474:')
329  call assert_fails('set cscopequickfix=s-a', 'E474:')
330endfunc
331
332func Test_withoutCscopeConnection()
333  call assert_equal(cscope_connection(), 0)
334
335  call assert_fails('cscope find s main', 'E567:')
336  let a = execute('cscope show')
337  call assert_match('no cscope connections', a)
338endfunc
339
340
341" vim: shiftwidth=2 sts=2 expandtab
342