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