Bram Moolenaar | da59dd5 | 2016-01-05 21:59:58 +0100 | [diff] [blame] | 1 | " Test for the quickfix commands. |
| 2 | |
| 3 | if !has('quickfix') |
| 4 | finish |
| 5 | endif |
| 6 | |
| 7 | " Tests for the :clist and :llist commands |
| 8 | function XlistTests(cchar) |
| 9 | let Xlist = a:cchar . 'list' |
| 10 | let Xgetexpr = a:cchar . 'getexpr' |
| 11 | |
| 12 | " With an empty list, command should return error |
| 13 | exe Xgetexpr . ' []' |
| 14 | exe 'silent! ' . Xlist |
| 15 | call assert_true(v:errmsg ==# 'E42: No Errors') |
| 16 | |
| 17 | " Populate the list and then try |
| 18 | exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1', |
| 19 | \ 'non-error 2', 'Xtestfile2:2:2:Line2', |
| 20 | \ 'non-error 3', 'Xtestfile3:3:1:Line3']" |
| 21 | |
| 22 | " List only valid entries |
| 23 | redir => result |
| 24 | exe Xlist |
| 25 | redir END |
| 26 | let l = split(result, "\n") |
| 27 | call assert_equal([' 2 Xtestfile1:1 col 3: Line1', |
| 28 | \ ' 4 Xtestfile2:2 col 2: Line2', |
| 29 | \ ' 6 Xtestfile3:3 col 1: Line3'], l) |
| 30 | |
| 31 | " List all the entries |
| 32 | redir => result |
| 33 | exe Xlist . "!" |
| 34 | redir END |
| 35 | let l = split(result, "\n") |
| 36 | call assert_equal([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1', |
| 37 | \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2', |
| 38 | \ ' 5: non-error 3', ' 6 Xtestfile3:3 col 1: Line3'], l) |
| 39 | |
| 40 | " List a range of errors |
| 41 | redir => result |
| 42 | exe Xlist . " 3,6" |
| 43 | redir END |
| 44 | let l = split(result, "\n") |
| 45 | call assert_equal([' 4 Xtestfile2:2 col 2: Line2', |
| 46 | \ ' 6 Xtestfile3:3 col 1: Line3'], l) |
| 47 | |
| 48 | redir => result |
| 49 | exe Xlist . "! 3,4" |
| 50 | redir END |
| 51 | let l = split(result, "\n") |
| 52 | call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l) |
| 53 | |
| 54 | redir => result |
| 55 | exe Xlist . " -6,-4" |
| 56 | redir END |
| 57 | let l = split(result, "\n") |
| 58 | call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l) |
| 59 | |
| 60 | redir => result |
| 61 | exe Xlist . "! -5,-3" |
| 62 | redir END |
| 63 | let l = split(result, "\n") |
| 64 | call assert_equal([' 2 Xtestfile1:1 col 3: Line1', |
| 65 | \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l) |
| 66 | endfunction |
| 67 | |
| 68 | function Test_clist() |
| 69 | call XlistTests('c') |
| 70 | call XlistTests('l') |
| 71 | endfunction |
| 72 | |
| 73 | " Tests for the :colder, :cnewer, :lolder and :lnewer commands |
| 74 | " Note that this test assumes that a quickfix/location list is |
Bram Moolenaar | cfc0a35 | 2016-01-09 20:23:00 +0100 | [diff] [blame] | 75 | " already set by the caller. |
Bram Moolenaar | da59dd5 | 2016-01-05 21:59:58 +0100 | [diff] [blame] | 76 | function XageTests(cchar) |
| 77 | let Xolder = a:cchar . 'older' |
| 78 | let Xnewer = a:cchar . 'newer' |
| 79 | let Xgetexpr = a:cchar . 'getexpr' |
| 80 | if a:cchar == 'c' |
| 81 | let Xgetlist = 'getqflist()' |
| 82 | else |
| 83 | let Xgetlist = 'getloclist(0)' |
| 84 | endif |
| 85 | |
| 86 | " Jumping to a non existent list should return error |
| 87 | exe 'silent! ' . Xolder . ' 99' |
| 88 | call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack') |
| 89 | |
| 90 | exe 'silent! ' . Xnewer . ' 99' |
| 91 | call assert_true(v:errmsg ==# 'E381: At top of quickfix stack') |
| 92 | |
| 93 | " Add three quickfix/location lists |
| 94 | exe Xgetexpr . " ['Xtestfile1:1:3:Line1']" |
| 95 | exe Xgetexpr . " ['Xtestfile2:2:2:Line2']" |
| 96 | exe Xgetexpr . " ['Xtestfile3:3:1:Line3']" |
| 97 | |
| 98 | " Go back two lists |
| 99 | exe Xolder |
| 100 | exe 'let l = ' . Xgetlist |
| 101 | call assert_equal('Line2', l[0].text) |
| 102 | |
| 103 | " Go forward two lists |
| 104 | exe Xnewer |
| 105 | exe 'let l = ' . Xgetlist |
| 106 | call assert_equal('Line3', l[0].text) |
| 107 | |
| 108 | " Test for the optional count argument |
| 109 | exe Xolder . ' 2' |
| 110 | exe 'let l = ' . Xgetlist |
| 111 | call assert_equal('Line1', l[0].text) |
| 112 | |
| 113 | exe Xnewer . ' 2' |
| 114 | exe 'let l = ' . Xgetlist |
| 115 | call assert_equal('Line3', l[0].text) |
| 116 | endfunction |
| 117 | |
| 118 | function Test_cage() |
Bram Moolenaar | cfc0a35 | 2016-01-09 20:23:00 +0100 | [diff] [blame] | 119 | let list = [{'bufnr': 1, 'lnum': 1}] |
| 120 | call setqflist(list) |
Bram Moolenaar | da59dd5 | 2016-01-05 21:59:58 +0100 | [diff] [blame] | 121 | call XageTests('c') |
Bram Moolenaar | cfc0a35 | 2016-01-09 20:23:00 +0100 | [diff] [blame] | 122 | |
| 123 | call setloclist(0, list) |
Bram Moolenaar | da59dd5 | 2016-01-05 21:59:58 +0100 | [diff] [blame] | 124 | call XageTests('l') |
| 125 | endfunction |
| 126 | |
| 127 | " Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen |
| 128 | " commands |
| 129 | function XwindowTests(cchar) |
| 130 | let Xwindow = a:cchar . 'window' |
| 131 | let Xclose = a:cchar . 'close' |
| 132 | let Xopen = a:cchar . 'open' |
| 133 | let Xgetexpr = a:cchar . 'getexpr' |
| 134 | |
| 135 | " Create a list with no valid entries |
| 136 | exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']" |
| 137 | |
| 138 | " Quickfix/Location window should not open with no valid errors |
| 139 | exe Xwindow |
| 140 | call assert_true(winnr('$') == 1) |
| 141 | |
| 142 | " Create a list with valid entries |
| 143 | exe Xgetexpr . " ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2', |
| 144 | \ 'Xtestfile3:3:1:Line3']" |
| 145 | |
| 146 | " Open the window |
| 147 | exe Xwindow |
| 148 | call assert_true(winnr('$') == 2 && winnr() == 2 && |
| 149 | \ getline('.') ==# 'Xtestfile1|1 col 3| Line1') |
| 150 | |
| 151 | " Close the window |
| 152 | exe Xclose |
| 153 | call assert_true(winnr('$') == 1) |
| 154 | |
| 155 | " Create a list with no valid entries |
| 156 | exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']" |
| 157 | |
| 158 | " Open the window |
| 159 | exe Xopen . ' 5' |
| 160 | call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1' |
| 161 | \ && winheight('.') == 5) |
| 162 | |
| 163 | " Opening the window again, should move the cursor to that window |
| 164 | wincmd t |
| 165 | exe Xopen . ' 7' |
| 166 | call assert_true(winnr('$') == 2 && winnr() == 2 && |
| 167 | \ winheight('.') == 7 && |
| 168 | \ getline('.') ==# '|| non-error 1') |
| 169 | |
| 170 | |
| 171 | " Calling cwindow should close the quickfix window with no valid errors |
| 172 | exe Xwindow |
| 173 | call assert_true(winnr('$') == 1) |
| 174 | endfunction |
| 175 | |
| 176 | function Test_cwindow() |
| 177 | call XwindowTests('c') |
| 178 | call XwindowTests('l') |
| 179 | endfunction |
| 180 | |
| 181 | " Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile |
| 182 | " commands. |
| 183 | function XfileTests(cchar) |
| 184 | let Xfile = a:cchar . 'file' |
| 185 | let Xgetfile = a:cchar . 'getfile' |
| 186 | let Xaddfile = a:cchar . 'addfile' |
| 187 | if a:cchar == 'c' |
| 188 | let Xgetlist = 'getqflist()' |
| 189 | else |
| 190 | let Xgetlist = 'getloclist(0)' |
| 191 | endif |
| 192 | |
| 193 | call writefile(['Xtestfile1:700:10:Line 700', |
| 194 | \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1') |
| 195 | |
| 196 | enew! |
| 197 | exe Xfile . ' Xqftestfile1' |
| 198 | exe 'let l = ' . Xgetlist |
| 199 | call assert_true(len(l) == 2 && |
| 200 | \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' && |
| 201 | \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800') |
| 202 | |
| 203 | " Run cfile/lfile from a modified buffer |
| 204 | enew! |
| 205 | silent! put ='Quickfix' |
| 206 | exe 'silent! ' . Xfile . ' Xqftestfile1' |
| 207 | call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)') |
| 208 | |
| 209 | call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1') |
| 210 | exe Xaddfile . ' Xqftestfile1' |
| 211 | exe 'let l = ' . Xgetlist |
| 212 | call assert_true(len(l) == 3 && |
| 213 | \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900') |
| 214 | |
| 215 | call writefile(['Xtestfile1:222:77:Line 222', |
| 216 | \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1') |
| 217 | |
| 218 | enew! |
| 219 | exe Xgetfile . ' Xqftestfile1' |
| 220 | exe 'let l = ' . Xgetlist |
| 221 | call assert_true(len(l) == 2 && |
| 222 | \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' && |
| 223 | \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333') |
| 224 | |
| 225 | call delete('Xqftestfile1') |
| 226 | endfunction |
| 227 | |
| 228 | function Test_cfile() |
| 229 | call XfileTests('c') |
| 230 | call XfileTests('l') |
| 231 | endfunction |
| 232 | |
| 233 | " Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and |
| 234 | " :lgetbuffer commands. |
| 235 | function XbufferTests(cchar) |
| 236 | let Xbuffer = a:cchar . 'buffer' |
| 237 | let Xgetbuffer = a:cchar . 'getbuffer' |
| 238 | let Xaddbuffer = a:cchar . 'addbuffer' |
| 239 | if a:cchar == 'c' |
| 240 | let Xgetlist = 'getqflist()' |
| 241 | else |
| 242 | let Xgetlist = 'getloclist(0)' |
| 243 | endif |
| 244 | |
| 245 | enew! |
| 246 | silent! call setline(1, ['Xtestfile7:700:10:Line 700', |
| 247 | \ 'Xtestfile8:800:15:Line 800']) |
| 248 | exe Xbuffer . "!" |
| 249 | exe 'let l = ' . Xgetlist |
| 250 | call assert_true(len(l) == 2 && |
| 251 | \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' && |
| 252 | \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800') |
| 253 | |
| 254 | enew! |
| 255 | silent! call setline(1, ['Xtestfile9:900:55:Line 900', |
| 256 | \ 'Xtestfile10:950:66:Line 950']) |
| 257 | exe Xgetbuffer |
| 258 | exe 'let l = ' . Xgetlist |
| 259 | call assert_true(len(l) == 2 && |
| 260 | \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' && |
| 261 | \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950') |
| 262 | |
| 263 | enew! |
| 264 | silent! call setline(1, ['Xtestfile11:700:20:Line 700', |
| 265 | \ 'Xtestfile12:750:25:Line 750']) |
| 266 | exe Xaddbuffer |
| 267 | exe 'let l = ' . Xgetlist |
| 268 | call assert_true(len(l) == 4 && |
| 269 | \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' && |
| 270 | \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' && |
| 271 | \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750') |
| 272 | |
| 273 | endfunction |
| 274 | |
| 275 | function Test_cbuffer() |
| 276 | call XbufferTests('c') |
| 277 | call XbufferTests('l') |
| 278 | endfunction |
| 279 | |
Bram Moolenaar | 75bdf6a | 2016-01-07 21:25:08 +0100 | [diff] [blame] | 280 | function Test_nomem() |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 281 | call alloc_fail(GetAllocId('qf_dirname_start'), 0, 0) |
Bram Moolenaar | a260b87 | 2016-01-15 20:48:22 +0100 | [diff] [blame] | 282 | call assert_fails('vimgrep vim runtest.vim', 'E342:') |
Bram Moolenaar | 75bdf6a | 2016-01-07 21:25:08 +0100 | [diff] [blame] | 283 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 284 | call alloc_fail(GetAllocId('qf_dirname_now'), 0, 0) |
Bram Moolenaar | a260b87 | 2016-01-15 20:48:22 +0100 | [diff] [blame] | 285 | call assert_fails('vimgrep vim runtest.vim', 'E342:') |
Bram Moolenaar | 75bdf6a | 2016-01-07 21:25:08 +0100 | [diff] [blame] | 286 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 287 | call alloc_fail(GetAllocId('qf_namebuf'), 0, 0) |
Bram Moolenaar | a260b87 | 2016-01-15 20:48:22 +0100 | [diff] [blame] | 288 | call assert_fails('cfile runtest.vim', 'E342:') |
Bram Moolenaar | 75bdf6a | 2016-01-07 21:25:08 +0100 | [diff] [blame] | 289 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 290 | call alloc_fail(GetAllocId('qf_errmsg'), 0, 0) |
Bram Moolenaar | a260b87 | 2016-01-15 20:48:22 +0100 | [diff] [blame] | 291 | call assert_fails('cfile runtest.vim', 'E342:') |
Bram Moolenaar | 75bdf6a | 2016-01-07 21:25:08 +0100 | [diff] [blame] | 292 | |
Bram Moolenaar | 28fb79d | 2016-01-09 22:28:33 +0100 | [diff] [blame] | 293 | call alloc_fail(GetAllocId('qf_pattern'), 0, 0) |
Bram Moolenaar | a260b87 | 2016-01-15 20:48:22 +0100 | [diff] [blame] | 294 | call assert_fails('cfile runtest.vim', 'E342:') |
Bram Moolenaar | 75bdf6a | 2016-01-07 21:25:08 +0100 | [diff] [blame] | 295 | |
| 296 | endfunc |
| 297 | |
Bram Moolenaar | 62ef797 | 2016-01-19 14:51:54 +0100 | [diff] [blame] | 298 | function Test_helpgrep() |
| 299 | helpgrep quickfix |
| 300 | copen |
| 301 | " This wipes out the buffer, make sure that doesn't cause trouble. |
| 302 | cclose |
| 303 | endfunc |
Bram Moolenaar | 75bdf6a | 2016-01-07 21:25:08 +0100 | [diff] [blame] | 304 | |
Bram Moolenaar | 6920c72 | 2016-01-22 22:44:10 +0100 | [diff] [blame] | 305 | func Test_errortitle() |
| 306 | augroup QfBufWinEnter |
| 307 | au! |
| 308 | au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE') |
| 309 | augroup END |
| 310 | copen |
| 311 | let a=[{'lnum': 308, 'bufnr': bufnr(''), 'col': 58, 'valid': 1, 'vcol': 0, 'nr': 0, 'type': '', 'pattern': '', 'text': ' au BufWinEnter * :let g:a=get(w:, ''quickfix_title'', ''NONE'')'}] |
| 312 | call setqflist(a) |
| 313 | call assert_equal(':setqflist()', g:a) |
| 314 | augroup QfBufWinEnter |
| 315 | au! |
| 316 | augroup END |
| 317 | augroup! QfBufWinEnter |
| 318 | endfunc |