blob: 0cea0e56c09ec4a0aca27601265269796e7322fe [file] [log] [blame]
Bram Moolenaarda59dd52016-01-05 21:59:58 +01001" Test for the quickfix commands.
2
3if !has('quickfix')
4 finish
5endif
6
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01007set encoding=utf-8
8
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02009function! s:setup_commands(cchar)
10 if a:cchar == 'c'
11 command! -nargs=* -bang Xlist <mods>clist<bang> <args>
12 command! -nargs=* Xgetexpr <mods>cgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020013 command! -nargs=* Xaddexpr <mods>caddexpr <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020014 command! -nargs=* Xolder <mods>colder <args>
15 command! -nargs=* Xnewer <mods>cnewer <args>
16 command! -nargs=* Xopen <mods>copen <args>
17 command! -nargs=* Xwindow <mods>cwindow <args>
18 command! -nargs=* Xclose <mods>cclose <args>
19 command! -nargs=* -bang Xfile <mods>cfile<bang> <args>
20 command! -nargs=* Xgetfile <mods>cgetfile <args>
21 command! -nargs=* Xaddfile <mods>caddfile <args>
22 command! -nargs=* -bang Xbuffer <mods>cbuffer<bang> <args>
23 command! -nargs=* Xgetbuffer <mods>cgetbuffer <args>
24 command! -nargs=* Xaddbuffer <mods>caddbuffer <args>
25 command! -nargs=* Xrewind <mods>crewind <args>
26 command! -nargs=* -bang Xnext <mods>cnext<bang> <args>
27 command! -nargs=* Xexpr <mods>cexpr <args>
28 command! -nargs=* Xvimgrep <mods>vimgrep <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020029 command! -nargs=* Xgrep <mods> grep <args>
30 command! -nargs=* Xgrepadd <mods> grepadd <args>
31 command! -nargs=* Xhelpgrep helpgrep <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020032 let g:Xgetlist = function('getqflist')
33 let g:Xsetlist = function('setqflist')
34 else
35 command! -nargs=* -bang Xlist <mods>llist<bang> <args>
36 command! -nargs=* Xgetexpr <mods>lgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020037 command! -nargs=* Xaddexpr <mods>laddexpr <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020038 command! -nargs=* Xolder <mods>lolder <args>
39 command! -nargs=* Xnewer <mods>lnewer <args>
40 command! -nargs=* Xopen <mods>lopen <args>
41 command! -nargs=* Xwindow <mods>lwindow <args>
42 command! -nargs=* Xclose <mods>lclose <args>
43 command! -nargs=* -bang Xfile <mods>lfile<bang> <args>
44 command! -nargs=* Xgetfile <mods>lgetfile <args>
45 command! -nargs=* Xaddfile <mods>laddfile <args>
46 command! -nargs=* -bang Xbuffer <mods>lbuffer<bang> <args>
47 command! -nargs=* Xgetbuffer <mods>lgetbuffer <args>
48 command! -nargs=* Xaddbuffer <mods>laddbuffer <args>
49 command! -nargs=* Xrewind <mods>lrewind <args>
50 command! -nargs=* -bang Xnext <mods>lnext<bang> <args>
51 command! -nargs=* Xexpr <mods>lexpr <args>
52 command! -nargs=* Xvimgrep <mods>lvimgrep <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020053 command! -nargs=* Xgrep <mods> lgrep <args>
54 command! -nargs=* Xgrepadd <mods> lgrepadd <args>
55 command! -nargs=* Xhelpgrep lhelpgrep <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020056 let g:Xgetlist = function('getloclist', [0])
57 let g:Xsetlist = function('setloclist', [0])
58 endif
59endfunction
60
Bram Moolenaarda59dd52016-01-05 21:59:58 +010061" Tests for the :clist and :llist commands
62function XlistTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020063 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +010064
65 " With an empty list, command should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020066 Xgetexpr []
67 silent! Xlist
Bram Moolenaarda59dd52016-01-05 21:59:58 +010068 call assert_true(v:errmsg ==# 'E42: No Errors')
69
70 " Populate the list and then try
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020071 Xgetexpr ['non-error 1', 'Xtestfile1:1:3:Line1',
Bram Moolenaarda59dd52016-01-05 21:59:58 +010072 \ 'non-error 2', 'Xtestfile2:2:2:Line2',
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020073 \ 'non-error 3', 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +010074
75 " List only valid entries
76 redir => result
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020077 Xlist
Bram Moolenaarda59dd52016-01-05 21:59:58 +010078 redir END
79 let l = split(result, "\n")
80 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
81 \ ' 4 Xtestfile2:2 col 2: Line2',
82 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
83
84 " List all the entries
85 redir => result
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020086 Xlist!
Bram Moolenaarda59dd52016-01-05 21:59:58 +010087 redir END
88 let l = split(result, "\n")
89 call assert_equal([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1',
90 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2',
91 \ ' 5: non-error 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
92
93 " List a range of errors
94 redir => result
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020095 Xlist 3,6
Bram Moolenaarda59dd52016-01-05 21:59:58 +010096 redir END
97 let l = split(result, "\n")
98 call assert_equal([' 4 Xtestfile2:2 col 2: Line2',
99 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
100
101 redir => result
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200102 Xlist! 3,4
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100103 redir END
104 let l = split(result, "\n")
105 call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
106
107 redir => result
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200108 Xlist -6,-4
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100109 redir END
110 let l = split(result, "\n")
111 call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l)
112
113 redir => result
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200114 Xlist! -5,-3
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100115 redir END
116 let l = split(result, "\n")
117 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
118 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
119endfunction
120
121function Test_clist()
122 call XlistTests('c')
123 call XlistTests('l')
124endfunction
125
126" Tests for the :colder, :cnewer, :lolder and :lnewer commands
127" Note that this test assumes that a quickfix/location list is
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100128" already set by the caller.
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100129function XageTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200130 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100131
132 " Jumping to a non existent list should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200133 silent! Xolder 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100134 call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
135
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200136 silent! Xnewer 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100137 call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
138
139 " Add three quickfix/location lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200140 Xgetexpr ['Xtestfile1:1:3:Line1']
141 Xgetexpr ['Xtestfile2:2:2:Line2']
142 Xgetexpr ['Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100143
144 " Go back two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200145 Xolder
146 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100147 call assert_equal('Line2', l[0].text)
148
149 " Go forward two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200150 Xnewer
151 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100152 call assert_equal('Line3', l[0].text)
153
154 " Test for the optional count argument
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200155 Xolder 2
156 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100157 call assert_equal('Line1', l[0].text)
158
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200159 Xnewer 2
160 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100161 call assert_equal('Line3', l[0].text)
162endfunction
163
164function Test_cage()
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100165 let list = [{'bufnr': 1, 'lnum': 1}]
166 call setqflist(list)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100167 call XageTests('c')
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100168
169 call setloclist(0, list)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100170 call XageTests('l')
171endfunction
172
173" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
174" commands
175function XwindowTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200176 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100177
178 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200179 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100180
181 " Quickfix/Location window should not open with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200182 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100183 call assert_true(winnr('$') == 1)
184
185 " Create a list with valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200186 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
187 \ 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100188
189 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200190 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100191 call assert_true(winnr('$') == 2 && winnr() == 2 &&
192 \ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
193
194 " Close the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200195 Xclose
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100196 call assert_true(winnr('$') == 1)
197
198 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200199 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100200
201 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200202 Xopen 5
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100203 call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
204 \ && winheight('.') == 5)
205
206 " Opening the window again, should move the cursor to that window
207 wincmd t
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200208 Xopen 7
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100209 call assert_true(winnr('$') == 2 && winnr() == 2 &&
210 \ winheight('.') == 7 &&
211 \ getline('.') ==# '|| non-error 1')
212
213
214 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200215 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100216 call assert_true(winnr('$') == 1)
217endfunction
218
219function Test_cwindow()
220 call XwindowTests('c')
221 call XwindowTests('l')
222endfunction
223
224" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
225" commands.
226function XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200227 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100228
229 call writefile(['Xtestfile1:700:10:Line 700',
230 \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
231
232 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200233 Xfile Xqftestfile1
234 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100235 call assert_true(len(l) == 2 &&
236 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
237 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
238
Bram Moolenaar049cba92016-06-26 14:38:04 +0200239 " Test with a non existent file
240 call assert_fails('Xfile non_existent_file', 'E40')
241
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100242 " Run cfile/lfile from a modified buffer
243 enew!
244 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200245 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100246 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
247
248 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200249 Xaddfile Xqftestfile1
250 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100251 call assert_true(len(l) == 3 &&
252 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
253
254 call writefile(['Xtestfile1:222:77:Line 222',
255 \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
256
257 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200258 Xgetfile Xqftestfile1
259 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100260 call assert_true(len(l) == 2 &&
261 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
262 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
263
264 call delete('Xqftestfile1')
265endfunction
266
267function Test_cfile()
268 call XfileTests('c')
269 call XfileTests('l')
270endfunction
271
272" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
273" :lgetbuffer commands.
274function XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200275 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100276
277 enew!
278 silent! call setline(1, ['Xtestfile7:700:10:Line 700',
279 \ 'Xtestfile8:800:15:Line 800'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200280 Xbuffer!
281 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100282 call assert_true(len(l) == 2 &&
283 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
284 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
285
286 enew!
287 silent! call setline(1, ['Xtestfile9:900:55:Line 900',
288 \ 'Xtestfile10:950:66:Line 950'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200289 Xgetbuffer
290 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100291 call assert_true(len(l) == 2 &&
292 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
293 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
294
295 enew!
296 silent! call setline(1, ['Xtestfile11:700:20:Line 700',
297 \ 'Xtestfile12:750:25:Line 750'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200298 Xaddbuffer
299 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100300 call assert_true(len(l) == 4 &&
301 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
302 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
303 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200304 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100305
306endfunction
307
308function Test_cbuffer()
309 call XbufferTests('c')
310 call XbufferTests('l')
311endfunction
312
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100313function Test_nomem()
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200314 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100315 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100316
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200317 call test_alloc_fail(GetAllocId('qf_dirname_now'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100318 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100319
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200320 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100321 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100322
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200323 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100324 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100325
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200326 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100327 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100328
329endfunc
330
Bram Moolenaar049cba92016-06-26 14:38:04 +0200331function! s:test_xhelpgrep(cchar)
332 call s:setup_commands(a:cchar)
333 Xhelpgrep quickfix
334 Xopen
335 if a:cchar == 'c'
336 let title_text = ':helpgrep quickfix'
337 else
338 let title_text = ':lhelpgrep quickfix'
339 endif
340 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100341 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200342 Xclose
343endfunction
344
345function Test_helpgrep()
346 call s:test_xhelpgrep('c')
347 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100348endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100349
Bram Moolenaar6920c722016-01-22 22:44:10 +0100350func Test_errortitle()
351 augroup QfBufWinEnter
352 au!
353 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
354 augroup END
355 copen
356 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'')'}]
357 call setqflist(a)
358 call assert_equal(':setqflist()', g:a)
359 augroup QfBufWinEnter
360 au!
361 augroup END
362 augroup! QfBufWinEnter
363endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100364
Bram Moolenaar5584df62016-03-18 21:00:51 +0100365func Test_vimgreptitle()
366 augroup QfBufWinEnter
367 au!
368 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
369 augroup END
370 try
371 vimgrep /pattern/j file
372 catch /E480/
373 endtry
374 copen
375 call assert_equal(': vimgrep /pattern/j file', g:a)
376 augroup QfBufWinEnter
377 au!
378 augroup END
379 augroup! QfBufWinEnter
380endfunc
381
Bram Moolenaare27dba42016-03-15 14:11:10 +0100382function XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200383 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +0100384
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200385 Xgetexpr ['file:1:1:message']
386 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100387 if a:cchar == 'c'
388 call setqflist(l, 'r')
389 else
390 call setloclist(0, l, 'r')
391 endif
392
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200393 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +0100394 if a:cchar == 'c'
395 let title = ':setqflist()'
396 else
397 let title = ':setloclist()'
398 endif
399 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200400 Xclose
Bram Moolenaare27dba42016-03-15 14:11:10 +0100401endfunction
402
403" Tests for quickfix window's title
404function Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200405 call XqfTitleTests('c')
406 call XqfTitleTests('l')
Bram Moolenaare27dba42016-03-15 14:11:10 +0100407endfunction
408
409" Tests for 'errorformat'
410function Test_efm()
411 let save_efm = &efm
412 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
413 cgetexpr ['WWWW', 'EEEE', 'CCCC']
414 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
415 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
416 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
417 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
418 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
419 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
420 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
421 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
422 let &efm = save_efm
423endfunction
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100424
425" This will test for problems in quickfix:
426" A. incorrectly copying location lists which caused the location list to show
427" a different name than the file that was actually being displayed.
428" B. not reusing the window for which the location list window is opened but
429" instead creating new windows.
430" C. make sure that the location list window is not reused instead of the
431" window it belongs to.
432"
433" Set up the test environment:
434function! ReadTestProtocol(name)
435 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
436 let word = substitute(base, '\v(.*)\..*', '\1', '')
437
438 setl modifiable
439 setl noreadonly
440 setl noswapfile
441 setl bufhidden=delete
442 %del _
443 " For problem 2:
444 " 'buftype' has to be set to reproduce the constant opening of new windows
445 setl buftype=nofile
446
447 call setline(1, word)
448
449 setl nomodified
450 setl nomodifiable
451 setl readonly
452 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
453endfunction
454
455function Test_locationlist()
456 enew
457
458 augroup testgroup
459 au!
460 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
461 augroup END
462
463 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
464
465 let qflist = []
466 for word in words
467 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
468 " NOTE: problem 1:
469 " intentionally not setting 'lnum' so that the quickfix entries are not
470 " valid
471 call setloclist(0, qflist, ' ')
472 endfor
473
474 " Test A
475 lrewind
476 enew
477 lopen
478 lnext
479 lnext
480 lnext
481 lnext
482 vert split
483 wincmd L
484 lopen
485 wincmd p
486 lnext
487 let fileName = expand("%")
488 wincmd p
489 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
490 let fileName = substitute(fileName, '\\', '/', 'g')
491 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
492 call assert_equal("test://bar.txt", fileName)
493 call assert_equal("test://bar.txt", locationListFileName)
494
495 wincmd n | only
496
497 " Test B:
498 lrewind
499 lopen
500 2
501 exe "normal \<CR>"
502 wincmd p
503 3
504 exe "normal \<CR>"
505 wincmd p
506 4
507 exe "normal \<CR>"
508 call assert_equal(2, winnr('$'))
509 wincmd n | only
510
511 " Test C:
512 lrewind
513 lopen
514 " Let's move the location list window to the top to check whether it (the
515 " first window found) will be reused when we try to open new windows:
516 wincmd K
517 2
518 exe "normal \<CR>"
519 wincmd p
520 3
521 exe "normal \<CR>"
522 wincmd p
523 4
524 exe "normal \<CR>"
525 1wincmd w
526 call assert_equal('quickfix', &buftype)
527 2wincmd w
528 let bufferName = expand("%")
529 let bufferName = substitute(bufferName, '\\', '/', 'g')
530 call assert_equal('test://quux.txt', bufferName)
531
532 wincmd n | only
533
534 augroup! testgroup
535endfunction
Bram Moolenaar0899d692016-03-19 13:35:03 +0100536
537function Test_locationlist_curwin_was_closed()
538 augroup testgroup
539 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200540 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
Bram Moolenaar0899d692016-03-19 13:35:03 +0100541 augroup END
542
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100543 function! R(n)
Bram Moolenaar0899d692016-03-19 13:35:03 +0100544 quit
545 endfunc
546
547 new
548 let q = []
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200549 call add(q, {'filename': 'test_curwin.txt' })
Bram Moolenaar0899d692016-03-19 13:35:03 +0100550 call setloclist(0, q)
551 call assert_fails('lrewind', 'E924:')
552
553 augroup! testgroup
554endfunction
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100555
556" More tests for 'errorformat'
557function! Test_efm1()
558 if !has('unix')
559 " The 'errorformat' setting is different on non-Unix systems.
560 " This test works only on Unix-like systems.
561 return
562 endif
563
564 let l = [
565 \ '"Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.',
566 \ '"Xtestfile", line 6 col 19; this is an error',
567 \ 'gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c',
568 \ 'Xtestfile:9: parse error before `asd''',
569 \ 'make: *** [vim] Error 1',
570 \ 'in file "Xtestfile" linenr 10: there is an error',
571 \ '',
572 \ '2 returned',
573 \ '"Xtestfile", line 11 col 1; this is an error',
574 \ '"Xtestfile", line 12 col 2; this is another error',
575 \ '"Xtestfile", line 14:10; this is an error in column 10',
576 \ '=Xtestfile=, line 15:10; this is another error, but in vcol 10 this time',
577 \ '"Xtestfile", linenr 16: yet another problem',
578 \ 'Error in "Xtestfile" at line 17:',
579 \ 'x should be a dot',
580 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17',
581 \ ' ^',
582 \ 'Error in "Xtestfile" at line 18:',
583 \ 'x should be a dot',
584 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18',
585 \ '.............^',
586 \ 'Error in "Xtestfile" at line 19:',
587 \ 'x should be a dot',
588 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19',
589 \ '--------------^',
590 \ 'Error in "Xtestfile" at line 20:',
591 \ 'x should be a dot',
592 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20',
593 \ ' ^',
594 \ '',
595 \ 'Does anyone know what is the problem and how to correction it?',
596 \ '"Xtestfile", line 21 col 9: What is the title of the quickfix window?',
597 \ '"Xtestfile", line 22 col 9: What is the title of the quickfix window?'
598 \ ]
599
600 call writefile(l, 'Xerrorfile1')
601 call writefile(l[:-2], 'Xerrorfile2')
602
603 let m = [
604 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2',
605 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3',
606 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4',
607 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5',
608 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6',
609 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7',
610 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8',
611 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9',
612 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10',
613 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11',
614 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12',
615 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13',
616 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14',
617 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15',
618 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16',
619 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17',
620 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18',
621 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19',
622 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20',
623 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21',
624 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22'
625 \ ]
626 call writefile(m, 'Xtestfile')
627
628 let save_efm = &efm
629 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
630 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
631
632 exe 'cf Xerrorfile2'
633 clast
634 copen
635 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
636 wincmd p
637
638 exe 'cf Xerrorfile1'
639 call assert_equal([4, 12], [line('.'), col('.')])
640 cn
641 call assert_equal([6, 19], [line('.'), col('.')])
642 cn
643 call assert_equal([9, 2], [line('.'), col('.')])
644 cn
645 call assert_equal([10, 2], [line('.'), col('.')])
646 cn
647 call assert_equal([11, 1], [line('.'), col('.')])
648 cn
649 call assert_equal([12, 2], [line('.'), col('.')])
650 cn
651 call assert_equal([14, 10], [line('.'), col('.')])
652 cn
653 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
654 cn
655 call assert_equal([16, 2], [line('.'), col('.')])
656 cn
657 call assert_equal([17, 6], [line('.'), col('.')])
658 cn
659 call assert_equal([18, 7], [line('.'), col('.')])
660 cn
661 call assert_equal([19, 8], [line('.'), col('.')])
662 cn
663 call assert_equal([20, 9], [line('.'), col('.')])
664 clast
665 cprev
666 cprev
667 wincmd w
668 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
669 wincmd p
670
671 let &efm = save_efm
672 call delete('Xerrorfile1')
673 call delete('Xerrorfile2')
674 call delete('Xtestfile')
675endfunction
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100676
Bram Moolenaarab47c612016-06-14 22:02:26 +0200677" Test for quickfix directory stack support
678function! s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200679 call s:setup_commands(a:cchar)
680
Bram Moolenaarab47c612016-06-14 22:02:26 +0200681 let save_efm=&efm
682 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
683
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200684 let lines = ["Entering dir 'dir1/a'",
685 \ 'habits2.txt:1:Nine Healthy Habits',
686 \ "Entering dir 'b'",
687 \ 'habits3.txt:2:0 Hours of television',
688 \ 'habits2.txt:7:5 Small meals',
689 \ "Entering dir 'dir1/c'",
690 \ 'habits4.txt:3:1 Hour of exercise',
691 \ "Leaving dir 'dir1/c'",
692 \ "Leaving dir 'dir1/a'",
693 \ 'habits1.txt:4:2 Liters of water',
694 \ "Entering dir 'dir2'",
695 \ 'habits5.txt:5:3 Cups of hot green tea',
696 \ "Leaving dir 'dir2'"
697 \]
Bram Moolenaarab47c612016-06-14 22:02:26 +0200698
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200699 Xexpr ""
700 for l in lines
701 Xaddexpr l
702 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +0200703
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200704 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +0200705
706 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
707 call assert_equal(1, qf[1].lnum)
708 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
709 call assert_equal(2, qf[3].lnum)
710 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
711 call assert_equal(7, qf[4].lnum)
712 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
713 call assert_equal(3, qf[6].lnum)
714 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
715 call assert_equal(4, qf[9].lnum)
716 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
717 call assert_equal(5, qf[11].lnum)
718
719 let &efm=save_efm
720endfunction
721
722" Tests for %D and %X errorformat options
723function! Test_efm_dirstack()
724 " Create the directory stack and files
725 call mkdir('dir1')
726 call mkdir('dir1/a')
727 call mkdir('dir1/a/b')
728 call mkdir('dir1/c')
729 call mkdir('dir2')
730
731 let lines = ["Nine Healthy Habits",
732 \ "0 Hours of television",
733 \ "1 Hour of exercise",
734 \ "2 Liters of water",
735 \ "3 Cups of hot green tea",
736 \ "4 Short mental breaks",
737 \ "5 Small meals",
738 \ "6 AM wake up time",
739 \ "7 Minutes of laughter",
740 \ "8 Hours of sleep (at least)",
741 \ "9 PM end of the day and off to bed"
742 \ ]
743 call writefile(lines, 'habits1.txt')
744 call writefile(lines, 'dir1/a/habits2.txt')
745 call writefile(lines, 'dir1/a/b/habits3.txt')
746 call writefile(lines, 'dir1/c/habits4.txt')
747 call writefile(lines, 'dir2/habits5.txt')
748
749 call s:dir_stack_tests('c')
750 call s:dir_stack_tests('l')
751
752 call delete('dir1', 'rf')
753 call delete('dir2', 'rf')
754 call delete('habits1.txt')
755endfunction
756
Bram Moolenaar049cba92016-06-26 14:38:04 +0200757" TODO:
758" Add tests for the following formats in 'errorformat'
759" %n %t %r %+ %- %O
760function! Test_efm2()
761 let save_efm = &efm
762
763 " Test for invalid efm
764 set efm=%L%M%N
765 call assert_fails('cexpr "abc.txt:1:Hello world"', 'E376:')
766 call assert_fails('lexpr "abc.txt:1:Hello world"', 'E376:')
767
768 " Test for %s format in efm
769 set efm=%f:%s
770 cexpr 'Xtestfile:Line search text'
771
772 let l = getqflist()
773 call assert_equal(l[0].pattern, '^\VLine search text\$')
774 call assert_equal(l[0].lnum, 0)
775
776 let lines=["[Xtestfile1]",
777 \ "(1,17) error: ';' missing",
778 \ "(21,2) warning: variable 'z' not defined",
779 \ "(67,3) error: end of file found before string ended",
780 \ "",
781 \ "[Xtestfile2]",
782 \ "",
783 \ "[Xtestfile3]",
784 \ "NEW compiler v1.1",
785 \ "(2,2) warning: variable 'x' not defined",
786 \ "(67,3) warning: 's' already defined"
787 \]
788 set efm=%+P[%f],(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%-Q
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200789 cexpr ""
790 for l in lines
791 caddexpr l
792 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +0200793 let l = getqflist()
794 call assert_equal(9, len(l))
795 call assert_equal(21, l[2].lnum)
796 call assert_equal(2, l[2].col)
797
798 let &efm = save_efm
799endfunction
800
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100801function XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200802 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100803 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100804 let ErrorNr = 'E925'
805 function! ReadFunc()
806 colder
807 cgetexpr []
808 endfunc
809 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100810 let ErrorNr = 'E926'
811 function! ReadFunc()
812 lolder
813 lgetexpr []
814 endfunc
815 endif
816
817 augroup testgroup
818 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200819 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100820 augroup END
821
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200822 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100823 let words = [ "a", "b" ]
824 let qflist = []
825 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200826 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200827 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100828 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200829 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100830
831 augroup! testgroup
832endfunc
833
834function Test_quickfix_was_changed_by_autocmd()
835 call XquickfixChangedByAutocmd('c')
836 call XquickfixChangedByAutocmd('l')
837endfunction
Bram Moolenaar8b201792016-03-25 15:01:10 +0100838
839func Test_caddbuffer_to_empty()
840 helpgr quickfix
841 call setqflist([], 'r')
842 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +0100843 try
844 cn
845 catch
846 " number of matches is unknown
847 call assert_true(v:exception =~ 'E553:')
848 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +0100849 quit!
850endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +0200851
852func Test_cgetexpr_works()
853 " this must not crash Vim
854 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200855 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +0200856endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200857
858" Tests for the setqflist() and setloclist() functions
859function SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200860 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200861
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200862 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200863 \ {'bufnr': a:bnum, 'lnum': 2}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200864 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200865 call assert_equal(2, len(l))
866 call assert_equal(2, l[1].lnum)
867
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200868 Xnext
869 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
870 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200871 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200872 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200873 call assert_equal(3, line('.'))
874
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200875 " Appending entries to the list should not change the cursor position
876 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200877 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200878 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200879 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +0200880 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
881 call assert_equal(1, line('.'))
882 close
883
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200884 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200885 \ {'bufnr': a:bnum, 'lnum': 4},
886 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200887 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200888 call assert_equal(3, len(l))
889 call assert_equal(5, l[2].lnum)
890
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200891 call g:Xsetlist([])
892 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200893 call assert_equal(0, len(l))
894endfunction
895
896function Test_setqflist()
897 new Xtestfile | only
898 let bnum = bufnr('%')
899 call setline(1, range(1,5))
900
901 call SetXlistTests('c', bnum)
902 call SetXlistTests('l', bnum)
903
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200904 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +0200905 call delete('Xtestfile')
906endfunction
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200907
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200908function Xlist_empty_middle(cchar)
909 call s:setup_commands(a:cchar)
910
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200911 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200912 Xvimgrep Test_ test_quickfix.vim
913 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200914 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200915 Xvimgrep empty test_quickfix.vim
916 call assert_true(len(g:Xgetlist()) > 0)
917 Xvimgrep matches test_quickfix.vim
918 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200919 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200920 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200921 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200922 call g:Xsetlist([], 'r')
923 call assert_true(len(g:Xgetlist()) == 0)
924 Xolder
925 call assert_equal(testlen, len(g:Xgetlist()))
926 Xnewer
927 Xnewer
928 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200929endfunc
930
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200931function Test_setqflist_empty_middle()
932 call Xlist_empty_middle('c')
933 call Xlist_empty_middle('l')
934endfunction
935
936function Xlist_empty_older(cchar)
937 call s:setup_commands(a:cchar)
938
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200939 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200940 Xvimgrep one test_quickfix.vim
941 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200942 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200943 Xvimgrep two test_quickfix.vim
944 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200945 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200946 Xvimgrep three test_quickfix.vim
947 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200948 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200949 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200950 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200951 call g:Xsetlist([], 'r')
952 call assert_true(len(g:Xgetlist()) == 0)
953 Xnewer
954 call assert_equal(twolen, len(g:Xgetlist()))
955 Xnewer
956 call assert_equal(threelen, len(g:Xgetlist()))
957endfunction
958
959function Test_setqflist_empty_older()
960 call Xlist_empty_older('c')
961 call Xlist_empty_older('l')
962endfunction
Bram Moolenaar1cee6932016-06-09 22:17:22 +0200963
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200964function! XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200965 call s:setup_commands(a:cchar)
966
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200967 let list1 = [{'filename': 'fnameA', 'text': 'A'},
968 \ {'filename': 'fnameB', 'text': 'B'}]
969 let list2 = [{'filename': 'fnameC', 'text': 'C'},
970 \ {'filename': 'fnameD', 'text': 'D'},
971 \ {'filename': 'fnameE', 'text': 'E'}]
972
973 " {action} is unspecified. Same as specifing ' '.
974 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200975 silent! Xnewer 99
976 call g:Xsetlist(list1)
977 call g:Xsetlist(list2)
978 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200979 call assert_equal(3, len(li))
980 call assert_equal('C', li[0]['text'])
981 call assert_equal('D', li[1]['text'])
982 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200983 silent! Xolder
984 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200985 call assert_equal(2, len(li))
986 call assert_equal('A', li[0]['text'])
987 call assert_equal('B', li[1]['text'])
988
989 " {action} is specified ' '.
990 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200991 silent! Xnewer 99
992 call g:Xsetlist(list1)
993 call g:Xsetlist(list2, ' ')
994 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200995 call assert_equal(3, len(li))
996 call assert_equal('C', li[0]['text'])
997 call assert_equal('D', li[1]['text'])
998 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200999 silent! Xolder
1000 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001001 call assert_equal(2, len(li))
1002 call assert_equal('A', li[0]['text'])
1003 call assert_equal('B', li[1]['text'])
1004
1005 " {action} is specified 'a'.
1006 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001007 silent! Xnewer 99
1008 call g:Xsetlist(list1)
1009 call g:Xsetlist(list2, 'a')
1010 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001011 call assert_equal(5, len(li))
1012 call assert_equal('A', li[0]['text'])
1013 call assert_equal('B', li[1]['text'])
1014 call assert_equal('C', li[2]['text'])
1015 call assert_equal('D', li[3]['text'])
1016 call assert_equal('E', li[4]['text'])
1017
1018 " {action} is specified 'r'.
1019 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001020 silent! Xnewer 99
1021 call g:Xsetlist(list1)
1022 call g:Xsetlist(list2, 'r')
1023 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001024 call assert_equal(3, len(li))
1025 call assert_equal('C', li[0]['text'])
1026 call assert_equal('D', li[1]['text'])
1027 call assert_equal('E', li[2]['text'])
1028
1029 " Test for wrong value.
1030 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001031 call assert_fails("call g:Xsetlist(0)", 'E714:')
1032 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
1033 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
1034 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
1035 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001036endfunc
1037
1038function Test_quickfix_set_list_with_act()
1039 call XquickfixSetListWithAct('c')
1040 call XquickfixSetListWithAct('l')
1041endfunction
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001042
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001043function XLongLinesTests(cchar)
1044 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001045
Bram Moolenaar049cba92016-06-26 14:38:04 +02001046 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001047 call assert_equal(1, l[0].lnum)
1048 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001049 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001050 call assert_equal(2, l[1].lnum)
1051 call assert_equal(1, l[1].col)
1052 call assert_equal(4070, len(l[1].text))
1053 call assert_equal(3, l[2].lnum)
1054 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001055 call assert_equal(4070, len(l[2].text))
1056 call assert_equal(4, l[3].lnum)
1057 call assert_equal(1, l[3].col)
1058 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001059
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001060 call g:Xsetlist([], 'r')
1061endfunction
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001062
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001063function s:long_lines_tests(cchar)
1064 call s:setup_commands(a:cchar)
1065
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001066 let testfile = 'samples/quickfix.txt'
1067
1068 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001069 exe 'Xgetfile' testfile
1070 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001071
1072 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001073 Xexpr readfile(testfile)
1074 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001075
1076 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001077 Xexpr join(readfile(testfile), "\n")
1078 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001079
1080 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001081 exe 'edit' testfile
1082 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02001083 call XLongLinesTests(a:cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001084endfunction
1085
1086function Test_long_lines()
1087 call s:long_lines_tests('c')
1088 call s:long_lines_tests('l')
1089endfunction
Bram Moolenaar049cba92016-06-26 14:38:04 +02001090
1091function! s:create_test_file(filename)
1092 let l = []
1093 for i in range(1, 20)
1094 call add(l, 'Line' . i)
1095 endfor
1096 call writefile(l, a:filename)
1097endfunction
1098
1099function! Test_switchbuf()
1100 call s:create_test_file('Xqftestfile1')
1101 call s:create_test_file('Xqftestfile2')
1102 call s:create_test_file('Xqftestfile3')
1103
1104 new | only
1105 edit Xqftestfile1
1106 let file1_winid = win_getid()
1107 new Xqftestfile2
1108 let file2_winid = win_getid()
1109 cgetexpr ['Xqftestfile1:5:Line5',
1110 \ 'Xqftestfile1:6:Line6',
1111 \ 'Xqftestfile2:10:Line10',
1112 \ 'Xqftestfile2:11:Line11',
1113 \ 'Xqftestfile3:15:Line15',
1114 \ 'Xqftestfile3:16:Line16']
1115
1116 new
1117 let winid = win_getid()
1118 cfirst | cnext
1119 call assert_equal(winid, win_getid())
1120 cnext | cnext
1121 call assert_equal(winid, win_getid())
1122 cnext | cnext
1123 call assert_equal(winid, win_getid())
1124 enew
1125
1126 set switchbuf=useopen
1127 cfirst | cnext
1128 call assert_equal(file1_winid, win_getid())
1129 cnext | cnext
1130 call assert_equal(file2_winid, win_getid())
1131 cnext | cnext
1132 call assert_equal(file2_winid, win_getid())
1133
1134 enew | only
1135 set switchbuf=usetab
1136 tabedit Xqftestfile1
1137 tabedit Xqftestfile2
1138 tabfirst
1139 cfirst | cnext
1140 call assert_equal(2, tabpagenr())
1141 cnext | cnext
1142 call assert_equal(3, tabpagenr())
1143 cnext | cnext
1144 call assert_equal(3, tabpagenr())
1145 tabfirst | tabonly | enew
1146
1147 set switchbuf=split
1148 cfirst | cnext
1149 call assert_equal(1, winnr('$'))
1150 cnext | cnext
1151 call assert_equal(2, winnr('$'))
1152 cnext | cnext
1153 call assert_equal(3, winnr('$'))
1154 enew | only
1155
1156 set switchbuf=newtab
1157 cfirst | cnext
1158 call assert_equal(1, tabpagenr('$'))
1159 cnext | cnext
1160 call assert_equal(2, tabpagenr('$'))
1161 cnext | cnext
1162 call assert_equal(3, tabpagenr('$'))
1163 tabfirst | enew | tabonly | only
1164
1165 set switchbuf=
1166 edit Xqftestfile1
1167 let file1_winid = win_getid()
1168 new Xqftestfile2
1169 let file2_winid = win_getid()
1170 copen
1171 exe "normal 1G\<CR>"
1172 call assert_equal(file1_winid, win_getid())
1173 copen
1174 exe "normal 3G\<CR>"
1175 call assert_equal(file2_winid, win_getid())
1176 copen | only
1177 exe "normal 5G\<CR>"
1178 call assert_equal(2, winnr('$'))
1179 call assert_equal(1, bufwinnr('Xqftestfile3'))
1180
1181 enew | only
1182
1183 call delete('Xqftestfile1')
1184 call delete('Xqftestfile2')
1185 call delete('Xqftestfile3')
1186endfunction
1187
1188function! Xadjust_qflnum(cchar)
1189 call s:setup_commands(a:cchar)
1190
1191 enew | only
1192
1193 call s:create_test_file('Xqftestfile')
1194 edit Xqftestfile
1195
1196 Xgetexpr ['Xqftestfile:5:Line5',
1197 \ 'Xqftestfile:10:Line10',
1198 \ 'Xqftestfile:15:Line15',
1199 \ 'Xqftestfile:20:Line20']
1200
1201 6,14delete
1202 call append(6, ['Buffer', 'Window'])
1203
1204 let l = g:Xgetlist()
1205
1206 call assert_equal(5, l[0].lnum)
1207 call assert_equal(6, l[2].lnum)
1208 call assert_equal(13, l[3].lnum)
1209
1210 enew!
1211 call delete('Xqftestfile')
1212endfunction
1213
1214function! Test_adjust_lnum()
1215 call Xadjust_qflnum('c')
1216 call Xadjust_qflnum('l')
1217endfunction
1218
1219" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
1220function! s:test_xgrep(cchar)
1221 call s:setup_commands(a:cchar)
1222
1223 " The following lines are used for the grep test. Don't remove.
1224 " Grep_Test_Text: Match 1
1225 " Grep_Test_Text: Match 2
1226 " GrepAdd_Test_Text: Match 1
1227 " GrepAdd_Test_Text: Match 2
1228 enew! | only
1229 set makeef&vim
1230 silent Xgrep Grep_Test_Text: test_quickfix.vim
1231 call assert_true(len(g:Xgetlist()) == 3)
1232 Xopen
1233 call assert_true(w:quickfix_title =~ '^:grep')
1234 Xclose
1235 enew
1236 set makeef=Temp_File_##
1237 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
1238 call assert_true(len(g:Xgetlist()) == 6)
1239endfunction
1240
1241function! Test_grep()
1242 if !has('unix')
1243 " The grepprg may not be set on non-Unix systems
1244 return
1245 endif
1246
1247 call s:test_xgrep('c')
1248 call s:test_xgrep('l')
1249endfunction
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001250
1251function! Test_two_windows()
1252 " Use one 'errorformat' for two windows. Add an expression to each of them,
1253 " make sure they each keep their own state.
1254 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1255 call mkdir('Xone/a', 'p')
1256 call mkdir('Xtwo/a', 'p')
1257 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
1258 call writefile(lines, 'Xone/a/one.txt')
1259 call writefile(lines, 'Xtwo/a/two.txt')
1260
1261 new one
1262 let one_id = win_getid()
1263 lexpr ""
1264 new two
1265 let two_id = win_getid()
1266 lexpr ""
1267
1268 laddexpr "Entering dir 'Xtwo/a'"
1269 call win_gotoid(one_id)
1270 laddexpr "Entering dir 'Xone/a'"
1271 call win_gotoid(two_id)
1272 laddexpr 'two.txt:5:two two two'
1273 call win_gotoid(one_id)
1274 laddexpr 'one.txt:3:one one one'
1275
1276 let loc_one = getloclist(one_id)
1277echo string(loc_one)
1278 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
1279 call assert_equal(3, loc_one[1].lnum)
1280
1281 let loc_two = getloclist(two_id)
1282echo string(loc_two)
1283 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
1284 call assert_equal(5, loc_two[1].lnum)
1285
1286 call win_gotoid(one_id)
1287 bwipe!
1288 call win_gotoid(two_id)
1289 bwipe!
1290 call delete('Xone', 'rf')
1291 call delete('Xtwo', 'rf')
1292endfunc