Skip to content

Commit 812ad4f

Browse files
committed
patch 7.4.2175
Problem: Insufficient testing of cscope. Solution: Add more tests. (Dominique Pelle)
1 parent c8ce615 commit 812ad4f

2 files changed

Lines changed: 191 additions & 82 deletions

File tree

src/testdir/test_cscope.vim

Lines changed: 189 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,23 @@ if !has('cscope') || !executable('cscope') || !has('quickfix')
44
finish
55
endif
66

7-
func 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:')
15-
endfunc
16-
177
func CscopeSetupOrClean(setup)
188
if a:setup
199
noa sp ../memfile_test.c
2010
saveas! Xmemfile_test.c
2111
call system('cscope -bk -fXcscope.out Xmemfile_test.c')
12+
call system('cscope -bk -fXcscope2.out Xmemfile_test.c')
2213
cscope add Xcscope.out
2314
set cscopequickfix=s-,g-,d-,c-,t-,e-,f-,i-,a-
2415
else
2516
cscope kill -1
26-
for file in ['Xcscope.out', 'Xmemfile_test.c']
17+
for file in ['Xcscope.out', 'Xcscope2.out', 'Xmemfile_test.c']
2718
call delete(file)
28-
endfor
19+
endfo
2920
endif
3021
endfunc
3122

32-
func Test_cscope1()
23+
func Test_cscopeWithCscopeConnections()
3324
call CscopeSetupOrClean(1)
3425
" Test 0: E568: duplicate cscope database not added
3526
try
@@ -39,103 +30,141 @@ func Test_cscope1()
3930
catch
4031
call assert_true(0)
4132
endtry
33+
call assert_fails('cscope add', 'E560')
4234
call assert_fails('cscope add Xcscope.out', 'E568')
4335
call assert_fails('cscope add doesnotexist.out', 'E563')
44-
call assert_fails('cscope kill 2', 'E261')
36+
4537
" Test 1: Find this C-Symbol
46-
let a=execute('cscope find s main')
47-
" Test 1.1 test where it moves the cursor
48-
call assert_equal('main(void)', getline('.'))
49-
" Test 1.2 test the output of the :cs command
50-
call assert_match('\n(1 of 1): <<main>> main(void )', a)
38+
for cmd in ['cs find s main', 'cs find 0 main']
39+
let a=execute(cmd)
40+
" Test 1.1 test where it moves the cursor
41+
call assert_equal('main(void)', getline('.'))
42+
" Test 1.2 test the output of the :cs command
43+
call assert_match('\n(1 of 1): <<main>> main(void )', a)
44+
endfor
5145

5246
" Test 2: Find this definition
53-
cscope find g test_mf_hash
54-
call assert_equal(['', '/*', ' * Test mf_hash_*() functions.', ' */', ' static void', 'test_mf_hash(void)', '{'], getline(line('.')-5, line('.')+1))
47+
for cmd in ['cs find g test_mf_hash', 'cs find 1 test_mf_hash']
48+
exe cmd
49+
call assert_equal(['', '/*', ' * Test mf_hash_*() functions.', ' */', ' static void', 'test_mf_hash(void)', '{'], getline(line('.')-5, line('.')+1))
50+
endfor
5551

5652
" Test 3: Find functions called by this function
57-
let a=execute('cscope find d test_mf_hash')
58-
call assert_match('\n(1 of 42): <<mf_hash_init>> mf_hash_init(&ht);', a)
59-
call assert_equal(' mf_hash_init(&ht);', getline('.'))
53+
for cmd in ['cs find d test_mf_hash', 'cs find 2 test_mf_hash']
54+
let a=execute(cmd)
55+
call assert_match('\n(1 of 42): <<mf_hash_init>> mf_hash_init(&ht);', a)
56+
call assert_equal(' mf_hash_init(&ht);', getline('.'))
57+
endfor
6058

6159
" Test 4: Find functions calling this function
62-
let a=execute('cscope find c test_mf_hash')
63-
call assert_match('\n(1 of 1): <<main>> test_mf_hash();', a)
64-
call assert_equal(' test_mf_hash();', getline('.'))
60+
for cmd in ['cs find c test_mf_hash', 'cs find 3 test_mf_hash']
61+
let a=execute(cmd)
62+
call assert_match('\n(1 of 1): <<main>> test_mf_hash();', a)
63+
call assert_equal(' test_mf_hash();', getline('.'))
64+
endfor
6565

6666
" Test 5: Find this text string
67-
let a=execute('cscope find t Bram')
68-
call assert_match('(1 of 1): <<<unknown>>> \* VIM - Vi IMproved^Iby Bram Moolenaar', a)
69-
call assert_equal(' * VIM - Vi IMproved by Bram Moolenaar', getline('.'))
67+
for cmd in ['cs find t Bram', 'cs find 4 Bram']
68+
let a=execute(cmd)
69+
call assert_match('(1 of 1): <<<unknown>>> \* VIM - Vi IMproved^Iby Bram Moolenaar', a)
70+
call assert_equal(' * VIM - Vi IMproved by Bram Moolenaar', getline('.'))
71+
endfor
7072

7173
" Test 6: Find this egrep pattern
7274
" test all matches returned by cscope
73-
let a=execute('cscope find e ^\#includ.')
75+
for cmd in ['cs find e ^\#includ.', 'cs find 6 ^\#includ.']
76+
let a=execute(cmd)
77+
call assert_match('\n(1 of 3): <<<unknown>>> #include <assert.h>', a)
78+
call assert_equal('#include <assert.h>', getline('.'))
79+
cnext
80+
call assert_equal('#include "main.c"', getline('.'))
81+
cnext
82+
call assert_equal('#include "memfile.c"', getline('.'))
83+
call assert_fails('cnext', 'E553:')
84+
endfor
85+
86+
" Test 7: Find the same egrep pattern using lcscope this time.
87+
let a=execute('lcs find e ^\#includ.')
7488
call assert_match('\n(1 of 3): <<<unknown>>> #include <assert.h>', a)
7589
call assert_equal('#include <assert.h>', getline('.'))
76-
cnext
90+
lnext
7791
call assert_equal('#include "main.c"', getline('.'))
78-
cnext
92+
lnext
7993
call assert_equal('#include "memfile.c"', getline('.'))
80-
call assert_fails('cnext', 'E553')
81-
82-
" Test 7: Find this file
83-
enew
84-
let a=execute('cscope find f Xmemfile_test.c')
85-
call assert_match('\n"Xmemfile_test.c" 143L, 3137C', a)
86-
call assert_equal('Xmemfile_test.c', @%)
87-
88-
" Test 8: Find files #including this file
89-
enew
90-
let a=execute('cscope find i assert.h')
91-
call assert_equal(['','"Xmemfile_test.c" 143L, 3137C','(1 of 1): <<global>> #include <assert.h>'], split(a, '\n', 1))
92-
call assert_equal('#include <assert.h>', getline('.'))
94+
call assert_fails('lnext', 'E553:')
95+
96+
" Test 8: Find this file
97+
for cmd in ['cs find f Xmemfile_test.c', 'cs find 7 Xmemfile_test.c']
98+
enew
99+
let a=execute(cmd)
100+
call assert_match('\n"Xmemfile_test.c" 143L, 3137C', a)
101+
call assert_equal('Xmemfile_test.c', @%)
102+
endfor
93103

94-
" Test 9: Invalid find command
95-
call assert_fails('cs find x', 'E560')
104+
" Test 9: Find files #including this file
105+
for cmd in ['cs find i assert.h', 'cs find 8 assert.h']
106+
enew
107+
let a=execute(cmd)
108+
call assert_equal(['','"Xmemfile_test.c" 143L, 3137C','(1 of 1): <<global>> #include <assert.h>'], split(a, '\n', 1))
109+
call assert_equal('#include <assert.h>', getline('.'))
110+
endfor
96111

97-
" Test 10: Find places where this symbol is assigned a value
112+
" Test 10: Invalid find command
113+
call assert_fails('cs find x', 'E560:')
114+
115+
" Test 11: Find places where this symbol is assigned a value
98116
" this needs a cscope >= 15.8
99117
" unfortunately, Travis has cscope version 15.7
100118
let cscope_version=systemlist('cscope --version')[0]
101119
let cs_version=str2float(matchstr(cscope_version, '\d\+\(\.\d\+\)\?'))
102120
if cs_version >= 15.8
103-
let a=execute('cscope find a item')
104-
call assert_equal(['', '(1 of 4): <<test_mf_hash>> item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);'], split(a, '\n', 1))
105-
call assert_equal(' item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);', getline('.'))
106-
cnext
107-
call assert_equal(' item = mf_hash_find(&ht, key);', getline('.'))
108-
cnext
109-
call assert_equal(' item = mf_hash_find(&ht, key);', getline('.'))
110-
cnext
111-
call assert_equal(' item = mf_hash_find(&ht, key);', getline('.'))
121+
for cmd in ['cs find a item', 'cs find 9 item']
122+
let a=execute(cmd)
123+
call assert_equal(['', '(1 of 4): <<test_mf_hash>> item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);'], split(a, '\n', 1))
124+
call assert_equal(' item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);', getline('.'))
125+
cnext
126+
call assert_equal(' item = mf_hash_find(&ht, key);', getline('.'))
127+
cnext
128+
call assert_equal(' item = mf_hash_find(&ht, key);', getline('.'))
129+
cnext
130+
call assert_equal(' item = mf_hash_find(&ht, key);', getline('.'))
131+
endfor
112132
endif
113133

114-
" Test 11: leading whitespace is not removed for cscope find text
134+
" Test 12: leading whitespace is not removed for cscope find text
115135
let a=execute('cscope find t test_mf_hash')
116136
call assert_equal(['', '(1 of 1): <<<unknown>>> test_mf_hash();'], split(a, '\n', 1))
117137
call assert_equal(' test_mf_hash();', getline('.'))
118138

119-
" Test 12: cscope help
120-
let a=execute('cscope help')
121-
call assert_match('^cscope commands:\n', a)
122-
call assert_match('\nadd :', a)
123-
call assert_match('\nfind :', a)
124-
call assert_match('\nhelp : Show this message', a)
125-
call assert_match('\nkill : Kill a connection', a)
126-
call assert_match('\nreset: Reinit all connections', a)
127-
call assert_match('\nshow : Show connections', a)
128-
129-
" Test 13: reset connections
139+
" Test 13: test with scscope
140+
let a=execute('scs find t Bram')
141+
call assert_match('(1 of 1): <<<unknown>>> \* VIM - Vi IMproved^Iby Bram Moolenaar', a)
142+
call assert_equal(' * VIM - Vi IMproved by Bram Moolenaar', getline('.'))
143+
144+
" Test 14: cscope help
145+
for cmd in ['cs', 'cs help', 'cs xxx']
146+
let a=execute(cmd)
147+
call assert_match('^cscope commands:\n', a)
148+
call assert_match('\nadd :', a)
149+
call assert_match('\nfind :', a)
150+
call assert_match('\nhelp : Show this message', a)
151+
call assert_match('\nkill : Kill a connection', a)
152+
call assert_match('\nreset: Reinit all connections', a)
153+
call assert_match('\nshow : Show connections', a)
154+
endfor
155+
let a=execute('scscope help')
156+
call assert_match('This cscope command does not support splitting the window\.', a)
157+
158+
" Test 15: reset connections
130159
let a=execute('cscope reset')
131160
call assert_match('\nAdded cscope database.*Xcscope.out (#0)', a)
132161
call assert_match('\nAll cscope databases reset', a)
133162

134-
" Test 14: cscope show
163+
" Test 16: cscope show
135164
let a=execute('cscope show')
136165
call assert_match('\n 0 \d\+.*Xcscope.out\s*<none>', a)
137166

138-
" Test 15: cstag and 'csto' option
167+
" Test 17: cstag and 'csto' option
139168
set csto=0
140169
let a=execute('cstag TEST_COUNT')
141170
call assert_match('(1 of 1): <<TEST_COUNT>> #define TEST_COUNT 50000', a)
@@ -144,26 +173,104 @@ func Test_cscope1()
144173
let a=execute('cstag index_to_key')
145174
call assert_match('(1 of 1): <<index_to_key>> #define index_to_key(i) ((i) ^ 15167)', a)
146175
call assert_equal('#define index_to_key(i) ((i) ^ 15167)', getline('.'))
147-
call assert_fails('cstag xxx', 'E257')
148-
call assert_fails('cstag', 'E562')
149-
150-
" Test 15: 'csprg' option
151-
call assert_equal('cscope', &csprg)
176+
call assert_fails('cstag xxx', 'E257:')
177+
call assert_fails('cstag', 'E562:')
152178

153-
" Test 16: 'cst' option
179+
" Test 18: 'cst' option
180+
set nocst
181+
call assert_fails('tag TEST_COUNT', 'E426:')
154182
set cst
155183
let a=execute('tag TEST_COUNT')
156184
call assert_match('(1 of 1): <<TEST_COUNT>> #define TEST_COUNT 50000', a)
157185
call assert_equal('#define TEST_COUNT 50000', getline('.'))
158-
set nocst
159-
call assert_fails('tag TEST_COUNT', 'E426')
186+
let a=execute('tags')
187+
call assert_match('1 1 TEST_COUNT\s\+\d\+\s\+#define index_to_key', a)
188+
189+
" Test 19: this should trigger call to cs_print_tags()
190+
" Unclear how to check result though, we just exercise the code.
191+
set cst cscopequickfix=s0
192+
call feedkeys(":cs find s main\<CR>", 't')
193+
194+
" Test 20: cscope kill
195+
call assert_fails('cscope kill 2', 'E261:')
196+
call assert_fails('cscope kill xxx', 'E261:')
197+
198+
let a=execute('cscope kill 0')
199+
call assert_match('cscope connection 0 closed', a)
200+
201+
cscope add Xcscope.out
202+
let a=execute('cscope kill Xcscope.out')
203+
call assert_match('cscope connection Xcscope.out closed', a)
204+
205+
cscope add Xcscope.out .
206+
let a=execute('cscope kill -1')
207+
call assert_match('cscope connection .*Xcscope.out closed', a)
208+
let a=execute('cscope kill -1')
209+
call assert_equal('', a)
210+
211+
" Test 21: 'csprg' option
212+
call assert_equal('cscope', &csprg)
213+
set csprg=doesnotexist
214+
call assert_fails('cscope add Xcscope2.out', 'E609:')
215+
set csprg=cscope
216+
217+
" Test 22: multiple cscope connections
218+
cscope add Xcscope.out
219+
cscope add Xcscope2.out . -C
220+
let a=execute('cscope show')
221+
call assert_match('\n 0 \d\+.*Xcscope.out\s*<none>', a)
222+
call assert_match('\n 1 \d\+.*Xcscope2.out\s*\.', a)
223+
224+
" Test 23: test Ex command line completion
225+
call feedkeys(":cs \<C-A>\<C-B>\"\<CR>", 'tx')
226+
call assert_equal('"cs add find help kill reset show', @:)
227+
228+
call feedkeys(":scs \<C-A>\<C-B>\"\<CR>", 'tx')
229+
call assert_equal('"scs find', @:)
230+
231+
call feedkeys(":cs find \<C-A>\<C-B>\"\<CR>", 'tx')
232+
call assert_equal('"cs find a c d e f g i s t', @:)
233+
234+
call feedkeys(":cs kill \<C-A>\<C-B>\"\<CR>", 'tx')
235+
call assert_equal('"cs kill -1 0 1', @:)
236+
237+
call feedkeys(":cs add Xcscope\<C-A>\<C-B>\"\<CR>", 'tx')
238+
call assert_equal('"cs add Xcscope.out Xcscope2.out', @:)
239+
240+
" Test 24: cscope_connection()
241+
call assert_equal(cscope_connection(), 1)
242+
call assert_equal(cscope_connection(0, 'out'), 1)
243+
call assert_equal(cscope_connection(0, 'xxx'), 1)
244+
call assert_equal(cscope_connection(1, 'out'), 1)
245+
call assert_equal(cscope_connection(1, 'xxx'), 0)
246+
call assert_equal(cscope_connection(2, 'out'), 0)
247+
call assert_equal(cscope_connection(3, 'xxx', '..'), 0)
248+
call assert_equal(cscope_connection(3, 'out', 'xxx'), 0)
249+
call assert_equal(cscope_connection(3, 'out', '.'), 1)
250+
call assert_equal(cscope_connection(4, 'out', '.'), 0)
160251

161252
" CleanUp
162253
call CscopeSetupOrClean(0)
163254

164-
" cscope command should fail after killing scope connection.
165-
call assert_fails('cscope find s main', 'E567')
255+
endfunc
256+
257+
func Test_cscopequickfix()
258+
set cscopequickfix=s-,g-,d+,c-,t+,e-,f0,i-,a-
259+
call assert_equal('s-,g-,d+,c-,t+,e-,f0,i-,a-', &cscopequickfix)
166260

261+
call assert_fails('set cscopequickfix=x-', 'E474:')
262+
call assert_fails('set cscopequickfix=s', 'E474:')
263+
call assert_fails('set cscopequickfix=s7', 'E474:')
264+
call assert_fails('set cscopequickfix=s-a', 'E474:')
167265
endfunc
168266

267+
func Test_withoutCscopeConnection()
268+
call assert_equal(cscope_connection(), 0)
269+
270+
call assert_fails('cscope find s main', 'E567:')
271+
let a=execute('cscope show')
272+
call assert_match('no cscope connections', a)
273+
endfunc
274+
275+
169276
" vim: shiftwidth=2 sts=2 expandtab

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,8 @@ static char *(features[]) =
763763

764764
static int included_patches[] =
765765
{ /* Add new patch number below this line */
766+
/**/
767+
2175,
766768
/**/
767769
2174,
768770
/**/

0 commit comments

Comments
 (0)