summaryrefslogtreecommitdiff
path: root/src/testdir/test_cscope.vim
blob: 2c7ed9fcce53d58f51462e0ab440806762c93b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
" Test for cscope commands.

if !has('cscope') || !executable('cscope') || !has('quickfix')
  finish
endif

func Test_cscopequickfix()
  set cscopequickfix=s-,g-,d+,c-,t+,e-,f0,i-,a-
  call assert_equal('s-,g-,d+,c-,t+,e-,f0,i-,a-', &cscopequickfix)

  call assert_fails('set cscopequickfix=x-', 'E474:')
  call assert_fails('set cscopequickfix=s', 'E474:')
  call assert_fails('set cscopequickfix=s7', 'E474:')
  call assert_fails('set cscopequickfix=s-a', 'E474:')
endfunc

func CscopeSetupOrClean(setup)
    if a:setup
      noa sp ../memfile_test.c
      saveas! Xmemfile_test.c
      call system('cscope -bk -fXcscope.out Xmemfile_test.c')
      cscope add Xcscope.out
      set cscopequickfix=s-,g-,d-,c-,t-,e-,f-,i-,a-
    else
      cscope kill -1
      for file in ['Xcscope.out', 'Xmemfile_test.c']
          call delete(file)
      endfor
    endif
endfunc

func Test_cscope1()
    call CscopeSetupOrClean(1)
    " Test 0: E568: duplicate cscope database not added
    try
      set nocscopeverbose
      cscope add Xcscope.out
      set cscopeverbose
    catch
      call assert_true(0)
    endtry
    call assert_fails('cscope add Xcscope.out', 'E568')
    call assert_fails('cscope add doesnotexist.out', 'E563')
    call assert_fails('cscope kill 2', 'E261')
    " Test 1: Find this C-Symbol
    let a=execute('cscope find s main')
    " Test 1.1 test where it moves the cursor
    call assert_equal('main(void)', getline('.'))
    " Test 1.2 test the output of the :cs command
    call assert_match('\n(1 of 1): <<main>> main(void )', a)

    " Test 2: Find this definition
    cscope find g test_mf_hash
    call assert_equal(['', '/*', ' * Test mf_hash_*() functions.', ' */', '    static void', 'test_mf_hash(void)', '{'], getline(line('.')-5, line('.')+1))

    " Test 3: Find functions called by this function
    let a=execute('cscope find d test_mf_hash')
    call assert_match('\n(1 of 42): <<mf_hash_init>> mf_hash_init(&ht);', a)
    call assert_equal('    mf_hash_init(&ht);', getline('.'))

    " Test 4: Find functions calling this function
    let a=execute('cscope find c test_mf_hash')
    call assert_match('\n(1 of 1): <<main>> test_mf_hash();', a)
    call assert_equal('    test_mf_hash();', getline('.'))

    " Test 5: Find this text string
    let a=execute('cscope find t Bram')
    call assert_match('(1 of 1): <<<unknown>>>  \* VIM - Vi IMproved^Iby Bram Moolenaar', a)
    call assert_equal(' * VIM - Vi IMproved	by Bram Moolenaar', getline('.'))

    " Test 6: Find this egrep pattern
    " test all matches returned by cscope
    let a=execute('cscope find e ^\#includ.')
    call assert_match('\n(1 of 3): <<<unknown>>> #include <assert.h>', a)
    call assert_equal('#include <assert.h>', getline('.'))
    cnext
    call assert_equal('#include "main.c"', getline('.'))
    cnext
    call assert_equal('#include "memfile.c"', getline('.'))
    call assert_fails('cnext', 'E553')

    " Test 7: Find this file
    enew
    let a=execute('cscope find f Xmemfile_test.c')
    call assert_match('\n"Xmemfile_test.c" 143L, 3137C', a)
    call assert_equal('Xmemfile_test.c', @%)

    " Test 8: Find files #including this file
    enew
    let a=execute('cscope find i assert.h')
    call assert_equal(['','"Xmemfile_test.c" 143L, 3137C','(1 of 1): <<global>> #include <assert.h>'], split(a, '\n', 1))
    call assert_equal('#include <assert.h>', getline('.'))

    " Test 9: Invalid find command
    call assert_fails('cs find x', 'E560')

    " Test 10: Find places where this symbol is assigned a value
    " this needs a cscope >= 15.8
    " unfortunately, Travis has cscope version 15.7
    let cscope_version=systemlist('cscope --version')[0]
    let cs_version=str2float(matchstr(cscope_version, '\d\+\(\.\d\+\)\?'))
    if cs_version >= 15.8
      let a=execute('cscope find a item')
      call assert_equal(['', '(1 of 4): <<test_mf_hash>> item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);'], split(a, '\n', 1))
      call assert_equal('	item = (mf_hashitem_T *)lalloc_clear(sizeof(mf_hashtab_T), FALSE);', getline('.'))
      cnext
      call assert_equal('	item = mf_hash_find(&ht, key);', getline('.'))
      cnext
      call assert_equal('	    item = mf_hash_find(&ht, key);', getline('.'))
      cnext
      call assert_equal('	item = mf_hash_find(&ht, key);', getline('.'))
    endif

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

    " Test 12: cscope help
    let a=execute('cscope help')
    call assert_match('^cscope commands:\n', a)
    call assert_match('\nadd  :', a)
    call assert_match('\nfind :', a)
    call assert_match('\nhelp : Show this message', a)
    call assert_match('\nkill : Kill a connection', a)
    call assert_match('\nreset: Reinit all connections', a)
    call assert_match('\nshow : Show connections', a)

    " Test 13: reset connections
    let a=execute('cscope reset')
    call assert_match('\nAdded cscope database.*Xcscope.out (#0)', a)
    call assert_match('\nAll cscope databases reset', a)

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

    " Test 15: cstag and 'csto' option
    set csto=0
    let a=execute('cstag TEST_COUNT')
    call assert_match('(1 of 1): <<TEST_COUNT>> #define TEST_COUNT 50000', a)
    call assert_equal('#define TEST_COUNT 50000', getline('.'))
    set csto=1
    let a=execute('cstag index_to_key')
    call assert_match('(1 of 1): <<index_to_key>> #define index_to_key(i) ((i) ^ 15167)', a)
    call assert_equal('#define index_to_key(i) ((i) ^ 15167)', getline('.'))
    call assert_fails('cstag xxx', 'E257')
    call assert_fails('cstag', 'E562')

    " Test 15: 'csprg' option
    call assert_equal('cscope', &csprg)

    " Test 16: 'cst' option
    set cst
    let a=execute('tag TEST_COUNT')
    call assert_match('(1 of 1): <<TEST_COUNT>> #define TEST_COUNT 50000', a)
    call assert_equal('#define TEST_COUNT 50000', getline('.'))
    set nocst
    call assert_fails('tag TEST_COUNT', 'E426')

    " CleanUp
    call CscopeSetupOrClean(0)

    " cscope command should fail after killing scope connection.
    call assert_fails('cscope find s main', 'E567')

endfunc

" vim: shiftwidth=2 sts=2 expandtab