blob: 312f2a3c9a7a398cc412169780d3644bbb898d88 [file] [log] [blame]
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001" Test for the quickfix feature.
Bram Moolenaarda59dd52016-01-05 21:59:58 +01002
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02003source check.vim
Bram Moolenaar62aec932022-01-29 21:45:34 +00004import './vim9.vim' as v9
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02005CheckFeature quickfix
Bram Moolenaarda59dd52016-01-05 21:59:58 +01006
Bram Moolenaare00fdf32019-09-15 19:09:42 +02007source screendump.vim
8
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01009set encoding=utf-8
10
Bram Moolenaar2b946c92016-11-12 18:14:44 +010011func s:setup_commands(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020012 if a:cchar == 'c'
13 command! -nargs=* -bang Xlist <mods>clist<bang> <args>
14 command! -nargs=* Xgetexpr <mods>cgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020015 command! -nargs=* Xaddexpr <mods>caddexpr <args>
Bram Moolenaar55b69262017-08-13 13:42:01 +020016 command! -nargs=* -count Xolder <mods><count>colder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020017 command! -nargs=* Xnewer <mods>cnewer <args>
Bram Moolenaarf9ae1542019-11-18 22:02:16 +010018 command! -nargs=* Xopen <mods> copen <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020019 command! -nargs=* Xwindow <mods>cwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020020 command! -nargs=* Xbottom <mods>cbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020021 command! -nargs=* Xclose <mods>cclose <args>
22 command! -nargs=* -bang Xfile <mods>cfile<bang> <args>
23 command! -nargs=* Xgetfile <mods>cgetfile <args>
24 command! -nargs=* Xaddfile <mods>caddfile <args>
25 command! -nargs=* -bang Xbuffer <mods>cbuffer<bang> <args>
26 command! -nargs=* Xgetbuffer <mods>cgetbuffer <args>
27 command! -nargs=* Xaddbuffer <mods>caddbuffer <args>
28 command! -nargs=* Xrewind <mods>crewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020029 command! -count -nargs=* -bang Xnext <mods><count>cnext<bang> <args>
30 command! -count -nargs=* -bang Xprev <mods><count>cprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020031 command! -nargs=* -bang Xfirst <mods>cfirst<bang> <args>
32 command! -nargs=* -bang Xlast <mods>clast<bang> <args>
Bram Moolenaar25190db2019-05-04 15:05:28 +020033 command! -count -nargs=* -bang Xnfile <mods><count>cnfile<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020034 command! -nargs=* -bang Xpfile <mods>cpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020035 command! -nargs=* Xexpr <mods>cexpr <args>
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +020036 command! -count=999 -nargs=* Xvimgrep <mods> <count>vimgrep <args>
Bram Moolenaarf9ae1542019-11-18 22:02:16 +010037 command! -nargs=* Xvimgrepadd <mods> vimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020038 command! -nargs=* Xgrep <mods> grep <args>
39 command! -nargs=* Xgrepadd <mods> grepadd <args>
40 command! -nargs=* Xhelpgrep helpgrep <args>
Bram Moolenaar74240d32017-12-10 15:26:15 +010041 command! -nargs=0 -count Xcc <count>cc
Bram Moolenaar3ff33112019-05-03 21:56:35 +020042 command! -count=1 -nargs=0 Xbelow <mods><count>cbelow
43 command! -count=1 -nargs=0 Xabove <mods><count>cabove
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +020044 command! -count=1 -nargs=0 Xbefore <mods><count>cbefore
45 command! -count=1 -nargs=0 Xafter <mods><count>cafter
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020046 let g:Xgetlist = function('getqflist')
47 let g:Xsetlist = function('setqflist')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020048 call setqflist([], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020049 else
50 command! -nargs=* -bang Xlist <mods>llist<bang> <args>
51 command! -nargs=* Xgetexpr <mods>lgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020052 command! -nargs=* Xaddexpr <mods>laddexpr <args>
Bram Moolenaar55b69262017-08-13 13:42:01 +020053 command! -nargs=* -count Xolder <mods><count>lolder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020054 command! -nargs=* Xnewer <mods>lnewer <args>
Bram Moolenaarf9ae1542019-11-18 22:02:16 +010055 command! -nargs=* Xopen <mods> lopen <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020056 command! -nargs=* Xwindow <mods>lwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020057 command! -nargs=* Xbottom <mods>lbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020058 command! -nargs=* Xclose <mods>lclose <args>
59 command! -nargs=* -bang Xfile <mods>lfile<bang> <args>
60 command! -nargs=* Xgetfile <mods>lgetfile <args>
61 command! -nargs=* Xaddfile <mods>laddfile <args>
62 command! -nargs=* -bang Xbuffer <mods>lbuffer<bang> <args>
63 command! -nargs=* Xgetbuffer <mods>lgetbuffer <args>
64 command! -nargs=* Xaddbuffer <mods>laddbuffer <args>
65 command! -nargs=* Xrewind <mods>lrewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020066 command! -count -nargs=* -bang Xnext <mods><count>lnext<bang> <args>
67 command! -count -nargs=* -bang Xprev <mods><count>lprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020068 command! -nargs=* -bang Xfirst <mods>lfirst<bang> <args>
69 command! -nargs=* -bang Xlast <mods>llast<bang> <args>
Bram Moolenaar25190db2019-05-04 15:05:28 +020070 command! -count -nargs=* -bang Xnfile <mods><count>lnfile<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020071 command! -nargs=* -bang Xpfile <mods>lpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020072 command! -nargs=* Xexpr <mods>lexpr <args>
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +020073 command! -count=999 -nargs=* Xvimgrep <mods> <count>lvimgrep <args>
Bram Moolenaarf9ae1542019-11-18 22:02:16 +010074 command! -nargs=* Xvimgrepadd <mods> lvimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020075 command! -nargs=* Xgrep <mods> lgrep <args>
76 command! -nargs=* Xgrepadd <mods> lgrepadd <args>
77 command! -nargs=* Xhelpgrep lhelpgrep <args>
Bram Moolenaar74240d32017-12-10 15:26:15 +010078 command! -nargs=0 -count Xcc <count>ll
Bram Moolenaar3ff33112019-05-03 21:56:35 +020079 command! -count=1 -nargs=0 Xbelow <mods><count>lbelow
80 command! -count=1 -nargs=0 Xabove <mods><count>labove
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +020081 command! -count=1 -nargs=0 Xbefore <mods><count>lbefore
82 command! -count=1 -nargs=0 Xafter <mods><count>lafter
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020083 let g:Xgetlist = function('getloclist', [0])
84 let g:Xsetlist = function('setloclist', [0])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020085 call setloclist(0, [], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020086 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +010087endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020088
Bram Moolenaara5d78d12021-12-15 12:28:22 +000089" This must be run before any error lists are created.
90func Test_AA_cc_no_errors()
91 call assert_fails('cc', 'E42:')
92 call assert_fails('ll', 'E42:')
93endfunc
94
Bram Moolenaarda59dd52016-01-05 21:59:58 +010095" Tests for the :clist and :llist commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +010096func XlistTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020097 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +010098
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020099 if a:cchar == 'l'
100 call assert_fails('llist', 'E776:')
101 endif
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100102 " With an empty list, command should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200103 Xgetexpr []
104 silent! Xlist
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100105 call assert_true(v:errmsg ==# 'E42: No Errors')
106
107 " Populate the list and then try
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100108 let lines =<< trim END
109 non-error 1
110 Xtestfile1:1:3:Line1
111 non-error 2
112 Xtestfile2:2:2:Line2
113 non-error| 3
114 Xtestfile3:3:1:Line3
115 END
116 Xgetexpr lines
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100117
118 " List only valid entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100119 let l = split(execute('Xlist', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100120 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
121 \ ' 4 Xtestfile2:2 col 2: Line2',
122 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
123
124 " List all the entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100125 let l = split(execute('Xlist!', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100126 call assert_equal([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1',
127 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2',
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100128 \ ' 5: non-error| 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100129
130 " List a range of errors
Bram Moolenaaree85df32017-03-19 14:19:50 +0100131 let l = split(execute('Xlist 3,6', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100132 call assert_equal([' 4 Xtestfile2:2 col 2: Line2',
133 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
134
Bram Moolenaaree85df32017-03-19 14:19:50 +0100135 let l = split(execute('Xlist! 3,4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100136 call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
137
Bram Moolenaaree85df32017-03-19 14:19:50 +0100138 let l = split(execute('Xlist -6,-4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100139 call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l)
140
Bram Moolenaaree85df32017-03-19 14:19:50 +0100141 let l = split(execute('Xlist! -5,-3', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100142 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
143 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100144
145 " Test for '+'
Bram Moolenaaree85df32017-03-19 14:19:50 +0100146 let l = split(execute('Xlist! +2', ''), "\n")
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100147 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
148 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaaree85df32017-03-19 14:19:50 +0100149
thinca6864efa2021-06-19 20:45:20 +0200150 " Ranged entries
151 call g:Xsetlist([{'lnum':10,'text':'Line1'},
152 \ {'lnum':20,'col':10,'text':'Line2'},
153 \ {'lnum':30,'col':15,'end_col':20,'text':'Line3'},
154 \ {'lnum':40,'end_lnum':45,'text':'Line4'},
155 \ {'lnum':50,'end_lnum':55,'col':15,'text':'Line5'},
156 \ {'lnum':60,'end_lnum':65,'col':25,'end_col':35,'text':'Line6'}])
157 let l = split(execute('Xlist', ""), "\n")
158 call assert_equal([' 1:10: Line1',
159 \ ' 2:20 col 10: Line2',
160 \ ' 3:30 col 15-20: Line3',
161 \ ' 4:40-45: Line4',
162 \ ' 5:50-55 col 15: Line5',
163 \ ' 6:60-65 col 25-35: Line6'], l)
164
Bram Moolenaaree85df32017-03-19 14:19:50 +0100165 " Different types of errors
166 call g:Xsetlist([{'lnum':10,'col':5,'type':'W', 'text':'Warning','nr':11},
167 \ {'lnum':20,'col':10,'type':'e','text':'Error','nr':22},
168 \ {'lnum':30,'col':15,'type':'i','text':'Info','nr':33},
169 \ {'lnum':40,'col':20,'type':'x', 'text':'Other','nr':44},
170 \ {'lnum':50,'col':25,'type':"\<C-A>",'text':'one','nr':55}])
171 let l = split(execute('Xlist', ""), "\n")
172 call assert_equal([' 1:10 col 5 warning 11: Warning',
173 \ ' 2:20 col 10 error 22: Error',
174 \ ' 3:30 col 15 info 33: Info',
175 \ ' 4:40 col 20 x 44: Other',
176 \ ' 5:50 col 25 55: one'], l)
177
Bram Moolenaard76ce852018-05-01 15:02:04 +0200178 " Test for module names, one needs to explicitly set `'valid':v:true` so
179 call g:Xsetlist([
180 \ {'lnum':10,'col':5,'type':'W','module':'Data.Text','text':'ModuleWarning','nr':11,'valid':v:true},
181 \ {'lnum':20,'col':10,'type':'W','module':'Data.Text','filename':'Data/Text.hs','text':'ModuleWarning','nr':22,'valid':v:true},
182 \ {'lnum':30,'col':15,'type':'W','filename':'Data/Text.hs','text':'FileWarning','nr':33,'valid':v:true}])
183 let l = split(execute('Xlist', ""), "\n")
184 call assert_equal([' 1 Data.Text:10 col 5 warning 11: ModuleWarning',
185 \ ' 2 Data.Text:20 col 10 warning 22: ModuleWarning',
186 \ ' 3 Data/Text.hs:30 col 15 warning 33: FileWarning'], l)
187
Bram Moolenaar5f30e262022-07-28 11:56:01 +0100188 " Very long line should be displayed.
189 let text = 'Line' .. repeat('1234567890', 130)
190 let lines = ['Xtestfile9:2:9:' .. text]
191 Xgetexpr lines
192
193 let l = split(execute('Xlist', ''), "\n")
194 call assert_equal([' 1 Xtestfile9:2 col 9: ' .. text] , l)
195
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100196 " For help entries in the quickfix list, only the filename without directory
197 " should be displayed
198 Xhelpgrep setqflist()
199 let l = split(execute('Xlist 1', ''), "\n")
200 call assert_match('^ 1 [^\\/]\{-}:', l[0])
201
Bram Moolenaaree85df32017-03-19 14:19:50 +0100202 " Error cases
203 call assert_fails('Xlist abc', 'E488:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100204endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100205
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100206func Test_clist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100207 call XlistTests('c')
208 call XlistTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100209endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100210
211" Tests for the :colder, :cnewer, :lolder and :lnewer commands
212" Note that this test assumes that a quickfix/location list is
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100213" already set by the caller.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100214func XageTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200215 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100216
Bram Moolenaar87f59b02019-04-04 14:04:11 +0200217 if a:cchar == 'l'
218 " No location list for the current window
219 call assert_fails('lolder', 'E776:')
220 call assert_fails('lnewer', 'E776:')
221 endif
222
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200223 let list = [{'bufnr': bufnr('%'), 'lnum': 1}]
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200224 call g:Xsetlist(list)
225
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100226 " Jumping to a non existent list should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200227 silent! Xolder 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100228 call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
229
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200230 silent! Xnewer 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100231 call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
232
233 " Add three quickfix/location lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200234 Xgetexpr ['Xtestfile1:1:3:Line1']
235 Xgetexpr ['Xtestfile2:2:2:Line2']
236 Xgetexpr ['Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100237
238 " Go back two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200239 Xolder
240 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100241 call assert_equal('Line2', l[0].text)
242
243 " Go forward two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200244 Xnewer
245 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100246 call assert_equal('Line3', l[0].text)
247
248 " Test for the optional count argument
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200249 Xolder 2
250 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100251 call assert_equal('Line1', l[0].text)
252
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200253 Xnewer 2
254 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100255 call assert_equal('Line3', l[0].text)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100256endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100257
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100258func Test_cage()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100259 call XageTests('c')
260 call XageTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100261endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100262
263" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
264" commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100265func XwindowTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200266 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100267
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200268 " Opening the location list window without any errors should fail
269 if a:cchar == 'l'
270 call assert_fails('lopen', 'E776:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000271 call assert_fails('lwindow', 'E776:')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200272 endif
273
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100274 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200275 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100276
277 " Quickfix/Location window should not open with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200278 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100279 call assert_true(winnr('$') == 1)
280
281 " Create a list with valid entries
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100282 let lines =<< trim END
283 Xtestfile1:1:3:Line1
284 Xtestfile2:2:2:Line2
285 Xtestfile3:3:1:Line3
286 END
287 Xgetexpr lines
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100288
289 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200290 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100291 call assert_true(winnr('$') == 2 && winnr() == 2 &&
292 \ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
Bram Moolenaar537ef082016-07-09 17:56:19 +0200293 redraw!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100294
295 " Close the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200296 Xclose
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100297 call assert_true(winnr('$') == 1)
298
299 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200300 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100301
302 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200303 Xopen 5
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100304 call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
Bram Moolenaarc05d1c02020-09-04 18:38:06 +0200305 \ && winheight(0) == 5)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100306
307 " Opening the window again, should move the cursor to that window
308 wincmd t
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200309 Xopen 7
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100310 call assert_true(winnr('$') == 2 && winnr() == 2 &&
Bram Moolenaarc05d1c02020-09-04 18:38:06 +0200311 \ winheight(0) == 7 &&
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100312 \ getline('.') ==# '|| non-error 1')
313
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100314 " :cnext in quickfix window should move to the next entry
315 Xnext
316 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100317
318 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200319 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100320 call assert_true(winnr('$') == 1)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200321
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100322 " Specifying the width should adjust the width for a vertically split
323 " quickfix window.
324 vert Xopen
325 call assert_equal(10, winwidth(0))
326 vert Xopen 12
327 call assert_equal(12, winwidth(0))
328 Xclose
329
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200330 " Horizontally or vertically splitting the quickfix window should create a
331 " normal window/buffer
332 Xopen
333 wincmd s
334 call assert_equal(0, getwininfo(win_getid())[0].quickfix)
335 call assert_equal(0, getwininfo(win_getid())[0].loclist)
336 call assert_notequal('quickfix', &buftype)
337 close
338 Xopen
339 wincmd v
340 call assert_equal(0, getwininfo(win_getid())[0].quickfix)
341 call assert_equal(0, getwininfo(win_getid())[0].loclist)
342 call assert_notequal('quickfix', &buftype)
343 close
344 Xopen
345 Xclose
346
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200347 if a:cchar == 'c'
348 " Opening the quickfix window in multiple tab pages should reuse the
349 " quickfix buffer
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100350 let lines =<< trim END
351 Xtestfile1:1:3:Line1
352 Xtestfile2:2:2:Line2
353 Xtestfile3:3:1:Line3
354 END
355 Xgetexpr lines
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200356 Xopen
357 let qfbufnum = bufnr('%')
358 tabnew
359 Xopen
360 call assert_equal(qfbufnum, bufnr('%'))
361 new | only | tabonly
362 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100363endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100364
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100365func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100366 call XwindowTests('c')
367 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100368endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100369
Bram Moolenaar36d50222019-05-02 20:17:40 +0200370func Test_copenHeight()
371 copen
372 wincmd H
373 let height = winheight(0)
374 copen 10
375 call assert_equal(height, winheight(0))
376 quit
377endfunc
378
Bram Moolenaar1142a312019-10-16 14:51:39 +0200379func Test_copenHeight_tabline()
380 set tabline=foo showtabline=2
381 copen
382 wincmd H
383 let height = winheight(0)
384 copen 10
385 call assert_equal(height, winheight(0))
386 quit
387 set tabline& showtabline&
388endfunc
389
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100390" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
391" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100392func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200393 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100394
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100395 let lines =<< trim END
396 Xtestfile1:700:10:Line 700
397 Xtestfile2:800:15:Line 800
398 END
399 call writefile(lines, 'Xqftestfile1')
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100400
401 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200402 Xfile Xqftestfile1
403 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100404 call assert_true(len(l) == 2 &&
405 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
406 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
407
Bram Moolenaar049cba92016-06-26 14:38:04 +0200408 " Test with a non existent file
Bram Moolenaare2e40752020-09-04 21:18:46 +0200409 call assert_fails('Xfile non_existent_file', 'E40:')
Bram Moolenaar049cba92016-06-26 14:38:04 +0200410
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100411 " Run cfile/lfile from a modified buffer
412 enew!
413 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200414 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100415 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
416
417 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200418 Xaddfile Xqftestfile1
419 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100420 call assert_true(len(l) == 3 &&
421 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
422
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100423 let lines =<< trim END
424 Xtestfile1:222:77:Line 222
425 Xtestfile2:333:88:Line 333
426 END
427 call writefile(lines, 'Xqftestfile1')
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100428
429 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200430 Xgetfile Xqftestfile1
431 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100432 call assert_true(len(l) == 2 &&
433 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
434 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
435
Bram Moolenaar70077dd2019-11-24 12:12:42 +0100436 " Test for a file with a long line and without a newline at the end
437 let text = repeat('x', 1024)
438 let t = 'a.txt:18:' . text
439 call writefile([t], 'Xqftestfile1', 'b')
440 silent! Xfile Xqftestfile1
441 call assert_equal(text, g:Xgetlist()[0].text)
442
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100443 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100444endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100445
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100446func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100447 call XfileTests('c')
448 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100449endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100450
451" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
452" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100453func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200454 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100455
456 enew!
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100457 let lines =<< trim END
458 Xtestfile7:700:10:Line 700
459 Xtestfile8:800:15:Line 800
460 END
461 silent! call setline(1, lines)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200462 Xbuffer!
463 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100464 call assert_true(len(l) == 2 &&
465 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
466 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
467
468 enew!
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100469 let lines =<< trim END
470 Xtestfile9:900:55:Line 900
471 Xtestfile10:950:66:Line 950
472 END
473 silent! call setline(1, lines)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200474 Xgetbuffer
475 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100476 call assert_true(len(l) == 2 &&
477 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
478 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
479
480 enew!
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100481 let lines =<< trim END
482 Xtestfile11:700:20:Line 700
483 Xtestfile12:750:25:Line 750
484 END
485 silent! call setline(1, lines)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200486 Xaddbuffer
487 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100488 call assert_true(len(l) == 4 &&
489 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
490 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
491 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200492 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100493
Bram Moolenaaree85df32017-03-19 14:19:50 +0100494 " Check for invalid buffer
495 call assert_fails('Xbuffer 199', 'E474:')
496
497 " Check for unloaded buffer
498 edit Xtestfile1
499 let bnr = bufnr('%')
500 enew!
501 call assert_fails('Xbuffer ' . bnr, 'E681:')
502
503 " Check for invalid range
504 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
505 " commands. So directly call the commands.
506 if (a:cchar == 'c')
507 call assert_fails('900,999cbuffer', 'E16:')
508 else
509 call assert_fails('900,999lbuffer', 'E16:')
510 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100511endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100512
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100513func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100514 call XbufferTests('c')
515 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100516endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100517
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100518func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200519 call s:setup_commands(a:cchar)
520
521 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100522endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200523
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100524func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200525 call XexprTests('c')
526 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100527endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200528
529" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100530func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200531 call s:setup_commands(a:cchar)
532
Bram Moolenaar74240d32017-12-10 15:26:15 +0100533 call g:Xsetlist([], 'f')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200534 " Jumping to first or next location list entry without any error should
535 " result in failure
Bram Moolenaar74240d32017-12-10 15:26:15 +0100536 if a:cchar == 'c'
537 let err = 'E42:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100538 let cmd = '$cc'
Bram Moolenaar74240d32017-12-10 15:26:15 +0100539 else
540 let err = 'E776:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100541 let cmd = '$ll'
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200542 endif
Bram Moolenaar74240d32017-12-10 15:26:15 +0100543 call assert_fails('Xnext', err)
544 call assert_fails('Xprev', err)
545 call assert_fails('Xnfile', err)
546 call assert_fails('Xpfile', err)
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100547 call assert_fails(cmd, err)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200548
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100549 Xexpr ''
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100550 call assert_fails(cmd, 'E42:')
551
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200552 call s:create_test_file('Xqftestfile1')
553 call s:create_test_file('Xqftestfile2')
554
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100555 let lines =<< trim END
556 Xqftestfile1:5:Line5
557 Xqftestfile1:6:Line6
558 Xqftestfile2:10:Line10
559 Xqftestfile2:11:Line11
560 RegularLine1
561 RegularLine2
562 END
563 Xgetexpr lines
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200564
565 Xfirst
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100566 call assert_fails('-5Xcc', 'E16:')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200567 call assert_fails('Xprev', 'E553:')
568 call assert_fails('Xpfile', 'E553:')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200569 Xnfile
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200570 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200571 call assert_equal(10, line('.'))
572 Xpfile
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200573 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200574 call assert_equal(6, line('.'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100575 5Xcc
576 call assert_equal(5, g:Xgetlist({'idx':0}).idx)
577 2Xcc
578 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100579 if a:cchar == 'c'
580 cc
581 else
582 ll
583 endif
584 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100585 10Xcc
586 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200587 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200588 Xprev
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200589 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200590 call assert_equal(11, line('.'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200591 call assert_fails('Xnext', 'E553:')
592 call assert_fails('Xnfile', 'E553:')
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100593 " To process the range using quickfix list entries, directly use the
594 " quickfix commands (don't use the user defined commands)
595 if a:cchar == 'c'
596 $cc
597 else
598 $ll
599 endif
600 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200601 Xrewind
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200602 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200603 call assert_equal(5, line('.'))
604
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200605 10Xnext
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200606 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200607 call assert_equal(11, line('.'))
608 10Xprev
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200609 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200610 call assert_equal(5, line('.'))
611
Bram Moolenaar74240d32017-12-10 15:26:15 +0100612 " Jumping to an error from the error window using cc command
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100613 let lines =<< trim END
614 Xqftestfile1:5:Line5
615 Xqftestfile1:6:Line6
616 Xqftestfile2:10:Line10
617 Xqftestfile2:11:Line11
618 END
619 Xgetexpr lines
Bram Moolenaar74240d32017-12-10 15:26:15 +0100620 Xopen
621 10Xcc
622 call assert_equal(11, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200623 call assert_equal('Xqftestfile2', @%)
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100624 Xopen
625 call cursor(2, 1)
626 if a:cchar == 'c'
627 .cc
628 else
629 .ll
630 endif
631 call assert_equal(6, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200632 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100633
634 " Jumping to an error from the error window (when only the error window is
635 " present)
636 Xopen | only
637 Xlast 1
638 call assert_equal(5, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200639 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100640
Bram Moolenaaree85df32017-03-19 14:19:50 +0100641 Xexpr ""
642 call assert_fails('Xnext', 'E42:')
643
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200644 call delete('Xqftestfile1')
645 call delete('Xqftestfile2')
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200646
647 " Should be able to use next/prev with invalid entries
648 Xexpr ""
649 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
650 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100651 Xaddexpr ['foo', 'bar', 'baz', 'quux', 'sh|moo']
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200652 call assert_equal(5, g:Xgetlist({'size' : 0}).size)
653 Xlast
654 call assert_equal(5, g:Xgetlist({'idx' : 0}).idx)
655 Xfirst
656 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
657 2Xnext
658 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100659endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200660
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100661func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200662 call Xtest_browse('c')
663 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100664endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200665
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000666" Test for memory allocation failures
667func Xnomem_tests(cchar)
668 call s:setup_commands(a:cchar)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100669
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000670 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
671 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
672
673 call test_alloc_fail(GetAllocId('qf_dirname_now'), 0, 0)
674 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100675
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200676 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000677 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100678
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200679 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000680 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100681
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200682 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000683 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100684
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000685 call test_alloc_fail(GetAllocId('qf_efm_fmtstr'), 0, 0)
686 set efm=%f
687 call assert_fails('Xexpr ["Xfile1"]', 'E342:')
688 set efm&
689
690 call test_alloc_fail(GetAllocId('qf_efm_fmtpart'), 0, 0)
691 set efm=%f:%l:%m,%f-%l-%m
692 call assert_fails('Xaddexpr ["Xfile2", "Xfile3"]', 'E342:')
693 set efm&
694
695 call test_alloc_fail(GetAllocId('qf_title'), 0, 0)
696 call assert_fails('Xexpr ""', 'E342:')
697 call assert_equal('', g:Xgetlist({'all': 1}).title)
698
699 call test_alloc_fail(GetAllocId('qf_mef_name'), 0, 0)
700 set makeef=Xtmp##.err
701 call assert_fails('Xgrep needle haystack', 'E342:')
702 set makeef&
703
704 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
705 call assert_fails('Xexpr "Xfile1:10:Line10"', 'E342:')
706
707 if a:cchar == 'l'
708 for id in ['qf_qfline', 'qf_qfinfo']
709 lgetexpr ["Xfile1:10:L10", "Xfile2:20:L20"]
710 call test_alloc_fail(GetAllocId(id), 0, 0)
711 call assert_fails('new', 'E342:')
712 call assert_equal(2, winnr('$'))
713 call assert_equal([], getloclist(0))
714 %bw!
715 endfor
716 endif
717
718 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
719 try
720 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
721 catch /^Vim:Interrupt$/
722 endtry
723
724 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
725 try
726 call assert_fails('Xvimgrep /vim/f runtest.vim', 'E342:')
727 catch /^Vim:Interrupt$/
728 endtry
729
730 let l = getqflist({"lines": ["Xfile1:10:L10"]})
731 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
732 call assert_fails('call g:Xsetlist(l.items)', 'E342:')
733
734 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
735 try
736 call assert_fails('Xhelpgrep quickfix', 'E342:')
737 catch /^Vim:Interrupt$/
738 endtry
739
740 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
741 call assert_fails('let l = g:Xgetlist({"lines": ["Xfile1:10:L10"]})', 'E342:')
742 call assert_equal(#{items: []}, l)
743
744 if a:cchar == 'l'
745 call setqflist([], 'f')
746 call setloclist(0, [], 'f')
747 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
748 call assert_fails('lhelpgrep quickfix', 'E342:')
749 call assert_equal([], getloclist(0))
750
751 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
752 call assert_fails('lvimgrep vim runtest.vim', 'E342:')
753
754 let l = getqflist({"lines": ["Xfile1:10:L10"]})
755 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
756 call assert_fails('call setloclist(0, l.items)', 'E342:')
757
758 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
759 call assert_fails('lbuffer', 'E342:')
760
761 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
762 call assert_fails('lexpr ["Xfile1:10:L10", "Xfile2:20:L20"]', 'E342:')
763
764 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
765 call assert_fails('lfile runtest.vim', 'E342:')
766 endif
767
768 call test_alloc_fail(GetAllocId('qf_dirstack'), 0, 0)
769 set efm=%DEntering\ dir\ %f,%f:%l:%m
770 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E342:')
771 set efm&
772
773 call test_alloc_fail(GetAllocId('qf_dirstack'), 0, 0)
774 set efm=%+P[%f],(%l)%m
775 call assert_fails('Xexpr ["[runtest.vim]", "(1)Hello"]', 'E342:')
776 set efm&
777
778 call test_alloc_fail(GetAllocId('qf_multiline_pfx'), 0, 0)
779 set efm=%EError,%Cline\ %l,%Z%m
780 call assert_fails('Xexpr ["Error", "line 1", "msg"]', 'E342:')
781 set efm&
782
783 call test_alloc_fail(GetAllocId('qf_makecmd'), 0, 0)
784 call assert_fails('Xgrep vim runtest.vim', 'E342:')
785
786 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
787 call assert_fails('Xexpr repeat("a", 8192)', 'E342:')
788
789 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
790 call assert_fails('Xexpr [repeat("a", 8192)]', 'E342:')
791
792 new
793 call setline(1, repeat('a', 8192))
794 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
795 call assert_fails('Xbuffer', 'E342:')
796 %bw!
797
798 call writefile([repeat('a', 8192)], 'Xtest')
799 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
800 call assert_fails('Xfile Xtest', 'E342:')
801 call delete('Xtest')
802endfunc
803
804func Test_nomem()
805 call Xnomem_tests('c')
806 call Xnomem_tests('l')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100807endfunc
808
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100809func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200810 call s:setup_commands(a:cchar)
811 Xhelpgrep quickfix
812 Xopen
813 if a:cchar == 'c'
814 let title_text = ':helpgrep quickfix'
815 else
816 let title_text = ':lhelpgrep quickfix'
817 endif
818 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200819
820 " Jumping to a help topic should open the help window
821 only
822 Xnext
823 call assert_true(&buftype == 'help')
824 call assert_true(winnr('$') == 2)
825 " Jumping to the next match should reuse the help window
826 Xnext
827 call assert_true(&buftype == 'help')
828 call assert_true(winnr() == 1)
829 call assert_true(winnr('$') == 2)
830 " Jumping to the next match from the quickfix window should reuse the help
831 " window
832 Xopen
833 Xnext
834 call assert_true(&buftype == 'help')
835 call assert_true(winnr() == 1)
836 call assert_true(winnr('$') == 2)
thinca6864efa2021-06-19 20:45:20 +0200837 call assert_match('|\d\+ col \d\+-\d\+|', getbufline(winbufnr(2), 1)[0])
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200838
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100839 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200840 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100841
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100842 " When the current window is vertically split, jumping to a help match
843 " should open the help window at the top.
844 only | enew
845 let w1 = win_getid()
846 vert new
847 let w2 = win_getid()
848 Xnext
849 let w3 = win_getid()
850 call assert_true(&buftype == 'help')
851 call assert_true(winnr() == 1)
Bram Moolenaara106e6c2019-11-24 22:13:58 +0100852 " See jump_to_help_window() for details
853 let w2_width = winwidth(w2)
854 if w2_width != &columns && w2_width < 80
855 call assert_equal(['col', [['leaf', w3],
856 \ ['row', [['leaf', w2], ['leaf', w1]]]]], winlayout())
857 else
858 call assert_equal(['row', [['col', [['leaf', w3], ['leaf', w2]]],
859 \ ['leaf', w1]]] , winlayout())
860 endif
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100861
862 new | only
863 set buftype=help
864 set modified
865 call assert_fails('Xnext', 'E37:')
866 set nomodified
867 new | only
868
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200869 if a:cchar == 'l'
870 " When a help window is present, running :lhelpgrep should reuse the
871 " help window and not the current window
872 new | only
873 call g:Xsetlist([], 'f')
874 help index.txt
875 wincmd w
876 lhelpgrep quickfix
877 call assert_equal(1, winnr())
878 call assert_notequal([], getloclist(1))
879 call assert_equal([], getloclist(2))
880 endif
881
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200882 new | only
883
Bram Moolenaaree85df32017-03-19 14:19:50 +0100884 " Search for non existing help string
885 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar108e7b42018-10-11 17:39:12 +0200886 " Invalid regular expression
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200887 call assert_fails('Xhelpgrep \@<!', 'E866:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100888endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200889
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100890func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200891 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200892 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200893 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100894endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100895
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100896def Test_helpgrep_vim9_restore_cpo()
897 assert_equal('aABceFs', &cpo)
898
899 var rtp_save = &rtp
900 var dir = 'Xruntime/after'
901 &rtp ..= ',' .. dir
902 mkdir(dir .. '/ftplugin', 'p')
Bram Moolenaar39f3b142021-02-14 12:57:36 +0100903 writefile(['vim9script'], dir .. '/ftplugin/qf.vim')
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100904 filetype plugin on
905 silent helpgrep grail
906 cwindow
907 silent helpgrep grail
908
909 assert_equal('aABceFs', &cpo)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000910 delete('Xruntime', 'rf')
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100911 &rtp = rtp_save
912 cclose
913 helpclose
914enddef
915
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000916" When running the :helpgrep command, if an autocmd modifies the 'cpoptions'
917" value, then Vim crashes. (issue fixed by 7.2b-004 and 8.2.4453)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +0000918func Test_helpgrep_restore_cpo_aucmd()
919 let save_cpo = &cpo
920 augroup QF_Test
921 au!
922 autocmd BufNew * set cpo=acd
923 augroup END
924
925 helpgrep quickfix
926 call assert_equal('acd', &cpo)
927 %bw!
928
929 set cpo&vim
930 augroup QF_Test
931 au!
932 autocmd BufReadPost * set cpo=
933 augroup END
934
935 helpgrep buffer
936 call assert_equal('', &cpo)
937
938 augroup QF_Test
939 au!
940 augroup END
941 %bw!
942 let &cpo = save_cpo
943endfunc
944
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +0200945def Test_vim9_cexpr()
946 var text = 'somefile:95:error'
947 cexpr text
948 var l = getqflist()
949 assert_equal(1, l->len())
950 assert_equal(95, l[0].lnum)
951 assert_equal('error', l[0].text)
952
953 text = 'somefile:77:warning'
954 caddexpr text
955 l = getqflist()
956 assert_equal(2, l->len())
957 assert_equal(77, l[1].lnum)
958 assert_equal('warning', l[1].text)
959enddef
960
Bram Moolenaar6920c722016-01-22 22:44:10 +0100961func Test_errortitle()
962 augroup QfBufWinEnter
963 au!
964 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
965 augroup END
966 copen
967 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'')'}]
968 call setqflist(a)
969 call assert_equal(':setqflist()', g:a)
970 augroup QfBufWinEnter
971 au!
972 augroup END
973 augroup! QfBufWinEnter
974endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100975
Bram Moolenaar5584df62016-03-18 21:00:51 +0100976func Test_vimgreptitle()
977 augroup QfBufWinEnter
978 au!
979 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
980 augroup END
981 try
982 vimgrep /pattern/j file
983 catch /E480/
984 endtry
985 copen
986 call assert_equal(': vimgrep /pattern/j file', g:a)
987 augroup QfBufWinEnter
988 au!
989 augroup END
990 augroup! QfBufWinEnter
991endfunc
992
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100993func Test_bufwinenter_once()
994 augroup QfBufWinEnter
995 au!
996 au BufWinEnter * let g:got_afile ..= 'got ' .. expand('<afile>')
997 augroup END
998 let g:got_afile = ''
999 copen
1000 call assert_equal('got quickfix', g:got_afile)
1001
1002 cclose
1003 unlet g:got_afile
1004 augroup QfBufWinEnter
1005 au!
1006 augroup END
1007 augroup! QfBufWinEnter
1008endfunc
1009
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001010func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001011 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +01001012
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001013 Xgetexpr ['file:1:1:message']
1014 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +01001015 if a:cchar == 'c'
1016 call setqflist(l, 'r')
1017 else
1018 call setloclist(0, l, 'r')
1019 endif
1020
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001021 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +01001022 if a:cchar == 'c'
1023 let title = ':setqflist()'
1024 else
1025 let title = ':setloclist()'
1026 endif
1027 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001028 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001029endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +01001030
1031" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001032func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001033 call XqfTitleTests('c')
1034 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001035endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +01001036
1037" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001038func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +01001039 let save_efm = &efm
1040 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
1041 cgetexpr ['WWWW', 'EEEE', 'CCCC']
1042 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1043 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
1044 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
1045 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1046 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
1047 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
1048 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1049 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
1050 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001051endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001052
1053" This will test for problems in quickfix:
1054" A. incorrectly copying location lists which caused the location list to show
1055" a different name than the file that was actually being displayed.
1056" B. not reusing the window for which the location list window is opened but
1057" instead creating new windows.
1058" C. make sure that the location list window is not reused instead of the
1059" window it belongs to.
1060"
1061" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001062func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001063 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
1064 let word = substitute(base, '\v(.*)\..*', '\1', '')
1065
1066 setl modifiable
1067 setl noreadonly
1068 setl noswapfile
1069 setl bufhidden=delete
1070 %del _
1071 " For problem 2:
1072 " 'buftype' has to be set to reproduce the constant opening of new windows
1073 setl buftype=nofile
1074
1075 call setline(1, word)
1076
1077 setl nomodified
1078 setl nomodifiable
1079 setl readonly
1080 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001081endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001082
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001083func Test_locationlist()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001084 enew
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001085
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001086 augroup testgroup
1087 au!
1088 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
1089 augroup END
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001090
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001091 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001092
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001093 let qflist = []
1094 for word in words
1095 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
1096 " NOTE: problem 1:
1097 " intentionally not setting 'lnum' so that the quickfix entries are not
1098 " valid
1099 eval qflist->setloclist(0, ' ')
1100 endfor
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001101
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001102 " Test A
1103 lrewind
1104 enew
1105 lopen
1106 4lnext
1107 vert split
1108 wincmd L
1109 lopen
1110 wincmd p
1111 lnext
1112 let fileName = expand("%")
1113 wincmd p
1114 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
1115 let fileName = substitute(fileName, '\\', '/', 'g')
1116 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
1117 call assert_equal("test://bar.txt", fileName)
1118 call assert_equal("test://bar.txt", locationListFileName)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001119
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001120 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001121
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001122 " Test B:
1123 lrewind
1124 lopen
1125 2
1126 exe "normal \<CR>"
1127 wincmd p
1128 3
1129 exe "normal \<CR>"
1130 wincmd p
1131 4
1132 exe "normal \<CR>"
1133 call assert_equal(2, winnr('$'))
1134 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001135
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001136 " Test C:
1137 lrewind
1138 lopen
1139 " Let's move the location list window to the top to check whether it (the
1140 " first window found) will be reused when we try to open new windows:
1141 wincmd K
1142 2
1143 exe "normal \<CR>"
1144 wincmd p
1145 3
1146 exe "normal \<CR>"
1147 wincmd p
1148 4
1149 exe "normal \<CR>"
1150 1wincmd w
1151 call assert_equal('quickfix', &buftype)
1152 2wincmd w
1153 let bufferName = expand("%")
1154 let bufferName = substitute(bufferName, '\\', '/', 'g')
1155 call assert_equal('test://quux.txt', bufferName)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001156
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001157 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001158
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001159 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +01001160endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +01001161
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001162func Test_locationlist_curwin_was_closed()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001163 augroup testgroup
1164 au!
1165 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
1166 augroup END
Bram Moolenaar0899d692016-03-19 13:35:03 +01001167
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001168 func! R(n)
1169 quit
1170 endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +01001171
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001172 new
1173 let q = []
1174 call add(q, {'filename': 'test_curwin.txt' })
1175 call setloclist(0, q)
1176 call assert_fails('lrewind', 'E924:')
Bram Moolenaar0899d692016-03-19 13:35:03 +01001177
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001178 augroup! testgroup
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001179 delfunc R
Bram Moolenaaree85df32017-03-19 14:19:50 +01001180endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001181
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001182func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02001183 call writefile(['loclistfoo'], 'loclistfoo')
1184 call writefile(['loclistbar'], 'loclistbar')
1185 set switchbuf=usetab
1186
1187 edit loclistfoo
1188 tabedit loclistbar
1189 silent lgrep loclistfoo loclist*
1190 call assert_equal(1, tabpagenr())
1191
1192 enew | only | tabonly
1193 set switchbuf&vim
1194 call delete('loclistfoo')
1195 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001196endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02001197
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001198" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001199func Test_efm1()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001200 " The 'errorformat' setting is different on non-Unix systems.
1201 " This test works only on Unix-like systems.
1202 CheckUnix
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001203
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001204 let l =<< trim [DATA]
1205 "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.
1206 "Xtestfile", line 6 col 19; this is an error
1207 gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c
1208 Xtestfile:9: parse error before `asd'
1209 make: *** [vim] Error 1
1210 in file "Xtestfile" linenr 10: there is an error
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001211
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001212 2 returned
1213 "Xtestfile", line 11 col 1; this is an error
1214 "Xtestfile", line 12 col 2; this is another error
1215 "Xtestfile", line 14:10; this is an error in column 10
1216 =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time
1217 "Xtestfile", linenr 16: yet another problem
1218 Error in "Xtestfile" at line 17:
1219 x should be a dot
1220 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
1221 ^
1222 Error in "Xtestfile" at line 18:
1223 x should be a dot
1224 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
1225 .............^
1226 Error in "Xtestfile" at line 19:
1227 x should be a dot
1228 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
1229 --------------^
1230 Error in "Xtestfile" at line 20:
1231 x should be a dot
1232 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
1233 ^
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001234
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001235 Does anyone know what is the problem and how to correction it?
1236 "Xtestfile", line 21 col 9: What is the title of the quickfix window?
1237 "Xtestfile", line 22 col 9: What is the title of the quickfix window?
1238 [DATA]
1239
1240 call writefile(l, 'Xerrorfile1')
1241 call writefile(l[:-2], 'Xerrorfile2')
1242
1243 let m =<< [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001244 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2
1245 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3
1246 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4
1247 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5
1248 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6
1249 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7
1250 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8
1251 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9
1252 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10
1253 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11
1254 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12
1255 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13
1256 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14
1257 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15
1258 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16
1259 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
1260 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
1261 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
1262 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
1263 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21
1264 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22
1265[DATA]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001266 call writefile(m, 'Xtestfile')
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001267
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001268 let save_efm = &efm
1269 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
1270 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001271
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001272 exe 'cf Xerrorfile2'
1273 clast
1274 copen
1275 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
1276 wincmd p
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001277
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001278 exe 'cf Xerrorfile1'
1279 call assert_equal([4, 12], [line('.'), col('.')])
1280 cn
1281 call assert_equal([6, 19], [line('.'), col('.')])
1282 cn
1283 call assert_equal([9, 2], [line('.'), col('.')])
1284 cn
1285 call assert_equal([10, 2], [line('.'), col('.')])
1286 cn
1287 call assert_equal([11, 1], [line('.'), col('.')])
1288 cn
1289 call assert_equal([12, 2], [line('.'), col('.')])
1290 cn
1291 call assert_equal([14, 10], [line('.'), col('.')])
1292 cn
1293 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
1294 cn
1295 call assert_equal([16, 2], [line('.'), col('.')])
1296 cn
1297 call assert_equal([17, 6], [line('.'), col('.')])
1298 cn
1299 call assert_equal([18, 7], [line('.'), col('.')])
1300 cn
1301 call assert_equal([19, 8], [line('.'), col('.')])
1302 cn
1303 call assert_equal([20, 9], [line('.'), col('.')])
1304 clast
1305 cprev
1306 cprev
1307 wincmd w
1308 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
1309 wincmd p
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001310
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001311 let &efm = save_efm
1312 call delete('Xerrorfile1')
1313 call delete('Xerrorfile2')
1314 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +01001315endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001316
Bram Moolenaarab47c612016-06-14 22:02:26 +02001317" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001318func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001319 call s:setup_commands(a:cchar)
1320
Bram Moolenaarab47c612016-06-14 22:02:26 +02001321 let save_efm=&efm
1322 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1323
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001324 let lines =<< trim END
1325 Entering dir 'dir1/a'
1326 habits2.txt:1:Nine Healthy Habits
1327 Entering dir 'b'
1328 habits3.txt:2:0 Hours of television
1329 habits2.txt:7:5 Small meals
1330 Entering dir 'dir1/c'
1331 habits4.txt:3:1 Hour of exercise
1332 Leaving dir 'dir1/c'
1333 Leaving dir 'dir1/a'
1334 habits1.txt:4:2 Liters of water
1335 Entering dir 'dir2'
1336 habits5.txt:5:3 Cups of hot green tea
1337 Leaving dir 'dir2'
1338 END
Bram Moolenaarab47c612016-06-14 22:02:26 +02001339
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001340 Xexpr ""
1341 for l in lines
1342 Xaddexpr l
1343 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +02001344
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001345 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001346
1347 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
1348 call assert_equal(1, qf[1].lnum)
1349 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
1350 call assert_equal(2, qf[3].lnum)
1351 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
1352 call assert_equal(7, qf[4].lnum)
1353 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
1354 call assert_equal(3, qf[6].lnum)
1355 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
1356 call assert_equal(4, qf[9].lnum)
1357 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
1358 call assert_equal(5, qf[11].lnum)
1359
1360 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001361endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001362
1363" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001364func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001365 " Create the directory stack and files
1366 call mkdir('dir1')
1367 call mkdir('dir1/a')
1368 call mkdir('dir1/a/b')
1369 call mkdir('dir1/c')
1370 call mkdir('dir2')
1371
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001372 let lines =<< trim END
1373 Nine Healthy Habits
1374 0 Hours of television
1375 1 Hour of exercise
1376 2 Liters of water
1377 3 Cups of hot green tea
1378 4 Short mental breaks
1379 5 Small meals
1380 6 AM wake up time
1381 7 Minutes of laughter
1382 8 Hours of sleep (at least)
1383 9 PM end of the day and off to bed
1384 END
Bram Moolenaarab47c612016-06-14 22:02:26 +02001385 call writefile(lines, 'habits1.txt')
1386 call writefile(lines, 'dir1/a/habits2.txt')
1387 call writefile(lines, 'dir1/a/b/habits3.txt')
1388 call writefile(lines, 'dir1/c/habits4.txt')
1389 call writefile(lines, 'dir2/habits5.txt')
1390
1391 call s:dir_stack_tests('c')
1392 call s:dir_stack_tests('l')
1393
1394 call delete('dir1', 'rf')
1395 call delete('dir2', 'rf')
1396 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001397endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001398
Bram Moolenaar9b457942016-10-09 16:10:05 +02001399" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001400func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001401 call s:setup_commands(a:cchar)
1402
1403 let save_efm = &efm
1404
1405 let &efm =
1406 \ '%Eerror %m %l,' .
1407 \ '%-Wignored %m %l,' .
1408 \ '%+Cmore ignored %m %l,' .
1409 \ '%Zignored end'
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001410 let lines =<< trim END
1411 ignored warning 1
1412 more ignored continuation 2
1413 ignored end
1414 error resync 4
1415 END
1416 Xgetexpr lines
Bram Moolenaar9b457942016-10-09 16:10:05 +02001417 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
1418 call assert_equal([['resync', 1, 4, 'E']], l)
1419
1420 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001421endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001422
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001423func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +02001424 call Xefm_ignore_continuations('c')
1425 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001426endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001427
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001428" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001429func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001430 call s:setup_commands(a:cchar)
1431
Bram Moolenaar049cba92016-06-26 14:38:04 +02001432 let save_efm = &efm
1433
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001434 set efm=%f:%l:%m,%f:%f:%l:%m
1435 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
1436
1437 set efm=%f:%l:%m,%f:%l:%r:%m
1438 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1439
1440 set efm=%f:%l:%m,%O:%f:%l:%m
1441 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1442
1443 set efm=%f:%l:%m,%f:%l:%*[^a-z
1444 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
1445
1446 set efm=%f:%l:%m,%f:%l:%*c
1447 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
1448
1449 set efm=%f:%l:%m,%L%M%N
1450 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
1451
1452 set efm=%f:%l:%m,%f:%l:%m:%R
1453 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
1454
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001455 " Invalid regular expression
1456 set efm=%\\%%k
1457 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E867:')
1458
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001459 set efm=
1460 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
1461
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001462 " Empty directory name. When there is an error in parsing new entries, make
1463 " sure the previous quickfix list is made the current list.
1464 set efm&
1465 cexpr ["one", "two"]
1466 let qf_id = getqflist(#{id: 0}).id
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001467 set efm=%DEntering\ dir\ abc,%f:%l:%m
1468 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001469 call assert_equal(qf_id, getqflist(#{id: 0}).id)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001470
1471 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001472endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001473
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001474func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001475 call Xinvalid_efm_Tests('c')
1476 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001477endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001478
1479" TODO:
1480" Add tests for the following formats in 'errorformat'
1481" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001482func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001483 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001484
1485 " Test for %s format in efm
1486 set efm=%f:%s
1487 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001488 let l = getqflist()
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001489 call assert_equal('^\VLine search text\$', l[0].pattern)
1490 call assert_equal(0, l[0].lnum)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001491
Bram Moolenaaree85df32017-03-19 14:19:50 +01001492 let l = split(execute('clist', ''), "\n")
1493 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1494
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001495 " Test for a long line
1496 cexpr 'Xtestfile:' . repeat('a', 1026)
1497 let l = getqflist()
1498 call assert_equal('^\V' . repeat('a', 1019) . '\$', l[0].pattern)
1499
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001500 " Test for %P, %Q and %t format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001501 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001502 [Xtestfile1]
1503 (1,17) error: ';' missing
1504 (21,2) warning: variable 'z' not defined
1505 (67,3) error: end of file found before string ended
1506 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001507
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001508 [Xtestfile2]
1509 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001510
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001511 [Xtestfile3]
1512 NEW compiler v1.1
1513 (2,2) warning: variable 'x' not defined
1514 (67,3) warning: 's' already defined
1515 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001516 [DATA]
1517
Bram Moolenaaree85df32017-03-19 14:19:50 +01001518 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001519 " To exercise the push/pop file functionality in quickfix, the test files
1520 " need to be created.
1521 call writefile(['Line1'], 'Xtestfile1')
1522 call writefile(['Line2'], 'Xtestfile2')
1523 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001524 cexpr ""
1525 for l in lines
1526 caddexpr l
1527 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001528 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001529 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001530 call assert_equal(21, l[2].lnum)
1531 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001532 call assert_equal('w', l[2].type)
1533 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001534 call delete('Xtestfile1')
1535 call delete('Xtestfile2')
1536 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001537
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001538 " Test for %P, %Q with non-existing files
1539 cexpr lines
1540 let l = getqflist()
1541 call assert_equal(14, len(l))
1542 call assert_equal('[Xtestfile1]', l[0].text)
1543 call assert_equal('[Xtestfile2]', l[6].text)
1544 call assert_equal('[Xtestfile3]', l[9].text)
1545
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001546 " Tests for %E, %C and %Z format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001547 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001548 Error 275
1549 line 42
1550 column 3
1551 ' ' expected after '--'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001552 [DATA]
1553
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001554 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1555 cgetexpr lines
1556 let l = getqflist()
1557 call assert_equal(275, l[0].nr)
1558 call assert_equal(42, l[0].lnum)
1559 call assert_equal(3, l[0].col)
1560 call assert_equal('E', l[0].type)
1561 call assert_equal("\n' ' expected after '--'", l[0].text)
1562
1563 " Test for %>
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001564 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001565 Error in line 147 of foo.c:
1566 unknown variable 'i'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001567 [DATA]
1568
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001569 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1570 cgetexpr lines
1571 let l = getqflist()
1572 call assert_equal(147, l[0].lnum)
1573 call assert_equal('E', l[0].type)
1574 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001575
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001576 " Test for %A, %C and other formats
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001577 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001578 ==============================================================
1579 FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)
1580 --------------------------------------------------------------
1581 Traceback (most recent call last):
1582 File "unittests/dbfacadeTest.py", line 89, in testFoo
1583 self.assertEquals(34, dtid)
1584 File "/usr/lib/python2.2/unittest.py", line 286, in
1585 failUnlessEqual
1586 raise self.failureException, \\
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001587 W:AssertionError: 34 != 33
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001588
1589 --------------------------------------------------------------
1590 Ran 27 tests in 0.063s
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001591 [DATA]
1592
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001593 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%t:%m
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001594 cgetexpr lines
1595 let l = getqflist()
1596 call assert_equal(8, len(l))
1597 call assert_equal(89, l[4].lnum)
1598 call assert_equal(1, l[4].valid)
1599 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001600 call assert_equal('W', l[4].type)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001601
Bram Moolenaard76ce852018-05-01 15:02:04 +02001602 " Test for %o
1603 set efm=%f(%o):%l\ %m
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001604 cgetexpr ['Xotestfile(Language.PureScript.Types):20 Error']
1605 call writefile(['Line1'], 'Xotestfile')
Bram Moolenaard76ce852018-05-01 15:02:04 +02001606 let l = getqflist()
1607 call assert_equal(1, len(l), string(l))
1608 call assert_equal('Language.PureScript.Types', l[0].module)
1609 copen
1610 call assert_equal('Language.PureScript.Types|20| Error', getline(1))
1611 call feedkeys("\<CR>", 'xn')
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001612 call assert_equal('Xotestfile', expand('%:t'))
Bram Moolenaard76ce852018-05-01 15:02:04 +02001613 cclose
1614 bd
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001615 call delete("Xotestfile")
Bram Moolenaard76ce852018-05-01 15:02:04 +02001616
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001617 " Test for a long module name
1618 cexpr 'Xtest(' . repeat('m', 1026) . '):15 message'
1619 let l = getqflist()
1620 call assert_equal(repeat('m', 1024), l[0].module)
1621 call assert_equal(15, l[0].lnum)
1622 call assert_equal('message', l[0].text)
1623
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001624 " The following sequence of commands used to crash Vim
1625 set efm=%W%m
1626 cgetexpr ['msg1']
1627 let l = getqflist()
1628 call assert_equal(1, len(l), string(l))
1629 call assert_equal('msg1', l[0].text)
1630 set efm=%C%m
1631 lexpr 'msg2'
1632 let l = getloclist(0)
1633 call assert_equal(1, len(l), string(l))
1634 call assert_equal('msg2', l[0].text)
1635 lopen
1636 call setqflist([], 'r')
1637 caddbuf
1638 let l = getqflist()
1639 call assert_equal(1, len(l), string(l))
1640 call assert_equal('|| msg2', l[0].text)
1641
Bram Moolenaar78ddc062018-05-15 21:56:34 +02001642 " When matching error lines, case should be ignored. Test for this.
1643 set noignorecase
1644 let l=getqflist({'lines' : ['Xtest:FOO10:Line 20'], 'efm':'%f:foo%l:%m'})
1645 call assert_equal(10, l.items[0].lnum)
1646 call assert_equal('Line 20', l.items[0].text)
1647 set ignorecase&
1648
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001649 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001650 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001651endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001652
Bram Moolenaare9283662020-06-07 14:10:47 +02001653" Test for '%t' (error type) field in 'efm'
1654func Test_efm_error_type()
1655 let save_efm = &efm
1656
1657 " error type
1658 set efm=%f:%l:%t:%m
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001659 let lines =<< trim END
1660 Xfile1:10:E:msg1
1661 Xfile1:20:W:msg2
1662 Xfile1:30:I:msg3
1663 Xfile1:40:N:msg4
1664 Xfile1:50:R:msg5
1665 END
1666 cexpr lines
Bram Moolenaare9283662020-06-07 14:10:47 +02001667 let output = split(execute('clist'), "\n")
1668 call assert_equal([
1669 \ ' 1 Xfile1:10 error: msg1',
1670 \ ' 2 Xfile1:20 warning: msg2',
1671 \ ' 3 Xfile1:30 info: msg3',
1672 \ ' 4 Xfile1:40 note: msg4',
1673 \ ' 5 Xfile1:50 R: msg5'], output)
1674
1675 " error type and a error number
1676 set efm=%f:%l:%t:%n:%m
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001677 let lines =<< trim END
1678 Xfile1:10:E:2:msg1
1679 Xfile1:20:W:4:msg2
1680 Xfile1:30:I:6:msg3
1681 Xfile1:40:N:8:msg4
1682 Xfile1:50:R:3:msg5
1683 END
1684 cexpr lines
Bram Moolenaare9283662020-06-07 14:10:47 +02001685 let output = split(execute('clist'), "\n")
1686 call assert_equal([
1687 \ ' 1 Xfile1:10 error 2: msg1',
1688 \ ' 2 Xfile1:20 warning 4: msg2',
1689 \ ' 3 Xfile1:30 info 6: msg3',
1690 \ ' 4 Xfile1:40 note 8: msg4',
1691 \ ' 5 Xfile1:50 R 3: msg5'], output)
1692 let &efm = save_efm
1693endfunc
1694
haya14busae023d492022-02-08 18:09:29 +00001695" Test for end_lnum ('%e') and end_col ('%k') fields in 'efm'
1696func Test_efm_end_lnum_col()
1697 let save_efm = &efm
1698
1699 " single line
1700 set efm=%f:%l-%e:%c-%k:%t:%m
1701 cexpr ["Xfile1:10-20:1-2:E:msg1", "Xfile1:20-30:2-3:W:msg2",]
1702 let output = split(execute('clist'), "\n")
1703 call assert_equal([
1704 \ ' 1 Xfile1:10-20 col 1-2 error: msg1',
1705 \ ' 2 Xfile1:20-30 col 2-3 warning: msg2'], output)
1706
1707 " multiple lines
1708 set efm=%A%n)%m,%Z%f:%l-%e:%c-%k
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001709 let lines =<< trim END
1710 1)msg1
1711 Xfile1:14-24:1-2
1712 2)msg2
1713 Xfile1:24-34:3-4
1714 END
1715 cexpr lines
haya14busae023d492022-02-08 18:09:29 +00001716 let output = split(execute('clist'), "\n")
1717 call assert_equal([
1718 \ ' 1 Xfile1:14-24 col 1-2 error 1: msg1',
1719 \ ' 2 Xfile1:24-34 col 3-4 error 2: msg2'], output)
1720 let &efm = save_efm
1721endfunc
1722
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001723func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001724 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001725 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001726 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001727 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001728 colder
1729 cgetexpr []
1730 endfunc
1731 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001732 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001733 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001734 lolder
1735 lgetexpr []
1736 endfunc
1737 endif
1738
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001739 augroup QF_Test
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001740 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001741 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001742 augroup END
1743
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001744 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001745 let words = [ "a", "b" ]
1746 let qflist = []
1747 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001748 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001749 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001750 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001751 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001752
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001753 augroup QF_Test
1754 au!
1755 augroup END
1756
1757 if a:cchar == 'c'
1758 cexpr ["Xtest1:1:Line"]
1759 cwindow
1760 only
1761 augroup QF_Test
1762 au!
1763 autocmd WinEnter * call setqflist([], 'f')
1764 augroup END
1765 call assert_fails('exe "normal \<CR>"', 'E925:')
1766 augroup QF_Test
1767 au!
1768 augroup END
1769 endif
1770 %bw!
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001771endfunc
1772
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001773func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001774 call XquickfixChangedByAutocmd('c')
1775 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001776endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001777
Bram Moolenaar4d170af2020-09-13 22:21:22 +02001778func Test_setloclist_in_autocommand()
1779 call writefile(['test1', 'test2'], 'Xfile')
1780 edit Xfile
1781 let s:bufnr = bufnr()
1782 call setloclist(1,
1783 \ [{'bufnr' : s:bufnr, 'lnum' : 1, 'text' : 'test1'},
1784 \ {'bufnr' : s:bufnr, 'lnum' : 2, 'text' : 'test2'}])
1785
1786 augroup Test_LocList
1787 au!
1788 autocmd BufEnter * call setloclist(1,
1789 \ [{'bufnr' : s:bufnr, 'lnum' : 1, 'text' : 'test1'},
1790 \ {'bufnr' : s:bufnr, 'lnum' : 2, 'text' : 'test2'}], 'r')
1791 augroup END
1792
1793 lopen
1794 call assert_fails('exe "normal j\<CR>"', 'E926:')
1795
1796 augroup Test_LocList
1797 au!
1798 augroup END
1799 call delete('Xfile')
1800endfunc
1801
Bram Moolenaar8b201792016-03-25 15:01:10 +01001802func Test_caddbuffer_to_empty()
1803 helpgr quickfix
1804 call setqflist([], 'r')
1805 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001806 try
1807 cn
1808 catch
1809 " number of matches is unknown
1810 call assert_true(v:exception =~ 'E553:')
1811 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001812 quit!
1813endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001814
1815func Test_cgetexpr_works()
1816 " this must not crash Vim
1817 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001818 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001819endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001820
1821" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001822func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001823 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001824
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001825 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
thinca6864efa2021-06-19 20:45:20 +02001826 \ {'bufnr': a:bnum, 'lnum': 2, 'end_lnum': 3, 'col': 4, 'end_col': 5}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001827 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001828 call assert_equal(2, len(l))
1829 call assert_equal(2, l[1].lnum)
thinca6864efa2021-06-19 20:45:20 +02001830 call assert_equal(3, l[1].end_lnum)
1831 call assert_equal(4, l[1].col)
1832 call assert_equal(5, l[1].end_col)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001833
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001834 Xnext
1835 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1836 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001837 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001838 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001839 call assert_equal(3, line('.'))
1840
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001841 " Appending entries to the list should not change the cursor position
1842 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001843 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001844 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001845 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001846 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1847 call assert_equal(1, line('.'))
1848 close
1849
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001850 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001851 \ {'bufnr': a:bnum, 'lnum': 4},
1852 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001853 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001854 call assert_equal(3, len(l))
1855 call assert_equal(5, l[2].lnum)
1856
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001857 call g:Xsetlist([])
1858 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001859 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001860
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001861 " Tests for setting the 'valid' flag
1862 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1863 Xwindow
1864 call assert_equal(1, winnr('$'))
1865 let l = g:Xgetlist()
1866 call g:Xsetlist(l)
1867 call assert_equal(0, g:Xgetlist()[0].valid)
Bram Moolenaar9752c722018-12-22 16:49:34 +01001868 " Adding a non-valid entry should not mark the list as having valid entries
1869 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':5, 'valid':0}], 'a')
1870 Xwindow
1871 call assert_equal(1, winnr('$'))
1872
1873 " :cnext/:cprev should still work even with invalid entries in the list
1874 let l = [{'bufnr' : a:bnum, 'lnum' : 1, 'text' : '1', 'valid' : 0},
1875 \ {'bufnr' : a:bnum, 'lnum' : 2, 'text' : '2', 'valid' : 0}]
1876 call g:Xsetlist(l)
1877 Xnext
1878 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1879 Xprev
1880 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1881 " :cnext/:cprev should still work after appending invalid entries to an
1882 " empty list
1883 call g:Xsetlist([])
1884 call g:Xsetlist(l, 'a')
1885 Xnext
1886 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1887 Xprev
1888 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1889
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001890 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1891 Xwindow
1892 call assert_equal(2, winnr('$'))
1893 Xclose
1894 let save_efm = &efm
1895 set efm=%m
1896 Xgetexpr 'TestMessage'
1897 let l = g:Xgetlist()
1898 call g:Xsetlist(l)
1899 call assert_equal(1, g:Xgetlist()[0].valid)
1900 let &efm = save_efm
1901
Bram Moolenaaree85df32017-03-19 14:19:50 +01001902 " Error cases:
1903 " Refer to a non-existing buffer and pass a non-dictionary type
1904 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1905 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1906 call g:Xsetlist([[1, 2,3]])
1907 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001908 call assert_fails('call g:Xsetlist([], [])', 'E928:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001909 call g:Xsetlist([test_null_dict()])
1910 call assert_equal([], g:Xgetlist())
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001911endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001912
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001913func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001914 new Xtestfile | only
1915 let bnum = bufnr('%')
1916 call setline(1, range(1,5))
1917
1918 call SetXlistTests('c', bnum)
1919 call SetXlistTests('l', bnum)
1920
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001921 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001922 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001923endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001924
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001925func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001926 call s:setup_commands(a:cchar)
1927
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001928 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001929 let @/ = 'Test_'
1930 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001931 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001932 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001933 Xvimgrep empty test_quickfix.vim
1934 call assert_true(len(g:Xgetlist()) > 0)
1935 Xvimgrep matches test_quickfix.vim
1936 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001937 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001938 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001939 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001940 call g:Xsetlist([], 'r')
1941 call assert_true(len(g:Xgetlist()) == 0)
1942 Xolder
1943 call assert_equal(testlen, len(g:Xgetlist()))
1944 Xnewer
1945 Xnewer
1946 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001947endfunc
1948
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001949func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001950 call Xlist_empty_middle('c')
1951 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001952endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001953
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001954func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001955 call s:setup_commands(a:cchar)
1956
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001957 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001958 Xvimgrep one test_quickfix.vim
1959 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001960 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001961 Xvimgrep two test_quickfix.vim
1962 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001963 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001964 Xvimgrep three test_quickfix.vim
1965 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001966 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001967 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001968 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001969 call g:Xsetlist([], 'r')
1970 call assert_true(len(g:Xgetlist()) == 0)
1971 Xnewer
1972 call assert_equal(twolen, len(g:Xgetlist()))
1973 Xnewer
1974 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001975endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001976
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001977func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001978 call Xlist_empty_older('c')
1979 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001980endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001981
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001982func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001983 call s:setup_commands(a:cchar)
1984
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001985 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1986 \ {'filename': 'fnameB', 'text': 'B'}]
1987 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1988 \ {'filename': 'fnameD', 'text': 'D'},
1989 \ {'filename': 'fnameE', 'text': 'E'}]
1990
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001991 " {action} is unspecified. Same as specifying ' '.
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001992 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001993 silent! Xnewer 99
1994 call g:Xsetlist(list1)
1995 call g:Xsetlist(list2)
1996 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001997 call assert_equal(3, len(li))
1998 call assert_equal('C', li[0]['text'])
1999 call assert_equal('D', li[1]['text'])
2000 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002001 silent! Xolder
2002 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002003 call assert_equal(2, len(li))
2004 call assert_equal('A', li[0]['text'])
2005 call assert_equal('B', li[1]['text'])
2006
2007 " {action} is specified ' '.
2008 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002009 silent! Xnewer 99
2010 call g:Xsetlist(list1)
2011 call g:Xsetlist(list2, ' ')
2012 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002013 call assert_equal(3, len(li))
2014 call assert_equal('C', li[0]['text'])
2015 call assert_equal('D', li[1]['text'])
2016 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002017 silent! Xolder
2018 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002019 call assert_equal(2, len(li))
2020 call assert_equal('A', li[0]['text'])
2021 call assert_equal('B', li[1]['text'])
2022
2023 " {action} is specified 'a'.
2024 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002025 silent! Xnewer 99
2026 call g:Xsetlist(list1)
2027 call g:Xsetlist(list2, 'a')
2028 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002029 call assert_equal(5, len(li))
2030 call assert_equal('A', li[0]['text'])
2031 call assert_equal('B', li[1]['text'])
2032 call assert_equal('C', li[2]['text'])
2033 call assert_equal('D', li[3]['text'])
2034 call assert_equal('E', li[4]['text'])
2035
2036 " {action} is specified 'r'.
2037 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002038 silent! Xnewer 99
2039 call g:Xsetlist(list1)
2040 call g:Xsetlist(list2, 'r')
2041 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002042 call assert_equal(3, len(li))
2043 call assert_equal('C', li[0]['text'])
2044 call assert_equal('D', li[1]['text'])
2045 call assert_equal('E', li[2]['text'])
2046
2047 " Test for wrong value.
2048 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002049 call assert_fails("call g:Xsetlist(0)", 'E714:')
2050 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
2051 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
2052 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
2053 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002054endfunc
2055
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01002056func Test_setqflist_invalid_nr()
2057 " The following command used to crash Vim
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002058 eval []->setqflist(' ', {'nr' : $XXX_DOES_NOT_EXIST})
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01002059endfunc
2060
Bram Moolenaar99234f22020-02-10 22:56:54 +01002061func Test_setqflist_user_sets_buftype()
2062 call setqflist([{'text': 'foo'}, {'text': 'bar'}])
2063 set buftype=quickfix
2064 call setqflist([], 'a')
2065 enew
2066endfunc
2067
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002068func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002069 call XquickfixSetListWithAct('c')
2070 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002071endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002072
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002073func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002074 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002075
Bram Moolenaar049cba92016-06-26 14:38:04 +02002076 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002077 call assert_equal(1, l[0].lnum)
2078 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002079 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002080 call assert_equal(2, l[1].lnum)
2081 call assert_equal(1, l[1].col)
2082 call assert_equal(4070, len(l[1].text))
2083 call assert_equal(3, l[2].lnum)
2084 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002085 call assert_equal(4070, len(l[2].text))
2086 call assert_equal(4, l[3].lnum)
2087 call assert_equal(1, l[3].col)
2088 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002089
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002090 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002091endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002092
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002093func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002094 call s:setup_commands(a:cchar)
2095
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002096 let testfile = 'samples/quickfix.txt'
2097
2098 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002099 exe 'Xgetfile' testfile
2100 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002101
2102 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002103 Xexpr readfile(testfile)
2104 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002105
2106 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002107 Xexpr join(readfile(testfile), "\n")
2108 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002109
2110 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002111 exe 'edit' testfile
2112 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02002113 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002114endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002115
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002116func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002117 call s:long_lines_tests('c')
2118 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002119endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002120
Bram Moolenaar59941cb2020-09-05 17:03:40 +02002121func Test_cgetfile_on_long_lines()
2122 " Problematic values if the line is longer than 4096 bytes. Then 1024 bytes
2123 " are read at a time.
2124 for len in [4078, 4079, 4080, 5102, 5103, 5104, 6126, 6127, 6128, 7150, 7151, 7152]
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002125 let lines =<< trim END
2126 /tmp/file1:1:1:aaa
2127 /tmp/file2:1:1:%s
2128 /tmp/file3:1:1:bbb
2129 /tmp/file4:1:1:ccc
2130 END
Bram Moolenaar59941cb2020-09-05 17:03:40 +02002131 let lines[1] = substitute(lines[1], '%s', repeat('x', len), '')
2132 call writefile(lines, 'Xcqetfile.txt')
2133 cgetfile Xcqetfile.txt
2134 call assert_equal(4, getqflist(#{size: v:true}).size, 'with length ' .. len)
2135 endfor
2136 call delete('Xcqetfile.txt')
2137endfunc
2138
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002139func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002140 let l = []
2141 for i in range(1, 20)
2142 call add(l, 'Line' . i)
2143 endfor
2144 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002145endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002146
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002147func Test_switchbuf()
Bram Moolenaar049cba92016-06-26 14:38:04 +02002148 call s:create_test_file('Xqftestfile1')
2149 call s:create_test_file('Xqftestfile2')
2150 call s:create_test_file('Xqftestfile3')
2151
2152 new | only
2153 edit Xqftestfile1
2154 let file1_winid = win_getid()
2155 new Xqftestfile2
2156 let file2_winid = win_getid()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002157 let lines =<< trim END
2158 Xqftestfile1:5:Line5
2159 Xqftestfile1:6:Line6
2160 Xqftestfile2:10:Line10
2161 Xqftestfile2:11:Line11
2162 Xqftestfile3:15:Line15
2163 Xqftestfile3:16:Line16
2164 END
2165 cgetexpr lines
Bram Moolenaar049cba92016-06-26 14:38:04 +02002166
2167 new
2168 let winid = win_getid()
2169 cfirst | cnext
2170 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002171 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002172 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002173 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002174 call assert_equal(winid, win_getid())
Bram Moolenaar049cba92016-06-26 14:38:04 +02002175
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002176 " Test for 'switchbuf' set to search for files in windows in the current
2177 " tabpage and jump to an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002178 set switchbuf=useopen
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002179 enew
Bram Moolenaar049cba92016-06-26 14:38:04 +02002180 cfirst | cnext
2181 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002182 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002183 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002184 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002185 call assert_equal(file2_winid, win_getid())
2186
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002187 " Test for 'switchbuf' set to search for files in tabpages and jump to an
2188 " existing tabpage (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002189 enew | only
2190 set switchbuf=usetab
2191 tabedit Xqftestfile1
2192 tabedit Xqftestfile2
Bram Moolenaar74240d32017-12-10 15:26:15 +01002193 tabedit Xqftestfile3
Bram Moolenaar049cba92016-06-26 14:38:04 +02002194 tabfirst
2195 cfirst | cnext
2196 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002197 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002198 call assert_equal(3, tabpagenr())
Bram Moolenaar74240d32017-12-10 15:26:15 +01002199 6cnext
2200 call assert_equal(4, tabpagenr())
2201 2cpfile
2202 call assert_equal(2, tabpagenr())
2203 2cnfile
2204 call assert_equal(4, tabpagenr())
Bram Moolenaar049cba92016-06-26 14:38:04 +02002205 tabfirst | tabonly | enew
2206
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002207 " Test for 'switchbuf' set to open a new window for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02002208 set switchbuf=split
2209 cfirst | cnext
2210 call assert_equal(1, winnr('$'))
2211 cnext | cnext
2212 call assert_equal(2, winnr('$'))
2213 cnext | cnext
2214 call assert_equal(3, winnr('$'))
Bram Moolenaar049cba92016-06-26 14:38:04 +02002215
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002216 " Test for 'switchbuf' set to open a new tabpage for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02002217 set switchbuf=newtab
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002218 enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02002219 cfirst | cnext
2220 call assert_equal(1, tabpagenr('$'))
2221 cnext | cnext
2222 call assert_equal(2, tabpagenr('$'))
2223 cnext | cnext
2224 call assert_equal(3, tabpagenr('$'))
2225 tabfirst | enew | tabonly | only
2226
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01002227 set switchbuf=uselast
2228 split
2229 let last_winid = win_getid()
2230 copen
2231 exe "normal 1G\<CR>"
2232 call assert_equal(last_winid, win_getid())
2233 enew | only
2234
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002235 " With an empty 'switchbuf', jumping to a quickfix entry should open the
2236 " file in an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002237 set switchbuf=
2238 edit Xqftestfile1
2239 let file1_winid = win_getid()
2240 new Xqftestfile2
2241 let file2_winid = win_getid()
2242 copen
2243 exe "normal 1G\<CR>"
2244 call assert_equal(file1_winid, win_getid())
2245 copen
2246 exe "normal 3G\<CR>"
2247 call assert_equal(file2_winid, win_getid())
2248 copen | only
2249 exe "normal 5G\<CR>"
2250 call assert_equal(2, winnr('$'))
2251 call assert_equal(1, bufwinnr('Xqftestfile3'))
2252
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002253 " If only quickfix window is open in the current tabpage, jumping to an
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002254 " entry with 'switchbuf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02002255 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002256 set switchbuf=usetab
2257 tabedit Xqftestfile1
2258 tabedit Xqftestfile2
2259 tabedit Xqftestfile3
2260 tabfirst
2261 copen | only
2262 clast
2263 call assert_equal(4, tabpagenr())
2264 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02002265
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002266 " Jumping to a file that is not present in any of the tabpages and the
2267 " current tabpage doesn't have any usable windows, should open it in a new
2268 " window in the current tabpage.
2269 copen | only
2270 cfirst
2271 call assert_equal(1, tabpagenr())
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02002272 call assert_equal('Xqftestfile1', @%)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002273
2274 " If opening a file changes 'switchbuf', then the new value should be
2275 " retained.
Bram Moolenaar41d42992020-05-03 16:29:50 +02002276 set modeline&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002277 call writefile(["vim: switchbuf=split"], 'Xqftestfile1')
2278 enew | only
2279 set switchbuf&vim
2280 cexpr "Xqftestfile1:1:10"
2281 call assert_equal('split', &switchbuf)
2282 call writefile(["vim: switchbuf=usetab"], 'Xqftestfile1')
2283 enew | only
2284 set switchbuf=useopen
2285 cexpr "Xqftestfile1:1:10"
2286 call assert_equal('usetab', &switchbuf)
2287 call writefile(["vim: switchbuf&vim"], 'Xqftestfile1')
2288 enew | only
2289 set switchbuf=useopen
2290 cexpr "Xqftestfile1:1:10"
2291 call assert_equal('', &switchbuf)
2292
Bram Moolenaar049cba92016-06-26 14:38:04 +02002293 call delete('Xqftestfile1')
2294 call delete('Xqftestfile2')
2295 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002296 set switchbuf&vim
2297
2298 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002299endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002300
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002301func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002302 call s:setup_commands(a:cchar)
2303
2304 enew | only
2305
Bram Moolenaarc1542742016-07-20 21:44:37 +02002306 let fname = 'Xqftestfile' . a:cchar
2307 call s:create_test_file(fname)
2308 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02002309
Bram Moolenaarc1542742016-07-20 21:44:37 +02002310 Xgetexpr [fname . ':5:Line5',
2311 \ fname . ':10:Line10',
2312 \ fname . ':15:Line15',
2313 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02002314
2315 6,14delete
2316 call append(6, ['Buffer', 'Window'])
2317
2318 let l = g:Xgetlist()
Bram Moolenaar049cba92016-06-26 14:38:04 +02002319 call assert_equal(5, l[0].lnum)
2320 call assert_equal(6, l[2].lnum)
2321 call assert_equal(13, l[3].lnum)
2322
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002323 " If a file doesn't have any quickfix entries, then deleting lines in the
2324 " file should not update the quickfix list
2325 call g:Xsetlist([], 'f')
2326 1,2delete
2327 call assert_equal([], g:Xgetlist())
2328
Bram Moolenaar049cba92016-06-26 14:38:04 +02002329 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02002330 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002331endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002332
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002333func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02002334 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02002335 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02002336 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02002337 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002338endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002339
2340" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002341func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002342 call s:setup_commands(a:cchar)
2343
2344 " The following lines are used for the grep test. Don't remove.
2345 " Grep_Test_Text: Match 1
2346 " Grep_Test_Text: Match 2
2347 " GrepAdd_Test_Text: Match 1
2348 " GrepAdd_Test_Text: Match 2
2349 enew! | only
2350 set makeef&vim
2351 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002352 call assert_true(len(g:Xgetlist()) == 5)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002353 Xopen
2354 call assert_true(w:quickfix_title =~ '^:grep')
2355 Xclose
2356 enew
2357 set makeef=Temp_File_##
2358 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002359 call assert_true(len(g:Xgetlist()) == 9)
2360
2361 " Try with 'grepprg' set to 'internal'
2362 set grepprg=internal
2363 silent Xgrep Grep_Test_Text: test_quickfix.vim
2364 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
2365 call assert_true(len(g:Xgetlist()) == 9)
2366 set grepprg&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002367
2368 call writefile(['Vim'], 'XtestTempFile')
2369 set makeef=XtestTempFile
2370 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002371 call assert_equal(5, len(g:Xgetlist()))
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002372 call assert_false(filereadable('XtestTempFile'))
2373 set makeef&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002374endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002375
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002376func Test_grep()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002377 " The grepprg may not be set on non-Unix systems
2378 CheckUnix
Bram Moolenaar049cba92016-06-26 14:38:04 +02002379
2380 call s:test_xgrep('c')
2381 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002382endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002383
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002384func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002385 " Use one 'errorformat' for two windows. Add an expression to each of them,
2386 " make sure they each keep their own state.
2387 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
2388 call mkdir('Xone/a', 'p')
2389 call mkdir('Xtwo/a', 'p')
2390 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2391 call writefile(lines, 'Xone/a/one.txt')
2392 call writefile(lines, 'Xtwo/a/two.txt')
2393
2394 new one
2395 let one_id = win_getid()
2396 lexpr ""
2397 new two
2398 let two_id = win_getid()
2399 lexpr ""
2400
2401 laddexpr "Entering dir 'Xtwo/a'"
2402 call win_gotoid(one_id)
2403 laddexpr "Entering dir 'Xone/a'"
2404 call win_gotoid(two_id)
2405 laddexpr 'two.txt:5:two two two'
2406 call win_gotoid(one_id)
2407 laddexpr 'one.txt:3:one one one'
2408
2409 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002410 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
2411 call assert_equal(3, loc_one[1].lnum)
2412
2413 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002414 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
2415 call assert_equal(5, loc_two[1].lnum)
2416
2417 call win_gotoid(one_id)
2418 bwipe!
2419 call win_gotoid(two_id)
2420 bwipe!
2421 call delete('Xone', 'rf')
2422 call delete('Xtwo', 'rf')
2423endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02002424
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002425func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02002426 call s:setup_commands(a:cchar)
2427
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002428 " Calling lbottom without any errors should fail
2429 if a:cchar == 'l'
2430 call assert_fails('lbottom', 'E776:')
2431 endif
2432
Bram Moolenaar875feea2017-06-11 16:07:51 +02002433 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02002434 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02002435 let wid = win_getid()
2436 call assert_equal(1, line('.'))
2437 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02002438 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02002439 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02002440 call win_gotoid(wid)
2441 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02002442 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02002443endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02002444
2445" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002446func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02002447 call XbottomTests('c')
2448 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002449endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002450
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002451func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002452 call s:setup_commands(a:cchar)
2453
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002454 " clear all lists after the first one, then replace the first one.
2455 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002456 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002457 let entry = {'filename': 'foo', 'lnum': 42}
2458 call g:Xsetlist([entry], 'r')
2459 call g:Xsetlist([entry, entry])
2460 call g:Xsetlist([entry, entry, entry])
2461 let res = split(execute(a:cchar . 'hist'), "\n")
2462 call assert_equal(3, len(res))
2463 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
2464 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
2465 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
2466 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002467
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002468 " Test for changing the quickfix lists
2469 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2470 exe '1' . a:cchar . 'hist'
2471 call assert_equal(1, g:Xgetlist({'nr' : 0}).nr)
2472 exe '3' . a:cchar . 'hist'
2473 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2474 call assert_fails('-2' . a:cchar . 'hist', 'E16:')
2475 call assert_fails('4' . a:cchar . 'hist', 'E16:')
2476
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002477 call g:Xsetlist([], 'f')
2478 let l = split(execute(a:cchar . 'hist'), "\n")
2479 call assert_equal('No entries', l[0])
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002480 if a:cchar == 'c'
2481 call assert_fails('4chist', 'E16:')
2482 else
2483 call assert_fails('4lhist', 'E776:')
2484 endif
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002485
2486 " An empty list should still show the stack history
2487 call g:Xsetlist([])
2488 let res = split(execute(a:cchar . 'hist'), "\n")
2489 call assert_equal('> error list 1 of 1; 0 ' . common, res[0])
2490
2491 call g:Xsetlist([], 'f')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002492endfunc
2493
2494func Test_history()
2495 call HistoryTest('c')
2496 call HistoryTest('l')
2497endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02002498
2499func Test_duplicate_buf()
2500 " make sure we can get the highest buffer number
2501 edit DoesNotExist
2502 edit DoesNotExist2
2503 let last_buffer = bufnr("$")
2504
2505 " make sure only one buffer is created
2506 call writefile(['this one', 'that one'], 'Xgrepthis')
2507 vimgrep one Xgrepthis
2508 vimgrep one Xgrepthis
2509 call assert_equal(last_buffer + 1, bufnr("$"))
2510
2511 call delete('Xgrepthis')
2512endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002513
2514" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002515func Xproperty_tests(cchar)
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002516 call s:setup_commands(a:cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002517
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002518 " Error cases
2519 call assert_fails('call g:Xgetlist(99)', 'E715:')
2520 call assert_fails('call g:Xsetlist(99)', 'E714:')
2521 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
Bram Moolenaard823fa92016-08-12 16:29:27 +02002522
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002523 " Set and get the title
2524 call g:Xsetlist([])
2525 Xopen
2526 wincmd p
2527 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
2528 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
2529 call assert_equal(0, s)
2530 let d = g:Xgetlist({"title":1})
2531 call assert_equal('Sample', d.title)
2532 " Try setting title to a non-string value
2533 call assert_equal(-1, g:Xsetlist([], 'a', {'title' : ['Test']}))
2534 call assert_equal('Sample', g:Xgetlist({"title":1}).title)
2535
2536 Xopen
2537 call assert_equal('Sample', w:quickfix_title)
2538 Xclose
2539
2540 " Tests for action argument
2541 silent! Xolder 999
2542 let qfnr = g:Xgetlist({'all':1}).nr
2543 call g:Xsetlist([], 'r', {'title' : 'N1'})
2544 call assert_equal('N1', g:Xgetlist({'all':1}).title)
2545 call g:Xsetlist([], ' ', {'title' : 'N2'})
2546 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
2547
2548 let res = g:Xgetlist({'nr': 0})
2549 call assert_equal(qfnr + 1, res.nr)
2550 call assert_equal(['nr'], keys(res))
2551
2552 call g:Xsetlist([], ' ', {'title' : 'N3'})
2553 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
2554
2555 " Changing the title of an earlier quickfix list
2556 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
2557 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
2558
2559 " Changing the title of an invalid quickfix list
2560 call assert_equal(-1, g:Xsetlist([], ' ',
2561 \ {'title' : 'SomeTitle', 'nr' : 99}))
2562 call assert_equal(-1, g:Xsetlist([], ' ',
2563 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
2564
2565 if a:cchar == 'c'
2566 copen
2567 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
2568 cclose
2569 endif
2570
2571 " Invalid arguments
Bram Moolenaare2e40752020-09-04 21:18:46 +02002572 call assert_fails('call g:Xgetlist([])', 'E715:')
2573 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002574 let s = g:Xsetlist([], 'a', {'abc':1})
2575 call assert_equal(-1, s)
2576
2577 call assert_equal({}, g:Xgetlist({'abc':1}))
2578 call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
2579 call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
2580
2581 if a:cchar == 'l'
2582 call assert_equal({}, getloclist(99, {'title': 1}))
2583 endif
2584
2585 " Context related tests
2586 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
2587 call assert_equal(0, s)
2588 call test_garbagecollect_now()
2589 let d = g:Xgetlist({'context':1})
2590 call assert_equal([1,2,3], d.context)
2591 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
2592 let d = g:Xgetlist({'context':1})
2593 call assert_equal({'color':'green'}, d.context)
2594 call g:Xsetlist([], 'a', {'context':"Context info"})
2595 let d = g:Xgetlist({'context':1})
2596 call assert_equal("Context info", d.context)
2597 call g:Xsetlist([], 'a', {'context':246})
2598 let d = g:Xgetlist({'context':1})
2599 call assert_equal(246, d.context)
2600 " set other Vim data types as context
2601 call g:Xsetlist([], 'a', {'context' : test_null_blob()})
2602 if has('channel')
2603 call g:Xsetlist([], 'a', {'context' : test_null_channel()})
2604 endif
2605 if has('job')
2606 call g:Xsetlist([], 'a', {'context' : test_null_job()})
2607 endif
2608 call g:Xsetlist([], 'a', {'context' : test_null_function()})
2609 call g:Xsetlist([], 'a', {'context' : test_null_partial()})
2610 call g:Xsetlist([], 'a', {'context' : ''})
2611 call test_garbagecollect_now()
2612 if a:cchar == 'l'
2613 " Test for copying context across two different location lists
2614 new | only
2615 let w1_id = win_getid()
2616 let l = [1]
2617 call setloclist(0, [], 'a', {'context':l})
2618 new
2619 let w2_id = win_getid()
2620 call add(l, 2)
2621 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
2622 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2623 unlet! l
2624 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2625 only
2626 call setloclist(0, [], 'f')
2627 call assert_equal('', getloclist(0, {'context':1}).context)
2628 endif
2629
2630 " Test for changing the context of previous quickfix lists
2631 call g:Xsetlist([], 'f')
2632 Xexpr "One"
2633 Xexpr "Two"
2634 Xexpr "Three"
2635 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
2636 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
2637 " Also, check for setting the context using quickfix list number zero.
2638 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
2639 call test_garbagecollect_now()
2640 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
2641 call assert_equal([1], l.context)
2642 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
2643 call assert_equal([2], l.context)
2644 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
2645 call assert_equal([3], l.context)
2646
2647 " Test for changing the context through reference and for garbage
2648 " collection of quickfix context
2649 let l = ["red"]
2650 call g:Xsetlist([], ' ', {'context' : l})
2651 call add(l, "blue")
2652 let x = g:Xgetlist({'context' : 1})
2653 call add(x.context, "green")
2654 call assert_equal(["red", "blue", "green"], l)
2655 call assert_equal(["red", "blue", "green"], x.context)
2656 unlet l
2657 call test_garbagecollect_now()
2658 let m = g:Xgetlist({'context' : 1})
2659 call assert_equal(["red", "blue", "green"], m.context)
2660
2661 " Test for setting/getting items
2662 Xexpr ""
2663 let qfprev = g:Xgetlist({'nr':0})
2664 let s = g:Xsetlist([], ' ', {'title':'Green',
2665 \ 'items' : [{'filename':'F1', 'lnum':10}]})
2666 call assert_equal(0, s)
2667 let qfcur = g:Xgetlist({'nr':0})
2668 call assert_true(qfcur.nr == qfprev.nr + 1)
2669 let l = g:Xgetlist({'items':1})
2670 call assert_equal('F1', bufname(l.items[0].bufnr))
2671 call assert_equal(10, l.items[0].lnum)
2672 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
2673 \ {'filename':'F2', 'lnum':30}]})
2674 let l = g:Xgetlist({'items':1})
2675 call assert_equal('F2', bufname(l.items[2].bufnr))
2676 call assert_equal(30, l.items[2].lnum)
2677 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
2678 let l = g:Xgetlist({'items':1})
2679 call assert_equal('F3', bufname(l.items[0].bufnr))
2680 call assert_equal(40, l.items[0].lnum)
2681 call g:Xsetlist([], 'r', {'items' : []})
2682 let l = g:Xgetlist({'items':1})
2683 call assert_equal(0, len(l.items))
2684
2685 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
2686 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2687 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2688 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
2689
2690 " Test for getting id of window associated with a location list window
2691 if a:cchar == 'l'
2692 only
2693 call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid)
2694 let wid = win_getid()
Bram Moolenaard823fa92016-08-12 16:29:27 +02002695 Xopen
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002696 call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid)
2697 wincmd w
2698 call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid)
2699 only
2700 endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02002701
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002702 " The following used to crash Vim with address sanitizer
2703 call g:Xsetlist([], 'f')
2704 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
2705 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002706
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002707 " Try setting the items using a string
2708 call assert_equal(-1, g:Xsetlist([], ' ', {'items' : 'Test'}))
Bram Moolenaar890680c2016-09-27 21:28:56 +02002709
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002710 " Save and restore the quickfix stack
2711 call g:Xsetlist([], 'f')
2712 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2713 Xexpr "File1:10:Line1"
2714 Xexpr "File2:20:Line2"
2715 Xexpr "File3:30:Line3"
2716 let last_qf = g:Xgetlist({'nr':'$'}).nr
2717 call assert_equal(3, last_qf)
2718 let qstack = []
2719 for i in range(1, last_qf)
2720 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
2721 endfor
2722 call g:Xsetlist([], 'f')
2723 for i in range(len(qstack))
2724 call g:Xsetlist([], ' ', qstack[i])
2725 endfor
2726 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2727 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
2728 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
2729 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
2730 call g:Xsetlist([], 'f')
Bram Moolenaar890680c2016-09-27 21:28:56 +02002731
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002732 " Swap two quickfix lists
2733 Xexpr "File1:10:Line10"
2734 Xexpr "File2:20:Line20"
2735 Xexpr "File3:30:Line30"
2736 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
2737 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
2738 let l1=g:Xgetlist({'nr':1,'all':1})
2739 let l2=g:Xgetlist({'nr':2,'all':1})
2740 let save_id = l1.id
2741 let l1.id=l2.id
2742 let l2.id=save_id
2743 call g:Xsetlist([], 'r', l1)
2744 call g:Xsetlist([], 'r', l2)
2745 let newl1=g:Xgetlist({'nr':1,'all':1})
2746 let newl2=g:Xgetlist({'nr':2,'all':1})
2747 call assert_equal('Fruits', newl1.title)
2748 call assert_equal(['Fruits'], newl1.context)
2749 call assert_equal('Line20', newl1.items[0].text)
2750 call assert_equal('Colors', newl2.title)
2751 call assert_equal(['Colors'], newl2.context)
2752 call assert_equal('Line10', newl2.items[0].text)
2753 call g:Xsetlist([], 'f')
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02002754
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002755 " Cannot specify both a non-empty list argument and a dict argument
2756 call assert_fails("call g:Xsetlist([{}], ' ', {})", 'E475:')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002757endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002758
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002759func Test_qf_property()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002760 call Xproperty_tests('c')
2761 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002762endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002763
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002764" Test for setting the current index in the location/quickfix list
2765func Xtest_setqfidx(cchar)
2766 call s:setup_commands(a:cchar)
2767
2768 Xgetexpr "F1:10:1:Line1\nF2:20:2:Line2\nF3:30:3:Line3"
2769 Xgetexpr "F4:10:1:Line1\nF5:20:2:Line2\nF6:30:3:Line3"
2770 Xgetexpr "F7:10:1:Line1\nF8:20:2:Line2\nF9:30:3:Line3"
2771
2772 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 2})
2773 call g:Xsetlist([], 'a', {'nr' : 2, 'idx' : 2})
2774 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 3})
2775 Xolder 2
2776 Xopen
2777 call assert_equal(3, line('.'))
2778 Xnewer
2779 call assert_equal(2, line('.'))
2780 Xnewer
2781 call assert_equal(2, line('.'))
2782 " Update the current index with the quickfix window open
2783 wincmd w
2784 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 3})
2785 Xopen
2786 call assert_equal(3, line('.'))
2787 Xclose
2788
2789 " Set the current index to the last entry
2790 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : '$'})
2791 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2792 " A large value should set the index to the last index
2793 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 1})
2794 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 999})
2795 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2796 " Invalid index values
2797 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : -1})
2798 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2799 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 0})
2800 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2801 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 'xx'})
2802 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2803 call assert_fails("call g:Xsetlist([], 'a', {'nr':1, 'idx':[]})", 'E745:')
2804
2805 call g:Xsetlist([], 'f')
2806 new | only
2807endfunc
2808
2809func Test_setqfidx()
2810 call Xtest_setqfidx('c')
2811 call Xtest_setqfidx('l')
2812endfunc
2813
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002814" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002815func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002816 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002817endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002818
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002819func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002820 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
2821 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
2822
2823 let g:acmds = []
2824 cexpr "F1:10:Line 10"
2825 caddexpr "F1:20:Line 20"
2826 cgetexpr "F1:30:Line 30"
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002827 cexpr ""
2828 caddexpr ""
2829 cgetexpr ""
2830 silent! cexpr non_existing_func()
2831 silent! caddexpr non_existing_func()
2832 silent! cgetexpr non_existing_func()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002833 let l =<< trim END
2834 precexpr
2835 postcexpr
2836 precaddexpr
2837 postcaddexpr
2838 precgetexpr
2839 postcgetexpr
2840 precexpr
2841 postcexpr
2842 precaddexpr
2843 postcaddexpr
2844 precgetexpr
2845 postcgetexpr
2846 precexpr
2847 precaddexpr
2848 precgetexpr
2849 END
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002850 call assert_equal(l, g:acmds)
2851
2852 let g:acmds = []
2853 enew! | call append(0, "F2:10:Line 10")
2854 cbuffer!
2855 enew! | call append(0, "F2:20:Line 20")
2856 cgetbuffer
2857 enew! | call append(0, "F2:30:Line 30")
2858 caddbuffer
2859 new
2860 let bnum = bufnr('%')
2861 bunload
2862 exe 'silent! cbuffer! ' . bnum
2863 exe 'silent! cgetbuffer ' . bnum
2864 exe 'silent! caddbuffer ' . bnum
2865 enew!
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002866 let l =<< trim END
2867 precbuffer
2868 postcbuffer
2869 precgetbuffer
2870 postcgetbuffer
2871 precaddbuffer
2872 postcaddbuffer
2873 precbuffer
2874 precgetbuffer
2875 precaddbuffer
2876 END
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002877 call assert_equal(l, g:acmds)
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002878
2879 call writefile(['Xtest:1:Line1'], 'Xtest')
2880 call writefile([], 'Xempty')
2881 let g:acmds = []
2882 cfile Xtest
2883 caddfile Xtest
2884 cgetfile Xtest
2885 cfile Xempty
2886 caddfile Xempty
2887 cgetfile Xempty
2888 silent! cfile do_not_exist
2889 silent! caddfile do_not_exist
2890 silent! cgetfile do_not_exist
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002891 let l =<< trim END
2892 precfile
2893 postcfile
2894 precaddfile
2895 postcaddfile
2896 precgetfile
2897 postcgetfile
2898 precfile
2899 postcfile
2900 precaddfile
2901 postcaddfile
2902 precgetfile
2903 postcgetfile
2904 precfile
2905 postcfile
2906 precaddfile
2907 postcaddfile
2908 precgetfile
2909 postcgetfile
2910 END
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002911 call assert_equal(l, g:acmds)
2912
2913 let g:acmds = []
2914 helpgrep quickfix
2915 silent! helpgrep non_existing_help_topic
2916 vimgrep test Xtest
2917 vimgrepadd test Xtest
2918 silent! vimgrep non_existing_test Xtest
2919 silent! vimgrepadd non_existing_test Xtest
2920 set makeprg=
2921 silent! make
2922 set makeprg&
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002923 let l =<< trim END
2924 prehelpgrep
2925 posthelpgrep
2926 prehelpgrep
2927 posthelpgrep
2928 previmgrep
2929 postvimgrep
2930 previmgrepadd
2931 postvimgrepadd
2932 previmgrep
2933 postvimgrep
2934 previmgrepadd
2935 postvimgrepadd
2936 premake
2937 postmake
2938 END
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002939 call assert_equal(l, g:acmds)
2940
2941 if has('unix')
2942 " Run this test only on Unix-like systems. The grepprg may not be set on
2943 " non-Unix systems.
2944 " The following lines are used for the grep test. Don't remove.
2945 " Grep_Autocmd_Text: Match 1
2946 " GrepAdd_Autocmd_Text: Match 2
2947 let g:acmds = []
2948 silent grep Grep_Autocmd_Text test_quickfix.vim
2949 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2950 silent grep abc123def Xtest
2951 silent grepadd abc123def Xtest
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002952 set grepprg=internal
2953 silent grep Grep_Autocmd_Text test_quickfix.vim
2954 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2955 silent lgrep Grep_Autocmd_Text test_quickfix.vim
2956 silent lgrepadd GrepAdd_Autocmd_Text test_quickfix.vim
2957 set grepprg&vim
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002958 let l =<< trim END
2959 pregrep
2960 postgrep
2961 pregrepadd
2962 postgrepadd
2963 pregrep
2964 postgrep
2965 pregrepadd
2966 postgrepadd
2967 pregrep
2968 postgrep
2969 pregrepadd
2970 postgrepadd
2971 prelgrep
2972 postlgrep
2973 prelgrepadd
2974 postlgrepadd
2975 END
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002976 call assert_equal(l, g:acmds)
2977 endif
2978
2979 call delete('Xtest')
2980 call delete('Xempty')
Bram Moolenaarb254af32017-12-18 19:48:58 +01002981 au! QuickFixCmdPre
2982 au! QuickFixCmdPost
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002983endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01002984
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002985func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01002986 set efm=%m
2987 lgetexpr '?'
2988
2989 try
2990 call DoesNotExit()
2991 catch
2992 lgetexpr '1'
2993 finally
2994 lgetexpr '1'
2995 endtry
2996
2997 call assert_equal('1', getloclist(0)[0].text)
2998
2999 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01003000endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01003001
Bram Moolenaar2b946c92016-11-12 18:14:44 +01003002func Test_caddbuffer_wrong()
3003 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01003004 let save_efm = &efm
3005 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
3006 cgetexpr ['WWWW', 'EEEE', 'CCCC']
3007 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01003008 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01003009 bwipe!
3010endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01003011
3012func Test_caddexpr_wrong()
3013 " This used to cause a memory access in freed memory.
3014 cbuffer
3015 cbuffer
3016 copen
3017 let save_efm = &efm
3018 set efm=%
3019 call assert_fails('caddexpr ""', 'E376:')
3020 let &efm = save_efm
3021endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01003022
3023func Test_dirstack_cleanup()
3024 " This used to cause a memory access in freed memory.
3025 let save_efm = &efm
3026 lexpr '0'
3027 lopen
3028 fun X(c)
3029 let save_efm=&efm
3030 set efm=%D%f
3031 if a:c == 'c'
3032 caddexpr '::'
3033 else
3034 laddexpr ':0:0'
3035 endif
3036 let &efm=save_efm
3037 endfun
3038 call X('c')
3039 call X('l')
3040 call setqflist([], 'r')
3041 caddbuffer
3042 let &efm = save_efm
3043endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01003044
3045" Tests for jumping to entries from the location list window and quickfix
3046" window
3047func Test_cwindow_jump()
3048 set efm=%f%%%l%%%m
3049 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3050 lopen | only
3051 lfirst
3052 call assert_true(winnr('$') == 2)
3053 call assert_true(winnr() == 1)
3054 " Location list for the new window should be set
3055 call assert_true(getloclist(0)[2].text == 'Line 30')
3056
3057 " Open a scratch buffer
3058 " Open a new window and create a location list
3059 " Open the location list window and close the other window
3060 " Jump to an entry.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003061 " Should create a new window and jump to the entry. The scratch buffer
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01003062 " should not be used.
3063 enew | only
3064 set buftype=nofile
3065 below new
3066 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3067 lopen
3068 2wincmd c
3069 lnext
3070 call assert_true(winnr('$') == 3)
3071 call assert_true(winnr() == 2)
3072
3073 " Open two windows with two different location lists
3074 " Open the location list window and close the previous window
3075 " Jump to an entry in the location list window
3076 " Should open the file in the first window and not set the location list.
3077 enew | only
3078 lgetexpr ["F1%5%Line 5"]
3079 below new
3080 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3081 lopen
3082 2wincmd c
3083 lnext
3084 call assert_true(winnr() == 1)
3085 call assert_true(getloclist(0)[0].text == 'Line 5')
3086
3087 enew | only
3088 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3089 copen
3090 cnext
3091 call assert_true(winnr('$') == 2)
3092 call assert_true(winnr() == 1)
3093
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003094 " open the quickfix buffer in two windows and jump to an entry. Should open
3095 " the file in the first quickfix window.
3096 enew | only
3097 copen
3098 let bnum = bufnr('')
3099 exe 'sbuffer ' . bnum
3100 wincmd b
3101 cfirst
3102 call assert_equal(2, winnr())
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003103 call assert_equal('F1', @%)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003104 enew | only
3105 exe 'sb' bnum
3106 exe 'botright sb' bnum
3107 wincmd t
3108 clast
3109 call assert_equal(2, winnr())
3110 call assert_equal('quickfix', getwinvar(1, '&buftype'))
3111 call assert_equal('quickfix', getwinvar(3, '&buftype'))
3112
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003113 " Jumping to a file from the location list window should find a usable
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01003114 " window by wrapping around the window list.
3115 enew | only
3116 call setloclist(0, [], 'f')
3117 new | new
3118 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3119 lopen
3120 1close
3121 call assert_equal(0, getloclist(3, {'id' : 0}).id)
3122 lnext
3123 call assert_equal(3, winnr())
3124 call assert_equal(getloclist(1, {'id' : 0}).id, getloclist(3, {'id' : 0}).id)
3125
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01003126 enew | only
3127 set efm&vim
3128endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01003129
Bram Moolenaare00fdf32019-09-15 19:09:42 +02003130func Test_cwindow_highlight()
3131 CheckScreendump
3132
3133 let lines =<< trim END
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003134 call setline(1, ['some', 'text', 'with', 'matches'])
3135 write XCwindow
3136 vimgrep e XCwindow
3137 redraw
3138 cwindow 4
Bram Moolenaare00fdf32019-09-15 19:09:42 +02003139 END
3140 call writefile(lines, 'XtestCwindow')
3141 let buf = RunVimInTerminal('-S XtestCwindow', #{rows: 12})
3142 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_1', {})
3143
3144 call term_sendkeys(buf, ":cnext\<CR>")
3145 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_2', {})
3146
Bram Moolenaar7fe956d2022-07-03 14:21:09 +01003147 call term_sendkeys(buf, "\<C-W>j:set cursorline\<CR>")
3148 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_3', {})
3149
3150 call term_sendkeys(buf, "j")
3151 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_4', {})
3152
Bram Moolenaare00fdf32019-09-15 19:09:42 +02003153 " clean up
3154 call StopVimInTerminal(buf)
3155 call delete('XtestCwindow')
3156 call delete('XCwindow')
3157endfunc
3158
Bram Moolenaaree85df32017-03-19 14:19:50 +01003159func XvimgrepTests(cchar)
3160 call s:setup_commands(a:cchar)
3161
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003162 let lines =<< trim END
3163 Editor:VIM vim
3164 Editor:Emacs EmAcS
3165 Editor:Notepad NOTEPAD
3166 END
3167 call writefile(lines, 'Xtestfile1')
Dominique Pelle923dce22021-11-21 11:36:04 +00003168 call writefile(['Linux', 'macOS', 'MS-Windows'], 'Xtestfile2')
Bram Moolenaaree85df32017-03-19 14:19:50 +01003169
3170 " Error cases
3171 call assert_fails('Xvimgrep /abc *', 'E682:')
3172
3173 let @/=''
3174 call assert_fails('Xvimgrep // *', 'E35:')
3175
3176 call assert_fails('Xvimgrep abc', 'E683:')
3177 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
3178 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
3179
3180 Xexpr ""
3181 Xvimgrepadd Notepad Xtestfile1
Dominique Pelle923dce22021-11-21 11:36:04 +00003182 Xvimgrepadd macOS Xtestfile2
Bram Moolenaaree85df32017-03-19 14:19:50 +01003183 let l = g:Xgetlist()
3184 call assert_equal(2, len(l))
3185 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
3186
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003187 10Xvimgrep #\cvim#g Xtestfile?
Bram Moolenaaree85df32017-03-19 14:19:50 +01003188 let l = g:Xgetlist()
3189 call assert_equal(2, len(l))
3190 call assert_equal(8, l[0].col)
thinca6864efa2021-06-19 20:45:20 +02003191 call assert_equal(11, l[0].end_col)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003192 call assert_equal(12, l[1].col)
thinca6864efa2021-06-19 20:45:20 +02003193 call assert_equal(15, l[1].end_col)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003194
3195 1Xvimgrep ?Editor? Xtestfile*
3196 let l = g:Xgetlist()
3197 call assert_equal(1, len(l))
3198 call assert_equal('Editor:VIM vim', l[0].text)
3199
3200 edit +3 Xtestfile2
3201 Xvimgrep +\cemacs+j Xtestfile1
3202 let l = g:Xgetlist()
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003203 call assert_equal('Xtestfile2', @%)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003204 call assert_equal('Editor:Emacs EmAcS', l[0].text)
3205
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02003206 " Test for unloading a buffer after vimgrep searched the buffer
3207 %bwipe
3208 Xvimgrep /Editor/j Xtestfile*
3209 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
3210 call assert_equal([], getbufinfo('Xtestfile2'))
3211
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00003212 " Test for opening the dummy buffer used by vimgrep in a window. The new
3213 " window should be closed
3214 %bw!
3215 augroup QF_Test
3216 au!
3217 autocmd BufReadPre * exe "sb " .. expand("<abuf>")
3218 augroup END
3219 call assert_fails("Xvimgrep /sublime/ Xtestfile1", 'E480:')
3220 call assert_equal(1, winnr('$'))
3221 augroup QF_Test
3222 au!
3223 augroup END
3224
Bram Moolenaaree85df32017-03-19 14:19:50 +01003225 call delete('Xtestfile1')
3226 call delete('Xtestfile2')
3227endfunc
3228
3229" Tests for the :vimgrep command
3230func Test_vimgrep()
3231 call XvimgrepTests('c')
3232 call XvimgrepTests('l')
3233endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003234
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01003235func Test_vimgrep_wildcards_expanded_once()
3236 new X[id-01] file.txt
3237 call setline(1, 'some text to search for')
3238 vimgrep text %
3239 bwipe!
3240endfunc
3241
Bram Moolenaar1c299432018-10-28 14:36:09 +01003242" Test for incsearch highlighting of the :vimgrep pattern
3243" This test used to cause "E315: ml_get: invalid lnum" errors.
3244func Test_vimgrep_incsearch()
3245 enew
3246 set incsearch
3247 call test_override("char_avail", 1)
3248
3249 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
3250 let l = getqflist()
3251 call assert_equal(2, len(l))
3252
3253 call test_override("ALL", 0)
3254 set noincsearch
3255endfunc
3256
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003257" Test vimgrep with the last search pattern not set
3258func Test_vimgrep_with_no_last_search_pat()
3259 let lines =<< trim [SCRIPT]
3260 call assert_fails('vimgrep // *', 'E35:')
3261 call writefile(v:errors, 'Xresult')
3262 qall!
3263 [SCRIPT]
3264 call writefile(lines, 'Xscript')
3265 if RunVim([], [], '--clean -S Xscript')
3266 call assert_equal([], readfile('Xresult'))
3267 endif
3268 call delete('Xscript')
3269 call delete('Xresult')
3270endfunc
3271
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02003272" Test vimgrep without swap file
3273func Test_vimgrep_without_swap_file()
3274 let lines =<< trim [SCRIPT]
3275 vimgrep grep test_c*
3276 call writefile(['done'], 'Xresult')
3277 qall!
3278 [SCRIPT]
3279 call writefile(lines, 'Xscript')
3280 if RunVim([], [], '--clean -n -S Xscript Xscript')
3281 call assert_equal(['done'], readfile('Xresult'))
3282 endif
3283 call delete('Xscript')
3284 call delete('Xresult')
3285endfunc
3286
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02003287func Test_vimgrep_existing_swapfile()
3288 call writefile(['match apple with apple'], 'Xapple')
3289 call writefile(['swapfile'], '.Xapple.swp')
3290 let g:foundSwap = 0
3291 let g:ignoreSwapExists = 1
3292 augroup grep
3293 au SwapExists * let foundSwap = 1 | let v:swapchoice = 'e'
3294 augroup END
3295 vimgrep apple Xapple
3296 call assert_equal(1, g:foundSwap)
3297 call assert_match('.Xapple.swo', swapname(''))
3298
3299 call delete('Xapple')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003300 call delete('.Xapple.swp')
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02003301 augroup grep
3302 au! SwapExists
3303 augroup END
3304 unlet g:ignoreSwapExists
3305endfunc
3306
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003307func XfreeTests(cchar)
3308 call s:setup_commands(a:cchar)
3309
3310 enew | only
3311
3312 " Deleting the quickfix stack should work even When the current list is
3313 " somewhere in the middle of the stack
3314 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3315 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3316 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3317 Xolder
3318 call g:Xsetlist([], 'f')
3319 call assert_equal(0, len(g:Xgetlist()))
3320
3321 " After deleting the stack, adding a new list should create a stack with a
3322 " single list.
3323 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3324 call assert_equal(1, g:Xgetlist({'all':1}).nr)
3325
3326 " Deleting the stack from a quickfix window should update/clear the
3327 " quickfix/location list window.
3328 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3329 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3330 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3331 Xolder
3332 Xwindow
3333 call g:Xsetlist([], 'f')
3334 call assert_equal(2, winnr('$'))
3335 call assert_equal(1, line('$'))
3336 Xclose
3337
3338 " Deleting the stack from a non-quickfix window should update/clear the
3339 " quickfix/location list window.
3340 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3341 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3342 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3343 Xolder
3344 Xwindow
3345 wincmd p
3346 call g:Xsetlist([], 'f')
3347 call assert_equal(0, len(g:Xgetlist()))
3348 wincmd p
3349 call assert_equal(2, winnr('$'))
3350 call assert_equal(1, line('$'))
3351
3352 " After deleting the location list stack, if the location list window is
3353 " opened, then a new location list should be created. So opening the
3354 " location list window again should not create a new window.
3355 if a:cchar == 'l'
3356 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3357 wincmd p
3358 lopen
3359 call assert_equal(2, winnr('$'))
3360 endif
3361 Xclose
3362endfunc
3363
Bram Moolenaar74240d32017-12-10 15:26:15 +01003364" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003365func Test_qf_free()
3366 call XfreeTests('c')
3367 call XfreeTests('l')
3368endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02003369
3370" Test for buffer overflow when parsing lines and adding new entries to
3371" the quickfix list.
3372func Test_bufoverflow()
3373 set efm=%f:%l:%m
3374 cgetexpr ['File1:100:' . repeat('x', 1025)]
3375
3376 set efm=%+GCompiler:\ %.%#,%f:%l:%m
3377 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
3378
3379 set efm=%DEntering\ directory\ %f,%f:%l:%m
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01003380 let lines =<< trim eval END
3381 Entering directory $"{repeat('a', 1006)}"
3382 File1:10:Hello World
3383 END
3384 cgetexpr lines
Bram Moolenaar6e62da32017-05-28 08:16:25 +02003385 set efm&vim
3386endfunc
3387
Bram Moolenaar875feea2017-06-11 16:07:51 +02003388" Tests for getting the quickfix stack size
3389func XsizeTests(cchar)
3390 call s:setup_commands(a:cchar)
3391
3392 call g:Xsetlist([], 'f')
3393 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003394 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
3395 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02003396
3397 Xexpr "File1:10:Line1"
3398 Xexpr "File2:20:Line2"
3399 Xexpr "File3:30:Line3"
3400 Xolder | Xolder
3401 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
3402 call g:Xsetlist([], 'f')
3403
3404 Xexpr "File1:10:Line1"
3405 Xexpr "File2:20:Line2"
3406 Xexpr "File3:30:Line3"
3407 Xolder | Xolder
3408 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
3409 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
3410endfunc
3411
3412func Test_Qf_Size()
3413 call XsizeTests('c')
3414 call XsizeTests('l')
3415endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02003416
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003417func Test_cclose_from_copen()
3418 augroup QF_Test
3419 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003420 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003421 augroup END
3422 copen
3423 augroup QF_Test
3424 au!
3425 augroup END
3426 augroup! QF_Test
3427endfunc
3428
Bram Moolenaar18141832017-06-25 21:17:25 +02003429func Test_cclose_in_autocmd()
3430 " Problem is only triggered if "starting" is zero, so that the OptionsSet
3431 " event will be triggered.
3432 call test_override('starting', 1)
3433 augroup QF_Test
3434 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003435 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar18141832017-06-25 21:17:25 +02003436 augroup END
3437 copen
3438 augroup QF_Test
3439 au!
3440 augroup END
3441 augroup! QF_Test
3442 call test_override('starting', 0)
3443endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003444
Bram Moolenaar379fb762018-08-30 15:58:28 +02003445" Check that ":file" without an argument is possible even when "curbuf_lock"
3446" is set.
3447func Test_file_from_copen()
3448 " Works without argument.
3449 augroup QF_Test
3450 au!
3451 au FileType qf file
3452 augroup END
3453 copen
3454
3455 augroup QF_Test
3456 au!
3457 augroup END
3458 cclose
3459
3460 " Fails with argument.
3461 augroup QF_Test
3462 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003463 au FileType qf call assert_fails(':file foo', 'E788:')
Bram Moolenaar379fb762018-08-30 15:58:28 +02003464 augroup END
3465 copen
3466 augroup QF_Test
3467 au!
3468 augroup END
3469 cclose
3470
3471 augroup! QF_Test
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02003472endfunc
Bram Moolenaar379fb762018-08-30 15:58:28 +02003473
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003474func Test_resize_from_copen()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003475 augroup QF_Test
3476 au!
3477 au FileType qf resize 5
3478 augroup END
3479 try
3480 " This should succeed without any exception. No other buffers are
3481 " involved in the autocmd.
3482 copen
3483 finally
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003484 augroup QF_Test
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003485 au!
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003486 augroup END
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003487 augroup! QF_Test
3488 endtry
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003489endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02003490
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003491func Test_vimgrep_with_textlock()
3492 new
3493
zeertzjqcfe45652022-05-27 17:26:55 +01003494 " Simple way to execute something with "textlock" set.
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003495 " Check that vimgrep without jumping can be executed.
3496 au InsertCharPre * vimgrep /RunTheTest/j runtest.vim
3497 normal ax
3498 let qflist = getqflist()
3499 call assert_true(len(qflist) > 0)
3500 call assert_match('RunTheTest', qflist[0].text)
3501 call setqflist([], 'r')
3502 au! InsertCharPre
3503
3504 " Check that vimgrepadd without jumping can be executed.
3505 au InsertCharPre * vimgrepadd /RunTheTest/j runtest.vim
3506 normal ax
3507 let qflist = getqflist()
3508 call assert_true(len(qflist) > 0)
3509 call assert_match('RunTheTest', qflist[0].text)
3510 call setqflist([], 'r')
3511 au! InsertCharPre
3512
3513 " Check that lvimgrep without jumping can be executed.
3514 au InsertCharPre * lvimgrep /RunTheTest/j runtest.vim
3515 normal ax
3516 let qflist = getloclist(0)
3517 call assert_true(len(qflist) > 0)
3518 call assert_match('RunTheTest', qflist[0].text)
3519 call setloclist(0, [], 'r')
3520 au! InsertCharPre
3521
3522 " Check that lvimgrepadd without jumping can be executed.
3523 au InsertCharPre * lvimgrepadd /RunTheTest/j runtest.vim
3524 normal ax
3525 let qflist = getloclist(0)
3526 call assert_true(len(qflist) > 0)
3527 call assert_match('RunTheTest', qflist[0].text)
3528 call setloclist(0, [], 'r')
3529 au! InsertCharPre
3530
3531 " trying to jump will give an error
3532 au InsertCharPre * vimgrep /RunTheTest/ runtest.vim
3533 call assert_fails('normal ax', 'E565:')
3534 au! InsertCharPre
3535
3536 au InsertCharPre * vimgrepadd /RunTheTest/ runtest.vim
3537 call assert_fails('normal ax', 'E565:')
3538 au! InsertCharPre
3539
3540 au InsertCharPre * lvimgrep /RunTheTest/ runtest.vim
3541 call assert_fails('normal ax', 'E565:')
3542 au! InsertCharPre
3543
3544 au InsertCharPre * lvimgrepadd /RunTheTest/ runtest.vim
3545 call assert_fails('normal ax', 'E565:')
3546 au! InsertCharPre
3547
3548 bwipe!
3549endfunc
3550
Bram Moolenaara8788f42017-07-19 17:06:20 +02003551" Tests for the quickfix buffer b:changedtick variable
3552func Xchangedtick_tests(cchar)
3553 call s:setup_commands(a:cchar)
3554
3555 new | only
3556
3557 Xexpr "" | Xexpr "" | Xexpr ""
3558
3559 Xopen
3560 Xolder
3561 Xolder
3562 Xaddexpr "F1:10:Line10"
3563 Xaddexpr "F2:20:Line20"
3564 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
3565 call g:Xsetlist([], 'f')
3566 call assert_equal(8, getbufvar('%', 'changedtick'))
3567 Xclose
3568endfunc
3569
3570func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02003571 call Xchangedtick_tests('c')
3572 call Xchangedtick_tests('l')
3573endfunc
3574
3575" Tests for parsing an expression using setqflist()
3576func Xsetexpr_tests(cchar)
3577 call s:setup_commands(a:cchar)
3578
3579 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003580 call g:Xsetlist([], ' ', {'lines' : t})
3581 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003582
3583 let l = g:Xgetlist()
3584 call assert_equal(3, len(l))
3585 call assert_equal(20, l[1].lnum)
3586 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003587 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003588 let l = g:Xgetlist()
3589 call assert_equal(1, len(l))
3590 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003591 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
3592 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003593
3594 call g:Xsetlist([], 'f')
3595 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003596 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
3597 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
3598 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
3599 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003600 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
3601 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02003602
3603 " Adding entries using a custom efm
3604 set efm&
3605 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
3606 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
3607 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3608 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
3609 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
3610 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3611 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
3612 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003613endfunc
3614
3615func Test_setexpr()
3616 call Xsetexpr_tests('c')
3617 call Xsetexpr_tests('l')
3618endfunc
3619
3620" Tests for per quickfix/location list directory stack
3621func Xmultidirstack_tests(cchar)
3622 call s:setup_commands(a:cchar)
3623
3624 call g:Xsetlist([], 'f')
3625 Xexpr "" | Xexpr ""
3626
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003627 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
3628 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
3629 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
3630 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003631
3632 let l1 = g:Xgetlist({'nr':1, 'items':1})
3633 let l2 = g:Xgetlist({'nr':2, 'items':1})
3634 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
3635 call assert_equal(3, l1.items[1].lnum)
3636 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
3637 call assert_equal(5, l2.items[1].lnum)
3638endfunc
3639
3640func Test_multidirstack()
3641 call mkdir('Xone/a', 'p')
3642 call mkdir('Xtwo/a', 'p')
3643 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3644 call writefile(lines, 'Xone/a/one.txt')
3645 call writefile(lines, 'Xtwo/a/two.txt')
3646 let save_efm = &efm
3647 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
3648
3649 call Xmultidirstack_tests('c')
3650 call Xmultidirstack_tests('l')
3651
3652 let &efm = save_efm
3653 call delete('Xone', 'rf')
3654 call delete('Xtwo', 'rf')
3655endfunc
3656
3657" Tests for per quickfix/location list file stack
3658func Xmultifilestack_tests(cchar)
3659 call s:setup_commands(a:cchar)
3660
3661 call g:Xsetlist([], 'f')
3662 Xexpr "" | Xexpr ""
3663
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003664 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
3665 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
3666 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
3667 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003668
3669 let l1 = g:Xgetlist({'nr':1, 'items':1})
3670 let l2 = g:Xgetlist({'nr':2, 'items':1})
3671 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
3672 call assert_equal(3, l1.items[1].lnum)
3673 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
3674 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02003675
3676 " Test for start of a new error line in the same line where a previous
3677 " error line ends with a file stack.
3678 let efm_val = 'Error\ l%l\ in\ %f,'
3679 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003680 let lines =<< trim END
3681 (one.txt
3682 Error l4 in one.txt
3683 ) (two.txt
3684 Error l6 in two.txt
3685 )
3686 Error l8 in one.txt
3687 END
3688 let l = g:Xgetlist({'lines': lines, 'efm' : efm_val})
Bram Moolenaare333e792018-04-08 13:27:39 +02003689 call assert_equal(3, len(l.items))
3690 call assert_equal('one.txt', bufname(l.items[0].bufnr))
3691 call assert_equal(4, l.items[0].lnum)
3692 call assert_equal('one.txt', l.items[0].text)
3693 call assert_equal('two.txt', bufname(l.items[1].bufnr))
3694 call assert_equal(6, l.items[1].lnum)
3695 call assert_equal('two.txt', l.items[1].text)
3696 call assert_equal('one.txt', bufname(l.items[2].bufnr))
3697 call assert_equal(8, l.items[2].lnum)
3698 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02003699endfunc
3700
3701func Test_multifilestack()
3702 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3703 call writefile(lines, 'one.txt')
3704 call writefile(lines, 'two.txt')
3705 let save_efm = &efm
3706 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
3707
3708 call Xmultifilestack_tests('c')
3709 call Xmultifilestack_tests('l')
3710
3711 let &efm = save_efm
3712 call delete('one.txt')
3713 call delete('two.txt')
3714endfunc
3715
3716" Tests for per buffer 'efm' setting
3717func Test_perbuf_efm()
3718 call writefile(["File1-10-Line10"], 'one.txt')
3719 call writefile(["File2#20#Line20"], 'two.txt')
3720 set efm=%f#%l#%m
3721 new | only
3722 new
3723 setlocal efm=%f-%l-%m
3724 cfile one.txt
3725 wincmd w
3726 caddfile two.txt
3727
3728 let l = getqflist()
3729 call assert_equal(10, l[0].lnum)
3730 call assert_equal('Line20', l[1].text)
3731
3732 set efm&
3733 new | only
3734 call delete('one.txt')
3735 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02003736endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003737
3738" Open multiple help windows using ":lhelpgrep
3739" This test used to crash Vim
3740func Test_Multi_LL_Help()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003741 new | only
3742 lhelpgrep window
3743 lopen
3744 e#
3745 lhelpgrep buffer
3746 call assert_equal(3, winnr('$'))
3747 call assert_true(len(getloclist(1)) != 0)
3748 call assert_true(len(getloclist(2)) != 0)
3749 new | only
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003750endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02003751
3752" Tests for adding new quickfix lists using setqflist()
3753func XaddQf_tests(cchar)
3754 call s:setup_commands(a:cchar)
3755
3756 " Create a new list using ' ' for action
3757 call g:Xsetlist([], 'f')
3758 call g:Xsetlist([], ' ', {'title' : 'Test1'})
3759 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3760 call assert_equal(1, l.nr)
3761 call assert_equal('Test1', l.title)
3762
3763 " Create a new list using ' ' for action and '$' for 'nr'
3764 call g:Xsetlist([], 'f')
3765 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
3766 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3767 call assert_equal(1, l.nr)
3768 call assert_equal('Test2', l.title)
3769
3770 " Create a new list using 'a' for action
3771 call g:Xsetlist([], 'f')
3772 call g:Xsetlist([], 'a', {'title' : 'Test3'})
3773 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3774 call assert_equal(1, l.nr)
3775 call assert_equal('Test3', l.title)
3776
3777 " Create a new list using 'a' for action and '$' for 'nr'
3778 call g:Xsetlist([], 'f')
3779 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
3780 call g:Xsetlist([], 'a', {'title' : 'Test4'})
3781 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3782 call assert_equal(1, l.nr)
3783 call assert_equal('Test4', l.title)
3784
3785 " Adding a quickfix list should remove all the lists following the current
3786 " list.
3787 Xexpr "" | Xexpr "" | Xexpr ""
3788 silent! 10Xolder
3789 call g:Xsetlist([], ' ', {'title' : 'Test5'})
3790 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3791 call assert_equal(2, l.nr)
3792 call assert_equal('Test5', l.title)
3793
3794 " Add a quickfix list using '$' as the list number.
3795 let lastqf = g:Xgetlist({'nr':'$'}).nr
3796 silent! 99Xolder
3797 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
3798 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3799 call assert_equal(lastqf + 1, l.nr)
3800 call assert_equal('Test6', l.title)
3801
3802 " Add a quickfix list using 'nr' set to one more than the quickfix
3803 " list size.
3804 let lastqf = g:Xgetlist({'nr':'$'}).nr
3805 silent! 99Xolder
3806 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
3807 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3808 call assert_equal(lastqf + 1, l.nr)
3809 call assert_equal('Test7', l.title)
3810
3811 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
3812 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
3813 silent! 99Xolder
3814 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
3815 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3816 call assert_equal(10, l.nr)
3817 call assert_equal('Test8', l.title)
3818
3819 " Add a quickfix list using 'nr' set to a value greater than 10
3820 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
3821
3822 " Try adding a quickfix list with 'nr' set to a value greater than the
3823 " quickfix list size but less than 10.
3824 call g:Xsetlist([], 'f')
3825 Xexpr "" | Xexpr "" | Xexpr ""
3826 silent! 99Xolder
3827 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
3828
3829 " Add a quickfix list using 'nr' set to a some string or list
3830 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
3831endfunc
3832
3833func Test_add_qf()
3834 call XaddQf_tests('c')
3835 call XaddQf_tests('l')
3836endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003837
3838" Test for getting the quickfix list items from some text without modifying
3839" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003840func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003841 call s:setup_commands(a:cchar)
3842 call g:Xsetlist([], 'f')
3843
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003844 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003845 call assert_equal(2, len(l))
3846 call assert_equal(30, l[1].lnum)
3847
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003848 call assert_equal({}, g:Xgetlist({'lines' : 10}))
3849 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
3850 call assert_equal([], g:Xgetlist({'lines' : []}).items)
3851 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003852
Bram Moolenaar36538222017-09-02 19:51:44 +02003853 " Parse text using a custom efm
3854 set efm&
3855 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
3856 call assert_equal('Line30', l[0].text)
3857 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
3858 call assert_equal('File3:30:Line30', l[0].text)
3859 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
3860 call assert_equal({}, l)
3861 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
3862 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
3863
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003864 " Make sure that the quickfix stack is not modified
3865 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
3866endfunc
3867
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003868func Test_get_list_from_lines()
3869 call XgetListFromLines('c')
3870 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003871endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003872
3873" Tests for the quickfix list id
3874func Xqfid_tests(cchar)
3875 call s:setup_commands(a:cchar)
3876
3877 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003878 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003879 Xexpr ''
3880 let start_id = g:Xgetlist({'id' : 0}).id
3881 Xexpr '' | Xexpr ''
3882 Xolder
3883 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
3884 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
3885 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003886 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003887 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003888 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3889 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003890
3891 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3892 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003893 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003894 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3895 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3896 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3897
3898 let qfid = g:Xgetlist({'id':0, 'nr':0})
3899 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003900 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003901endfunc
3902
3903func Test_qf_id()
3904 call Xqfid_tests('c')
3905 call Xqfid_tests('l')
3906endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003907
3908func Xqfjump_tests(cchar)
3909 call s:setup_commands(a:cchar)
3910
3911 call writefile(["Line1\tFoo", "Line2"], 'F1')
3912 call writefile(["Line1\tBar", "Line2"], 'F2')
3913 call writefile(["Line1\tBaz", "Line2"], 'F3')
3914
3915 call g:Xsetlist([], 'f')
3916
3917 " Tests for
3918 " Jumping to a line using a pattern
3919 " Jumping to a column greater than the last column in a line
3920 " Jumping to a line greater than the last line in the file
3921 let l = []
3922 for i in range(1, 7)
3923 call add(l, {})
3924 endfor
3925 let l[0].filename='F1'
3926 let l[0].pattern='Line1'
3927 let l[1].filename='F2'
3928 let l[1].pattern='Line1'
3929 let l[2].filename='F3'
3930 let l[2].pattern='Line1'
3931 let l[3].filename='F3'
3932 let l[3].lnum=1
3933 let l[3].col=9
3934 let l[3].vcol=1
3935 let l[4].filename='F3'
3936 let l[4].lnum=99
3937 let l[5].filename='F3'
3938 let l[5].lnum=1
3939 let l[5].col=99
3940 let l[5].vcol=1
3941 let l[6].filename='F3'
3942 let l[6].pattern='abcxyz'
3943
3944 call g:Xsetlist([], ' ', {'items' : l})
3945 Xopen | only
3946 2Xnext
3947 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003948 call assert_equal('F3', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +01003949 Xnext
3950 call assert_equal(7, col('.'))
3951 Xnext
3952 call assert_equal(2, line('.'))
3953 Xnext
3954 call assert_equal(9, col('.'))
3955 2
3956 Xnext
3957 call assert_equal(2, line('.'))
3958
3959 if a:cchar == 'l'
3960 " When jumping to a location list entry in the location list window and
3961 " no usable windows are available, then a new window should be opened.
3962 enew! | new | only
3963 call g:Xsetlist([], 'f')
3964 setlocal buftype=nofile
3965 new
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003966 let lines =<< trim END
3967 F1:1:1:Line1
3968 F1:2:2:Line2
3969 F2:1:1:Line1
3970 F2:2:2:Line2
3971 F3:1:1:Line1
3972 F3:2:2:Line2
3973 END
3974 call g:Xsetlist([], ' ', {'lines': lines})
Bram Moolenaar74240d32017-12-10 15:26:15 +01003975 Xopen
3976 let winid = win_getid()
3977 wincmd p
3978 close
3979 call win_gotoid(winid)
3980 Xnext
3981 call assert_equal(3, winnr('$'))
3982 call assert_equal(1, winnr())
3983 call assert_equal(2, line('.'))
3984
3985 " When jumping to an entry in the location list window and the window
3986 " associated with the location list is not present and a window containing
3987 " the file is already present, then that window should be used.
3988 close
3989 belowright new
3990 call g:Xsetlist([], 'f')
3991 edit F3
3992 call win_gotoid(winid)
3993 Xlast
3994 call assert_equal(3, winnr())
3995 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3996 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3997 endif
3998
3999 " Cleanup
4000 enew!
4001 new | only
4002
4003 call delete('F1')
4004 call delete('F2')
4005 call delete('F3')
4006endfunc
4007
4008func Test_qfjump()
4009 call Xqfjump_tests('c')
4010 call Xqfjump_tests('l')
4011endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01004012
4013" Tests for the getqflist() and getloclist() functions when the list is not
4014" present or is empty
4015func Xgetlist_empty_tests(cchar)
4016 call s:setup_commands(a:cchar)
4017
4018 " Empty quickfix stack
4019 call g:Xsetlist([], 'f')
4020 call assert_equal('', g:Xgetlist({'context' : 0}).context)
4021 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
4022 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
4023 call assert_equal([], g:Xgetlist({'items' : 0}).items)
4024 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
4025 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
4026 call assert_equal('', g:Xgetlist({'title' : 0}).title)
4027 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004028 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004029 if a:cchar == 'c'
4030 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004031 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004032 \ 'title' : '', 'winid' : 0, 'changedtick': 0,
4033 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004034 else
4035 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
4036 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004037 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004038 \ 'qfbufnr' : 0, 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004039 \ g:Xgetlist({'all' : 0}))
4040 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004041
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004042 " Quickfix window with empty stack
4043 silent! Xopen
4044 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004045 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004046 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
4047 Xclose
4048
Bram Moolenaara6d48492017-12-12 22:45:31 +01004049 " Empty quickfix list
4050 Xexpr ""
4051 call assert_equal('', g:Xgetlist({'context' : 0}).context)
4052 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
4053 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
4054 call assert_equal([], g:Xgetlist({'items' : 0}).items)
4055 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
4056 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
4057 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
4058 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004059 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01004060
4061 let qfid = g:Xgetlist({'id' : 0}).id
4062 call g:Xsetlist([], 'f')
4063
4064 " Non-existing quickfix identifier
4065 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
4066 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
4067 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
4068 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
4069 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
4070 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
4071 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
4072 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004073 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004074 if a:cchar == 'c'
4075 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4076 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004077 \ 'qfbufnr' : qfbufnr, 'quickfixtextfunc' : '',
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004078 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
4079 else
4080 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4081 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004082 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4083 \ 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004084 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
4085 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004086
4087 " Non-existing quickfix list number
4088 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
4089 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
4090 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
4091 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
4092 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
4093 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
4094 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
4095 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004096 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004097 if a:cchar == 'c'
4098 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4099 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004100 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr,
4101 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004102 else
4103 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4104 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004105 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4106 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004107 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004108endfunc
4109
Bram Moolenaar4f1b0832022-08-29 20:45:16 +01004110func Test_empty_list_quickfixtextfunc()
4111 " This was crashing. Can only reproduce by running it in a separate Vim
4112 " instance.
4113 let lines =<< trim END
4114 func s:Func(o)
4115 cgetexpr '0'
4116 endfunc
4117 cope
4118 let &quickfixtextfunc = 's:Func'
4119 cgetfile [ex
4120 END
4121 call writefile(lines, 'Xquickfixtextfunc')
4122 call RunVim([], [], '-e -s -S Xquickfixtextfunc -c qa')
4123 call delete('Xquickfixtextfunc')
4124endfunc
4125
Bram Moolenaara6d48492017-12-12 22:45:31 +01004126func Test_getqflist()
4127 call Xgetlist_empty_tests('c')
4128 call Xgetlist_empty_tests('l')
4129endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01004130
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01004131func Test_getqflist_invalid_nr()
4132 " The following commands used to crash Vim
4133 cexpr ""
4134 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
4135
4136 " Cleanup
4137 call setqflist([], 'r')
4138endfunc
4139
Bram Moolenaarb254af32017-12-18 19:48:58 +01004140" Tests for the quickfix/location list changedtick
4141func Xqftick_tests(cchar)
4142 call s:setup_commands(a:cchar)
4143
4144 call g:Xsetlist([], 'f')
4145
4146 Xexpr "F1:10:Line10"
4147 let qfid = g:Xgetlist({'id' : 0}).id
4148 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4149 Xaddexpr "F2:20:Line20\nF2:21:Line21"
4150 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4151 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
4152 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
4153 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
4154 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
4155 call g:Xsetlist([], 'a', {'title' : 'New Title'})
4156 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
4157
4158 enew!
4159 call append(0, ["F5:50:L50", "F6:60:L60"])
4160 Xaddbuffer
4161 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
4162 enew!
4163
4164 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
4165 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
4166 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4167 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
4168 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
4169 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4170 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
4171 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4172 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4173 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
4174 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4175
4176 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
4177 Xfile Xone
4178 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4179 Xaddfile Xone
4180 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4181
4182 " Test case for updating a non-current quickfix list
4183 call g:Xsetlist([], 'f')
4184 Xexpr "F1:1:L1"
4185 Xexpr "F2:2:L2"
4186 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
4187 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4188 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
4189
4190 call delete("Xone")
4191endfunc
4192
4193func Test_qf_tick()
4194 call Xqftick_tests('c')
4195 call Xqftick_tests('l')
4196endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01004197
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02004198" Test helpgrep with lang specifier
4199func Xtest_helpgrep_with_lang_specifier(cchar)
4200 call s:setup_commands(a:cchar)
4201 Xhelpgrep Vim@en
4202 call assert_equal('help', &filetype)
4203 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
4204 new | only
4205endfunc
4206
4207func Test_helpgrep_with_lang_specifier()
4208 call Xtest_helpgrep_with_lang_specifier('c')
4209 call Xtest_helpgrep_with_lang_specifier('l')
4210endfunc
4211
Bram Moolenaar12237442017-12-19 12:38:52 +01004212" The following test used to crash Vim.
4213" Open the location list window and close the regular window associated with
4214" the location list. When the garbage collection runs now, it incorrectly
4215" marks the location list context as not in use and frees the context.
4216func Test_ll_window_ctx()
4217 call setloclist(0, [], 'f')
4218 call setloclist(0, [], 'a', {'context' : []})
4219 lopen | only
4220 call test_garbagecollect_now()
4221 echo getloclist(0, {'context' : 1}).context
4222 enew | only
4223endfunc
4224
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004225" The following test used to crash vim
4226func Test_lfile_crash()
4227 sp Xtest
4228 au QuickFixCmdPre * bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02004229 call assert_fails('lfile', 'E40:')
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004230 au! QuickFixCmdPre
4231endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01004232
4233" The following test used to crash vim
4234func Test_lbuffer_crash()
4235 sv Xtest
4236 augroup QF_Test
4237 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004238 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bw
Bram Moolenaar3c097222017-12-21 20:54:49 +01004239 augroup END
4240 lbuffer
4241 augroup QF_Test
4242 au!
4243 augroup END
4244endfunc
4245
4246" The following test used to crash vim
4247func Test_lexpr_crash()
4248 augroup QF_Test
4249 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004250 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004251 augroup END
4252 lexpr ""
4253 augroup QF_Test
4254 au!
4255 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004256
Bram Moolenaar3c097222017-12-21 20:54:49 +01004257 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004258 augroup QF_Test
4259 au!
4260 au BufNew * call setloclist(0, [], 'f')
4261 augroup END
4262 lexpr 'x:1:x'
4263 augroup QF_Test
4264 au!
4265 augroup END
4266
4267 enew | only
4268 lexpr ''
4269 lopen
4270 augroup QF_Test
4271 au!
4272 au FileType * call setloclist(0, [], 'f')
4273 augroup END
4274 lexpr ''
4275 augroup QF_Test
4276 au!
4277 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01004278endfunc
4279
4280" The following test used to crash Vim
4281func Test_lvimgrep_crash()
4282 sv Xtest
4283 augroup QF_Test
4284 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004285 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004286 augroup END
4287 lvimgrep quickfix test_quickfix.vim
4288 augroup QF_Test
4289 au!
4290 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004291
4292 new | only
4293 augroup QF_Test
4294 au!
4295 au BufEnter * call setloclist(0, [], 'r')
4296 augroup END
4297 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
4298 augroup QF_Test
4299 au!
4300 augroup END
4301
Bram Moolenaar3c097222017-12-21 20:54:49 +01004302 enew | only
4303endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01004304
Bram Moolenaar2573af32020-03-14 17:21:34 +01004305func Test_lvimgrep_crash2()
4306 au BufNewFile x sfind
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004307 call assert_fails('lvimgrep x x', 'E471:')
4308 call assert_fails('lvimgrep x x x', 'E471:')
Bram Moolenaar2573af32020-03-14 17:21:34 +01004309
4310 au! BufNewFile
4311endfunc
4312
Bram Moolenaarde046542017-12-26 13:53:11 +01004313" Test for the position of the quickfix and location list window
4314func Test_qfwin_pos()
4315 " Open two windows
4316 new | only
4317 new
4318 cexpr ['F1:10:L10']
4319 copen
4320 " Quickfix window should be the bottom most window
4321 call assert_equal(3, winnr())
4322 close
4323 " Open at the very top
4324 wincmd t
4325 topleft copen
4326 call assert_equal(1, winnr())
4327 close
4328 " open left of the current window
4329 wincmd t
4330 below new
4331 leftabove copen
4332 call assert_equal(2, winnr())
4333 close
4334 " open right of the current window
4335 rightbelow copen
4336 call assert_equal(3, winnr())
4337 close
4338endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004339
4340" Tests for quickfix/location lists changed by autocommands when
4341" :vimgrep/:lvimgrep commands are running.
4342func Test_vimgrep_autocmd()
4343 call setqflist([], 'f')
4344 call writefile(['stars'], 'Xtest1.txt')
4345 call writefile(['stars'], 'Xtest2.txt')
4346
4347 " Test 1:
4348 " When searching for a pattern using :vimgrep, if the quickfix list is
4349 " changed by an autocmd, the results should be added to the correct quickfix
4350 " list.
4351 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
4352 silent vimgrep stars Xtest*.txt
4353 call assert_equal(1, getqflist({'nr' : 0}).nr)
4354 call assert_equal(3, getqflist({'nr' : '$'}).nr)
4355 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
4356 au! BufRead Xtest2.txt
4357
4358 " Test 2:
4359 " When searching for a pattern using :vimgrep, if the quickfix list is
4360 " freed, then a error should be given.
4361 silent! %bwipe!
4362 call setqflist([], 'f')
4363 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
4364 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
4365 au! BufRead Xtest2.txt
4366
4367 " Test 3:
4368 " When searching for a pattern using :lvimgrep, if the location list is
4369 " freed, then the command should error out.
4370 silent! %bwipe!
4371 let g:save_winid = win_getid()
4372 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
4373 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
4374 au! BufRead Xtest2.txt
4375
4376 call delete('Xtest1.txt')
4377 call delete('Xtest2.txt')
4378 call setqflist([], 'f')
4379endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004380
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004381" Test for an autocmd changing the current directory when running vimgrep
4382func Xvimgrep_autocmd_cd(cchar)
4383 call s:setup_commands(a:cchar)
4384
4385 %bwipe
4386 let save_cwd = getcwd()
4387
4388 augroup QF_Test
4389 au!
4390 autocmd BufRead * silent cd %:p:h
4391 augroup END
4392
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01004393 10Xvimgrep /vim/ Xgrepdir/**
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004394 let l = g:Xgetlist()
4395 call assert_equal('f1.txt', bufname(l[0].bufnr))
4396 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
4397
4398 augroup QF_Test
4399 au!
4400 augroup END
4401
4402 exe 'cd ' . save_cwd
4403endfunc
4404
4405func Test_vimgrep_autocmd_cd()
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01004406 call mkdir('Xgrepdir/a', 'p')
4407 call mkdir('Xgrepdir/b', 'p')
4408 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xgrepdir/a/f1.txt')
4409 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xgrepdir/b/f2.txt')
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004410 call Xvimgrep_autocmd_cd('c')
4411 call Xvimgrep_autocmd_cd('l')
4412 %bwipe
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01004413 call delete('Xgrepdir', 'rf')
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004414endfunc
4415
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004416" The following test used to crash Vim
4417func Test_lhelpgrep_autocmd()
4418 lhelpgrep quickfix
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004419 augroup QF_Test
4420 au!
4421 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
4422 augroup END
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004423 lhelpgrep buffer
4424 call assert_equal('help', &filetype)
4425 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
4426 lhelpgrep tabpage
4427 call assert_equal('help', &filetype)
4428 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004429 augroup QF_Test
4430 au!
4431 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004432
4433 new | only
4434 augroup QF_Test
4435 au!
4436 au BufEnter * call setqflist([], 'f')
4437 augroup END
4438 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaardbfa7952020-11-02 20:04:22 +01004439 " run the test with a help window already open
4440 help
4441 wincmd w
4442 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004443 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004444 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004445 augroup END
4446
4447 new | only
4448 augroup QF_Test
4449 au!
4450 au BufEnter * call setqflist([], 'r')
4451 augroup END
4452 call assert_fails('helpgrep quickfix', 'E925:')
4453 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004454 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004455 augroup END
4456
4457 new | only
4458 augroup QF_Test
4459 au!
4460 au BufEnter * call setloclist(0, [], 'r')
4461 augroup END
4462 call assert_fails('lhelpgrep quickfix', 'E926:')
4463 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004464 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004465 augroup END
4466
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004467 " Replace the contents of a help window location list when it is still in
4468 " use.
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004469 new | only
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004470 lhelpgrep quickfix
4471 wincmd w
4472 augroup QF_Test
4473 au!
4474 autocmd WinEnter * call setloclist(0, [], 'r')
4475 augroup END
4476 call assert_fails('lhelpgrep win_getid', 'E926:')
4477 augroup QF_Test
4478 au!
4479 augroup END
4480
4481 %bw!
4482endfunc
4483
4484" The following test used to crash Vim
4485func Test_lhelpgrep_autocmd_free_loclist()
4486 %bw!
4487 lhelpgrep quickfix
4488 wincmd w
4489 augroup QF_Test
4490 au!
4491 autocmd WinEnter * call setloclist(0, [], 'f')
4492 augroup END
4493 lhelpgrep win_getid
4494 wincmd w
4495 wincmd w
4496 wincmd w
4497 augroup QF_Test
4498 au!
4499 augroup END
4500 %bw!
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004501endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02004502
4503" Test for shortening/simplifying the file name when opening the
4504" quickfix window or when displaying the quickfix list
4505func Test_shorten_fname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02004506 CheckUnix
Bram Moolenaara796d462018-05-01 14:30:36 +02004507 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004508 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004509 let fname = getcwd() . '/test_quickfix.vim'
4510 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4511 call assert_equal(fname, bufname('test_quickfix.vim'))
4512 " Opening the quickfix window should simplify the file path
4513 cwindow
4514 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
4515 cclose
4516 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004517 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004518 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4519 call assert_equal(fname, bufname('test_quickfix.vim'))
4520 " Displaying the quickfix list should simplify the file path
4521 silent! clist
4522 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004523 " Add a few entries for the same file with different paths and check whether
4524 " the buffer name is shortened
4525 %bwipe
4526 call setqflist([], 'f')
4527 call setqflist([{'filename' : 'test_quickfix.vim', 'lnum' : 10},
4528 \ {'filename' : '../testdir/test_quickfix.vim', 'lnum' : 20},
4529 \ {'filename' : fname, 'lnum' : 30}], ' ')
4530 copen
4531 call assert_equal(['test_quickfix.vim|10| ',
4532 \ 'test_quickfix.vim|20| ',
4533 \ 'test_quickfix.vim|30| '], getline(1, '$'))
4534 cclose
Bram Moolenaara796d462018-05-01 14:30:36 +02004535endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004536
4537" Quickfix title tests
4538" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
4539" Otherwise due to indentation, the title is set with spaces at the beginning
4540" of the command.
4541func Test_qftitle()
4542 call writefile(["F1:1:Line1"], 'Xerr')
4543
4544 " :cexpr
4545 exe "cexpr readfile('Xerr')"
4546 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
4547
4548 " :cgetexpr
4549 exe "cgetexpr readfile('Xerr')"
4550 call assert_equal(":cgetexpr readfile('Xerr')",
4551 \ getqflist({'title' : 1}).title)
4552
4553 " :caddexpr
4554 call setqflist([], 'f')
4555 exe "caddexpr readfile('Xerr')"
4556 call assert_equal(":caddexpr readfile('Xerr')",
4557 \ getqflist({'title' : 1}).title)
4558
4559 " :cbuffer
4560 new Xerr
4561 exe "cbuffer"
4562 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
4563
4564 " :cgetbuffer
4565 edit Xerr
4566 exe "cgetbuffer"
4567 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
4568
4569 " :caddbuffer
4570 call setqflist([], 'f')
4571 edit Xerr
4572 exe "caddbuffer"
4573 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
4574
4575 " :cfile
4576 exe "cfile Xerr"
4577 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
4578
4579 " :cgetfile
4580 exe "cgetfile Xerr"
4581 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
4582
4583 " :caddfile
4584 call setqflist([], 'f')
4585 exe "caddfile Xerr"
4586 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
4587
4588 " :grep
4589 set grepprg=internal
4590 exe "grep F1 Xerr"
4591 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
4592
4593 " :grepadd
4594 call setqflist([], 'f')
4595 exe "grepadd F1 Xerr"
4596 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
4597 set grepprg&vim
4598
4599 " :vimgrep
4600 exe "vimgrep F1 Xerr"
4601 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
4602
4603 " :vimgrepadd
4604 call setqflist([], 'f')
4605 exe "vimgrepadd F1 Xerr"
4606 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
4607
4608 call setqflist(['F1:10:L10'], ' ')
4609 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4610
4611 call setqflist([], 'f')
4612 call setqflist(['F1:10:L10'], 'a')
4613 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4614
4615 call setqflist([], 'f')
4616 call setqflist(['F1:10:L10'], 'r')
4617 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4618
4619 close
4620 call delete('Xerr')
4621
4622 call setqflist([], ' ', {'title' : 'Errors'})
4623 copen
4624 call assert_equal('Errors', w:quickfix_title)
4625 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
4626 call assert_equal('Errors', w:quickfix_title)
4627 cclose
Bram Moolenaar530bed92020-12-16 21:02:56 +01004628
4629 " Switching to another quickfix list in one tab page should update the
4630 " quickfix window title and statusline in all the other tab pages also
4631 call setqflist([], 'f')
4632 %bw!
4633 cgetexpr ['file_one:1:1: error in the first quickfix list']
4634 call setqflist([], 'a', {'title': 'first quickfix list'})
4635 cgetexpr ['file_two:2:1: error in the second quickfix list']
4636 call setqflist([], 'a', {'title': 'second quickfix list'})
4637 copen
4638 wincmd t
4639 tabnew two
4640 copen
4641 wincmd t
4642 colder
4643 call assert_equal('first quickfix list', gettabwinvar(1, 2, 'quickfix_title'))
4644 call assert_equal('first quickfix list', gettabwinvar(2, 2, 'quickfix_title'))
4645 call assert_equal(1, tabpagewinnr(1))
4646 call assert_equal(1, tabpagewinnr(2))
4647 tabnew
4648 call setqflist([], 'a', {'title': 'new quickfix title'})
4649 call assert_equal('new quickfix title', gettabwinvar(1, 2, 'quickfix_title'))
4650 call assert_equal('new quickfix title', gettabwinvar(2, 2, 'quickfix_title'))
4651 %bw!
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004652endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02004653
4654func Test_lbuffer_with_bwipe()
4655 new
4656 new
4657 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004658 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bwipe
Bram Moolenaar600323b2018-06-16 22:16:47 +02004659 augroup END
4660 lbuffer
4661 augroup nasty
4662 au!
4663 augroup END
4664endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02004665
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004666" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
4667" running
4668func Xexpr_acmd_freelist(cchar)
4669 call s:setup_commands(a:cchar)
4670
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004671 " This was using freed memory (but with what events?)
Bram Moolenaar0366c012018-06-18 20:52:13 +02004672 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004673 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02004674 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004675 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02004676 augroup nasty
4677 au!
4678 augroup END
4679endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004680
4681func Test_cexpr_acmd_freelist()
4682 call Xexpr_acmd_freelist('c')
4683 call Xexpr_acmd_freelist('l')
4684endfunc
4685
4686" Test for commands that create a new quickfix/location list and jump to the
4687" first error automatically.
4688func Xjumpto_first_error_test(cchar)
4689 call s:setup_commands(a:cchar)
4690
4691 call s:create_test_file('Xtestfile1')
4692 call s:create_test_file('Xtestfile2')
4693 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4694
4695 " Test for cexpr/lexpr
4696 enew
4697 Xexpr l
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004698 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004699 call assert_equal(2, line('.'))
4700
4701 " Test for cfile/lfile
4702 enew
4703 call writefile(l, 'Xerr')
4704 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004705 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004706 call assert_equal(2, line('.'))
4707
4708 " Test for cbuffer/lbuffer
4709 edit Xerr
4710 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004711 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004712 call assert_equal(2, line('.'))
4713
4714 call delete('Xerr')
4715 call delete('Xtestfile1')
4716 call delete('Xtestfile2')
4717endfunc
4718
4719func Test_jumpto_first_error()
4720 call Xjumpto_first_error_test('c')
4721 call Xjumpto_first_error_test('l')
4722endfunc
4723
4724" Test for a quickfix autocmd changing the quickfix/location list before
4725" jumping to the first error in the new list.
4726func Xautocmd_changelist(cchar)
4727 call s:setup_commands(a:cchar)
4728
4729 " Test for cfile/lfile
4730 call s:create_test_file('Xtestfile1')
4731 call s:create_test_file('Xtestfile2')
4732 Xexpr 'Xtestfile1:2:Line2'
4733 autocmd QuickFixCmdPost * Xolder
4734 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4735 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004736 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004737 call assert_equal(4, line('.'))
4738 autocmd! QuickFixCmdPost
4739
4740 " Test for cbuffer/lbuffer
4741 call g:Xsetlist([], 'f')
4742 Xexpr 'Xtestfile1:2:Line2'
4743 autocmd QuickFixCmdPost * Xolder
4744 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4745 edit Xerr
4746 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004747 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004748 call assert_equal(4, line('.'))
4749 autocmd! QuickFixCmdPost
4750
4751 " Test for cexpr/lexpr
4752 call g:Xsetlist([], 'f')
4753 Xexpr 'Xtestfile1:2:Line2'
4754 autocmd QuickFixCmdPost * Xolder
4755 Xexpr 'Xtestfile2:4:Line4'
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004756 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004757 call assert_equal(4, line('.'))
4758 autocmd! QuickFixCmdPost
4759
Bram Moolenaar851332e2018-07-03 19:16:00 +02004760 " The grepprg may not be set on non-Unix systems
4761 if has('unix')
4762 " Test for grep/lgrep
4763 call g:Xsetlist([], 'f')
4764 Xexpr 'Xtestfile1:2:Line2'
4765 autocmd QuickFixCmdPost * Xolder
4766 silent Xgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004767 call assert_equal('Xtestfile2', @%)
Bram Moolenaar851332e2018-07-03 19:16:00 +02004768 call assert_equal(5, line('.'))
4769 autocmd! QuickFixCmdPost
4770 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004771
4772 " Test for vimgrep/lvimgrep
4773 call g:Xsetlist([], 'f')
4774 Xexpr 'Xtestfile1:2:Line2'
4775 autocmd QuickFixCmdPost * Xolder
4776 silent Xvimgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004777 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004778 call assert_equal(5, line('.'))
4779 autocmd! QuickFixCmdPost
4780
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004781 " Test for autocommands clearing the quickfix list before jumping to the
4782 " first error. This should not result in an error
4783 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
4784 let v:errmsg = ''
4785 " Test for cfile/lfile
4786 Xfile Xerr
4787 call assert_true(v:errmsg !~# 'E42:')
4788 " Test for cbuffer/lbuffer
4789 edit Xerr
4790 Xbuffer
4791 call assert_true(v:errmsg !~# 'E42:')
4792 " Test for cexpr/lexpr
4793 Xexpr 'Xtestfile2:4:Line4'
4794 call assert_true(v:errmsg !~# 'E42:')
4795 " Test for grep/lgrep
4796 " The grepprg may not be set on non-Unix systems
4797 if has('unix')
4798 silent Xgrep Line5 Xtestfile2
4799 call assert_true(v:errmsg !~# 'E42:')
4800 endif
4801 " Test for vimgrep/lvimgrep
4802 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
4803 autocmd! QuickFixCmdPost
4804
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004805 call delete('Xerr')
4806 call delete('Xtestfile1')
4807 call delete('Xtestfile2')
4808endfunc
4809
4810func Test_autocmd_changelist()
4811 call Xautocmd_changelist('c')
4812 call Xautocmd_changelist('l')
4813endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004814
4815" Tests for the ':filter /pat/ clist' command
4816func Test_filter_clist()
4817 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4818 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4819 \ split(execute('filter /Line 15/ clist'), "\n"))
4820 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4821 \ split(execute('filter /Xfile1/ clist'), "\n"))
4822 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4823
4824 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4825 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4826 call assert_equal([' 2 pqr:pat2: '],
4827 \ split(execute('filter /pqr/ clist'), "\n"))
4828 call assert_equal([' 1 abc:pat1: '],
4829 \ split(execute('filter /pat1/ clist'), "\n"))
4830endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004831
4832" Tests for the "CTRL-W <CR>" command.
4833func Xview_result_split_tests(cchar)
4834 call s:setup_commands(a:cchar)
4835
4836 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4837 call g:Xsetlist([])
4838 Xopen
4839 let l:win_count = winnr('$')
Bram Moolenaare2e40752020-09-04 21:18:46 +02004840 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42:')
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004841 call assert_equal(l:win_count, winnr('$'))
4842 Xclose
4843endfunc
4844
4845func Test_view_result_split()
4846 call Xview_result_split_tests('c')
4847 call Xview_result_split_tests('l')
4848endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004849
4850" Test that :cc sets curswant
4851func Test_curswant()
4852 helpgrep quickfix
4853 normal! llll
4854 1cc
4855 call assert_equal(getcurpos()[4], virtcol('.'))
4856 cclose | helpclose
4857endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004858
4859" Test for opening a file from the quickfix window using CTRL-W <Enter>
4860" doesn't leave an empty buffer around.
4861func Test_splitview()
4862 call s:create_test_file('Xtestfile1')
4863 call s:create_test_file('Xtestfile2')
4864 new | only
4865 let last_bufnr = bufnr('Test_sv_1', 1)
4866 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4867 cgetexpr l
4868 copen
4869 let numbufs = len(getbufinfo())
4870 exe "normal \<C-W>\<CR>"
4871 copen
4872 exe "normal j\<C-W>\<CR>"
4873 " Make sure new empty buffers are not created
4874 call assert_equal(numbufs, len(getbufinfo()))
4875 " Creating a new buffer should use the next available buffer number
4876 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4877 bwipe Test_sv_1
4878 bwipe Test_sv_2
4879 new | only
4880
4881 " When split opening files from location list window, make sure that two
4882 " windows doesn't refer to the same location list
4883 lgetexpr l
4884 let locid = getloclist(0, {'id' : 0}).id
4885 lopen
4886 exe "normal \<C-W>\<CR>"
4887 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4888 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4889 new | only
4890
4891 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004892 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004893 lhelpgrep window
4894 let locid = getloclist(0, {'id' : 0}).id
4895 lwindow
4896 exe "normal j\<C-W>\<CR>"
4897 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4898 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4899 new | only
4900
Bram Moolenaar406cd902020-02-18 21:54:41 +01004901 " Using :split or :vsplit from a quickfix window should behave like a :new
4902 " or a :vnew command
4903 copen
4904 split
4905 call assert_equal(3, winnr('$'))
4906 let l = getwininfo()
4907 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4908 close
4909 copen
4910 vsplit
4911 let l = getwininfo()
4912 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4913 new | only
4914
Bram Moolenaarb2443732018-11-11 22:50:27 +01004915 call delete('Xtestfile1')
4916 call delete('Xtestfile2')
4917endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004918
4919" Test for parsing entries using visual screen column
4920func Test_viscol()
4921 enew
4922 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4923 edit Xfile1
4924
4925 " Use byte offset for column number
4926 set efm&
4927 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
4928 call assert_equal([5, 8], [col('.'), virtcol('.')])
4929 cnext
4930 call assert_equal([9, 12], [col('.'), virtcol('.')])
4931 cnext
4932 call assert_equal([14, 20], [col('.'), virtcol('.')])
4933
4934 " Use screen column offset for column number
4935 set efm=%f:%l:%v:%m
4936 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4937 call assert_equal([5, 8], [col('.'), virtcol('.')])
4938 cnext
4939 call assert_equal([9, 12], [col('.'), virtcol('.')])
4940 cnext
4941 call assert_equal([14, 20], [col('.'), virtcol('.')])
4942 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4943 call assert_equal([5, 8], [col('.'), virtcol('.')])
4944 cnext
4945 call assert_equal([10, 16], [col('.'), virtcol('.')])
4946 cnext
4947 call assert_equal([14, 20], [col('.'), virtcol('.')])
4948
4949 enew
4950 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4951
4952 " Use byte offset for column number
4953 set efm&
4954 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
4955 call assert_equal([8, 10], [col('.'), virtcol('.')])
4956 cnext
4957 call assert_equal([11, 17], [col('.'), virtcol('.')])
4958 cnext
4959 call assert_equal([16, 25], [col('.'), virtcol('.')])
4960
4961 " Use screen column offset for column number
4962 set efm=%f:%l:%v:%m
4963 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4964 call assert_equal([8, 10], [col('.'), virtcol('.')])
4965 cnext
4966 call assert_equal([11, 17], [col('.'), virtcol('.')])
4967 cnext
4968 call assert_equal([16, 25], [col('.'), virtcol('.')])
4969
Bram Moolenaarc95940c2020-10-20 14:59:12 +02004970 " Use screen column number with a multi-line error message
4971 enew
4972 call writefile(["à test"], 'Xfile1')
4973 set efm=%E===\ %f\ ===,%C%l:%v,%Z%m
4974 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4975 call assert_equal('Xfile1', @%)
4976 call assert_equal([0, 1, 4, 0], getpos('.'))
4977
4978 " Repeat previous test with byte offset %c: ensure that fix to issue #7145
4979 " does not break this
4980 set efm=%E===\ %f\ ===,%C%l:%c,%Z%m
4981 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4982 call assert_equal('Xfile1', @%)
4983 call assert_equal([0, 1, 3, 0], getpos('.'))
4984
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004985 enew | only
4986 set efm&
4987 call delete('Xfile1')
4988endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004989
4990" Test for the quickfix window buffer
4991func Xqfbuf_test(cchar)
4992 call s:setup_commands(a:cchar)
4993
4994 " Quickfix buffer should be reused across closing and opening a quickfix
4995 " window
4996 Xexpr "F1:10:Line10"
4997 Xopen
4998 let qfbnum = bufnr('')
4999 Xclose
5000 " Even after the quickfix window is closed, the buffer should be loaded
5001 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005002 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005003 Xopen
5004 " Buffer should be reused when opening the window again
5005 call assert_equal(qfbnum, bufnr(''))
5006 Xclose
5007
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00005008 " When quickfix buffer is wiped out, getqflist() should return 0
5009 %bw!
5010 Xexpr ""
5011 Xopen
5012 bw!
5013 call assert_equal(0, g:Xgetlist({'qfbufnr': 0}).qfbufnr)
5014
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005015 if a:cchar == 'l'
5016 %bwipe
5017 " For a location list, when both the file window and the location list
5018 " window for the list are closed, then the buffer should be freed.
5019 new | only
5020 lexpr "F1:10:Line10"
5021 let wid = win_getid()
5022 lopen
5023 let qfbnum = bufnr('')
5024 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
5025 close
5026 " When the location list window is closed, the buffer name should not
5027 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005028 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005029 call assert_true(bufloaded(qfbnum))
5030
Bram Moolenaard82a81c2019-03-02 07:57:18 +01005031 " After deleting a location list buffer using ":bdelete", opening the
5032 " location list window should mark the buffer as a location list buffer.
5033 exe "bdelete " . qfbnum
5034 lopen
5035 call assert_equal("quickfix", &buftype)
5036 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
5037 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
5038 call assert_false(&swapfile)
5039 lclose
5040
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005041 " When the location list is cleared for the window, the buffer should be
5042 " removed
5043 call setloclist(0, [], 'f')
5044 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005045 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005046
5047 " When the location list is freed with the location list window open, the
5048 " location list buffer should not be lost. It should be reused when the
5049 " location list is again populated.
5050 lexpr "F1:10:Line10"
5051 lopen
5052 let wid = win_getid()
5053 let qfbnum = bufnr('')
5054 wincmd p
5055 call setloclist(0, [], 'f')
5056 lexpr "F1:10:Line10"
5057 lopen
5058 call assert_equal(wid, win_getid())
5059 call assert_equal(qfbnum, bufnr(''))
5060 lclose
5061
5062 " When the window with the location list is closed, the buffer should be
5063 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005064 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005065 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005066 endif
5067endfunc
5068
5069func Test_qfbuf()
5070 call Xqfbuf_test('c')
5071 call Xqfbuf_test('l')
5072endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005073
5074" If there is an autocmd to use only one window, then opening the location
5075" list window used to crash Vim.
5076func Test_winonly_autocmd()
5077 call s:create_test_file('Xtest1')
5078 " Autocmd to show only one Vim window at a time
5079 autocmd WinEnter * only
5080 new
5081 " Load the location list
5082 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
5083 let loclistid = getloclist(0, {'id' : 0}).id
5084 " Open the location list window. Only this window will be shown and the file
5085 " window is closed.
5086 lopen
5087 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
5088 " Jump to an entry in the location list and make sure that the cursor is
5089 " positioned correctly.
5090 ll 3
5091 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005092 call assert_equal('Xtest1', @%)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005093 call assert_equal(15, line('.'))
5094 " Cleanup
5095 autocmd! WinEnter
5096 new | only
5097 call delete('Xtest1')
5098endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02005099
5100" Test to make sure that an empty quickfix buffer is not reused for loading
5101" a normal buffer.
5102func Test_empty_qfbuf()
5103 enew | only
5104 call writefile(["Test"], 'Xfile1')
5105 call setqflist([], 'f')
5106 copen | only
5107 let qfbuf = bufnr('')
5108 edit Xfile1
5109 call assert_notequal(qfbuf, bufnr(''))
5110 enew
5111 call delete('Xfile1')
5112endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005113
5114" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005115" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005116func Xtest_below(cchar)
5117 call s:setup_commands(a:cchar)
5118
5119 " No quickfix/location list
5120 call assert_fails('Xbelow', 'E42:')
5121 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005122 call assert_fails('Xbefore', 'E42:')
5123 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005124
5125 " Empty quickfix/location list
5126 call g:Xsetlist([])
5127 call assert_fails('Xbelow', 'E42:')
5128 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005129 call assert_fails('Xbefore', 'E42:')
5130 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005131
5132 call s:create_test_file('X1')
5133 call s:create_test_file('X2')
5134 call s:create_test_file('X3')
5135 call s:create_test_file('X4')
5136
5137 " Invalid entries
5138 edit X1
5139 call g:Xsetlist(["E1", "E2"])
5140 call assert_fails('Xbelow', 'E42:')
5141 call assert_fails('Xabove', 'E42:')
5142 call assert_fails('3Xbelow', 'E42:')
5143 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005144 call assert_fails('Xbefore', 'E42:')
5145 call assert_fails('Xafter', 'E42:')
5146 call assert_fails('3Xbefore', 'E42:')
5147 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005148
5149 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005150 Xexpr ["X1:5:3:L5", "X2:5:2:L5", "X2:10:3:L10", "X2:15:4:L15", "X3:3:5:L3"]
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005151 edit +7 X2
5152 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005153 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005154 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005155 normal 7G
5156 Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005157 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005158 call assert_fails('Xbefore', 'E553:')
5159
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005160 normal 2j
5161 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005162 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005163 normal 7G
5164 Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005165 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005166
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005167 " Last error in this file
5168 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005169 call assert_equal(['X2', 15], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005170 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005171 normal gg
5172 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005173 call assert_equal(['X2', 15, 4], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005174 call assert_fails('Xafter', 'E553:')
5175
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005176 " First error in this file
5177 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005178 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005179 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005180 normal G
5181 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005182 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005183 call assert_fails('Xbefore', 'E553:')
5184
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005185 normal gg
5186 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005187 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005188 normal gg
5189 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005190 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005191
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005192 normal G
5193 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005194 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005195 normal G
5196 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005197 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005198
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005199 edit X4
5200 call assert_fails('Xabove', 'E42:')
5201 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005202 call assert_fails('Xbefore', 'E42:')
5203 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005204 if a:cchar == 'l'
5205 " If a buffer has location list entries from some other window but not
5206 " from the current window, then the commands should fail.
5207 edit X1 | split | call setloclist(0, [], 'f')
5208 call assert_fails('Xabove', 'E776:')
5209 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005210 call assert_fails('Xbefore', 'E776:')
5211 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005212 close
5213 endif
5214
5215 " Test for lines with multiple quickfix entries
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005216 let lines =<< trim END
5217 X1:5:L5
5218 X2:5:1:L5_1
5219 X2:5:2:L5_2
5220 X2:5:3:L5_3
5221 X2:10:1:L10_1
5222 X2:10:2:L10_2
5223 X2:10:3:L10_3
5224 X2:15:1:L15_1
5225 X2:15:2:L15_2
5226 X2:15:3:L15_3
5227 X3:3:L3
5228 END
5229 Xexpr lines
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005230 edit +1 X2
5231 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005232 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005233 normal 1G
5234 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005235 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005236
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005237 normal gg
5238 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005239 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005240 normal gg
5241 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005242 call assert_equal(['X2', 15, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005243
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005244 normal G
5245 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005246 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005247 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005248 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005249 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005250
5251 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005252 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005253 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005254 normal G
5255 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005256 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005257
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005258 normal 10G
5259 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005260 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005261 normal 10G$
5262 2Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005263 call assert_equal(['X2', 10, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005264
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005265 normal 10G
5266 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005267 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005268 normal 9G
5269 5Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005270 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005271
5272 " Invalid range
5273 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02005274 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005275 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005276 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02005277 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005278 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005279 endif
5280
5281 call delete('X1')
5282 call delete('X2')
5283 call delete('X3')
5284 call delete('X4')
5285endfunc
5286
5287func Test_cbelow()
5288 call Xtest_below('c')
5289 call Xtest_below('l')
5290endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02005291
5292func Test_quickfix_count()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005293 let commands =<< trim END
5294 cNext
5295 cNfile
5296 cabove
5297 cbelow
5298 cfirst
5299 clast
5300 cnewer
5301 cnext
5302 cnfile
5303 colder
5304 cprevious
5305 crewind
5306 lNext
5307 lNfile
5308 labove
5309 lbelow
5310 lfirst
5311 llast
5312 lnewer
5313 lnext
5314 lnfile
5315 lolder
5316 lprevious
5317 lrewind
5318 END
Bram Moolenaar25190db2019-05-04 15:05:28 +02005319 for cmd in commands
5320 call assert_fails('-1' .. cmd, 'E16:')
5321 call assert_fails('.' .. cmd, 'E16:')
5322 call assert_fails('%' .. cmd, 'E16:')
5323 call assert_fails('$' .. cmd, 'E16:')
5324 endfor
5325endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005326
5327" Test for aborting quickfix commands using QuickFixCmdPre
5328func Xtest_qfcmd_abort(cchar)
5329 call s:setup_commands(a:cchar)
5330
5331 call g:Xsetlist([], 'f')
5332
5333 " cexpr/lexpr
5334 let e = ''
5335 try
5336 Xexpr ["F1:10:Line10", "F2:20:Line20"]
5337 catch /.*/
5338 let e = v:exception
5339 endtry
5340 call assert_equal('AbortCmd', e)
5341 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5342
5343 " cfile/lfile
5344 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
5345 let e = ''
5346 try
5347 Xfile Xfile1
5348 catch /.*/
5349 let e = v:exception
5350 endtry
5351 call assert_equal('AbortCmd', e)
5352 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5353 call delete('Xfile1')
5354
5355 " cgetbuffer/lgetbuffer
5356 enew!
5357 call append(0, ["F1:10:Line10", "F2:20:Line20"])
5358 let e = ''
5359 try
5360 Xgetbuffer
5361 catch /.*/
5362 let e = v:exception
5363 endtry
5364 call assert_equal('AbortCmd', e)
5365 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5366 enew!
5367
5368 " vimgrep/lvimgrep
5369 let e = ''
5370 try
5371 Xvimgrep /func/ test_quickfix.vim
5372 catch /.*/
5373 let e = v:exception
5374 endtry
5375 call assert_equal('AbortCmd', e)
5376 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5377
5378 " helpgrep/lhelpgrep
5379 let e = ''
5380 try
5381 Xhelpgrep quickfix
5382 catch /.*/
5383 let e = v:exception
5384 endtry
5385 call assert_equal('AbortCmd', e)
5386 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5387
5388 " grep/lgrep
5389 if has('unix')
5390 let e = ''
5391 try
5392 silent Xgrep func test_quickfix.vim
5393 catch /.*/
5394 let e = v:exception
5395 endtry
5396 call assert_equal('AbortCmd', e)
5397 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5398 endif
5399endfunc
5400
5401func Test_qfcmd_abort()
5402 augroup QF_Test
5403 au!
5404 autocmd QuickFixCmdPre * throw "AbortCmd"
5405 augroup END
5406
5407 call Xtest_qfcmd_abort('c')
5408 call Xtest_qfcmd_abort('l')
5409
5410 augroup QF_Test
5411 au!
5412 augroup END
5413endfunc
5414
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005415" Test for using a file in one of the parent directories.
5416func Test_search_in_dirstack()
5417 call mkdir('Xtestdir/a/b/c', 'p')
5418 let save_cwd = getcwd()
5419 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
5420 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
5421 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
5422 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
5423
5424 let lines = "Entering dir Xtestdir\n" .
5425 \ "Entering dir a\n" .
5426 \ "Entering dir b\n" .
5427 \ "Xfile2:2:X2_L2\n" .
5428 \ "Leaving dir a\n" .
5429 \ "Xfile1:2:X1_L2\n" .
5430 \ "Xfile3:1:X3_L1\n" .
5431 \ "Entering dir c\n" .
5432 \ "Xfile4:2:X4_L2\n" .
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005433 \ "Leaving dir c\n"
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005434 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005435 cexpr lines .. "Leaving dir Xtestdir|\n" | let next = 1
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005436 call assert_equal(11, getqflist({'size' : 0}).size)
5437 call assert_equal(4, getqflist({'idx' : 0}).idx)
5438 call assert_equal('X2_L2', getline('.'))
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005439 call assert_equal(1, next)
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005440 cnext
5441 call assert_equal(6, getqflist({'idx' : 0}).idx)
5442 call assert_equal('X1_L2', getline('.'))
5443 cnext
5444 call assert_equal(7, getqflist({'idx' : 0}).idx)
5445 call assert_equal(1, line('$'))
5446 call assert_equal('', getline(1))
5447 cnext
5448 call assert_equal(9, getqflist({'idx' : 0}).idx)
5449 call assert_equal(1, line('$'))
5450 call assert_equal('', getline(1))
5451
5452 set efm&
5453 exe 'cd ' . save_cwd
5454 call delete('Xtestdir', 'rf')
5455endfunc
5456
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005457" Test for :cquit
5458func Test_cquit()
5459 " Exit Vim with a non-zero value
5460 if RunVim([], ["cquit 7"], '')
5461 call assert_equal(7, v:shell_error)
5462 endif
5463
5464 if RunVim([], ["50cquit"], '')
5465 call assert_equal(50, v:shell_error)
5466 endif
5467
5468 " Exit Vim with default value
5469 if RunVim([], ["cquit"], '')
5470 call assert_equal(1, v:shell_error)
5471 endif
5472
5473 " Exit Vim with zero value
5474 if RunVim([], ["cquit 0"], '')
5475 call assert_equal(0, v:shell_error)
5476 endif
5477
5478 " Exit Vim with negative value
5479 call assert_fails('-3cquit', 'E16:')
5480endfunc
5481
Bram Moolenaar858ba062020-05-31 23:11:59 +02005482" Test for getting a specific item from a quickfix list
5483func Xtest_getqflist_by_idx(cchar)
5484 call s:setup_commands(a:cchar)
5485 " Empty list
5486 call assert_equal([], g:Xgetlist({'idx' : 1, 'items' : 0}).items)
5487 Xexpr ['F1:10:L10', 'F1:20:L20']
5488 let l = g:Xgetlist({'idx' : 2, 'items' : 0}).items
5489 call assert_equal(bufnr('F1'), l[0].bufnr)
5490 call assert_equal(20, l[0].lnum)
5491 call assert_equal('L20', l[0].text)
5492 call assert_equal([], g:Xgetlist({'idx' : -1, 'items' : 0}).items)
5493 call assert_equal([], g:Xgetlist({'idx' : 3, 'items' : 0}).items)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005494 call assert_equal({}, g:Xgetlist(#{idx: "abc"}))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005495 %bwipe!
5496endfunc
5497
5498func Test_getqflist_by_idx()
5499 call Xtest_getqflist_by_idx('c')
5500 call Xtest_getqflist_by_idx('l')
5501endfunc
5502
5503" Test for the 'quickfixtextfunc' setting
5504func Tqfexpr(info)
5505 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005506 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005507 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005508 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005509 endif
5510
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005511 let l = []
5512 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5513 let e = qfl[idx]
5514 let s = ''
5515 if e.bufnr != 0
5516 let bname = bufname(e.bufnr)
5517 let s ..= fnamemodify(bname, ':.')
5518 endif
5519 let s ..= '-'
5520 let s ..= 'L' .. string(e.lnum) .. 'C' .. string(e.col) .. '-'
5521 let s ..= e.text
5522 call add(l, s)
5523 endfor
Bram Moolenaar858ba062020-05-31 23:11:59 +02005524
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005525 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005526endfunc
5527
5528func Xtest_qftextfunc(cchar)
5529 call s:setup_commands(a:cchar)
5530
5531 set efm=%f:%l:%c:%m
5532 set quickfixtextfunc=Tqfexpr
Bram Moolenaard43906d2020-07-20 21:31:32 +02005533 call assert_equal('Tqfexpr', &quickfixtextfunc)
5534 call assert_equal('',
5535 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
thinca6864efa2021-06-19 20:45:20 +02005536 call g:Xsetlist([
5537 \ { 'filename': 'F1', 'lnum': 10, 'col': 2,
5538 \ 'end_col': 7, 'text': 'green'},
5539 \ { 'filename': 'F1', 'lnum': 20, 'end_lnum': 25, 'col': 4,
5540 \ 'end_col': 8, 'text': 'blue'},
5541 \ ])
5542
Bram Moolenaar858ba062020-05-31 23:11:59 +02005543 Xwindow
5544 call assert_equal('F1-L10C2-green', getline(1))
5545 call assert_equal('F1-L20C4-blue', getline(2))
5546 Xclose
5547 set quickfixtextfunc&vim
5548 Xwindow
thinca6864efa2021-06-19 20:45:20 +02005549 call assert_equal('F1|10 col 2-7| green', getline(1))
5550 call assert_equal('F1|20-25 col 4-8| blue', getline(2))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005551 Xclose
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005552
5553 set efm=%f:%l:%c:%m
5554 set quickfixtextfunc=Tqfexpr
5555 " Update the list with only the cwindow
5556 Xwindow
5557 only
5558 call g:Xsetlist([
5559 \ { 'filename': 'F2', 'lnum': 20, 'col': 2,
5560 \ 'end_col': 7, 'text': 'red'}
5561 \ ])
5562 call assert_equal(['F2-L20C2-red'], getline(1, '$'))
5563 new
5564 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005565 set efm&
5566 set quickfixtextfunc&
5567
5568 " Test for per list 'quickfixtextfunc' setting
5569 func PerQfText(info)
5570 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005571 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005572 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005573 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005574 endif
5575 if empty(qfl)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005576 return []
Bram Moolenaar858ba062020-05-31 23:11:59 +02005577 endif
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005578 let l = []
5579 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5580 call add(l, 'Line ' .. qfl[idx].lnum .. ', Col ' .. qfl[idx].col)
5581 endfor
5582 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005583 endfunc
5584 set quickfixtextfunc=Tqfexpr
5585 call g:Xsetlist([], ' ', {'quickfixtextfunc' : "PerQfText"})
5586 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5587 Xwindow
5588 call assert_equal('Line 10, Col 2', getline(1))
5589 call assert_equal('Line 20, Col 4', getline(2))
5590 Xclose
Bram Moolenaard43906d2020-07-20 21:31:32 +02005591 call assert_equal(function('PerQfText'),
5592 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005593 " Add entries to the list when the quickfix buffer is hidden
5594 Xaddexpr ['F1:30:6:red']
5595 Xwindow
5596 call assert_equal('Line 30, Col 6', getline(3))
5597 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005598 call g:Xsetlist([], 'r', {'quickfixtextfunc' : ''})
Bram Moolenaard43906d2020-07-20 21:31:32 +02005599 call assert_equal('', g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar858ba062020-05-31 23:11:59 +02005600 set quickfixtextfunc&
5601 delfunc PerQfText
5602
5603 " Non-existing function
5604 set quickfixtextfunc=Tabc
5605 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5606 call assert_fails("Xwindow", 'E117:')
5607 Xclose
5608 set quickfixtextfunc&
5609
5610 " set option to a non-function
5611 set quickfixtextfunc=[10,\ 20]
5612 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5613 call assert_fails("Xwindow", 'E117:')
5614 Xclose
5615 set quickfixtextfunc&
5616
5617 " set option to a function with different set of arguments
5618 func Xqftext(a, b, c)
5619 return a:a .. a:b .. a:c
5620 endfunc
5621 set quickfixtextfunc=Xqftext
5622 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E119:')
5623 call assert_fails("Xwindow", 'E119:')
5624 Xclose
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005625
5626 " set option to a function that returns a list with non-strings
5627 func Xqftext2(d)
5628 return ['one', [], 'two']
5629 endfunc
5630 set quickfixtextfunc=Xqftext2
5631 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue', 'F1:30:6:red']",
5632 \ 'E730:')
5633 call assert_fails('Xwindow', 'E730:')
Bram Moolenaard43906d2020-07-20 21:31:32 +02005634 call assert_equal(['one', 'F1|20 col 4| blue', 'F1|30 col 6| red'],
5635 \ getline(1, '$'))
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005636 Xclose
5637
Bram Moolenaar858ba062020-05-31 23:11:59 +02005638 set quickfixtextfunc&
5639 delfunc Xqftext
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005640 delfunc Xqftext2
Bram Moolenaard43906d2020-07-20 21:31:32 +02005641
5642 " set the global option to a lambda function
5643 set quickfixtextfunc={d\ ->\ map(g:Xgetlist({'id'\ :\ d.id,\ 'items'\ :\ 1}).items[d.start_idx-1:d.end_idx-1],\ 'v:val.text')}
5644 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5645 Xwindow
5646 call assert_equal(['green', 'blue'], getline(1, '$'))
5647 Xclose
5648 call assert_equal("{d -> map(g:Xgetlist({'id' : d.id, 'items' : 1}).items[d.start_idx-1:d.end_idx-1], 'v:val.text')}", &quickfixtextfunc)
5649 set quickfixtextfunc&
5650
5651 " use a lambda function that returns an empty list
5652 set quickfixtextfunc={d\ ->\ []}
5653 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5654 Xwindow
5655 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5656 \ getline(1, '$'))
5657 Xclose
5658 set quickfixtextfunc&
5659
5660 " use a lambda function that returns a list with empty strings
5661 set quickfixtextfunc={d\ ->\ ['',\ '']}
5662 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5663 Xwindow
5664 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5665 \ getline(1, '$'))
5666 Xclose
5667 set quickfixtextfunc&
5668
5669 " set the per-quickfix list text function to a lambda function
5670 call g:Xsetlist([], ' ',
5671 \ {'quickfixtextfunc' :
5672 \ {d -> map(g:Xgetlist({'id' : d.id, 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5673 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5674 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5675 Xwindow
5676 call assert_equal('Line 10, Col 2', getline(1))
5677 call assert_equal('Line 20, Col 4', getline(2))
5678 Xclose
5679 call assert_match("function('<lambda>\\d\\+')", string(g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc))
5680 call g:Xsetlist([], 'f')
Bram Moolenaar858ba062020-05-31 23:11:59 +02005681endfunc
5682
5683func Test_qftextfunc()
5684 call Xtest_qftextfunc('c')
5685 call Xtest_qftextfunc('l')
5686endfunc
5687
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005688func Test_qftextfunc_callback()
5689 let lines =<< trim END
5690 set efm=%f:%l:%c:%m
5691
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00005692 #" Test for using a function name
5693 LET &qftf = 'g:Tqfexpr'
5694 cexpr "F0:0:0:L0"
5695 copen
5696 call assert_equal('F0-L0C0-L0', getline(1))
5697 cclose
5698
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005699 #" Test for using a function()
5700 set qftf=function('g:Tqfexpr')
5701 cexpr "F1:1:1:L1"
5702 copen
5703 call assert_equal('F1-L1C1-L1', getline(1))
5704 cclose
5705
5706 #" Using a funcref variable to set 'quickfixtextfunc'
5707 VAR Fn = function('g:Tqfexpr')
5708 LET &qftf = Fn
5709 cexpr "F2:2:2:L2"
5710 copen
5711 call assert_equal('F2-L2C2-L2', getline(1))
5712 cclose
5713
5714 #" Using string(funcref_variable) to set 'quickfixtextfunc'
5715 LET Fn = function('g:Tqfexpr')
5716 LET &qftf = string(Fn)
5717 cexpr "F3:3:3:L3"
5718 copen
5719 call assert_equal('F3-L3C3-L3', getline(1))
5720 cclose
5721
5722 #" Test for using a funcref()
5723 set qftf=funcref('g:Tqfexpr')
5724 cexpr "F4:4:4:L4"
5725 copen
5726 call assert_equal('F4-L4C4-L4', getline(1))
5727 cclose
5728
5729 #" Using a funcref variable to set 'quickfixtextfunc'
5730 LET Fn = funcref('g:Tqfexpr')
5731 LET &qftf = Fn
5732 cexpr "F5:5:5:L5"
5733 copen
5734 call assert_equal('F5-L5C5-L5', getline(1))
5735 cclose
5736
5737 #" Using a string(funcref_variable) to set 'quickfixtextfunc'
5738 LET Fn = funcref('g:Tqfexpr')
5739 LET &qftf = string(Fn)
5740 cexpr "F5:5:5:L5"
5741 copen
5742 call assert_equal('F5-L5C5-L5', getline(1))
5743 cclose
5744
5745 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00005746 VAR optval = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005747 LET optval = substitute(optval, ' ', '\\ ', 'g')
5748 exe "set qftf=" .. optval
5749 cexpr "F6:6:6:L6"
5750 copen
5751 call assert_equal('F6-L6C6-L6', getline(1))
5752 cclose
5753
5754 #" Set 'quickfixtextfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005755 LET &qftf = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005756 cexpr "F7:7:7:L7"
5757 copen
5758 call assert_equal('F7-L7C7-L7', getline(1))
5759 cclose
5760
5761 #" Set 'quickfixtextfunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005762 LET &qftf = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005763 cexpr "F8:8:8:L8"
5764 copen
5765 call assert_equal('F8-L8C8-L8', getline(1))
5766 cclose
5767
5768 #" Set 'quickfixtextfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005769 VAR Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005770 LET &qftf = Lambda
5771 cexpr "F9:9:9:L9"
5772 copen
5773 call assert_equal('F9-L9C9-L9', getline(1))
5774 cclose
5775
5776 #" Set 'quickfixtextfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005777 LET Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005778 LET &qftf = string(Lambda)
5779 cexpr "F9:9:9:L9"
5780 copen
5781 call assert_equal('F9-L9C9-L9', getline(1))
5782 cclose
5783 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00005784 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005785
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00005786 " Test for using a script-local function name
5787 func s:TqfFunc2(info)
5788 let g:TqfFunc2Args = [a:info.start_idx, a:info.end_idx]
5789 return ''
5790 endfunc
5791 let g:TqfFunc2Args = []
5792 set quickfixtextfunc=s:TqfFunc2
5793 cexpr "F10:10:10:L10"
5794 cclose
5795 call assert_equal([1, 1], g:TqfFunc2Args)
5796
5797 let &quickfixtextfunc = 's:TqfFunc2'
5798 cexpr "F11:11:11:L11"
5799 cclose
5800 call assert_equal([1, 1], g:TqfFunc2Args)
5801 delfunc s:TqfFunc2
5802
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005803 " set 'quickfixtextfunc' to a partial with dict. This used to cause a crash.
5804 func SetQftfFunc()
5805 let params = {'qftf': function('g:DictQftfFunc')}
5806 let &quickfixtextfunc = params.qftf
5807 endfunc
5808 func g:DictQftfFunc(_) dict
5809 endfunc
5810 call SetQftfFunc()
5811 new
5812 call SetQftfFunc()
5813 bw
5814 call test_garbagecollect_now()
5815 new
5816 set qftf=
5817 wincmd w
5818 set qftf=
5819 :%bw!
5820
5821 " set per-quickfix list 'quickfixtextfunc' to a partial with dict. This used
5822 " to cause a crash.
5823 let &qftf = ''
5824 func SetLocalQftfFunc()
5825 let params = {'qftf': function('g:DictQftfFunc')}
5826 call setqflist([], 'a', {'quickfixtextfunc' : params.qftf})
5827 endfunc
5828 call SetLocalQftfFunc()
5829 call test_garbagecollect_now()
5830 call setqflist([], 'a', {'quickfixtextfunc' : ''})
5831 delfunc g:DictQftfFunc
5832 delfunc SetQftfFunc
5833 delfunc SetLocalQftfFunc
5834 set efm&
5835endfunc
5836
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02005837" Test for updating a location list for some other window and check that
5838" 'qftextfunc' uses the correct location list.
5839func Test_qftextfunc_other_loclist()
5840 %bw!
5841 call setloclist(0, [], 'f')
5842
5843 " create a window and a location list for it and open the location list
5844 " window
5845 lexpr ['F1:10:12:one', 'F1:20:14:two']
5846 let w1_id = win_getid()
5847 call setloclist(0, [], ' ',
5848 \ {'lines': ['F1:10:12:one', 'F1:20:14:two'],
5849 \ 'quickfixtextfunc':
5850 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5851 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5852 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5853 lwindow
5854 let w2_id = win_getid()
5855
5856 " create another window and a location list for it and open the location
5857 " list window
5858 topleft new
5859 let w3_id = win_getid()
5860 call setloclist(0, [], ' ',
5861 \ {'lines': ['F2:30:32:eleven', 'F2:40:34:twelve'],
5862 \ 'quickfixtextfunc':
5863 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5864 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5865 \ "'Ligne ' .. v:val.lnum .. ', Colonne ' .. v:val.col")}})
5866 lwindow
5867 let w4_id = win_getid()
5868
5869 topleft new
5870 lexpr ['F3:50:52:green', 'F3:60:54:blue']
5871 let w5_id = win_getid()
5872
5873 " change the location list for some other window
5874 call setloclist(0, [], 'r', {'lines': ['F3:55:56:aaa', 'F3:57:58:bbb']})
5875 call setloclist(w1_id, [], 'r', {'lines': ['F1:62:63:bbb', 'F1:64:65:ccc']})
5876 call setloclist(w3_id, [], 'r', {'lines': ['F2:76:77:ddd', 'F2:78:79:eee']})
5877 call assert_equal(['Line 62, Col 63', 'Line 64, Col 65'],
5878 \ getbufline(winbufnr(w2_id), 1, '$'))
5879 call assert_equal(['Ligne 76, Colonne 77', 'Ligne 78, Colonne 79'],
5880 \ getbufline(winbufnr(w4_id), 1, '$'))
5881 call setloclist(w2_id, [], 'r', {'lines': ['F1:32:33:fff', 'F1:34:35:ggg']})
5882 call setloclist(w4_id, [], 'r', {'lines': ['F2:46:47:hhh', 'F2:48:49:jjj']})
5883 call assert_equal(['Line 32, Col 33', 'Line 34, Col 35'],
5884 \ getbufline(winbufnr(w2_id), 1, '$'))
5885 call assert_equal(['Ligne 46, Colonne 47', 'Ligne 48, Colonne 49'],
5886 \ getbufline(winbufnr(w4_id), 1, '$'))
5887
5888 call win_gotoid(w5_id)
5889 lwindow
5890 call assert_equal(['F3|55 col 56| aaa', 'F3|57 col 58| bbb'],
5891 \ getline(1, '$'))
5892 %bw!
5893endfunc
5894
Bram Moolenaarec98e932020-06-08 19:35:59 +02005895" Running :lhelpgrep command more than once in a help window, doesn't jump to
5896" the help topic
5897func Test_lhelpgrep_from_help_window()
5898 call mkdir('Xtestdir/doc', 'p')
5899 call writefile(['window'], 'Xtestdir/doc/a.txt')
5900 call writefile(['buffer'], 'Xtestdir/doc/b.txt')
5901 let save_rtp = &rtp
5902 let &rtp = 'Xtestdir'
5903 lhelpgrep window
5904 lhelpgrep buffer
5905 call assert_equal('b.txt', fnamemodify(@%, ":p:t"))
5906 lhelpgrep window
5907 call assert_equal('a.txt', fnamemodify(@%, ":p:t"))
5908 let &rtp = save_rtp
5909 call delete('Xtestdir', 'rf')
5910 new | only!
5911endfunc
5912
Bram Moolenaarf7c4d832020-08-11 20:42:19 +02005913" Test for the crash fixed by 7.3.715
5914func Test_setloclist_crash()
5915 %bw!
5916 let g:BufNum = bufnr()
5917 augroup QF_Test
5918 au!
5919 au BufUnload * call setloclist(0, [{'bufnr':g:BufNum, 'lnum':1, 'col':1, 'text': 'tango down'}])
5920 augroup END
5921
5922 try
5923 lvimgrep /.*/ *.mak
5924 catch /E926:/
5925 endtry
5926 call assert_equal('tango down', getloclist(0, {'items' : 0}).items[0].text)
5927 call assert_equal(1, getloclist(0, {'size' : 0}).size)
5928
5929 augroup QF_Test
5930 au!
5931 augroup END
5932 unlet g:BufNum
5933 %bw!
5934endfunc
5935
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005936" Test for adding an invalid entry with the quickfix window open and making
5937" sure that the window contents are not changed
5938func Test_add_invalid_entry_with_qf_window()
5939 call setqflist([], 'f')
5940 cexpr "Xfile1:10:aa"
5941 copen
5942 call setqflist(['bb'], 'a')
5943 call assert_equal(1, line('$'))
5944 call assert_equal(['Xfile1|10| aa'], getline(1, '$'))
shane.xb.qian0d5e1ec2021-06-20 16:31:00 +02005945 call assert_equal([{'lnum': 10 , 'end_lnum': 0 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'end_col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , getqflist())
5946
5947 call setqflist([{'lnum': 10 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5948 call assert_equal(1 , line('$'))
5949 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5950 call assert_equal([{'lnum': 10 , 'end_lnum': 0 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'end_col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , getqflist())
5951
5952 call setqflist([{'lnum': 10 , 'end_lnum': 0 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'end_col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5953 call assert_equal(1 , line('$'))
5954 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5955 call assert_equal([{'lnum': 10 , 'end_lnum': 0 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'end_col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , getqflist())
5956
5957 call setqflist([{'lnum': 10 , 'end_lnum': -123 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'end_col': -456 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5958 call assert_equal(1 , line('$'))
5959 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5960 call assert_equal([{'lnum': 10 , 'end_lnum': -123 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'end_col': -456 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , getqflist())
5961
5962 call setqflist([{'lnum': 10 , 'end_lnum': -123 , 'bufnr': bufnr('Xfile1') , 'col': 666 , 'end_col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5963 call assert_equal(1 , line('$'))
5964 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5965 call assert_equal([{'lnum': 10 , 'end_lnum': -123 , 'bufnr': bufnr('Xfile1') , 'col': 666 , 'end_col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , getqflist())
5966
5967 call setqflist([{'lnum': 10 , 'end_lnum': -123 , 'bufnr': bufnr('Xfile1') , 'col': 666 , 'end_col': -456 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5968 call assert_equal(1 , line('$'))
5969 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5970 call assert_equal([{'lnum': 10 , 'end_lnum': -123 , 'bufnr': bufnr('Xfile1') , 'col': 666 , 'end_col': -456 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , getqflist())
5971
5972 call setqflist([{'lnum': 10 , 'end_lnum': -123 , 'bufnr': bufnr('Xfile1') , 'col': 666 , 'end_col': 222 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5973 call assert_equal(1 , line('$'))
5974 call assert_equal(['Xfile1|10 col 666-222| aa'] , getline(1 , '$'))
5975 call assert_equal([{'lnum': 10 , 'end_lnum': -123 , 'bufnr': bufnr('Xfile1') , 'col': 666 , 'end_col': 222 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , getqflist())
5976
5977 call setqflist([{'lnum': 10 , 'end_lnum': 6 , 'bufnr': bufnr('Xfile1') , 'col': 666 , 'end_col': 222 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5978 call assert_equal(1 , line('$'))
5979 call assert_equal(['Xfile1|10-6 col 666-222| aa'] , getline(1 , '$'))
5980 call assert_equal([{'lnum': 10 , 'end_lnum': 6 , 'bufnr': bufnr('Xfile1') , 'col': 666 , 'end_col': 222 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , getqflist())
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005981 cclose
5982endfunc
5983
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01005984" Test for very weird problem: autocommand causes a failure, resulting opening
5985" the quickfix window to fail. This still splits the window, but otherwise
5986" should not mess up buffers.
5987func Test_quickfix_window_fails_to_open()
5988 CheckScreendump
5989
5990 let lines =<< trim END
5991 anything
5992 try
5993 anything
5994 endtry
5995 END
5996 call writefile(lines, 'XquickfixFails')
5997
5998 let lines =<< trim END
5999 split XquickfixFails
6000 silent vimgrep anything %
6001 normal o
6002 au BufLeave * ++once source XquickfixFails
6003 " This will trigger the autocommand, which causes an error, what follows
6004 " is aborted but the window was already split.
6005 silent! cwindow
6006 END
6007 call writefile(lines, 'XtestWinFails')
6008 let buf = RunVimInTerminal('-S XtestWinFails', #{rows: 13})
6009 call VerifyScreenDump(buf, 'Test_quickfix_window_fails', {})
6010
6011 " clean up
6012 call term_sendkeys(buf, ":bwipe!\<CR>")
6013 call term_wait(buf)
6014 call StopVimInTerminal(buf)
6015 call delete('XtestWinFails')
6016 call delete('XquickfixFails')
6017endfunc
6018
Bram Moolenaar2d870f82020-12-05 13:41:01 +01006019" Test for updating the quickfix buffer whenever the associated quickfix list
Bram Moolenaar287153c2020-11-29 14:20:27 +01006020" is changed.
6021func Xqfbuf_update(cchar)
6022 call s:setup_commands(a:cchar)
6023
6024 Xexpr "F1:1:line1"
6025 Xopen
6026 call assert_equal(['F1|1| line1'], getline(1, '$'))
6027 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6028
6029 " Test setqflist() using the 'lines' key in 'what'
6030 " add a new entry
6031 call g:Xsetlist([], 'a', {'lines' : ['F2:2: line2']})
6032 call assert_equal(['F1|1| line1', 'F2|2| line2'], getline(1, '$'))
6033 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6034 " replace all the entries with a single entry
6035 call g:Xsetlist([], 'r', {'lines' : ['F3:3: line3']})
6036 call assert_equal(['F3|3| line3'], getline(1, '$'))
6037 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6038 " remove all the entries
6039 call g:Xsetlist([], 'r', {'lines' : []})
6040 call assert_equal([''], getline(1, '$'))
6041 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6042 " add a new list
6043 call g:Xsetlist([], ' ', {'lines' : ['F4:4: line4']})
6044 call assert_equal(['F4|4| line4'], getline(1, '$'))
6045 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6046
6047 " Test setqflist() using the 'items' key in 'what'
6048 " add a new entry
6049 call g:Xsetlist([], 'a', {'items' : [{'filename' : 'F5', 'lnum' : 5, 'text' : 'line5'}]})
6050 call assert_equal(['F4|4| line4', 'F5|5| line5'], getline(1, '$'))
6051 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6052 " replace all the entries with a single entry
6053 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F6', 'lnum' : 6, 'text' : 'line6'}]})
6054 call assert_equal(['F6|6| line6'], getline(1, '$'))
6055 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6056 " remove all the entries
6057 call g:Xsetlist([], 'r', {'items' : []})
6058 call assert_equal([''], getline(1, '$'))
6059 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6060 " add a new list
6061 call g:Xsetlist([], ' ', {'items' : [{'filename' : 'F7', 'lnum' : 7, 'text' : 'line7'}]})
6062 call assert_equal(['F7|7| line7'], getline(1, '$'))
6063 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6064
6065 call g:Xsetlist([], ' ', {})
6066 call assert_equal([''], getline(1, '$'))
6067 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6068
6069 Xclose
6070endfunc
6071
6072func Test_qfbuf_update()
6073 call Xqfbuf_update('c')
6074 call Xqfbuf_update('l')
6075endfunc
6076
Bram Moolenaar8c801b32021-03-05 20:58:22 +01006077func Test_vimgrep_noswapfile()
6078 set noswapfile
6079 call writefile(['one', 'two', 'three'], 'Xgreppie')
6080 vimgrep two Xgreppie
6081 call assert_equal('two', getline('.'))
6082
6083 call delete('Xgreppie')
6084 set swapfile
6085endfunc
6086
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006087" Test for the :vimgrep 'f' flag (fuzzy match)
6088func Xvimgrep_fuzzy_match(cchar)
6089 call s:setup_commands(a:cchar)
6090
6091 Xvimgrep /three one/f Xfile*
6092 let l = g:Xgetlist()
6093 call assert_equal(2, len(l))
6094 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6095 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6096 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6097 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6098
6099 Xvimgrep /the/f Xfile*
6100 let l = g:Xgetlist()
6101 call assert_equal(3, len(l))
6102 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6103 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6104 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6105 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6106 call assert_equal(['Xfile2', 4, 4, 'aaathreeaaa'],
6107 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6108
6109 Xvimgrep /aaa/fg Xfile*
6110 let l = g:Xgetlist()
6111 call assert_equal(4, len(l))
6112 call assert_equal(['Xfile1', 2, 1, 'aaaaaa'],
6113 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6114 call assert_equal(['Xfile1', 2, 4, 'aaaaaa'],
6115 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6116 call assert_equal(['Xfile2', 4, 1, 'aaathreeaaa'],
6117 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6118 call assert_equal(['Xfile2', 4, 9, 'aaathreeaaa'],
6119 \ [bufname(l[3].bufnr), l[3].lnum, l[3].col, l[3].text])
6120
6121 call assert_fails('Xvimgrep /xyz/fg Xfile*', 'E480:')
6122endfunc
6123
6124func Test_vimgrep_fuzzy_match()
6125 call writefile(['one two three', 'aaaaaa'], 'Xfile1')
6126 call writefile(['one', 'three one two', 'two', 'aaathreeaaa'], 'Xfile2')
6127 call Xvimgrep_fuzzy_match('c')
6128 call Xvimgrep_fuzzy_match('l')
6129 call delete('Xfile1')
6130 call delete('Xfile2')
6131endfunc
6132
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006133func Test_locationlist_open_in_newtab()
6134 call s:create_test_file('Xqftestfile1')
6135 call s:create_test_file('Xqftestfile2')
6136 call s:create_test_file('Xqftestfile3')
6137
6138 %bwipe!
6139
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01006140 let lines =<< trim END
6141 Xqftestfile1:5:Line5
6142 Xqftestfile2:10:Line10
6143 Xqftestfile3:16:Line16
6144 END
6145 lgetexpr lines
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006146
6147 silent! llast
6148 call assert_equal(1, tabpagenr('$'))
6149 call assert_equal('Xqftestfile3', bufname())
6150
6151 set switchbuf=newtab
6152
6153 silent! lfirst
6154 call assert_equal(2, tabpagenr('$'))
6155 call assert_equal('Xqftestfile1', bufname())
6156
6157 silent! lnext
6158 call assert_equal(3, tabpagenr('$'))
6159 call assert_equal('Xqftestfile2', bufname())
6160
6161 call delete('Xqftestfile1')
6162 call delete('Xqftestfile2')
6163 call delete('Xqftestfile3')
6164 set switchbuf&vim
6165
6166 %bwipe!
6167endfunc
6168
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02006169" Test for win_gettype() in quickfix and location list windows
6170func Test_win_gettype()
6171 copen
6172 call assert_equal("quickfix", win_gettype())
6173 let wid = win_getid()
6174 wincmd p
6175 call assert_equal("quickfix", win_gettype(wid))
6176 cclose
6177 lexpr ''
6178 lopen
6179 call assert_equal("loclist", win_gettype())
6180 let wid = win_getid()
6181 wincmd p
6182 call assert_equal("loclist", win_gettype(wid))
6183 lclose
6184endfunc
6185
Christian Brabandt0b226f62021-12-01 10:54:24 +00006186fun Test_vimgrep_nomatch()
6187 call XexprTests('c')
6188 call g:Xsetlist([{'lnum':10,'text':'Line1'}])
6189 copen
6190 if has("win32")
6191 call assert_fails('vimgrep foo *.zzz', 'E479:')
6192 let expected = [{'lnum': 10, 'bufnr': 0, 'end_lnum': 0, 'pattern': '', 'valid': 0, 'vcol': 0, 'nr': 0, 'module': '', 'type': '', 'end_col': 0, 'col': 0, 'text': 'Line1'}]
6193 else
6194 call assert_fails('vimgrep foo *.zzz', 'E480:')
6195 let expected = []
6196 endif
6197 call assert_equal(expected, getqflist())
6198 cclose
6199endfunc
6200
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00006201" Test for opening the quickfix window in two tab pages and then closing one
6202" of the quickfix windows. This should not make the quickfix buffer unlisted.
6203" (github issue #9300).
6204func Test_two_qf_windows()
6205 cexpr "F1:1:line1"
6206 copen
6207 tabnew
6208 copen
6209 call assert_true(&buflisted)
6210 cclose
6211 tabfirst
6212 call assert_true(&buflisted)
6213 let bnum = bufnr()
6214 cclose
6215 " if all the quickfix windows are closed, then buffer should be unlisted.
6216 call assert_false(buflisted(bnum))
6217 %bw!
6218
6219 " Repeat the test for a location list
6220 lexpr "F2:2:line2"
6221 lopen
6222 let bnum = bufnr()
6223 tabnew
6224 exe "buffer" bnum
6225 tabfirst
6226 lclose
6227 tablast
6228 call assert_true(buflisted(bnum))
6229 tabclose
6230 lopen
6231 call assert_true(buflisted(bnum))
6232 lclose
6233 call assert_false(buflisted(bnum))
6234 %bw!
6235endfunc
6236
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006237" Weird sequence of commands that caused entering a wiped-out buffer
6238func Test_lopen_bwipe()
6239 func R()
6240 silent! tab lopen
6241 e x
6242 silent! lfile
6243 endfunc
6244
6245 cal R()
6246 cal R()
6247 cal R()
6248 bw!
6249 delfunc R
6250endfunc
6251
Bram Moolenaare3537ae2022-02-08 15:05:20 +00006252" Another sequence of commands that caused all buffers to be wiped out
6253func Test_lopen_bwipe_all()
6254 let lines =<< trim END
6255 func R()
6256 silent! tab lopen
6257 e foo
6258 silent! lfile
6259 endfunc
6260 cal R()
6261 exe "norm \<C-W>\<C-V>0"
6262 cal R()
6263 bwipe
6264
6265 call writefile(['done'], 'Xresult')
6266 qall!
6267 END
6268 call writefile(lines, 'Xscript')
6269 if RunVim([], [], '-u NONE -n -X -Z -e -m -s -S Xscript')
6270 call assert_equal(['done'], readfile('Xresult'))
6271 endif
6272
6273 call delete('Xscript')
6274 call delete('Xresult')
6275endfunc
6276
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00006277" Test for calling setqflist() function recursively
6278func Test_recursive_setqflist()
6279 augroup QF_Test
6280 au!
6281 autocmd BufWinEnter quickfix call setqflist([], 'r')
6282 augroup END
6283
6284 copen
6285 call assert_fails("call setqflist([], 'a')", 'E952:')
6286
6287 augroup QF_Test
6288 au!
6289 augroup END
6290 %bw!
6291endfunc
6292
6293" Test for failure to create a new window when selecting a file from the
6294" quickfix window
6295func Test_cwindow_newwin_fails()
6296 cgetexpr ["Xfile1:10:L10", "Xfile1:20:L20"]
6297 cwindow
6298 only
6299 let qf_wid = win_getid()
6300 " create the maximum number of scratch windows
6301 let hor_win_count = (&lines - 1)/2
6302 let hor_split_count = hor_win_count - 1
6303 for s in range(1, hor_split_count) | new | set buftype=nofile | endfor
6304 call win_gotoid(qf_wid)
6305 call assert_fails('exe "normal \<CR>"', 'E36:')
6306 %bw!
6307endfunc
6308
6309" Test for updating the location list when only the location list window is
6310" present and the corresponding file window is closed.
6311func Test_loclist_update_with_llwin_only()
6312 %bw!
6313 new
6314 wincmd w
6315 lexpr ["Xfile1:1:Line1"]
6316 lopen
6317 wincmd p
6318 close
6319 call setloclist(2, [], 'r', {'lines': ["Xtest2:2:Line2"]})
6320 call assert_equal(['Xtest2|2| Line2'], getbufline(winbufnr(2), 1, '$'))
6321 %bw!
6322endfunc
6323
6324" Test for getting the quickfix list after a buffer with an error is wiped out
6325func Test_getqflist_wiped_out_buffer()
6326 %bw!
6327 cexpr ["Xtest1:34:Wiped out"]
6328 let bnum = bufnr('Xtest1')
6329 call assert_equal(bnum, getqflist()[0].bufnr)
6330 bw Xtest1
6331 call assert_equal(0, getqflist()[0].bufnr)
6332 %bw!
6333endfunc
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006334
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00006335" Test for the status message that is displayed when opening a new quickfix
6336" list
6337func Test_qflist_statusmsg()
6338 cexpr "1\n2"
6339 cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"
6340 call assert_equal('(4 of 4): msg', v:statusmsg)
6341 call setqflist([], 'f')
6342 %bw!
6343
6344 " When creating a new quickfix list, if an autocmd changes the quickfix list
6345 " in the stack, then an error message should be displayed.
6346 augroup QF_Test
6347 au!
6348 au BufEnter test_quickfix.vim colder
6349 augroup END
6350 cexpr "1\n2"
6351 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6352 call setqflist([], 'f')
6353 augroup QF_Test
6354 au!
6355 augroup END
6356 %bw!
6357
6358 augroup QF_Test
6359 au!
6360 au BufEnter test_quickfix.vim caddexpr "4"
6361 augroup END
6362 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6363 call setqflist([], 'f')
6364 augroup QF_Test
6365 au!
6366 augroup END
6367 %bw!
6368endfunc
6369
Bram Moolenaard6c67622022-08-24 20:07:22 +01006370func Test_quickfixtextfunc_recursive()
6371 func s:QFTfunc(o)
6372 cgete '0'
6373 endfunc
6374 copen
6375 let &quickfixtextfunc = 's:QFTfunc'
6376 cex ""
6377
6378 let &quickfixtextfunc = ''
6379 cclose
6380endfunc
6381
Yegappan Lakshmanan6d24a512022-08-27 20:59:57 +01006382" Test for replacing the location list from an autocmd. This used to cause a
6383" read from freed memory.
6384func Test_loclist_replace_autocmd()
6385 %bw!
6386 call setloclist(0, [], 'f')
6387 let s:bufnr = bufnr()
6388 cal setloclist(0, [{'0': 0, '': ''}])
6389 au BufEnter * cal setloclist(1, [{'t': ''}, {'bufnr': s:bufnr}], 'r')
6390 lopen
6391 try
6392 exe "norm j\<CR>"
6393 catch
6394 endtry
6395 lnext
6396 %bw!
6397 call setloclist(0, [], 'f')
6398endfunc
Bram Moolenaard6c67622022-08-24 20:07:22 +01006399
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006400" vim: shiftwidth=2 sts=2 expandtab