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