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