xref: /vim-8.2.3635/src/testdir/test_cscope.vim (revision edf634e0)
1" Test for cscope commands.
2
3if !has('cscope') || !executable('cscope') || !has('quickfix')
4  finish
5endif
6
7func Test_cscopequickfix()
8  set cscopequickfix=s-,g-,d+,c-,t+,e-,f0,i-,a-
9  call assert_equal('s-,g-,d+,c-,t+,e-,f0,i-,a-', &cscopequickfix)
10
11  call assert_fails('set cscopequickfix=x-', 'E474:')
12  call assert_fails('set cscopequickfix=s', 'E474:')
13  call assert_fails('set cscopequickfix=s7', 'E474:')
14  call assert_fails('set cscopequickfix=s-a', 'E474:')
15endfunc
16
17func CscopeSetupOrClean(setup)
18    if a:setup
19      noa sp ../memfile_test.c
20      saveas! Xmemfile_test.c
21      call system('cscope -bk -fXcscope.out Xmemfile_test.c')
22      cscope add Xcscope.out
23      set cscopequickfix=s-,g-,d-,c-,t-,e-,f-,i-,a-
24    else
25      cscope kill -1
26      for file in ['Xcscope.out', 'Xmemfile_test.c']
27          call delete(file)
28      endfor
29    endif
30endfunc
31
32func Test_cscope1()
33    call CscopeSetupOrClean(1)
34    " Test 0: E568: duplicate cscope database not added
35    try
36      set nocscopeverbose
37      cscope add Xcscope.out
38      set cscopeverbose
39    catch
40      call assert_true(0)
41    endtry
42    call assert_fails('cscope add Xcscope.out', 'E568')
43    " Test 1: Find this C-Symbol
44    let a=execute('cscope find s main')
45    " Test 1.1 test where it moves the cursor
46    call assert_equal('main(void)', getline('.'))
47    " Test 1.2 test the output of the :cs command
48    call assert_match('\n(1 of 1): <<main>> main(void )', a)
49
50    " Test 2: Find this definition
51    cscope find g test_mf_hash
52    call assert_equal(['', '/*', ' * Test mf_hash_*() functions.', ' */', '    static void', 'test_mf_hash(void)', '{'], getline(line('.')-5, line('.')+1))
53
54    " Test 3: Find functions called by this function
55    let a=execute('cscope find d test_mf_hash')
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
59    " Test 4: Find functions calling this function
60    let a=execute('cscope find c test_mf_hash')
61    call assert_match('\n(1 of 1): <<main>> test_mf_hash();', a)
62    call assert_equal('    test_mf_hash();', getline('.'))
63
64    " Test 5: Find this text string
65    let a=execute('cscope find t Bram')
66    call assert_match('(1 of 1): <<<unknown>>>  \* VIM - Vi IMproved^Iby Bram Moolenaar', a)
67    call assert_equal(' * VIM - Vi IMproved	by Bram Moolenaar', getline('.'))
68
69    " Test 6: Find this egrep pattern
70    " test all matches returned by cscope
71    let a=execute('cscope find e ^\#includ.')
72    call assert_match('\n(1 of 3): <<<unknown>>> #include <assert.h>', a)
73    call assert_equal('#include <assert.h>', getline('.'))
74    cnext
75    call assert_equal('#include "main.c"', getline('.'))
76    cnext
77    call assert_equal('#include "memfile.c"', getline('.'))
78    call assert_fails('cnext', 'E553')
79
80    " Test 7: Find this file
81    enew
82    let a=execute('cscope find f Xmemfile_test.c')
83    call assert_match('\n"Xmemfile_test.c" 143L, 3137C', a)
84    call assert_equal('Xmemfile_test.c', @%)
85
86    " Test 8: Find files #including this file
87    enew
88    let a=execute('cscope find i assert.h')
89    call assert_equal(['','"Xmemfile_test.c" 143L, 3137C','(1 of 1): <<global>> #include <assert.h>'], split(a, '\n', 1))
90    call assert_equal('#include <assert.h>', getline('.'))
91
92    " Test 9: Find places where this symbol is assigned a value
93    " this needs a cscope >= 15.8
94    " unfortunatly, Travis has cscope version 15.7
95    let cscope_version=systemlist('cscope --version')[0]
96    let cs_version=str2float(matchstr(cscope_version, '\d\+\(\.\d\+\)\?'))
97    if cs_version >= 15.8
98      let a=execute('cscope find a item')
99      call assert_equal(['', '(1 of 4): <<test_mf_hash>> item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);'], split(a, '\n', 1))
100      call assert_equal('	item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);', getline('.'))
101      cnext
102      call assert_equal('	item = mf_hash_find(&ht, key);', getline('.'))
103      cnext
104      call assert_equal('	    item = mf_hash_find(&ht, key);', getline('.'))
105      cnext
106      call assert_equal('	item = mf_hash_find(&ht, key);', getline('.'))
107    endif
108
109    " Test 10: leading whitespace is not removed for cscope find text
110    let a=execute('cscope find t     test_mf_hash')
111    call assert_equal(['', '(1 of 1): <<<unknown>>>     test_mf_hash();'], split(a, '\n', 1))
112    call assert_equal('    test_mf_hash();', getline('.'))
113
114    " Test 11: cscope help
115    let a=execute('cscope help')
116    call assert_match('^cscope commands:\n', a)
117    call assert_match('\nadd  :', a)
118    call assert_match('\nfind :', a)
119    call assert_match('\nhelp : Show this message', a)
120    call assert_match('\nkill : Kill a connection', a)
121    call assert_match('\nreset: Reinit all connections', a)
122    call assert_match('\nshow : Show connections', a)
123
124    " Test 12: reset connections
125    let a=execute('cscope reset')
126    call assert_match('\nAdded cscope database.*Xcscope.out (#0)', a)
127    call assert_match('\nAll cscope databases reset', a)
128
129    " Test 13: cscope show
130    let a=execute('cscope show')
131    call assert_match('\n 0 \d\+.*Xcscope.out\s*<none>', a)
132
133    " Test 14: 'csprg' option
134    call assert_equal('cscope', &csprg)
135
136    " CleanUp
137    call CscopeSetupOrClean(0)
138endfunc
139
140" vim: shiftwidth=2 sts=2 expandtab
141