blob: 762fa8d8d0e3c88051e638407494ef74f7b0f677 [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
4110func Test_getqflist()
4111 call Xgetlist_empty_tests('c')
4112 call Xgetlist_empty_tests('l')
4113endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01004114
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01004115func Test_getqflist_invalid_nr()
4116 " The following commands used to crash Vim
4117 cexpr ""
4118 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
4119
4120 " Cleanup
4121 call setqflist([], 'r')
4122endfunc
4123
Bram Moolenaarb254af32017-12-18 19:48:58 +01004124" Tests for the quickfix/location list changedtick
4125func Xqftick_tests(cchar)
4126 call s:setup_commands(a:cchar)
4127
4128 call g:Xsetlist([], 'f')
4129
4130 Xexpr "F1:10:Line10"
4131 let qfid = g:Xgetlist({'id' : 0}).id
4132 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4133 Xaddexpr "F2:20:Line20\nF2:21:Line21"
4134 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4135 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
4136 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
4137 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
4138 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
4139 call g:Xsetlist([], 'a', {'title' : 'New Title'})
4140 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
4141
4142 enew!
4143 call append(0, ["F5:50:L50", "F6:60:L60"])
4144 Xaddbuffer
4145 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
4146 enew!
4147
4148 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
4149 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
4150 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4151 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
4152 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
4153 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4154 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
4155 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4156 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4157 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
4158 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4159
4160 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
4161 Xfile Xone
4162 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4163 Xaddfile Xone
4164 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4165
4166 " Test case for updating a non-current quickfix list
4167 call g:Xsetlist([], 'f')
4168 Xexpr "F1:1:L1"
4169 Xexpr "F2:2:L2"
4170 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
4171 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4172 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
4173
4174 call delete("Xone")
4175endfunc
4176
4177func Test_qf_tick()
4178 call Xqftick_tests('c')
4179 call Xqftick_tests('l')
4180endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01004181
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02004182" Test helpgrep with lang specifier
4183func Xtest_helpgrep_with_lang_specifier(cchar)
4184 call s:setup_commands(a:cchar)
4185 Xhelpgrep Vim@en
4186 call assert_equal('help', &filetype)
4187 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
4188 new | only
4189endfunc
4190
4191func Test_helpgrep_with_lang_specifier()
4192 call Xtest_helpgrep_with_lang_specifier('c')
4193 call Xtest_helpgrep_with_lang_specifier('l')
4194endfunc
4195
Bram Moolenaar12237442017-12-19 12:38:52 +01004196" The following test used to crash Vim.
4197" Open the location list window and close the regular window associated with
4198" the location list. When the garbage collection runs now, it incorrectly
4199" marks the location list context as not in use and frees the context.
4200func Test_ll_window_ctx()
4201 call setloclist(0, [], 'f')
4202 call setloclist(0, [], 'a', {'context' : []})
4203 lopen | only
4204 call test_garbagecollect_now()
4205 echo getloclist(0, {'context' : 1}).context
4206 enew | only
4207endfunc
4208
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004209" The following test used to crash vim
4210func Test_lfile_crash()
4211 sp Xtest
4212 au QuickFixCmdPre * bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02004213 call assert_fails('lfile', 'E40:')
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004214 au! QuickFixCmdPre
4215endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01004216
4217" The following test used to crash vim
4218func Test_lbuffer_crash()
4219 sv Xtest
4220 augroup QF_Test
4221 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004222 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bw
Bram Moolenaar3c097222017-12-21 20:54:49 +01004223 augroup END
4224 lbuffer
4225 augroup QF_Test
4226 au!
4227 augroup END
4228endfunc
4229
4230" The following test used to crash vim
4231func Test_lexpr_crash()
4232 augroup QF_Test
4233 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004234 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004235 augroup END
4236 lexpr ""
4237 augroup QF_Test
4238 au!
4239 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004240
Bram Moolenaar3c097222017-12-21 20:54:49 +01004241 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004242 augroup QF_Test
4243 au!
4244 au BufNew * call setloclist(0, [], 'f')
4245 augroup END
4246 lexpr 'x:1:x'
4247 augroup QF_Test
4248 au!
4249 augroup END
4250
4251 enew | only
4252 lexpr ''
4253 lopen
4254 augroup QF_Test
4255 au!
4256 au FileType * call setloclist(0, [], 'f')
4257 augroup END
4258 lexpr ''
4259 augroup QF_Test
4260 au!
4261 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01004262endfunc
4263
4264" The following test used to crash Vim
4265func Test_lvimgrep_crash()
4266 sv Xtest
4267 augroup QF_Test
4268 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004269 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004270 augroup END
4271 lvimgrep quickfix test_quickfix.vim
4272 augroup QF_Test
4273 au!
4274 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004275
4276 new | only
4277 augroup QF_Test
4278 au!
4279 au BufEnter * call setloclist(0, [], 'r')
4280 augroup END
4281 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
4282 augroup QF_Test
4283 au!
4284 augroup END
4285
Bram Moolenaar3c097222017-12-21 20:54:49 +01004286 enew | only
4287endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01004288
Bram Moolenaar2573af32020-03-14 17:21:34 +01004289func Test_lvimgrep_crash2()
4290 au BufNewFile x sfind
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004291 call assert_fails('lvimgrep x x', 'E471:')
4292 call assert_fails('lvimgrep x x x', 'E471:')
Bram Moolenaar2573af32020-03-14 17:21:34 +01004293
4294 au! BufNewFile
4295endfunc
4296
Bram Moolenaarde046542017-12-26 13:53:11 +01004297" Test for the position of the quickfix and location list window
4298func Test_qfwin_pos()
4299 " Open two windows
4300 new | only
4301 new
4302 cexpr ['F1:10:L10']
4303 copen
4304 " Quickfix window should be the bottom most window
4305 call assert_equal(3, winnr())
4306 close
4307 " Open at the very top
4308 wincmd t
4309 topleft copen
4310 call assert_equal(1, winnr())
4311 close
4312 " open left of the current window
4313 wincmd t
4314 below new
4315 leftabove copen
4316 call assert_equal(2, winnr())
4317 close
4318 " open right of the current window
4319 rightbelow copen
4320 call assert_equal(3, winnr())
4321 close
4322endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004323
4324" Tests for quickfix/location lists changed by autocommands when
4325" :vimgrep/:lvimgrep commands are running.
4326func Test_vimgrep_autocmd()
4327 call setqflist([], 'f')
4328 call writefile(['stars'], 'Xtest1.txt')
4329 call writefile(['stars'], 'Xtest2.txt')
4330
4331 " Test 1:
4332 " When searching for a pattern using :vimgrep, if the quickfix list is
4333 " changed by an autocmd, the results should be added to the correct quickfix
4334 " list.
4335 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
4336 silent vimgrep stars Xtest*.txt
4337 call assert_equal(1, getqflist({'nr' : 0}).nr)
4338 call assert_equal(3, getqflist({'nr' : '$'}).nr)
4339 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
4340 au! BufRead Xtest2.txt
4341
4342 " Test 2:
4343 " When searching for a pattern using :vimgrep, if the quickfix list is
4344 " freed, then a error should be given.
4345 silent! %bwipe!
4346 call setqflist([], 'f')
4347 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
4348 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
4349 au! BufRead Xtest2.txt
4350
4351 " Test 3:
4352 " When searching for a pattern using :lvimgrep, if the location list is
4353 " freed, then the command should error out.
4354 silent! %bwipe!
4355 let g:save_winid = win_getid()
4356 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
4357 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
4358 au! BufRead Xtest2.txt
4359
4360 call delete('Xtest1.txt')
4361 call delete('Xtest2.txt')
4362 call setqflist([], 'f')
4363endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004364
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004365" Test for an autocmd changing the current directory when running vimgrep
4366func Xvimgrep_autocmd_cd(cchar)
4367 call s:setup_commands(a:cchar)
4368
4369 %bwipe
4370 let save_cwd = getcwd()
4371
4372 augroup QF_Test
4373 au!
4374 autocmd BufRead * silent cd %:p:h
4375 augroup END
4376
4377 10Xvimgrep /vim/ Xdir/**
4378 let l = g:Xgetlist()
4379 call assert_equal('f1.txt', bufname(l[0].bufnr))
4380 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
4381
4382 augroup QF_Test
4383 au!
4384 augroup END
4385
4386 exe 'cd ' . save_cwd
4387endfunc
4388
4389func Test_vimgrep_autocmd_cd()
4390 call mkdir('Xdir/a', 'p')
4391 call mkdir('Xdir/b', 'p')
4392 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xdir/a/f1.txt')
4393 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xdir/b/f2.txt')
4394 call Xvimgrep_autocmd_cd('c')
4395 call Xvimgrep_autocmd_cd('l')
4396 %bwipe
4397 call delete('Xdir', 'rf')
4398endfunc
4399
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004400" The following test used to crash Vim
4401func Test_lhelpgrep_autocmd()
4402 lhelpgrep quickfix
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004403 augroup QF_Test
4404 au!
4405 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
4406 augroup END
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004407 lhelpgrep buffer
4408 call assert_equal('help', &filetype)
4409 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
4410 lhelpgrep tabpage
4411 call assert_equal('help', &filetype)
4412 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004413 augroup QF_Test
4414 au!
4415 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004416
4417 new | only
4418 augroup QF_Test
4419 au!
4420 au BufEnter * call setqflist([], 'f')
4421 augroup END
4422 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaardbfa7952020-11-02 20:04:22 +01004423 " run the test with a help window already open
4424 help
4425 wincmd w
4426 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004427 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004428 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004429 augroup END
4430
4431 new | only
4432 augroup QF_Test
4433 au!
4434 au BufEnter * call setqflist([], 'r')
4435 augroup END
4436 call assert_fails('helpgrep quickfix', 'E925:')
4437 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004438 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004439 augroup END
4440
4441 new | only
4442 augroup QF_Test
4443 au!
4444 au BufEnter * call setloclist(0, [], 'r')
4445 augroup END
4446 call assert_fails('lhelpgrep quickfix', 'E926:')
4447 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004448 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004449 augroup END
4450
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004451 " Replace the contents of a help window location list when it is still in
4452 " use.
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004453 new | only
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004454 lhelpgrep quickfix
4455 wincmd w
4456 augroup QF_Test
4457 au!
4458 autocmd WinEnter * call setloclist(0, [], 'r')
4459 augroup END
4460 call assert_fails('lhelpgrep win_getid', 'E926:')
4461 augroup QF_Test
4462 au!
4463 augroup END
4464
4465 %bw!
4466endfunc
4467
4468" The following test used to crash Vim
4469func Test_lhelpgrep_autocmd_free_loclist()
4470 %bw!
4471 lhelpgrep quickfix
4472 wincmd w
4473 augroup QF_Test
4474 au!
4475 autocmd WinEnter * call setloclist(0, [], 'f')
4476 augroup END
4477 lhelpgrep win_getid
4478 wincmd w
4479 wincmd w
4480 wincmd w
4481 augroup QF_Test
4482 au!
4483 augroup END
4484 %bw!
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004485endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02004486
4487" Test for shortening/simplifying the file name when opening the
4488" quickfix window or when displaying the quickfix list
4489func Test_shorten_fname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02004490 CheckUnix
Bram Moolenaara796d462018-05-01 14:30:36 +02004491 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004492 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004493 let fname = getcwd() . '/test_quickfix.vim'
4494 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4495 call assert_equal(fname, bufname('test_quickfix.vim'))
4496 " Opening the quickfix window should simplify the file path
4497 cwindow
4498 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
4499 cclose
4500 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004501 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004502 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4503 call assert_equal(fname, bufname('test_quickfix.vim'))
4504 " Displaying the quickfix list should simplify the file path
4505 silent! clist
4506 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004507 " Add a few entries for the same file with different paths and check whether
4508 " the buffer name is shortened
4509 %bwipe
4510 call setqflist([], 'f')
4511 call setqflist([{'filename' : 'test_quickfix.vim', 'lnum' : 10},
4512 \ {'filename' : '../testdir/test_quickfix.vim', 'lnum' : 20},
4513 \ {'filename' : fname, 'lnum' : 30}], ' ')
4514 copen
4515 call assert_equal(['test_quickfix.vim|10| ',
4516 \ 'test_quickfix.vim|20| ',
4517 \ 'test_quickfix.vim|30| '], getline(1, '$'))
4518 cclose
Bram Moolenaara796d462018-05-01 14:30:36 +02004519endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004520
4521" Quickfix title tests
4522" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
4523" Otherwise due to indentation, the title is set with spaces at the beginning
4524" of the command.
4525func Test_qftitle()
4526 call writefile(["F1:1:Line1"], 'Xerr')
4527
4528 " :cexpr
4529 exe "cexpr readfile('Xerr')"
4530 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
4531
4532 " :cgetexpr
4533 exe "cgetexpr readfile('Xerr')"
4534 call assert_equal(":cgetexpr readfile('Xerr')",
4535 \ getqflist({'title' : 1}).title)
4536
4537 " :caddexpr
4538 call setqflist([], 'f')
4539 exe "caddexpr readfile('Xerr')"
4540 call assert_equal(":caddexpr readfile('Xerr')",
4541 \ getqflist({'title' : 1}).title)
4542
4543 " :cbuffer
4544 new Xerr
4545 exe "cbuffer"
4546 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
4547
4548 " :cgetbuffer
4549 edit Xerr
4550 exe "cgetbuffer"
4551 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
4552
4553 " :caddbuffer
4554 call setqflist([], 'f')
4555 edit Xerr
4556 exe "caddbuffer"
4557 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
4558
4559 " :cfile
4560 exe "cfile Xerr"
4561 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
4562
4563 " :cgetfile
4564 exe "cgetfile Xerr"
4565 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
4566
4567 " :caddfile
4568 call setqflist([], 'f')
4569 exe "caddfile Xerr"
4570 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
4571
4572 " :grep
4573 set grepprg=internal
4574 exe "grep F1 Xerr"
4575 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
4576
4577 " :grepadd
4578 call setqflist([], 'f')
4579 exe "grepadd F1 Xerr"
4580 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
4581 set grepprg&vim
4582
4583 " :vimgrep
4584 exe "vimgrep F1 Xerr"
4585 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
4586
4587 " :vimgrepadd
4588 call setqflist([], 'f')
4589 exe "vimgrepadd F1 Xerr"
4590 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
4591
4592 call setqflist(['F1:10:L10'], ' ')
4593 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4594
4595 call setqflist([], 'f')
4596 call setqflist(['F1:10:L10'], 'a')
4597 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4598
4599 call setqflist([], 'f')
4600 call setqflist(['F1:10:L10'], 'r')
4601 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4602
4603 close
4604 call delete('Xerr')
4605
4606 call setqflist([], ' ', {'title' : 'Errors'})
4607 copen
4608 call assert_equal('Errors', w:quickfix_title)
4609 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
4610 call assert_equal('Errors', w:quickfix_title)
4611 cclose
Bram Moolenaar530bed92020-12-16 21:02:56 +01004612
4613 " Switching to another quickfix list in one tab page should update the
4614 " quickfix window title and statusline in all the other tab pages also
4615 call setqflist([], 'f')
4616 %bw!
4617 cgetexpr ['file_one:1:1: error in the first quickfix list']
4618 call setqflist([], 'a', {'title': 'first quickfix list'})
4619 cgetexpr ['file_two:2:1: error in the second quickfix list']
4620 call setqflist([], 'a', {'title': 'second quickfix list'})
4621 copen
4622 wincmd t
4623 tabnew two
4624 copen
4625 wincmd t
4626 colder
4627 call assert_equal('first quickfix list', gettabwinvar(1, 2, 'quickfix_title'))
4628 call assert_equal('first quickfix list', gettabwinvar(2, 2, 'quickfix_title'))
4629 call assert_equal(1, tabpagewinnr(1))
4630 call assert_equal(1, tabpagewinnr(2))
4631 tabnew
4632 call setqflist([], 'a', {'title': 'new quickfix title'})
4633 call assert_equal('new quickfix title', gettabwinvar(1, 2, 'quickfix_title'))
4634 call assert_equal('new quickfix title', gettabwinvar(2, 2, 'quickfix_title'))
4635 %bw!
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004636endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02004637
4638func Test_lbuffer_with_bwipe()
4639 new
4640 new
4641 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004642 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bwipe
Bram Moolenaar600323b2018-06-16 22:16:47 +02004643 augroup END
4644 lbuffer
4645 augroup nasty
4646 au!
4647 augroup END
4648endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02004649
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004650" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
4651" running
4652func Xexpr_acmd_freelist(cchar)
4653 call s:setup_commands(a:cchar)
4654
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004655 " This was using freed memory (but with what events?)
Bram Moolenaar0366c012018-06-18 20:52:13 +02004656 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004657 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02004658 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004659 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02004660 augroup nasty
4661 au!
4662 augroup END
4663endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004664
4665func Test_cexpr_acmd_freelist()
4666 call Xexpr_acmd_freelist('c')
4667 call Xexpr_acmd_freelist('l')
4668endfunc
4669
4670" Test for commands that create a new quickfix/location list and jump to the
4671" first error automatically.
4672func Xjumpto_first_error_test(cchar)
4673 call s:setup_commands(a:cchar)
4674
4675 call s:create_test_file('Xtestfile1')
4676 call s:create_test_file('Xtestfile2')
4677 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4678
4679 " Test for cexpr/lexpr
4680 enew
4681 Xexpr l
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004682 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004683 call assert_equal(2, line('.'))
4684
4685 " Test for cfile/lfile
4686 enew
4687 call writefile(l, 'Xerr')
4688 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004689 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004690 call assert_equal(2, line('.'))
4691
4692 " Test for cbuffer/lbuffer
4693 edit Xerr
4694 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004695 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004696 call assert_equal(2, line('.'))
4697
4698 call delete('Xerr')
4699 call delete('Xtestfile1')
4700 call delete('Xtestfile2')
4701endfunc
4702
4703func Test_jumpto_first_error()
4704 call Xjumpto_first_error_test('c')
4705 call Xjumpto_first_error_test('l')
4706endfunc
4707
4708" Test for a quickfix autocmd changing the quickfix/location list before
4709" jumping to the first error in the new list.
4710func Xautocmd_changelist(cchar)
4711 call s:setup_commands(a:cchar)
4712
4713 " Test for cfile/lfile
4714 call s:create_test_file('Xtestfile1')
4715 call s:create_test_file('Xtestfile2')
4716 Xexpr 'Xtestfile1:2:Line2'
4717 autocmd QuickFixCmdPost * Xolder
4718 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4719 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004720 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004721 call assert_equal(4, line('.'))
4722 autocmd! QuickFixCmdPost
4723
4724 " Test for cbuffer/lbuffer
4725 call g:Xsetlist([], 'f')
4726 Xexpr 'Xtestfile1:2:Line2'
4727 autocmd QuickFixCmdPost * Xolder
4728 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4729 edit Xerr
4730 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004731 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004732 call assert_equal(4, line('.'))
4733 autocmd! QuickFixCmdPost
4734
4735 " Test for cexpr/lexpr
4736 call g:Xsetlist([], 'f')
4737 Xexpr 'Xtestfile1:2:Line2'
4738 autocmd QuickFixCmdPost * Xolder
4739 Xexpr 'Xtestfile2:4:Line4'
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004740 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004741 call assert_equal(4, line('.'))
4742 autocmd! QuickFixCmdPost
4743
Bram Moolenaar851332e2018-07-03 19:16:00 +02004744 " The grepprg may not be set on non-Unix systems
4745 if has('unix')
4746 " Test for grep/lgrep
4747 call g:Xsetlist([], 'f')
4748 Xexpr 'Xtestfile1:2:Line2'
4749 autocmd QuickFixCmdPost * Xolder
4750 silent Xgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004751 call assert_equal('Xtestfile2', @%)
Bram Moolenaar851332e2018-07-03 19:16:00 +02004752 call assert_equal(5, line('.'))
4753 autocmd! QuickFixCmdPost
4754 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004755
4756 " Test for vimgrep/lvimgrep
4757 call g:Xsetlist([], 'f')
4758 Xexpr 'Xtestfile1:2:Line2'
4759 autocmd QuickFixCmdPost * Xolder
4760 silent Xvimgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004761 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004762 call assert_equal(5, line('.'))
4763 autocmd! QuickFixCmdPost
4764
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004765 " Test for autocommands clearing the quickfix list before jumping to the
4766 " first error. This should not result in an error
4767 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
4768 let v:errmsg = ''
4769 " Test for cfile/lfile
4770 Xfile Xerr
4771 call assert_true(v:errmsg !~# 'E42:')
4772 " Test for cbuffer/lbuffer
4773 edit Xerr
4774 Xbuffer
4775 call assert_true(v:errmsg !~# 'E42:')
4776 " Test for cexpr/lexpr
4777 Xexpr 'Xtestfile2:4:Line4'
4778 call assert_true(v:errmsg !~# 'E42:')
4779 " Test for grep/lgrep
4780 " The grepprg may not be set on non-Unix systems
4781 if has('unix')
4782 silent Xgrep Line5 Xtestfile2
4783 call assert_true(v:errmsg !~# 'E42:')
4784 endif
4785 " Test for vimgrep/lvimgrep
4786 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
4787 autocmd! QuickFixCmdPost
4788
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004789 call delete('Xerr')
4790 call delete('Xtestfile1')
4791 call delete('Xtestfile2')
4792endfunc
4793
4794func Test_autocmd_changelist()
4795 call Xautocmd_changelist('c')
4796 call Xautocmd_changelist('l')
4797endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004798
4799" Tests for the ':filter /pat/ clist' command
4800func Test_filter_clist()
4801 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4802 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4803 \ split(execute('filter /Line 15/ clist'), "\n"))
4804 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4805 \ split(execute('filter /Xfile1/ clist'), "\n"))
4806 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4807
4808 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4809 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4810 call assert_equal([' 2 pqr:pat2: '],
4811 \ split(execute('filter /pqr/ clist'), "\n"))
4812 call assert_equal([' 1 abc:pat1: '],
4813 \ split(execute('filter /pat1/ clist'), "\n"))
4814endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004815
4816" Tests for the "CTRL-W <CR>" command.
4817func Xview_result_split_tests(cchar)
4818 call s:setup_commands(a:cchar)
4819
4820 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4821 call g:Xsetlist([])
4822 Xopen
4823 let l:win_count = winnr('$')
Bram Moolenaare2e40752020-09-04 21:18:46 +02004824 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42:')
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004825 call assert_equal(l:win_count, winnr('$'))
4826 Xclose
4827endfunc
4828
4829func Test_view_result_split()
4830 call Xview_result_split_tests('c')
4831 call Xview_result_split_tests('l')
4832endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004833
4834" Test that :cc sets curswant
4835func Test_curswant()
4836 helpgrep quickfix
4837 normal! llll
4838 1cc
4839 call assert_equal(getcurpos()[4], virtcol('.'))
4840 cclose | helpclose
4841endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004842
4843" Test for opening a file from the quickfix window using CTRL-W <Enter>
4844" doesn't leave an empty buffer around.
4845func Test_splitview()
4846 call s:create_test_file('Xtestfile1')
4847 call s:create_test_file('Xtestfile2')
4848 new | only
4849 let last_bufnr = bufnr('Test_sv_1', 1)
4850 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4851 cgetexpr l
4852 copen
4853 let numbufs = len(getbufinfo())
4854 exe "normal \<C-W>\<CR>"
4855 copen
4856 exe "normal j\<C-W>\<CR>"
4857 " Make sure new empty buffers are not created
4858 call assert_equal(numbufs, len(getbufinfo()))
4859 " Creating a new buffer should use the next available buffer number
4860 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4861 bwipe Test_sv_1
4862 bwipe Test_sv_2
4863 new | only
4864
4865 " When split opening files from location list window, make sure that two
4866 " windows doesn't refer to the same location list
4867 lgetexpr l
4868 let locid = getloclist(0, {'id' : 0}).id
4869 lopen
4870 exe "normal \<C-W>\<CR>"
4871 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4872 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4873 new | only
4874
4875 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004876 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004877 lhelpgrep window
4878 let locid = getloclist(0, {'id' : 0}).id
4879 lwindow
4880 exe "normal j\<C-W>\<CR>"
4881 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4882 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4883 new | only
4884
Bram Moolenaar406cd902020-02-18 21:54:41 +01004885 " Using :split or :vsplit from a quickfix window should behave like a :new
4886 " or a :vnew command
4887 copen
4888 split
4889 call assert_equal(3, winnr('$'))
4890 let l = getwininfo()
4891 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4892 close
4893 copen
4894 vsplit
4895 let l = getwininfo()
4896 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4897 new | only
4898
Bram Moolenaarb2443732018-11-11 22:50:27 +01004899 call delete('Xtestfile1')
4900 call delete('Xtestfile2')
4901endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004902
4903" Test for parsing entries using visual screen column
4904func Test_viscol()
4905 enew
4906 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4907 edit Xfile1
4908
4909 " Use byte offset for column number
4910 set efm&
4911 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
4912 call assert_equal([5, 8], [col('.'), virtcol('.')])
4913 cnext
4914 call assert_equal([9, 12], [col('.'), virtcol('.')])
4915 cnext
4916 call assert_equal([14, 20], [col('.'), virtcol('.')])
4917
4918 " Use screen column offset for column number
4919 set efm=%f:%l:%v:%m
4920 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4921 call assert_equal([5, 8], [col('.'), virtcol('.')])
4922 cnext
4923 call assert_equal([9, 12], [col('.'), virtcol('.')])
4924 cnext
4925 call assert_equal([14, 20], [col('.'), virtcol('.')])
4926 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4927 call assert_equal([5, 8], [col('.'), virtcol('.')])
4928 cnext
4929 call assert_equal([10, 16], [col('.'), virtcol('.')])
4930 cnext
4931 call assert_equal([14, 20], [col('.'), virtcol('.')])
4932
4933 enew
4934 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4935
4936 " Use byte offset for column number
4937 set efm&
4938 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
4939 call assert_equal([8, 10], [col('.'), virtcol('.')])
4940 cnext
4941 call assert_equal([11, 17], [col('.'), virtcol('.')])
4942 cnext
4943 call assert_equal([16, 25], [col('.'), virtcol('.')])
4944
4945 " Use screen column offset for column number
4946 set efm=%f:%l:%v:%m
4947 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4948 call assert_equal([8, 10], [col('.'), virtcol('.')])
4949 cnext
4950 call assert_equal([11, 17], [col('.'), virtcol('.')])
4951 cnext
4952 call assert_equal([16, 25], [col('.'), virtcol('.')])
4953
Bram Moolenaarc95940c2020-10-20 14:59:12 +02004954 " Use screen column number with a multi-line error message
4955 enew
4956 call writefile(["à test"], 'Xfile1')
4957 set efm=%E===\ %f\ ===,%C%l:%v,%Z%m
4958 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4959 call assert_equal('Xfile1', @%)
4960 call assert_equal([0, 1, 4, 0], getpos('.'))
4961
4962 " Repeat previous test with byte offset %c: ensure that fix to issue #7145
4963 " does not break this
4964 set efm=%E===\ %f\ ===,%C%l:%c,%Z%m
4965 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4966 call assert_equal('Xfile1', @%)
4967 call assert_equal([0, 1, 3, 0], getpos('.'))
4968
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004969 enew | only
4970 set efm&
4971 call delete('Xfile1')
4972endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004973
4974" Test for the quickfix window buffer
4975func Xqfbuf_test(cchar)
4976 call s:setup_commands(a:cchar)
4977
4978 " Quickfix buffer should be reused across closing and opening a quickfix
4979 " window
4980 Xexpr "F1:10:Line10"
4981 Xopen
4982 let qfbnum = bufnr('')
4983 Xclose
4984 " Even after the quickfix window is closed, the buffer should be loaded
4985 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004986 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004987 Xopen
4988 " Buffer should be reused when opening the window again
4989 call assert_equal(qfbnum, bufnr(''))
4990 Xclose
4991
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00004992 " When quickfix buffer is wiped out, getqflist() should return 0
4993 %bw!
4994 Xexpr ""
4995 Xopen
4996 bw!
4997 call assert_equal(0, g:Xgetlist({'qfbufnr': 0}).qfbufnr)
4998
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004999 if a:cchar == 'l'
5000 %bwipe
5001 " For a location list, when both the file window and the location list
5002 " window for the list are closed, then the buffer should be freed.
5003 new | only
5004 lexpr "F1:10:Line10"
5005 let wid = win_getid()
5006 lopen
5007 let qfbnum = bufnr('')
5008 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
5009 close
5010 " When the location list window is closed, the buffer name should not
5011 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005012 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005013 call assert_true(bufloaded(qfbnum))
5014
Bram Moolenaard82a81c2019-03-02 07:57:18 +01005015 " After deleting a location list buffer using ":bdelete", opening the
5016 " location list window should mark the buffer as a location list buffer.
5017 exe "bdelete " . qfbnum
5018 lopen
5019 call assert_equal("quickfix", &buftype)
5020 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
5021 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
5022 call assert_false(&swapfile)
5023 lclose
5024
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005025 " When the location list is cleared for the window, the buffer should be
5026 " removed
5027 call setloclist(0, [], 'f')
5028 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005029 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005030
5031 " When the location list is freed with the location list window open, the
5032 " location list buffer should not be lost. It should be reused when the
5033 " location list is again populated.
5034 lexpr "F1:10:Line10"
5035 lopen
5036 let wid = win_getid()
5037 let qfbnum = bufnr('')
5038 wincmd p
5039 call setloclist(0, [], 'f')
5040 lexpr "F1:10:Line10"
5041 lopen
5042 call assert_equal(wid, win_getid())
5043 call assert_equal(qfbnum, bufnr(''))
5044 lclose
5045
5046 " When the window with the location list is closed, the buffer should be
5047 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005048 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005049 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005050 endif
5051endfunc
5052
5053func Test_qfbuf()
5054 call Xqfbuf_test('c')
5055 call Xqfbuf_test('l')
5056endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005057
5058" If there is an autocmd to use only one window, then opening the location
5059" list window used to crash Vim.
5060func Test_winonly_autocmd()
5061 call s:create_test_file('Xtest1')
5062 " Autocmd to show only one Vim window at a time
5063 autocmd WinEnter * only
5064 new
5065 " Load the location list
5066 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
5067 let loclistid = getloclist(0, {'id' : 0}).id
5068 " Open the location list window. Only this window will be shown and the file
5069 " window is closed.
5070 lopen
5071 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
5072 " Jump to an entry in the location list and make sure that the cursor is
5073 " positioned correctly.
5074 ll 3
5075 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005076 call assert_equal('Xtest1', @%)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005077 call assert_equal(15, line('.'))
5078 " Cleanup
5079 autocmd! WinEnter
5080 new | only
5081 call delete('Xtest1')
5082endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02005083
5084" Test to make sure that an empty quickfix buffer is not reused for loading
5085" a normal buffer.
5086func Test_empty_qfbuf()
5087 enew | only
5088 call writefile(["Test"], 'Xfile1')
5089 call setqflist([], 'f')
5090 copen | only
5091 let qfbuf = bufnr('')
5092 edit Xfile1
5093 call assert_notequal(qfbuf, bufnr(''))
5094 enew
5095 call delete('Xfile1')
5096endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005097
5098" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005099" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005100func Xtest_below(cchar)
5101 call s:setup_commands(a:cchar)
5102
5103 " No quickfix/location list
5104 call assert_fails('Xbelow', 'E42:')
5105 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005106 call assert_fails('Xbefore', 'E42:')
5107 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005108
5109 " Empty quickfix/location list
5110 call g:Xsetlist([])
5111 call assert_fails('Xbelow', 'E42:')
5112 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005113 call assert_fails('Xbefore', 'E42:')
5114 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005115
5116 call s:create_test_file('X1')
5117 call s:create_test_file('X2')
5118 call s:create_test_file('X3')
5119 call s:create_test_file('X4')
5120
5121 " Invalid entries
5122 edit X1
5123 call g:Xsetlist(["E1", "E2"])
5124 call assert_fails('Xbelow', 'E42:')
5125 call assert_fails('Xabove', 'E42:')
5126 call assert_fails('3Xbelow', 'E42:')
5127 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005128 call assert_fails('Xbefore', 'E42:')
5129 call assert_fails('Xafter', 'E42:')
5130 call assert_fails('3Xbefore', 'E42:')
5131 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005132
5133 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005134 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 +02005135 edit +7 X2
5136 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005137 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005138 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005139 normal 7G
5140 Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005141 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005142 call assert_fails('Xbefore', 'E553:')
5143
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005144 normal 2j
5145 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005146 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005147 normal 7G
5148 Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005149 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005150
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005151 " Last error in this file
5152 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005153 call assert_equal(['X2', 15], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005154 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005155 normal gg
5156 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005157 call assert_equal(['X2', 15, 4], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005158 call assert_fails('Xafter', 'E553:')
5159
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005160 " First error in this file
5161 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005162 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005163 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005164 normal G
5165 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005166 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005167 call assert_fails('Xbefore', 'E553:')
5168
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005169 normal gg
5170 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005171 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005172 normal gg
5173 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005174 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005175
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005176 normal G
5177 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005178 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005179 normal G
5180 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005181 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005182
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005183 edit X4
5184 call assert_fails('Xabove', 'E42:')
5185 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005186 call assert_fails('Xbefore', 'E42:')
5187 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005188 if a:cchar == 'l'
5189 " If a buffer has location list entries from some other window but not
5190 " from the current window, then the commands should fail.
5191 edit X1 | split | call setloclist(0, [], 'f')
5192 call assert_fails('Xabove', 'E776:')
5193 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005194 call assert_fails('Xbefore', 'E776:')
5195 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005196 close
5197 endif
5198
5199 " Test for lines with multiple quickfix entries
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005200 let lines =<< trim END
5201 X1:5:L5
5202 X2:5:1:L5_1
5203 X2:5:2:L5_2
5204 X2:5:3:L5_3
5205 X2:10:1:L10_1
5206 X2:10:2:L10_2
5207 X2:10:3:L10_3
5208 X2:15:1:L15_1
5209 X2:15:2:L15_2
5210 X2:15:3:L15_3
5211 X3:3:L3
5212 END
5213 Xexpr lines
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005214 edit +1 X2
5215 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005216 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005217 normal 1G
5218 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005219 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005220
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005221 normal gg
5222 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005223 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005224 normal gg
5225 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005226 call assert_equal(['X2', 15, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005227
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005228 normal G
5229 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005230 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005231 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005232 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005233 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005234
5235 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005236 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005237 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005238 normal G
5239 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005240 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005241
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005242 normal 10G
5243 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005244 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005245 normal 10G$
5246 2Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005247 call assert_equal(['X2', 10, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005248
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005249 normal 10G
5250 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005251 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005252 normal 9G
5253 5Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005254 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005255
5256 " Invalid range
5257 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02005258 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005259 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005260 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02005261 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005262 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005263 endif
5264
5265 call delete('X1')
5266 call delete('X2')
5267 call delete('X3')
5268 call delete('X4')
5269endfunc
5270
5271func Test_cbelow()
5272 call Xtest_below('c')
5273 call Xtest_below('l')
5274endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02005275
5276func Test_quickfix_count()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005277 let commands =<< trim END
5278 cNext
5279 cNfile
5280 cabove
5281 cbelow
5282 cfirst
5283 clast
5284 cnewer
5285 cnext
5286 cnfile
5287 colder
5288 cprevious
5289 crewind
5290 lNext
5291 lNfile
5292 labove
5293 lbelow
5294 lfirst
5295 llast
5296 lnewer
5297 lnext
5298 lnfile
5299 lolder
5300 lprevious
5301 lrewind
5302 END
Bram Moolenaar25190db2019-05-04 15:05:28 +02005303 for cmd in commands
5304 call assert_fails('-1' .. cmd, 'E16:')
5305 call assert_fails('.' .. cmd, 'E16:')
5306 call assert_fails('%' .. cmd, 'E16:')
5307 call assert_fails('$' .. cmd, 'E16:')
5308 endfor
5309endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005310
5311" Test for aborting quickfix commands using QuickFixCmdPre
5312func Xtest_qfcmd_abort(cchar)
5313 call s:setup_commands(a:cchar)
5314
5315 call g:Xsetlist([], 'f')
5316
5317 " cexpr/lexpr
5318 let e = ''
5319 try
5320 Xexpr ["F1:10:Line10", "F2:20:Line20"]
5321 catch /.*/
5322 let e = v:exception
5323 endtry
5324 call assert_equal('AbortCmd', e)
5325 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5326
5327 " cfile/lfile
5328 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
5329 let e = ''
5330 try
5331 Xfile Xfile1
5332 catch /.*/
5333 let e = v:exception
5334 endtry
5335 call assert_equal('AbortCmd', e)
5336 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5337 call delete('Xfile1')
5338
5339 " cgetbuffer/lgetbuffer
5340 enew!
5341 call append(0, ["F1:10:Line10", "F2:20:Line20"])
5342 let e = ''
5343 try
5344 Xgetbuffer
5345 catch /.*/
5346 let e = v:exception
5347 endtry
5348 call assert_equal('AbortCmd', e)
5349 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5350 enew!
5351
5352 " vimgrep/lvimgrep
5353 let e = ''
5354 try
5355 Xvimgrep /func/ test_quickfix.vim
5356 catch /.*/
5357 let e = v:exception
5358 endtry
5359 call assert_equal('AbortCmd', e)
5360 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5361
5362 " helpgrep/lhelpgrep
5363 let e = ''
5364 try
5365 Xhelpgrep quickfix
5366 catch /.*/
5367 let e = v:exception
5368 endtry
5369 call assert_equal('AbortCmd', e)
5370 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5371
5372 " grep/lgrep
5373 if has('unix')
5374 let e = ''
5375 try
5376 silent Xgrep func test_quickfix.vim
5377 catch /.*/
5378 let e = v:exception
5379 endtry
5380 call assert_equal('AbortCmd', e)
5381 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5382 endif
5383endfunc
5384
5385func Test_qfcmd_abort()
5386 augroup QF_Test
5387 au!
5388 autocmd QuickFixCmdPre * throw "AbortCmd"
5389 augroup END
5390
5391 call Xtest_qfcmd_abort('c')
5392 call Xtest_qfcmd_abort('l')
5393
5394 augroup QF_Test
5395 au!
5396 augroup END
5397endfunc
5398
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005399" Test for using a file in one of the parent directories.
5400func Test_search_in_dirstack()
5401 call mkdir('Xtestdir/a/b/c', 'p')
5402 let save_cwd = getcwd()
5403 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
5404 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
5405 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
5406 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
5407
5408 let lines = "Entering dir Xtestdir\n" .
5409 \ "Entering dir a\n" .
5410 \ "Entering dir b\n" .
5411 \ "Xfile2:2:X2_L2\n" .
5412 \ "Leaving dir a\n" .
5413 \ "Xfile1:2:X1_L2\n" .
5414 \ "Xfile3:1:X3_L1\n" .
5415 \ "Entering dir c\n" .
5416 \ "Xfile4:2:X4_L2\n" .
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005417 \ "Leaving dir c\n"
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005418 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005419 cexpr lines .. "Leaving dir Xtestdir|\n" | let next = 1
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005420 call assert_equal(11, getqflist({'size' : 0}).size)
5421 call assert_equal(4, getqflist({'idx' : 0}).idx)
5422 call assert_equal('X2_L2', getline('.'))
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005423 call assert_equal(1, next)
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005424 cnext
5425 call assert_equal(6, getqflist({'idx' : 0}).idx)
5426 call assert_equal('X1_L2', getline('.'))
5427 cnext
5428 call assert_equal(7, getqflist({'idx' : 0}).idx)
5429 call assert_equal(1, line('$'))
5430 call assert_equal('', getline(1))
5431 cnext
5432 call assert_equal(9, getqflist({'idx' : 0}).idx)
5433 call assert_equal(1, line('$'))
5434 call assert_equal('', getline(1))
5435
5436 set efm&
5437 exe 'cd ' . save_cwd
5438 call delete('Xtestdir', 'rf')
5439endfunc
5440
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005441" Test for :cquit
5442func Test_cquit()
5443 " Exit Vim with a non-zero value
5444 if RunVim([], ["cquit 7"], '')
5445 call assert_equal(7, v:shell_error)
5446 endif
5447
5448 if RunVim([], ["50cquit"], '')
5449 call assert_equal(50, v:shell_error)
5450 endif
5451
5452 " Exit Vim with default value
5453 if RunVim([], ["cquit"], '')
5454 call assert_equal(1, v:shell_error)
5455 endif
5456
5457 " Exit Vim with zero value
5458 if RunVim([], ["cquit 0"], '')
5459 call assert_equal(0, v:shell_error)
5460 endif
5461
5462 " Exit Vim with negative value
5463 call assert_fails('-3cquit', 'E16:')
5464endfunc
5465
Bram Moolenaar858ba062020-05-31 23:11:59 +02005466" Test for getting a specific item from a quickfix list
5467func Xtest_getqflist_by_idx(cchar)
5468 call s:setup_commands(a:cchar)
5469 " Empty list
5470 call assert_equal([], g:Xgetlist({'idx' : 1, 'items' : 0}).items)
5471 Xexpr ['F1:10:L10', 'F1:20:L20']
5472 let l = g:Xgetlist({'idx' : 2, 'items' : 0}).items
5473 call assert_equal(bufnr('F1'), l[0].bufnr)
5474 call assert_equal(20, l[0].lnum)
5475 call assert_equal('L20', l[0].text)
5476 call assert_equal([], g:Xgetlist({'idx' : -1, 'items' : 0}).items)
5477 call assert_equal([], g:Xgetlist({'idx' : 3, 'items' : 0}).items)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005478 call assert_equal({}, g:Xgetlist(#{idx: "abc"}))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005479 %bwipe!
5480endfunc
5481
5482func Test_getqflist_by_idx()
5483 call Xtest_getqflist_by_idx('c')
5484 call Xtest_getqflist_by_idx('l')
5485endfunc
5486
5487" Test for the 'quickfixtextfunc' setting
5488func Tqfexpr(info)
5489 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005490 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005491 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005492 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005493 endif
5494
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005495 let l = []
5496 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5497 let e = qfl[idx]
5498 let s = ''
5499 if e.bufnr != 0
5500 let bname = bufname(e.bufnr)
5501 let s ..= fnamemodify(bname, ':.')
5502 endif
5503 let s ..= '-'
5504 let s ..= 'L' .. string(e.lnum) .. 'C' .. string(e.col) .. '-'
5505 let s ..= e.text
5506 call add(l, s)
5507 endfor
Bram Moolenaar858ba062020-05-31 23:11:59 +02005508
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005509 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005510endfunc
5511
5512func Xtest_qftextfunc(cchar)
5513 call s:setup_commands(a:cchar)
5514
5515 set efm=%f:%l:%c:%m
5516 set quickfixtextfunc=Tqfexpr
Bram Moolenaard43906d2020-07-20 21:31:32 +02005517 call assert_equal('Tqfexpr', &quickfixtextfunc)
5518 call assert_equal('',
5519 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
thinca6864efa2021-06-19 20:45:20 +02005520 call g:Xsetlist([
5521 \ { 'filename': 'F1', 'lnum': 10, 'col': 2,
5522 \ 'end_col': 7, 'text': 'green'},
5523 \ { 'filename': 'F1', 'lnum': 20, 'end_lnum': 25, 'col': 4,
5524 \ 'end_col': 8, 'text': 'blue'},
5525 \ ])
5526
Bram Moolenaar858ba062020-05-31 23:11:59 +02005527 Xwindow
5528 call assert_equal('F1-L10C2-green', getline(1))
5529 call assert_equal('F1-L20C4-blue', getline(2))
5530 Xclose
5531 set quickfixtextfunc&vim
5532 Xwindow
thinca6864efa2021-06-19 20:45:20 +02005533 call assert_equal('F1|10 col 2-7| green', getline(1))
5534 call assert_equal('F1|20-25 col 4-8| blue', getline(2))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005535 Xclose
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005536
5537 set efm=%f:%l:%c:%m
5538 set quickfixtextfunc=Tqfexpr
5539 " Update the list with only the cwindow
5540 Xwindow
5541 only
5542 call g:Xsetlist([
5543 \ { 'filename': 'F2', 'lnum': 20, 'col': 2,
5544 \ 'end_col': 7, 'text': 'red'}
5545 \ ])
5546 call assert_equal(['F2-L20C2-red'], getline(1, '$'))
5547 new
5548 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005549 set efm&
5550 set quickfixtextfunc&
5551
5552 " Test for per list 'quickfixtextfunc' setting
5553 func PerQfText(info)
5554 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005555 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005556 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005557 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005558 endif
5559 if empty(qfl)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005560 return []
Bram Moolenaar858ba062020-05-31 23:11:59 +02005561 endif
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005562 let l = []
5563 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5564 call add(l, 'Line ' .. qfl[idx].lnum .. ', Col ' .. qfl[idx].col)
5565 endfor
5566 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005567 endfunc
5568 set quickfixtextfunc=Tqfexpr
5569 call g:Xsetlist([], ' ', {'quickfixtextfunc' : "PerQfText"})
5570 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5571 Xwindow
5572 call assert_equal('Line 10, Col 2', getline(1))
5573 call assert_equal('Line 20, Col 4', getline(2))
5574 Xclose
Bram Moolenaard43906d2020-07-20 21:31:32 +02005575 call assert_equal(function('PerQfText'),
5576 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005577 " Add entries to the list when the quickfix buffer is hidden
5578 Xaddexpr ['F1:30:6:red']
5579 Xwindow
5580 call assert_equal('Line 30, Col 6', getline(3))
5581 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005582 call g:Xsetlist([], 'r', {'quickfixtextfunc' : ''})
Bram Moolenaard43906d2020-07-20 21:31:32 +02005583 call assert_equal('', g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar858ba062020-05-31 23:11:59 +02005584 set quickfixtextfunc&
5585 delfunc PerQfText
5586
5587 " Non-existing function
5588 set quickfixtextfunc=Tabc
5589 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5590 call assert_fails("Xwindow", 'E117:')
5591 Xclose
5592 set quickfixtextfunc&
5593
5594 " set option to a non-function
5595 set quickfixtextfunc=[10,\ 20]
5596 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5597 call assert_fails("Xwindow", 'E117:')
5598 Xclose
5599 set quickfixtextfunc&
5600
5601 " set option to a function with different set of arguments
5602 func Xqftext(a, b, c)
5603 return a:a .. a:b .. a:c
5604 endfunc
5605 set quickfixtextfunc=Xqftext
5606 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E119:')
5607 call assert_fails("Xwindow", 'E119:')
5608 Xclose
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005609
5610 " set option to a function that returns a list with non-strings
5611 func Xqftext2(d)
5612 return ['one', [], 'two']
5613 endfunc
5614 set quickfixtextfunc=Xqftext2
5615 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue', 'F1:30:6:red']",
5616 \ 'E730:')
5617 call assert_fails('Xwindow', 'E730:')
Bram Moolenaard43906d2020-07-20 21:31:32 +02005618 call assert_equal(['one', 'F1|20 col 4| blue', 'F1|30 col 6| red'],
5619 \ getline(1, '$'))
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005620 Xclose
5621
Bram Moolenaar858ba062020-05-31 23:11:59 +02005622 set quickfixtextfunc&
5623 delfunc Xqftext
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005624 delfunc Xqftext2
Bram Moolenaard43906d2020-07-20 21:31:32 +02005625
5626 " set the global option to a lambda function
5627 set quickfixtextfunc={d\ ->\ map(g:Xgetlist({'id'\ :\ d.id,\ 'items'\ :\ 1}).items[d.start_idx-1:d.end_idx-1],\ 'v:val.text')}
5628 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5629 Xwindow
5630 call assert_equal(['green', 'blue'], getline(1, '$'))
5631 Xclose
5632 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)
5633 set quickfixtextfunc&
5634
5635 " use a lambda function that returns an empty list
5636 set quickfixtextfunc={d\ ->\ []}
5637 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5638 Xwindow
5639 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5640 \ getline(1, '$'))
5641 Xclose
5642 set quickfixtextfunc&
5643
5644 " use a lambda function that returns a list with empty strings
5645 set quickfixtextfunc={d\ ->\ ['',\ '']}
5646 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5647 Xwindow
5648 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5649 \ getline(1, '$'))
5650 Xclose
5651 set quickfixtextfunc&
5652
5653 " set the per-quickfix list text function to a lambda function
5654 call g:Xsetlist([], ' ',
5655 \ {'quickfixtextfunc' :
5656 \ {d -> map(g:Xgetlist({'id' : d.id, 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5657 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5658 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5659 Xwindow
5660 call assert_equal('Line 10, Col 2', getline(1))
5661 call assert_equal('Line 20, Col 4', getline(2))
5662 Xclose
5663 call assert_match("function('<lambda>\\d\\+')", string(g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc))
5664 call g:Xsetlist([], 'f')
Bram Moolenaar858ba062020-05-31 23:11:59 +02005665endfunc
5666
5667func Test_qftextfunc()
5668 call Xtest_qftextfunc('c')
5669 call Xtest_qftextfunc('l')
5670endfunc
5671
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005672func Test_qftextfunc_callback()
5673 let lines =<< trim END
5674 set efm=%f:%l:%c:%m
5675
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00005676 #" Test for using a function name
5677 LET &qftf = 'g:Tqfexpr'
5678 cexpr "F0:0:0:L0"
5679 copen
5680 call assert_equal('F0-L0C0-L0', getline(1))
5681 cclose
5682
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005683 #" Test for using a function()
5684 set qftf=function('g:Tqfexpr')
5685 cexpr "F1:1:1:L1"
5686 copen
5687 call assert_equal('F1-L1C1-L1', getline(1))
5688 cclose
5689
5690 #" Using a funcref variable to set 'quickfixtextfunc'
5691 VAR Fn = function('g:Tqfexpr')
5692 LET &qftf = Fn
5693 cexpr "F2:2:2:L2"
5694 copen
5695 call assert_equal('F2-L2C2-L2', getline(1))
5696 cclose
5697
5698 #" Using string(funcref_variable) to set 'quickfixtextfunc'
5699 LET Fn = function('g:Tqfexpr')
5700 LET &qftf = string(Fn)
5701 cexpr "F3:3:3:L3"
5702 copen
5703 call assert_equal('F3-L3C3-L3', getline(1))
5704 cclose
5705
5706 #" Test for using a funcref()
5707 set qftf=funcref('g:Tqfexpr')
5708 cexpr "F4:4:4:L4"
5709 copen
5710 call assert_equal('F4-L4C4-L4', getline(1))
5711 cclose
5712
5713 #" Using a funcref variable to set 'quickfixtextfunc'
5714 LET Fn = funcref('g:Tqfexpr')
5715 LET &qftf = Fn
5716 cexpr "F5:5:5:L5"
5717 copen
5718 call assert_equal('F5-L5C5-L5', getline(1))
5719 cclose
5720
5721 #" Using a string(funcref_variable) to set 'quickfixtextfunc'
5722 LET Fn = funcref('g:Tqfexpr')
5723 LET &qftf = string(Fn)
5724 cexpr "F5:5:5:L5"
5725 copen
5726 call assert_equal('F5-L5C5-L5', getline(1))
5727 cclose
5728
5729 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00005730 VAR optval = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005731 LET optval = substitute(optval, ' ', '\\ ', 'g')
5732 exe "set qftf=" .. optval
5733 cexpr "F6:6:6:L6"
5734 copen
5735 call assert_equal('F6-L6C6-L6', getline(1))
5736 cclose
5737
5738 #" Set 'quickfixtextfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005739 LET &qftf = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005740 cexpr "F7:7:7:L7"
5741 copen
5742 call assert_equal('F7-L7C7-L7', getline(1))
5743 cclose
5744
5745 #" Set 'quickfixtextfunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005746 LET &qftf = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005747 cexpr "F8:8:8:L8"
5748 copen
5749 call assert_equal('F8-L8C8-L8', getline(1))
5750 cclose
5751
5752 #" Set 'quickfixtextfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005753 VAR Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005754 LET &qftf = Lambda
5755 cexpr "F9:9:9:L9"
5756 copen
5757 call assert_equal('F9-L9C9-L9', getline(1))
5758 cclose
5759
5760 #" Set 'quickfixtextfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005761 LET Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005762 LET &qftf = string(Lambda)
5763 cexpr "F9:9:9:L9"
5764 copen
5765 call assert_equal('F9-L9C9-L9', getline(1))
5766 cclose
5767 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00005768 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005769
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00005770 " Test for using a script-local function name
5771 func s:TqfFunc2(info)
5772 let g:TqfFunc2Args = [a:info.start_idx, a:info.end_idx]
5773 return ''
5774 endfunc
5775 let g:TqfFunc2Args = []
5776 set quickfixtextfunc=s:TqfFunc2
5777 cexpr "F10:10:10:L10"
5778 cclose
5779 call assert_equal([1, 1], g:TqfFunc2Args)
5780
5781 let &quickfixtextfunc = 's:TqfFunc2'
5782 cexpr "F11:11:11:L11"
5783 cclose
5784 call assert_equal([1, 1], g:TqfFunc2Args)
5785 delfunc s:TqfFunc2
5786
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005787 " set 'quickfixtextfunc' to a partial with dict. This used to cause a crash.
5788 func SetQftfFunc()
5789 let params = {'qftf': function('g:DictQftfFunc')}
5790 let &quickfixtextfunc = params.qftf
5791 endfunc
5792 func g:DictQftfFunc(_) dict
5793 endfunc
5794 call SetQftfFunc()
5795 new
5796 call SetQftfFunc()
5797 bw
5798 call test_garbagecollect_now()
5799 new
5800 set qftf=
5801 wincmd w
5802 set qftf=
5803 :%bw!
5804
5805 " set per-quickfix list 'quickfixtextfunc' to a partial with dict. This used
5806 " to cause a crash.
5807 let &qftf = ''
5808 func SetLocalQftfFunc()
5809 let params = {'qftf': function('g:DictQftfFunc')}
5810 call setqflist([], 'a', {'quickfixtextfunc' : params.qftf})
5811 endfunc
5812 call SetLocalQftfFunc()
5813 call test_garbagecollect_now()
5814 call setqflist([], 'a', {'quickfixtextfunc' : ''})
5815 delfunc g:DictQftfFunc
5816 delfunc SetQftfFunc
5817 delfunc SetLocalQftfFunc
5818 set efm&
5819endfunc
5820
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02005821" Test for updating a location list for some other window and check that
5822" 'qftextfunc' uses the correct location list.
5823func Test_qftextfunc_other_loclist()
5824 %bw!
5825 call setloclist(0, [], 'f')
5826
5827 " create a window and a location list for it and open the location list
5828 " window
5829 lexpr ['F1:10:12:one', 'F1:20:14:two']
5830 let w1_id = win_getid()
5831 call setloclist(0, [], ' ',
5832 \ {'lines': ['F1:10:12:one', 'F1:20:14:two'],
5833 \ 'quickfixtextfunc':
5834 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5835 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5836 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5837 lwindow
5838 let w2_id = win_getid()
5839
5840 " create another window and a location list for it and open the location
5841 " list window
5842 topleft new
5843 let w3_id = win_getid()
5844 call setloclist(0, [], ' ',
5845 \ {'lines': ['F2:30:32:eleven', 'F2:40:34:twelve'],
5846 \ 'quickfixtextfunc':
5847 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5848 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5849 \ "'Ligne ' .. v:val.lnum .. ', Colonne ' .. v:val.col")}})
5850 lwindow
5851 let w4_id = win_getid()
5852
5853 topleft new
5854 lexpr ['F3:50:52:green', 'F3:60:54:blue']
5855 let w5_id = win_getid()
5856
5857 " change the location list for some other window
5858 call setloclist(0, [], 'r', {'lines': ['F3:55:56:aaa', 'F3:57:58:bbb']})
5859 call setloclist(w1_id, [], 'r', {'lines': ['F1:62:63:bbb', 'F1:64:65:ccc']})
5860 call setloclist(w3_id, [], 'r', {'lines': ['F2:76:77:ddd', 'F2:78:79:eee']})
5861 call assert_equal(['Line 62, Col 63', 'Line 64, Col 65'],
5862 \ getbufline(winbufnr(w2_id), 1, '$'))
5863 call assert_equal(['Ligne 76, Colonne 77', 'Ligne 78, Colonne 79'],
5864 \ getbufline(winbufnr(w4_id), 1, '$'))
5865 call setloclist(w2_id, [], 'r', {'lines': ['F1:32:33:fff', 'F1:34:35:ggg']})
5866 call setloclist(w4_id, [], 'r', {'lines': ['F2:46:47:hhh', 'F2:48:49:jjj']})
5867 call assert_equal(['Line 32, Col 33', 'Line 34, Col 35'],
5868 \ getbufline(winbufnr(w2_id), 1, '$'))
5869 call assert_equal(['Ligne 46, Colonne 47', 'Ligne 48, Colonne 49'],
5870 \ getbufline(winbufnr(w4_id), 1, '$'))
5871
5872 call win_gotoid(w5_id)
5873 lwindow
5874 call assert_equal(['F3|55 col 56| aaa', 'F3|57 col 58| bbb'],
5875 \ getline(1, '$'))
5876 %bw!
5877endfunc
5878
Bram Moolenaarec98e932020-06-08 19:35:59 +02005879" Running :lhelpgrep command more than once in a help window, doesn't jump to
5880" the help topic
5881func Test_lhelpgrep_from_help_window()
5882 call mkdir('Xtestdir/doc', 'p')
5883 call writefile(['window'], 'Xtestdir/doc/a.txt')
5884 call writefile(['buffer'], 'Xtestdir/doc/b.txt')
5885 let save_rtp = &rtp
5886 let &rtp = 'Xtestdir'
5887 lhelpgrep window
5888 lhelpgrep buffer
5889 call assert_equal('b.txt', fnamemodify(@%, ":p:t"))
5890 lhelpgrep window
5891 call assert_equal('a.txt', fnamemodify(@%, ":p:t"))
5892 let &rtp = save_rtp
5893 call delete('Xtestdir', 'rf')
5894 new | only!
5895endfunc
5896
Bram Moolenaarf7c4d832020-08-11 20:42:19 +02005897" Test for the crash fixed by 7.3.715
5898func Test_setloclist_crash()
5899 %bw!
5900 let g:BufNum = bufnr()
5901 augroup QF_Test
5902 au!
5903 au BufUnload * call setloclist(0, [{'bufnr':g:BufNum, 'lnum':1, 'col':1, 'text': 'tango down'}])
5904 augroup END
5905
5906 try
5907 lvimgrep /.*/ *.mak
5908 catch /E926:/
5909 endtry
5910 call assert_equal('tango down', getloclist(0, {'items' : 0}).items[0].text)
5911 call assert_equal(1, getloclist(0, {'size' : 0}).size)
5912
5913 augroup QF_Test
5914 au!
5915 augroup END
5916 unlet g:BufNum
5917 %bw!
5918endfunc
5919
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005920" Test for adding an invalid entry with the quickfix window open and making
5921" sure that the window contents are not changed
5922func Test_add_invalid_entry_with_qf_window()
5923 call setqflist([], 'f')
5924 cexpr "Xfile1:10:aa"
5925 copen
5926 call setqflist(['bb'], 'a')
5927 call assert_equal(1, line('$'))
5928 call assert_equal(['Xfile1|10| aa'], getline(1, '$'))
shane.xb.qian0d5e1ec2021-06-20 16:31:00 +02005929 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())
5930
5931 call setqflist([{'lnum': 10 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5932 call assert_equal(1 , line('$'))
5933 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5934 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())
5935
5936 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')
5937 call assert_equal(1 , line('$'))
5938 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5939 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())
5940
5941 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')
5942 call assert_equal(1 , line('$'))
5943 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5944 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())
5945
5946 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')
5947 call assert_equal(1 , line('$'))
5948 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5949 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())
5950
5951 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')
5952 call assert_equal(1 , line('$'))
5953 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5954 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())
5955
5956 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')
5957 call assert_equal(1 , line('$'))
5958 call assert_equal(['Xfile1|10 col 666-222| aa'] , getline(1 , '$'))
5959 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())
5960
5961 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')
5962 call assert_equal(1 , line('$'))
5963 call assert_equal(['Xfile1|10-6 col 666-222| aa'] , getline(1 , '$'))
5964 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 +01005965 cclose
5966endfunc
5967
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01005968" Test for very weird problem: autocommand causes a failure, resulting opening
5969" the quickfix window to fail. This still splits the window, but otherwise
5970" should not mess up buffers.
5971func Test_quickfix_window_fails_to_open()
5972 CheckScreendump
5973
5974 let lines =<< trim END
5975 anything
5976 try
5977 anything
5978 endtry
5979 END
5980 call writefile(lines, 'XquickfixFails')
5981
5982 let lines =<< trim END
5983 split XquickfixFails
5984 silent vimgrep anything %
5985 normal o
5986 au BufLeave * ++once source XquickfixFails
5987 " This will trigger the autocommand, which causes an error, what follows
5988 " is aborted but the window was already split.
5989 silent! cwindow
5990 END
5991 call writefile(lines, 'XtestWinFails')
5992 let buf = RunVimInTerminal('-S XtestWinFails', #{rows: 13})
5993 call VerifyScreenDump(buf, 'Test_quickfix_window_fails', {})
5994
5995 " clean up
5996 call term_sendkeys(buf, ":bwipe!\<CR>")
5997 call term_wait(buf)
5998 call StopVimInTerminal(buf)
5999 call delete('XtestWinFails')
6000 call delete('XquickfixFails')
6001endfunc
6002
Bram Moolenaar2d870f82020-12-05 13:41:01 +01006003" Test for updating the quickfix buffer whenever the associated quickfix list
Bram Moolenaar287153c2020-11-29 14:20:27 +01006004" is changed.
6005func Xqfbuf_update(cchar)
6006 call s:setup_commands(a:cchar)
6007
6008 Xexpr "F1:1:line1"
6009 Xopen
6010 call assert_equal(['F1|1| line1'], getline(1, '$'))
6011 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6012
6013 " Test setqflist() using the 'lines' key in 'what'
6014 " add a new entry
6015 call g:Xsetlist([], 'a', {'lines' : ['F2:2: line2']})
6016 call assert_equal(['F1|1| line1', 'F2|2| line2'], getline(1, '$'))
6017 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6018 " replace all the entries with a single entry
6019 call g:Xsetlist([], 'r', {'lines' : ['F3:3: line3']})
6020 call assert_equal(['F3|3| line3'], getline(1, '$'))
6021 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6022 " remove all the entries
6023 call g:Xsetlist([], 'r', {'lines' : []})
6024 call assert_equal([''], getline(1, '$'))
6025 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6026 " add a new list
6027 call g:Xsetlist([], ' ', {'lines' : ['F4:4: line4']})
6028 call assert_equal(['F4|4| line4'], getline(1, '$'))
6029 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6030
6031 " Test setqflist() using the 'items' key in 'what'
6032 " add a new entry
6033 call g:Xsetlist([], 'a', {'items' : [{'filename' : 'F5', 'lnum' : 5, 'text' : 'line5'}]})
6034 call assert_equal(['F4|4| line4', 'F5|5| line5'], getline(1, '$'))
6035 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6036 " replace all the entries with a single entry
6037 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F6', 'lnum' : 6, 'text' : 'line6'}]})
6038 call assert_equal(['F6|6| line6'], getline(1, '$'))
6039 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6040 " remove all the entries
6041 call g:Xsetlist([], 'r', {'items' : []})
6042 call assert_equal([''], getline(1, '$'))
6043 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6044 " add a new list
6045 call g:Xsetlist([], ' ', {'items' : [{'filename' : 'F7', 'lnum' : 7, 'text' : 'line7'}]})
6046 call assert_equal(['F7|7| line7'], getline(1, '$'))
6047 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6048
6049 call g:Xsetlist([], ' ', {})
6050 call assert_equal([''], getline(1, '$'))
6051 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6052
6053 Xclose
6054endfunc
6055
6056func Test_qfbuf_update()
6057 call Xqfbuf_update('c')
6058 call Xqfbuf_update('l')
6059endfunc
6060
Bram Moolenaar8c801b32021-03-05 20:58:22 +01006061func Test_vimgrep_noswapfile()
6062 set noswapfile
6063 call writefile(['one', 'two', 'three'], 'Xgreppie')
6064 vimgrep two Xgreppie
6065 call assert_equal('two', getline('.'))
6066
6067 call delete('Xgreppie')
6068 set swapfile
6069endfunc
6070
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006071" Test for the :vimgrep 'f' flag (fuzzy match)
6072func Xvimgrep_fuzzy_match(cchar)
6073 call s:setup_commands(a:cchar)
6074
6075 Xvimgrep /three one/f Xfile*
6076 let l = g:Xgetlist()
6077 call assert_equal(2, len(l))
6078 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6079 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6080 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6081 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6082
6083 Xvimgrep /the/f Xfile*
6084 let l = g:Xgetlist()
6085 call assert_equal(3, len(l))
6086 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6087 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6088 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6089 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6090 call assert_equal(['Xfile2', 4, 4, 'aaathreeaaa'],
6091 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6092
6093 Xvimgrep /aaa/fg Xfile*
6094 let l = g:Xgetlist()
6095 call assert_equal(4, len(l))
6096 call assert_equal(['Xfile1', 2, 1, 'aaaaaa'],
6097 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6098 call assert_equal(['Xfile1', 2, 4, 'aaaaaa'],
6099 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6100 call assert_equal(['Xfile2', 4, 1, 'aaathreeaaa'],
6101 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6102 call assert_equal(['Xfile2', 4, 9, 'aaathreeaaa'],
6103 \ [bufname(l[3].bufnr), l[3].lnum, l[3].col, l[3].text])
6104
6105 call assert_fails('Xvimgrep /xyz/fg Xfile*', 'E480:')
6106endfunc
6107
6108func Test_vimgrep_fuzzy_match()
6109 call writefile(['one two three', 'aaaaaa'], 'Xfile1')
6110 call writefile(['one', 'three one two', 'two', 'aaathreeaaa'], 'Xfile2')
6111 call Xvimgrep_fuzzy_match('c')
6112 call Xvimgrep_fuzzy_match('l')
6113 call delete('Xfile1')
6114 call delete('Xfile2')
6115endfunc
6116
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006117func Test_locationlist_open_in_newtab()
6118 call s:create_test_file('Xqftestfile1')
6119 call s:create_test_file('Xqftestfile2')
6120 call s:create_test_file('Xqftestfile3')
6121
6122 %bwipe!
6123
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01006124 let lines =<< trim END
6125 Xqftestfile1:5:Line5
6126 Xqftestfile2:10:Line10
6127 Xqftestfile3:16:Line16
6128 END
6129 lgetexpr lines
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006130
6131 silent! llast
6132 call assert_equal(1, tabpagenr('$'))
6133 call assert_equal('Xqftestfile3', bufname())
6134
6135 set switchbuf=newtab
6136
6137 silent! lfirst
6138 call assert_equal(2, tabpagenr('$'))
6139 call assert_equal('Xqftestfile1', bufname())
6140
6141 silent! lnext
6142 call assert_equal(3, tabpagenr('$'))
6143 call assert_equal('Xqftestfile2', bufname())
6144
6145 call delete('Xqftestfile1')
6146 call delete('Xqftestfile2')
6147 call delete('Xqftestfile3')
6148 set switchbuf&vim
6149
6150 %bwipe!
6151endfunc
6152
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02006153" Test for win_gettype() in quickfix and location list windows
6154func Test_win_gettype()
6155 copen
6156 call assert_equal("quickfix", win_gettype())
6157 let wid = win_getid()
6158 wincmd p
6159 call assert_equal("quickfix", win_gettype(wid))
6160 cclose
6161 lexpr ''
6162 lopen
6163 call assert_equal("loclist", win_gettype())
6164 let wid = win_getid()
6165 wincmd p
6166 call assert_equal("loclist", win_gettype(wid))
6167 lclose
6168endfunc
6169
Christian Brabandt0b226f62021-12-01 10:54:24 +00006170fun Test_vimgrep_nomatch()
6171 call XexprTests('c')
6172 call g:Xsetlist([{'lnum':10,'text':'Line1'}])
6173 copen
6174 if has("win32")
6175 call assert_fails('vimgrep foo *.zzz', 'E479:')
6176 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'}]
6177 else
6178 call assert_fails('vimgrep foo *.zzz', 'E480:')
6179 let expected = []
6180 endif
6181 call assert_equal(expected, getqflist())
6182 cclose
6183endfunc
6184
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00006185" Test for opening the quickfix window in two tab pages and then closing one
6186" of the quickfix windows. This should not make the quickfix buffer unlisted.
6187" (github issue #9300).
6188func Test_two_qf_windows()
6189 cexpr "F1:1:line1"
6190 copen
6191 tabnew
6192 copen
6193 call assert_true(&buflisted)
6194 cclose
6195 tabfirst
6196 call assert_true(&buflisted)
6197 let bnum = bufnr()
6198 cclose
6199 " if all the quickfix windows are closed, then buffer should be unlisted.
6200 call assert_false(buflisted(bnum))
6201 %bw!
6202
6203 " Repeat the test for a location list
6204 lexpr "F2:2:line2"
6205 lopen
6206 let bnum = bufnr()
6207 tabnew
6208 exe "buffer" bnum
6209 tabfirst
6210 lclose
6211 tablast
6212 call assert_true(buflisted(bnum))
6213 tabclose
6214 lopen
6215 call assert_true(buflisted(bnum))
6216 lclose
6217 call assert_false(buflisted(bnum))
6218 %bw!
6219endfunc
6220
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006221" Weird sequence of commands that caused entering a wiped-out buffer
6222func Test_lopen_bwipe()
6223 func R()
6224 silent! tab lopen
6225 e x
6226 silent! lfile
6227 endfunc
6228
6229 cal R()
6230 cal R()
6231 cal R()
6232 bw!
6233 delfunc R
6234endfunc
6235
Bram Moolenaare3537ae2022-02-08 15:05:20 +00006236" Another sequence of commands that caused all buffers to be wiped out
6237func Test_lopen_bwipe_all()
6238 let lines =<< trim END
6239 func R()
6240 silent! tab lopen
6241 e foo
6242 silent! lfile
6243 endfunc
6244 cal R()
6245 exe "norm \<C-W>\<C-V>0"
6246 cal R()
6247 bwipe
6248
6249 call writefile(['done'], 'Xresult')
6250 qall!
6251 END
6252 call writefile(lines, 'Xscript')
6253 if RunVim([], [], '-u NONE -n -X -Z -e -m -s -S Xscript')
6254 call assert_equal(['done'], readfile('Xresult'))
6255 endif
6256
6257 call delete('Xscript')
6258 call delete('Xresult')
6259endfunc
6260
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00006261" Test for calling setqflist() function recursively
6262func Test_recursive_setqflist()
6263 augroup QF_Test
6264 au!
6265 autocmd BufWinEnter quickfix call setqflist([], 'r')
6266 augroup END
6267
6268 copen
6269 call assert_fails("call setqflist([], 'a')", 'E952:')
6270
6271 augroup QF_Test
6272 au!
6273 augroup END
6274 %bw!
6275endfunc
6276
6277" Test for failure to create a new window when selecting a file from the
6278" quickfix window
6279func Test_cwindow_newwin_fails()
6280 cgetexpr ["Xfile1:10:L10", "Xfile1:20:L20"]
6281 cwindow
6282 only
6283 let qf_wid = win_getid()
6284 " create the maximum number of scratch windows
6285 let hor_win_count = (&lines - 1)/2
6286 let hor_split_count = hor_win_count - 1
6287 for s in range(1, hor_split_count) | new | set buftype=nofile | endfor
6288 call win_gotoid(qf_wid)
6289 call assert_fails('exe "normal \<CR>"', 'E36:')
6290 %bw!
6291endfunc
6292
6293" Test for updating the location list when only the location list window is
6294" present and the corresponding file window is closed.
6295func Test_loclist_update_with_llwin_only()
6296 %bw!
6297 new
6298 wincmd w
6299 lexpr ["Xfile1:1:Line1"]
6300 lopen
6301 wincmd p
6302 close
6303 call setloclist(2, [], 'r', {'lines': ["Xtest2:2:Line2"]})
6304 call assert_equal(['Xtest2|2| Line2'], getbufline(winbufnr(2), 1, '$'))
6305 %bw!
6306endfunc
6307
6308" Test for getting the quickfix list after a buffer with an error is wiped out
6309func Test_getqflist_wiped_out_buffer()
6310 %bw!
6311 cexpr ["Xtest1:34:Wiped out"]
6312 let bnum = bufnr('Xtest1')
6313 call assert_equal(bnum, getqflist()[0].bufnr)
6314 bw Xtest1
6315 call assert_equal(0, getqflist()[0].bufnr)
6316 %bw!
6317endfunc
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006318
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00006319" Test for the status message that is displayed when opening a new quickfix
6320" list
6321func Test_qflist_statusmsg()
6322 cexpr "1\n2"
6323 cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"
6324 call assert_equal('(4 of 4): msg', v:statusmsg)
6325 call setqflist([], 'f')
6326 %bw!
6327
6328 " When creating a new quickfix list, if an autocmd changes the quickfix list
6329 " in the stack, then an error message should be displayed.
6330 augroup QF_Test
6331 au!
6332 au BufEnter test_quickfix.vim colder
6333 augroup END
6334 cexpr "1\n2"
6335 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6336 call setqflist([], 'f')
6337 augroup QF_Test
6338 au!
6339 augroup END
6340 %bw!
6341
6342 augroup QF_Test
6343 au!
6344 au BufEnter test_quickfix.vim caddexpr "4"
6345 augroup END
6346 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6347 call setqflist([], 'f')
6348 augroup QF_Test
6349 au!
6350 augroup END
6351 %bw!
6352endfunc
6353
Bram Moolenaard6c67622022-08-24 20:07:22 +01006354func Test_quickfixtextfunc_recursive()
6355 func s:QFTfunc(o)
6356 cgete '0'
6357 endfunc
6358 copen
6359 let &quickfixtextfunc = 's:QFTfunc'
6360 cex ""
6361
6362 let &quickfixtextfunc = ''
6363 cclose
6364endfunc
6365
6366
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006367" vim: shiftwidth=2 sts=2 expandtab