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