blob: 671381ce1950b95d43c06360fa9c80c1fa076b36 [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 Moolenaar2b946c92016-11-12 18:14:44 +01009func s:setup_commands(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020010 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 Moolenaar55b69262017-08-13 13:42:01 +020014 command! -nargs=* -count Xolder <mods><count>colder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020015 command! -nargs=* Xnewer <mods>cnewer <args>
16 command! -nargs=* Xopen <mods>copen <args>
17 command! -nargs=* Xwindow <mods>cwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020018 command! -nargs=* Xbottom <mods>cbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020019 command! -nargs=* Xclose <mods>cclose <args>
20 command! -nargs=* -bang Xfile <mods>cfile<bang> <args>
21 command! -nargs=* Xgetfile <mods>cgetfile <args>
22 command! -nargs=* Xaddfile <mods>caddfile <args>
23 command! -nargs=* -bang Xbuffer <mods>cbuffer<bang> <args>
24 command! -nargs=* Xgetbuffer <mods>cgetbuffer <args>
25 command! -nargs=* Xaddbuffer <mods>caddbuffer <args>
26 command! -nargs=* Xrewind <mods>crewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020027 command! -count -nargs=* -bang Xnext <mods><count>cnext<bang> <args>
28 command! -count -nargs=* -bang Xprev <mods><count>cprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020029 command! -nargs=* -bang Xfirst <mods>cfirst<bang> <args>
30 command! -nargs=* -bang Xlast <mods>clast<bang> <args>
31 command! -nargs=* -bang Xnfile <mods>cnfile<bang> <args>
32 command! -nargs=* -bang Xpfile <mods>cpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020033 command! -nargs=* Xexpr <mods>cexpr <args>
Bram Moolenaaree85df32017-03-19 14:19:50 +010034 command! -range -nargs=* Xvimgrep <mods><count>vimgrep <args>
35 command! -nargs=* Xvimgrepadd <mods>vimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020036 command! -nargs=* Xgrep <mods> grep <args>
37 command! -nargs=* Xgrepadd <mods> grepadd <args>
38 command! -nargs=* Xhelpgrep helpgrep <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020039 let g:Xgetlist = function('getqflist')
40 let g:Xsetlist = function('setqflist')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020041 call setqflist([], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020042 else
43 command! -nargs=* -bang Xlist <mods>llist<bang> <args>
44 command! -nargs=* Xgetexpr <mods>lgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020045 command! -nargs=* Xaddexpr <mods>laddexpr <args>
Bram Moolenaar55b69262017-08-13 13:42:01 +020046 command! -nargs=* -count Xolder <mods><count>lolder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020047 command! -nargs=* Xnewer <mods>lnewer <args>
48 command! -nargs=* Xopen <mods>lopen <args>
49 command! -nargs=* Xwindow <mods>lwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020050 command! -nargs=* Xbottom <mods>lbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020051 command! -nargs=* Xclose <mods>lclose <args>
52 command! -nargs=* -bang Xfile <mods>lfile<bang> <args>
53 command! -nargs=* Xgetfile <mods>lgetfile <args>
54 command! -nargs=* Xaddfile <mods>laddfile <args>
55 command! -nargs=* -bang Xbuffer <mods>lbuffer<bang> <args>
56 command! -nargs=* Xgetbuffer <mods>lgetbuffer <args>
57 command! -nargs=* Xaddbuffer <mods>laddbuffer <args>
58 command! -nargs=* Xrewind <mods>lrewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020059 command! -count -nargs=* -bang Xnext <mods><count>lnext<bang> <args>
60 command! -count -nargs=* -bang Xprev <mods><count>lprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020061 command! -nargs=* -bang Xfirst <mods>lfirst<bang> <args>
62 command! -nargs=* -bang Xlast <mods>llast<bang> <args>
63 command! -nargs=* -bang Xnfile <mods>lnfile<bang> <args>
64 command! -nargs=* -bang Xpfile <mods>lpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020065 command! -nargs=* Xexpr <mods>lexpr <args>
Bram Moolenaaree85df32017-03-19 14:19:50 +010066 command! -range -nargs=* Xvimgrep <mods><count>lvimgrep <args>
67 command! -nargs=* Xvimgrepadd <mods>lvimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020068 command! -nargs=* Xgrep <mods> lgrep <args>
69 command! -nargs=* Xgrepadd <mods> lgrepadd <args>
70 command! -nargs=* Xhelpgrep lhelpgrep <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020071 let g:Xgetlist = function('getloclist', [0])
72 let g:Xsetlist = function('setloclist', [0])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020073 call setloclist(0, [], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020074 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +010075endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020076
Bram Moolenaarda59dd52016-01-05 21:59:58 +010077" Tests for the :clist and :llist commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +010078func XlistTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020079 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +010080
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020081 if a:cchar == 'l'
82 call assert_fails('llist', 'E776:')
83 endif
Bram Moolenaarda59dd52016-01-05 21:59:58 +010084 " With an empty list, command should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020085 Xgetexpr []
86 silent! Xlist
Bram Moolenaarda59dd52016-01-05 21:59:58 +010087 call assert_true(v:errmsg ==# 'E42: No Errors')
88
89 " Populate the list and then try
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020090 Xgetexpr ['non-error 1', 'Xtestfile1:1:3:Line1',
Bram Moolenaarda59dd52016-01-05 21:59:58 +010091 \ 'non-error 2', 'Xtestfile2:2:2:Line2',
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020092 \ 'non-error 3', 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +010093
94 " List only valid entries
Bram Moolenaaree85df32017-03-19 14:19:50 +010095 let l = split(execute('Xlist', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +010096 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
97 \ ' 4 Xtestfile2:2 col 2: Line2',
98 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
99
100 " List all the entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100101 let l = split(execute('Xlist!', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100102 call assert_equal([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1',
103 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2',
104 \ ' 5: non-error 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
105
106 " List a range of errors
Bram Moolenaaree85df32017-03-19 14:19:50 +0100107 let l = split(execute('Xlist 3,6', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100108 call assert_equal([' 4 Xtestfile2:2 col 2: Line2',
109 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
110
Bram Moolenaaree85df32017-03-19 14:19:50 +0100111 let l = split(execute('Xlist! 3,4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100112 call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
113
Bram Moolenaaree85df32017-03-19 14:19:50 +0100114 let l = split(execute('Xlist -6,-4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100115 call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l)
116
Bram Moolenaaree85df32017-03-19 14:19:50 +0100117 let l = split(execute('Xlist! -5,-3', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100118 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
119 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100120
121 " Test for '+'
Bram Moolenaaree85df32017-03-19 14:19:50 +0100122 let l = split(execute('Xlist! +2', ''), "\n")
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100123 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
124 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaaree85df32017-03-19 14:19:50 +0100125
126 " Different types of errors
127 call g:Xsetlist([{'lnum':10,'col':5,'type':'W', 'text':'Warning','nr':11},
128 \ {'lnum':20,'col':10,'type':'e','text':'Error','nr':22},
129 \ {'lnum':30,'col':15,'type':'i','text':'Info','nr':33},
130 \ {'lnum':40,'col':20,'type':'x', 'text':'Other','nr':44},
131 \ {'lnum':50,'col':25,'type':"\<C-A>",'text':'one','nr':55}])
132 let l = split(execute('Xlist', ""), "\n")
133 call assert_equal([' 1:10 col 5 warning 11: Warning',
134 \ ' 2:20 col 10 error 22: Error',
135 \ ' 3:30 col 15 info 33: Info',
136 \ ' 4:40 col 20 x 44: Other',
137 \ ' 5:50 col 25 55: one'], l)
138
139 " Error cases
140 call assert_fails('Xlist abc', 'E488:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100141endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100142
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100143func Test_clist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100144 call XlistTests('c')
145 call XlistTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100146endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100147
148" Tests for the :colder, :cnewer, :lolder and :lnewer commands
149" Note that this test assumes that a quickfix/location list is
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100150" already set by the caller.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100151func XageTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200152 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100153
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200154 let list = [{'bufnr': 1, 'lnum': 1}]
155 call g:Xsetlist(list)
156
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100157 " Jumping to a non existent list should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200158 silent! Xolder 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100159 call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
160
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200161 silent! Xnewer 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100162 call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
163
164 " Add three quickfix/location lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200165 Xgetexpr ['Xtestfile1:1:3:Line1']
166 Xgetexpr ['Xtestfile2:2:2:Line2']
167 Xgetexpr ['Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100168
169 " Go back two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200170 Xolder
171 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100172 call assert_equal('Line2', l[0].text)
173
174 " Go forward two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200175 Xnewer
176 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100177 call assert_equal('Line3', l[0].text)
178
179 " Test for the optional count argument
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200180 Xolder 2
181 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100182 call assert_equal('Line1', l[0].text)
183
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200184 Xnewer 2
185 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100186 call assert_equal('Line3', l[0].text)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100187endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100188
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100189func Test_cage()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100190 call XageTests('c')
191 call XageTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100192endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100193
194" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
195" commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100196func XwindowTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200197 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100198
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200199 " Opening the location list window without any errors should fail
200 if a:cchar == 'l'
201 call assert_fails('lopen', 'E776:')
202 endif
203
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100204 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200205 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100206
207 " Quickfix/Location window should not open with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200208 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100209 call assert_true(winnr('$') == 1)
210
211 " Create a list with valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200212 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
213 \ 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100214
215 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200216 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100217 call assert_true(winnr('$') == 2 && winnr() == 2 &&
218 \ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
Bram Moolenaar537ef082016-07-09 17:56:19 +0200219 redraw!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100220
221 " Close the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200222 Xclose
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100223 call assert_true(winnr('$') == 1)
224
225 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200226 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100227
228 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200229 Xopen 5
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100230 call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
231 \ && winheight('.') == 5)
232
233 " Opening the window again, should move the cursor to that window
234 wincmd t
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200235 Xopen 7
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100236 call assert_true(winnr('$') == 2 && winnr() == 2 &&
237 \ winheight('.') == 7 &&
238 \ getline('.') ==# '|| non-error 1')
239
240
241 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200242 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100243 call assert_true(winnr('$') == 1)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200244
245 if a:cchar == 'c'
246 " Opening the quickfix window in multiple tab pages should reuse the
247 " quickfix buffer
248 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
249 \ 'Xtestfile3:3:1:Line3']
250 Xopen
251 let qfbufnum = bufnr('%')
252 tabnew
253 Xopen
254 call assert_equal(qfbufnum, bufnr('%'))
255 new | only | tabonly
256 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100257endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100258
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100259func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100260 call XwindowTests('c')
261 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100262endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100263
264" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
265" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100266func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200267 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100268
269 call writefile(['Xtestfile1:700:10:Line 700',
270 \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
271
272 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200273 Xfile Xqftestfile1
274 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100275 call assert_true(len(l) == 2 &&
276 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
277 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
278
Bram Moolenaar049cba92016-06-26 14:38:04 +0200279 " Test with a non existent file
280 call assert_fails('Xfile non_existent_file', 'E40')
281
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100282 " Run cfile/lfile from a modified buffer
283 enew!
284 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200285 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100286 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
287
288 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200289 Xaddfile Xqftestfile1
290 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100291 call assert_true(len(l) == 3 &&
292 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
293
294 call writefile(['Xtestfile1:222:77:Line 222',
295 \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
296
297 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200298 Xgetfile Xqftestfile1
299 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100300 call assert_true(len(l) == 2 &&
301 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
302 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
303
304 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100305endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100306
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100307func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100308 call XfileTests('c')
309 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100310endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100311
312" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
313" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100314func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200315 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100316
317 enew!
318 silent! call setline(1, ['Xtestfile7:700:10:Line 700',
319 \ 'Xtestfile8:800:15:Line 800'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200320 Xbuffer!
321 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100322 call assert_true(len(l) == 2 &&
323 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
324 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
325
326 enew!
327 silent! call setline(1, ['Xtestfile9:900:55:Line 900',
328 \ 'Xtestfile10:950:66:Line 950'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200329 Xgetbuffer
330 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100331 call assert_true(len(l) == 2 &&
332 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
333 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
334
335 enew!
336 silent! call setline(1, ['Xtestfile11:700:20:Line 700',
337 \ 'Xtestfile12:750:25:Line 750'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200338 Xaddbuffer
339 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100340 call assert_true(len(l) == 4 &&
341 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
342 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
343 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200344 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100345
Bram Moolenaaree85df32017-03-19 14:19:50 +0100346 " Check for invalid buffer
347 call assert_fails('Xbuffer 199', 'E474:')
348
349 " Check for unloaded buffer
350 edit Xtestfile1
351 let bnr = bufnr('%')
352 enew!
353 call assert_fails('Xbuffer ' . bnr, 'E681:')
354
355 " Check for invalid range
356 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
357 " commands. So directly call the commands.
358 if (a:cchar == 'c')
359 call assert_fails('900,999cbuffer', 'E16:')
360 else
361 call assert_fails('900,999lbuffer', 'E16:')
362 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100363endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100364
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100365func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100366 call XbufferTests('c')
367 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100368endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100369
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100370func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200371 call s:setup_commands(a:cchar)
372
373 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100374endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200375
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100376func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200377 call XexprTests('c')
378 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100379endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200380
381" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100382func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200383 call s:setup_commands(a:cchar)
384
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200385 " Jumping to first or next location list entry without any error should
386 " result in failure
387 if a:cchar == 'l'
388 call assert_fails('lfirst', 'E776:')
389 call assert_fails('lnext', 'E776:')
390 endif
391
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200392 call s:create_test_file('Xqftestfile1')
393 call s:create_test_file('Xqftestfile2')
394
395 Xgetexpr ['Xqftestfile1:5:Line5',
396 \ 'Xqftestfile1:6:Line6',
397 \ 'Xqftestfile2:10:Line10',
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200398 \ 'Xqftestfile2:11:Line11',
399 \ 'RegularLine1',
400 \ 'RegularLine2']
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200401
402 Xfirst
403 call assert_fails('Xprev', 'E553')
404 call assert_fails('Xpfile', 'E553')
405 Xnfile
406 call assert_equal('Xqftestfile2', bufname('%'))
407 call assert_equal(10, line('.'))
408 Xpfile
409 call assert_equal('Xqftestfile1', bufname('%'))
410 call assert_equal(6, line('.'))
411 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200412 Xprev
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200413 call assert_equal('Xqftestfile2', bufname('%'))
414 call assert_equal(11, line('.'))
415 call assert_fails('Xnext', 'E553')
416 call assert_fails('Xnfile', 'E553')
417 Xrewind
418 call assert_equal('Xqftestfile1', bufname('%'))
419 call assert_equal(5, line('.'))
420
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200421 10Xnext
422 call assert_equal('Xqftestfile2', bufname('%'))
423 call assert_equal(11, line('.'))
424 10Xprev
425 call assert_equal('Xqftestfile1', bufname('%'))
426 call assert_equal(5, line('.'))
427
Bram Moolenaaree85df32017-03-19 14:19:50 +0100428 Xexpr ""
429 call assert_fails('Xnext', 'E42:')
430
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200431 call delete('Xqftestfile1')
432 call delete('Xqftestfile2')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100433endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200434
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100435func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200436 call Xtest_browse('c')
437 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100438endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200439
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100440func Test_nomem()
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200441 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100442 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100443
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200444 call test_alloc_fail(GetAllocId('qf_dirname_now'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100445 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100446
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200447 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100448 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100449
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200450 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100451 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100452
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200453 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100454 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100455
456endfunc
457
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100458func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200459 call s:setup_commands(a:cchar)
460 Xhelpgrep quickfix
461 Xopen
462 if a:cchar == 'c'
463 let title_text = ':helpgrep quickfix'
464 else
465 let title_text = ':lhelpgrep quickfix'
466 endif
467 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200468
469 " Jumping to a help topic should open the help window
470 only
471 Xnext
472 call assert_true(&buftype == 'help')
473 call assert_true(winnr('$') == 2)
474 " Jumping to the next match should reuse the help window
475 Xnext
476 call assert_true(&buftype == 'help')
477 call assert_true(winnr() == 1)
478 call assert_true(winnr('$') == 2)
479 " Jumping to the next match from the quickfix window should reuse the help
480 " window
481 Xopen
482 Xnext
483 call assert_true(&buftype == 'help')
484 call assert_true(winnr() == 1)
485 call assert_true(winnr('$') == 2)
486
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100487 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200488 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100489
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200490 if a:cchar == 'l'
491 " When a help window is present, running :lhelpgrep should reuse the
492 " help window and not the current window
493 new | only
494 call g:Xsetlist([], 'f')
495 help index.txt
496 wincmd w
497 lhelpgrep quickfix
498 call assert_equal(1, winnr())
499 call assert_notequal([], getloclist(1))
500 call assert_equal([], getloclist(2))
501 endif
502
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200503 new | only
504
Bram Moolenaaree85df32017-03-19 14:19:50 +0100505 " Search for non existing help string
506 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100507endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200508
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100509func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200510 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200511 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200512 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100513endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100514
Bram Moolenaar6920c722016-01-22 22:44:10 +0100515func Test_errortitle()
516 augroup QfBufWinEnter
517 au!
518 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
519 augroup END
520 copen
521 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'')'}]
522 call setqflist(a)
523 call assert_equal(':setqflist()', g:a)
524 augroup QfBufWinEnter
525 au!
526 augroup END
527 augroup! QfBufWinEnter
528endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100529
Bram Moolenaar5584df62016-03-18 21:00:51 +0100530func Test_vimgreptitle()
531 augroup QfBufWinEnter
532 au!
533 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
534 augroup END
535 try
536 vimgrep /pattern/j file
537 catch /E480/
538 endtry
539 copen
540 call assert_equal(': vimgrep /pattern/j file', g:a)
541 augroup QfBufWinEnter
542 au!
543 augroup END
544 augroup! QfBufWinEnter
545endfunc
546
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100547func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200548 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +0100549
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200550 Xgetexpr ['file:1:1:message']
551 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100552 if a:cchar == 'c'
553 call setqflist(l, 'r')
554 else
555 call setloclist(0, l, 'r')
556 endif
557
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200558 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +0100559 if a:cchar == 'c'
560 let title = ':setqflist()'
561 else
562 let title = ':setloclist()'
563 endif
564 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200565 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100566endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100567
568" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100569func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200570 call XqfTitleTests('c')
571 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100572endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100573
574" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100575func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100576 let save_efm = &efm
577 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
578 cgetexpr ['WWWW', 'EEEE', 'CCCC']
579 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
580 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
581 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
582 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
583 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
584 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
585 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
586 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
587 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100588endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100589
590" This will test for problems in quickfix:
591" A. incorrectly copying location lists which caused the location list to show
592" a different name than the file that was actually being displayed.
593" B. not reusing the window for which the location list window is opened but
594" instead creating new windows.
595" C. make sure that the location list window is not reused instead of the
596" window it belongs to.
597"
598" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100599func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100600 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
601 let word = substitute(base, '\v(.*)\..*', '\1', '')
602
603 setl modifiable
604 setl noreadonly
605 setl noswapfile
606 setl bufhidden=delete
607 %del _
608 " For problem 2:
609 " 'buftype' has to be set to reproduce the constant opening of new windows
610 setl buftype=nofile
611
612 call setline(1, word)
613
614 setl nomodified
615 setl nomodifiable
616 setl readonly
617 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100618endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100619
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100620func Test_locationlist()
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100621 enew
622
623 augroup testgroup
624 au!
625 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
626 augroup END
627
628 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
629
630 let qflist = []
631 for word in words
632 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
633 " NOTE: problem 1:
634 " intentionally not setting 'lnum' so that the quickfix entries are not
635 " valid
636 call setloclist(0, qflist, ' ')
637 endfor
638
639 " Test A
640 lrewind
641 enew
642 lopen
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200643 4lnext
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100644 vert split
645 wincmd L
646 lopen
647 wincmd p
648 lnext
649 let fileName = expand("%")
650 wincmd p
651 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
652 let fileName = substitute(fileName, '\\', '/', 'g')
653 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
654 call assert_equal("test://bar.txt", fileName)
655 call assert_equal("test://bar.txt", locationListFileName)
656
657 wincmd n | only
658
659 " Test B:
660 lrewind
661 lopen
662 2
663 exe "normal \<CR>"
664 wincmd p
665 3
666 exe "normal \<CR>"
667 wincmd p
668 4
669 exe "normal \<CR>"
670 call assert_equal(2, winnr('$'))
671 wincmd n | only
672
673 " Test C:
674 lrewind
675 lopen
676 " Let's move the location list window to the top to check whether it (the
677 " first window found) will be reused when we try to open new windows:
678 wincmd K
679 2
680 exe "normal \<CR>"
681 wincmd p
682 3
683 exe "normal \<CR>"
684 wincmd p
685 4
686 exe "normal \<CR>"
687 1wincmd w
688 call assert_equal('quickfix', &buftype)
689 2wincmd w
690 let bufferName = expand("%")
691 let bufferName = substitute(bufferName, '\\', '/', 'g')
692 call assert_equal('test://quux.txt', bufferName)
693
694 wincmd n | only
695
696 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100697endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +0100698
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100699func Test_locationlist_curwin_was_closed()
Bram Moolenaar0899d692016-03-19 13:35:03 +0100700 augroup testgroup
701 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200702 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
Bram Moolenaar0899d692016-03-19 13:35:03 +0100703 augroup END
704
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100705 func! R(n)
Bram Moolenaar0899d692016-03-19 13:35:03 +0100706 quit
707 endfunc
708
709 new
710 let q = []
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200711 call add(q, {'filename': 'test_curwin.txt' })
Bram Moolenaar0899d692016-03-19 13:35:03 +0100712 call setloclist(0, q)
713 call assert_fails('lrewind', 'E924:')
714
715 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100716endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100717
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100718func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200719 call writefile(['loclistfoo'], 'loclistfoo')
720 call writefile(['loclistbar'], 'loclistbar')
721 set switchbuf=usetab
722
723 edit loclistfoo
724 tabedit loclistbar
725 silent lgrep loclistfoo loclist*
726 call assert_equal(1, tabpagenr())
727
728 enew | only | tabonly
729 set switchbuf&vim
730 call delete('loclistfoo')
731 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100732endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200733
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100734" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100735func Test_efm1()
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100736 if !has('unix')
737 " The 'errorformat' setting is different on non-Unix systems.
738 " This test works only on Unix-like systems.
739 return
740 endif
741
742 let l = [
743 \ '"Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.',
744 \ '"Xtestfile", line 6 col 19; this is an error',
745 \ 'gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c',
746 \ 'Xtestfile:9: parse error before `asd''',
747 \ 'make: *** [vim] Error 1',
748 \ 'in file "Xtestfile" linenr 10: there is an error',
749 \ '',
750 \ '2 returned',
751 \ '"Xtestfile", line 11 col 1; this is an error',
752 \ '"Xtestfile", line 12 col 2; this is another error',
753 \ '"Xtestfile", line 14:10; this is an error in column 10',
754 \ '=Xtestfile=, line 15:10; this is another error, but in vcol 10 this time',
755 \ '"Xtestfile", linenr 16: yet another problem',
756 \ 'Error in "Xtestfile" at line 17:',
757 \ 'x should be a dot',
758 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17',
759 \ ' ^',
760 \ 'Error in "Xtestfile" at line 18:',
761 \ 'x should be a dot',
762 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18',
763 \ '.............^',
764 \ 'Error in "Xtestfile" at line 19:',
765 \ 'x should be a dot',
766 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19',
767 \ '--------------^',
768 \ 'Error in "Xtestfile" at line 20:',
769 \ 'x should be a dot',
770 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20',
771 \ ' ^',
772 \ '',
773 \ 'Does anyone know what is the problem and how to correction it?',
774 \ '"Xtestfile", line 21 col 9: What is the title of the quickfix window?',
775 \ '"Xtestfile", line 22 col 9: What is the title of the quickfix window?'
776 \ ]
777
778 call writefile(l, 'Xerrorfile1')
779 call writefile(l[:-2], 'Xerrorfile2')
780
781 let m = [
782 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2',
783 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3',
784 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4',
785 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5',
786 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6',
787 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7',
788 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8',
789 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9',
790 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10',
791 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11',
792 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12',
793 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13',
794 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14',
795 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15',
796 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16',
797 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17',
798 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18',
799 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19',
800 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20',
801 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21',
802 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22'
803 \ ]
804 call writefile(m, 'Xtestfile')
805
806 let save_efm = &efm
807 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
808 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
809
810 exe 'cf Xerrorfile2'
811 clast
812 copen
813 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
814 wincmd p
815
816 exe 'cf Xerrorfile1'
817 call assert_equal([4, 12], [line('.'), col('.')])
818 cn
819 call assert_equal([6, 19], [line('.'), col('.')])
820 cn
821 call assert_equal([9, 2], [line('.'), col('.')])
822 cn
823 call assert_equal([10, 2], [line('.'), col('.')])
824 cn
825 call assert_equal([11, 1], [line('.'), col('.')])
826 cn
827 call assert_equal([12, 2], [line('.'), col('.')])
828 cn
829 call assert_equal([14, 10], [line('.'), col('.')])
830 cn
831 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
832 cn
833 call assert_equal([16, 2], [line('.'), col('.')])
834 cn
835 call assert_equal([17, 6], [line('.'), col('.')])
836 cn
837 call assert_equal([18, 7], [line('.'), col('.')])
838 cn
839 call assert_equal([19, 8], [line('.'), col('.')])
840 cn
841 call assert_equal([20, 9], [line('.'), col('.')])
842 clast
843 cprev
844 cprev
845 wincmd w
846 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
847 wincmd p
848
849 let &efm = save_efm
850 call delete('Xerrorfile1')
851 call delete('Xerrorfile2')
852 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +0100853endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100854
Bram Moolenaarab47c612016-06-14 22:02:26 +0200855" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100856func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200857 call s:setup_commands(a:cchar)
858
Bram Moolenaarab47c612016-06-14 22:02:26 +0200859 let save_efm=&efm
860 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
861
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200862 let lines = ["Entering dir 'dir1/a'",
863 \ 'habits2.txt:1:Nine Healthy Habits',
864 \ "Entering dir 'b'",
865 \ 'habits3.txt:2:0 Hours of television',
866 \ 'habits2.txt:7:5 Small meals',
867 \ "Entering dir 'dir1/c'",
868 \ 'habits4.txt:3:1 Hour of exercise',
869 \ "Leaving dir 'dir1/c'",
870 \ "Leaving dir 'dir1/a'",
871 \ 'habits1.txt:4:2 Liters of water',
872 \ "Entering dir 'dir2'",
873 \ 'habits5.txt:5:3 Cups of hot green tea',
874 \ "Leaving dir 'dir2'"
875 \]
Bram Moolenaarab47c612016-06-14 22:02:26 +0200876
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200877 Xexpr ""
878 for l in lines
879 Xaddexpr l
880 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +0200881
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200882 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +0200883
884 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
885 call assert_equal(1, qf[1].lnum)
886 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
887 call assert_equal(2, qf[3].lnum)
888 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
889 call assert_equal(7, qf[4].lnum)
890 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
891 call assert_equal(3, qf[6].lnum)
892 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
893 call assert_equal(4, qf[9].lnum)
894 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
895 call assert_equal(5, qf[11].lnum)
896
897 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100898endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +0200899
900" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100901func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +0200902 " Create the directory stack and files
903 call mkdir('dir1')
904 call mkdir('dir1/a')
905 call mkdir('dir1/a/b')
906 call mkdir('dir1/c')
907 call mkdir('dir2')
908
909 let lines = ["Nine Healthy Habits",
910 \ "0 Hours of television",
911 \ "1 Hour of exercise",
912 \ "2 Liters of water",
913 \ "3 Cups of hot green tea",
914 \ "4 Short mental breaks",
915 \ "5 Small meals",
916 \ "6 AM wake up time",
917 \ "7 Minutes of laughter",
918 \ "8 Hours of sleep (at least)",
919 \ "9 PM end of the day and off to bed"
920 \ ]
921 call writefile(lines, 'habits1.txt')
922 call writefile(lines, 'dir1/a/habits2.txt')
923 call writefile(lines, 'dir1/a/b/habits3.txt')
924 call writefile(lines, 'dir1/c/habits4.txt')
925 call writefile(lines, 'dir2/habits5.txt')
926
927 call s:dir_stack_tests('c')
928 call s:dir_stack_tests('l')
929
930 call delete('dir1', 'rf')
931 call delete('dir2', 'rf')
932 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100933endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +0200934
Bram Moolenaar9b457942016-10-09 16:10:05 +0200935" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100936func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +0200937 call s:setup_commands(a:cchar)
938
939 let save_efm = &efm
940
941 let &efm =
942 \ '%Eerror %m %l,' .
943 \ '%-Wignored %m %l,' .
944 \ '%+Cmore ignored %m %l,' .
945 \ '%Zignored end'
946 Xgetexpr ['ignored warning 1', 'more ignored continuation 2', 'ignored end', 'error resync 4']
947 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
948 call assert_equal([['resync', 1, 4, 'E']], l)
949
950 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100951endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +0200952
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100953func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +0200954 call Xefm_ignore_continuations('c')
955 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100956endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +0200957
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200958" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100959func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200960 call s:setup_commands(a:cchar)
961
Bram Moolenaar049cba92016-06-26 14:38:04 +0200962 let save_efm = &efm
963
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200964 set efm=%f:%l:%m,%f:%f:%l:%m
965 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
966
967 set efm=%f:%l:%m,%f:%l:%r:%m
968 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
969
970 set efm=%f:%l:%m,%O:%f:%l:%m
971 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
972
973 set efm=%f:%l:%m,%f:%l:%*[^a-z
974 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
975
976 set efm=%f:%l:%m,%f:%l:%*c
977 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
978
979 set efm=%f:%l:%m,%L%M%N
980 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
981
982 set efm=%f:%l:%m,%f:%l:%m:%R
983 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
984
985 set efm=
986 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
987
988 set efm=%DEntering\ dir\ abc,%f:%l:%m
989 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
990
991 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100992endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200993
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100994func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200995 call Xinvalid_efm_Tests('c')
996 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100997endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200998
999" TODO:
1000" Add tests for the following formats in 'errorformat'
1001" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001002func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001003 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001004
1005 " Test for %s format in efm
1006 set efm=%f:%s
1007 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001008 let l = getqflist()
1009 call assert_equal(l[0].pattern, '^\VLine search text\$')
1010 call assert_equal(l[0].lnum, 0)
1011
Bram Moolenaaree85df32017-03-19 14:19:50 +01001012 let l = split(execute('clist', ''), "\n")
1013 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1014
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001015 " Test for %P, %Q and %t format specifiers
Bram Moolenaar049cba92016-06-26 14:38:04 +02001016 let lines=["[Xtestfile1]",
1017 \ "(1,17) error: ';' missing",
1018 \ "(21,2) warning: variable 'z' not defined",
1019 \ "(67,3) error: end of file found before string ended",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001020 \ "--",
Bram Moolenaar049cba92016-06-26 14:38:04 +02001021 \ "",
1022 \ "[Xtestfile2]",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001023 \ "--",
Bram Moolenaar049cba92016-06-26 14:38:04 +02001024 \ "",
1025 \ "[Xtestfile3]",
1026 \ "NEW compiler v1.1",
1027 \ "(2,2) warning: variable 'x' not defined",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001028 \ "(67,3) warning: 's' already defined",
1029 \ "--"
Bram Moolenaar049cba92016-06-26 14:38:04 +02001030 \]
Bram Moolenaaree85df32017-03-19 14:19:50 +01001031 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001032 " To exercise the push/pop file functionality in quickfix, the test files
1033 " need to be created.
1034 call writefile(['Line1'], 'Xtestfile1')
1035 call writefile(['Line2'], 'Xtestfile2')
1036 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001037 cexpr ""
1038 for l in lines
1039 caddexpr l
1040 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001041 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001042 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001043 call assert_equal(21, l[2].lnum)
1044 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001045 call assert_equal('w', l[2].type)
1046 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001047 call delete('Xtestfile1')
1048 call delete('Xtestfile2')
1049 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001050
1051 " Tests for %E, %C and %Z format specifiers
1052 let lines = ["Error 275",
1053 \ "line 42",
1054 \ "column 3",
1055 \ "' ' expected after '--'"
1056 \]
1057 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1058 cgetexpr lines
1059 let l = getqflist()
1060 call assert_equal(275, l[0].nr)
1061 call assert_equal(42, l[0].lnum)
1062 call assert_equal(3, l[0].col)
1063 call assert_equal('E', l[0].type)
1064 call assert_equal("\n' ' expected after '--'", l[0].text)
1065
1066 " Test for %>
1067 let lines = ["Error in line 147 of foo.c:",
1068 \"unknown variable 'i'"
1069 \]
1070 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1071 cgetexpr lines
1072 let l = getqflist()
1073 call assert_equal(147, l[0].lnum)
1074 call assert_equal('E', l[0].type)
1075 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001076
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001077 " Test for %A, %C and other formats
1078 let lines = [
1079 \"==============================================================",
1080 \"FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)",
1081 \"--------------------------------------------------------------",
1082 \"Traceback (most recent call last):",
1083 \' File "unittests/dbfacadeTest.py", line 89, in testFoo',
1084 \" self.assertEquals(34, dtid)",
1085 \' File "/usr/lib/python2.2/unittest.py", line 286, in',
1086 \" failUnlessEqual",
1087 \" raise self.failureException, \\",
1088 \"AssertionError: 34 != 33",
1089 \"",
1090 \"--------------------------------------------------------------",
1091 \"Ran 27 tests in 0.063s"
1092 \]
1093 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
1094 cgetexpr lines
1095 let l = getqflist()
1096 call assert_equal(8, len(l))
1097 call assert_equal(89, l[4].lnum)
1098 call assert_equal(1, l[4].valid)
1099 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
1100
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001101 " The following sequence of commands used to crash Vim
1102 set efm=%W%m
1103 cgetexpr ['msg1']
1104 let l = getqflist()
1105 call assert_equal(1, len(l), string(l))
1106 call assert_equal('msg1', l[0].text)
1107 set efm=%C%m
1108 lexpr 'msg2'
1109 let l = getloclist(0)
1110 call assert_equal(1, len(l), string(l))
1111 call assert_equal('msg2', l[0].text)
1112 lopen
1113 call setqflist([], 'r')
1114 caddbuf
1115 let l = getqflist()
1116 call assert_equal(1, len(l), string(l))
1117 call assert_equal('|| msg2', l[0].text)
1118
1119 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001120 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001121endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001122
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001123func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001124 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001125 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001126 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001127 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001128 colder
1129 cgetexpr []
1130 endfunc
1131 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001132 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001133 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001134 lolder
1135 lgetexpr []
1136 endfunc
1137 endif
1138
1139 augroup testgroup
1140 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001141 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001142 augroup END
1143
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001144 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001145 let words = [ "a", "b" ]
1146 let qflist = []
1147 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001148 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001149 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001150 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001151 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001152
1153 augroup! testgroup
1154endfunc
1155
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001156func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001157 call XquickfixChangedByAutocmd('c')
1158 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001159endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001160
1161func Test_caddbuffer_to_empty()
1162 helpgr quickfix
1163 call setqflist([], 'r')
1164 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001165 try
1166 cn
1167 catch
1168 " number of matches is unknown
1169 call assert_true(v:exception =~ 'E553:')
1170 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001171 quit!
1172endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001173
1174func Test_cgetexpr_works()
1175 " this must not crash Vim
1176 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001177 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001178endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001179
1180" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001181func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001182 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001183
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001184 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001185 \ {'bufnr': a:bnum, 'lnum': 2}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001186 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001187 call assert_equal(2, len(l))
1188 call assert_equal(2, l[1].lnum)
1189
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001190 Xnext
1191 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1192 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001193 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001194 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001195 call assert_equal(3, line('.'))
1196
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001197 " Appending entries to the list should not change the cursor position
1198 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001199 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001200 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001201 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001202 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1203 call assert_equal(1, line('.'))
1204 close
1205
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001206 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001207 \ {'bufnr': a:bnum, 'lnum': 4},
1208 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001209 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001210 call assert_equal(3, len(l))
1211 call assert_equal(5, l[2].lnum)
1212
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001213 call g:Xsetlist([])
1214 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001215 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001216
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001217 " Tests for setting the 'valid' flag
1218 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1219 Xwindow
1220 call assert_equal(1, winnr('$'))
1221 let l = g:Xgetlist()
1222 call g:Xsetlist(l)
1223 call assert_equal(0, g:Xgetlist()[0].valid)
1224 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1225 Xwindow
1226 call assert_equal(2, winnr('$'))
1227 Xclose
1228 let save_efm = &efm
1229 set efm=%m
1230 Xgetexpr 'TestMessage'
1231 let l = g:Xgetlist()
1232 call g:Xsetlist(l)
1233 call assert_equal(1, g:Xgetlist()[0].valid)
1234 let &efm = save_efm
1235
Bram Moolenaaree85df32017-03-19 14:19:50 +01001236 " Error cases:
1237 " Refer to a non-existing buffer and pass a non-dictionary type
1238 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1239 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1240 call g:Xsetlist([[1, 2,3]])
1241 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001242endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001243
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001244func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001245 new Xtestfile | only
1246 let bnum = bufnr('%')
1247 call setline(1, range(1,5))
1248
1249 call SetXlistTests('c', bnum)
1250 call SetXlistTests('l', bnum)
1251
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001252 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001253 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001254endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001255
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001256func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001257 call s:setup_commands(a:cchar)
1258
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001259 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001260 let @/ = 'Test_'
1261 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001262 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001263 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001264 Xvimgrep empty test_quickfix.vim
1265 call assert_true(len(g:Xgetlist()) > 0)
1266 Xvimgrep matches test_quickfix.vim
1267 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001268 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001269 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001270 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001271 call g:Xsetlist([], 'r')
1272 call assert_true(len(g:Xgetlist()) == 0)
1273 Xolder
1274 call assert_equal(testlen, len(g:Xgetlist()))
1275 Xnewer
1276 Xnewer
1277 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001278endfunc
1279
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001280func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001281 call Xlist_empty_middle('c')
1282 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001283endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001284
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001285func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001286 call s:setup_commands(a:cchar)
1287
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001288 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001289 Xvimgrep one test_quickfix.vim
1290 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001291 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001292 Xvimgrep two test_quickfix.vim
1293 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001294 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001295 Xvimgrep three test_quickfix.vim
1296 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001297 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001298 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001299 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001300 call g:Xsetlist([], 'r')
1301 call assert_true(len(g:Xgetlist()) == 0)
1302 Xnewer
1303 call assert_equal(twolen, len(g:Xgetlist()))
1304 Xnewer
1305 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001306endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001307
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001308func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001309 call Xlist_empty_older('c')
1310 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001311endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001312
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001313func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001314 call s:setup_commands(a:cchar)
1315
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001316 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1317 \ {'filename': 'fnameB', 'text': 'B'}]
1318 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1319 \ {'filename': 'fnameD', 'text': 'D'},
1320 \ {'filename': 'fnameE', 'text': 'E'}]
1321
1322 " {action} is unspecified. Same as specifing ' '.
1323 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001324 silent! Xnewer 99
1325 call g:Xsetlist(list1)
1326 call g:Xsetlist(list2)
1327 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001328 call assert_equal(3, len(li))
1329 call assert_equal('C', li[0]['text'])
1330 call assert_equal('D', li[1]['text'])
1331 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001332 silent! Xolder
1333 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001334 call assert_equal(2, len(li))
1335 call assert_equal('A', li[0]['text'])
1336 call assert_equal('B', li[1]['text'])
1337
1338 " {action} is specified ' '.
1339 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001340 silent! Xnewer 99
1341 call g:Xsetlist(list1)
1342 call g:Xsetlist(list2, ' ')
1343 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001344 call assert_equal(3, len(li))
1345 call assert_equal('C', li[0]['text'])
1346 call assert_equal('D', li[1]['text'])
1347 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001348 silent! Xolder
1349 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001350 call assert_equal(2, len(li))
1351 call assert_equal('A', li[0]['text'])
1352 call assert_equal('B', li[1]['text'])
1353
1354 " {action} is specified 'a'.
1355 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001356 silent! Xnewer 99
1357 call g:Xsetlist(list1)
1358 call g:Xsetlist(list2, 'a')
1359 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001360 call assert_equal(5, len(li))
1361 call assert_equal('A', li[0]['text'])
1362 call assert_equal('B', li[1]['text'])
1363 call assert_equal('C', li[2]['text'])
1364 call assert_equal('D', li[3]['text'])
1365 call assert_equal('E', li[4]['text'])
1366
1367 " {action} is specified 'r'.
1368 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001369 silent! Xnewer 99
1370 call g:Xsetlist(list1)
1371 call g:Xsetlist(list2, 'r')
1372 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001373 call assert_equal(3, len(li))
1374 call assert_equal('C', li[0]['text'])
1375 call assert_equal('D', li[1]['text'])
1376 call assert_equal('E', li[2]['text'])
1377
1378 " Test for wrong value.
1379 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001380 call assert_fails("call g:Xsetlist(0)", 'E714:')
1381 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
1382 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
1383 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
1384 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001385endfunc
1386
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001387func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001388 call XquickfixSetListWithAct('c')
1389 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001390endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001391
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001392func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001393 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001394
Bram Moolenaar049cba92016-06-26 14:38:04 +02001395 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001396 call assert_equal(1, l[0].lnum)
1397 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001398 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001399 call assert_equal(2, l[1].lnum)
1400 call assert_equal(1, l[1].col)
1401 call assert_equal(4070, len(l[1].text))
1402 call assert_equal(3, l[2].lnum)
1403 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001404 call assert_equal(4070, len(l[2].text))
1405 call assert_equal(4, l[3].lnum)
1406 call assert_equal(1, l[3].col)
1407 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001408
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001409 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001410endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001411
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001412func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001413 call s:setup_commands(a:cchar)
1414
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001415 let testfile = 'samples/quickfix.txt'
1416
1417 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001418 exe 'Xgetfile' testfile
1419 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001420
1421 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001422 Xexpr readfile(testfile)
1423 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001424
1425 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001426 Xexpr join(readfile(testfile), "\n")
1427 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001428
1429 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001430 exe 'edit' testfile
1431 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02001432 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001433endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001434
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001435func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001436 call s:long_lines_tests('c')
1437 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001438endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001439
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001440func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001441 let l = []
1442 for i in range(1, 20)
1443 call add(l, 'Line' . i)
1444 endfor
1445 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001446endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001447
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001448func Test_switchbuf()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001449 call s:create_test_file('Xqftestfile1')
1450 call s:create_test_file('Xqftestfile2')
1451 call s:create_test_file('Xqftestfile3')
1452
1453 new | only
1454 edit Xqftestfile1
1455 let file1_winid = win_getid()
1456 new Xqftestfile2
1457 let file2_winid = win_getid()
1458 cgetexpr ['Xqftestfile1:5:Line5',
1459 \ 'Xqftestfile1:6:Line6',
1460 \ 'Xqftestfile2:10:Line10',
1461 \ 'Xqftestfile2:11:Line11',
1462 \ 'Xqftestfile3:15:Line15',
1463 \ 'Xqftestfile3:16:Line16']
1464
1465 new
1466 let winid = win_getid()
1467 cfirst | cnext
1468 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001469 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001470 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001471 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001472 call assert_equal(winid, win_getid())
1473 enew
1474
1475 set switchbuf=useopen
1476 cfirst | cnext
1477 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001478 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001479 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001480 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001481 call assert_equal(file2_winid, win_getid())
1482
1483 enew | only
1484 set switchbuf=usetab
1485 tabedit Xqftestfile1
1486 tabedit Xqftestfile2
1487 tabfirst
1488 cfirst | cnext
1489 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001490 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001491 call assert_equal(3, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001492 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001493 call assert_equal(3, tabpagenr())
1494 tabfirst | tabonly | enew
1495
1496 set switchbuf=split
1497 cfirst | cnext
1498 call assert_equal(1, winnr('$'))
1499 cnext | cnext
1500 call assert_equal(2, winnr('$'))
1501 cnext | cnext
1502 call assert_equal(3, winnr('$'))
1503 enew | only
1504
1505 set switchbuf=newtab
1506 cfirst | cnext
1507 call assert_equal(1, tabpagenr('$'))
1508 cnext | cnext
1509 call assert_equal(2, tabpagenr('$'))
1510 cnext | cnext
1511 call assert_equal(3, tabpagenr('$'))
1512 tabfirst | enew | tabonly | only
1513
1514 set switchbuf=
1515 edit Xqftestfile1
1516 let file1_winid = win_getid()
1517 new Xqftestfile2
1518 let file2_winid = win_getid()
1519 copen
1520 exe "normal 1G\<CR>"
1521 call assert_equal(file1_winid, win_getid())
1522 copen
1523 exe "normal 3G\<CR>"
1524 call assert_equal(file2_winid, win_getid())
1525 copen | only
1526 exe "normal 5G\<CR>"
1527 call assert_equal(2, winnr('$'))
1528 call assert_equal(1, bufwinnr('Xqftestfile3'))
1529
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001530 " If only quickfix window is open in the current tabpage, jumping to an
1531 " entry with 'switchubf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02001532 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001533 set switchbuf=usetab
1534 tabedit Xqftestfile1
1535 tabedit Xqftestfile2
1536 tabedit Xqftestfile3
1537 tabfirst
1538 copen | only
1539 clast
1540 call assert_equal(4, tabpagenr())
1541 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001542
1543 call delete('Xqftestfile1')
1544 call delete('Xqftestfile2')
1545 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001546 set switchbuf&vim
1547
1548 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001549endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001550
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001551func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001552 call s:setup_commands(a:cchar)
1553
1554 enew | only
1555
Bram Moolenaarc1542742016-07-20 21:44:37 +02001556 let fname = 'Xqftestfile' . a:cchar
1557 call s:create_test_file(fname)
1558 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02001559
Bram Moolenaarc1542742016-07-20 21:44:37 +02001560 Xgetexpr [fname . ':5:Line5',
1561 \ fname . ':10:Line10',
1562 \ fname . ':15:Line15',
1563 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02001564
1565 6,14delete
1566 call append(6, ['Buffer', 'Window'])
1567
1568 let l = g:Xgetlist()
1569
1570 call assert_equal(5, l[0].lnum)
1571 call assert_equal(6, l[2].lnum)
1572 call assert_equal(13, l[3].lnum)
1573
1574 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02001575 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001576endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001577
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001578func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02001579 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001580 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02001581 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001582 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001583endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001584
1585" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001586func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001587 call s:setup_commands(a:cchar)
1588
1589 " The following lines are used for the grep test. Don't remove.
1590 " Grep_Test_Text: Match 1
1591 " Grep_Test_Text: Match 2
1592 " GrepAdd_Test_Text: Match 1
1593 " GrepAdd_Test_Text: Match 2
1594 enew! | only
1595 set makeef&vim
1596 silent Xgrep Grep_Test_Text: test_quickfix.vim
1597 call assert_true(len(g:Xgetlist()) == 3)
1598 Xopen
1599 call assert_true(w:quickfix_title =~ '^:grep')
1600 Xclose
1601 enew
1602 set makeef=Temp_File_##
1603 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
1604 call assert_true(len(g:Xgetlist()) == 6)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001605endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001606
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001607func Test_grep()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001608 if !has('unix')
1609 " The grepprg may not be set on non-Unix systems
1610 return
1611 endif
1612
1613 call s:test_xgrep('c')
1614 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001615endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001616
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001617func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001618 " Use one 'errorformat' for two windows. Add an expression to each of them,
1619 " make sure they each keep their own state.
1620 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1621 call mkdir('Xone/a', 'p')
1622 call mkdir('Xtwo/a', 'p')
1623 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
1624 call writefile(lines, 'Xone/a/one.txt')
1625 call writefile(lines, 'Xtwo/a/two.txt')
1626
1627 new one
1628 let one_id = win_getid()
1629 lexpr ""
1630 new two
1631 let two_id = win_getid()
1632 lexpr ""
1633
1634 laddexpr "Entering dir 'Xtwo/a'"
1635 call win_gotoid(one_id)
1636 laddexpr "Entering dir 'Xone/a'"
1637 call win_gotoid(two_id)
1638 laddexpr 'two.txt:5:two two two'
1639 call win_gotoid(one_id)
1640 laddexpr 'one.txt:3:one one one'
1641
1642 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001643 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
1644 call assert_equal(3, loc_one[1].lnum)
1645
1646 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001647 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
1648 call assert_equal(5, loc_two[1].lnum)
1649
1650 call win_gotoid(one_id)
1651 bwipe!
1652 call win_gotoid(two_id)
1653 bwipe!
1654 call delete('Xone', 'rf')
1655 call delete('Xtwo', 'rf')
1656endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02001657
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001658func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02001659 call s:setup_commands(a:cchar)
1660
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001661 " Calling lbottom without any errors should fail
1662 if a:cchar == 'l'
1663 call assert_fails('lbottom', 'E776:')
1664 endif
1665
Bram Moolenaar875feea2017-06-11 16:07:51 +02001666 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02001667 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02001668 let wid = win_getid()
1669 call assert_equal(1, line('.'))
1670 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02001671 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02001672 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02001673 call win_gotoid(wid)
1674 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02001675 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02001676endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02001677
1678" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001679func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02001680 call XbottomTests('c')
1681 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001682endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001683
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001684func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001685 call s:setup_commands(a:cchar)
1686
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001687 " clear all lists after the first one, then replace the first one.
1688 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001689 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001690 let entry = {'filename': 'foo', 'lnum': 42}
1691 call g:Xsetlist([entry], 'r')
1692 call g:Xsetlist([entry, entry])
1693 call g:Xsetlist([entry, entry, entry])
1694 let res = split(execute(a:cchar . 'hist'), "\n")
1695 call assert_equal(3, len(res))
1696 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
1697 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
1698 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
1699 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02001700
1701 call g:Xsetlist([], 'f')
1702 let l = split(execute(a:cchar . 'hist'), "\n")
1703 call assert_equal('No entries', l[0])
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001704endfunc
1705
1706func Test_history()
1707 call HistoryTest('c')
1708 call HistoryTest('l')
1709endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02001710
1711func Test_duplicate_buf()
1712 " make sure we can get the highest buffer number
1713 edit DoesNotExist
1714 edit DoesNotExist2
1715 let last_buffer = bufnr("$")
1716
1717 " make sure only one buffer is created
1718 call writefile(['this one', 'that one'], 'Xgrepthis')
1719 vimgrep one Xgrepthis
1720 vimgrep one Xgrepthis
1721 call assert_equal(last_buffer + 1, bufnr("$"))
1722
1723 call delete('Xgrepthis')
1724endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02001725
1726" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001727func Xproperty_tests(cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001728 call s:setup_commands(a:cchar)
1729
1730 " Error cases
1731 call assert_fails('call g:Xgetlist(99)', 'E715:')
1732 call assert_fails('call g:Xsetlist(99)', 'E714:')
1733 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
1734
1735 " Set and get the title
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001736 call g:Xsetlist([])
Bram Moolenaard823fa92016-08-12 16:29:27 +02001737 Xopen
1738 wincmd p
1739 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001740 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
1741 call assert_equal(0, s)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001742 let d = g:Xgetlist({"title":1})
1743 call assert_equal('Sample', d.title)
1744
1745 Xopen
1746 call assert_equal('Sample', w:quickfix_title)
1747 Xclose
1748
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02001749 " Tests for action argument
1750 silent! Xolder 999
1751 let qfnr = g:Xgetlist({'all':1}).nr
1752 call g:Xsetlist([], 'r', {'title' : 'N1'})
1753 call assert_equal('N1', g:Xgetlist({'all':1}).title)
1754 call g:Xsetlist([], ' ', {'title' : 'N2'})
1755 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
Bram Moolenaar890680c2016-09-27 21:28:56 +02001756
1757 let res = g:Xgetlist({'nr': 0})
1758 call assert_equal(qfnr + 1, res.nr)
1759 call assert_equal(['nr'], keys(res))
1760
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02001761 call g:Xsetlist([], ' ', {'title' : 'N3'})
1762 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
1763
Bram Moolenaaree85df32017-03-19 14:19:50 +01001764 " Changing the title of an earlier quickfix list
Bram Moolenaar55b69262017-08-13 13:42:01 +02001765 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
Bram Moolenaaree85df32017-03-19 14:19:50 +01001766 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
1767
1768 " Changing the title of an invalid quickfix list
1769 call assert_equal(-1, g:Xsetlist([], ' ',
1770 \ {'title' : 'SomeTitle', 'nr' : 99}))
1771 call assert_equal(-1, g:Xsetlist([], ' ',
1772 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
1773
1774 if a:cchar == 'c'
1775 copen
1776 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
1777 cclose
1778 endif
1779
Bram Moolenaard823fa92016-08-12 16:29:27 +02001780 " Invalid arguments
1781 call assert_fails('call g:Xgetlist([])', 'E715')
1782 call assert_fails('call g:Xsetlist([], "a", [])', 'E715')
1783 let s = g:Xsetlist([], 'a', {'abc':1})
1784 call assert_equal(-1, s)
1785
1786 call assert_equal({}, g:Xgetlist({'abc':1}))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001787 call assert_equal({}, g:Xgetlist({'nr':99, 'title':1}))
1788 call assert_equal({}, g:Xgetlist({'nr':[], 'title':1}))
Bram Moolenaard823fa92016-08-12 16:29:27 +02001789
1790 if a:cchar == 'l'
Bram Moolenaar890680c2016-09-27 21:28:56 +02001791 call assert_equal({}, getloclist(99, {'title': 1}))
Bram Moolenaard823fa92016-08-12 16:29:27 +02001792 endif
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001793
1794 " Context related tests
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001795 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
1796 call assert_equal(0, s)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001797 call test_garbagecollect_now()
1798 let d = g:Xgetlist({'context':1})
1799 call assert_equal([1,2,3], d.context)
1800 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
1801 let d = g:Xgetlist({'context':1})
1802 call assert_equal({'color':'green'}, d.context)
1803 call g:Xsetlist([], 'a', {'context':"Context info"})
1804 let d = g:Xgetlist({'context':1})
1805 call assert_equal("Context info", d.context)
1806 call g:Xsetlist([], 'a', {'context':246})
1807 let d = g:Xgetlist({'context':1})
1808 call assert_equal(246, d.context)
1809 if a:cchar == 'l'
1810 " Test for copying context across two different location lists
1811 new | only
1812 let w1_id = win_getid()
1813 let l = [1]
1814 call setloclist(0, [], 'a', {'context':l})
1815 new
1816 let w2_id = win_getid()
1817 call add(l, 2)
1818 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
1819 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
1820 unlet! l
1821 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
1822 only
1823 call setloclist(0, [], 'f')
1824 call assert_equal({}, getloclist(0, {'context':1}))
1825 endif
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001826
1827 " Test for changing the context of previous quickfix lists
1828 call g:Xsetlist([], 'f')
1829 Xexpr "One"
1830 Xexpr "Two"
1831 Xexpr "Three"
Bram Moolenaar55b69262017-08-13 13:42:01 +02001832 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
1833 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001834 " Also, check for setting the context using quickfix list number zero.
Bram Moolenaar55b69262017-08-13 13:42:01 +02001835 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001836 call test_garbagecollect_now()
1837 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
1838 call assert_equal([1], l.context)
1839 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
1840 call assert_equal([2], l.context)
1841 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
1842 call assert_equal([3], l.context)
1843
1844 " Test for changing the context through reference and for garbage
1845 " collection of quickfix context
1846 let l = ["red"]
1847 call g:Xsetlist([], ' ', {'context' : l})
1848 call add(l, "blue")
1849 let x = g:Xgetlist({'context' : 1})
1850 call add(x.context, "green")
1851 call assert_equal(["red", "blue", "green"], l)
1852 call assert_equal(["red", "blue", "green"], x.context)
1853 unlet l
1854 call test_garbagecollect_now()
1855 let m = g:Xgetlist({'context' : 1})
1856 call assert_equal(["red", "blue", "green"], m.context)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001857
1858 " Test for setting/getting items
1859 Xexpr ""
1860 let qfprev = g:Xgetlist({'nr':0})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001861 let s = g:Xsetlist([], ' ', {'title':'Green',
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001862 \ 'items' : [{'filename':'F1', 'lnum':10}]})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001863 call assert_equal(0, s)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001864 let qfcur = g:Xgetlist({'nr':0})
1865 call assert_true(qfcur.nr == qfprev.nr + 1)
1866 let l = g:Xgetlist({'items':1})
1867 call assert_equal('F1', bufname(l.items[0].bufnr))
1868 call assert_equal(10, l.items[0].lnum)
1869 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
1870 \ {'filename':'F2', 'lnum':30}]})
1871 let l = g:Xgetlist({'items':1})
1872 call assert_equal('F2', bufname(l.items[2].bufnr))
1873 call assert_equal(30, l.items[2].lnum)
1874 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
1875 let l = g:Xgetlist({'items':1})
1876 call assert_equal('F3', bufname(l.items[0].bufnr))
1877 call assert_equal(40, l.items[0].lnum)
1878 call g:Xsetlist([], 'r', {'items' : []})
1879 let l = g:Xgetlist({'items':1})
1880 call assert_equal(0, len(l.items))
1881
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02001882 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
1883 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
1884 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
1885 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
1886
Bram Moolenaarae338332017-08-11 20:25:26 +02001887 " The following used to crash Vim with address sanitizer
1888 call g:Xsetlist([], 'f')
1889 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
1890 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
1891
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001892 " Save and restore the quickfix stack
1893 call g:Xsetlist([], 'f')
1894 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
1895 Xexpr "File1:10:Line1"
1896 Xexpr "File2:20:Line2"
1897 Xexpr "File3:30:Line3"
1898 let last_qf = g:Xgetlist({'nr':'$'}).nr
1899 call assert_equal(3, last_qf)
1900 let qstack = []
1901 for i in range(1, last_qf)
1902 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
1903 endfor
1904 call g:Xsetlist([], 'f')
1905 for i in range(len(qstack))
1906 call g:Xsetlist([], ' ', qstack[i])
1907 endfor
1908 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
1909 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
1910 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
1911 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
1912 call g:Xsetlist([], 'f')
1913
1914 " Swap two quickfix lists
1915 Xexpr "File1:10:Line10"
1916 Xexpr "File2:20:Line20"
1917 Xexpr "File3:30:Line30"
1918 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
1919 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
1920 let l1=g:Xgetlist({'nr':1,'all':1})
1921 let l2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02001922 let save_id = l1.id
1923 let l1.id=l2.id
1924 let l2.id=save_id
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001925 call g:Xsetlist([], 'r', l1)
1926 call g:Xsetlist([], 'r', l2)
1927 let newl1=g:Xgetlist({'nr':1,'all':1})
1928 let newl2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02001929 call assert_equal('Fruits', newl1.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001930 call assert_equal(['Fruits'], newl1.context)
1931 call assert_equal('Line20', newl1.items[0].text)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02001932 call assert_equal('Colors', newl2.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001933 call assert_equal(['Colors'], newl2.context)
1934 call assert_equal('Line10', newl2.items[0].text)
1935 call g:Xsetlist([], 'f')
Bram Moolenaaree85df32017-03-19 14:19:50 +01001936endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02001937
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001938func Test_qf_property()
Bram Moolenaard823fa92016-08-12 16:29:27 +02001939 call Xproperty_tests('c')
1940 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01001941endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02001942
1943" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001944func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02001945 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001946endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02001947
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001948func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02001949 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
1950 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
1951
1952 let g:acmds = []
1953 cexpr "F1:10:Line 10"
1954 caddexpr "F1:20:Line 20"
1955 cgetexpr "F1:30:Line 30"
1956 enew! | call append(0, "F2:10:Line 10")
1957 cbuffer!
1958 enew! | call append(0, "F2:20:Line 20")
1959 cgetbuffer
1960 enew! | call append(0, "F2:30:Line 30")
1961 caddbuffer
1962
1963 let l = ['precexpr',
1964 \ 'postcexpr',
1965 \ 'precaddexpr',
1966 \ 'postcaddexpr',
1967 \ 'precgetexpr',
1968 \ 'postcgetexpr',
1969 \ 'precbuffer',
1970 \ 'postcbuffer',
1971 \ 'precgetbuffer',
1972 \ 'postcgetbuffer',
1973 \ 'precaddbuffer',
1974 \ 'postcaddbuffer']
1975 call assert_equal(l, g:acmds)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001976endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01001977
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001978func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01001979 set efm=%m
1980 lgetexpr '?'
1981
1982 try
1983 call DoesNotExit()
1984 catch
1985 lgetexpr '1'
1986 finally
1987 lgetexpr '1'
1988 endtry
1989
1990 call assert_equal('1', getloclist(0)[0].text)
1991
1992 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001993endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01001994
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001995func Test_caddbuffer_wrong()
1996 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01001997 let save_efm = &efm
1998 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
1999 cgetexpr ['WWWW', 'EEEE', 'CCCC']
2000 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002001 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002002 bwipe!
2003endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002004
2005func Test_caddexpr_wrong()
2006 " This used to cause a memory access in freed memory.
2007 cbuffer
2008 cbuffer
2009 copen
2010 let save_efm = &efm
2011 set efm=%
2012 call assert_fails('caddexpr ""', 'E376:')
2013 let &efm = save_efm
2014endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01002015
2016func Test_dirstack_cleanup()
2017 " This used to cause a memory access in freed memory.
2018 let save_efm = &efm
2019 lexpr '0'
2020 lopen
2021 fun X(c)
2022 let save_efm=&efm
2023 set efm=%D%f
2024 if a:c == 'c'
2025 caddexpr '::'
2026 else
2027 laddexpr ':0:0'
2028 endif
2029 let &efm=save_efm
2030 endfun
2031 call X('c')
2032 call X('l')
2033 call setqflist([], 'r')
2034 caddbuffer
2035 let &efm = save_efm
2036endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002037
2038" Tests for jumping to entries from the location list window and quickfix
2039" window
2040func Test_cwindow_jump()
2041 set efm=%f%%%l%%%m
2042 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2043 lopen | only
2044 lfirst
2045 call assert_true(winnr('$') == 2)
2046 call assert_true(winnr() == 1)
2047 " Location list for the new window should be set
2048 call assert_true(getloclist(0)[2].text == 'Line 30')
2049
2050 " Open a scratch buffer
2051 " Open a new window and create a location list
2052 " Open the location list window and close the other window
2053 " Jump to an entry.
2054 " Should create a new window and jump to the entry. The scrtach buffer
2055 " should not be used.
2056 enew | only
2057 set buftype=nofile
2058 below new
2059 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2060 lopen
2061 2wincmd c
2062 lnext
2063 call assert_true(winnr('$') == 3)
2064 call assert_true(winnr() == 2)
2065
2066 " Open two windows with two different location lists
2067 " Open the location list window and close the previous window
2068 " Jump to an entry in the location list window
2069 " Should open the file in the first window and not set the location list.
2070 enew | only
2071 lgetexpr ["F1%5%Line 5"]
2072 below new
2073 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2074 lopen
2075 2wincmd c
2076 lnext
2077 call assert_true(winnr() == 1)
2078 call assert_true(getloclist(0)[0].text == 'Line 5')
2079
2080 enew | only
2081 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2082 copen
2083 cnext
2084 call assert_true(winnr('$') == 2)
2085 call assert_true(winnr() == 1)
2086
2087 enew | only
2088 set efm&vim
2089endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01002090
2091func XvimgrepTests(cchar)
2092 call s:setup_commands(a:cchar)
2093
2094 call writefile(['Editor:VIM vim',
2095 \ 'Editor:Emacs EmAcS',
2096 \ 'Editor:Notepad NOTEPAD'], 'Xtestfile1')
2097 call writefile(['Linux', 'MacOS', 'MS-Windows'], 'Xtestfile2')
2098
2099 " Error cases
2100 call assert_fails('Xvimgrep /abc *', 'E682:')
2101
2102 let @/=''
2103 call assert_fails('Xvimgrep // *', 'E35:')
2104
2105 call assert_fails('Xvimgrep abc', 'E683:')
2106 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
2107 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
2108
2109 Xexpr ""
2110 Xvimgrepadd Notepad Xtestfile1
2111 Xvimgrepadd MacOS Xtestfile2
2112 let l = g:Xgetlist()
2113 call assert_equal(2, len(l))
2114 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
2115
2116 Xvimgrep #\cvim#g Xtestfile?
2117 let l = g:Xgetlist()
2118 call assert_equal(2, len(l))
2119 call assert_equal(8, l[0].col)
2120 call assert_equal(12, l[1].col)
2121
2122 1Xvimgrep ?Editor? Xtestfile*
2123 let l = g:Xgetlist()
2124 call assert_equal(1, len(l))
2125 call assert_equal('Editor:VIM vim', l[0].text)
2126
2127 edit +3 Xtestfile2
2128 Xvimgrep +\cemacs+j Xtestfile1
2129 let l = g:Xgetlist()
2130 call assert_equal('Xtestfile2', bufname(''))
2131 call assert_equal('Editor:Emacs EmAcS', l[0].text)
2132
2133 call delete('Xtestfile1')
2134 call delete('Xtestfile2')
2135endfunc
2136
2137" Tests for the :vimgrep command
2138func Test_vimgrep()
2139 call XvimgrepTests('c')
2140 call XvimgrepTests('l')
2141endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002142
2143func XfreeTests(cchar)
2144 call s:setup_commands(a:cchar)
2145
2146 enew | only
2147
2148 " Deleting the quickfix stack should work even When the current list is
2149 " somewhere in the middle of the stack
2150 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2151 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2152 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2153 Xolder
2154 call g:Xsetlist([], 'f')
2155 call assert_equal(0, len(g:Xgetlist()))
2156
2157 " After deleting the stack, adding a new list should create a stack with a
2158 " single list.
2159 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2160 call assert_equal(1, g:Xgetlist({'all':1}).nr)
2161
2162 " Deleting the stack from a quickfix window should update/clear the
2163 " quickfix/location list window.
2164 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2165 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2166 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2167 Xolder
2168 Xwindow
2169 call g:Xsetlist([], 'f')
2170 call assert_equal(2, winnr('$'))
2171 call assert_equal(1, line('$'))
2172 Xclose
2173
2174 " Deleting the stack from a non-quickfix window should update/clear the
2175 " quickfix/location list window.
2176 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2177 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2178 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2179 Xolder
2180 Xwindow
2181 wincmd p
2182 call g:Xsetlist([], 'f')
2183 call assert_equal(0, len(g:Xgetlist()))
2184 wincmd p
2185 call assert_equal(2, winnr('$'))
2186 call assert_equal(1, line('$'))
2187
2188 " After deleting the location list stack, if the location list window is
2189 " opened, then a new location list should be created. So opening the
2190 " location list window again should not create a new window.
2191 if a:cchar == 'l'
2192 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2193 wincmd p
2194 lopen
2195 call assert_equal(2, winnr('$'))
2196 endif
2197 Xclose
2198endfunc
2199
2200" Tests for the quickifx free functionality
2201func Test_qf_free()
2202 call XfreeTests('c')
2203 call XfreeTests('l')
2204endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002205
2206" Test for buffer overflow when parsing lines and adding new entries to
2207" the quickfix list.
2208func Test_bufoverflow()
2209 set efm=%f:%l:%m
2210 cgetexpr ['File1:100:' . repeat('x', 1025)]
2211
2212 set efm=%+GCompiler:\ %.%#,%f:%l:%m
2213 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
2214
2215 set efm=%DEntering\ directory\ %f,%f:%l:%m
2216 cgetexpr ['Entering directory ' . repeat('a', 1006),
2217 \ 'File1:10:Hello World']
2218 set efm&vim
2219endfunc
2220
Bram Moolenaar875feea2017-06-11 16:07:51 +02002221" Tests for getting the quickfix stack size
2222func XsizeTests(cchar)
2223 call s:setup_commands(a:cchar)
2224
2225 call g:Xsetlist([], 'f')
2226 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2227 call assert_equal(1, len(g:Xgetlist({'nr':'$', 'all':1})))
2228 call assert_equal(0, len(g:Xgetlist({'nr':0})))
2229
2230 Xexpr "File1:10:Line1"
2231 Xexpr "File2:20:Line2"
2232 Xexpr "File3:30:Line3"
2233 Xolder | Xolder
2234 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2235 call g:Xsetlist([], 'f')
2236
2237 Xexpr "File1:10:Line1"
2238 Xexpr "File2:20:Line2"
2239 Xexpr "File3:30:Line3"
2240 Xolder | Xolder
2241 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
2242 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
2243endfunc
2244
2245func Test_Qf_Size()
2246 call XsizeTests('c')
2247 call XsizeTests('l')
2248endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02002249
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002250func Test_cclose_from_copen()
2251 augroup QF_Test
2252 au!
2253 au FileType qf :call assert_fails(':cclose', 'E788')
2254 augroup END
2255 copen
2256 augroup QF_Test
2257 au!
2258 augroup END
2259 augroup! QF_Test
2260endfunc
2261
Bram Moolenaar18141832017-06-25 21:17:25 +02002262func Test_cclose_in_autocmd()
2263 " Problem is only triggered if "starting" is zero, so that the OptionsSet
2264 " event will be triggered.
2265 call test_override('starting', 1)
2266 augroup QF_Test
2267 au!
2268 au FileType qf :call assert_fails(':cclose', 'E788')
2269 augroup END
2270 copen
2271 augroup QF_Test
2272 au!
2273 augroup END
2274 augroup! QF_Test
2275 call test_override('starting', 0)
2276endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002277
2278func Test_resize_from_copen()
2279 augroup QF_Test
2280 au!
2281 au FileType qf resize 5
2282 augroup END
2283 try
2284 " This should succeed without any exception. No other buffers are
2285 " involved in the autocmd.
2286 copen
2287 finally
2288 augroup QF_Test
2289 au!
2290 augroup END
2291 augroup! QF_Test
2292 endtry
2293endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02002294
2295" Tests for the quickfix buffer b:changedtick variable
2296func Xchangedtick_tests(cchar)
2297 call s:setup_commands(a:cchar)
2298
2299 new | only
2300
2301 Xexpr "" | Xexpr "" | Xexpr ""
2302
2303 Xopen
2304 Xolder
2305 Xolder
2306 Xaddexpr "F1:10:Line10"
2307 Xaddexpr "F2:20:Line20"
2308 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
2309 call g:Xsetlist([], 'f')
2310 call assert_equal(8, getbufvar('%', 'changedtick'))
2311 Xclose
2312endfunc
2313
2314func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02002315 call Xchangedtick_tests('c')
2316 call Xchangedtick_tests('l')
2317endfunc
2318
2319" Tests for parsing an expression using setqflist()
2320func Xsetexpr_tests(cchar)
2321 call s:setup_commands(a:cchar)
2322
2323 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002324 call g:Xsetlist([], ' ', {'lines' : t})
2325 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002326
2327 let l = g:Xgetlist()
2328 call assert_equal(3, len(l))
2329 call assert_equal(20, l[1].lnum)
2330 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002331 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002332 let l = g:Xgetlist()
2333 call assert_equal(1, len(l))
2334 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002335 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
2336 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02002337
2338 call g:Xsetlist([], 'f')
2339 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002340 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
2341 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
2342 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
2343 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002344 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
2345 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02002346
2347 " Adding entries using a custom efm
2348 set efm&
2349 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
2350 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
2351 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
2352 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
2353 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
2354 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
2355 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
2356 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02002357endfunc
2358
2359func Test_setexpr()
2360 call Xsetexpr_tests('c')
2361 call Xsetexpr_tests('l')
2362endfunc
2363
2364" Tests for per quickfix/location list directory stack
2365func Xmultidirstack_tests(cchar)
2366 call s:setup_commands(a:cchar)
2367
2368 call g:Xsetlist([], 'f')
2369 Xexpr "" | Xexpr ""
2370
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002371 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
2372 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
2373 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
2374 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002375
2376 let l1 = g:Xgetlist({'nr':1, 'items':1})
2377 let l2 = g:Xgetlist({'nr':2, 'items':1})
2378 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
2379 call assert_equal(3, l1.items[1].lnum)
2380 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
2381 call assert_equal(5, l2.items[1].lnum)
2382endfunc
2383
2384func Test_multidirstack()
2385 call mkdir('Xone/a', 'p')
2386 call mkdir('Xtwo/a', 'p')
2387 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2388 call writefile(lines, 'Xone/a/one.txt')
2389 call writefile(lines, 'Xtwo/a/two.txt')
2390 let save_efm = &efm
2391 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
2392
2393 call Xmultidirstack_tests('c')
2394 call Xmultidirstack_tests('l')
2395
2396 let &efm = save_efm
2397 call delete('Xone', 'rf')
2398 call delete('Xtwo', 'rf')
2399endfunc
2400
2401" Tests for per quickfix/location list file stack
2402func Xmultifilestack_tests(cchar)
2403 call s:setup_commands(a:cchar)
2404
2405 call g:Xsetlist([], 'f')
2406 Xexpr "" | Xexpr ""
2407
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002408 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
2409 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
2410 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
2411 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002412
2413 let l1 = g:Xgetlist({'nr':1, 'items':1})
2414 let l2 = g:Xgetlist({'nr':2, 'items':1})
2415 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
2416 call assert_equal(3, l1.items[1].lnum)
2417 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
2418 call assert_equal(5, l2.items[1].lnum)
2419endfunc
2420
2421func Test_multifilestack()
2422 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2423 call writefile(lines, 'one.txt')
2424 call writefile(lines, 'two.txt')
2425 let save_efm = &efm
2426 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
2427
2428 call Xmultifilestack_tests('c')
2429 call Xmultifilestack_tests('l')
2430
2431 let &efm = save_efm
2432 call delete('one.txt')
2433 call delete('two.txt')
2434endfunc
2435
2436" Tests for per buffer 'efm' setting
2437func Test_perbuf_efm()
2438 call writefile(["File1-10-Line10"], 'one.txt')
2439 call writefile(["File2#20#Line20"], 'two.txt')
2440 set efm=%f#%l#%m
2441 new | only
2442 new
2443 setlocal efm=%f-%l-%m
2444 cfile one.txt
2445 wincmd w
2446 caddfile two.txt
2447
2448 let l = getqflist()
2449 call assert_equal(10, l[0].lnum)
2450 call assert_equal('Line20', l[1].text)
2451
2452 set efm&
2453 new | only
2454 call delete('one.txt')
2455 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02002456endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002457
2458" Open multiple help windows using ":lhelpgrep
2459" This test used to crash Vim
2460func Test_Multi_LL_Help()
2461 new | only
2462 lhelpgrep window
2463 lopen
2464 e#
2465 lhelpgrep buffer
2466 call assert_equal(3, winnr('$'))
2467 call assert_true(len(getloclist(1)) != 0)
2468 call assert_true(len(getloclist(2)) != 0)
2469 new | only
2470endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02002471
2472" Tests for adding new quickfix lists using setqflist()
2473func XaddQf_tests(cchar)
2474 call s:setup_commands(a:cchar)
2475
2476 " Create a new list using ' ' for action
2477 call g:Xsetlist([], 'f')
2478 call g:Xsetlist([], ' ', {'title' : 'Test1'})
2479 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2480 call assert_equal(1, l.nr)
2481 call assert_equal('Test1', l.title)
2482
2483 " Create a new list using ' ' for action and '$' for 'nr'
2484 call g:Xsetlist([], 'f')
2485 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
2486 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2487 call assert_equal(1, l.nr)
2488 call assert_equal('Test2', l.title)
2489
2490 " Create a new list using 'a' for action
2491 call g:Xsetlist([], 'f')
2492 call g:Xsetlist([], 'a', {'title' : 'Test3'})
2493 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2494 call assert_equal(1, l.nr)
2495 call assert_equal('Test3', l.title)
2496
2497 " Create a new list using 'a' for action and '$' for 'nr'
2498 call g:Xsetlist([], 'f')
2499 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
2500 call g:Xsetlist([], 'a', {'title' : 'Test4'})
2501 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2502 call assert_equal(1, l.nr)
2503 call assert_equal('Test4', l.title)
2504
2505 " Adding a quickfix list should remove all the lists following the current
2506 " list.
2507 Xexpr "" | Xexpr "" | Xexpr ""
2508 silent! 10Xolder
2509 call g:Xsetlist([], ' ', {'title' : 'Test5'})
2510 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2511 call assert_equal(2, l.nr)
2512 call assert_equal('Test5', l.title)
2513
2514 " Add a quickfix list using '$' as the list number.
2515 let lastqf = g:Xgetlist({'nr':'$'}).nr
2516 silent! 99Xolder
2517 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
2518 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2519 call assert_equal(lastqf + 1, l.nr)
2520 call assert_equal('Test6', l.title)
2521
2522 " Add a quickfix list using 'nr' set to one more than the quickfix
2523 " list size.
2524 let lastqf = g:Xgetlist({'nr':'$'}).nr
2525 silent! 99Xolder
2526 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
2527 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2528 call assert_equal(lastqf + 1, l.nr)
2529 call assert_equal('Test7', l.title)
2530
2531 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
2532 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
2533 silent! 99Xolder
2534 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
2535 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2536 call assert_equal(10, l.nr)
2537 call assert_equal('Test8', l.title)
2538
2539 " Add a quickfix list using 'nr' set to a value greater than 10
2540 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
2541
2542 " Try adding a quickfix list with 'nr' set to a value greater than the
2543 " quickfix list size but less than 10.
2544 call g:Xsetlist([], 'f')
2545 Xexpr "" | Xexpr "" | Xexpr ""
2546 silent! 99Xolder
2547 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
2548
2549 " Add a quickfix list using 'nr' set to a some string or list
2550 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
2551endfunc
2552
2553func Test_add_qf()
2554 call XaddQf_tests('c')
2555 call XaddQf_tests('l')
2556endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002557
2558" Test for getting the quickfix list items from some text without modifying
2559" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002560func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002561 call s:setup_commands(a:cchar)
2562 call g:Xsetlist([], 'f')
2563
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002564 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002565 call assert_equal(2, len(l))
2566 call assert_equal(30, l[1].lnum)
2567
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002568 call assert_equal({}, g:Xgetlist({'lines' : 10}))
2569 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
2570 call assert_equal([], g:Xgetlist({'lines' : []}).items)
2571 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002572
Bram Moolenaar36538222017-09-02 19:51:44 +02002573 " Parse text using a custom efm
2574 set efm&
2575 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
2576 call assert_equal('Line30', l[0].text)
2577 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
2578 call assert_equal('File3:30:Line30', l[0].text)
2579 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
2580 call assert_equal({}, l)
2581 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
2582 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
2583
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002584 " Make sure that the quickfix stack is not modified
2585 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
2586endfunc
2587
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002588func Test_get_list_from_lines()
2589 call XgetListFromLines('c')
2590 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002591endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002592
2593" Tests for the quickfix list id
2594func Xqfid_tests(cchar)
2595 call s:setup_commands(a:cchar)
2596
2597 call g:Xsetlist([], 'f')
2598 call assert_equal({}, g:Xgetlist({'id':0}))
2599 Xexpr ''
2600 let start_id = g:Xgetlist({'id' : 0}).id
2601 Xexpr '' | Xexpr ''
2602 Xolder
2603 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
2604 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
2605 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
2606 call assert_equal({}, g:Xgetlist({'id':0, 'nr':99}))
2607 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
2608 call assert_equal({}, g:Xgetlist({'id':99, 'nr':0}))
2609 call assert_equal({}, g:Xgetlist({'id':"abc", 'nr':0}))
2610
2611 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
2612 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002613 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002614 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
2615 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
2616 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
2617
2618 let qfid = g:Xgetlist({'id':0, 'nr':0})
2619 call g:Xsetlist([], 'f')
2620 call assert_equal({}, g:Xgetlist({'id':qfid, 'nr':0}))
2621endfunc
2622
2623func Test_qf_id()
2624 call Xqfid_tests('c')
2625 call Xqfid_tests('l')
2626endfunc