blob: ee98502a79b643fd9f60b7bf2b745aa58692e115 [file] [log] [blame]
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001" Test for the quickfix feature.
Bram Moolenaarda59dd52016-01-05 21:59:58 +01002
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>
Bram Moolenaar74240d32017-12-10 15:26:15 +010031 command! -nargs=* -bang -range Xnfile <mods><count>cnfile<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020032 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 Moolenaar74240d32017-12-10 15:26:15 +010039 command! -nargs=0 -count Xcc <count>cc
Bram Moolenaar3ff33112019-05-03 21:56:35 +020040 command! -count=1 -nargs=0 Xbelow <mods><count>cbelow
41 command! -count=1 -nargs=0 Xabove <mods><count>cabove
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020042 let g:Xgetlist = function('getqflist')
43 let g:Xsetlist = function('setqflist')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020044 call setqflist([], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020045 else
46 command! -nargs=* -bang Xlist <mods>llist<bang> <args>
47 command! -nargs=* Xgetexpr <mods>lgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020048 command! -nargs=* Xaddexpr <mods>laddexpr <args>
Bram Moolenaar55b69262017-08-13 13:42:01 +020049 command! -nargs=* -count Xolder <mods><count>lolder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020050 command! -nargs=* Xnewer <mods>lnewer <args>
51 command! -nargs=* Xopen <mods>lopen <args>
52 command! -nargs=* Xwindow <mods>lwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020053 command! -nargs=* Xbottom <mods>lbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020054 command! -nargs=* Xclose <mods>lclose <args>
55 command! -nargs=* -bang Xfile <mods>lfile<bang> <args>
56 command! -nargs=* Xgetfile <mods>lgetfile <args>
57 command! -nargs=* Xaddfile <mods>laddfile <args>
58 command! -nargs=* -bang Xbuffer <mods>lbuffer<bang> <args>
59 command! -nargs=* Xgetbuffer <mods>lgetbuffer <args>
60 command! -nargs=* Xaddbuffer <mods>laddbuffer <args>
61 command! -nargs=* Xrewind <mods>lrewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020062 command! -count -nargs=* -bang Xnext <mods><count>lnext<bang> <args>
63 command! -count -nargs=* -bang Xprev <mods><count>lprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020064 command! -nargs=* -bang Xfirst <mods>lfirst<bang> <args>
65 command! -nargs=* -bang Xlast <mods>llast<bang> <args>
Bram Moolenaar74240d32017-12-10 15:26:15 +010066 command! -nargs=* -bang -range Xnfile <mods><count>lnfile<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020067 command! -nargs=* -bang Xpfile <mods>lpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020068 command! -nargs=* Xexpr <mods>lexpr <args>
Bram Moolenaaree85df32017-03-19 14:19:50 +010069 command! -range -nargs=* Xvimgrep <mods><count>lvimgrep <args>
70 command! -nargs=* Xvimgrepadd <mods>lvimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020071 command! -nargs=* Xgrep <mods> lgrep <args>
72 command! -nargs=* Xgrepadd <mods> lgrepadd <args>
73 command! -nargs=* Xhelpgrep lhelpgrep <args>
Bram Moolenaar74240d32017-12-10 15:26:15 +010074 command! -nargs=0 -count Xcc <count>ll
Bram Moolenaar3ff33112019-05-03 21:56:35 +020075 command! -count=1 -nargs=0 Xbelow <mods><count>lbelow
76 command! -count=1 -nargs=0 Xabove <mods><count>labove
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020077 let g:Xgetlist = function('getloclist', [0])
78 let g:Xsetlist = function('setloclist', [0])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020079 call setloclist(0, [], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020080 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +010081endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020082
Bram Moolenaarda59dd52016-01-05 21:59:58 +010083" Tests for the :clist and :llist commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +010084func XlistTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020085 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +010086
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020087 if a:cchar == 'l'
88 call assert_fails('llist', 'E776:')
89 endif
Bram Moolenaarda59dd52016-01-05 21:59:58 +010090 " With an empty list, command should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020091 Xgetexpr []
92 silent! Xlist
Bram Moolenaarda59dd52016-01-05 21:59:58 +010093 call assert_true(v:errmsg ==# 'E42: No Errors')
94
95 " Populate the list and then try
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020096 Xgetexpr ['non-error 1', 'Xtestfile1:1:3:Line1',
Bram Moolenaarda59dd52016-01-05 21:59:58 +010097 \ 'non-error 2', 'Xtestfile2:2:2:Line2',
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020098 \ 'non-error 3', 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +010099
100 " List only valid 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([' 2 Xtestfile1:1 col 3: Line1',
103 \ ' 4 Xtestfile2:2 col 2: Line2',
104 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
105
106 " List all the entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100107 let l = split(execute('Xlist!', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100108 call assert_equal([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1',
109 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2',
110 \ ' 5: non-error 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
111
112 " List a range of errors
Bram Moolenaaree85df32017-03-19 14:19:50 +0100113 let l = split(execute('Xlist 3,6', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100114 call assert_equal([' 4 Xtestfile2:2 col 2: Line2',
115 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
116
Bram Moolenaaree85df32017-03-19 14:19:50 +0100117 let l = split(execute('Xlist! 3,4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100118 call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
119
Bram Moolenaaree85df32017-03-19 14:19:50 +0100120 let l = split(execute('Xlist -6,-4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100121 call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l)
122
Bram Moolenaaree85df32017-03-19 14:19:50 +0100123 let l = split(execute('Xlist! -5,-3', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100124 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
125 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100126
127 " Test for '+'
Bram Moolenaaree85df32017-03-19 14:19:50 +0100128 let l = split(execute('Xlist! +2', ''), "\n")
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100129 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
130 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaaree85df32017-03-19 14:19:50 +0100131
132 " Different types of errors
133 call g:Xsetlist([{'lnum':10,'col':5,'type':'W', 'text':'Warning','nr':11},
134 \ {'lnum':20,'col':10,'type':'e','text':'Error','nr':22},
135 \ {'lnum':30,'col':15,'type':'i','text':'Info','nr':33},
136 \ {'lnum':40,'col':20,'type':'x', 'text':'Other','nr':44},
137 \ {'lnum':50,'col':25,'type':"\<C-A>",'text':'one','nr':55}])
138 let l = split(execute('Xlist', ""), "\n")
139 call assert_equal([' 1:10 col 5 warning 11: Warning',
140 \ ' 2:20 col 10 error 22: Error',
141 \ ' 3:30 col 15 info 33: Info',
142 \ ' 4:40 col 20 x 44: Other',
143 \ ' 5:50 col 25 55: one'], l)
144
Bram Moolenaard76ce852018-05-01 15:02:04 +0200145 " Test for module names, one needs to explicitly set `'valid':v:true` so
146 call g:Xsetlist([
147 \ {'lnum':10,'col':5,'type':'W','module':'Data.Text','text':'ModuleWarning','nr':11,'valid':v:true},
148 \ {'lnum':20,'col':10,'type':'W','module':'Data.Text','filename':'Data/Text.hs','text':'ModuleWarning','nr':22,'valid':v:true},
149 \ {'lnum':30,'col':15,'type':'W','filename':'Data/Text.hs','text':'FileWarning','nr':33,'valid':v:true}])
150 let l = split(execute('Xlist', ""), "\n")
151 call assert_equal([' 1 Data.Text:10 col 5 warning 11: ModuleWarning',
152 \ ' 2 Data.Text:20 col 10 warning 22: ModuleWarning',
153 \ ' 3 Data/Text.hs:30 col 15 warning 33: FileWarning'], l)
154
Bram Moolenaaree85df32017-03-19 14:19:50 +0100155 " Error cases
156 call assert_fails('Xlist abc', 'E488:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100157endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100158
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100159func Test_clist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100160 call XlistTests('c')
161 call XlistTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100162endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100163
164" Tests for the :colder, :cnewer, :lolder and :lnewer commands
165" Note that this test assumes that a quickfix/location list is
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100166" already set by the caller.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100167func XageTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200168 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100169
Bram Moolenaar87f59b02019-04-04 14:04:11 +0200170 if a:cchar == 'l'
171 " No location list for the current window
172 call assert_fails('lolder', 'E776:')
173 call assert_fails('lnewer', 'E776:')
174 endif
175
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200176 let list = [{'bufnr': bufnr('%'), 'lnum': 1}]
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200177 call g:Xsetlist(list)
178
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100179 " Jumping to a non existent list should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200180 silent! Xolder 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100181 call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
182
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200183 silent! Xnewer 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100184 call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
185
186 " Add three quickfix/location lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200187 Xgetexpr ['Xtestfile1:1:3:Line1']
188 Xgetexpr ['Xtestfile2:2:2:Line2']
189 Xgetexpr ['Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100190
191 " Go back two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200192 Xolder
193 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100194 call assert_equal('Line2', l[0].text)
195
196 " Go forward two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200197 Xnewer
198 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100199 call assert_equal('Line3', l[0].text)
200
201 " Test for the optional count argument
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200202 Xolder 2
203 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100204 call assert_equal('Line1', l[0].text)
205
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200206 Xnewer 2
207 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100208 call assert_equal('Line3', l[0].text)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100209endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100210
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100211func Test_cage()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100212 call XageTests('c')
213 call XageTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100214endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100215
216" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
217" commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100218func XwindowTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200219 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100220
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200221 " Opening the location list window without any errors should fail
222 if a:cchar == 'l'
223 call assert_fails('lopen', 'E776:')
224 endif
225
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100226 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200227 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100228
229 " Quickfix/Location window should not open with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200230 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100231 call assert_true(winnr('$') == 1)
232
233 " Create a list with valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200234 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
235 \ 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100236
237 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200238 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100239 call assert_true(winnr('$') == 2 && winnr() == 2 &&
240 \ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
Bram Moolenaar537ef082016-07-09 17:56:19 +0200241 redraw!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100242
243 " Close the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200244 Xclose
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100245 call assert_true(winnr('$') == 1)
246
247 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200248 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100249
250 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200251 Xopen 5
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100252 call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
253 \ && winheight('.') == 5)
254
255 " Opening the window again, should move the cursor to that window
256 wincmd t
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200257 Xopen 7
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100258 call assert_true(winnr('$') == 2 && winnr() == 2 &&
259 \ winheight('.') == 7 &&
260 \ getline('.') ==# '|| non-error 1')
261
262
263 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200264 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100265 call assert_true(winnr('$') == 1)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200266
267 if a:cchar == 'c'
268 " Opening the quickfix window in multiple tab pages should reuse the
269 " quickfix buffer
270 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
271 \ 'Xtestfile3:3:1:Line3']
272 Xopen
273 let qfbufnum = bufnr('%')
274 tabnew
275 Xopen
276 call assert_equal(qfbufnum, bufnr('%'))
277 new | only | tabonly
278 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100279endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100280
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100281func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100282 call XwindowTests('c')
283 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100284endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100285
Bram Moolenaar36d50222019-05-02 20:17:40 +0200286func Test_copenHeight()
287 copen
288 wincmd H
289 let height = winheight(0)
290 copen 10
291 call assert_equal(height, winheight(0))
292 quit
293endfunc
294
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100295" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
296" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100297func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200298 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100299
300 call writefile(['Xtestfile1:700:10:Line 700',
301 \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
302
303 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200304 Xfile Xqftestfile1
305 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100306 call assert_true(len(l) == 2 &&
307 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
308 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
309
Bram Moolenaar049cba92016-06-26 14:38:04 +0200310 " Test with a non existent file
311 call assert_fails('Xfile non_existent_file', 'E40')
312
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100313 " Run cfile/lfile from a modified buffer
314 enew!
315 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200316 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100317 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
318
319 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200320 Xaddfile Xqftestfile1
321 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100322 call assert_true(len(l) == 3 &&
323 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
324
325 call writefile(['Xtestfile1:222:77:Line 222',
326 \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
327
328 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200329 Xgetfile Xqftestfile1
330 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100331 call assert_true(len(l) == 2 &&
332 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
333 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
334
335 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100336endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100337
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100338func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100339 call XfileTests('c')
340 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100341endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100342
343" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
344" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100345func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200346 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100347
348 enew!
349 silent! call setline(1, ['Xtestfile7:700:10:Line 700',
350 \ 'Xtestfile8:800:15:Line 800'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200351 Xbuffer!
352 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100353 call assert_true(len(l) == 2 &&
354 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
355 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
356
357 enew!
358 silent! call setline(1, ['Xtestfile9:900:55:Line 900',
359 \ 'Xtestfile10:950:66:Line 950'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200360 Xgetbuffer
361 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100362 call assert_true(len(l) == 2 &&
363 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
364 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
365
366 enew!
367 silent! call setline(1, ['Xtestfile11:700:20:Line 700',
368 \ 'Xtestfile12:750:25:Line 750'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200369 Xaddbuffer
370 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100371 call assert_true(len(l) == 4 &&
372 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
373 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
374 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200375 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100376
Bram Moolenaaree85df32017-03-19 14:19:50 +0100377 " Check for invalid buffer
378 call assert_fails('Xbuffer 199', 'E474:')
379
380 " Check for unloaded buffer
381 edit Xtestfile1
382 let bnr = bufnr('%')
383 enew!
384 call assert_fails('Xbuffer ' . bnr, 'E681:')
385
386 " Check for invalid range
387 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
388 " commands. So directly call the commands.
389 if (a:cchar == 'c')
390 call assert_fails('900,999cbuffer', 'E16:')
391 else
392 call assert_fails('900,999lbuffer', 'E16:')
393 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100394endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100395
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100396func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100397 call XbufferTests('c')
398 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100399endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100400
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100401func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200402 call s:setup_commands(a:cchar)
403
404 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100405endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200406
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100407func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200408 call XexprTests('c')
409 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100410endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200411
412" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100413func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200414 call s:setup_commands(a:cchar)
415
Bram Moolenaar74240d32017-12-10 15:26:15 +0100416 call g:Xsetlist([], 'f')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200417 " Jumping to first or next location list entry without any error should
418 " result in failure
Bram Moolenaar74240d32017-12-10 15:26:15 +0100419 if a:cchar == 'c'
420 let err = 'E42:'
421 else
422 let err = 'E776:'
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200423 endif
Bram Moolenaar74240d32017-12-10 15:26:15 +0100424 call assert_fails('Xnext', err)
425 call assert_fails('Xprev', err)
426 call assert_fails('Xnfile', err)
427 call assert_fails('Xpfile', err)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200428
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200429 call s:create_test_file('Xqftestfile1')
430 call s:create_test_file('Xqftestfile2')
431
432 Xgetexpr ['Xqftestfile1:5:Line5',
433 \ 'Xqftestfile1:6:Line6',
434 \ 'Xqftestfile2:10:Line10',
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200435 \ 'Xqftestfile2:11:Line11',
436 \ 'RegularLine1',
437 \ 'RegularLine2']
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200438
439 Xfirst
440 call assert_fails('Xprev', 'E553')
441 call assert_fails('Xpfile', 'E553')
442 Xnfile
443 call assert_equal('Xqftestfile2', bufname('%'))
444 call assert_equal(10, line('.'))
445 Xpfile
446 call assert_equal('Xqftestfile1', bufname('%'))
447 call assert_equal(6, line('.'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100448 5Xcc
449 call assert_equal(5, g:Xgetlist({'idx':0}).idx)
450 2Xcc
451 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
452 10Xcc
453 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200454 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200455 Xprev
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200456 call assert_equal('Xqftestfile2', bufname('%'))
457 call assert_equal(11, line('.'))
458 call assert_fails('Xnext', 'E553')
459 call assert_fails('Xnfile', 'E553')
460 Xrewind
461 call assert_equal('Xqftestfile1', bufname('%'))
462 call assert_equal(5, line('.'))
463
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200464 10Xnext
465 call assert_equal('Xqftestfile2', bufname('%'))
466 call assert_equal(11, line('.'))
467 10Xprev
468 call assert_equal('Xqftestfile1', bufname('%'))
469 call assert_equal(5, line('.'))
470
Bram Moolenaar74240d32017-12-10 15:26:15 +0100471 " Jumping to an error from the error window using cc command
472 Xgetexpr ['Xqftestfile1:5:Line5',
473 \ 'Xqftestfile1:6:Line6',
474 \ 'Xqftestfile2:10:Line10',
475 \ 'Xqftestfile2:11:Line11']
476 Xopen
477 10Xcc
478 call assert_equal(11, line('.'))
479 call assert_equal('Xqftestfile2', bufname('%'))
480
481 " Jumping to an error from the error window (when only the error window is
482 " present)
483 Xopen | only
484 Xlast 1
485 call assert_equal(5, line('.'))
486 call assert_equal('Xqftestfile1', bufname('%'))
487
Bram Moolenaaree85df32017-03-19 14:19:50 +0100488 Xexpr ""
489 call assert_fails('Xnext', 'E42:')
490
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200491 call delete('Xqftestfile1')
492 call delete('Xqftestfile2')
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200493
494 " Should be able to use next/prev with invalid entries
495 Xexpr ""
496 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
497 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
498 Xaddexpr ['foo', 'bar', 'baz', 'quux', 'shmoo']
499 call assert_equal(5, g:Xgetlist({'size' : 0}).size)
500 Xlast
501 call assert_equal(5, g:Xgetlist({'idx' : 0}).idx)
502 Xfirst
503 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
504 2Xnext
505 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100506endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200507
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100508func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200509 call Xtest_browse('c')
510 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100511endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200512
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100513func Test_nomem()
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200514 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100515 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100516
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200517 call test_alloc_fail(GetAllocId('qf_dirname_now'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100518 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100519
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200520 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100521 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100522
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200523 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100524 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100525
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200526 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100527 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100528
529endfunc
530
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100531func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200532 call s:setup_commands(a:cchar)
533 Xhelpgrep quickfix
534 Xopen
535 if a:cchar == 'c'
536 let title_text = ':helpgrep quickfix'
537 else
538 let title_text = ':lhelpgrep quickfix'
539 endif
540 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200541
542 " Jumping to a help topic should open the help window
543 only
544 Xnext
545 call assert_true(&buftype == 'help')
546 call assert_true(winnr('$') == 2)
547 " Jumping to the next match should reuse the help window
548 Xnext
549 call assert_true(&buftype == 'help')
550 call assert_true(winnr() == 1)
551 call assert_true(winnr('$') == 2)
552 " Jumping to the next match from the quickfix window should reuse the help
553 " window
554 Xopen
555 Xnext
556 call assert_true(&buftype == 'help')
557 call assert_true(winnr() == 1)
558 call assert_true(winnr('$') == 2)
559
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100560 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200561 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100562
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200563 if a:cchar == 'l'
564 " When a help window is present, running :lhelpgrep should reuse the
565 " help window and not the current window
566 new | only
567 call g:Xsetlist([], 'f')
568 help index.txt
569 wincmd w
570 lhelpgrep quickfix
571 call assert_equal(1, winnr())
572 call assert_notequal([], getloclist(1))
573 call assert_equal([], getloclist(2))
574 endif
575
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200576 new | only
577
Bram Moolenaaree85df32017-03-19 14:19:50 +0100578 " Search for non existing help string
579 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar108e7b42018-10-11 17:39:12 +0200580 " Invalid regular expression
581 call assert_fails('Xhelpgrep \@<!', 'E480:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100582endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200583
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100584func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200585 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200586 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200587 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100588endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100589
Bram Moolenaar6920c722016-01-22 22:44:10 +0100590func Test_errortitle()
591 augroup QfBufWinEnter
592 au!
593 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
594 augroup END
595 copen
596 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'')'}]
597 call setqflist(a)
598 call assert_equal(':setqflist()', g:a)
599 augroup QfBufWinEnter
600 au!
601 augroup END
602 augroup! QfBufWinEnter
603endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100604
Bram Moolenaar5584df62016-03-18 21:00:51 +0100605func Test_vimgreptitle()
606 augroup QfBufWinEnter
607 au!
608 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
609 augroup END
610 try
611 vimgrep /pattern/j file
612 catch /E480/
613 endtry
614 copen
615 call assert_equal(': vimgrep /pattern/j file', g:a)
616 augroup QfBufWinEnter
617 au!
618 augroup END
619 augroup! QfBufWinEnter
620endfunc
621
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100622func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200623 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +0100624
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200625 Xgetexpr ['file:1:1:message']
626 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100627 if a:cchar == 'c'
628 call setqflist(l, 'r')
629 else
630 call setloclist(0, l, 'r')
631 endif
632
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200633 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +0100634 if a:cchar == 'c'
635 let title = ':setqflist()'
636 else
637 let title = ':setloclist()'
638 endif
639 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200640 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100641endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100642
643" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100644func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200645 call XqfTitleTests('c')
646 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100647endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100648
649" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100650func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100651 let save_efm = &efm
652 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
653 cgetexpr ['WWWW', 'EEEE', 'CCCC']
654 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
655 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
656 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
657 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
658 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
659 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
660 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
661 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
662 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100663endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100664
665" This will test for problems in quickfix:
666" A. incorrectly copying location lists which caused the location list to show
667" a different name than the file that was actually being displayed.
668" B. not reusing the window for which the location list window is opened but
669" instead creating new windows.
670" C. make sure that the location list window is not reused instead of the
671" window it belongs to.
672"
673" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100674func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100675 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
676 let word = substitute(base, '\v(.*)\..*', '\1', '')
677
678 setl modifiable
679 setl noreadonly
680 setl noswapfile
681 setl bufhidden=delete
682 %del _
683 " For problem 2:
684 " 'buftype' has to be set to reproduce the constant opening of new windows
685 setl buftype=nofile
686
687 call setline(1, word)
688
689 setl nomodified
690 setl nomodifiable
691 setl readonly
692 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100693endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100694
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100695func Test_locationlist()
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100696 enew
697
698 augroup testgroup
699 au!
700 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
701 augroup END
702
703 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
704
705 let qflist = []
706 for word in words
707 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
708 " NOTE: problem 1:
709 " intentionally not setting 'lnum' so that the quickfix entries are not
710 " valid
711 call setloclist(0, qflist, ' ')
712 endfor
713
714 " Test A
715 lrewind
716 enew
717 lopen
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200718 4lnext
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100719 vert split
720 wincmd L
721 lopen
722 wincmd p
723 lnext
724 let fileName = expand("%")
725 wincmd p
726 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
727 let fileName = substitute(fileName, '\\', '/', 'g')
728 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
729 call assert_equal("test://bar.txt", fileName)
730 call assert_equal("test://bar.txt", locationListFileName)
731
732 wincmd n | only
733
734 " Test B:
735 lrewind
736 lopen
737 2
738 exe "normal \<CR>"
739 wincmd p
740 3
741 exe "normal \<CR>"
742 wincmd p
743 4
744 exe "normal \<CR>"
745 call assert_equal(2, winnr('$'))
746 wincmd n | only
747
748 " Test C:
749 lrewind
750 lopen
751 " Let's move the location list window to the top to check whether it (the
752 " first window found) will be reused when we try to open new windows:
753 wincmd K
754 2
755 exe "normal \<CR>"
756 wincmd p
757 3
758 exe "normal \<CR>"
759 wincmd p
760 4
761 exe "normal \<CR>"
762 1wincmd w
763 call assert_equal('quickfix', &buftype)
764 2wincmd w
765 let bufferName = expand("%")
766 let bufferName = substitute(bufferName, '\\', '/', 'g')
767 call assert_equal('test://quux.txt', bufferName)
768
769 wincmd n | only
770
771 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100772endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +0100773
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100774func Test_locationlist_curwin_was_closed()
Bram Moolenaar0899d692016-03-19 13:35:03 +0100775 augroup testgroup
776 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200777 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
Bram Moolenaar0899d692016-03-19 13:35:03 +0100778 augroup END
779
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100780 func! R(n)
Bram Moolenaar0899d692016-03-19 13:35:03 +0100781 quit
782 endfunc
783
784 new
785 let q = []
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200786 call add(q, {'filename': 'test_curwin.txt' })
Bram Moolenaar0899d692016-03-19 13:35:03 +0100787 call setloclist(0, q)
788 call assert_fails('lrewind', 'E924:')
789
790 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100791endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100792
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100793func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200794 call writefile(['loclistfoo'], 'loclistfoo')
795 call writefile(['loclistbar'], 'loclistbar')
796 set switchbuf=usetab
797
798 edit loclistfoo
799 tabedit loclistbar
800 silent lgrep loclistfoo loclist*
801 call assert_equal(1, tabpagenr())
802
803 enew | only | tabonly
804 set switchbuf&vim
805 call delete('loclistfoo')
806 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100807endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200808
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100809" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100810func Test_efm1()
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100811 if !has('unix')
812 " The 'errorformat' setting is different on non-Unix systems.
813 " This test works only on Unix-like systems.
814 return
815 endif
816
817 let l = [
818 \ '"Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.',
819 \ '"Xtestfile", line 6 col 19; this is an error',
820 \ 'gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c',
821 \ 'Xtestfile:9: parse error before `asd''',
822 \ 'make: *** [vim] Error 1',
823 \ 'in file "Xtestfile" linenr 10: there is an error',
824 \ '',
825 \ '2 returned',
826 \ '"Xtestfile", line 11 col 1; this is an error',
827 \ '"Xtestfile", line 12 col 2; this is another error',
828 \ '"Xtestfile", line 14:10; this is an error in column 10',
829 \ '=Xtestfile=, line 15:10; this is another error, but in vcol 10 this time',
830 \ '"Xtestfile", linenr 16: yet another problem',
831 \ 'Error in "Xtestfile" at line 17:',
832 \ 'x should be a dot',
833 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17',
834 \ ' ^',
835 \ 'Error in "Xtestfile" at line 18:',
836 \ 'x should be a dot',
837 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18',
838 \ '.............^',
839 \ 'Error in "Xtestfile" at line 19:',
840 \ 'x should be a dot',
841 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19',
842 \ '--------------^',
843 \ 'Error in "Xtestfile" at line 20:',
844 \ 'x should be a dot',
845 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20',
846 \ ' ^',
847 \ '',
848 \ 'Does anyone know what is the problem and how to correction it?',
849 \ '"Xtestfile", line 21 col 9: What is the title of the quickfix window?',
850 \ '"Xtestfile", line 22 col 9: What is the title of the quickfix window?'
851 \ ]
852
853 call writefile(l, 'Xerrorfile1')
854 call writefile(l[:-2], 'Xerrorfile2')
855
856 let m = [
857 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2',
858 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3',
859 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4',
860 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5',
861 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6',
862 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7',
863 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8',
864 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9',
865 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10',
866 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11',
867 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12',
868 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13',
869 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14',
870 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15',
871 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16',
872 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17',
873 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18',
874 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19',
875 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20',
876 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21',
877 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22'
878 \ ]
879 call writefile(m, 'Xtestfile')
880
881 let save_efm = &efm
882 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
883 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
884
885 exe 'cf Xerrorfile2'
886 clast
887 copen
888 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
889 wincmd p
890
891 exe 'cf Xerrorfile1'
892 call assert_equal([4, 12], [line('.'), col('.')])
893 cn
894 call assert_equal([6, 19], [line('.'), col('.')])
895 cn
896 call assert_equal([9, 2], [line('.'), col('.')])
897 cn
898 call assert_equal([10, 2], [line('.'), col('.')])
899 cn
900 call assert_equal([11, 1], [line('.'), col('.')])
901 cn
902 call assert_equal([12, 2], [line('.'), col('.')])
903 cn
904 call assert_equal([14, 10], [line('.'), col('.')])
905 cn
906 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
907 cn
908 call assert_equal([16, 2], [line('.'), col('.')])
909 cn
910 call assert_equal([17, 6], [line('.'), col('.')])
911 cn
912 call assert_equal([18, 7], [line('.'), col('.')])
913 cn
914 call assert_equal([19, 8], [line('.'), col('.')])
915 cn
916 call assert_equal([20, 9], [line('.'), col('.')])
917 clast
918 cprev
919 cprev
920 wincmd w
921 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
922 wincmd p
923
924 let &efm = save_efm
925 call delete('Xerrorfile1')
926 call delete('Xerrorfile2')
927 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +0100928endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100929
Bram Moolenaarab47c612016-06-14 22:02:26 +0200930" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100931func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200932 call s:setup_commands(a:cchar)
933
Bram Moolenaarab47c612016-06-14 22:02:26 +0200934 let save_efm=&efm
935 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
936
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200937 let lines = ["Entering dir 'dir1/a'",
938 \ 'habits2.txt:1:Nine Healthy Habits',
939 \ "Entering dir 'b'",
940 \ 'habits3.txt:2:0 Hours of television',
941 \ 'habits2.txt:7:5 Small meals',
942 \ "Entering dir 'dir1/c'",
943 \ 'habits4.txt:3:1 Hour of exercise',
944 \ "Leaving dir 'dir1/c'",
945 \ "Leaving dir 'dir1/a'",
946 \ 'habits1.txt:4:2 Liters of water',
947 \ "Entering dir 'dir2'",
948 \ 'habits5.txt:5:3 Cups of hot green tea',
949 \ "Leaving dir 'dir2'"
950 \]
Bram Moolenaarab47c612016-06-14 22:02:26 +0200951
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200952 Xexpr ""
953 for l in lines
954 Xaddexpr l
955 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +0200956
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200957 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +0200958
959 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
960 call assert_equal(1, qf[1].lnum)
961 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
962 call assert_equal(2, qf[3].lnum)
963 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
964 call assert_equal(7, qf[4].lnum)
965 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
966 call assert_equal(3, qf[6].lnum)
967 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
968 call assert_equal(4, qf[9].lnum)
969 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
970 call assert_equal(5, qf[11].lnum)
971
972 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100973endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +0200974
975" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100976func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +0200977 " Create the directory stack and files
978 call mkdir('dir1')
979 call mkdir('dir1/a')
980 call mkdir('dir1/a/b')
981 call mkdir('dir1/c')
982 call mkdir('dir2')
983
984 let lines = ["Nine Healthy Habits",
985 \ "0 Hours of television",
986 \ "1 Hour of exercise",
987 \ "2 Liters of water",
988 \ "3 Cups of hot green tea",
989 \ "4 Short mental breaks",
990 \ "5 Small meals",
991 \ "6 AM wake up time",
992 \ "7 Minutes of laughter",
993 \ "8 Hours of sleep (at least)",
994 \ "9 PM end of the day and off to bed"
995 \ ]
996 call writefile(lines, 'habits1.txt')
997 call writefile(lines, 'dir1/a/habits2.txt')
998 call writefile(lines, 'dir1/a/b/habits3.txt')
999 call writefile(lines, 'dir1/c/habits4.txt')
1000 call writefile(lines, 'dir2/habits5.txt')
1001
1002 call s:dir_stack_tests('c')
1003 call s:dir_stack_tests('l')
1004
1005 call delete('dir1', 'rf')
1006 call delete('dir2', 'rf')
1007 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001008endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001009
Bram Moolenaar9b457942016-10-09 16:10:05 +02001010" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001011func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001012 call s:setup_commands(a:cchar)
1013
1014 let save_efm = &efm
1015
1016 let &efm =
1017 \ '%Eerror %m %l,' .
1018 \ '%-Wignored %m %l,' .
1019 \ '%+Cmore ignored %m %l,' .
1020 \ '%Zignored end'
1021 Xgetexpr ['ignored warning 1', 'more ignored continuation 2', 'ignored end', 'error resync 4']
1022 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
1023 call assert_equal([['resync', 1, 4, 'E']], l)
1024
1025 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001026endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001027
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001028func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +02001029 call Xefm_ignore_continuations('c')
1030 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001031endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001032
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001033" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001034func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001035 call s:setup_commands(a:cchar)
1036
Bram Moolenaar049cba92016-06-26 14:38:04 +02001037 let save_efm = &efm
1038
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001039 set efm=%f:%l:%m,%f:%f:%l:%m
1040 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
1041
1042 set efm=%f:%l:%m,%f:%l:%r:%m
1043 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1044
1045 set efm=%f:%l:%m,%O:%f:%l:%m
1046 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1047
1048 set efm=%f:%l:%m,%f:%l:%*[^a-z
1049 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
1050
1051 set efm=%f:%l:%m,%f:%l:%*c
1052 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
1053
1054 set efm=%f:%l:%m,%L%M%N
1055 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
1056
1057 set efm=%f:%l:%m,%f:%l:%m:%R
1058 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
1059
1060 set efm=
1061 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
1062
1063 set efm=%DEntering\ dir\ abc,%f:%l:%m
1064 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
1065
1066 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001067endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001068
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001069func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001070 call Xinvalid_efm_Tests('c')
1071 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001072endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001073
1074" TODO:
1075" Add tests for the following formats in 'errorformat'
1076" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001077func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001078 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001079
1080 " Test for %s format in efm
1081 set efm=%f:%s
1082 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001083 let l = getqflist()
1084 call assert_equal(l[0].pattern, '^\VLine search text\$')
1085 call assert_equal(l[0].lnum, 0)
1086
Bram Moolenaaree85df32017-03-19 14:19:50 +01001087 let l = split(execute('clist', ''), "\n")
1088 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1089
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001090 " Test for %P, %Q and %t format specifiers
Bram Moolenaar049cba92016-06-26 14:38:04 +02001091 let lines=["[Xtestfile1]",
1092 \ "(1,17) error: ';' missing",
1093 \ "(21,2) warning: variable 'z' not defined",
1094 \ "(67,3) error: end of file found before string ended",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001095 \ "--",
Bram Moolenaar049cba92016-06-26 14:38:04 +02001096 \ "",
1097 \ "[Xtestfile2]",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001098 \ "--",
Bram Moolenaar049cba92016-06-26 14:38:04 +02001099 \ "",
1100 \ "[Xtestfile3]",
1101 \ "NEW compiler v1.1",
1102 \ "(2,2) warning: variable 'x' not defined",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001103 \ "(67,3) warning: 's' already defined",
1104 \ "--"
Bram Moolenaar049cba92016-06-26 14:38:04 +02001105 \]
Bram Moolenaaree85df32017-03-19 14:19:50 +01001106 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001107 " To exercise the push/pop file functionality in quickfix, the test files
1108 " need to be created.
1109 call writefile(['Line1'], 'Xtestfile1')
1110 call writefile(['Line2'], 'Xtestfile2')
1111 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001112 cexpr ""
1113 for l in lines
1114 caddexpr l
1115 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001116 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001117 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001118 call assert_equal(21, l[2].lnum)
1119 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001120 call assert_equal('w', l[2].type)
1121 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001122 call delete('Xtestfile1')
1123 call delete('Xtestfile2')
1124 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001125
1126 " Tests for %E, %C and %Z format specifiers
1127 let lines = ["Error 275",
1128 \ "line 42",
1129 \ "column 3",
1130 \ "' ' expected after '--'"
1131 \]
1132 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1133 cgetexpr lines
1134 let l = getqflist()
1135 call assert_equal(275, l[0].nr)
1136 call assert_equal(42, l[0].lnum)
1137 call assert_equal(3, l[0].col)
1138 call assert_equal('E', l[0].type)
1139 call assert_equal("\n' ' expected after '--'", l[0].text)
1140
1141 " Test for %>
1142 let lines = ["Error in line 147 of foo.c:",
1143 \"unknown variable 'i'"
1144 \]
1145 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1146 cgetexpr lines
1147 let l = getqflist()
1148 call assert_equal(147, l[0].lnum)
1149 call assert_equal('E', l[0].type)
1150 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001151
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001152 " Test for %A, %C and other formats
1153 let lines = [
1154 \"==============================================================",
1155 \"FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)",
1156 \"--------------------------------------------------------------",
1157 \"Traceback (most recent call last):",
1158 \' File "unittests/dbfacadeTest.py", line 89, in testFoo',
1159 \" self.assertEquals(34, dtid)",
1160 \' File "/usr/lib/python2.2/unittest.py", line 286, in',
1161 \" failUnlessEqual",
1162 \" raise self.failureException, \\",
1163 \"AssertionError: 34 != 33",
1164 \"",
1165 \"--------------------------------------------------------------",
1166 \"Ran 27 tests in 0.063s"
1167 \]
1168 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
1169 cgetexpr lines
1170 let l = getqflist()
1171 call assert_equal(8, len(l))
1172 call assert_equal(89, l[4].lnum)
1173 call assert_equal(1, l[4].valid)
1174 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
1175
Bram Moolenaard76ce852018-05-01 15:02:04 +02001176 " Test for %o
1177 set efm=%f(%o):%l\ %m
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001178 cgetexpr ['Xotestfile(Language.PureScript.Types):20 Error']
1179 call writefile(['Line1'], 'Xotestfile')
Bram Moolenaard76ce852018-05-01 15:02:04 +02001180 let l = getqflist()
1181 call assert_equal(1, len(l), string(l))
1182 call assert_equal('Language.PureScript.Types', l[0].module)
1183 copen
1184 call assert_equal('Language.PureScript.Types|20| Error', getline(1))
1185 call feedkeys("\<CR>", 'xn')
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001186 call assert_equal('Xotestfile', expand('%:t'))
Bram Moolenaard76ce852018-05-01 15:02:04 +02001187 cclose
1188 bd
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001189 call delete("Xotestfile")
Bram Moolenaard76ce852018-05-01 15:02:04 +02001190
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001191 " The following sequence of commands used to crash Vim
1192 set efm=%W%m
1193 cgetexpr ['msg1']
1194 let l = getqflist()
1195 call assert_equal(1, len(l), string(l))
1196 call assert_equal('msg1', l[0].text)
1197 set efm=%C%m
1198 lexpr 'msg2'
1199 let l = getloclist(0)
1200 call assert_equal(1, len(l), string(l))
1201 call assert_equal('msg2', l[0].text)
1202 lopen
1203 call setqflist([], 'r')
1204 caddbuf
1205 let l = getqflist()
1206 call assert_equal(1, len(l), string(l))
1207 call assert_equal('|| msg2', l[0].text)
1208
Bram Moolenaar78ddc062018-05-15 21:56:34 +02001209 " When matching error lines, case should be ignored. Test for this.
1210 set noignorecase
1211 let l=getqflist({'lines' : ['Xtest:FOO10:Line 20'], 'efm':'%f:foo%l:%m'})
1212 call assert_equal(10, l.items[0].lnum)
1213 call assert_equal('Line 20', l.items[0].text)
1214 set ignorecase&
1215
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001216 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001217 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001218endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001219
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001220func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001221 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001222 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001223 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001224 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001225 colder
1226 cgetexpr []
1227 endfunc
1228 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001229 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001230 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001231 lolder
1232 lgetexpr []
1233 endfunc
1234 endif
1235
1236 augroup testgroup
1237 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001238 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001239 augroup END
1240
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001241 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001242 let words = [ "a", "b" ]
1243 let qflist = []
1244 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001245 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001246 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001247 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001248 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001249
1250 augroup! testgroup
1251endfunc
1252
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001253func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001254 call XquickfixChangedByAutocmd('c')
1255 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001256endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001257
1258func Test_caddbuffer_to_empty()
1259 helpgr quickfix
1260 call setqflist([], 'r')
1261 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001262 try
1263 cn
1264 catch
1265 " number of matches is unknown
1266 call assert_true(v:exception =~ 'E553:')
1267 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001268 quit!
1269endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001270
1271func Test_cgetexpr_works()
1272 " this must not crash Vim
1273 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001274 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001275endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001276
1277" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001278func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001279 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001280
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001281 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001282 \ {'bufnr': a:bnum, 'lnum': 2}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001283 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001284 call assert_equal(2, len(l))
1285 call assert_equal(2, l[1].lnum)
1286
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001287 Xnext
1288 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1289 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001290 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001291 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001292 call assert_equal(3, line('.'))
1293
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001294 " Appending entries to the list should not change the cursor position
1295 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001296 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001297 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001298 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001299 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1300 call assert_equal(1, line('.'))
1301 close
1302
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001303 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001304 \ {'bufnr': a:bnum, 'lnum': 4},
1305 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001306 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001307 call assert_equal(3, len(l))
1308 call assert_equal(5, l[2].lnum)
1309
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001310 call g:Xsetlist([])
1311 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001312 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001313
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001314 " Tests for setting the 'valid' flag
1315 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1316 Xwindow
1317 call assert_equal(1, winnr('$'))
1318 let l = g:Xgetlist()
1319 call g:Xsetlist(l)
1320 call assert_equal(0, g:Xgetlist()[0].valid)
Bram Moolenaar9752c722018-12-22 16:49:34 +01001321 " Adding a non-valid entry should not mark the list as having valid entries
1322 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':5, 'valid':0}], 'a')
1323 Xwindow
1324 call assert_equal(1, winnr('$'))
1325
1326 " :cnext/:cprev should still work even with invalid entries in the list
1327 let l = [{'bufnr' : a:bnum, 'lnum' : 1, 'text' : '1', 'valid' : 0},
1328 \ {'bufnr' : a:bnum, 'lnum' : 2, 'text' : '2', 'valid' : 0}]
1329 call g:Xsetlist(l)
1330 Xnext
1331 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1332 Xprev
1333 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1334 " :cnext/:cprev should still work after appending invalid entries to an
1335 " empty list
1336 call g:Xsetlist([])
1337 call g:Xsetlist(l, 'a')
1338 Xnext
1339 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1340 Xprev
1341 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1342
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001343 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1344 Xwindow
1345 call assert_equal(2, winnr('$'))
1346 Xclose
1347 let save_efm = &efm
1348 set efm=%m
1349 Xgetexpr 'TestMessage'
1350 let l = g:Xgetlist()
1351 call g:Xsetlist(l)
1352 call assert_equal(1, g:Xgetlist()[0].valid)
1353 let &efm = save_efm
1354
Bram Moolenaaree85df32017-03-19 14:19:50 +01001355 " Error cases:
1356 " Refer to a non-existing buffer and pass a non-dictionary type
1357 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1358 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1359 call g:Xsetlist([[1, 2,3]])
1360 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001361endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001362
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001363func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001364 new Xtestfile | only
1365 let bnum = bufnr('%')
1366 call setline(1, range(1,5))
1367
1368 call SetXlistTests('c', bnum)
1369 call SetXlistTests('l', bnum)
1370
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001371 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001372 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001373endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001374
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001375func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001376 call s:setup_commands(a:cchar)
1377
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001378 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001379 let @/ = 'Test_'
1380 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001381 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001382 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001383 Xvimgrep empty test_quickfix.vim
1384 call assert_true(len(g:Xgetlist()) > 0)
1385 Xvimgrep matches test_quickfix.vim
1386 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001387 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001388 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001389 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001390 call g:Xsetlist([], 'r')
1391 call assert_true(len(g:Xgetlist()) == 0)
1392 Xolder
1393 call assert_equal(testlen, len(g:Xgetlist()))
1394 Xnewer
1395 Xnewer
1396 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001397endfunc
1398
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001399func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001400 call Xlist_empty_middle('c')
1401 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001402endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001403
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001404func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001405 call s:setup_commands(a:cchar)
1406
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001407 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001408 Xvimgrep one test_quickfix.vim
1409 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001410 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001411 Xvimgrep two test_quickfix.vim
1412 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001413 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001414 Xvimgrep three test_quickfix.vim
1415 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001416 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001417 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001418 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001419 call g:Xsetlist([], 'r')
1420 call assert_true(len(g:Xgetlist()) == 0)
1421 Xnewer
1422 call assert_equal(twolen, len(g:Xgetlist()))
1423 Xnewer
1424 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001425endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001426
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001427func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001428 call Xlist_empty_older('c')
1429 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001430endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001431
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001432func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001433 call s:setup_commands(a:cchar)
1434
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001435 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1436 \ {'filename': 'fnameB', 'text': 'B'}]
1437 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1438 \ {'filename': 'fnameD', 'text': 'D'},
1439 \ {'filename': 'fnameE', 'text': 'E'}]
1440
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001441 " {action} is unspecified. Same as specifying ' '.
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001442 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001443 silent! Xnewer 99
1444 call g:Xsetlist(list1)
1445 call g:Xsetlist(list2)
1446 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001447 call assert_equal(3, len(li))
1448 call assert_equal('C', li[0]['text'])
1449 call assert_equal('D', li[1]['text'])
1450 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001451 silent! Xolder
1452 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001453 call assert_equal(2, len(li))
1454 call assert_equal('A', li[0]['text'])
1455 call assert_equal('B', li[1]['text'])
1456
1457 " {action} is specified ' '.
1458 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001459 silent! Xnewer 99
1460 call g:Xsetlist(list1)
1461 call g:Xsetlist(list2, ' ')
1462 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001463 call assert_equal(3, len(li))
1464 call assert_equal('C', li[0]['text'])
1465 call assert_equal('D', li[1]['text'])
1466 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001467 silent! Xolder
1468 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001469 call assert_equal(2, len(li))
1470 call assert_equal('A', li[0]['text'])
1471 call assert_equal('B', li[1]['text'])
1472
1473 " {action} is specified 'a'.
1474 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001475 silent! Xnewer 99
1476 call g:Xsetlist(list1)
1477 call g:Xsetlist(list2, 'a')
1478 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001479 call assert_equal(5, len(li))
1480 call assert_equal('A', li[0]['text'])
1481 call assert_equal('B', li[1]['text'])
1482 call assert_equal('C', li[2]['text'])
1483 call assert_equal('D', li[3]['text'])
1484 call assert_equal('E', li[4]['text'])
1485
1486 " {action} is specified 'r'.
1487 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001488 silent! Xnewer 99
1489 call g:Xsetlist(list1)
1490 call g:Xsetlist(list2, 'r')
1491 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001492 call assert_equal(3, len(li))
1493 call assert_equal('C', li[0]['text'])
1494 call assert_equal('D', li[1]['text'])
1495 call assert_equal('E', li[2]['text'])
1496
1497 " Test for wrong value.
1498 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001499 call assert_fails("call g:Xsetlist(0)", 'E714:')
1500 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
1501 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
1502 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
1503 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001504endfunc
1505
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001506func Test_setqflist_invalid_nr()
1507 " The following command used to crash Vim
1508 call setqflist([], ' ', {'nr' : $XXX_DOES_NOT_EXIST})
1509endfunc
1510
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001511func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001512 call XquickfixSetListWithAct('c')
1513 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001514endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001515
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001516func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001517 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001518
Bram Moolenaar049cba92016-06-26 14:38:04 +02001519 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001520 call assert_equal(1, l[0].lnum)
1521 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001522 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001523 call assert_equal(2, l[1].lnum)
1524 call assert_equal(1, l[1].col)
1525 call assert_equal(4070, len(l[1].text))
1526 call assert_equal(3, l[2].lnum)
1527 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001528 call assert_equal(4070, len(l[2].text))
1529 call assert_equal(4, l[3].lnum)
1530 call assert_equal(1, l[3].col)
1531 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001532
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001533 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001534endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001535
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001536func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001537 call s:setup_commands(a:cchar)
1538
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001539 let testfile = 'samples/quickfix.txt'
1540
1541 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001542 exe 'Xgetfile' testfile
1543 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001544
1545 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001546 Xexpr readfile(testfile)
1547 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001548
1549 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001550 Xexpr join(readfile(testfile), "\n")
1551 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001552
1553 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001554 exe 'edit' testfile
1555 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02001556 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001557endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001558
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001559func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001560 call s:long_lines_tests('c')
1561 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001562endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001563
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001564func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001565 let l = []
1566 for i in range(1, 20)
1567 call add(l, 'Line' . i)
1568 endfor
1569 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001570endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001571
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001572func Test_switchbuf()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001573 call s:create_test_file('Xqftestfile1')
1574 call s:create_test_file('Xqftestfile2')
1575 call s:create_test_file('Xqftestfile3')
1576
1577 new | only
1578 edit Xqftestfile1
1579 let file1_winid = win_getid()
1580 new Xqftestfile2
1581 let file2_winid = win_getid()
1582 cgetexpr ['Xqftestfile1:5:Line5',
1583 \ 'Xqftestfile1:6:Line6',
1584 \ 'Xqftestfile2:10:Line10',
1585 \ 'Xqftestfile2:11:Line11',
1586 \ 'Xqftestfile3:15:Line15',
1587 \ 'Xqftestfile3:16:Line16']
1588
1589 new
1590 let winid = win_getid()
1591 cfirst | cnext
1592 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001593 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001594 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001595 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001596 call assert_equal(winid, win_getid())
1597 enew
1598
1599 set switchbuf=useopen
1600 cfirst | cnext
1601 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001602 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001603 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001604 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001605 call assert_equal(file2_winid, win_getid())
1606
1607 enew | only
1608 set switchbuf=usetab
1609 tabedit Xqftestfile1
1610 tabedit Xqftestfile2
Bram Moolenaar74240d32017-12-10 15:26:15 +01001611 tabedit Xqftestfile3
Bram Moolenaar049cba92016-06-26 14:38:04 +02001612 tabfirst
1613 cfirst | cnext
1614 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001615 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001616 call assert_equal(3, tabpagenr())
Bram Moolenaar74240d32017-12-10 15:26:15 +01001617 6cnext
1618 call assert_equal(4, tabpagenr())
1619 2cpfile
1620 call assert_equal(2, tabpagenr())
1621 2cnfile
1622 call assert_equal(4, tabpagenr())
Bram Moolenaar049cba92016-06-26 14:38:04 +02001623 tabfirst | tabonly | enew
1624
1625 set switchbuf=split
1626 cfirst | cnext
1627 call assert_equal(1, winnr('$'))
1628 cnext | cnext
1629 call assert_equal(2, winnr('$'))
1630 cnext | cnext
1631 call assert_equal(3, winnr('$'))
1632 enew | only
1633
1634 set switchbuf=newtab
1635 cfirst | cnext
1636 call assert_equal(1, tabpagenr('$'))
1637 cnext | cnext
1638 call assert_equal(2, tabpagenr('$'))
1639 cnext | cnext
1640 call assert_equal(3, tabpagenr('$'))
1641 tabfirst | enew | tabonly | only
1642
1643 set switchbuf=
1644 edit Xqftestfile1
1645 let file1_winid = win_getid()
1646 new Xqftestfile2
1647 let file2_winid = win_getid()
1648 copen
1649 exe "normal 1G\<CR>"
1650 call assert_equal(file1_winid, win_getid())
1651 copen
1652 exe "normal 3G\<CR>"
1653 call assert_equal(file2_winid, win_getid())
1654 copen | only
1655 exe "normal 5G\<CR>"
1656 call assert_equal(2, winnr('$'))
1657 call assert_equal(1, bufwinnr('Xqftestfile3'))
1658
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001659 " If only quickfix window is open in the current tabpage, jumping to an
1660 " entry with 'switchubf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02001661 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001662 set switchbuf=usetab
1663 tabedit Xqftestfile1
1664 tabedit Xqftestfile2
1665 tabedit Xqftestfile3
1666 tabfirst
1667 copen | only
1668 clast
1669 call assert_equal(4, tabpagenr())
1670 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001671
1672 call delete('Xqftestfile1')
1673 call delete('Xqftestfile2')
1674 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001675 set switchbuf&vim
1676
1677 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001678endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001679
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001680func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001681 call s:setup_commands(a:cchar)
1682
1683 enew | only
1684
Bram Moolenaarc1542742016-07-20 21:44:37 +02001685 let fname = 'Xqftestfile' . a:cchar
1686 call s:create_test_file(fname)
1687 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02001688
Bram Moolenaarc1542742016-07-20 21:44:37 +02001689 Xgetexpr [fname . ':5:Line5',
1690 \ fname . ':10:Line10',
1691 \ fname . ':15:Line15',
1692 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02001693
1694 6,14delete
1695 call append(6, ['Buffer', 'Window'])
1696
1697 let l = g:Xgetlist()
1698
1699 call assert_equal(5, l[0].lnum)
1700 call assert_equal(6, l[2].lnum)
1701 call assert_equal(13, l[3].lnum)
1702
1703 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02001704 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001705endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001706
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001707func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02001708 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001709 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02001710 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001711 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001712endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001713
1714" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001715func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001716 call s:setup_commands(a:cchar)
1717
1718 " The following lines are used for the grep test. Don't remove.
1719 " Grep_Test_Text: Match 1
1720 " Grep_Test_Text: Match 2
1721 " GrepAdd_Test_Text: Match 1
1722 " GrepAdd_Test_Text: Match 2
1723 enew! | only
1724 set makeef&vim
1725 silent Xgrep Grep_Test_Text: test_quickfix.vim
1726 call assert_true(len(g:Xgetlist()) == 3)
1727 Xopen
1728 call assert_true(w:quickfix_title =~ '^:grep')
1729 Xclose
1730 enew
1731 set makeef=Temp_File_##
1732 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
1733 call assert_true(len(g:Xgetlist()) == 6)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001734endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001735
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001736func Test_grep()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001737 if !has('unix')
1738 " The grepprg may not be set on non-Unix systems
1739 return
1740 endif
1741
1742 call s:test_xgrep('c')
1743 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001744endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001745
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001746func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001747 " Use one 'errorformat' for two windows. Add an expression to each of them,
1748 " make sure they each keep their own state.
1749 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1750 call mkdir('Xone/a', 'p')
1751 call mkdir('Xtwo/a', 'p')
1752 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
1753 call writefile(lines, 'Xone/a/one.txt')
1754 call writefile(lines, 'Xtwo/a/two.txt')
1755
1756 new one
1757 let one_id = win_getid()
1758 lexpr ""
1759 new two
1760 let two_id = win_getid()
1761 lexpr ""
1762
1763 laddexpr "Entering dir 'Xtwo/a'"
1764 call win_gotoid(one_id)
1765 laddexpr "Entering dir 'Xone/a'"
1766 call win_gotoid(two_id)
1767 laddexpr 'two.txt:5:two two two'
1768 call win_gotoid(one_id)
1769 laddexpr 'one.txt:3:one one one'
1770
1771 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001772 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
1773 call assert_equal(3, loc_one[1].lnum)
1774
1775 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001776 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
1777 call assert_equal(5, loc_two[1].lnum)
1778
1779 call win_gotoid(one_id)
1780 bwipe!
1781 call win_gotoid(two_id)
1782 bwipe!
1783 call delete('Xone', 'rf')
1784 call delete('Xtwo', 'rf')
1785endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02001786
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001787func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02001788 call s:setup_commands(a:cchar)
1789
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001790 " Calling lbottom without any errors should fail
1791 if a:cchar == 'l'
1792 call assert_fails('lbottom', 'E776:')
1793 endif
1794
Bram Moolenaar875feea2017-06-11 16:07:51 +02001795 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02001796 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02001797 let wid = win_getid()
1798 call assert_equal(1, line('.'))
1799 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02001800 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02001801 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02001802 call win_gotoid(wid)
1803 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02001804 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02001805endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02001806
1807" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001808func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02001809 call XbottomTests('c')
1810 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001811endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001812
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001813func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001814 call s:setup_commands(a:cchar)
1815
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001816 " clear all lists after the first one, then replace the first one.
1817 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001818 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001819 let entry = {'filename': 'foo', 'lnum': 42}
1820 call g:Xsetlist([entry], 'r')
1821 call g:Xsetlist([entry, entry])
1822 call g:Xsetlist([entry, entry, entry])
1823 let res = split(execute(a:cchar . 'hist'), "\n")
1824 call assert_equal(3, len(res))
1825 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
1826 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
1827 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
1828 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02001829
1830 call g:Xsetlist([], 'f')
1831 let l = split(execute(a:cchar . 'hist'), "\n")
1832 call assert_equal('No entries', l[0])
Bram Moolenaar5b69c222019-01-11 14:50:06 +01001833
1834 " An empty list should still show the stack history
1835 call g:Xsetlist([])
1836 let res = split(execute(a:cchar . 'hist'), "\n")
1837 call assert_equal('> error list 1 of 1; 0 ' . common, res[0])
1838
1839 call g:Xsetlist([], 'f')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001840endfunc
1841
1842func Test_history()
1843 call HistoryTest('c')
1844 call HistoryTest('l')
1845endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02001846
1847func Test_duplicate_buf()
1848 " make sure we can get the highest buffer number
1849 edit DoesNotExist
1850 edit DoesNotExist2
1851 let last_buffer = bufnr("$")
1852
1853 " make sure only one buffer is created
1854 call writefile(['this one', 'that one'], 'Xgrepthis')
1855 vimgrep one Xgrepthis
1856 vimgrep one Xgrepthis
1857 call assert_equal(last_buffer + 1, bufnr("$"))
1858
1859 call delete('Xgrepthis')
1860endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02001861
1862" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001863func Xproperty_tests(cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001864 call s:setup_commands(a:cchar)
1865
1866 " Error cases
1867 call assert_fails('call g:Xgetlist(99)', 'E715:')
1868 call assert_fails('call g:Xsetlist(99)', 'E714:')
1869 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
1870
1871 " Set and get the title
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001872 call g:Xsetlist([])
Bram Moolenaard823fa92016-08-12 16:29:27 +02001873 Xopen
1874 wincmd p
1875 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001876 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
1877 call assert_equal(0, s)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001878 let d = g:Xgetlist({"title":1})
1879 call assert_equal('Sample', d.title)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02001880 " Try setting title to a non-string value
1881 call assert_equal(-1, g:Xsetlist([], 'a', {'title' : ['Test']}))
1882 call assert_equal('Sample', g:Xgetlist({"title":1}).title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001883
1884 Xopen
1885 call assert_equal('Sample', w:quickfix_title)
1886 Xclose
1887
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02001888 " Tests for action argument
1889 silent! Xolder 999
1890 let qfnr = g:Xgetlist({'all':1}).nr
1891 call g:Xsetlist([], 'r', {'title' : 'N1'})
1892 call assert_equal('N1', g:Xgetlist({'all':1}).title)
1893 call g:Xsetlist([], ' ', {'title' : 'N2'})
1894 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
Bram Moolenaar890680c2016-09-27 21:28:56 +02001895
1896 let res = g:Xgetlist({'nr': 0})
1897 call assert_equal(qfnr + 1, res.nr)
1898 call assert_equal(['nr'], keys(res))
1899
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02001900 call g:Xsetlist([], ' ', {'title' : 'N3'})
1901 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
1902
Bram Moolenaaree85df32017-03-19 14:19:50 +01001903 " Changing the title of an earlier quickfix list
Bram Moolenaar55b69262017-08-13 13:42:01 +02001904 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
Bram Moolenaaree85df32017-03-19 14:19:50 +01001905 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
1906
1907 " Changing the title of an invalid quickfix list
1908 call assert_equal(-1, g:Xsetlist([], ' ',
1909 \ {'title' : 'SomeTitle', 'nr' : 99}))
1910 call assert_equal(-1, g:Xsetlist([], ' ',
1911 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
1912
1913 if a:cchar == 'c'
1914 copen
1915 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
1916 cclose
1917 endif
1918
Bram Moolenaard823fa92016-08-12 16:29:27 +02001919 " Invalid arguments
1920 call assert_fails('call g:Xgetlist([])', 'E715')
1921 call assert_fails('call g:Xsetlist([], "a", [])', 'E715')
1922 let s = g:Xsetlist([], 'a', {'abc':1})
1923 call assert_equal(-1, s)
1924
1925 call assert_equal({}, g:Xgetlist({'abc':1}))
Bram Moolenaara6d48492017-12-12 22:45:31 +01001926 call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
1927 call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001928
1929 if a:cchar == 'l'
Bram Moolenaar890680c2016-09-27 21:28:56 +02001930 call assert_equal({}, getloclist(99, {'title': 1}))
Bram Moolenaard823fa92016-08-12 16:29:27 +02001931 endif
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001932
1933 " Context related tests
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001934 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
1935 call assert_equal(0, s)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001936 call test_garbagecollect_now()
1937 let d = g:Xgetlist({'context':1})
1938 call assert_equal([1,2,3], d.context)
1939 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
1940 let d = g:Xgetlist({'context':1})
1941 call assert_equal({'color':'green'}, d.context)
1942 call g:Xsetlist([], 'a', {'context':"Context info"})
1943 let d = g:Xgetlist({'context':1})
1944 call assert_equal("Context info", d.context)
1945 call g:Xsetlist([], 'a', {'context':246})
1946 let d = g:Xgetlist({'context':1})
1947 call assert_equal(246, d.context)
1948 if a:cchar == 'l'
1949 " Test for copying context across two different location lists
1950 new | only
1951 let w1_id = win_getid()
1952 let l = [1]
1953 call setloclist(0, [], 'a', {'context':l})
1954 new
1955 let w2_id = win_getid()
1956 call add(l, 2)
1957 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
1958 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
1959 unlet! l
1960 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
1961 only
1962 call setloclist(0, [], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01001963 call assert_equal('', getloclist(0, {'context':1}).context)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001964 endif
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001965
1966 " Test for changing the context of previous quickfix lists
1967 call g:Xsetlist([], 'f')
1968 Xexpr "One"
1969 Xexpr "Two"
1970 Xexpr "Three"
Bram Moolenaar55b69262017-08-13 13:42:01 +02001971 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
1972 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001973 " Also, check for setting the context using quickfix list number zero.
Bram Moolenaar55b69262017-08-13 13:42:01 +02001974 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001975 call test_garbagecollect_now()
1976 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
1977 call assert_equal([1], l.context)
1978 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
1979 call assert_equal([2], l.context)
1980 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
1981 call assert_equal([3], l.context)
1982
1983 " Test for changing the context through reference and for garbage
1984 " collection of quickfix context
1985 let l = ["red"]
1986 call g:Xsetlist([], ' ', {'context' : l})
1987 call add(l, "blue")
1988 let x = g:Xgetlist({'context' : 1})
1989 call add(x.context, "green")
1990 call assert_equal(["red", "blue", "green"], l)
1991 call assert_equal(["red", "blue", "green"], x.context)
1992 unlet l
1993 call test_garbagecollect_now()
1994 let m = g:Xgetlist({'context' : 1})
1995 call assert_equal(["red", "blue", "green"], m.context)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02001996
1997 " Test for setting/getting items
1998 Xexpr ""
1999 let qfprev = g:Xgetlist({'nr':0})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002000 let s = g:Xsetlist([], ' ', {'title':'Green',
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002001 \ 'items' : [{'filename':'F1', 'lnum':10}]})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002002 call assert_equal(0, s)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002003 let qfcur = g:Xgetlist({'nr':0})
2004 call assert_true(qfcur.nr == qfprev.nr + 1)
2005 let l = g:Xgetlist({'items':1})
2006 call assert_equal('F1', bufname(l.items[0].bufnr))
2007 call assert_equal(10, l.items[0].lnum)
2008 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
2009 \ {'filename':'F2', 'lnum':30}]})
2010 let l = g:Xgetlist({'items':1})
2011 call assert_equal('F2', bufname(l.items[2].bufnr))
2012 call assert_equal(30, l.items[2].lnum)
2013 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
2014 let l = g:Xgetlist({'items':1})
2015 call assert_equal('F3', bufname(l.items[0].bufnr))
2016 call assert_equal(40, l.items[0].lnum)
2017 call g:Xsetlist([], 'r', {'items' : []})
2018 let l = g:Xgetlist({'items':1})
2019 call assert_equal(0, len(l.items))
2020
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002021 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
2022 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2023 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2024 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
2025
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02002026 " Test for getting id of window associated with a location list window
2027 if a:cchar == 'l'
2028 only
2029 call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid)
2030 let wid = win_getid()
2031 Xopen
2032 call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid)
2033 wincmd w
2034 call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid)
2035 only
2036 endif
2037
Bram Moolenaarae338332017-08-11 20:25:26 +02002038 " The following used to crash Vim with address sanitizer
2039 call g:Xsetlist([], 'f')
2040 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
2041 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
2042
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02002043 " Try setting the items using a string
2044 call assert_equal(-1, g:Xsetlist([], ' ', {'items' : 'Test'}))
2045
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002046 " Save and restore the quickfix stack
2047 call g:Xsetlist([], 'f')
2048 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2049 Xexpr "File1:10:Line1"
2050 Xexpr "File2:20:Line2"
2051 Xexpr "File3:30:Line3"
2052 let last_qf = g:Xgetlist({'nr':'$'}).nr
2053 call assert_equal(3, last_qf)
2054 let qstack = []
2055 for i in range(1, last_qf)
2056 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
2057 endfor
2058 call g:Xsetlist([], 'f')
2059 for i in range(len(qstack))
2060 call g:Xsetlist([], ' ', qstack[i])
2061 endfor
2062 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2063 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
2064 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
2065 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
2066 call g:Xsetlist([], 'f')
2067
2068 " Swap two quickfix lists
2069 Xexpr "File1:10:Line10"
2070 Xexpr "File2:20:Line20"
2071 Xexpr "File3:30:Line30"
2072 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
2073 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
2074 let l1=g:Xgetlist({'nr':1,'all':1})
2075 let l2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002076 let save_id = l1.id
2077 let l1.id=l2.id
2078 let l2.id=save_id
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002079 call g:Xsetlist([], 'r', l1)
2080 call g:Xsetlist([], 'r', l2)
2081 let newl1=g:Xgetlist({'nr':1,'all':1})
2082 let newl2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002083 call assert_equal('Fruits', newl1.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002084 call assert_equal(['Fruits'], newl1.context)
2085 call assert_equal('Line20', newl1.items[0].text)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002086 call assert_equal('Colors', newl2.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002087 call assert_equal(['Colors'], newl2.context)
2088 call assert_equal('Line10', newl2.items[0].text)
2089 call g:Xsetlist([], 'f')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002090endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002091
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002092func Test_qf_property()
Bram Moolenaard823fa92016-08-12 16:29:27 +02002093 call Xproperty_tests('c')
2094 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002095endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002096
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002097" Test for setting the current index in the location/quickfix list
2098func Xtest_setqfidx(cchar)
2099 call s:setup_commands(a:cchar)
2100
2101 Xgetexpr "F1:10:1:Line1\nF2:20:2:Line2\nF3:30:3:Line3"
2102 Xgetexpr "F4:10:1:Line1\nF5:20:2:Line2\nF6:30:3:Line3"
2103 Xgetexpr "F7:10:1:Line1\nF8:20:2:Line2\nF9:30:3:Line3"
2104
2105 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 2})
2106 call g:Xsetlist([], 'a', {'nr' : 2, 'idx' : 2})
2107 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 3})
2108 Xolder 2
2109 Xopen
2110 call assert_equal(3, line('.'))
2111 Xnewer
2112 call assert_equal(2, line('.'))
2113 Xnewer
2114 call assert_equal(2, line('.'))
2115 " Update the current index with the quickfix window open
2116 wincmd w
2117 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 3})
2118 Xopen
2119 call assert_equal(3, line('.'))
2120 Xclose
2121
2122 " Set the current index to the last entry
2123 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : '$'})
2124 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2125 " A large value should set the index to the last index
2126 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 1})
2127 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 999})
2128 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2129 " Invalid index values
2130 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : -1})
2131 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2132 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 0})
2133 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2134 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 'xx'})
2135 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2136 call assert_fails("call g:Xsetlist([], 'a', {'nr':1, 'idx':[]})", 'E745:')
2137
2138 call g:Xsetlist([], 'f')
2139 new | only
2140endfunc
2141
2142func Test_setqfidx()
2143 call Xtest_setqfidx('c')
2144 call Xtest_setqfidx('l')
2145endfunc
2146
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002147" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002148func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002149 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002150endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002151
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002152func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002153 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
2154 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
2155
2156 let g:acmds = []
2157 cexpr "F1:10:Line 10"
2158 caddexpr "F1:20:Line 20"
2159 cgetexpr "F1:30:Line 30"
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002160 cexpr ""
2161 caddexpr ""
2162 cgetexpr ""
2163 silent! cexpr non_existing_func()
2164 silent! caddexpr non_existing_func()
2165 silent! cgetexpr non_existing_func()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002166 let l = ['precexpr',
2167 \ 'postcexpr',
2168 \ 'precaddexpr',
2169 \ 'postcaddexpr',
2170 \ 'precgetexpr',
2171 \ 'postcgetexpr',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002172 \ 'precexpr',
2173 \ 'postcexpr',
2174 \ 'precaddexpr',
2175 \ 'postcaddexpr',
2176 \ 'precgetexpr',
2177 \ 'postcgetexpr',
2178 \ 'precexpr',
2179 \ 'precaddexpr',
2180 \ 'precgetexpr']
2181 call assert_equal(l, g:acmds)
2182
2183 let g:acmds = []
2184 enew! | call append(0, "F2:10:Line 10")
2185 cbuffer!
2186 enew! | call append(0, "F2:20:Line 20")
2187 cgetbuffer
2188 enew! | call append(0, "F2:30:Line 30")
2189 caddbuffer
2190 new
2191 let bnum = bufnr('%')
2192 bunload
2193 exe 'silent! cbuffer! ' . bnum
2194 exe 'silent! cgetbuffer ' . bnum
2195 exe 'silent! caddbuffer ' . bnum
2196 enew!
2197 let l = ['precbuffer',
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002198 \ 'postcbuffer',
2199 \ 'precgetbuffer',
2200 \ 'postcgetbuffer',
2201 \ 'precaddbuffer',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002202 \ 'postcaddbuffer',
2203 \ 'precbuffer',
2204 \ 'precgetbuffer',
2205 \ 'precaddbuffer']
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002206 call assert_equal(l, g:acmds)
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002207
2208 call writefile(['Xtest:1:Line1'], 'Xtest')
2209 call writefile([], 'Xempty')
2210 let g:acmds = []
2211 cfile Xtest
2212 caddfile Xtest
2213 cgetfile Xtest
2214 cfile Xempty
2215 caddfile Xempty
2216 cgetfile Xempty
2217 silent! cfile do_not_exist
2218 silent! caddfile do_not_exist
2219 silent! cgetfile do_not_exist
2220 let l = ['precfile',
2221 \ 'postcfile',
2222 \ 'precaddfile',
2223 \ 'postcaddfile',
2224 \ 'precgetfile',
2225 \ 'postcgetfile',
2226 \ 'precfile',
2227 \ 'postcfile',
2228 \ 'precaddfile',
2229 \ 'postcaddfile',
2230 \ 'precgetfile',
2231 \ 'postcgetfile',
2232 \ 'precfile',
2233 \ 'postcfile',
2234 \ 'precaddfile',
2235 \ 'postcaddfile',
2236 \ 'precgetfile',
2237 \ 'postcgetfile']
2238 call assert_equal(l, g:acmds)
2239
2240 let g:acmds = []
2241 helpgrep quickfix
2242 silent! helpgrep non_existing_help_topic
2243 vimgrep test Xtest
2244 vimgrepadd test Xtest
2245 silent! vimgrep non_existing_test Xtest
2246 silent! vimgrepadd non_existing_test Xtest
2247 set makeprg=
2248 silent! make
2249 set makeprg&
2250 let l = ['prehelpgrep',
2251 \ 'posthelpgrep',
2252 \ 'prehelpgrep',
2253 \ 'posthelpgrep',
2254 \ 'previmgrep',
2255 \ 'postvimgrep',
2256 \ 'previmgrepadd',
2257 \ 'postvimgrepadd',
2258 \ 'previmgrep',
2259 \ 'postvimgrep',
2260 \ 'previmgrepadd',
2261 \ 'postvimgrepadd',
2262 \ 'premake',
2263 \ 'postmake']
2264 call assert_equal(l, g:acmds)
2265
2266 if has('unix')
2267 " Run this test only on Unix-like systems. The grepprg may not be set on
2268 " non-Unix systems.
2269 " The following lines are used for the grep test. Don't remove.
2270 " Grep_Autocmd_Text: Match 1
2271 " GrepAdd_Autocmd_Text: Match 2
2272 let g:acmds = []
2273 silent grep Grep_Autocmd_Text test_quickfix.vim
2274 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2275 silent grep abc123def Xtest
2276 silent grepadd abc123def Xtest
2277 let l = ['pregrep',
2278 \ 'postgrep',
2279 \ 'pregrepadd',
2280 \ 'postgrepadd',
2281 \ 'pregrep',
2282 \ 'postgrep',
2283 \ 'pregrepadd',
2284 \ 'postgrepadd']
2285 call assert_equal(l, g:acmds)
2286 endif
2287
2288 call delete('Xtest')
2289 call delete('Xempty')
Bram Moolenaarb254af32017-12-18 19:48:58 +01002290 au! QuickFixCmdPre
2291 au! QuickFixCmdPost
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002292endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01002293
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002294func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01002295 set efm=%m
2296 lgetexpr '?'
2297
2298 try
2299 call DoesNotExit()
2300 catch
2301 lgetexpr '1'
2302 finally
2303 lgetexpr '1'
2304 endtry
2305
2306 call assert_equal('1', getloclist(0)[0].text)
2307
2308 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002309endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002310
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002311func Test_caddbuffer_wrong()
2312 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002313 let save_efm = &efm
2314 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
2315 cgetexpr ['WWWW', 'EEEE', 'CCCC']
2316 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002317 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002318 bwipe!
2319endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002320
2321func Test_caddexpr_wrong()
2322 " This used to cause a memory access in freed memory.
2323 cbuffer
2324 cbuffer
2325 copen
2326 let save_efm = &efm
2327 set efm=%
2328 call assert_fails('caddexpr ""', 'E376:')
2329 let &efm = save_efm
2330endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01002331
2332func Test_dirstack_cleanup()
2333 " This used to cause a memory access in freed memory.
2334 let save_efm = &efm
2335 lexpr '0'
2336 lopen
2337 fun X(c)
2338 let save_efm=&efm
2339 set efm=%D%f
2340 if a:c == 'c'
2341 caddexpr '::'
2342 else
2343 laddexpr ':0:0'
2344 endif
2345 let &efm=save_efm
2346 endfun
2347 call X('c')
2348 call X('l')
2349 call setqflist([], 'r')
2350 caddbuffer
2351 let &efm = save_efm
2352endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002353
2354" Tests for jumping to entries from the location list window and quickfix
2355" window
2356func Test_cwindow_jump()
2357 set efm=%f%%%l%%%m
2358 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2359 lopen | only
2360 lfirst
2361 call assert_true(winnr('$') == 2)
2362 call assert_true(winnr() == 1)
2363 " Location list for the new window should be set
2364 call assert_true(getloclist(0)[2].text == 'Line 30')
2365
2366 " Open a scratch buffer
2367 " Open a new window and create a location list
2368 " Open the location list window and close the other window
2369 " Jump to an entry.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002370 " Should create a new window and jump to the entry. The scratch buffer
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002371 " should not be used.
2372 enew | only
2373 set buftype=nofile
2374 below new
2375 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2376 lopen
2377 2wincmd c
2378 lnext
2379 call assert_true(winnr('$') == 3)
2380 call assert_true(winnr() == 2)
2381
2382 " Open two windows with two different location lists
2383 " Open the location list window and close the previous window
2384 " Jump to an entry in the location list window
2385 " Should open the file in the first window and not set the location list.
2386 enew | only
2387 lgetexpr ["F1%5%Line 5"]
2388 below new
2389 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2390 lopen
2391 2wincmd c
2392 lnext
2393 call assert_true(winnr() == 1)
2394 call assert_true(getloclist(0)[0].text == 'Line 5')
2395
2396 enew | only
2397 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2398 copen
2399 cnext
2400 call assert_true(winnr('$') == 2)
2401 call assert_true(winnr() == 1)
2402
2403 enew | only
2404 set efm&vim
2405endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01002406
2407func XvimgrepTests(cchar)
2408 call s:setup_commands(a:cchar)
2409
2410 call writefile(['Editor:VIM vim',
2411 \ 'Editor:Emacs EmAcS',
2412 \ 'Editor:Notepad NOTEPAD'], 'Xtestfile1')
2413 call writefile(['Linux', 'MacOS', 'MS-Windows'], 'Xtestfile2')
2414
2415 " Error cases
2416 call assert_fails('Xvimgrep /abc *', 'E682:')
2417
2418 let @/=''
2419 call assert_fails('Xvimgrep // *', 'E35:')
2420
2421 call assert_fails('Xvimgrep abc', 'E683:')
2422 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
2423 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
2424
2425 Xexpr ""
2426 Xvimgrepadd Notepad Xtestfile1
2427 Xvimgrepadd MacOS Xtestfile2
2428 let l = g:Xgetlist()
2429 call assert_equal(2, len(l))
2430 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
2431
2432 Xvimgrep #\cvim#g Xtestfile?
2433 let l = g:Xgetlist()
2434 call assert_equal(2, len(l))
2435 call assert_equal(8, l[0].col)
2436 call assert_equal(12, l[1].col)
2437
2438 1Xvimgrep ?Editor? Xtestfile*
2439 let l = g:Xgetlist()
2440 call assert_equal(1, len(l))
2441 call assert_equal('Editor:VIM vim', l[0].text)
2442
2443 edit +3 Xtestfile2
2444 Xvimgrep +\cemacs+j Xtestfile1
2445 let l = g:Xgetlist()
2446 call assert_equal('Xtestfile2', bufname(''))
2447 call assert_equal('Editor:Emacs EmAcS', l[0].text)
2448
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02002449 " Test for unloading a buffer after vimgrep searched the buffer
2450 %bwipe
2451 Xvimgrep /Editor/j Xtestfile*
2452 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
2453 call assert_equal([], getbufinfo('Xtestfile2'))
2454
Bram Moolenaaree85df32017-03-19 14:19:50 +01002455 call delete('Xtestfile1')
2456 call delete('Xtestfile2')
2457endfunc
2458
2459" Tests for the :vimgrep command
2460func Test_vimgrep()
2461 call XvimgrepTests('c')
2462 call XvimgrepTests('l')
2463endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002464
Bram Moolenaar1c299432018-10-28 14:36:09 +01002465" Test for incsearch highlighting of the :vimgrep pattern
2466" This test used to cause "E315: ml_get: invalid lnum" errors.
2467func Test_vimgrep_incsearch()
2468 enew
2469 set incsearch
2470 call test_override("char_avail", 1)
2471
2472 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
2473 let l = getqflist()
2474 call assert_equal(2, len(l))
2475
2476 call test_override("ALL", 0)
2477 set noincsearch
2478endfunc
2479
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002480func XfreeTests(cchar)
2481 call s:setup_commands(a:cchar)
2482
2483 enew | only
2484
2485 " Deleting the quickfix stack should work even When the current list is
2486 " somewhere in the middle of the stack
2487 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2488 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2489 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2490 Xolder
2491 call g:Xsetlist([], 'f')
2492 call assert_equal(0, len(g:Xgetlist()))
2493
2494 " After deleting the stack, adding a new list should create a stack with a
2495 " single list.
2496 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2497 call assert_equal(1, g:Xgetlist({'all':1}).nr)
2498
2499 " Deleting the stack from a quickfix window should update/clear the
2500 " quickfix/location list window.
2501 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2502 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2503 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2504 Xolder
2505 Xwindow
2506 call g:Xsetlist([], 'f')
2507 call assert_equal(2, winnr('$'))
2508 call assert_equal(1, line('$'))
2509 Xclose
2510
2511 " Deleting the stack from a non-quickfix window should update/clear the
2512 " quickfix/location list window.
2513 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2514 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2515 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2516 Xolder
2517 Xwindow
2518 wincmd p
2519 call g:Xsetlist([], 'f')
2520 call assert_equal(0, len(g:Xgetlist()))
2521 wincmd p
2522 call assert_equal(2, winnr('$'))
2523 call assert_equal(1, line('$'))
2524
2525 " After deleting the location list stack, if the location list window is
2526 " opened, then a new location list should be created. So opening the
2527 " location list window again should not create a new window.
2528 if a:cchar == 'l'
2529 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2530 wincmd p
2531 lopen
2532 call assert_equal(2, winnr('$'))
2533 endif
2534 Xclose
2535endfunc
2536
Bram Moolenaar74240d32017-12-10 15:26:15 +01002537" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002538func Test_qf_free()
2539 call XfreeTests('c')
2540 call XfreeTests('l')
2541endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002542
2543" Test for buffer overflow when parsing lines and adding new entries to
2544" the quickfix list.
2545func Test_bufoverflow()
2546 set efm=%f:%l:%m
2547 cgetexpr ['File1:100:' . repeat('x', 1025)]
2548
2549 set efm=%+GCompiler:\ %.%#,%f:%l:%m
2550 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
2551
2552 set efm=%DEntering\ directory\ %f,%f:%l:%m
2553 cgetexpr ['Entering directory ' . repeat('a', 1006),
2554 \ 'File1:10:Hello World']
2555 set efm&vim
2556endfunc
2557
Bram Moolenaar875feea2017-06-11 16:07:51 +02002558" Tests for getting the quickfix stack size
2559func XsizeTests(cchar)
2560 call s:setup_commands(a:cchar)
2561
2562 call g:Xsetlist([], 'f')
2563 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01002564 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
2565 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02002566
2567 Xexpr "File1:10:Line1"
2568 Xexpr "File2:20:Line2"
2569 Xexpr "File3:30:Line3"
2570 Xolder | Xolder
2571 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2572 call g:Xsetlist([], 'f')
2573
2574 Xexpr "File1:10:Line1"
2575 Xexpr "File2:20:Line2"
2576 Xexpr "File3:30:Line3"
2577 Xolder | Xolder
2578 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
2579 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
2580endfunc
2581
2582func Test_Qf_Size()
2583 call XsizeTests('c')
2584 call XsizeTests('l')
2585endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02002586
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002587func Test_cclose_from_copen()
2588 augroup QF_Test
2589 au!
2590 au FileType qf :call assert_fails(':cclose', 'E788')
2591 augroup END
2592 copen
2593 augroup QF_Test
2594 au!
2595 augroup END
2596 augroup! QF_Test
2597endfunc
2598
Bram Moolenaar18141832017-06-25 21:17:25 +02002599func Test_cclose_in_autocmd()
2600 " Problem is only triggered if "starting" is zero, so that the OptionsSet
2601 " event will be triggered.
2602 call test_override('starting', 1)
2603 augroup QF_Test
2604 au!
2605 au FileType qf :call assert_fails(':cclose', 'E788')
2606 augroup END
2607 copen
2608 augroup QF_Test
2609 au!
2610 augroup END
2611 augroup! QF_Test
2612 call test_override('starting', 0)
2613endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002614
Bram Moolenaar379fb762018-08-30 15:58:28 +02002615" Check that ":file" without an argument is possible even when "curbuf_lock"
2616" is set.
2617func Test_file_from_copen()
2618 " Works without argument.
2619 augroup QF_Test
2620 au!
2621 au FileType qf file
2622 augroup END
2623 copen
2624
2625 augroup QF_Test
2626 au!
2627 augroup END
2628 cclose
2629
2630 " Fails with argument.
2631 augroup QF_Test
2632 au!
2633 au FileType qf call assert_fails(':file foo', 'E788')
2634 augroup END
2635 copen
2636 augroup QF_Test
2637 au!
2638 augroup END
2639 cclose
2640
2641 augroup! QF_Test
2642endfunction
2643
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002644func Test_resize_from_copen()
2645 augroup QF_Test
2646 au!
2647 au FileType qf resize 5
2648 augroup END
2649 try
2650 " This should succeed without any exception. No other buffers are
2651 " involved in the autocmd.
2652 copen
2653 finally
2654 augroup QF_Test
2655 au!
2656 augroup END
2657 augroup! QF_Test
2658 endtry
2659endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02002660
2661" Tests for the quickfix buffer b:changedtick variable
2662func Xchangedtick_tests(cchar)
2663 call s:setup_commands(a:cchar)
2664
2665 new | only
2666
2667 Xexpr "" | Xexpr "" | Xexpr ""
2668
2669 Xopen
2670 Xolder
2671 Xolder
2672 Xaddexpr "F1:10:Line10"
2673 Xaddexpr "F2:20:Line20"
2674 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
2675 call g:Xsetlist([], 'f')
2676 call assert_equal(8, getbufvar('%', 'changedtick'))
2677 Xclose
2678endfunc
2679
2680func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02002681 call Xchangedtick_tests('c')
2682 call Xchangedtick_tests('l')
2683endfunc
2684
2685" Tests for parsing an expression using setqflist()
2686func Xsetexpr_tests(cchar)
2687 call s:setup_commands(a:cchar)
2688
2689 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002690 call g:Xsetlist([], ' ', {'lines' : t})
2691 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002692
2693 let l = g:Xgetlist()
2694 call assert_equal(3, len(l))
2695 call assert_equal(20, l[1].lnum)
2696 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002697 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002698 let l = g:Xgetlist()
2699 call assert_equal(1, len(l))
2700 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002701 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
2702 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02002703
2704 call g:Xsetlist([], 'f')
2705 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002706 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
2707 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
2708 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
2709 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002710 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
2711 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02002712
2713 " Adding entries using a custom efm
2714 set efm&
2715 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
2716 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
2717 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
2718 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
2719 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
2720 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
2721 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
2722 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02002723endfunc
2724
2725func Test_setexpr()
2726 call Xsetexpr_tests('c')
2727 call Xsetexpr_tests('l')
2728endfunc
2729
2730" Tests for per quickfix/location list directory stack
2731func Xmultidirstack_tests(cchar)
2732 call s:setup_commands(a:cchar)
2733
2734 call g:Xsetlist([], 'f')
2735 Xexpr "" | Xexpr ""
2736
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002737 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
2738 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
2739 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
2740 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002741
2742 let l1 = g:Xgetlist({'nr':1, 'items':1})
2743 let l2 = g:Xgetlist({'nr':2, 'items':1})
2744 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
2745 call assert_equal(3, l1.items[1].lnum)
2746 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
2747 call assert_equal(5, l2.items[1].lnum)
2748endfunc
2749
2750func Test_multidirstack()
2751 call mkdir('Xone/a', 'p')
2752 call mkdir('Xtwo/a', 'p')
2753 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2754 call writefile(lines, 'Xone/a/one.txt')
2755 call writefile(lines, 'Xtwo/a/two.txt')
2756 let save_efm = &efm
2757 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
2758
2759 call Xmultidirstack_tests('c')
2760 call Xmultidirstack_tests('l')
2761
2762 let &efm = save_efm
2763 call delete('Xone', 'rf')
2764 call delete('Xtwo', 'rf')
2765endfunc
2766
2767" Tests for per quickfix/location list file stack
2768func Xmultifilestack_tests(cchar)
2769 call s:setup_commands(a:cchar)
2770
2771 call g:Xsetlist([], 'f')
2772 Xexpr "" | Xexpr ""
2773
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002774 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
2775 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
2776 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
2777 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002778
2779 let l1 = g:Xgetlist({'nr':1, 'items':1})
2780 let l2 = g:Xgetlist({'nr':2, 'items':1})
2781 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
2782 call assert_equal(3, l1.items[1].lnum)
2783 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
2784 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02002785
2786 " Test for start of a new error line in the same line where a previous
2787 " error line ends with a file stack.
2788 let efm_val = 'Error\ l%l\ in\ %f,'
2789 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
2790 let l = g:Xgetlist({'lines' : [
2791 \ '(one.txt',
2792 \ 'Error l4 in one.txt',
2793 \ ') (two.txt',
2794 \ 'Error l6 in two.txt',
2795 \ ')',
2796 \ 'Error l8 in one.txt'
2797 \ ], 'efm' : efm_val})
2798 call assert_equal(3, len(l.items))
2799 call assert_equal('one.txt', bufname(l.items[0].bufnr))
2800 call assert_equal(4, l.items[0].lnum)
2801 call assert_equal('one.txt', l.items[0].text)
2802 call assert_equal('two.txt', bufname(l.items[1].bufnr))
2803 call assert_equal(6, l.items[1].lnum)
2804 call assert_equal('two.txt', l.items[1].text)
2805 call assert_equal('one.txt', bufname(l.items[2].bufnr))
2806 call assert_equal(8, l.items[2].lnum)
2807 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02002808endfunc
2809
2810func Test_multifilestack()
2811 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2812 call writefile(lines, 'one.txt')
2813 call writefile(lines, 'two.txt')
2814 let save_efm = &efm
2815 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
2816
2817 call Xmultifilestack_tests('c')
2818 call Xmultifilestack_tests('l')
2819
2820 let &efm = save_efm
2821 call delete('one.txt')
2822 call delete('two.txt')
2823endfunc
2824
2825" Tests for per buffer 'efm' setting
2826func Test_perbuf_efm()
2827 call writefile(["File1-10-Line10"], 'one.txt')
2828 call writefile(["File2#20#Line20"], 'two.txt')
2829 set efm=%f#%l#%m
2830 new | only
2831 new
2832 setlocal efm=%f-%l-%m
2833 cfile one.txt
2834 wincmd w
2835 caddfile two.txt
2836
2837 let l = getqflist()
2838 call assert_equal(10, l[0].lnum)
2839 call assert_equal('Line20', l[1].text)
2840
2841 set efm&
2842 new | only
2843 call delete('one.txt')
2844 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02002845endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002846
2847" Open multiple help windows using ":lhelpgrep
2848" This test used to crash Vim
2849func Test_Multi_LL_Help()
2850 new | only
2851 lhelpgrep window
2852 lopen
2853 e#
2854 lhelpgrep buffer
2855 call assert_equal(3, winnr('$'))
2856 call assert_true(len(getloclist(1)) != 0)
2857 call assert_true(len(getloclist(2)) != 0)
2858 new | only
2859endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02002860
2861" Tests for adding new quickfix lists using setqflist()
2862func XaddQf_tests(cchar)
2863 call s:setup_commands(a:cchar)
2864
2865 " Create a new list using ' ' for action
2866 call g:Xsetlist([], 'f')
2867 call g:Xsetlist([], ' ', {'title' : 'Test1'})
2868 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2869 call assert_equal(1, l.nr)
2870 call assert_equal('Test1', l.title)
2871
2872 " Create a new list using ' ' for action and '$' for 'nr'
2873 call g:Xsetlist([], 'f')
2874 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
2875 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2876 call assert_equal(1, l.nr)
2877 call assert_equal('Test2', l.title)
2878
2879 " Create a new list using 'a' for action
2880 call g:Xsetlist([], 'f')
2881 call g:Xsetlist([], 'a', {'title' : 'Test3'})
2882 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2883 call assert_equal(1, l.nr)
2884 call assert_equal('Test3', l.title)
2885
2886 " Create a new list using 'a' for action and '$' for 'nr'
2887 call g:Xsetlist([], 'f')
2888 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
2889 call g:Xsetlist([], 'a', {'title' : 'Test4'})
2890 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2891 call assert_equal(1, l.nr)
2892 call assert_equal('Test4', l.title)
2893
2894 " Adding a quickfix list should remove all the lists following the current
2895 " list.
2896 Xexpr "" | Xexpr "" | Xexpr ""
2897 silent! 10Xolder
2898 call g:Xsetlist([], ' ', {'title' : 'Test5'})
2899 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2900 call assert_equal(2, l.nr)
2901 call assert_equal('Test5', l.title)
2902
2903 " Add a quickfix list using '$' as the list number.
2904 let lastqf = g:Xgetlist({'nr':'$'}).nr
2905 silent! 99Xolder
2906 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
2907 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2908 call assert_equal(lastqf + 1, l.nr)
2909 call assert_equal('Test6', l.title)
2910
2911 " Add a quickfix list using 'nr' set to one more than the quickfix
2912 " list size.
2913 let lastqf = g:Xgetlist({'nr':'$'}).nr
2914 silent! 99Xolder
2915 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
2916 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2917 call assert_equal(lastqf + 1, l.nr)
2918 call assert_equal('Test7', l.title)
2919
2920 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
2921 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
2922 silent! 99Xolder
2923 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
2924 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2925 call assert_equal(10, l.nr)
2926 call assert_equal('Test8', l.title)
2927
2928 " Add a quickfix list using 'nr' set to a value greater than 10
2929 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
2930
2931 " Try adding a quickfix list with 'nr' set to a value greater than the
2932 " quickfix list size but less than 10.
2933 call g:Xsetlist([], 'f')
2934 Xexpr "" | Xexpr "" | Xexpr ""
2935 silent! 99Xolder
2936 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
2937
2938 " Add a quickfix list using 'nr' set to a some string or list
2939 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
2940endfunc
2941
2942func Test_add_qf()
2943 call XaddQf_tests('c')
2944 call XaddQf_tests('l')
2945endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002946
2947" Test for getting the quickfix list items from some text without modifying
2948" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002949func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002950 call s:setup_commands(a:cchar)
2951 call g:Xsetlist([], 'f')
2952
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002953 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002954 call assert_equal(2, len(l))
2955 call assert_equal(30, l[1].lnum)
2956
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002957 call assert_equal({}, g:Xgetlist({'lines' : 10}))
2958 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
2959 call assert_equal([], g:Xgetlist({'lines' : []}).items)
2960 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002961
Bram Moolenaar36538222017-09-02 19:51:44 +02002962 " Parse text using a custom efm
2963 set efm&
2964 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
2965 call assert_equal('Line30', l[0].text)
2966 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
2967 call assert_equal('File3:30:Line30', l[0].text)
2968 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
2969 call assert_equal({}, l)
2970 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
2971 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
2972
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002973 " Make sure that the quickfix stack is not modified
2974 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
2975endfunc
2976
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002977func Test_get_list_from_lines()
2978 call XgetListFromLines('c')
2979 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002980endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002981
2982" Tests for the quickfix list id
2983func Xqfid_tests(cchar)
2984 call s:setup_commands(a:cchar)
2985
2986 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01002987 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002988 Xexpr ''
2989 let start_id = g:Xgetlist({'id' : 0}).id
2990 Xexpr '' | Xexpr ''
2991 Xolder
2992 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
2993 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
2994 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01002995 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002996 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01002997 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
2998 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002999
3000 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3001 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003002 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003003 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3004 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3005 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3006
3007 let qfid = g:Xgetlist({'id':0, 'nr':0})
3008 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003009 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003010endfunc
3011
3012func Test_qf_id()
3013 call Xqfid_tests('c')
3014 call Xqfid_tests('l')
3015endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003016
3017func Xqfjump_tests(cchar)
3018 call s:setup_commands(a:cchar)
3019
3020 call writefile(["Line1\tFoo", "Line2"], 'F1')
3021 call writefile(["Line1\tBar", "Line2"], 'F2')
3022 call writefile(["Line1\tBaz", "Line2"], 'F3')
3023
3024 call g:Xsetlist([], 'f')
3025
3026 " Tests for
3027 " Jumping to a line using a pattern
3028 " Jumping to a column greater than the last column in a line
3029 " Jumping to a line greater than the last line in the file
3030 let l = []
3031 for i in range(1, 7)
3032 call add(l, {})
3033 endfor
3034 let l[0].filename='F1'
3035 let l[0].pattern='Line1'
3036 let l[1].filename='F2'
3037 let l[1].pattern='Line1'
3038 let l[2].filename='F3'
3039 let l[2].pattern='Line1'
3040 let l[3].filename='F3'
3041 let l[3].lnum=1
3042 let l[3].col=9
3043 let l[3].vcol=1
3044 let l[4].filename='F3'
3045 let l[4].lnum=99
3046 let l[5].filename='F3'
3047 let l[5].lnum=1
3048 let l[5].col=99
3049 let l[5].vcol=1
3050 let l[6].filename='F3'
3051 let l[6].pattern='abcxyz'
3052
3053 call g:Xsetlist([], ' ', {'items' : l})
3054 Xopen | only
3055 2Xnext
3056 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
3057 call assert_equal('F3', bufname('%'))
3058 Xnext
3059 call assert_equal(7, col('.'))
3060 Xnext
3061 call assert_equal(2, line('.'))
3062 Xnext
3063 call assert_equal(9, col('.'))
3064 2
3065 Xnext
3066 call assert_equal(2, line('.'))
3067
3068 if a:cchar == 'l'
3069 " When jumping to a location list entry in the location list window and
3070 " no usable windows are available, then a new window should be opened.
3071 enew! | new | only
3072 call g:Xsetlist([], 'f')
3073 setlocal buftype=nofile
3074 new
3075 call g:Xsetlist([], ' ', {'lines' : ['F1:1:1:Line1', 'F1:2:2:Line2', 'F2:1:1:Line1', 'F2:2:2:Line2', 'F3:1:1:Line1', 'F3:2:2:Line2']})
3076 Xopen
3077 let winid = win_getid()
3078 wincmd p
3079 close
3080 call win_gotoid(winid)
3081 Xnext
3082 call assert_equal(3, winnr('$'))
3083 call assert_equal(1, winnr())
3084 call assert_equal(2, line('.'))
3085
3086 " When jumping to an entry in the location list window and the window
3087 " associated with the location list is not present and a window containing
3088 " the file is already present, then that window should be used.
3089 close
3090 belowright new
3091 call g:Xsetlist([], 'f')
3092 edit F3
3093 call win_gotoid(winid)
3094 Xlast
3095 call assert_equal(3, winnr())
3096 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3097 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3098 endif
3099
3100 " Cleanup
3101 enew!
3102 new | only
3103
3104 call delete('F1')
3105 call delete('F2')
3106 call delete('F3')
3107endfunc
3108
3109func Test_qfjump()
3110 call Xqfjump_tests('c')
3111 call Xqfjump_tests('l')
3112endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01003113
3114" Tests for the getqflist() and getloclist() functions when the list is not
3115" present or is empty
3116func Xgetlist_empty_tests(cchar)
3117 call s:setup_commands(a:cchar)
3118
3119 " Empty quickfix stack
3120 call g:Xsetlist([], 'f')
3121 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3122 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
3123 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3124 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3125 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
3126 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3127 call assert_equal('', g:Xgetlist({'title' : 0}).title)
3128 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003129 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003130 if a:cchar == 'c'
3131 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003132 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003133 \ 'title' : '', 'winid' : 0, 'changedtick': 0},
3134 \ g:Xgetlist({'all' : 0}))
3135 else
3136 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
3137 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003138 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
3139 \ 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003140 \ g:Xgetlist({'all' : 0}))
3141 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003142
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003143 " Quickfix window with empty stack
3144 silent! Xopen
3145 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003146 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003147 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
3148 Xclose
3149
Bram Moolenaara6d48492017-12-12 22:45:31 +01003150 " Empty quickfix list
3151 Xexpr ""
3152 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3153 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
3154 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3155 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3156 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
3157 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3158 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
3159 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003160 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003161
3162 let qfid = g:Xgetlist({'id' : 0}).id
3163 call g:Xsetlist([], 'f')
3164
3165 " Non-existing quickfix identifier
3166 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
3167 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
3168 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
3169 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
3170 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
3171 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
3172 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
3173 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003174 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003175 if a:cchar == 'c'
3176 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3177 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003178 \ 'qfbufnr' : qfbufnr,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003179 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
3180 else
3181 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3182 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003183 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003184 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
3185 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003186
3187 " Non-existing quickfix list number
3188 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
3189 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
3190 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
3191 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
3192 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
3193 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
3194 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
3195 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003196 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003197 if a:cchar == 'c'
3198 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3199 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003200 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr},
3201 \ g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003202 else
3203 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3204 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003205 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003206 \ g:Xgetlist({'nr' : 5, 'all' : 0}))
3207 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003208endfunc
3209
3210func Test_getqflist()
3211 call Xgetlist_empty_tests('c')
3212 call Xgetlist_empty_tests('l')
3213endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01003214
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01003215func Test_getqflist_invalid_nr()
3216 " The following commands used to crash Vim
3217 cexpr ""
3218 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
3219
3220 " Cleanup
3221 call setqflist([], 'r')
3222endfunc
3223
Bram Moolenaarb254af32017-12-18 19:48:58 +01003224" Tests for the quickfix/location list changedtick
3225func Xqftick_tests(cchar)
3226 call s:setup_commands(a:cchar)
3227
3228 call g:Xsetlist([], 'f')
3229
3230 Xexpr "F1:10:Line10"
3231 let qfid = g:Xgetlist({'id' : 0}).id
3232 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3233 Xaddexpr "F2:20:Line20\nF2:21:Line21"
3234 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3235 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
3236 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
3237 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
3238 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
3239 call g:Xsetlist([], 'a', {'title' : 'New Title'})
3240 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
3241
3242 enew!
3243 call append(0, ["F5:50:L50", "F6:60:L60"])
3244 Xaddbuffer
3245 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
3246 enew!
3247
3248 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
3249 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
3250 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3251 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
3252 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
3253 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3254 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
3255 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3256 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3257 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
3258 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3259
3260 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
3261 Xfile Xone
3262 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3263 Xaddfile Xone
3264 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3265
3266 " Test case for updating a non-current quickfix list
3267 call g:Xsetlist([], 'f')
3268 Xexpr "F1:1:L1"
3269 Xexpr "F2:2:L2"
3270 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
3271 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3272 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
3273
3274 call delete("Xone")
3275endfunc
3276
3277func Test_qf_tick()
3278 call Xqftick_tests('c')
3279 call Xqftick_tests('l')
3280endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01003281
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02003282" Test helpgrep with lang specifier
3283func Xtest_helpgrep_with_lang_specifier(cchar)
3284 call s:setup_commands(a:cchar)
3285 Xhelpgrep Vim@en
3286 call assert_equal('help', &filetype)
3287 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
3288 new | only
3289endfunc
3290
3291func Test_helpgrep_with_lang_specifier()
3292 call Xtest_helpgrep_with_lang_specifier('c')
3293 call Xtest_helpgrep_with_lang_specifier('l')
3294endfunc
3295
Bram Moolenaar12237442017-12-19 12:38:52 +01003296" The following test used to crash Vim.
3297" Open the location list window and close the regular window associated with
3298" the location list. When the garbage collection runs now, it incorrectly
3299" marks the location list context as not in use and frees the context.
3300func Test_ll_window_ctx()
3301 call setloclist(0, [], 'f')
3302 call setloclist(0, [], 'a', {'context' : []})
3303 lopen | only
3304 call test_garbagecollect_now()
3305 echo getloclist(0, {'context' : 1}).context
3306 enew | only
3307endfunc
3308
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01003309" The following test used to crash vim
3310func Test_lfile_crash()
3311 sp Xtest
3312 au QuickFixCmdPre * bw
3313 call assert_fails('lfile', 'E40')
3314 au! QuickFixCmdPre
3315endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01003316
3317" The following test used to crash vim
3318func Test_lbuffer_crash()
3319 sv Xtest
3320 augroup QF_Test
3321 au!
3322 au * * bw
3323 augroup END
3324 lbuffer
3325 augroup QF_Test
3326 au!
3327 augroup END
3328endfunc
3329
3330" The following test used to crash vim
3331func Test_lexpr_crash()
3332 augroup QF_Test
3333 au!
3334 au * * call setloclist(0, [], 'f')
3335 augroup END
3336 lexpr ""
3337 augroup QF_Test
3338 au!
3339 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003340
Bram Moolenaar3c097222017-12-21 20:54:49 +01003341 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003342 augroup QF_Test
3343 au!
3344 au BufNew * call setloclist(0, [], 'f')
3345 augroup END
3346 lexpr 'x:1:x'
3347 augroup QF_Test
3348 au!
3349 augroup END
3350
3351 enew | only
3352 lexpr ''
3353 lopen
3354 augroup QF_Test
3355 au!
3356 au FileType * call setloclist(0, [], 'f')
3357 augroup END
3358 lexpr ''
3359 augroup QF_Test
3360 au!
3361 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01003362endfunc
3363
3364" The following test used to crash Vim
3365func Test_lvimgrep_crash()
3366 sv Xtest
3367 augroup QF_Test
3368 au!
3369 au * * call setloclist(0, [], 'f')
3370 augroup END
3371 lvimgrep quickfix test_quickfix.vim
3372 augroup QF_Test
3373 au!
3374 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003375
3376 new | only
3377 augroup QF_Test
3378 au!
3379 au BufEnter * call setloclist(0, [], 'r')
3380 augroup END
3381 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
3382 augroup QF_Test
3383 au!
3384 augroup END
3385
Bram Moolenaar3c097222017-12-21 20:54:49 +01003386 enew | only
3387endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01003388
3389" Test for the position of the quickfix and location list window
3390func Test_qfwin_pos()
3391 " Open two windows
3392 new | only
3393 new
3394 cexpr ['F1:10:L10']
3395 copen
3396 " Quickfix window should be the bottom most window
3397 call assert_equal(3, winnr())
3398 close
3399 " Open at the very top
3400 wincmd t
3401 topleft copen
3402 call assert_equal(1, winnr())
3403 close
3404 " open left of the current window
3405 wincmd t
3406 below new
3407 leftabove copen
3408 call assert_equal(2, winnr())
3409 close
3410 " open right of the current window
3411 rightbelow copen
3412 call assert_equal(3, winnr())
3413 close
3414endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02003415
3416" Tests for quickfix/location lists changed by autocommands when
3417" :vimgrep/:lvimgrep commands are running.
3418func Test_vimgrep_autocmd()
3419 call setqflist([], 'f')
3420 call writefile(['stars'], 'Xtest1.txt')
3421 call writefile(['stars'], 'Xtest2.txt')
3422
3423 " Test 1:
3424 " When searching for a pattern using :vimgrep, if the quickfix list is
3425 " changed by an autocmd, the results should be added to the correct quickfix
3426 " list.
3427 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
3428 silent vimgrep stars Xtest*.txt
3429 call assert_equal(1, getqflist({'nr' : 0}).nr)
3430 call assert_equal(3, getqflist({'nr' : '$'}).nr)
3431 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
3432 au! BufRead Xtest2.txt
3433
3434 " Test 2:
3435 " When searching for a pattern using :vimgrep, if the quickfix list is
3436 " freed, then a error should be given.
3437 silent! %bwipe!
3438 call setqflist([], 'f')
3439 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
3440 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
3441 au! BufRead Xtest2.txt
3442
3443 " Test 3:
3444 " When searching for a pattern using :lvimgrep, if the location list is
3445 " freed, then the command should error out.
3446 silent! %bwipe!
3447 let g:save_winid = win_getid()
3448 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
3449 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
3450 au! BufRead Xtest2.txt
3451
3452 call delete('Xtest1.txt')
3453 call delete('Xtest2.txt')
3454 call setqflist([], 'f')
3455endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003456
3457" The following test used to crash Vim
3458func Test_lhelpgrep_autocmd()
3459 lhelpgrep quickfix
3460 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
3461 lhelpgrep buffer
3462 call assert_equal('help', &filetype)
3463 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
3464 lhelpgrep tabpage
3465 call assert_equal('help', &filetype)
3466 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
3467 au! QuickFixCmdPost
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003468
3469 new | only
3470 augroup QF_Test
3471 au!
3472 au BufEnter * call setqflist([], 'f')
3473 augroup END
3474 call assert_fails('helpgrep quickfix', 'E925:')
3475 augroup QF_Test
3476 au! BufEnter
3477 augroup END
3478
3479 new | only
3480 augroup QF_Test
3481 au!
3482 au BufEnter * call setqflist([], 'r')
3483 augroup END
3484 call assert_fails('helpgrep quickfix', 'E925:')
3485 augroup QF_Test
3486 au! BufEnter
3487 augroup END
3488
3489 new | only
3490 augroup QF_Test
3491 au!
3492 au BufEnter * call setloclist(0, [], 'r')
3493 augroup END
3494 call assert_fails('lhelpgrep quickfix', 'E926:')
3495 augroup QF_Test
3496 au! BufEnter
3497 augroup END
3498
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003499 new | only
3500endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02003501
3502" Test for shortening/simplifying the file name when opening the
3503" quickfix window or when displaying the quickfix list
3504func Test_shorten_fname()
3505 if !has('unix')
3506 return
3507 endif
3508 %bwipe
3509 " Create a quickfix list with a absolute path filename
3510 let fname = getcwd() . '/test_quickfix.vim'
3511 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
3512 call assert_equal(fname, bufname('test_quickfix.vim'))
3513 " Opening the quickfix window should simplify the file path
3514 cwindow
3515 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
3516 cclose
3517 %bwipe
3518 " Create a quickfix list with a absolute path filename
3519 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
3520 call assert_equal(fname, bufname('test_quickfix.vim'))
3521 " Displaying the quickfix list should simplify the file path
3522 silent! clist
3523 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
3524endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02003525
3526" Quickfix title tests
3527" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
3528" Otherwise due to indentation, the title is set with spaces at the beginning
3529" of the command.
3530func Test_qftitle()
3531 call writefile(["F1:1:Line1"], 'Xerr')
3532
3533 " :cexpr
3534 exe "cexpr readfile('Xerr')"
3535 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
3536
3537 " :cgetexpr
3538 exe "cgetexpr readfile('Xerr')"
3539 call assert_equal(":cgetexpr readfile('Xerr')",
3540 \ getqflist({'title' : 1}).title)
3541
3542 " :caddexpr
3543 call setqflist([], 'f')
3544 exe "caddexpr readfile('Xerr')"
3545 call assert_equal(":caddexpr readfile('Xerr')",
3546 \ getqflist({'title' : 1}).title)
3547
3548 " :cbuffer
3549 new Xerr
3550 exe "cbuffer"
3551 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
3552
3553 " :cgetbuffer
3554 edit Xerr
3555 exe "cgetbuffer"
3556 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
3557
3558 " :caddbuffer
3559 call setqflist([], 'f')
3560 edit Xerr
3561 exe "caddbuffer"
3562 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
3563
3564 " :cfile
3565 exe "cfile Xerr"
3566 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
3567
3568 " :cgetfile
3569 exe "cgetfile Xerr"
3570 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
3571
3572 " :caddfile
3573 call setqflist([], 'f')
3574 exe "caddfile Xerr"
3575 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
3576
3577 " :grep
3578 set grepprg=internal
3579 exe "grep F1 Xerr"
3580 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
3581
3582 " :grepadd
3583 call setqflist([], 'f')
3584 exe "grepadd F1 Xerr"
3585 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
3586 set grepprg&vim
3587
3588 " :vimgrep
3589 exe "vimgrep F1 Xerr"
3590 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
3591
3592 " :vimgrepadd
3593 call setqflist([], 'f')
3594 exe "vimgrepadd F1 Xerr"
3595 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
3596
3597 call setqflist(['F1:10:L10'], ' ')
3598 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3599
3600 call setqflist([], 'f')
3601 call setqflist(['F1:10:L10'], 'a')
3602 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3603
3604 call setqflist([], 'f')
3605 call setqflist(['F1:10:L10'], 'r')
3606 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3607
3608 close
3609 call delete('Xerr')
3610
3611 call setqflist([], ' ', {'title' : 'Errors'})
3612 copen
3613 call assert_equal('Errors', w:quickfix_title)
3614 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
3615 call assert_equal('Errors', w:quickfix_title)
3616 cclose
3617endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02003618
3619func Test_lbuffer_with_bwipe()
3620 new
3621 new
3622 augroup nasty
3623 au * * bwipe
3624 augroup END
3625 lbuffer
3626 augroup nasty
3627 au!
3628 augroup END
3629endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02003630
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003631" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
3632" running
3633func Xexpr_acmd_freelist(cchar)
3634 call s:setup_commands(a:cchar)
3635
Bram Moolenaar0366c012018-06-18 20:52:13 +02003636 " This was using freed memory.
3637 augroup nasty
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003638 au * * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02003639 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003640 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02003641 augroup nasty
3642 au!
3643 augroup END
3644endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003645
3646func Test_cexpr_acmd_freelist()
3647 call Xexpr_acmd_freelist('c')
3648 call Xexpr_acmd_freelist('l')
3649endfunc
3650
3651" Test for commands that create a new quickfix/location list and jump to the
3652" first error automatically.
3653func Xjumpto_first_error_test(cchar)
3654 call s:setup_commands(a:cchar)
3655
3656 call s:create_test_file('Xtestfile1')
3657 call s:create_test_file('Xtestfile2')
3658 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
3659
3660 " Test for cexpr/lexpr
3661 enew
3662 Xexpr l
3663 call assert_equal('Xtestfile1', bufname(''))
3664 call assert_equal(2, line('.'))
3665
3666 " Test for cfile/lfile
3667 enew
3668 call writefile(l, 'Xerr')
3669 Xfile Xerr
3670 call assert_equal('Xtestfile1', bufname(''))
3671 call assert_equal(2, line('.'))
3672
3673 " Test for cbuffer/lbuffer
3674 edit Xerr
3675 Xbuffer
3676 call assert_equal('Xtestfile1', bufname(''))
3677 call assert_equal(2, line('.'))
3678
3679 call delete('Xerr')
3680 call delete('Xtestfile1')
3681 call delete('Xtestfile2')
3682endfunc
3683
3684func Test_jumpto_first_error()
3685 call Xjumpto_first_error_test('c')
3686 call Xjumpto_first_error_test('l')
3687endfunc
3688
3689" Test for a quickfix autocmd changing the quickfix/location list before
3690" jumping to the first error in the new list.
3691func Xautocmd_changelist(cchar)
3692 call s:setup_commands(a:cchar)
3693
3694 " Test for cfile/lfile
3695 call s:create_test_file('Xtestfile1')
3696 call s:create_test_file('Xtestfile2')
3697 Xexpr 'Xtestfile1:2:Line2'
3698 autocmd QuickFixCmdPost * Xolder
3699 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
3700 Xfile Xerr
3701 call assert_equal('Xtestfile2', bufname(''))
3702 call assert_equal(4, line('.'))
3703 autocmd! QuickFixCmdPost
3704
3705 " Test for cbuffer/lbuffer
3706 call g:Xsetlist([], 'f')
3707 Xexpr 'Xtestfile1:2:Line2'
3708 autocmd QuickFixCmdPost * Xolder
3709 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
3710 edit Xerr
3711 Xbuffer
3712 call assert_equal('Xtestfile2', bufname(''))
3713 call assert_equal(4, line('.'))
3714 autocmd! QuickFixCmdPost
3715
3716 " Test for cexpr/lexpr
3717 call g:Xsetlist([], 'f')
3718 Xexpr 'Xtestfile1:2:Line2'
3719 autocmd QuickFixCmdPost * Xolder
3720 Xexpr 'Xtestfile2:4:Line4'
3721 call assert_equal('Xtestfile2', bufname(''))
3722 call assert_equal(4, line('.'))
3723 autocmd! QuickFixCmdPost
3724
Bram Moolenaar851332e2018-07-03 19:16:00 +02003725 " The grepprg may not be set on non-Unix systems
3726 if has('unix')
3727 " Test for grep/lgrep
3728 call g:Xsetlist([], 'f')
3729 Xexpr 'Xtestfile1:2:Line2'
3730 autocmd QuickFixCmdPost * Xolder
3731 silent Xgrep Line5 Xtestfile2
3732 call assert_equal('Xtestfile2', bufname(''))
3733 call assert_equal(5, line('.'))
3734 autocmd! QuickFixCmdPost
3735 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003736
3737 " Test for vimgrep/lvimgrep
3738 call g:Xsetlist([], 'f')
3739 Xexpr 'Xtestfile1:2:Line2'
3740 autocmd QuickFixCmdPost * Xolder
3741 silent Xvimgrep Line5 Xtestfile2
3742 call assert_equal('Xtestfile2', bufname(''))
3743 call assert_equal(5, line('.'))
3744 autocmd! QuickFixCmdPost
3745
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003746 " Test for autocommands clearing the quickfix list before jumping to the
3747 " first error. This should not result in an error
3748 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
3749 let v:errmsg = ''
3750 " Test for cfile/lfile
3751 Xfile Xerr
3752 call assert_true(v:errmsg !~# 'E42:')
3753 " Test for cbuffer/lbuffer
3754 edit Xerr
3755 Xbuffer
3756 call assert_true(v:errmsg !~# 'E42:')
3757 " Test for cexpr/lexpr
3758 Xexpr 'Xtestfile2:4:Line4'
3759 call assert_true(v:errmsg !~# 'E42:')
3760 " Test for grep/lgrep
3761 " The grepprg may not be set on non-Unix systems
3762 if has('unix')
3763 silent Xgrep Line5 Xtestfile2
3764 call assert_true(v:errmsg !~# 'E42:')
3765 endif
3766 " Test for vimgrep/lvimgrep
3767 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
3768 autocmd! QuickFixCmdPost
3769
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003770 call delete('Xerr')
3771 call delete('Xtestfile1')
3772 call delete('Xtestfile2')
3773endfunc
3774
3775func Test_autocmd_changelist()
3776 call Xautocmd_changelist('c')
3777 call Xautocmd_changelist('l')
3778endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003779
3780" Tests for the ':filter /pat/ clist' command
3781func Test_filter_clist()
3782 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
3783 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
3784 \ split(execute('filter /Line 15/ clist'), "\n"))
3785 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
3786 \ split(execute('filter /Xfile1/ clist'), "\n"))
3787 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
3788
3789 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
3790 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
3791 call assert_equal([' 2 pqr:pat2: '],
3792 \ split(execute('filter /pqr/ clist'), "\n"))
3793 call assert_equal([' 1 abc:pat1: '],
3794 \ split(execute('filter /pat1/ clist'), "\n"))
3795endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003796
3797" Tests for the "CTRL-W <CR>" command.
3798func Xview_result_split_tests(cchar)
3799 call s:setup_commands(a:cchar)
3800
3801 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
3802 call g:Xsetlist([])
3803 Xopen
3804 let l:win_count = winnr('$')
3805 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42')
3806 call assert_equal(l:win_count, winnr('$'))
3807 Xclose
3808endfunc
3809
3810func Test_view_result_split()
3811 call Xview_result_split_tests('c')
3812 call Xview_result_split_tests('l')
3813endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003814
3815" Test that :cc sets curswant
3816func Test_curswant()
3817 helpgrep quickfix
3818 normal! llll
3819 1cc
3820 call assert_equal(getcurpos()[4], virtcol('.'))
3821 cclose | helpclose
3822endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01003823
3824" Test for opening a file from the quickfix window using CTRL-W <Enter>
3825" doesn't leave an empty buffer around.
3826func Test_splitview()
3827 call s:create_test_file('Xtestfile1')
3828 call s:create_test_file('Xtestfile2')
3829 new | only
3830 let last_bufnr = bufnr('Test_sv_1', 1)
3831 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
3832 cgetexpr l
3833 copen
3834 let numbufs = len(getbufinfo())
3835 exe "normal \<C-W>\<CR>"
3836 copen
3837 exe "normal j\<C-W>\<CR>"
3838 " Make sure new empty buffers are not created
3839 call assert_equal(numbufs, len(getbufinfo()))
3840 " Creating a new buffer should use the next available buffer number
3841 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
3842 bwipe Test_sv_1
3843 bwipe Test_sv_2
3844 new | only
3845
3846 " When split opening files from location list window, make sure that two
3847 " windows doesn't refer to the same location list
3848 lgetexpr l
3849 let locid = getloclist(0, {'id' : 0}).id
3850 lopen
3851 exe "normal \<C-W>\<CR>"
3852 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
3853 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
3854 new | only
3855
3856 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003857 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003858 lhelpgrep window
3859 let locid = getloclist(0, {'id' : 0}).id
3860 lwindow
3861 exe "normal j\<C-W>\<CR>"
3862 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
3863 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
3864 new | only
3865
3866 call delete('Xtestfile1')
3867 call delete('Xtestfile2')
3868endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003869
3870" Test for parsing entries using visual screen column
3871func Test_viscol()
3872 enew
3873 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
3874 edit Xfile1
3875
3876 " Use byte offset for column number
3877 set efm&
3878 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
3879 call assert_equal([5, 8], [col('.'), virtcol('.')])
3880 cnext
3881 call assert_equal([9, 12], [col('.'), virtcol('.')])
3882 cnext
3883 call assert_equal([14, 20], [col('.'), virtcol('.')])
3884
3885 " Use screen column offset for column number
3886 set efm=%f:%l:%v:%m
3887 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
3888 call assert_equal([5, 8], [col('.'), virtcol('.')])
3889 cnext
3890 call assert_equal([9, 12], [col('.'), virtcol('.')])
3891 cnext
3892 call assert_equal([14, 20], [col('.'), virtcol('.')])
3893 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
3894 call assert_equal([5, 8], [col('.'), virtcol('.')])
3895 cnext
3896 call assert_equal([10, 16], [col('.'), virtcol('.')])
3897 cnext
3898 call assert_equal([14, 20], [col('.'), virtcol('.')])
3899
3900 enew
3901 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
3902
3903 " Use byte offset for column number
3904 set efm&
3905 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
3906 call assert_equal([8, 10], [col('.'), virtcol('.')])
3907 cnext
3908 call assert_equal([11, 17], [col('.'), virtcol('.')])
3909 cnext
3910 call assert_equal([16, 25], [col('.'), virtcol('.')])
3911
3912 " Use screen column offset for column number
3913 set efm=%f:%l:%v:%m
3914 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
3915 call assert_equal([8, 10], [col('.'), virtcol('.')])
3916 cnext
3917 call assert_equal([11, 17], [col('.'), virtcol('.')])
3918 cnext
3919 call assert_equal([16, 25], [col('.'), virtcol('.')])
3920
3921 enew | only
3922 set efm&
3923 call delete('Xfile1')
3924endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003925
3926" Test for the quickfix window buffer
3927func Xqfbuf_test(cchar)
3928 call s:setup_commands(a:cchar)
3929
3930 " Quickfix buffer should be reused across closing and opening a quickfix
3931 " window
3932 Xexpr "F1:10:Line10"
3933 Xopen
3934 let qfbnum = bufnr('')
3935 Xclose
3936 " Even after the quickfix window is closed, the buffer should be loaded
3937 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003938 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003939 Xopen
3940 " Buffer should be reused when opening the window again
3941 call assert_equal(qfbnum, bufnr(''))
3942 Xclose
3943
3944 if a:cchar == 'l'
3945 %bwipe
3946 " For a location list, when both the file window and the location list
3947 " window for the list are closed, then the buffer should be freed.
3948 new | only
3949 lexpr "F1:10:Line10"
3950 let wid = win_getid()
3951 lopen
3952 let qfbnum = bufnr('')
3953 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
3954 close
3955 " When the location list window is closed, the buffer name should not
3956 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003957 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003958 call assert_true(bufloaded(qfbnum))
3959
Bram Moolenaard82a81c2019-03-02 07:57:18 +01003960 " After deleting a location list buffer using ":bdelete", opening the
3961 " location list window should mark the buffer as a location list buffer.
3962 exe "bdelete " . qfbnum
3963 lopen
3964 call assert_equal("quickfix", &buftype)
3965 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
3966 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
3967 call assert_false(&swapfile)
3968 lclose
3969
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003970 " When the location list is cleared for the window, the buffer should be
3971 " removed
3972 call setloclist(0, [], 'f')
3973 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003974 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003975
3976 " When the location list is freed with the location list window open, the
3977 " location list buffer should not be lost. It should be reused when the
3978 " location list is again populated.
3979 lexpr "F1:10:Line10"
3980 lopen
3981 let wid = win_getid()
3982 let qfbnum = bufnr('')
3983 wincmd p
3984 call setloclist(0, [], 'f')
3985 lexpr "F1:10:Line10"
3986 lopen
3987 call assert_equal(wid, win_getid())
3988 call assert_equal(qfbnum, bufnr(''))
3989 lclose
3990
3991 " When the window with the location list is closed, the buffer should be
3992 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003993 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003994 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003995 endif
3996endfunc
3997
3998func Test_qfbuf()
3999 call Xqfbuf_test('c')
4000 call Xqfbuf_test('l')
4001endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004002
4003" If there is an autocmd to use only one window, then opening the location
4004" list window used to crash Vim.
4005func Test_winonly_autocmd()
4006 call s:create_test_file('Xtest1')
4007 " Autocmd to show only one Vim window at a time
4008 autocmd WinEnter * only
4009 new
4010 " Load the location list
4011 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
4012 let loclistid = getloclist(0, {'id' : 0}).id
4013 " Open the location list window. Only this window will be shown and the file
4014 " window is closed.
4015 lopen
4016 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4017 " Jump to an entry in the location list and make sure that the cursor is
4018 " positioned correctly.
4019 ll 3
4020 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4021 call assert_equal('Xtest1', bufname(''))
4022 call assert_equal(15, line('.'))
4023 " Cleanup
4024 autocmd! WinEnter
4025 new | only
4026 call delete('Xtest1')
4027endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02004028
4029" Test to make sure that an empty quickfix buffer is not reused for loading
4030" a normal buffer.
4031func Test_empty_qfbuf()
4032 enew | only
4033 call writefile(["Test"], 'Xfile1')
4034 call setqflist([], 'f')
4035 copen | only
4036 let qfbuf = bufnr('')
4037 edit Xfile1
4038 call assert_notequal(qfbuf, bufnr(''))
4039 enew
4040 call delete('Xfile1')
4041endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004042
4043" Test for the :cbelow, :cabove, :lbelow and :labove commands.
4044func Xtest_below(cchar)
4045 call s:setup_commands(a:cchar)
4046
4047 " No quickfix/location list
4048 call assert_fails('Xbelow', 'E42:')
4049 call assert_fails('Xabove', 'E42:')
4050
4051 " Empty quickfix/location list
4052 call g:Xsetlist([])
4053 call assert_fails('Xbelow', 'E42:')
4054 call assert_fails('Xabove', 'E42:')
4055
4056 call s:create_test_file('X1')
4057 call s:create_test_file('X2')
4058 call s:create_test_file('X3')
4059 call s:create_test_file('X4')
4060
4061 " Invalid entries
4062 edit X1
4063 call g:Xsetlist(["E1", "E2"])
4064 call assert_fails('Xbelow', 'E42:')
4065 call assert_fails('Xabove', 'E42:')
4066 call assert_fails('3Xbelow', 'E42:')
4067 call assert_fails('4Xabove', 'E42:')
4068
4069 " Test the commands with various arguments
4070 Xexpr ["X1:5:L5", "X2:5:L5", "X2:10:L10", "X2:15:L15", "X3:3:L3"]
4071 edit +7 X2
4072 Xabove
4073 call assert_equal(['X2', 5], [bufname(''), line('.')])
4074 call assert_fails('Xabove', 'E553:')
4075 normal 2j
4076 Xbelow
4077 call assert_equal(['X2', 10], [bufname(''), line('.')])
4078 " Last error in this file
4079 Xbelow 99
4080 call assert_equal(['X2', 15], [bufname(''), line('.')])
4081 call assert_fails('Xbelow', 'E553:')
4082 " First error in this file
4083 Xabove 99
4084 call assert_equal(['X2', 5], [bufname(''), line('.')])
4085 call assert_fails('Xabove', 'E553:')
4086 normal gg
4087 Xbelow 2
4088 call assert_equal(['X2', 10], [bufname(''), line('.')])
4089 normal G
4090 Xabove 2
4091 call assert_equal(['X2', 10], [bufname(''), line('.')])
4092 edit X4
4093 call assert_fails('Xabove', 'E42:')
4094 call assert_fails('Xbelow', 'E42:')
4095 if a:cchar == 'l'
4096 " If a buffer has location list entries from some other window but not
4097 " from the current window, then the commands should fail.
4098 edit X1 | split | call setloclist(0, [], 'f')
4099 call assert_fails('Xabove', 'E776:')
4100 call assert_fails('Xbelow', 'E776:')
4101 close
4102 endif
4103
4104 " Test for lines with multiple quickfix entries
4105 Xexpr ["X1:5:L5", "X2:5:1:L5_1", "X2:5:2:L5_2", "X2:5:3:L5_3",
4106 \ "X2:10:1:L10_1", "X2:10:2:L10_2", "X2:10:3:L10_3",
4107 \ "X2:15:1:L15_1", "X2:15:2:L15_2", "X2:15:3:L15_3", "X3:3:L3"]
4108 edit +1 X2
4109 Xbelow 2
4110 call assert_equal(['X2', 10, 1], [bufname(''), line('.'), col('.')])
4111 normal gg
4112 Xbelow 99
4113 call assert_equal(['X2', 15, 1], [bufname(''), line('.'), col('.')])
4114 normal G
4115 Xabove 2
4116 call assert_equal(['X2', 10, 1], [bufname(''), line('.'), col('.')])
4117 normal G
4118 Xabove 99
4119 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
4120 normal 10G
4121 Xabove
4122 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
4123 normal 10G
4124 Xbelow
4125 call assert_equal(['X2', 15, 1], [bufname(''), line('.'), col('.')])
4126
4127 " Invalid range
4128 if a:cchar == 'c'
4129 call assert_fails('-2cbelow', 'E553:')
4130 " TODO: should go to first error in the current line?
4131 0cabove
4132 else
4133 call assert_fails('-2lbelow', 'E553:')
4134 " TODO: should go to first error in the current line?
4135 0labove
4136 endif
4137
4138 call delete('X1')
4139 call delete('X2')
4140 call delete('X3')
4141 call delete('X4')
4142endfunc
4143
4144func Test_cbelow()
4145 call Xtest_below('c')
4146 call Xtest_below('l')
4147endfunc