blob: 2a973754deef6142fa926e3da5b556be7936f08c [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 Moolenaarf9ae1542019-11-18 22:02:16 +0100188 " For help entries in the quickfix list, only the filename without directory
189 " should be displayed
190 Xhelpgrep setqflist()
191 let l = split(execute('Xlist 1', ''), "\n")
192 call assert_match('^ 1 [^\\/]\{-}:', l[0])
193
Bram Moolenaaree85df32017-03-19 14:19:50 +0100194 " Error cases
195 call assert_fails('Xlist abc', 'E488:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100196endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100197
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100198func Test_clist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100199 call XlistTests('c')
200 call XlistTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100201endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100202
203" Tests for the :colder, :cnewer, :lolder and :lnewer commands
204" Note that this test assumes that a quickfix/location list is
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100205" already set by the caller.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100206func XageTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200207 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100208
Bram Moolenaar87f59b02019-04-04 14:04:11 +0200209 if a:cchar == 'l'
210 " No location list for the current window
211 call assert_fails('lolder', 'E776:')
212 call assert_fails('lnewer', 'E776:')
213 endif
214
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200215 let list = [{'bufnr': bufnr('%'), 'lnum': 1}]
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200216 call g:Xsetlist(list)
217
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100218 " Jumping to a non existent list should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200219 silent! Xolder 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100220 call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
221
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200222 silent! Xnewer 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100223 call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
224
225 " Add three quickfix/location lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200226 Xgetexpr ['Xtestfile1:1:3:Line1']
227 Xgetexpr ['Xtestfile2:2:2:Line2']
228 Xgetexpr ['Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100229
230 " Go back two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200231 Xolder
232 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100233 call assert_equal('Line2', l[0].text)
234
235 " Go forward two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200236 Xnewer
237 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100238 call assert_equal('Line3', l[0].text)
239
240 " Test for the optional count argument
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200241 Xolder 2
242 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100243 call assert_equal('Line1', l[0].text)
244
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200245 Xnewer 2
246 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100247 call assert_equal('Line3', l[0].text)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100248endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100249
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100250func Test_cage()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100251 call XageTests('c')
252 call XageTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100253endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100254
255" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
256" commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100257func XwindowTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200258 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100259
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200260 " Opening the location list window without any errors should fail
261 if a:cchar == 'l'
262 call assert_fails('lopen', 'E776:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000263 call assert_fails('lwindow', 'E776:')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200264 endif
265
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100266 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200267 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100268
269 " Quickfix/Location window should not open with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200270 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100271 call assert_true(winnr('$') == 1)
272
273 " Create a list with valid entries
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100274 let lines =<< trim END
275 Xtestfile1:1:3:Line1
276 Xtestfile2:2:2:Line2
277 Xtestfile3:3:1:Line3
278 END
279 Xgetexpr lines
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100280
281 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200282 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100283 call assert_true(winnr('$') == 2 && winnr() == 2 &&
284 \ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
Bram Moolenaar537ef082016-07-09 17:56:19 +0200285 redraw!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100286
287 " Close the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200288 Xclose
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100289 call assert_true(winnr('$') == 1)
290
291 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200292 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100293
294 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200295 Xopen 5
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100296 call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
Bram Moolenaarc05d1c02020-09-04 18:38:06 +0200297 \ && winheight(0) == 5)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100298
299 " Opening the window again, should move the cursor to that window
300 wincmd t
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200301 Xopen 7
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100302 call assert_true(winnr('$') == 2 && winnr() == 2 &&
Bram Moolenaarc05d1c02020-09-04 18:38:06 +0200303 \ winheight(0) == 7 &&
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100304 \ getline('.') ==# '|| non-error 1')
305
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100306 " :cnext in quickfix window should move to the next entry
307 Xnext
308 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100309
310 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200311 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100312 call assert_true(winnr('$') == 1)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200313
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100314 " Specifying the width should adjust the width for a vertically split
315 " quickfix window.
316 vert Xopen
317 call assert_equal(10, winwidth(0))
318 vert Xopen 12
319 call assert_equal(12, winwidth(0))
320 Xclose
321
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200322 " Horizontally or vertically splitting the quickfix window should create a
323 " normal window/buffer
324 Xopen
325 wincmd s
326 call assert_equal(0, getwininfo(win_getid())[0].quickfix)
327 call assert_equal(0, getwininfo(win_getid())[0].loclist)
328 call assert_notequal('quickfix', &buftype)
329 close
330 Xopen
331 wincmd v
332 call assert_equal(0, getwininfo(win_getid())[0].quickfix)
333 call assert_equal(0, getwininfo(win_getid())[0].loclist)
334 call assert_notequal('quickfix', &buftype)
335 close
336 Xopen
337 Xclose
338
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200339 if a:cchar == 'c'
340 " Opening the quickfix window in multiple tab pages should reuse the
341 " quickfix buffer
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100342 let lines =<< trim END
343 Xtestfile1:1:3:Line1
344 Xtestfile2:2:2:Line2
345 Xtestfile3:3:1:Line3
346 END
347 Xgetexpr lines
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200348 Xopen
349 let qfbufnum = bufnr('%')
350 tabnew
351 Xopen
352 call assert_equal(qfbufnum, bufnr('%'))
353 new | only | tabonly
354 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100355endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100356
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100357func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100358 call XwindowTests('c')
359 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100360endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100361
Bram Moolenaar36d50222019-05-02 20:17:40 +0200362func Test_copenHeight()
363 copen
364 wincmd H
365 let height = winheight(0)
366 copen 10
367 call assert_equal(height, winheight(0))
368 quit
369endfunc
370
Bram Moolenaar1142a312019-10-16 14:51:39 +0200371func Test_copenHeight_tabline()
372 set tabline=foo showtabline=2
373 copen
374 wincmd H
375 let height = winheight(0)
376 copen 10
377 call assert_equal(height, winheight(0))
378 quit
379 set tabline& showtabline&
380endfunc
381
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100382" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
383" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100384func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200385 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100386
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100387 let lines =<< trim END
388 Xtestfile1:700:10:Line 700
389 Xtestfile2:800:15:Line 800
390 END
391 call writefile(lines, 'Xqftestfile1')
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100392
393 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200394 Xfile Xqftestfile1
395 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100396 call assert_true(len(l) == 2 &&
397 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
398 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
399
Bram Moolenaar049cba92016-06-26 14:38:04 +0200400 " Test with a non existent file
Bram Moolenaare2e40752020-09-04 21:18:46 +0200401 call assert_fails('Xfile non_existent_file', 'E40:')
Bram Moolenaar049cba92016-06-26 14:38:04 +0200402
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100403 " Run cfile/lfile from a modified buffer
404 enew!
405 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200406 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100407 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
408
409 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200410 Xaddfile Xqftestfile1
411 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100412 call assert_true(len(l) == 3 &&
413 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
414
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100415 let lines =<< trim END
416 Xtestfile1:222:77:Line 222
417 Xtestfile2:333:88:Line 333
418 END
419 call writefile(lines, 'Xqftestfile1')
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100420
421 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200422 Xgetfile Xqftestfile1
423 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100424 call assert_true(len(l) == 2 &&
425 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
426 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
427
Bram Moolenaar70077dd2019-11-24 12:12:42 +0100428 " Test for a file with a long line and without a newline at the end
429 let text = repeat('x', 1024)
430 let t = 'a.txt:18:' . text
431 call writefile([t], 'Xqftestfile1', 'b')
432 silent! Xfile Xqftestfile1
433 call assert_equal(text, g:Xgetlist()[0].text)
434
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100435 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100436endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100437
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100438func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100439 call XfileTests('c')
440 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100441endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100442
443" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
444" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100445func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200446 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100447
448 enew!
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100449 let lines =<< trim END
450 Xtestfile7:700:10:Line 700
451 Xtestfile8:800:15:Line 800
452 END
453 silent! call setline(1, lines)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200454 Xbuffer!
455 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100456 call assert_true(len(l) == 2 &&
457 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
458 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
459
460 enew!
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100461 let lines =<< trim END
462 Xtestfile9:900:55:Line 900
463 Xtestfile10:950:66:Line 950
464 END
465 silent! call setline(1, lines)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200466 Xgetbuffer
467 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100468 call assert_true(len(l) == 2 &&
469 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
470 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
471
472 enew!
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100473 let lines =<< trim END
474 Xtestfile11:700:20:Line 700
475 Xtestfile12:750:25:Line 750
476 END
477 silent! call setline(1, lines)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200478 Xaddbuffer
479 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100480 call assert_true(len(l) == 4 &&
481 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
482 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
483 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200484 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100485
Bram Moolenaaree85df32017-03-19 14:19:50 +0100486 " Check for invalid buffer
487 call assert_fails('Xbuffer 199', 'E474:')
488
489 " Check for unloaded buffer
490 edit Xtestfile1
491 let bnr = bufnr('%')
492 enew!
493 call assert_fails('Xbuffer ' . bnr, 'E681:')
494
495 " Check for invalid range
496 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
497 " commands. So directly call the commands.
498 if (a:cchar == 'c')
499 call assert_fails('900,999cbuffer', 'E16:')
500 else
501 call assert_fails('900,999lbuffer', 'E16:')
502 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100503endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100504
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100505func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100506 call XbufferTests('c')
507 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100508endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100509
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100510func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200511 call s:setup_commands(a:cchar)
512
513 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100514endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200515
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100516func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200517 call XexprTests('c')
518 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100519endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200520
521" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100522func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200523 call s:setup_commands(a:cchar)
524
Bram Moolenaar74240d32017-12-10 15:26:15 +0100525 call g:Xsetlist([], 'f')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200526 " Jumping to first or next location list entry without any error should
527 " result in failure
Bram Moolenaar74240d32017-12-10 15:26:15 +0100528 if a:cchar == 'c'
529 let err = 'E42:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100530 let cmd = '$cc'
Bram Moolenaar74240d32017-12-10 15:26:15 +0100531 else
532 let err = 'E776:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100533 let cmd = '$ll'
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200534 endif
Bram Moolenaar74240d32017-12-10 15:26:15 +0100535 call assert_fails('Xnext', err)
536 call assert_fails('Xprev', err)
537 call assert_fails('Xnfile', err)
538 call assert_fails('Xpfile', err)
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100539 call assert_fails(cmd, err)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200540
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100541 Xexpr ''
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100542 call assert_fails(cmd, 'E42:')
543
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200544 call s:create_test_file('Xqftestfile1')
545 call s:create_test_file('Xqftestfile2')
546
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100547 let lines =<< trim END
548 Xqftestfile1:5:Line5
549 Xqftestfile1:6:Line6
550 Xqftestfile2:10:Line10
551 Xqftestfile2:11:Line11
552 RegularLine1
553 RegularLine2
554 END
555 Xgetexpr lines
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200556
557 Xfirst
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100558 call assert_fails('-5Xcc', 'E16:')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200559 call assert_fails('Xprev', 'E553:')
560 call assert_fails('Xpfile', 'E553:')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200561 Xnfile
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200562 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200563 call assert_equal(10, line('.'))
564 Xpfile
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200565 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200566 call assert_equal(6, line('.'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100567 5Xcc
568 call assert_equal(5, g:Xgetlist({'idx':0}).idx)
569 2Xcc
570 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100571 if a:cchar == 'c'
572 cc
573 else
574 ll
575 endif
576 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100577 10Xcc
578 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200579 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200580 Xprev
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200581 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200582 call assert_equal(11, line('.'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200583 call assert_fails('Xnext', 'E553:')
584 call assert_fails('Xnfile', 'E553:')
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100585 " To process the range using quickfix list entries, directly use the
586 " quickfix commands (don't use the user defined commands)
587 if a:cchar == 'c'
588 $cc
589 else
590 $ll
591 endif
592 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200593 Xrewind
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200594 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200595 call assert_equal(5, line('.'))
596
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200597 10Xnext
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200598 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200599 call assert_equal(11, line('.'))
600 10Xprev
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200601 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200602 call assert_equal(5, line('.'))
603
Bram Moolenaar74240d32017-12-10 15:26:15 +0100604 " Jumping to an error from the error window using cc command
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +0100605 let lines =<< trim END
606 Xqftestfile1:5:Line5
607 Xqftestfile1:6:Line6
608 Xqftestfile2:10:Line10
609 Xqftestfile2:11:Line11
610 END
611 Xgetexpr lines
Bram Moolenaar74240d32017-12-10 15:26:15 +0100612 Xopen
613 10Xcc
614 call assert_equal(11, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200615 call assert_equal('Xqftestfile2', @%)
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100616 Xopen
617 call cursor(2, 1)
618 if a:cchar == 'c'
619 .cc
620 else
621 .ll
622 endif
623 call assert_equal(6, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200624 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100625
626 " Jumping to an error from the error window (when only the error window is
627 " present)
628 Xopen | only
629 Xlast 1
630 call assert_equal(5, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200631 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100632
Bram Moolenaaree85df32017-03-19 14:19:50 +0100633 Xexpr ""
634 call assert_fails('Xnext', 'E42:')
635
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200636 call delete('Xqftestfile1')
637 call delete('Xqftestfile2')
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200638
639 " Should be able to use next/prev with invalid entries
640 Xexpr ""
641 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
642 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100643 Xaddexpr ['foo', 'bar', 'baz', 'quux', 'sh|moo']
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200644 call assert_equal(5, g:Xgetlist({'size' : 0}).size)
645 Xlast
646 call assert_equal(5, g:Xgetlist({'idx' : 0}).idx)
647 Xfirst
648 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
649 2Xnext
650 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100651endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200652
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100653func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200654 call Xtest_browse('c')
655 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100656endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200657
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000658" Test for memory allocation failures
659func Xnomem_tests(cchar)
660 call s:setup_commands(a:cchar)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100661
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000662 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
663 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
664
665 call test_alloc_fail(GetAllocId('qf_dirname_now'), 0, 0)
666 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100667
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200668 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000669 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100670
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200671 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000672 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100673
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200674 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000675 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100676
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000677 call test_alloc_fail(GetAllocId('qf_efm_fmtstr'), 0, 0)
678 set efm=%f
679 call assert_fails('Xexpr ["Xfile1"]', 'E342:')
680 set efm&
681
682 call test_alloc_fail(GetAllocId('qf_efm_fmtpart'), 0, 0)
683 set efm=%f:%l:%m,%f-%l-%m
684 call assert_fails('Xaddexpr ["Xfile2", "Xfile3"]', 'E342:')
685 set efm&
686
687 call test_alloc_fail(GetAllocId('qf_title'), 0, 0)
688 call assert_fails('Xexpr ""', 'E342:')
689 call assert_equal('', g:Xgetlist({'all': 1}).title)
690
691 call test_alloc_fail(GetAllocId('qf_mef_name'), 0, 0)
692 set makeef=Xtmp##.err
693 call assert_fails('Xgrep needle haystack', 'E342:')
694 set makeef&
695
696 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
697 call assert_fails('Xexpr "Xfile1:10:Line10"', 'E342:')
698
699 if a:cchar == 'l'
700 for id in ['qf_qfline', 'qf_qfinfo']
701 lgetexpr ["Xfile1:10:L10", "Xfile2:20:L20"]
702 call test_alloc_fail(GetAllocId(id), 0, 0)
703 call assert_fails('new', 'E342:')
704 call assert_equal(2, winnr('$'))
705 call assert_equal([], getloclist(0))
706 %bw!
707 endfor
708 endif
709
710 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
711 try
712 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
713 catch /^Vim:Interrupt$/
714 endtry
715
716 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
717 try
718 call assert_fails('Xvimgrep /vim/f runtest.vim', 'E342:')
719 catch /^Vim:Interrupt$/
720 endtry
721
722 let l = getqflist({"lines": ["Xfile1:10:L10"]})
723 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
724 call assert_fails('call g:Xsetlist(l.items)', 'E342:')
725
726 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
727 try
728 call assert_fails('Xhelpgrep quickfix', 'E342:')
729 catch /^Vim:Interrupt$/
730 endtry
731
732 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
733 call assert_fails('let l = g:Xgetlist({"lines": ["Xfile1:10:L10"]})', 'E342:')
734 call assert_equal(#{items: []}, l)
735
736 if a:cchar == 'l'
737 call setqflist([], 'f')
738 call setloclist(0, [], 'f')
739 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
740 call assert_fails('lhelpgrep quickfix', 'E342:')
741 call assert_equal([], getloclist(0))
742
743 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
744 call assert_fails('lvimgrep vim runtest.vim', 'E342:')
745
746 let l = getqflist({"lines": ["Xfile1:10:L10"]})
747 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
748 call assert_fails('call setloclist(0, l.items)', 'E342:')
749
750 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
751 call assert_fails('lbuffer', 'E342:')
752
753 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
754 call assert_fails('lexpr ["Xfile1:10:L10", "Xfile2:20:L20"]', 'E342:')
755
756 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
757 call assert_fails('lfile runtest.vim', 'E342:')
758 endif
759
760 call test_alloc_fail(GetAllocId('qf_dirstack'), 0, 0)
761 set efm=%DEntering\ dir\ %f,%f:%l:%m
762 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E342:')
763 set efm&
764
765 call test_alloc_fail(GetAllocId('qf_dirstack'), 0, 0)
766 set efm=%+P[%f],(%l)%m
767 call assert_fails('Xexpr ["[runtest.vim]", "(1)Hello"]', 'E342:')
768 set efm&
769
770 call test_alloc_fail(GetAllocId('qf_multiline_pfx'), 0, 0)
771 set efm=%EError,%Cline\ %l,%Z%m
772 call assert_fails('Xexpr ["Error", "line 1", "msg"]', 'E342:')
773 set efm&
774
775 call test_alloc_fail(GetAllocId('qf_makecmd'), 0, 0)
776 call assert_fails('Xgrep vim runtest.vim', 'E342:')
777
778 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
779 call assert_fails('Xexpr repeat("a", 8192)', 'E342:')
780
781 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
782 call assert_fails('Xexpr [repeat("a", 8192)]', 'E342:')
783
784 new
785 call setline(1, repeat('a', 8192))
786 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
787 call assert_fails('Xbuffer', 'E342:')
788 %bw!
789
790 call writefile([repeat('a', 8192)], 'Xtest')
791 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
792 call assert_fails('Xfile Xtest', 'E342:')
793 call delete('Xtest')
794endfunc
795
796func Test_nomem()
797 call Xnomem_tests('c')
798 call Xnomem_tests('l')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100799endfunc
800
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100801func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200802 call s:setup_commands(a:cchar)
803 Xhelpgrep quickfix
804 Xopen
805 if a:cchar == 'c'
806 let title_text = ':helpgrep quickfix'
807 else
808 let title_text = ':lhelpgrep quickfix'
809 endif
810 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200811
812 " Jumping to a help topic should open the help window
813 only
814 Xnext
815 call assert_true(&buftype == 'help')
816 call assert_true(winnr('$') == 2)
817 " Jumping to the next match should reuse the help window
818 Xnext
819 call assert_true(&buftype == 'help')
820 call assert_true(winnr() == 1)
821 call assert_true(winnr('$') == 2)
822 " Jumping to the next match from the quickfix window should reuse the help
823 " window
824 Xopen
825 Xnext
826 call assert_true(&buftype == 'help')
827 call assert_true(winnr() == 1)
828 call assert_true(winnr('$') == 2)
thinca6864efa2021-06-19 20:45:20 +0200829 call assert_match('|\d\+ col \d\+-\d\+|', getbufline(winbufnr(2), 1)[0])
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200830
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100831 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200832 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100833
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100834 " When the current window is vertically split, jumping to a help match
835 " should open the help window at the top.
836 only | enew
837 let w1 = win_getid()
838 vert new
839 let w2 = win_getid()
840 Xnext
841 let w3 = win_getid()
842 call assert_true(&buftype == 'help')
843 call assert_true(winnr() == 1)
Bram Moolenaara106e6c2019-11-24 22:13:58 +0100844 " See jump_to_help_window() for details
845 let w2_width = winwidth(w2)
846 if w2_width != &columns && w2_width < 80
847 call assert_equal(['col', [['leaf', w3],
848 \ ['row', [['leaf', w2], ['leaf', w1]]]]], winlayout())
849 else
850 call assert_equal(['row', [['col', [['leaf', w3], ['leaf', w2]]],
851 \ ['leaf', w1]]] , winlayout())
852 endif
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100853
854 new | only
855 set buftype=help
856 set modified
857 call assert_fails('Xnext', 'E37:')
858 set nomodified
859 new | only
860
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200861 if a:cchar == 'l'
862 " When a help window is present, running :lhelpgrep should reuse the
863 " help window and not the current window
864 new | only
865 call g:Xsetlist([], 'f')
866 help index.txt
867 wincmd w
868 lhelpgrep quickfix
869 call assert_equal(1, winnr())
870 call assert_notequal([], getloclist(1))
871 call assert_equal([], getloclist(2))
872 endif
873
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200874 new | only
875
Bram Moolenaaree85df32017-03-19 14:19:50 +0100876 " Search for non existing help string
877 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar108e7b42018-10-11 17:39:12 +0200878 " Invalid regular expression
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200879 call assert_fails('Xhelpgrep \@<!', 'E866:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100880endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200881
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100882func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200883 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200884 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200885 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100886endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100887
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100888def Test_helpgrep_vim9_restore_cpo()
889 assert_equal('aABceFs', &cpo)
890
891 var rtp_save = &rtp
892 var dir = 'Xruntime/after'
893 &rtp ..= ',' .. dir
894 mkdir(dir .. '/ftplugin', 'p')
Bram Moolenaar39f3b142021-02-14 12:57:36 +0100895 writefile(['vim9script'], dir .. '/ftplugin/qf.vim')
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100896 filetype plugin on
897 silent helpgrep grail
898 cwindow
899 silent helpgrep grail
900
901 assert_equal('aABceFs', &cpo)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000902 delete('Xruntime', 'rf')
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100903 &rtp = rtp_save
904 cclose
905 helpclose
906enddef
907
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000908" When running the :helpgrep command, if an autocmd modifies the 'cpoptions'
909" value, then Vim crashes. (issue fixed by 7.2b-004 and 8.2.4453)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +0000910func Test_helpgrep_restore_cpo_aucmd()
911 let save_cpo = &cpo
912 augroup QF_Test
913 au!
914 autocmd BufNew * set cpo=acd
915 augroup END
916
917 helpgrep quickfix
918 call assert_equal('acd', &cpo)
919 %bw!
920
921 set cpo&vim
922 augroup QF_Test
923 au!
924 autocmd BufReadPost * set cpo=
925 augroup END
926
927 helpgrep buffer
928 call assert_equal('', &cpo)
929
930 augroup QF_Test
931 au!
932 augroup END
933 %bw!
934 let &cpo = save_cpo
935endfunc
936
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +0200937def Test_vim9_cexpr()
938 var text = 'somefile:95:error'
939 cexpr text
940 var l = getqflist()
941 assert_equal(1, l->len())
942 assert_equal(95, l[0].lnum)
943 assert_equal('error', l[0].text)
944
945 text = 'somefile:77:warning'
946 caddexpr text
947 l = getqflist()
948 assert_equal(2, l->len())
949 assert_equal(77, l[1].lnum)
950 assert_equal('warning', l[1].text)
951enddef
952
Bram Moolenaar6920c722016-01-22 22:44:10 +0100953func Test_errortitle()
954 augroup QfBufWinEnter
955 au!
956 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
957 augroup END
958 copen
959 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'')'}]
960 call setqflist(a)
961 call assert_equal(':setqflist()', g:a)
962 augroup QfBufWinEnter
963 au!
964 augroup END
965 augroup! QfBufWinEnter
966endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100967
Bram Moolenaar5584df62016-03-18 21:00:51 +0100968func Test_vimgreptitle()
969 augroup QfBufWinEnter
970 au!
971 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
972 augroup END
973 try
974 vimgrep /pattern/j file
975 catch /E480/
976 endtry
977 copen
978 call assert_equal(': vimgrep /pattern/j file', g:a)
979 augroup QfBufWinEnter
980 au!
981 augroup END
982 augroup! QfBufWinEnter
983endfunc
984
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100985func Test_bufwinenter_once()
986 augroup QfBufWinEnter
987 au!
988 au BufWinEnter * let g:got_afile ..= 'got ' .. expand('<afile>')
989 augroup END
990 let g:got_afile = ''
991 copen
992 call assert_equal('got quickfix', g:got_afile)
993
994 cclose
995 unlet g:got_afile
996 augroup QfBufWinEnter
997 au!
998 augroup END
999 augroup! QfBufWinEnter
1000endfunc
1001
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001002func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001003 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +01001004
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001005 Xgetexpr ['file:1:1:message']
1006 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +01001007 if a:cchar == 'c'
1008 call setqflist(l, 'r')
1009 else
1010 call setloclist(0, l, 'r')
1011 endif
1012
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001013 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +01001014 if a:cchar == 'c'
1015 let title = ':setqflist()'
1016 else
1017 let title = ':setloclist()'
1018 endif
1019 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001020 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001021endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +01001022
1023" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001024func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001025 call XqfTitleTests('c')
1026 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001027endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +01001028
1029" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001030func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +01001031 let save_efm = &efm
1032 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
1033 cgetexpr ['WWWW', 'EEEE', 'CCCC']
1034 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1035 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
1036 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
1037 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1038 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
1039 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
1040 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1041 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
1042 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001043endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001044
1045" This will test for problems in quickfix:
1046" A. incorrectly copying location lists which caused the location list to show
1047" a different name than the file that was actually being displayed.
1048" B. not reusing the window for which the location list window is opened but
1049" instead creating new windows.
1050" C. make sure that the location list window is not reused instead of the
1051" window it belongs to.
1052"
1053" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001054func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001055 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
1056 let word = substitute(base, '\v(.*)\..*', '\1', '')
1057
1058 setl modifiable
1059 setl noreadonly
1060 setl noswapfile
1061 setl bufhidden=delete
1062 %del _
1063 " For problem 2:
1064 " 'buftype' has to be set to reproduce the constant opening of new windows
1065 setl buftype=nofile
1066
1067 call setline(1, word)
1068
1069 setl nomodified
1070 setl nomodifiable
1071 setl readonly
1072 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001073endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001074
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001075func Test_locationlist()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001076 enew
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001077
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001078 augroup testgroup
1079 au!
1080 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
1081 augroup END
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001082
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001083 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001084
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001085 let qflist = []
1086 for word in words
1087 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
1088 " NOTE: problem 1:
1089 " intentionally not setting 'lnum' so that the quickfix entries are not
1090 " valid
1091 eval qflist->setloclist(0, ' ')
1092 endfor
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001093
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001094 " Test A
1095 lrewind
1096 enew
1097 lopen
1098 4lnext
1099 vert split
1100 wincmd L
1101 lopen
1102 wincmd p
1103 lnext
1104 let fileName = expand("%")
1105 wincmd p
1106 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
1107 let fileName = substitute(fileName, '\\', '/', 'g')
1108 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
1109 call assert_equal("test://bar.txt", fileName)
1110 call assert_equal("test://bar.txt", locationListFileName)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001111
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001112 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001113
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001114 " Test B:
1115 lrewind
1116 lopen
1117 2
1118 exe "normal \<CR>"
1119 wincmd p
1120 3
1121 exe "normal \<CR>"
1122 wincmd p
1123 4
1124 exe "normal \<CR>"
1125 call assert_equal(2, winnr('$'))
1126 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001127
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001128 " Test C:
1129 lrewind
1130 lopen
1131 " Let's move the location list window to the top to check whether it (the
1132 " first window found) will be reused when we try to open new windows:
1133 wincmd K
1134 2
1135 exe "normal \<CR>"
1136 wincmd p
1137 3
1138 exe "normal \<CR>"
1139 wincmd p
1140 4
1141 exe "normal \<CR>"
1142 1wincmd w
1143 call assert_equal('quickfix', &buftype)
1144 2wincmd w
1145 let bufferName = expand("%")
1146 let bufferName = substitute(bufferName, '\\', '/', 'g')
1147 call assert_equal('test://quux.txt', bufferName)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001148
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001149 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001150
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001151 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +01001152endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +01001153
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001154func Test_locationlist_curwin_was_closed()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001155 augroup testgroup
1156 au!
1157 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
1158 augroup END
Bram Moolenaar0899d692016-03-19 13:35:03 +01001159
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001160 func! R(n)
1161 quit
1162 endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +01001163
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001164 new
1165 let q = []
1166 call add(q, {'filename': 'test_curwin.txt' })
1167 call setloclist(0, q)
1168 call assert_fails('lrewind', 'E924:')
Bram Moolenaar0899d692016-03-19 13:35:03 +01001169
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001170 augroup! testgroup
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001171 delfunc R
Bram Moolenaaree85df32017-03-19 14:19:50 +01001172endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001173
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001174func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02001175 call writefile(['loclistfoo'], 'loclistfoo')
1176 call writefile(['loclistbar'], 'loclistbar')
1177 set switchbuf=usetab
1178
1179 edit loclistfoo
1180 tabedit loclistbar
1181 silent lgrep loclistfoo loclist*
1182 call assert_equal(1, tabpagenr())
1183
1184 enew | only | tabonly
1185 set switchbuf&vim
1186 call delete('loclistfoo')
1187 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001188endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02001189
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001190" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001191func Test_efm1()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001192 " The 'errorformat' setting is different on non-Unix systems.
1193 " This test works only on Unix-like systems.
1194 CheckUnix
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001195
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001196 let l =<< trim [DATA]
1197 "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.
1198 "Xtestfile", line 6 col 19; this is an error
1199 gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c
1200 Xtestfile:9: parse error before `asd'
1201 make: *** [vim] Error 1
1202 in file "Xtestfile" linenr 10: there is an error
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001203
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001204 2 returned
1205 "Xtestfile", line 11 col 1; this is an error
1206 "Xtestfile", line 12 col 2; this is another error
1207 "Xtestfile", line 14:10; this is an error in column 10
1208 =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time
1209 "Xtestfile", linenr 16: yet another problem
1210 Error in "Xtestfile" at line 17:
1211 x should be a dot
1212 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
1213 ^
1214 Error in "Xtestfile" at line 18:
1215 x should be a dot
1216 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
1217 .............^
1218 Error in "Xtestfile" at line 19:
1219 x should be a dot
1220 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
1221 --------------^
1222 Error in "Xtestfile" at line 20:
1223 x should be a dot
1224 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
1225 ^
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001226
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001227 Does anyone know what is the problem and how to correction it?
1228 "Xtestfile", line 21 col 9: What is the title of the quickfix window?
1229 "Xtestfile", line 22 col 9: What is the title of the quickfix window?
1230 [DATA]
1231
1232 call writefile(l, 'Xerrorfile1')
1233 call writefile(l[:-2], 'Xerrorfile2')
1234
1235 let m =<< [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001236 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2
1237 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3
1238 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4
1239 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5
1240 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6
1241 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7
1242 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8
1243 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9
1244 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10
1245 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11
1246 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12
1247 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13
1248 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14
1249 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15
1250 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16
1251 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
1252 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
1253 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
1254 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
1255 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21
1256 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22
1257[DATA]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001258 call writefile(m, 'Xtestfile')
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001259
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001260 let save_efm = &efm
1261 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
1262 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001263
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001264 exe 'cf Xerrorfile2'
1265 clast
1266 copen
1267 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
1268 wincmd p
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001269
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001270 exe 'cf Xerrorfile1'
1271 call assert_equal([4, 12], [line('.'), col('.')])
1272 cn
1273 call assert_equal([6, 19], [line('.'), col('.')])
1274 cn
1275 call assert_equal([9, 2], [line('.'), col('.')])
1276 cn
1277 call assert_equal([10, 2], [line('.'), col('.')])
1278 cn
1279 call assert_equal([11, 1], [line('.'), col('.')])
1280 cn
1281 call assert_equal([12, 2], [line('.'), col('.')])
1282 cn
1283 call assert_equal([14, 10], [line('.'), col('.')])
1284 cn
1285 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
1286 cn
1287 call assert_equal([16, 2], [line('.'), col('.')])
1288 cn
1289 call assert_equal([17, 6], [line('.'), col('.')])
1290 cn
1291 call assert_equal([18, 7], [line('.'), col('.')])
1292 cn
1293 call assert_equal([19, 8], [line('.'), col('.')])
1294 cn
1295 call assert_equal([20, 9], [line('.'), col('.')])
1296 clast
1297 cprev
1298 cprev
1299 wincmd w
1300 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
1301 wincmd p
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001302
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001303 let &efm = save_efm
1304 call delete('Xerrorfile1')
1305 call delete('Xerrorfile2')
1306 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +01001307endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001308
Bram Moolenaarab47c612016-06-14 22:02:26 +02001309" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001310func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001311 call s:setup_commands(a:cchar)
1312
Bram Moolenaarab47c612016-06-14 22:02:26 +02001313 let save_efm=&efm
1314 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1315
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001316 let lines =<< trim END
1317 Entering dir 'dir1/a'
1318 habits2.txt:1:Nine Healthy Habits
1319 Entering dir 'b'
1320 habits3.txt:2:0 Hours of television
1321 habits2.txt:7:5 Small meals
1322 Entering dir 'dir1/c'
1323 habits4.txt:3:1 Hour of exercise
1324 Leaving dir 'dir1/c'
1325 Leaving dir 'dir1/a'
1326 habits1.txt:4:2 Liters of water
1327 Entering dir 'dir2'
1328 habits5.txt:5:3 Cups of hot green tea
1329 Leaving dir 'dir2'
1330 END
Bram Moolenaarab47c612016-06-14 22:02:26 +02001331
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001332 Xexpr ""
1333 for l in lines
1334 Xaddexpr l
1335 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +02001336
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001337 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001338
1339 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
1340 call assert_equal(1, qf[1].lnum)
1341 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
1342 call assert_equal(2, qf[3].lnum)
1343 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
1344 call assert_equal(7, qf[4].lnum)
1345 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
1346 call assert_equal(3, qf[6].lnum)
1347 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
1348 call assert_equal(4, qf[9].lnum)
1349 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
1350 call assert_equal(5, qf[11].lnum)
1351
1352 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001353endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001354
1355" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001356func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001357 " Create the directory stack and files
1358 call mkdir('dir1')
1359 call mkdir('dir1/a')
1360 call mkdir('dir1/a/b')
1361 call mkdir('dir1/c')
1362 call mkdir('dir2')
1363
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001364 let lines =<< trim END
1365 Nine Healthy Habits
1366 0 Hours of television
1367 1 Hour of exercise
1368 2 Liters of water
1369 3 Cups of hot green tea
1370 4 Short mental breaks
1371 5 Small meals
1372 6 AM wake up time
1373 7 Minutes of laughter
1374 8 Hours of sleep (at least)
1375 9 PM end of the day and off to bed
1376 END
Bram Moolenaarab47c612016-06-14 22:02:26 +02001377 call writefile(lines, 'habits1.txt')
1378 call writefile(lines, 'dir1/a/habits2.txt')
1379 call writefile(lines, 'dir1/a/b/habits3.txt')
1380 call writefile(lines, 'dir1/c/habits4.txt')
1381 call writefile(lines, 'dir2/habits5.txt')
1382
1383 call s:dir_stack_tests('c')
1384 call s:dir_stack_tests('l')
1385
1386 call delete('dir1', 'rf')
1387 call delete('dir2', 'rf')
1388 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001389endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001390
Bram Moolenaar9b457942016-10-09 16:10:05 +02001391" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001392func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001393 call s:setup_commands(a:cchar)
1394
1395 let save_efm = &efm
1396
1397 let &efm =
1398 \ '%Eerror %m %l,' .
1399 \ '%-Wignored %m %l,' .
1400 \ '%+Cmore ignored %m %l,' .
1401 \ '%Zignored end'
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001402 let lines =<< trim END
1403 ignored warning 1
1404 more ignored continuation 2
1405 ignored end
1406 error resync 4
1407 END
1408 Xgetexpr lines
Bram Moolenaar9b457942016-10-09 16:10:05 +02001409 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
1410 call assert_equal([['resync', 1, 4, 'E']], l)
1411
1412 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001413endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001414
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001415func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +02001416 call Xefm_ignore_continuations('c')
1417 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001418endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001419
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001420" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001421func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001422 call s:setup_commands(a:cchar)
1423
Bram Moolenaar049cba92016-06-26 14:38:04 +02001424 let save_efm = &efm
1425
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001426 set efm=%f:%l:%m,%f:%f:%l:%m
1427 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
1428
1429 set efm=%f:%l:%m,%f:%l:%r:%m
1430 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1431
1432 set efm=%f:%l:%m,%O:%f:%l:%m
1433 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1434
1435 set efm=%f:%l:%m,%f:%l:%*[^a-z
1436 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
1437
1438 set efm=%f:%l:%m,%f:%l:%*c
1439 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
1440
1441 set efm=%f:%l:%m,%L%M%N
1442 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
1443
1444 set efm=%f:%l:%m,%f:%l:%m:%R
1445 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
1446
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001447 " Invalid regular expression
1448 set efm=%\\%%k
1449 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E867:')
1450
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001451 set efm=
1452 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
1453
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001454 " Empty directory name. When there is an error in parsing new entries, make
1455 " sure the previous quickfix list is made the current list.
1456 set efm&
1457 cexpr ["one", "two"]
1458 let qf_id = getqflist(#{id: 0}).id
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001459 set efm=%DEntering\ dir\ abc,%f:%l:%m
1460 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001461 call assert_equal(qf_id, getqflist(#{id: 0}).id)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001462
1463 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001464endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001465
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001466func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001467 call Xinvalid_efm_Tests('c')
1468 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001469endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001470
1471" TODO:
1472" Add tests for the following formats in 'errorformat'
1473" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001474func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001475 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001476
1477 " Test for %s format in efm
1478 set efm=%f:%s
1479 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001480 let l = getqflist()
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001481 call assert_equal('^\VLine search text\$', l[0].pattern)
1482 call assert_equal(0, l[0].lnum)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001483
Bram Moolenaaree85df32017-03-19 14:19:50 +01001484 let l = split(execute('clist', ''), "\n")
1485 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1486
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001487 " Test for a long line
1488 cexpr 'Xtestfile:' . repeat('a', 1026)
1489 let l = getqflist()
1490 call assert_equal('^\V' . repeat('a', 1019) . '\$', l[0].pattern)
1491
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001492 " Test for %P, %Q and %t format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001493 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001494 [Xtestfile1]
1495 (1,17) error: ';' missing
1496 (21,2) warning: variable 'z' not defined
1497 (67,3) error: end of file found before string ended
1498 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001499
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001500 [Xtestfile2]
1501 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001502
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001503 [Xtestfile3]
1504 NEW compiler v1.1
1505 (2,2) warning: variable 'x' not defined
1506 (67,3) warning: 's' already defined
1507 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001508 [DATA]
1509
Bram Moolenaaree85df32017-03-19 14:19:50 +01001510 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001511 " To exercise the push/pop file functionality in quickfix, the test files
1512 " need to be created.
1513 call writefile(['Line1'], 'Xtestfile1')
1514 call writefile(['Line2'], 'Xtestfile2')
1515 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001516 cexpr ""
1517 for l in lines
1518 caddexpr l
1519 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001520 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001521 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001522 call assert_equal(21, l[2].lnum)
1523 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001524 call assert_equal('w', l[2].type)
1525 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001526 call delete('Xtestfile1')
1527 call delete('Xtestfile2')
1528 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001529
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001530 " Test for %P, %Q with non-existing files
1531 cexpr lines
1532 let l = getqflist()
1533 call assert_equal(14, len(l))
1534 call assert_equal('[Xtestfile1]', l[0].text)
1535 call assert_equal('[Xtestfile2]', l[6].text)
1536 call assert_equal('[Xtestfile3]', l[9].text)
1537
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001538 " Tests for %E, %C and %Z format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001539 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001540 Error 275
1541 line 42
1542 column 3
1543 ' ' expected after '--'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001544 [DATA]
1545
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001546 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1547 cgetexpr lines
1548 let l = getqflist()
1549 call assert_equal(275, l[0].nr)
1550 call assert_equal(42, l[0].lnum)
1551 call assert_equal(3, l[0].col)
1552 call assert_equal('E', l[0].type)
1553 call assert_equal("\n' ' expected after '--'", l[0].text)
1554
1555 " Test for %>
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001556 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001557 Error in line 147 of foo.c:
1558 unknown variable 'i'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001559 [DATA]
1560
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001561 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1562 cgetexpr lines
1563 let l = getqflist()
1564 call assert_equal(147, l[0].lnum)
1565 call assert_equal('E', l[0].type)
1566 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001567
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001568 " Test for %A, %C and other formats
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001569 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001570 ==============================================================
1571 FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)
1572 --------------------------------------------------------------
1573 Traceback (most recent call last):
1574 File "unittests/dbfacadeTest.py", line 89, in testFoo
1575 self.assertEquals(34, dtid)
1576 File "/usr/lib/python2.2/unittest.py", line 286, in
1577 failUnlessEqual
1578 raise self.failureException, \\
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001579 W:AssertionError: 34 != 33
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001580
1581 --------------------------------------------------------------
1582 Ran 27 tests in 0.063s
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001583 [DATA]
1584
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001585 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%t:%m
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001586 cgetexpr lines
1587 let l = getqflist()
1588 call assert_equal(8, len(l))
1589 call assert_equal(89, l[4].lnum)
1590 call assert_equal(1, l[4].valid)
1591 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001592 call assert_equal('W', l[4].type)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001593
Bram Moolenaard76ce852018-05-01 15:02:04 +02001594 " Test for %o
1595 set efm=%f(%o):%l\ %m
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001596 cgetexpr ['Xotestfile(Language.PureScript.Types):20 Error']
1597 call writefile(['Line1'], 'Xotestfile')
Bram Moolenaard76ce852018-05-01 15:02:04 +02001598 let l = getqflist()
1599 call assert_equal(1, len(l), string(l))
1600 call assert_equal('Language.PureScript.Types', l[0].module)
1601 copen
1602 call assert_equal('Language.PureScript.Types|20| Error', getline(1))
1603 call feedkeys("\<CR>", 'xn')
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001604 call assert_equal('Xotestfile', expand('%:t'))
Bram Moolenaard76ce852018-05-01 15:02:04 +02001605 cclose
1606 bd
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001607 call delete("Xotestfile")
Bram Moolenaard76ce852018-05-01 15:02:04 +02001608
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001609 " Test for a long module name
1610 cexpr 'Xtest(' . repeat('m', 1026) . '):15 message'
1611 let l = getqflist()
1612 call assert_equal(repeat('m', 1024), l[0].module)
1613 call assert_equal(15, l[0].lnum)
1614 call assert_equal('message', l[0].text)
1615
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001616 " The following sequence of commands used to crash Vim
1617 set efm=%W%m
1618 cgetexpr ['msg1']
1619 let l = getqflist()
1620 call assert_equal(1, len(l), string(l))
1621 call assert_equal('msg1', l[0].text)
1622 set efm=%C%m
1623 lexpr 'msg2'
1624 let l = getloclist(0)
1625 call assert_equal(1, len(l), string(l))
1626 call assert_equal('msg2', l[0].text)
1627 lopen
1628 call setqflist([], 'r')
1629 caddbuf
1630 let l = getqflist()
1631 call assert_equal(1, len(l), string(l))
1632 call assert_equal('|| msg2', l[0].text)
1633
Bram Moolenaar78ddc062018-05-15 21:56:34 +02001634 " When matching error lines, case should be ignored. Test for this.
1635 set noignorecase
1636 let l=getqflist({'lines' : ['Xtest:FOO10:Line 20'], 'efm':'%f:foo%l:%m'})
1637 call assert_equal(10, l.items[0].lnum)
1638 call assert_equal('Line 20', l.items[0].text)
1639 set ignorecase&
1640
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001641 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001642 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001643endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001644
Bram Moolenaare9283662020-06-07 14:10:47 +02001645" Test for '%t' (error type) field in 'efm'
1646func Test_efm_error_type()
1647 let save_efm = &efm
1648
1649 " error type
1650 set efm=%f:%l:%t:%m
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001651 let lines =<< trim END
1652 Xfile1:10:E:msg1
1653 Xfile1:20:W:msg2
1654 Xfile1:30:I:msg3
1655 Xfile1:40:N:msg4
1656 Xfile1:50:R:msg5
1657 END
1658 cexpr lines
Bram Moolenaare9283662020-06-07 14:10:47 +02001659 let output = split(execute('clist'), "\n")
1660 call assert_equal([
1661 \ ' 1 Xfile1:10 error: msg1',
1662 \ ' 2 Xfile1:20 warning: msg2',
1663 \ ' 3 Xfile1:30 info: msg3',
1664 \ ' 4 Xfile1:40 note: msg4',
1665 \ ' 5 Xfile1:50 R: msg5'], output)
1666
1667 " error type and a error number
1668 set efm=%f:%l:%t:%n:%m
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001669 let lines =<< trim END
1670 Xfile1:10:E:2:msg1
1671 Xfile1:20:W:4:msg2
1672 Xfile1:30:I:6:msg3
1673 Xfile1:40:N:8:msg4
1674 Xfile1:50:R:3:msg5
1675 END
1676 cexpr lines
Bram Moolenaare9283662020-06-07 14:10:47 +02001677 let output = split(execute('clist'), "\n")
1678 call assert_equal([
1679 \ ' 1 Xfile1:10 error 2: msg1',
1680 \ ' 2 Xfile1:20 warning 4: msg2',
1681 \ ' 3 Xfile1:30 info 6: msg3',
1682 \ ' 4 Xfile1:40 note 8: msg4',
1683 \ ' 5 Xfile1:50 R 3: msg5'], output)
1684 let &efm = save_efm
1685endfunc
1686
haya14busae023d492022-02-08 18:09:29 +00001687" Test for end_lnum ('%e') and end_col ('%k') fields in 'efm'
1688func Test_efm_end_lnum_col()
1689 let save_efm = &efm
1690
1691 " single line
1692 set efm=%f:%l-%e:%c-%k:%t:%m
1693 cexpr ["Xfile1:10-20:1-2:E:msg1", "Xfile1:20-30:2-3:W:msg2",]
1694 let output = split(execute('clist'), "\n")
1695 call assert_equal([
1696 \ ' 1 Xfile1:10-20 col 1-2 error: msg1',
1697 \ ' 2 Xfile1:20-30 col 2-3 warning: msg2'], output)
1698
1699 " multiple lines
1700 set efm=%A%n)%m,%Z%f:%l-%e:%c-%k
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01001701 let lines =<< trim END
1702 1)msg1
1703 Xfile1:14-24:1-2
1704 2)msg2
1705 Xfile1:24-34:3-4
1706 END
1707 cexpr lines
haya14busae023d492022-02-08 18:09:29 +00001708 let output = split(execute('clist'), "\n")
1709 call assert_equal([
1710 \ ' 1 Xfile1:14-24 col 1-2 error 1: msg1',
1711 \ ' 2 Xfile1:24-34 col 3-4 error 2: msg2'], output)
1712 let &efm = save_efm
1713endfunc
1714
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001715func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001716 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001717 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001718 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001719 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001720 colder
1721 cgetexpr []
1722 endfunc
1723 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001724 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001725 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001726 lolder
1727 lgetexpr []
1728 endfunc
1729 endif
1730
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001731 augroup QF_Test
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001732 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001733 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001734 augroup END
1735
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001736 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001737 let words = [ "a", "b" ]
1738 let qflist = []
1739 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001740 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001741 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001742 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001743 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001744
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001745 augroup QF_Test
1746 au!
1747 augroup END
1748
1749 if a:cchar == 'c'
1750 cexpr ["Xtest1:1:Line"]
1751 cwindow
1752 only
1753 augroup QF_Test
1754 au!
1755 autocmd WinEnter * call setqflist([], 'f')
1756 augroup END
1757 call assert_fails('exe "normal \<CR>"', 'E925:')
1758 augroup QF_Test
1759 au!
1760 augroup END
1761 endif
1762 %bw!
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001763endfunc
1764
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001765func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001766 call XquickfixChangedByAutocmd('c')
1767 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001768endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001769
Bram Moolenaar4d170af2020-09-13 22:21:22 +02001770func Test_setloclist_in_autocommand()
1771 call writefile(['test1', 'test2'], 'Xfile')
1772 edit Xfile
1773 let s:bufnr = bufnr()
1774 call setloclist(1,
1775 \ [{'bufnr' : s:bufnr, 'lnum' : 1, 'text' : 'test1'},
1776 \ {'bufnr' : s:bufnr, 'lnum' : 2, 'text' : 'test2'}])
1777
1778 augroup Test_LocList
1779 au!
1780 autocmd BufEnter * call setloclist(1,
1781 \ [{'bufnr' : s:bufnr, 'lnum' : 1, 'text' : 'test1'},
1782 \ {'bufnr' : s:bufnr, 'lnum' : 2, 'text' : 'test2'}], 'r')
1783 augroup END
1784
1785 lopen
1786 call assert_fails('exe "normal j\<CR>"', 'E926:')
1787
1788 augroup Test_LocList
1789 au!
1790 augroup END
1791 call delete('Xfile')
1792endfunc
1793
Bram Moolenaar8b201792016-03-25 15:01:10 +01001794func Test_caddbuffer_to_empty()
1795 helpgr quickfix
1796 call setqflist([], 'r')
1797 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001798 try
1799 cn
1800 catch
1801 " number of matches is unknown
1802 call assert_true(v:exception =~ 'E553:')
1803 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001804 quit!
1805endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001806
1807func Test_cgetexpr_works()
1808 " this must not crash Vim
1809 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001810 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001811endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001812
1813" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001814func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001815 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001816
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001817 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
thinca6864efa2021-06-19 20:45:20 +02001818 \ {'bufnr': a:bnum, 'lnum': 2, 'end_lnum': 3, 'col': 4, 'end_col': 5}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001819 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001820 call assert_equal(2, len(l))
1821 call assert_equal(2, l[1].lnum)
thinca6864efa2021-06-19 20:45:20 +02001822 call assert_equal(3, l[1].end_lnum)
1823 call assert_equal(4, l[1].col)
1824 call assert_equal(5, l[1].end_col)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001825
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001826 Xnext
1827 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1828 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001829 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001830 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001831 call assert_equal(3, line('.'))
1832
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001833 " Appending entries to the list should not change the cursor position
1834 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001835 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001836 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001837 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001838 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1839 call assert_equal(1, line('.'))
1840 close
1841
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001842 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001843 \ {'bufnr': a:bnum, 'lnum': 4},
1844 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001845 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001846 call assert_equal(3, len(l))
1847 call assert_equal(5, l[2].lnum)
1848
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001849 call g:Xsetlist([])
1850 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001851 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001852
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001853 " Tests for setting the 'valid' flag
1854 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1855 Xwindow
1856 call assert_equal(1, winnr('$'))
1857 let l = g:Xgetlist()
1858 call g:Xsetlist(l)
1859 call assert_equal(0, g:Xgetlist()[0].valid)
Bram Moolenaar9752c722018-12-22 16:49:34 +01001860 " Adding a non-valid entry should not mark the list as having valid entries
1861 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':5, 'valid':0}], 'a')
1862 Xwindow
1863 call assert_equal(1, winnr('$'))
1864
1865 " :cnext/:cprev should still work even with invalid entries in the list
1866 let l = [{'bufnr' : a:bnum, 'lnum' : 1, 'text' : '1', 'valid' : 0},
1867 \ {'bufnr' : a:bnum, 'lnum' : 2, 'text' : '2', 'valid' : 0}]
1868 call g:Xsetlist(l)
1869 Xnext
1870 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1871 Xprev
1872 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1873 " :cnext/:cprev should still work after appending invalid entries to an
1874 " empty list
1875 call g:Xsetlist([])
1876 call g:Xsetlist(l, 'a')
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
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001882 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1883 Xwindow
1884 call assert_equal(2, winnr('$'))
1885 Xclose
1886 let save_efm = &efm
1887 set efm=%m
1888 Xgetexpr 'TestMessage'
1889 let l = g:Xgetlist()
1890 call g:Xsetlist(l)
1891 call assert_equal(1, g:Xgetlist()[0].valid)
1892 let &efm = save_efm
1893
Bram Moolenaaree85df32017-03-19 14:19:50 +01001894 " Error cases:
1895 " Refer to a non-existing buffer and pass a non-dictionary type
1896 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1897 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1898 call g:Xsetlist([[1, 2,3]])
1899 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001900 call assert_fails('call g:Xsetlist([], [])', 'E928:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001901 call g:Xsetlist([test_null_dict()])
1902 call assert_equal([], g:Xgetlist())
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001903endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001904
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001905func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001906 new Xtestfile | only
1907 let bnum = bufnr('%')
1908 call setline(1, range(1,5))
1909
1910 call SetXlistTests('c', bnum)
1911 call SetXlistTests('l', bnum)
1912
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001913 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001914 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001915endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001916
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001917func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001918 call s:setup_commands(a:cchar)
1919
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001920 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001921 let @/ = 'Test_'
1922 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001923 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001924 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001925 Xvimgrep empty test_quickfix.vim
1926 call assert_true(len(g:Xgetlist()) > 0)
1927 Xvimgrep matches test_quickfix.vim
1928 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001929 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001930 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001931 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001932 call g:Xsetlist([], 'r')
1933 call assert_true(len(g:Xgetlist()) == 0)
1934 Xolder
1935 call assert_equal(testlen, len(g:Xgetlist()))
1936 Xnewer
1937 Xnewer
1938 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001939endfunc
1940
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001941func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001942 call Xlist_empty_middle('c')
1943 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001944endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001945
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001946func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001947 call s:setup_commands(a:cchar)
1948
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001949 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001950 Xvimgrep one test_quickfix.vim
1951 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001952 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001953 Xvimgrep two test_quickfix.vim
1954 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001955 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001956 Xvimgrep three test_quickfix.vim
1957 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001958 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001959 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001960 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001961 call g:Xsetlist([], 'r')
1962 call assert_true(len(g:Xgetlist()) == 0)
1963 Xnewer
1964 call assert_equal(twolen, len(g:Xgetlist()))
1965 Xnewer
1966 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001967endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001968
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001969func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001970 call Xlist_empty_older('c')
1971 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001972endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001973
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001974func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001975 call s:setup_commands(a:cchar)
1976
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001977 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1978 \ {'filename': 'fnameB', 'text': 'B'}]
1979 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1980 \ {'filename': 'fnameD', 'text': 'D'},
1981 \ {'filename': 'fnameE', 'text': 'E'}]
1982
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001983 " {action} is unspecified. Same as specifying ' '.
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001984 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001985 silent! Xnewer 99
1986 call g:Xsetlist(list1)
1987 call g:Xsetlist(list2)
1988 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001989 call assert_equal(3, len(li))
1990 call assert_equal('C', li[0]['text'])
1991 call assert_equal('D', li[1]['text'])
1992 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001993 silent! Xolder
1994 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001995 call assert_equal(2, len(li))
1996 call assert_equal('A', li[0]['text'])
1997 call assert_equal('B', li[1]['text'])
1998
1999 " {action} is specified ' '.
2000 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002001 silent! Xnewer 99
2002 call g:Xsetlist(list1)
2003 call g:Xsetlist(list2, ' ')
2004 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002005 call assert_equal(3, len(li))
2006 call assert_equal('C', li[0]['text'])
2007 call assert_equal('D', li[1]['text'])
2008 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002009 silent! Xolder
2010 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002011 call assert_equal(2, len(li))
2012 call assert_equal('A', li[0]['text'])
2013 call assert_equal('B', li[1]['text'])
2014
2015 " {action} is specified 'a'.
2016 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002017 silent! Xnewer 99
2018 call g:Xsetlist(list1)
2019 call g:Xsetlist(list2, 'a')
2020 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002021 call assert_equal(5, len(li))
2022 call assert_equal('A', li[0]['text'])
2023 call assert_equal('B', li[1]['text'])
2024 call assert_equal('C', li[2]['text'])
2025 call assert_equal('D', li[3]['text'])
2026 call assert_equal('E', li[4]['text'])
2027
2028 " {action} is specified 'r'.
2029 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002030 silent! Xnewer 99
2031 call g:Xsetlist(list1)
2032 call g:Xsetlist(list2, 'r')
2033 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002034 call assert_equal(3, len(li))
2035 call assert_equal('C', li[0]['text'])
2036 call assert_equal('D', li[1]['text'])
2037 call assert_equal('E', li[2]['text'])
2038
2039 " Test for wrong value.
2040 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002041 call assert_fails("call g:Xsetlist(0)", 'E714:')
2042 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
2043 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
2044 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
2045 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002046endfunc
2047
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01002048func Test_setqflist_invalid_nr()
2049 " The following command used to crash Vim
Bram Moolenaaraad222c2019-09-06 22:46:09 +02002050 eval []->setqflist(' ', {'nr' : $XXX_DOES_NOT_EXIST})
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01002051endfunc
2052
Bram Moolenaar99234f22020-02-10 22:56:54 +01002053func Test_setqflist_user_sets_buftype()
2054 call setqflist([{'text': 'foo'}, {'text': 'bar'}])
2055 set buftype=quickfix
2056 call setqflist([], 'a')
2057 enew
2058endfunc
2059
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002060func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002061 call XquickfixSetListWithAct('c')
2062 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002063endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002064
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002065func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002066 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002067
Bram Moolenaar049cba92016-06-26 14:38:04 +02002068 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002069 call assert_equal(1, l[0].lnum)
2070 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002071 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002072 call assert_equal(2, l[1].lnum)
2073 call assert_equal(1, l[1].col)
2074 call assert_equal(4070, len(l[1].text))
2075 call assert_equal(3, l[2].lnum)
2076 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002077 call assert_equal(4070, len(l[2].text))
2078 call assert_equal(4, l[3].lnum)
2079 call assert_equal(1, l[3].col)
2080 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002081
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002082 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002083endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002084
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002085func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002086 call s:setup_commands(a:cchar)
2087
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002088 let testfile = 'samples/quickfix.txt'
2089
2090 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002091 exe 'Xgetfile' testfile
2092 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002093
2094 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002095 Xexpr readfile(testfile)
2096 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002097
2098 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002099 Xexpr join(readfile(testfile), "\n")
2100 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002101
2102 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002103 exe 'edit' testfile
2104 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02002105 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002106endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002107
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002108func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002109 call s:long_lines_tests('c')
2110 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002111endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002112
Bram Moolenaar59941cb2020-09-05 17:03:40 +02002113func Test_cgetfile_on_long_lines()
2114 " Problematic values if the line is longer than 4096 bytes. Then 1024 bytes
2115 " are read at a time.
2116 for len in [4078, 4079, 4080, 5102, 5103, 5104, 6126, 6127, 6128, 7150, 7151, 7152]
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002117 let lines =<< trim END
2118 /tmp/file1:1:1:aaa
2119 /tmp/file2:1:1:%s
2120 /tmp/file3:1:1:bbb
2121 /tmp/file4:1:1:ccc
2122 END
Bram Moolenaar59941cb2020-09-05 17:03:40 +02002123 let lines[1] = substitute(lines[1], '%s', repeat('x', len), '')
2124 call writefile(lines, 'Xcqetfile.txt')
2125 cgetfile Xcqetfile.txt
2126 call assert_equal(4, getqflist(#{size: v:true}).size, 'with length ' .. len)
2127 endfor
2128 call delete('Xcqetfile.txt')
2129endfunc
2130
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002131func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002132 let l = []
2133 for i in range(1, 20)
2134 call add(l, 'Line' . i)
2135 endfor
2136 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002137endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002138
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002139func Test_switchbuf()
Bram Moolenaar049cba92016-06-26 14:38:04 +02002140 call s:create_test_file('Xqftestfile1')
2141 call s:create_test_file('Xqftestfile2')
2142 call s:create_test_file('Xqftestfile3')
2143
2144 new | only
2145 edit Xqftestfile1
2146 let file1_winid = win_getid()
2147 new Xqftestfile2
2148 let file2_winid = win_getid()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002149 let lines =<< trim END
2150 Xqftestfile1:5:Line5
2151 Xqftestfile1:6:Line6
2152 Xqftestfile2:10:Line10
2153 Xqftestfile2:11:Line11
2154 Xqftestfile3:15:Line15
2155 Xqftestfile3:16:Line16
2156 END
2157 cgetexpr lines
Bram Moolenaar049cba92016-06-26 14:38:04 +02002158
2159 new
2160 let winid = win_getid()
2161 cfirst | cnext
2162 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002163 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002164 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002165 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002166 call assert_equal(winid, win_getid())
Bram Moolenaar049cba92016-06-26 14:38:04 +02002167
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002168 " Test for 'switchbuf' set to search for files in windows in the current
2169 " tabpage and jump to an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002170 set switchbuf=useopen
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002171 enew
Bram Moolenaar049cba92016-06-26 14:38:04 +02002172 cfirst | cnext
2173 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002174 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002175 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002176 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002177 call assert_equal(file2_winid, win_getid())
2178
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002179 " Test for 'switchbuf' set to search for files in tabpages and jump to an
2180 " existing tabpage (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002181 enew | only
2182 set switchbuf=usetab
2183 tabedit Xqftestfile1
2184 tabedit Xqftestfile2
Bram Moolenaar74240d32017-12-10 15:26:15 +01002185 tabedit Xqftestfile3
Bram Moolenaar049cba92016-06-26 14:38:04 +02002186 tabfirst
2187 cfirst | cnext
2188 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002189 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002190 call assert_equal(3, tabpagenr())
Bram Moolenaar74240d32017-12-10 15:26:15 +01002191 6cnext
2192 call assert_equal(4, tabpagenr())
2193 2cpfile
2194 call assert_equal(2, tabpagenr())
2195 2cnfile
2196 call assert_equal(4, tabpagenr())
Bram Moolenaar049cba92016-06-26 14:38:04 +02002197 tabfirst | tabonly | enew
2198
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002199 " Test for 'switchbuf' set to open a new window for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02002200 set switchbuf=split
2201 cfirst | cnext
2202 call assert_equal(1, winnr('$'))
2203 cnext | cnext
2204 call assert_equal(2, winnr('$'))
2205 cnext | cnext
2206 call assert_equal(3, winnr('$'))
Bram Moolenaar049cba92016-06-26 14:38:04 +02002207
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002208 " Test for 'switchbuf' set to open a new tabpage for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02002209 set switchbuf=newtab
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002210 enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02002211 cfirst | cnext
2212 call assert_equal(1, tabpagenr('$'))
2213 cnext | cnext
2214 call assert_equal(2, tabpagenr('$'))
2215 cnext | cnext
2216 call assert_equal(3, tabpagenr('$'))
2217 tabfirst | enew | tabonly | only
2218
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01002219 set switchbuf=uselast
2220 split
2221 let last_winid = win_getid()
2222 copen
2223 exe "normal 1G\<CR>"
2224 call assert_equal(last_winid, win_getid())
2225 enew | only
2226
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002227 " With an empty 'switchbuf', jumping to a quickfix entry should open the
2228 " file in an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002229 set switchbuf=
2230 edit Xqftestfile1
2231 let file1_winid = win_getid()
2232 new Xqftestfile2
2233 let file2_winid = win_getid()
2234 copen
2235 exe "normal 1G\<CR>"
2236 call assert_equal(file1_winid, win_getid())
2237 copen
2238 exe "normal 3G\<CR>"
2239 call assert_equal(file2_winid, win_getid())
2240 copen | only
2241 exe "normal 5G\<CR>"
2242 call assert_equal(2, winnr('$'))
2243 call assert_equal(1, bufwinnr('Xqftestfile3'))
2244
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002245 " If only quickfix window is open in the current tabpage, jumping to an
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002246 " entry with 'switchbuf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02002247 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002248 set switchbuf=usetab
2249 tabedit Xqftestfile1
2250 tabedit Xqftestfile2
2251 tabedit Xqftestfile3
2252 tabfirst
2253 copen | only
2254 clast
2255 call assert_equal(4, tabpagenr())
2256 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02002257
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002258 " Jumping to a file that is not present in any of the tabpages and the
2259 " current tabpage doesn't have any usable windows, should open it in a new
2260 " window in the current tabpage.
2261 copen | only
2262 cfirst
2263 call assert_equal(1, tabpagenr())
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02002264 call assert_equal('Xqftestfile1', @%)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002265
2266 " If opening a file changes 'switchbuf', then the new value should be
2267 " retained.
Bram Moolenaar41d42992020-05-03 16:29:50 +02002268 set modeline&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002269 call writefile(["vim: switchbuf=split"], 'Xqftestfile1')
2270 enew | only
2271 set switchbuf&vim
2272 cexpr "Xqftestfile1:1:10"
2273 call assert_equal('split', &switchbuf)
2274 call writefile(["vim: switchbuf=usetab"], 'Xqftestfile1')
2275 enew | only
2276 set switchbuf=useopen
2277 cexpr "Xqftestfile1:1:10"
2278 call assert_equal('usetab', &switchbuf)
2279 call writefile(["vim: switchbuf&vim"], 'Xqftestfile1')
2280 enew | only
2281 set switchbuf=useopen
2282 cexpr "Xqftestfile1:1:10"
2283 call assert_equal('', &switchbuf)
2284
Bram Moolenaar049cba92016-06-26 14:38:04 +02002285 call delete('Xqftestfile1')
2286 call delete('Xqftestfile2')
2287 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002288 set switchbuf&vim
2289
2290 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002291endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002292
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002293func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002294 call s:setup_commands(a:cchar)
2295
2296 enew | only
2297
Bram Moolenaarc1542742016-07-20 21:44:37 +02002298 let fname = 'Xqftestfile' . a:cchar
2299 call s:create_test_file(fname)
2300 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02002301
Bram Moolenaarc1542742016-07-20 21:44:37 +02002302 Xgetexpr [fname . ':5:Line5',
2303 \ fname . ':10:Line10',
2304 \ fname . ':15:Line15',
2305 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02002306
2307 6,14delete
2308 call append(6, ['Buffer', 'Window'])
2309
2310 let l = g:Xgetlist()
Bram Moolenaar049cba92016-06-26 14:38:04 +02002311 call assert_equal(5, l[0].lnum)
2312 call assert_equal(6, l[2].lnum)
2313 call assert_equal(13, l[3].lnum)
2314
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002315 " If a file doesn't have any quickfix entries, then deleting lines in the
2316 " file should not update the quickfix list
2317 call g:Xsetlist([], 'f')
2318 1,2delete
2319 call assert_equal([], g:Xgetlist())
2320
Bram Moolenaar049cba92016-06-26 14:38:04 +02002321 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02002322 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002323endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002324
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002325func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02002326 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02002327 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02002328 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02002329 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002330endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002331
2332" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002333func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002334 call s:setup_commands(a:cchar)
2335
2336 " The following lines are used for the grep test. Don't remove.
2337 " Grep_Test_Text: Match 1
2338 " Grep_Test_Text: Match 2
2339 " GrepAdd_Test_Text: Match 1
2340 " GrepAdd_Test_Text: Match 2
2341 enew! | only
2342 set makeef&vim
2343 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002344 call assert_true(len(g:Xgetlist()) == 5)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002345 Xopen
2346 call assert_true(w:quickfix_title =~ '^:grep')
2347 Xclose
2348 enew
2349 set makeef=Temp_File_##
2350 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002351 call assert_true(len(g:Xgetlist()) == 9)
2352
2353 " Try with 'grepprg' set to 'internal'
2354 set grepprg=internal
2355 silent Xgrep Grep_Test_Text: test_quickfix.vim
2356 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
2357 call assert_true(len(g:Xgetlist()) == 9)
2358 set grepprg&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002359
2360 call writefile(['Vim'], 'XtestTempFile')
2361 set makeef=XtestTempFile
2362 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002363 call assert_equal(5, len(g:Xgetlist()))
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002364 call assert_false(filereadable('XtestTempFile'))
2365 set makeef&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002366endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002367
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002368func Test_grep()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002369 " The grepprg may not be set on non-Unix systems
2370 CheckUnix
Bram Moolenaar049cba92016-06-26 14:38:04 +02002371
2372 call s:test_xgrep('c')
2373 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002374endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002375
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002376func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002377 " Use one 'errorformat' for two windows. Add an expression to each of them,
2378 " make sure they each keep their own state.
2379 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
2380 call mkdir('Xone/a', 'p')
2381 call mkdir('Xtwo/a', 'p')
2382 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2383 call writefile(lines, 'Xone/a/one.txt')
2384 call writefile(lines, 'Xtwo/a/two.txt')
2385
2386 new one
2387 let one_id = win_getid()
2388 lexpr ""
2389 new two
2390 let two_id = win_getid()
2391 lexpr ""
2392
2393 laddexpr "Entering dir 'Xtwo/a'"
2394 call win_gotoid(one_id)
2395 laddexpr "Entering dir 'Xone/a'"
2396 call win_gotoid(two_id)
2397 laddexpr 'two.txt:5:two two two'
2398 call win_gotoid(one_id)
2399 laddexpr 'one.txt:3:one one one'
2400
2401 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002402 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
2403 call assert_equal(3, loc_one[1].lnum)
2404
2405 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002406 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
2407 call assert_equal(5, loc_two[1].lnum)
2408
2409 call win_gotoid(one_id)
2410 bwipe!
2411 call win_gotoid(two_id)
2412 bwipe!
2413 call delete('Xone', 'rf')
2414 call delete('Xtwo', 'rf')
2415endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02002416
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002417func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02002418 call s:setup_commands(a:cchar)
2419
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002420 " Calling lbottom without any errors should fail
2421 if a:cchar == 'l'
2422 call assert_fails('lbottom', 'E776:')
2423 endif
2424
Bram Moolenaar875feea2017-06-11 16:07:51 +02002425 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02002426 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02002427 let wid = win_getid()
2428 call assert_equal(1, line('.'))
2429 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02002430 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02002431 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02002432 call win_gotoid(wid)
2433 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02002434 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02002435endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02002436
2437" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002438func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02002439 call XbottomTests('c')
2440 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002441endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002442
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002443func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002444 call s:setup_commands(a:cchar)
2445
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002446 " clear all lists after the first one, then replace the first one.
2447 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002448 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002449 let entry = {'filename': 'foo', 'lnum': 42}
2450 call g:Xsetlist([entry], 'r')
2451 call g:Xsetlist([entry, entry])
2452 call g:Xsetlist([entry, entry, entry])
2453 let res = split(execute(a:cchar . 'hist'), "\n")
2454 call assert_equal(3, len(res))
2455 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
2456 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
2457 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
2458 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002459
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002460 " Test for changing the quickfix lists
2461 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2462 exe '1' . a:cchar . 'hist'
2463 call assert_equal(1, g:Xgetlist({'nr' : 0}).nr)
2464 exe '3' . a:cchar . 'hist'
2465 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2466 call assert_fails('-2' . a:cchar . 'hist', 'E16:')
2467 call assert_fails('4' . a:cchar . 'hist', 'E16:')
2468
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002469 call g:Xsetlist([], 'f')
2470 let l = split(execute(a:cchar . 'hist'), "\n")
2471 call assert_equal('No entries', l[0])
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002472 if a:cchar == 'c'
2473 call assert_fails('4chist', 'E16:')
2474 else
2475 call assert_fails('4lhist', 'E776:')
2476 endif
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002477
2478 " An empty list should still show the stack history
2479 call g:Xsetlist([])
2480 let res = split(execute(a:cchar . 'hist'), "\n")
2481 call assert_equal('> error list 1 of 1; 0 ' . common, res[0])
2482
2483 call g:Xsetlist([], 'f')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002484endfunc
2485
2486func Test_history()
2487 call HistoryTest('c')
2488 call HistoryTest('l')
2489endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02002490
2491func Test_duplicate_buf()
2492 " make sure we can get the highest buffer number
2493 edit DoesNotExist
2494 edit DoesNotExist2
2495 let last_buffer = bufnr("$")
2496
2497 " make sure only one buffer is created
2498 call writefile(['this one', 'that one'], 'Xgrepthis')
2499 vimgrep one Xgrepthis
2500 vimgrep one Xgrepthis
2501 call assert_equal(last_buffer + 1, bufnr("$"))
2502
2503 call delete('Xgrepthis')
2504endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002505
2506" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002507func Xproperty_tests(cchar)
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002508 call s:setup_commands(a:cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002509
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002510 " Error cases
2511 call assert_fails('call g:Xgetlist(99)', 'E715:')
2512 call assert_fails('call g:Xsetlist(99)', 'E714:')
2513 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
Bram Moolenaard823fa92016-08-12 16:29:27 +02002514
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002515 " Set and get the title
2516 call g:Xsetlist([])
2517 Xopen
2518 wincmd p
2519 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
2520 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
2521 call assert_equal(0, s)
2522 let d = g:Xgetlist({"title":1})
2523 call assert_equal('Sample', d.title)
2524 " Try setting title to a non-string value
2525 call assert_equal(-1, g:Xsetlist([], 'a', {'title' : ['Test']}))
2526 call assert_equal('Sample', g:Xgetlist({"title":1}).title)
2527
2528 Xopen
2529 call assert_equal('Sample', w:quickfix_title)
2530 Xclose
2531
2532 " Tests for action argument
2533 silent! Xolder 999
2534 let qfnr = g:Xgetlist({'all':1}).nr
2535 call g:Xsetlist([], 'r', {'title' : 'N1'})
2536 call assert_equal('N1', g:Xgetlist({'all':1}).title)
2537 call g:Xsetlist([], ' ', {'title' : 'N2'})
2538 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
2539
2540 let res = g:Xgetlist({'nr': 0})
2541 call assert_equal(qfnr + 1, res.nr)
2542 call assert_equal(['nr'], keys(res))
2543
2544 call g:Xsetlist([], ' ', {'title' : 'N3'})
2545 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
2546
2547 " Changing the title of an earlier quickfix list
2548 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
2549 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
2550
2551 " Changing the title of an invalid quickfix list
2552 call assert_equal(-1, g:Xsetlist([], ' ',
2553 \ {'title' : 'SomeTitle', 'nr' : 99}))
2554 call assert_equal(-1, g:Xsetlist([], ' ',
2555 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
2556
2557 if a:cchar == 'c'
2558 copen
2559 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
2560 cclose
2561 endif
2562
2563 " Invalid arguments
Bram Moolenaare2e40752020-09-04 21:18:46 +02002564 call assert_fails('call g:Xgetlist([])', 'E715:')
2565 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002566 let s = g:Xsetlist([], 'a', {'abc':1})
2567 call assert_equal(-1, s)
2568
2569 call assert_equal({}, g:Xgetlist({'abc':1}))
2570 call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
2571 call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
2572
2573 if a:cchar == 'l'
2574 call assert_equal({}, getloclist(99, {'title': 1}))
2575 endif
2576
2577 " Context related tests
2578 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
2579 call assert_equal(0, s)
2580 call test_garbagecollect_now()
2581 let d = g:Xgetlist({'context':1})
2582 call assert_equal([1,2,3], d.context)
2583 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
2584 let d = g:Xgetlist({'context':1})
2585 call assert_equal({'color':'green'}, d.context)
2586 call g:Xsetlist([], 'a', {'context':"Context info"})
2587 let d = g:Xgetlist({'context':1})
2588 call assert_equal("Context info", d.context)
2589 call g:Xsetlist([], 'a', {'context':246})
2590 let d = g:Xgetlist({'context':1})
2591 call assert_equal(246, d.context)
2592 " set other Vim data types as context
2593 call g:Xsetlist([], 'a', {'context' : test_null_blob()})
2594 if has('channel')
2595 call g:Xsetlist([], 'a', {'context' : test_null_channel()})
2596 endif
2597 if has('job')
2598 call g:Xsetlist([], 'a', {'context' : test_null_job()})
2599 endif
2600 call g:Xsetlist([], 'a', {'context' : test_null_function()})
2601 call g:Xsetlist([], 'a', {'context' : test_null_partial()})
2602 call g:Xsetlist([], 'a', {'context' : ''})
2603 call test_garbagecollect_now()
2604 if a:cchar == 'l'
2605 " Test for copying context across two different location lists
2606 new | only
2607 let w1_id = win_getid()
2608 let l = [1]
2609 call setloclist(0, [], 'a', {'context':l})
2610 new
2611 let w2_id = win_getid()
2612 call add(l, 2)
2613 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
2614 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2615 unlet! l
2616 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2617 only
2618 call setloclist(0, [], 'f')
2619 call assert_equal('', getloclist(0, {'context':1}).context)
2620 endif
2621
2622 " Test for changing the context of previous quickfix lists
2623 call g:Xsetlist([], 'f')
2624 Xexpr "One"
2625 Xexpr "Two"
2626 Xexpr "Three"
2627 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
2628 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
2629 " Also, check for setting the context using quickfix list number zero.
2630 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
2631 call test_garbagecollect_now()
2632 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
2633 call assert_equal([1], l.context)
2634 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
2635 call assert_equal([2], l.context)
2636 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
2637 call assert_equal([3], l.context)
2638
2639 " Test for changing the context through reference and for garbage
2640 " collection of quickfix context
2641 let l = ["red"]
2642 call g:Xsetlist([], ' ', {'context' : l})
2643 call add(l, "blue")
2644 let x = g:Xgetlist({'context' : 1})
2645 call add(x.context, "green")
2646 call assert_equal(["red", "blue", "green"], l)
2647 call assert_equal(["red", "blue", "green"], x.context)
2648 unlet l
2649 call test_garbagecollect_now()
2650 let m = g:Xgetlist({'context' : 1})
2651 call assert_equal(["red", "blue", "green"], m.context)
2652
2653 " Test for setting/getting items
2654 Xexpr ""
2655 let qfprev = g:Xgetlist({'nr':0})
2656 let s = g:Xsetlist([], ' ', {'title':'Green',
2657 \ 'items' : [{'filename':'F1', 'lnum':10}]})
2658 call assert_equal(0, s)
2659 let qfcur = g:Xgetlist({'nr':0})
2660 call assert_true(qfcur.nr == qfprev.nr + 1)
2661 let l = g:Xgetlist({'items':1})
2662 call assert_equal('F1', bufname(l.items[0].bufnr))
2663 call assert_equal(10, l.items[0].lnum)
2664 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
2665 \ {'filename':'F2', 'lnum':30}]})
2666 let l = g:Xgetlist({'items':1})
2667 call assert_equal('F2', bufname(l.items[2].bufnr))
2668 call assert_equal(30, l.items[2].lnum)
2669 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
2670 let l = g:Xgetlist({'items':1})
2671 call assert_equal('F3', bufname(l.items[0].bufnr))
2672 call assert_equal(40, l.items[0].lnum)
2673 call g:Xsetlist([], 'r', {'items' : []})
2674 let l = g:Xgetlist({'items':1})
2675 call assert_equal(0, len(l.items))
2676
2677 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
2678 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2679 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2680 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
2681
2682 " Test for getting id of window associated with a location list window
2683 if a:cchar == 'l'
2684 only
2685 call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid)
2686 let wid = win_getid()
Bram Moolenaard823fa92016-08-12 16:29:27 +02002687 Xopen
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002688 call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid)
2689 wincmd w
2690 call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid)
2691 only
2692 endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02002693
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002694 " The following used to crash Vim with address sanitizer
2695 call g:Xsetlist([], 'f')
2696 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
2697 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002698
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002699 " Try setting the items using a string
2700 call assert_equal(-1, g:Xsetlist([], ' ', {'items' : 'Test'}))
Bram Moolenaar890680c2016-09-27 21:28:56 +02002701
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002702 " Save and restore the quickfix stack
2703 call g:Xsetlist([], 'f')
2704 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2705 Xexpr "File1:10:Line1"
2706 Xexpr "File2:20:Line2"
2707 Xexpr "File3:30:Line3"
2708 let last_qf = g:Xgetlist({'nr':'$'}).nr
2709 call assert_equal(3, last_qf)
2710 let qstack = []
2711 for i in range(1, last_qf)
2712 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
2713 endfor
2714 call g:Xsetlist([], 'f')
2715 for i in range(len(qstack))
2716 call g:Xsetlist([], ' ', qstack[i])
2717 endfor
2718 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2719 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
2720 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
2721 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
2722 call g:Xsetlist([], 'f')
Bram Moolenaar890680c2016-09-27 21:28:56 +02002723
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002724 " Swap two quickfix lists
2725 Xexpr "File1:10:Line10"
2726 Xexpr "File2:20:Line20"
2727 Xexpr "File3:30:Line30"
2728 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
2729 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
2730 let l1=g:Xgetlist({'nr':1,'all':1})
2731 let l2=g:Xgetlist({'nr':2,'all':1})
2732 let save_id = l1.id
2733 let l1.id=l2.id
2734 let l2.id=save_id
2735 call g:Xsetlist([], 'r', l1)
2736 call g:Xsetlist([], 'r', l2)
2737 let newl1=g:Xgetlist({'nr':1,'all':1})
2738 let newl2=g:Xgetlist({'nr':2,'all':1})
2739 call assert_equal('Fruits', newl1.title)
2740 call assert_equal(['Fruits'], newl1.context)
2741 call assert_equal('Line20', newl1.items[0].text)
2742 call assert_equal('Colors', newl2.title)
2743 call assert_equal(['Colors'], newl2.context)
2744 call assert_equal('Line10', newl2.items[0].text)
2745 call g:Xsetlist([], 'f')
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02002746
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002747 " Cannot specify both a non-empty list argument and a dict argument
2748 call assert_fails("call g:Xsetlist([{}], ' ', {})", 'E475:')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002749endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002750
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002751func Test_qf_property()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002752 call Xproperty_tests('c')
2753 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002754endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002755
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002756" Test for setting the current index in the location/quickfix list
2757func Xtest_setqfidx(cchar)
2758 call s:setup_commands(a:cchar)
2759
2760 Xgetexpr "F1:10:1:Line1\nF2:20:2:Line2\nF3:30:3:Line3"
2761 Xgetexpr "F4:10:1:Line1\nF5:20:2:Line2\nF6:30:3:Line3"
2762 Xgetexpr "F7:10:1:Line1\nF8:20:2:Line2\nF9:30:3:Line3"
2763
2764 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 2})
2765 call g:Xsetlist([], 'a', {'nr' : 2, 'idx' : 2})
2766 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 3})
2767 Xolder 2
2768 Xopen
2769 call assert_equal(3, line('.'))
2770 Xnewer
2771 call assert_equal(2, line('.'))
2772 Xnewer
2773 call assert_equal(2, line('.'))
2774 " Update the current index with the quickfix window open
2775 wincmd w
2776 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 3})
2777 Xopen
2778 call assert_equal(3, line('.'))
2779 Xclose
2780
2781 " Set the current index to the last entry
2782 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : '$'})
2783 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2784 " A large value should set the index to the last index
2785 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 1})
2786 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 999})
2787 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2788 " Invalid index values
2789 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : -1})
2790 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2791 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 0})
2792 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2793 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 'xx'})
2794 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2795 call assert_fails("call g:Xsetlist([], 'a', {'nr':1, 'idx':[]})", 'E745:')
2796
2797 call g:Xsetlist([], 'f')
2798 new | only
2799endfunc
2800
2801func Test_setqfidx()
2802 call Xtest_setqfidx('c')
2803 call Xtest_setqfidx('l')
2804endfunc
2805
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002806" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002807func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002808 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002809endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002810
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002811func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002812 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
2813 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
2814
2815 let g:acmds = []
2816 cexpr "F1:10:Line 10"
2817 caddexpr "F1:20:Line 20"
2818 cgetexpr "F1:30:Line 30"
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002819 cexpr ""
2820 caddexpr ""
2821 cgetexpr ""
2822 silent! cexpr non_existing_func()
2823 silent! caddexpr non_existing_func()
2824 silent! cgetexpr non_existing_func()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002825 let l =<< trim END
2826 precexpr
2827 postcexpr
2828 precaddexpr
2829 postcaddexpr
2830 precgetexpr
2831 postcgetexpr
2832 precexpr
2833 postcexpr
2834 precaddexpr
2835 postcaddexpr
2836 precgetexpr
2837 postcgetexpr
2838 precexpr
2839 precaddexpr
2840 precgetexpr
2841 END
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002842 call assert_equal(l, g:acmds)
2843
2844 let g:acmds = []
2845 enew! | call append(0, "F2:10:Line 10")
2846 cbuffer!
2847 enew! | call append(0, "F2:20:Line 20")
2848 cgetbuffer
2849 enew! | call append(0, "F2:30:Line 30")
2850 caddbuffer
2851 new
2852 let bnum = bufnr('%')
2853 bunload
2854 exe 'silent! cbuffer! ' . bnum
2855 exe 'silent! cgetbuffer ' . bnum
2856 exe 'silent! caddbuffer ' . bnum
2857 enew!
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002858 let l =<< trim END
2859 precbuffer
2860 postcbuffer
2861 precgetbuffer
2862 postcgetbuffer
2863 precaddbuffer
2864 postcaddbuffer
2865 precbuffer
2866 precgetbuffer
2867 precaddbuffer
2868 END
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002869 call assert_equal(l, g:acmds)
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002870
2871 call writefile(['Xtest:1:Line1'], 'Xtest')
2872 call writefile([], 'Xempty')
2873 let g:acmds = []
2874 cfile Xtest
2875 caddfile Xtest
2876 cgetfile Xtest
2877 cfile Xempty
2878 caddfile Xempty
2879 cgetfile Xempty
2880 silent! cfile do_not_exist
2881 silent! caddfile do_not_exist
2882 silent! cgetfile do_not_exist
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002883 let l =<< trim END
2884 precfile
2885 postcfile
2886 precaddfile
2887 postcaddfile
2888 precgetfile
2889 postcgetfile
2890 precfile
2891 postcfile
2892 precaddfile
2893 postcaddfile
2894 precgetfile
2895 postcgetfile
2896 precfile
2897 postcfile
2898 precaddfile
2899 postcaddfile
2900 precgetfile
2901 postcgetfile
2902 END
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002903 call assert_equal(l, g:acmds)
2904
2905 let g:acmds = []
2906 helpgrep quickfix
2907 silent! helpgrep non_existing_help_topic
2908 vimgrep test Xtest
2909 vimgrepadd test Xtest
2910 silent! vimgrep non_existing_test Xtest
2911 silent! vimgrepadd non_existing_test Xtest
2912 set makeprg=
2913 silent! make
2914 set makeprg&
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002915 let l =<< trim END
2916 prehelpgrep
2917 posthelpgrep
2918 prehelpgrep
2919 posthelpgrep
2920 previmgrep
2921 postvimgrep
2922 previmgrepadd
2923 postvimgrepadd
2924 previmgrep
2925 postvimgrep
2926 previmgrepadd
2927 postvimgrepadd
2928 premake
2929 postmake
2930 END
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002931 call assert_equal(l, g:acmds)
2932
2933 if has('unix')
2934 " Run this test only on Unix-like systems. The grepprg may not be set on
2935 " non-Unix systems.
2936 " The following lines are used for the grep test. Don't remove.
2937 " Grep_Autocmd_Text: Match 1
2938 " GrepAdd_Autocmd_Text: Match 2
2939 let g:acmds = []
2940 silent grep Grep_Autocmd_Text test_quickfix.vim
2941 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2942 silent grep abc123def Xtest
2943 silent grepadd abc123def Xtest
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002944 set grepprg=internal
2945 silent grep Grep_Autocmd_Text test_quickfix.vim
2946 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2947 silent lgrep Grep_Autocmd_Text test_quickfix.vim
2948 silent lgrepadd GrepAdd_Autocmd_Text test_quickfix.vim
2949 set grepprg&vim
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01002950 let l =<< trim END
2951 pregrep
2952 postgrep
2953 pregrepadd
2954 postgrepadd
2955 pregrep
2956 postgrep
2957 pregrepadd
2958 postgrepadd
2959 pregrep
2960 postgrep
2961 pregrepadd
2962 postgrepadd
2963 prelgrep
2964 postlgrep
2965 prelgrepadd
2966 postlgrepadd
2967 END
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002968 call assert_equal(l, g:acmds)
2969 endif
2970
2971 call delete('Xtest')
2972 call delete('Xempty')
Bram Moolenaarb254af32017-12-18 19:48:58 +01002973 au! QuickFixCmdPre
2974 au! QuickFixCmdPost
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002975endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01002976
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002977func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01002978 set efm=%m
2979 lgetexpr '?'
2980
2981 try
2982 call DoesNotExit()
2983 catch
2984 lgetexpr '1'
2985 finally
2986 lgetexpr '1'
2987 endtry
2988
2989 call assert_equal('1', getloclist(0)[0].text)
2990
2991 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002992endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002993
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002994func Test_caddbuffer_wrong()
2995 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002996 let save_efm = &efm
2997 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
2998 cgetexpr ['WWWW', 'EEEE', 'CCCC']
2999 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01003000 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01003001 bwipe!
3002endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01003003
3004func Test_caddexpr_wrong()
3005 " This used to cause a memory access in freed memory.
3006 cbuffer
3007 cbuffer
3008 copen
3009 let save_efm = &efm
3010 set efm=%
3011 call assert_fails('caddexpr ""', 'E376:')
3012 let &efm = save_efm
3013endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01003014
3015func Test_dirstack_cleanup()
3016 " This used to cause a memory access in freed memory.
3017 let save_efm = &efm
3018 lexpr '0'
3019 lopen
3020 fun X(c)
3021 let save_efm=&efm
3022 set efm=%D%f
3023 if a:c == 'c'
3024 caddexpr '::'
3025 else
3026 laddexpr ':0:0'
3027 endif
3028 let &efm=save_efm
3029 endfun
3030 call X('c')
3031 call X('l')
3032 call setqflist([], 'r')
3033 caddbuffer
3034 let &efm = save_efm
3035endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01003036
3037" Tests for jumping to entries from the location list window and quickfix
3038" window
3039func Test_cwindow_jump()
3040 set efm=%f%%%l%%%m
3041 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3042 lopen | only
3043 lfirst
3044 call assert_true(winnr('$') == 2)
3045 call assert_true(winnr() == 1)
3046 " Location list for the new window should be set
3047 call assert_true(getloclist(0)[2].text == 'Line 30')
3048
3049 " Open a scratch buffer
3050 " Open a new window and create a location list
3051 " Open the location list window and close the other window
3052 " Jump to an entry.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003053 " Should create a new window and jump to the entry. The scratch buffer
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01003054 " should not be used.
3055 enew | only
3056 set buftype=nofile
3057 below new
3058 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3059 lopen
3060 2wincmd c
3061 lnext
3062 call assert_true(winnr('$') == 3)
3063 call assert_true(winnr() == 2)
3064
3065 " Open two windows with two different location lists
3066 " Open the location list window and close the previous window
3067 " Jump to an entry in the location list window
3068 " Should open the file in the first window and not set the location list.
3069 enew | only
3070 lgetexpr ["F1%5%Line 5"]
3071 below new
3072 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3073 lopen
3074 2wincmd c
3075 lnext
3076 call assert_true(winnr() == 1)
3077 call assert_true(getloclist(0)[0].text == 'Line 5')
3078
3079 enew | only
3080 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3081 copen
3082 cnext
3083 call assert_true(winnr('$') == 2)
3084 call assert_true(winnr() == 1)
3085
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003086 " open the quickfix buffer in two windows and jump to an entry. Should open
3087 " the file in the first quickfix window.
3088 enew | only
3089 copen
3090 let bnum = bufnr('')
3091 exe 'sbuffer ' . bnum
3092 wincmd b
3093 cfirst
3094 call assert_equal(2, winnr())
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003095 call assert_equal('F1', @%)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003096 enew | only
3097 exe 'sb' bnum
3098 exe 'botright sb' bnum
3099 wincmd t
3100 clast
3101 call assert_equal(2, winnr())
3102 call assert_equal('quickfix', getwinvar(1, '&buftype'))
3103 call assert_equal('quickfix', getwinvar(3, '&buftype'))
3104
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003105 " Jumping to a file from the location list window should find a usable
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01003106 " window by wrapping around the window list.
3107 enew | only
3108 call setloclist(0, [], 'f')
3109 new | new
3110 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3111 lopen
3112 1close
3113 call assert_equal(0, getloclist(3, {'id' : 0}).id)
3114 lnext
3115 call assert_equal(3, winnr())
3116 call assert_equal(getloclist(1, {'id' : 0}).id, getloclist(3, {'id' : 0}).id)
3117
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01003118 enew | only
3119 set efm&vim
3120endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01003121
Bram Moolenaare00fdf32019-09-15 19:09:42 +02003122func Test_cwindow_highlight()
3123 CheckScreendump
3124
3125 let lines =<< trim END
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003126 call setline(1, ['some', 'text', 'with', 'matches'])
3127 write XCwindow
3128 vimgrep e XCwindow
3129 redraw
3130 cwindow 4
Bram Moolenaare00fdf32019-09-15 19:09:42 +02003131 END
3132 call writefile(lines, 'XtestCwindow')
3133 let buf = RunVimInTerminal('-S XtestCwindow', #{rows: 12})
3134 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_1', {})
3135
3136 call term_sendkeys(buf, ":cnext\<CR>")
3137 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_2', {})
3138
3139 " clean up
3140 call StopVimInTerminal(buf)
3141 call delete('XtestCwindow')
3142 call delete('XCwindow')
3143endfunc
3144
Bram Moolenaaree85df32017-03-19 14:19:50 +01003145func XvimgrepTests(cchar)
3146 call s:setup_commands(a:cchar)
3147
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003148 let lines =<< trim END
3149 Editor:VIM vim
3150 Editor:Emacs EmAcS
3151 Editor:Notepad NOTEPAD
3152 END
3153 call writefile(lines, 'Xtestfile1')
Dominique Pelle923dce22021-11-21 11:36:04 +00003154 call writefile(['Linux', 'macOS', 'MS-Windows'], 'Xtestfile2')
Bram Moolenaaree85df32017-03-19 14:19:50 +01003155
3156 " Error cases
3157 call assert_fails('Xvimgrep /abc *', 'E682:')
3158
3159 let @/=''
3160 call assert_fails('Xvimgrep // *', 'E35:')
3161
3162 call assert_fails('Xvimgrep abc', 'E683:')
3163 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
3164 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
3165
3166 Xexpr ""
3167 Xvimgrepadd Notepad Xtestfile1
Dominique Pelle923dce22021-11-21 11:36:04 +00003168 Xvimgrepadd macOS Xtestfile2
Bram Moolenaaree85df32017-03-19 14:19:50 +01003169 let l = g:Xgetlist()
3170 call assert_equal(2, len(l))
3171 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
3172
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003173 10Xvimgrep #\cvim#g Xtestfile?
Bram Moolenaaree85df32017-03-19 14:19:50 +01003174 let l = g:Xgetlist()
3175 call assert_equal(2, len(l))
3176 call assert_equal(8, l[0].col)
thinca6864efa2021-06-19 20:45:20 +02003177 call assert_equal(11, l[0].end_col)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003178 call assert_equal(12, l[1].col)
thinca6864efa2021-06-19 20:45:20 +02003179 call assert_equal(15, l[1].end_col)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003180
3181 1Xvimgrep ?Editor? Xtestfile*
3182 let l = g:Xgetlist()
3183 call assert_equal(1, len(l))
3184 call assert_equal('Editor:VIM vim', l[0].text)
3185
3186 edit +3 Xtestfile2
3187 Xvimgrep +\cemacs+j Xtestfile1
3188 let l = g:Xgetlist()
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003189 call assert_equal('Xtestfile2', @%)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003190 call assert_equal('Editor:Emacs EmAcS', l[0].text)
3191
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02003192 " Test for unloading a buffer after vimgrep searched the buffer
3193 %bwipe
3194 Xvimgrep /Editor/j Xtestfile*
3195 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
3196 call assert_equal([], getbufinfo('Xtestfile2'))
3197
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00003198 " Test for opening the dummy buffer used by vimgrep in a window. The new
3199 " window should be closed
3200 %bw!
3201 augroup QF_Test
3202 au!
3203 autocmd BufReadPre * exe "sb " .. expand("<abuf>")
3204 augroup END
3205 call assert_fails("Xvimgrep /sublime/ Xtestfile1", 'E480:')
3206 call assert_equal(1, winnr('$'))
3207 augroup QF_Test
3208 au!
3209 augroup END
3210
Bram Moolenaaree85df32017-03-19 14:19:50 +01003211 call delete('Xtestfile1')
3212 call delete('Xtestfile2')
3213endfunc
3214
3215" Tests for the :vimgrep command
3216func Test_vimgrep()
3217 call XvimgrepTests('c')
3218 call XvimgrepTests('l')
3219endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003220
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01003221func Test_vimgrep_wildcards_expanded_once()
3222 new X[id-01] file.txt
3223 call setline(1, 'some text to search for')
3224 vimgrep text %
3225 bwipe!
3226endfunc
3227
Bram Moolenaar1c299432018-10-28 14:36:09 +01003228" Test for incsearch highlighting of the :vimgrep pattern
3229" This test used to cause "E315: ml_get: invalid lnum" errors.
3230func Test_vimgrep_incsearch()
3231 enew
3232 set incsearch
3233 call test_override("char_avail", 1)
3234
3235 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
3236 let l = getqflist()
3237 call assert_equal(2, len(l))
3238
3239 call test_override("ALL", 0)
3240 set noincsearch
3241endfunc
3242
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003243" Test vimgrep with the last search pattern not set
3244func Test_vimgrep_with_no_last_search_pat()
3245 let lines =<< trim [SCRIPT]
3246 call assert_fails('vimgrep // *', 'E35:')
3247 call writefile(v:errors, 'Xresult')
3248 qall!
3249 [SCRIPT]
3250 call writefile(lines, 'Xscript')
3251 if RunVim([], [], '--clean -S Xscript')
3252 call assert_equal([], readfile('Xresult'))
3253 endif
3254 call delete('Xscript')
3255 call delete('Xresult')
3256endfunc
3257
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02003258" Test vimgrep without swap file
3259func Test_vimgrep_without_swap_file()
3260 let lines =<< trim [SCRIPT]
3261 vimgrep grep test_c*
3262 call writefile(['done'], 'Xresult')
3263 qall!
3264 [SCRIPT]
3265 call writefile(lines, 'Xscript')
3266 if RunVim([], [], '--clean -n -S Xscript Xscript')
3267 call assert_equal(['done'], readfile('Xresult'))
3268 endif
3269 call delete('Xscript')
3270 call delete('Xresult')
3271endfunc
3272
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02003273func Test_vimgrep_existing_swapfile()
3274 call writefile(['match apple with apple'], 'Xapple')
3275 call writefile(['swapfile'], '.Xapple.swp')
3276 let g:foundSwap = 0
3277 let g:ignoreSwapExists = 1
3278 augroup grep
3279 au SwapExists * let foundSwap = 1 | let v:swapchoice = 'e'
3280 augroup END
3281 vimgrep apple Xapple
3282 call assert_equal(1, g:foundSwap)
3283 call assert_match('.Xapple.swo', swapname(''))
3284
3285 call delete('Xapple')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003286 call delete('.Xapple.swp')
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02003287 augroup grep
3288 au! SwapExists
3289 augroup END
3290 unlet g:ignoreSwapExists
3291endfunc
3292
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003293func XfreeTests(cchar)
3294 call s:setup_commands(a:cchar)
3295
3296 enew | only
3297
3298 " Deleting the quickfix stack should work even When the current list is
3299 " somewhere in the middle of the stack
3300 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3301 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3302 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3303 Xolder
3304 call g:Xsetlist([], 'f')
3305 call assert_equal(0, len(g:Xgetlist()))
3306
3307 " After deleting the stack, adding a new list should create a stack with a
3308 " single list.
3309 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3310 call assert_equal(1, g:Xgetlist({'all':1}).nr)
3311
3312 " Deleting the stack from a quickfix window should update/clear the
3313 " quickfix/location list window.
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 Xwindow
3319 call g:Xsetlist([], 'f')
3320 call assert_equal(2, winnr('$'))
3321 call assert_equal(1, line('$'))
3322 Xclose
3323
3324 " Deleting the stack from a non-quickfix window should update/clear the
3325 " quickfix/location list window.
3326 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3327 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3328 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3329 Xolder
3330 Xwindow
3331 wincmd p
3332 call g:Xsetlist([], 'f')
3333 call assert_equal(0, len(g:Xgetlist()))
3334 wincmd p
3335 call assert_equal(2, winnr('$'))
3336 call assert_equal(1, line('$'))
3337
3338 " After deleting the location list stack, if the location list window is
3339 " opened, then a new location list should be created. So opening the
3340 " location list window again should not create a new window.
3341 if a:cchar == 'l'
3342 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3343 wincmd p
3344 lopen
3345 call assert_equal(2, winnr('$'))
3346 endif
3347 Xclose
3348endfunc
3349
Bram Moolenaar74240d32017-12-10 15:26:15 +01003350" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003351func Test_qf_free()
3352 call XfreeTests('c')
3353 call XfreeTests('l')
3354endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02003355
3356" Test for buffer overflow when parsing lines and adding new entries to
3357" the quickfix list.
3358func Test_bufoverflow()
3359 set efm=%f:%l:%m
3360 cgetexpr ['File1:100:' . repeat('x', 1025)]
3361
3362 set efm=%+GCompiler:\ %.%#,%f:%l:%m
3363 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
3364
3365 set efm=%DEntering\ directory\ %f,%f:%l:%m
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01003366 let lines =<< trim eval END
3367 Entering directory $"{repeat('a', 1006)}"
3368 File1:10:Hello World
3369 END
3370 cgetexpr lines
Bram Moolenaar6e62da32017-05-28 08:16:25 +02003371 set efm&vim
3372endfunc
3373
Bram Moolenaar875feea2017-06-11 16:07:51 +02003374" Tests for getting the quickfix stack size
3375func XsizeTests(cchar)
3376 call s:setup_commands(a:cchar)
3377
3378 call g:Xsetlist([], 'f')
3379 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003380 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
3381 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02003382
3383 Xexpr "File1:10:Line1"
3384 Xexpr "File2:20:Line2"
3385 Xexpr "File3:30:Line3"
3386 Xolder | Xolder
3387 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
3388 call g:Xsetlist([], 'f')
3389
3390 Xexpr "File1:10:Line1"
3391 Xexpr "File2:20:Line2"
3392 Xexpr "File3:30:Line3"
3393 Xolder | Xolder
3394 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
3395 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
3396endfunc
3397
3398func Test_Qf_Size()
3399 call XsizeTests('c')
3400 call XsizeTests('l')
3401endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02003402
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003403func Test_cclose_from_copen()
3404 augroup QF_Test
3405 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003406 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003407 augroup END
3408 copen
3409 augroup QF_Test
3410 au!
3411 augroup END
3412 augroup! QF_Test
3413endfunc
3414
Bram Moolenaar18141832017-06-25 21:17:25 +02003415func Test_cclose_in_autocmd()
3416 " Problem is only triggered if "starting" is zero, so that the OptionsSet
3417 " event will be triggered.
3418 call test_override('starting', 1)
3419 augroup QF_Test
3420 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003421 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar18141832017-06-25 21:17:25 +02003422 augroup END
3423 copen
3424 augroup QF_Test
3425 au!
3426 augroup END
3427 augroup! QF_Test
3428 call test_override('starting', 0)
3429endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003430
Bram Moolenaar379fb762018-08-30 15:58:28 +02003431" Check that ":file" without an argument is possible even when "curbuf_lock"
3432" is set.
3433func Test_file_from_copen()
3434 " Works without argument.
3435 augroup QF_Test
3436 au!
3437 au FileType qf file
3438 augroup END
3439 copen
3440
3441 augroup QF_Test
3442 au!
3443 augroup END
3444 cclose
3445
3446 " Fails with argument.
3447 augroup QF_Test
3448 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003449 au FileType qf call assert_fails(':file foo', 'E788:')
Bram Moolenaar379fb762018-08-30 15:58:28 +02003450 augroup END
3451 copen
3452 augroup QF_Test
3453 au!
3454 augroup END
3455 cclose
3456
3457 augroup! QF_Test
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02003458endfunc
Bram Moolenaar379fb762018-08-30 15:58:28 +02003459
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003460func Test_resize_from_copen()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003461 augroup QF_Test
3462 au!
3463 au FileType qf resize 5
3464 augroup END
3465 try
3466 " This should succeed without any exception. No other buffers are
3467 " involved in the autocmd.
3468 copen
3469 finally
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003470 augroup QF_Test
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003471 au!
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003472 augroup END
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003473 augroup! QF_Test
3474 endtry
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003475endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02003476
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003477func Test_vimgrep_with_textlock()
3478 new
3479
zeertzjqcfe45652022-05-27 17:26:55 +01003480 " Simple way to execute something with "textlock" set.
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003481 " Check that vimgrep without jumping can be executed.
3482 au InsertCharPre * vimgrep /RunTheTest/j runtest.vim
3483 normal ax
3484 let qflist = getqflist()
3485 call assert_true(len(qflist) > 0)
3486 call assert_match('RunTheTest', qflist[0].text)
3487 call setqflist([], 'r')
3488 au! InsertCharPre
3489
3490 " Check that vimgrepadd without jumping can be executed.
3491 au InsertCharPre * vimgrepadd /RunTheTest/j runtest.vim
3492 normal ax
3493 let qflist = getqflist()
3494 call assert_true(len(qflist) > 0)
3495 call assert_match('RunTheTest', qflist[0].text)
3496 call setqflist([], 'r')
3497 au! InsertCharPre
3498
3499 " Check that lvimgrep without jumping can be executed.
3500 au InsertCharPre * lvimgrep /RunTheTest/j runtest.vim
3501 normal ax
3502 let qflist = getloclist(0)
3503 call assert_true(len(qflist) > 0)
3504 call assert_match('RunTheTest', qflist[0].text)
3505 call setloclist(0, [], 'r')
3506 au! InsertCharPre
3507
3508 " Check that lvimgrepadd without jumping can be executed.
3509 au InsertCharPre * lvimgrepadd /RunTheTest/j runtest.vim
3510 normal ax
3511 let qflist = getloclist(0)
3512 call assert_true(len(qflist) > 0)
3513 call assert_match('RunTheTest', qflist[0].text)
3514 call setloclist(0, [], 'r')
3515 au! InsertCharPre
3516
3517 " trying to jump will give an error
3518 au InsertCharPre * vimgrep /RunTheTest/ runtest.vim
3519 call assert_fails('normal ax', 'E565:')
3520 au! InsertCharPre
3521
3522 au InsertCharPre * vimgrepadd /RunTheTest/ runtest.vim
3523 call assert_fails('normal ax', 'E565:')
3524 au! InsertCharPre
3525
3526 au InsertCharPre * lvimgrep /RunTheTest/ runtest.vim
3527 call assert_fails('normal ax', 'E565:')
3528 au! InsertCharPre
3529
3530 au InsertCharPre * lvimgrepadd /RunTheTest/ runtest.vim
3531 call assert_fails('normal ax', 'E565:')
3532 au! InsertCharPre
3533
3534 bwipe!
3535endfunc
3536
Bram Moolenaara8788f42017-07-19 17:06:20 +02003537" Tests for the quickfix buffer b:changedtick variable
3538func Xchangedtick_tests(cchar)
3539 call s:setup_commands(a:cchar)
3540
3541 new | only
3542
3543 Xexpr "" | Xexpr "" | Xexpr ""
3544
3545 Xopen
3546 Xolder
3547 Xolder
3548 Xaddexpr "F1:10:Line10"
3549 Xaddexpr "F2:20:Line20"
3550 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
3551 call g:Xsetlist([], 'f')
3552 call assert_equal(8, getbufvar('%', 'changedtick'))
3553 Xclose
3554endfunc
3555
3556func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02003557 call Xchangedtick_tests('c')
3558 call Xchangedtick_tests('l')
3559endfunc
3560
3561" Tests for parsing an expression using setqflist()
3562func Xsetexpr_tests(cchar)
3563 call s:setup_commands(a:cchar)
3564
3565 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003566 call g:Xsetlist([], ' ', {'lines' : t})
3567 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003568
3569 let l = g:Xgetlist()
3570 call assert_equal(3, len(l))
3571 call assert_equal(20, l[1].lnum)
3572 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003573 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003574 let l = g:Xgetlist()
3575 call assert_equal(1, len(l))
3576 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003577 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
3578 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003579
3580 call g:Xsetlist([], 'f')
3581 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003582 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
3583 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
3584 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
3585 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003586 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
3587 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02003588
3589 " Adding entries using a custom efm
3590 set efm&
3591 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
3592 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
3593 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3594 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
3595 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
3596 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3597 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
3598 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003599endfunc
3600
3601func Test_setexpr()
3602 call Xsetexpr_tests('c')
3603 call Xsetexpr_tests('l')
3604endfunc
3605
3606" Tests for per quickfix/location list directory stack
3607func Xmultidirstack_tests(cchar)
3608 call s:setup_commands(a:cchar)
3609
3610 call g:Xsetlist([], 'f')
3611 Xexpr "" | Xexpr ""
3612
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003613 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
3614 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
3615 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
3616 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003617
3618 let l1 = g:Xgetlist({'nr':1, 'items':1})
3619 let l2 = g:Xgetlist({'nr':2, 'items':1})
3620 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
3621 call assert_equal(3, l1.items[1].lnum)
3622 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
3623 call assert_equal(5, l2.items[1].lnum)
3624endfunc
3625
3626func Test_multidirstack()
3627 call mkdir('Xone/a', 'p')
3628 call mkdir('Xtwo/a', 'p')
3629 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3630 call writefile(lines, 'Xone/a/one.txt')
3631 call writefile(lines, 'Xtwo/a/two.txt')
3632 let save_efm = &efm
3633 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
3634
3635 call Xmultidirstack_tests('c')
3636 call Xmultidirstack_tests('l')
3637
3638 let &efm = save_efm
3639 call delete('Xone', 'rf')
3640 call delete('Xtwo', 'rf')
3641endfunc
3642
3643" Tests for per quickfix/location list file stack
3644func Xmultifilestack_tests(cchar)
3645 call s:setup_commands(a:cchar)
3646
3647 call g:Xsetlist([], 'f')
3648 Xexpr "" | Xexpr ""
3649
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003650 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
3651 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
3652 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
3653 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003654
3655 let l1 = g:Xgetlist({'nr':1, 'items':1})
3656 let l2 = g:Xgetlist({'nr':2, 'items':1})
3657 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
3658 call assert_equal(3, l1.items[1].lnum)
3659 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
3660 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02003661
3662 " Test for start of a new error line in the same line where a previous
3663 " error line ends with a file stack.
3664 let efm_val = 'Error\ l%l\ in\ %f,'
3665 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003666 let lines =<< trim END
3667 (one.txt
3668 Error l4 in one.txt
3669 ) (two.txt
3670 Error l6 in two.txt
3671 )
3672 Error l8 in one.txt
3673 END
3674 let l = g:Xgetlist({'lines': lines, 'efm' : efm_val})
Bram Moolenaare333e792018-04-08 13:27:39 +02003675 call assert_equal(3, len(l.items))
3676 call assert_equal('one.txt', bufname(l.items[0].bufnr))
3677 call assert_equal(4, l.items[0].lnum)
3678 call assert_equal('one.txt', l.items[0].text)
3679 call assert_equal('two.txt', bufname(l.items[1].bufnr))
3680 call assert_equal(6, l.items[1].lnum)
3681 call assert_equal('two.txt', l.items[1].text)
3682 call assert_equal('one.txt', bufname(l.items[2].bufnr))
3683 call assert_equal(8, l.items[2].lnum)
3684 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02003685endfunc
3686
3687func Test_multifilestack()
3688 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3689 call writefile(lines, 'one.txt')
3690 call writefile(lines, 'two.txt')
3691 let save_efm = &efm
3692 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
3693
3694 call Xmultifilestack_tests('c')
3695 call Xmultifilestack_tests('l')
3696
3697 let &efm = save_efm
3698 call delete('one.txt')
3699 call delete('two.txt')
3700endfunc
3701
3702" Tests for per buffer 'efm' setting
3703func Test_perbuf_efm()
3704 call writefile(["File1-10-Line10"], 'one.txt')
3705 call writefile(["File2#20#Line20"], 'two.txt')
3706 set efm=%f#%l#%m
3707 new | only
3708 new
3709 setlocal efm=%f-%l-%m
3710 cfile one.txt
3711 wincmd w
3712 caddfile two.txt
3713
3714 let l = getqflist()
3715 call assert_equal(10, l[0].lnum)
3716 call assert_equal('Line20', l[1].text)
3717
3718 set efm&
3719 new | only
3720 call delete('one.txt')
3721 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02003722endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003723
3724" Open multiple help windows using ":lhelpgrep
3725" This test used to crash Vim
3726func Test_Multi_LL_Help()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003727 new | only
3728 lhelpgrep window
3729 lopen
3730 e#
3731 lhelpgrep buffer
3732 call assert_equal(3, winnr('$'))
3733 call assert_true(len(getloclist(1)) != 0)
3734 call assert_true(len(getloclist(2)) != 0)
3735 new | only
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003736endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02003737
3738" Tests for adding new quickfix lists using setqflist()
3739func XaddQf_tests(cchar)
3740 call s:setup_commands(a:cchar)
3741
3742 " Create a new list using ' ' for action
3743 call g:Xsetlist([], 'f')
3744 call g:Xsetlist([], ' ', {'title' : 'Test1'})
3745 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3746 call assert_equal(1, l.nr)
3747 call assert_equal('Test1', l.title)
3748
3749 " Create a new list using ' ' for action and '$' for 'nr'
3750 call g:Xsetlist([], 'f')
3751 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
3752 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3753 call assert_equal(1, l.nr)
3754 call assert_equal('Test2', l.title)
3755
3756 " Create a new list using 'a' for action
3757 call g:Xsetlist([], 'f')
3758 call g:Xsetlist([], 'a', {'title' : 'Test3'})
3759 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3760 call assert_equal(1, l.nr)
3761 call assert_equal('Test3', l.title)
3762
3763 " Create a new list using 'a' for action and '$' for 'nr'
3764 call g:Xsetlist([], 'f')
3765 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
3766 call g:Xsetlist([], 'a', {'title' : 'Test4'})
3767 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3768 call assert_equal(1, l.nr)
3769 call assert_equal('Test4', l.title)
3770
3771 " Adding a quickfix list should remove all the lists following the current
3772 " list.
3773 Xexpr "" | Xexpr "" | Xexpr ""
3774 silent! 10Xolder
3775 call g:Xsetlist([], ' ', {'title' : 'Test5'})
3776 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3777 call assert_equal(2, l.nr)
3778 call assert_equal('Test5', l.title)
3779
3780 " Add a quickfix list using '$' as the list number.
3781 let lastqf = g:Xgetlist({'nr':'$'}).nr
3782 silent! 99Xolder
3783 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
3784 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3785 call assert_equal(lastqf + 1, l.nr)
3786 call assert_equal('Test6', l.title)
3787
3788 " Add a quickfix list using 'nr' set to one more than the quickfix
3789 " list size.
3790 let lastqf = g:Xgetlist({'nr':'$'}).nr
3791 silent! 99Xolder
3792 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
3793 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3794 call assert_equal(lastqf + 1, l.nr)
3795 call assert_equal('Test7', l.title)
3796
3797 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
3798 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
3799 silent! 99Xolder
3800 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
3801 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3802 call assert_equal(10, l.nr)
3803 call assert_equal('Test8', l.title)
3804
3805 " Add a quickfix list using 'nr' set to a value greater than 10
3806 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
3807
3808 " Try adding a quickfix list with 'nr' set to a value greater than the
3809 " quickfix list size but less than 10.
3810 call g:Xsetlist([], 'f')
3811 Xexpr "" | Xexpr "" | Xexpr ""
3812 silent! 99Xolder
3813 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
3814
3815 " Add a quickfix list using 'nr' set to a some string or list
3816 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
3817endfunc
3818
3819func Test_add_qf()
3820 call XaddQf_tests('c')
3821 call XaddQf_tests('l')
3822endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003823
3824" Test for getting the quickfix list items from some text without modifying
3825" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003826func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003827 call s:setup_commands(a:cchar)
3828 call g:Xsetlist([], 'f')
3829
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003830 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003831 call assert_equal(2, len(l))
3832 call assert_equal(30, l[1].lnum)
3833
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003834 call assert_equal({}, g:Xgetlist({'lines' : 10}))
3835 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
3836 call assert_equal([], g:Xgetlist({'lines' : []}).items)
3837 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003838
Bram Moolenaar36538222017-09-02 19:51:44 +02003839 " Parse text using a custom efm
3840 set efm&
3841 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
3842 call assert_equal('Line30', l[0].text)
3843 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
3844 call assert_equal('File3:30:Line30', l[0].text)
3845 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
3846 call assert_equal({}, l)
3847 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
3848 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
3849
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003850 " Make sure that the quickfix stack is not modified
3851 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
3852endfunc
3853
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003854func Test_get_list_from_lines()
3855 call XgetListFromLines('c')
3856 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003857endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003858
3859" Tests for the quickfix list id
3860func Xqfid_tests(cchar)
3861 call s:setup_commands(a:cchar)
3862
3863 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003864 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003865 Xexpr ''
3866 let start_id = g:Xgetlist({'id' : 0}).id
3867 Xexpr '' | Xexpr ''
3868 Xolder
3869 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
3870 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
3871 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003872 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003873 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003874 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3875 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003876
3877 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3878 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003879 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003880 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3881 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3882 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3883
3884 let qfid = g:Xgetlist({'id':0, 'nr':0})
3885 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003886 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003887endfunc
3888
3889func Test_qf_id()
3890 call Xqfid_tests('c')
3891 call Xqfid_tests('l')
3892endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003893
3894func Xqfjump_tests(cchar)
3895 call s:setup_commands(a:cchar)
3896
3897 call writefile(["Line1\tFoo", "Line2"], 'F1')
3898 call writefile(["Line1\tBar", "Line2"], 'F2')
3899 call writefile(["Line1\tBaz", "Line2"], 'F3')
3900
3901 call g:Xsetlist([], 'f')
3902
3903 " Tests for
3904 " Jumping to a line using a pattern
3905 " Jumping to a column greater than the last column in a line
3906 " Jumping to a line greater than the last line in the file
3907 let l = []
3908 for i in range(1, 7)
3909 call add(l, {})
3910 endfor
3911 let l[0].filename='F1'
3912 let l[0].pattern='Line1'
3913 let l[1].filename='F2'
3914 let l[1].pattern='Line1'
3915 let l[2].filename='F3'
3916 let l[2].pattern='Line1'
3917 let l[3].filename='F3'
3918 let l[3].lnum=1
3919 let l[3].col=9
3920 let l[3].vcol=1
3921 let l[4].filename='F3'
3922 let l[4].lnum=99
3923 let l[5].filename='F3'
3924 let l[5].lnum=1
3925 let l[5].col=99
3926 let l[5].vcol=1
3927 let l[6].filename='F3'
3928 let l[6].pattern='abcxyz'
3929
3930 call g:Xsetlist([], ' ', {'items' : l})
3931 Xopen | only
3932 2Xnext
3933 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003934 call assert_equal('F3', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +01003935 Xnext
3936 call assert_equal(7, col('.'))
3937 Xnext
3938 call assert_equal(2, line('.'))
3939 Xnext
3940 call assert_equal(9, col('.'))
3941 2
3942 Xnext
3943 call assert_equal(2, line('.'))
3944
3945 if a:cchar == 'l'
3946 " When jumping to a location list entry in the location list window and
3947 " no usable windows are available, then a new window should be opened.
3948 enew! | new | only
3949 call g:Xsetlist([], 'f')
3950 setlocal buftype=nofile
3951 new
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003952 let lines =<< trim END
3953 F1:1:1:Line1
3954 F1:2:2:Line2
3955 F2:1:1:Line1
3956 F2:2:2:Line2
3957 F3:1:1:Line1
3958 F3:2:2:Line2
3959 END
3960 call g:Xsetlist([], ' ', {'lines': lines})
Bram Moolenaar74240d32017-12-10 15:26:15 +01003961 Xopen
3962 let winid = win_getid()
3963 wincmd p
3964 close
3965 call win_gotoid(winid)
3966 Xnext
3967 call assert_equal(3, winnr('$'))
3968 call assert_equal(1, winnr())
3969 call assert_equal(2, line('.'))
3970
3971 " When jumping to an entry in the location list window and the window
3972 " associated with the location list is not present and a window containing
3973 " the file is already present, then that window should be used.
3974 close
3975 belowright new
3976 call g:Xsetlist([], 'f')
3977 edit F3
3978 call win_gotoid(winid)
3979 Xlast
3980 call assert_equal(3, winnr())
3981 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3982 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3983 endif
3984
3985 " Cleanup
3986 enew!
3987 new | only
3988
3989 call delete('F1')
3990 call delete('F2')
3991 call delete('F3')
3992endfunc
3993
3994func Test_qfjump()
3995 call Xqfjump_tests('c')
3996 call Xqfjump_tests('l')
3997endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01003998
3999" Tests for the getqflist() and getloclist() functions when the list is not
4000" present or is empty
4001func Xgetlist_empty_tests(cchar)
4002 call s:setup_commands(a:cchar)
4003
4004 " Empty quickfix stack
4005 call g:Xsetlist([], 'f')
4006 call assert_equal('', g:Xgetlist({'context' : 0}).context)
4007 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
4008 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
4009 call assert_equal([], g:Xgetlist({'items' : 0}).items)
4010 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
4011 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
4012 call assert_equal('', g:Xgetlist({'title' : 0}).title)
4013 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004014 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004015 if a:cchar == 'c'
4016 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004017 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004018 \ 'title' : '', 'winid' : 0, 'changedtick': 0,
4019 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004020 else
4021 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
4022 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004023 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004024 \ 'qfbufnr' : 0, 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004025 \ g:Xgetlist({'all' : 0}))
4026 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004027
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004028 " Quickfix window with empty stack
4029 silent! Xopen
4030 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004031 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004032 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
4033 Xclose
4034
Bram Moolenaara6d48492017-12-12 22:45:31 +01004035 " Empty quickfix list
4036 Xexpr ""
4037 call assert_equal('', g:Xgetlist({'context' : 0}).context)
4038 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
4039 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
4040 call assert_equal([], g:Xgetlist({'items' : 0}).items)
4041 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
4042 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
4043 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
4044 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004045 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01004046
4047 let qfid = g:Xgetlist({'id' : 0}).id
4048 call g:Xsetlist([], 'f')
4049
4050 " Non-existing quickfix identifier
4051 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
4052 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
4053 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
4054 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
4055 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
4056 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
4057 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
4058 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004059 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004060 if a:cchar == 'c'
4061 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4062 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004063 \ 'qfbufnr' : qfbufnr, 'quickfixtextfunc' : '',
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004064 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
4065 else
4066 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4067 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004068 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4069 \ 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004070 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
4071 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004072
4073 " Non-existing quickfix list number
4074 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
4075 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
4076 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
4077 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
4078 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
4079 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
4080 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
4081 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004082 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004083 if a:cchar == 'c'
4084 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4085 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004086 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr,
4087 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004088 else
4089 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4090 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004091 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4092 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004093 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004094endfunc
4095
4096func Test_getqflist()
4097 call Xgetlist_empty_tests('c')
4098 call Xgetlist_empty_tests('l')
4099endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01004100
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01004101func Test_getqflist_invalid_nr()
4102 " The following commands used to crash Vim
4103 cexpr ""
4104 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
4105
4106 " Cleanup
4107 call setqflist([], 'r')
4108endfunc
4109
Bram Moolenaarb254af32017-12-18 19:48:58 +01004110" Tests for the quickfix/location list changedtick
4111func Xqftick_tests(cchar)
4112 call s:setup_commands(a:cchar)
4113
4114 call g:Xsetlist([], 'f')
4115
4116 Xexpr "F1:10:Line10"
4117 let qfid = g:Xgetlist({'id' : 0}).id
4118 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4119 Xaddexpr "F2:20:Line20\nF2:21:Line21"
4120 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4121 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
4122 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
4123 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
4124 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
4125 call g:Xsetlist([], 'a', {'title' : 'New Title'})
4126 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
4127
4128 enew!
4129 call append(0, ["F5:50:L50", "F6:60:L60"])
4130 Xaddbuffer
4131 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
4132 enew!
4133
4134 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
4135 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
4136 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4137 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
4138 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
4139 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4140 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
4141 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4142 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4143 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
4144 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4145
4146 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
4147 Xfile Xone
4148 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4149 Xaddfile Xone
4150 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4151
4152 " Test case for updating a non-current quickfix list
4153 call g:Xsetlist([], 'f')
4154 Xexpr "F1:1:L1"
4155 Xexpr "F2:2:L2"
4156 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
4157 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4158 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
4159
4160 call delete("Xone")
4161endfunc
4162
4163func Test_qf_tick()
4164 call Xqftick_tests('c')
4165 call Xqftick_tests('l')
4166endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01004167
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02004168" Test helpgrep with lang specifier
4169func Xtest_helpgrep_with_lang_specifier(cchar)
4170 call s:setup_commands(a:cchar)
4171 Xhelpgrep Vim@en
4172 call assert_equal('help', &filetype)
4173 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
4174 new | only
4175endfunc
4176
4177func Test_helpgrep_with_lang_specifier()
4178 call Xtest_helpgrep_with_lang_specifier('c')
4179 call Xtest_helpgrep_with_lang_specifier('l')
4180endfunc
4181
Bram Moolenaar12237442017-12-19 12:38:52 +01004182" The following test used to crash Vim.
4183" Open the location list window and close the regular window associated with
4184" the location list. When the garbage collection runs now, it incorrectly
4185" marks the location list context as not in use and frees the context.
4186func Test_ll_window_ctx()
4187 call setloclist(0, [], 'f')
4188 call setloclist(0, [], 'a', {'context' : []})
4189 lopen | only
4190 call test_garbagecollect_now()
4191 echo getloclist(0, {'context' : 1}).context
4192 enew | only
4193endfunc
4194
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004195" The following test used to crash vim
4196func Test_lfile_crash()
4197 sp Xtest
4198 au QuickFixCmdPre * bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02004199 call assert_fails('lfile', 'E40:')
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004200 au! QuickFixCmdPre
4201endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01004202
4203" The following test used to crash vim
4204func Test_lbuffer_crash()
4205 sv Xtest
4206 augroup QF_Test
4207 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004208 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bw
Bram Moolenaar3c097222017-12-21 20:54:49 +01004209 augroup END
4210 lbuffer
4211 augroup QF_Test
4212 au!
4213 augroup END
4214endfunc
4215
4216" The following test used to crash vim
4217func Test_lexpr_crash()
4218 augroup QF_Test
4219 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004220 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004221 augroup END
4222 lexpr ""
4223 augroup QF_Test
4224 au!
4225 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004226
Bram Moolenaar3c097222017-12-21 20:54:49 +01004227 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004228 augroup QF_Test
4229 au!
4230 au BufNew * call setloclist(0, [], 'f')
4231 augroup END
4232 lexpr 'x:1:x'
4233 augroup QF_Test
4234 au!
4235 augroup END
4236
4237 enew | only
4238 lexpr ''
4239 lopen
4240 augroup QF_Test
4241 au!
4242 au FileType * call setloclist(0, [], 'f')
4243 augroup END
4244 lexpr ''
4245 augroup QF_Test
4246 au!
4247 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01004248endfunc
4249
4250" The following test used to crash Vim
4251func Test_lvimgrep_crash()
4252 sv Xtest
4253 augroup QF_Test
4254 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004255 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004256 augroup END
4257 lvimgrep quickfix test_quickfix.vim
4258 augroup QF_Test
4259 au!
4260 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004261
4262 new | only
4263 augroup QF_Test
4264 au!
4265 au BufEnter * call setloclist(0, [], 'r')
4266 augroup END
4267 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
4268 augroup QF_Test
4269 au!
4270 augroup END
4271
Bram Moolenaar3c097222017-12-21 20:54:49 +01004272 enew | only
4273endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01004274
Bram Moolenaar2573af32020-03-14 17:21:34 +01004275func Test_lvimgrep_crash2()
4276 au BufNewFile x sfind
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004277 call assert_fails('lvimgrep x x', 'E471:')
4278 call assert_fails('lvimgrep x x x', 'E471:')
Bram Moolenaar2573af32020-03-14 17:21:34 +01004279
4280 au! BufNewFile
4281endfunc
4282
Bram Moolenaarde046542017-12-26 13:53:11 +01004283" Test for the position of the quickfix and location list window
4284func Test_qfwin_pos()
4285 " Open two windows
4286 new | only
4287 new
4288 cexpr ['F1:10:L10']
4289 copen
4290 " Quickfix window should be the bottom most window
4291 call assert_equal(3, winnr())
4292 close
4293 " Open at the very top
4294 wincmd t
4295 topleft copen
4296 call assert_equal(1, winnr())
4297 close
4298 " open left of the current window
4299 wincmd t
4300 below new
4301 leftabove copen
4302 call assert_equal(2, winnr())
4303 close
4304 " open right of the current window
4305 rightbelow copen
4306 call assert_equal(3, winnr())
4307 close
4308endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004309
4310" Tests for quickfix/location lists changed by autocommands when
4311" :vimgrep/:lvimgrep commands are running.
4312func Test_vimgrep_autocmd()
4313 call setqflist([], 'f')
4314 call writefile(['stars'], 'Xtest1.txt')
4315 call writefile(['stars'], 'Xtest2.txt')
4316
4317 " Test 1:
4318 " When searching for a pattern using :vimgrep, if the quickfix list is
4319 " changed by an autocmd, the results should be added to the correct quickfix
4320 " list.
4321 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
4322 silent vimgrep stars Xtest*.txt
4323 call assert_equal(1, getqflist({'nr' : 0}).nr)
4324 call assert_equal(3, getqflist({'nr' : '$'}).nr)
4325 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
4326 au! BufRead Xtest2.txt
4327
4328 " Test 2:
4329 " When searching for a pattern using :vimgrep, if the quickfix list is
4330 " freed, then a error should be given.
4331 silent! %bwipe!
4332 call setqflist([], 'f')
4333 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
4334 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
4335 au! BufRead Xtest2.txt
4336
4337 " Test 3:
4338 " When searching for a pattern using :lvimgrep, if the location list is
4339 " freed, then the command should error out.
4340 silent! %bwipe!
4341 let g:save_winid = win_getid()
4342 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
4343 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
4344 au! BufRead Xtest2.txt
4345
4346 call delete('Xtest1.txt')
4347 call delete('Xtest2.txt')
4348 call setqflist([], 'f')
4349endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004350
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004351" Test for an autocmd changing the current directory when running vimgrep
4352func Xvimgrep_autocmd_cd(cchar)
4353 call s:setup_commands(a:cchar)
4354
4355 %bwipe
4356 let save_cwd = getcwd()
4357
4358 augroup QF_Test
4359 au!
4360 autocmd BufRead * silent cd %:p:h
4361 augroup END
4362
4363 10Xvimgrep /vim/ Xdir/**
4364 let l = g:Xgetlist()
4365 call assert_equal('f1.txt', bufname(l[0].bufnr))
4366 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
4367
4368 augroup QF_Test
4369 au!
4370 augroup END
4371
4372 exe 'cd ' . save_cwd
4373endfunc
4374
4375func Test_vimgrep_autocmd_cd()
4376 call mkdir('Xdir/a', 'p')
4377 call mkdir('Xdir/b', 'p')
4378 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xdir/a/f1.txt')
4379 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xdir/b/f2.txt')
4380 call Xvimgrep_autocmd_cd('c')
4381 call Xvimgrep_autocmd_cd('l')
4382 %bwipe
4383 call delete('Xdir', 'rf')
4384endfunc
4385
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004386" The following test used to crash Vim
4387func Test_lhelpgrep_autocmd()
4388 lhelpgrep quickfix
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004389 augroup QF_Test
4390 au!
4391 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
4392 augroup END
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004393 lhelpgrep buffer
4394 call assert_equal('help', &filetype)
4395 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
4396 lhelpgrep tabpage
4397 call assert_equal('help', &filetype)
4398 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004399 augroup QF_Test
4400 au!
4401 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004402
4403 new | only
4404 augroup QF_Test
4405 au!
4406 au BufEnter * call setqflist([], 'f')
4407 augroup END
4408 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaardbfa7952020-11-02 20:04:22 +01004409 " run the test with a help window already open
4410 help
4411 wincmd w
4412 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004413 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004414 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004415 augroup END
4416
4417 new | only
4418 augroup QF_Test
4419 au!
4420 au BufEnter * call setqflist([], 'r')
4421 augroup END
4422 call assert_fails('helpgrep quickfix', 'E925:')
4423 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004424 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004425 augroup END
4426
4427 new | only
4428 augroup QF_Test
4429 au!
4430 au BufEnter * call setloclist(0, [], 'r')
4431 augroup END
4432 call assert_fails('lhelpgrep quickfix', 'E926:')
4433 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004434 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004435 augroup END
4436
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004437 " Replace the contents of a help window location list when it is still in
4438 " use.
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004439 new | only
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004440 lhelpgrep quickfix
4441 wincmd w
4442 augroup QF_Test
4443 au!
4444 autocmd WinEnter * call setloclist(0, [], 'r')
4445 augroup END
4446 call assert_fails('lhelpgrep win_getid', 'E926:')
4447 augroup QF_Test
4448 au!
4449 augroup END
4450
4451 %bw!
4452endfunc
4453
4454" The following test used to crash Vim
4455func Test_lhelpgrep_autocmd_free_loclist()
4456 %bw!
4457 lhelpgrep quickfix
4458 wincmd w
4459 augroup QF_Test
4460 au!
4461 autocmd WinEnter * call setloclist(0, [], 'f')
4462 augroup END
4463 lhelpgrep win_getid
4464 wincmd w
4465 wincmd w
4466 wincmd w
4467 augroup QF_Test
4468 au!
4469 augroup END
4470 %bw!
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004471endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02004472
4473" Test for shortening/simplifying the file name when opening the
4474" quickfix window or when displaying the quickfix list
4475func Test_shorten_fname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02004476 CheckUnix
Bram Moolenaara796d462018-05-01 14:30:36 +02004477 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004478 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004479 let fname = getcwd() . '/test_quickfix.vim'
4480 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4481 call assert_equal(fname, bufname('test_quickfix.vim'))
4482 " Opening the quickfix window should simplify the file path
4483 cwindow
4484 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
4485 cclose
4486 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004487 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004488 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4489 call assert_equal(fname, bufname('test_quickfix.vim'))
4490 " Displaying the quickfix list should simplify the file path
4491 silent! clist
4492 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004493 " Add a few entries for the same file with different paths and check whether
4494 " the buffer name is shortened
4495 %bwipe
4496 call setqflist([], 'f')
4497 call setqflist([{'filename' : 'test_quickfix.vim', 'lnum' : 10},
4498 \ {'filename' : '../testdir/test_quickfix.vim', 'lnum' : 20},
4499 \ {'filename' : fname, 'lnum' : 30}], ' ')
4500 copen
4501 call assert_equal(['test_quickfix.vim|10| ',
4502 \ 'test_quickfix.vim|20| ',
4503 \ 'test_quickfix.vim|30| '], getline(1, '$'))
4504 cclose
Bram Moolenaara796d462018-05-01 14:30:36 +02004505endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004506
4507" Quickfix title tests
4508" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
4509" Otherwise due to indentation, the title is set with spaces at the beginning
4510" of the command.
4511func Test_qftitle()
4512 call writefile(["F1:1:Line1"], 'Xerr')
4513
4514 " :cexpr
4515 exe "cexpr readfile('Xerr')"
4516 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
4517
4518 " :cgetexpr
4519 exe "cgetexpr readfile('Xerr')"
4520 call assert_equal(":cgetexpr readfile('Xerr')",
4521 \ getqflist({'title' : 1}).title)
4522
4523 " :caddexpr
4524 call setqflist([], 'f')
4525 exe "caddexpr readfile('Xerr')"
4526 call assert_equal(":caddexpr readfile('Xerr')",
4527 \ getqflist({'title' : 1}).title)
4528
4529 " :cbuffer
4530 new Xerr
4531 exe "cbuffer"
4532 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
4533
4534 " :cgetbuffer
4535 edit Xerr
4536 exe "cgetbuffer"
4537 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
4538
4539 " :caddbuffer
4540 call setqflist([], 'f')
4541 edit Xerr
4542 exe "caddbuffer"
4543 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
4544
4545 " :cfile
4546 exe "cfile Xerr"
4547 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
4548
4549 " :cgetfile
4550 exe "cgetfile Xerr"
4551 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
4552
4553 " :caddfile
4554 call setqflist([], 'f')
4555 exe "caddfile Xerr"
4556 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
4557
4558 " :grep
4559 set grepprg=internal
4560 exe "grep F1 Xerr"
4561 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
4562
4563 " :grepadd
4564 call setqflist([], 'f')
4565 exe "grepadd F1 Xerr"
4566 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
4567 set grepprg&vim
4568
4569 " :vimgrep
4570 exe "vimgrep F1 Xerr"
4571 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
4572
4573 " :vimgrepadd
4574 call setqflist([], 'f')
4575 exe "vimgrepadd F1 Xerr"
4576 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
4577
4578 call setqflist(['F1:10:L10'], ' ')
4579 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4580
4581 call setqflist([], 'f')
4582 call setqflist(['F1:10:L10'], 'a')
4583 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4584
4585 call setqflist([], 'f')
4586 call setqflist(['F1:10:L10'], 'r')
4587 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4588
4589 close
4590 call delete('Xerr')
4591
4592 call setqflist([], ' ', {'title' : 'Errors'})
4593 copen
4594 call assert_equal('Errors', w:quickfix_title)
4595 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
4596 call assert_equal('Errors', w:quickfix_title)
4597 cclose
Bram Moolenaar530bed92020-12-16 21:02:56 +01004598
4599 " Switching to another quickfix list in one tab page should update the
4600 " quickfix window title and statusline in all the other tab pages also
4601 call setqflist([], 'f')
4602 %bw!
4603 cgetexpr ['file_one:1:1: error in the first quickfix list']
4604 call setqflist([], 'a', {'title': 'first quickfix list'})
4605 cgetexpr ['file_two:2:1: error in the second quickfix list']
4606 call setqflist([], 'a', {'title': 'second quickfix list'})
4607 copen
4608 wincmd t
4609 tabnew two
4610 copen
4611 wincmd t
4612 colder
4613 call assert_equal('first quickfix list', gettabwinvar(1, 2, 'quickfix_title'))
4614 call assert_equal('first quickfix list', gettabwinvar(2, 2, 'quickfix_title'))
4615 call assert_equal(1, tabpagewinnr(1))
4616 call assert_equal(1, tabpagewinnr(2))
4617 tabnew
4618 call setqflist([], 'a', {'title': 'new quickfix title'})
4619 call assert_equal('new quickfix title', gettabwinvar(1, 2, 'quickfix_title'))
4620 call assert_equal('new quickfix title', gettabwinvar(2, 2, 'quickfix_title'))
4621 %bw!
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004622endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02004623
4624func Test_lbuffer_with_bwipe()
4625 new
4626 new
4627 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004628 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bwipe
Bram Moolenaar600323b2018-06-16 22:16:47 +02004629 augroup END
4630 lbuffer
4631 augroup nasty
4632 au!
4633 augroup END
4634endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02004635
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004636" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
4637" running
4638func Xexpr_acmd_freelist(cchar)
4639 call s:setup_commands(a:cchar)
4640
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004641 " This was using freed memory (but with what events?)
Bram Moolenaar0366c012018-06-18 20:52:13 +02004642 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004643 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02004644 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004645 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02004646 augroup nasty
4647 au!
4648 augroup END
4649endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004650
4651func Test_cexpr_acmd_freelist()
4652 call Xexpr_acmd_freelist('c')
4653 call Xexpr_acmd_freelist('l')
4654endfunc
4655
4656" Test for commands that create a new quickfix/location list and jump to the
4657" first error automatically.
4658func Xjumpto_first_error_test(cchar)
4659 call s:setup_commands(a:cchar)
4660
4661 call s:create_test_file('Xtestfile1')
4662 call s:create_test_file('Xtestfile2')
4663 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4664
4665 " Test for cexpr/lexpr
4666 enew
4667 Xexpr l
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004668 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004669 call assert_equal(2, line('.'))
4670
4671 " Test for cfile/lfile
4672 enew
4673 call writefile(l, 'Xerr')
4674 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004675 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004676 call assert_equal(2, line('.'))
4677
4678 " Test for cbuffer/lbuffer
4679 edit Xerr
4680 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004681 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004682 call assert_equal(2, line('.'))
4683
4684 call delete('Xerr')
4685 call delete('Xtestfile1')
4686 call delete('Xtestfile2')
4687endfunc
4688
4689func Test_jumpto_first_error()
4690 call Xjumpto_first_error_test('c')
4691 call Xjumpto_first_error_test('l')
4692endfunc
4693
4694" Test for a quickfix autocmd changing the quickfix/location list before
4695" jumping to the first error in the new list.
4696func Xautocmd_changelist(cchar)
4697 call s:setup_commands(a:cchar)
4698
4699 " Test for cfile/lfile
4700 call s:create_test_file('Xtestfile1')
4701 call s:create_test_file('Xtestfile2')
4702 Xexpr 'Xtestfile1:2:Line2'
4703 autocmd QuickFixCmdPost * Xolder
4704 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4705 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004706 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004707 call assert_equal(4, line('.'))
4708 autocmd! QuickFixCmdPost
4709
4710 " Test for cbuffer/lbuffer
4711 call g:Xsetlist([], 'f')
4712 Xexpr 'Xtestfile1:2:Line2'
4713 autocmd QuickFixCmdPost * Xolder
4714 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4715 edit Xerr
4716 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004717 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004718 call assert_equal(4, line('.'))
4719 autocmd! QuickFixCmdPost
4720
4721 " Test for cexpr/lexpr
4722 call g:Xsetlist([], 'f')
4723 Xexpr 'Xtestfile1:2:Line2'
4724 autocmd QuickFixCmdPost * Xolder
4725 Xexpr 'Xtestfile2:4:Line4'
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004726 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004727 call assert_equal(4, line('.'))
4728 autocmd! QuickFixCmdPost
4729
Bram Moolenaar851332e2018-07-03 19:16:00 +02004730 " The grepprg may not be set on non-Unix systems
4731 if has('unix')
4732 " Test for grep/lgrep
4733 call g:Xsetlist([], 'f')
4734 Xexpr 'Xtestfile1:2:Line2'
4735 autocmd QuickFixCmdPost * Xolder
4736 silent Xgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004737 call assert_equal('Xtestfile2', @%)
Bram Moolenaar851332e2018-07-03 19:16:00 +02004738 call assert_equal(5, line('.'))
4739 autocmd! QuickFixCmdPost
4740 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004741
4742 " Test for vimgrep/lvimgrep
4743 call g:Xsetlist([], 'f')
4744 Xexpr 'Xtestfile1:2:Line2'
4745 autocmd QuickFixCmdPost * Xolder
4746 silent Xvimgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004747 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004748 call assert_equal(5, line('.'))
4749 autocmd! QuickFixCmdPost
4750
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004751 " Test for autocommands clearing the quickfix list before jumping to the
4752 " first error. This should not result in an error
4753 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
4754 let v:errmsg = ''
4755 " Test for cfile/lfile
4756 Xfile Xerr
4757 call assert_true(v:errmsg !~# 'E42:')
4758 " Test for cbuffer/lbuffer
4759 edit Xerr
4760 Xbuffer
4761 call assert_true(v:errmsg !~# 'E42:')
4762 " Test for cexpr/lexpr
4763 Xexpr 'Xtestfile2:4:Line4'
4764 call assert_true(v:errmsg !~# 'E42:')
4765 " Test for grep/lgrep
4766 " The grepprg may not be set on non-Unix systems
4767 if has('unix')
4768 silent Xgrep Line5 Xtestfile2
4769 call assert_true(v:errmsg !~# 'E42:')
4770 endif
4771 " Test for vimgrep/lvimgrep
4772 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
4773 autocmd! QuickFixCmdPost
4774
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004775 call delete('Xerr')
4776 call delete('Xtestfile1')
4777 call delete('Xtestfile2')
4778endfunc
4779
4780func Test_autocmd_changelist()
4781 call Xautocmd_changelist('c')
4782 call Xautocmd_changelist('l')
4783endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004784
4785" Tests for the ':filter /pat/ clist' command
4786func Test_filter_clist()
4787 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4788 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4789 \ split(execute('filter /Line 15/ clist'), "\n"))
4790 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4791 \ split(execute('filter /Xfile1/ clist'), "\n"))
4792 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4793
4794 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4795 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4796 call assert_equal([' 2 pqr:pat2: '],
4797 \ split(execute('filter /pqr/ clist'), "\n"))
4798 call assert_equal([' 1 abc:pat1: '],
4799 \ split(execute('filter /pat1/ clist'), "\n"))
4800endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004801
4802" Tests for the "CTRL-W <CR>" command.
4803func Xview_result_split_tests(cchar)
4804 call s:setup_commands(a:cchar)
4805
4806 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4807 call g:Xsetlist([])
4808 Xopen
4809 let l:win_count = winnr('$')
Bram Moolenaare2e40752020-09-04 21:18:46 +02004810 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42:')
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004811 call assert_equal(l:win_count, winnr('$'))
4812 Xclose
4813endfunc
4814
4815func Test_view_result_split()
4816 call Xview_result_split_tests('c')
4817 call Xview_result_split_tests('l')
4818endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004819
4820" Test that :cc sets curswant
4821func Test_curswant()
4822 helpgrep quickfix
4823 normal! llll
4824 1cc
4825 call assert_equal(getcurpos()[4], virtcol('.'))
4826 cclose | helpclose
4827endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004828
4829" Test for opening a file from the quickfix window using CTRL-W <Enter>
4830" doesn't leave an empty buffer around.
4831func Test_splitview()
4832 call s:create_test_file('Xtestfile1')
4833 call s:create_test_file('Xtestfile2')
4834 new | only
4835 let last_bufnr = bufnr('Test_sv_1', 1)
4836 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4837 cgetexpr l
4838 copen
4839 let numbufs = len(getbufinfo())
4840 exe "normal \<C-W>\<CR>"
4841 copen
4842 exe "normal j\<C-W>\<CR>"
4843 " Make sure new empty buffers are not created
4844 call assert_equal(numbufs, len(getbufinfo()))
4845 " Creating a new buffer should use the next available buffer number
4846 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4847 bwipe Test_sv_1
4848 bwipe Test_sv_2
4849 new | only
4850
4851 " When split opening files from location list window, make sure that two
4852 " windows doesn't refer to the same location list
4853 lgetexpr l
4854 let locid = getloclist(0, {'id' : 0}).id
4855 lopen
4856 exe "normal \<C-W>\<CR>"
4857 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4858 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4859 new | only
4860
4861 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004862 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004863 lhelpgrep window
4864 let locid = getloclist(0, {'id' : 0}).id
4865 lwindow
4866 exe "normal j\<C-W>\<CR>"
4867 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4868 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4869 new | only
4870
Bram Moolenaar406cd902020-02-18 21:54:41 +01004871 " Using :split or :vsplit from a quickfix window should behave like a :new
4872 " or a :vnew command
4873 copen
4874 split
4875 call assert_equal(3, winnr('$'))
4876 let l = getwininfo()
4877 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4878 close
4879 copen
4880 vsplit
4881 let l = getwininfo()
4882 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4883 new | only
4884
Bram Moolenaarb2443732018-11-11 22:50:27 +01004885 call delete('Xtestfile1')
4886 call delete('Xtestfile2')
4887endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004888
4889" Test for parsing entries using visual screen column
4890func Test_viscol()
4891 enew
4892 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4893 edit Xfile1
4894
4895 " Use byte offset for column number
4896 set efm&
4897 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
4898 call assert_equal([5, 8], [col('.'), virtcol('.')])
4899 cnext
4900 call assert_equal([9, 12], [col('.'), virtcol('.')])
4901 cnext
4902 call assert_equal([14, 20], [col('.'), virtcol('.')])
4903
4904 " Use screen column offset for column number
4905 set efm=%f:%l:%v:%m
4906 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4907 call assert_equal([5, 8], [col('.'), virtcol('.')])
4908 cnext
4909 call assert_equal([9, 12], [col('.'), virtcol('.')])
4910 cnext
4911 call assert_equal([14, 20], [col('.'), virtcol('.')])
4912 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4913 call assert_equal([5, 8], [col('.'), virtcol('.')])
4914 cnext
4915 call assert_equal([10, 16], [col('.'), virtcol('.')])
4916 cnext
4917 call assert_equal([14, 20], [col('.'), virtcol('.')])
4918
4919 enew
4920 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4921
4922 " Use byte offset for column number
4923 set efm&
4924 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
4925 call assert_equal([8, 10], [col('.'), virtcol('.')])
4926 cnext
4927 call assert_equal([11, 17], [col('.'), virtcol('.')])
4928 cnext
4929 call assert_equal([16, 25], [col('.'), virtcol('.')])
4930
4931 " Use screen column offset for column number
4932 set efm=%f:%l:%v:%m
4933 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4934 call assert_equal([8, 10], [col('.'), virtcol('.')])
4935 cnext
4936 call assert_equal([11, 17], [col('.'), virtcol('.')])
4937 cnext
4938 call assert_equal([16, 25], [col('.'), virtcol('.')])
4939
Bram Moolenaarc95940c2020-10-20 14:59:12 +02004940 " Use screen column number with a multi-line error message
4941 enew
4942 call writefile(["à test"], 'Xfile1')
4943 set efm=%E===\ %f\ ===,%C%l:%v,%Z%m
4944 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4945 call assert_equal('Xfile1', @%)
4946 call assert_equal([0, 1, 4, 0], getpos('.'))
4947
4948 " Repeat previous test with byte offset %c: ensure that fix to issue #7145
4949 " does not break this
4950 set efm=%E===\ %f\ ===,%C%l:%c,%Z%m
4951 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4952 call assert_equal('Xfile1', @%)
4953 call assert_equal([0, 1, 3, 0], getpos('.'))
4954
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004955 enew | only
4956 set efm&
4957 call delete('Xfile1')
4958endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004959
4960" Test for the quickfix window buffer
4961func Xqfbuf_test(cchar)
4962 call s:setup_commands(a:cchar)
4963
4964 " Quickfix buffer should be reused across closing and opening a quickfix
4965 " window
4966 Xexpr "F1:10:Line10"
4967 Xopen
4968 let qfbnum = bufnr('')
4969 Xclose
4970 " Even after the quickfix window is closed, the buffer should be loaded
4971 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004972 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004973 Xopen
4974 " Buffer should be reused when opening the window again
4975 call assert_equal(qfbnum, bufnr(''))
4976 Xclose
4977
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00004978 " When quickfix buffer is wiped out, getqflist() should return 0
4979 %bw!
4980 Xexpr ""
4981 Xopen
4982 bw!
4983 call assert_equal(0, g:Xgetlist({'qfbufnr': 0}).qfbufnr)
4984
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004985 if a:cchar == 'l'
4986 %bwipe
4987 " For a location list, when both the file window and the location list
4988 " window for the list are closed, then the buffer should be freed.
4989 new | only
4990 lexpr "F1:10:Line10"
4991 let wid = win_getid()
4992 lopen
4993 let qfbnum = bufnr('')
4994 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
4995 close
4996 " When the location list window is closed, the buffer name should not
4997 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004998 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004999 call assert_true(bufloaded(qfbnum))
5000
Bram Moolenaard82a81c2019-03-02 07:57:18 +01005001 " After deleting a location list buffer using ":bdelete", opening the
5002 " location list window should mark the buffer as a location list buffer.
5003 exe "bdelete " . qfbnum
5004 lopen
5005 call assert_equal("quickfix", &buftype)
5006 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
5007 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
5008 call assert_false(&swapfile)
5009 lclose
5010
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005011 " When the location list is cleared for the window, the buffer should be
5012 " removed
5013 call setloclist(0, [], 'f')
5014 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005015 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005016
5017 " When the location list is freed with the location list window open, the
5018 " location list buffer should not be lost. It should be reused when the
5019 " location list is again populated.
5020 lexpr "F1:10:Line10"
5021 lopen
5022 let wid = win_getid()
5023 let qfbnum = bufnr('')
5024 wincmd p
5025 call setloclist(0, [], 'f')
5026 lexpr "F1:10:Line10"
5027 lopen
5028 call assert_equal(wid, win_getid())
5029 call assert_equal(qfbnum, bufnr(''))
5030 lclose
5031
5032 " When the window with the location list is closed, the buffer should be
5033 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005034 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005035 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005036 endif
5037endfunc
5038
5039func Test_qfbuf()
5040 call Xqfbuf_test('c')
5041 call Xqfbuf_test('l')
5042endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005043
5044" If there is an autocmd to use only one window, then opening the location
5045" list window used to crash Vim.
5046func Test_winonly_autocmd()
5047 call s:create_test_file('Xtest1')
5048 " Autocmd to show only one Vim window at a time
5049 autocmd WinEnter * only
5050 new
5051 " Load the location list
5052 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
5053 let loclistid = getloclist(0, {'id' : 0}).id
5054 " Open the location list window. Only this window will be shown and the file
5055 " window is closed.
5056 lopen
5057 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
5058 " Jump to an entry in the location list and make sure that the cursor is
5059 " positioned correctly.
5060 ll 3
5061 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005062 call assert_equal('Xtest1', @%)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005063 call assert_equal(15, line('.'))
5064 " Cleanup
5065 autocmd! WinEnter
5066 new | only
5067 call delete('Xtest1')
5068endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02005069
5070" Test to make sure that an empty quickfix buffer is not reused for loading
5071" a normal buffer.
5072func Test_empty_qfbuf()
5073 enew | only
5074 call writefile(["Test"], 'Xfile1')
5075 call setqflist([], 'f')
5076 copen | only
5077 let qfbuf = bufnr('')
5078 edit Xfile1
5079 call assert_notequal(qfbuf, bufnr(''))
5080 enew
5081 call delete('Xfile1')
5082endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005083
5084" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005085" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005086func Xtest_below(cchar)
5087 call s:setup_commands(a:cchar)
5088
5089 " No quickfix/location list
5090 call assert_fails('Xbelow', 'E42:')
5091 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005092 call assert_fails('Xbefore', 'E42:')
5093 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005094
5095 " Empty quickfix/location list
5096 call g:Xsetlist([])
5097 call assert_fails('Xbelow', 'E42:')
5098 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005099 call assert_fails('Xbefore', 'E42:')
5100 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005101
5102 call s:create_test_file('X1')
5103 call s:create_test_file('X2')
5104 call s:create_test_file('X3')
5105 call s:create_test_file('X4')
5106
5107 " Invalid entries
5108 edit X1
5109 call g:Xsetlist(["E1", "E2"])
5110 call assert_fails('Xbelow', 'E42:')
5111 call assert_fails('Xabove', 'E42:')
5112 call assert_fails('3Xbelow', 'E42:')
5113 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005114 call assert_fails('Xbefore', 'E42:')
5115 call assert_fails('Xafter', 'E42:')
5116 call assert_fails('3Xbefore', 'E42:')
5117 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005118
5119 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005120 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 +02005121 edit +7 X2
5122 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005123 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005124 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005125 normal 7G
5126 Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005127 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005128 call assert_fails('Xbefore', 'E553:')
5129
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005130 normal 2j
5131 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005132 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005133 normal 7G
5134 Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005135 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005136
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005137 " Last error in this file
5138 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005139 call assert_equal(['X2', 15], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005140 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005141 normal gg
5142 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005143 call assert_equal(['X2', 15, 4], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005144 call assert_fails('Xafter', 'E553:')
5145
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005146 " First error in this file
5147 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005148 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005149 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005150 normal G
5151 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005152 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005153 call assert_fails('Xbefore', 'E553:')
5154
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005155 normal gg
5156 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005157 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005158 normal gg
5159 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005160 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005161
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005162 normal G
5163 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005164 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005165 normal G
5166 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005167 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005168
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005169 edit X4
5170 call assert_fails('Xabove', 'E42:')
5171 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005172 call assert_fails('Xbefore', 'E42:')
5173 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005174 if a:cchar == 'l'
5175 " If a buffer has location list entries from some other window but not
5176 " from the current window, then the commands should fail.
5177 edit X1 | split | call setloclist(0, [], 'f')
5178 call assert_fails('Xabove', 'E776:')
5179 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005180 call assert_fails('Xbefore', 'E776:')
5181 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005182 close
5183 endif
5184
5185 " Test for lines with multiple quickfix entries
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005186 let lines =<< trim END
5187 X1:5:L5
5188 X2:5:1:L5_1
5189 X2:5:2:L5_2
5190 X2:5:3:L5_3
5191 X2:10:1:L10_1
5192 X2:10:2:L10_2
5193 X2:10:3:L10_3
5194 X2:15:1:L15_1
5195 X2:15:2:L15_2
5196 X2:15:3:L15_3
5197 X3:3:L3
5198 END
5199 Xexpr lines
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005200 edit +1 X2
5201 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005202 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005203 normal 1G
5204 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005205 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005206
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005207 normal gg
5208 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005209 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005210 normal gg
5211 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005212 call assert_equal(['X2', 15, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005213
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005214 normal G
5215 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005216 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005217 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005218 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005219 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005220
5221 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005222 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005223 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005224 normal G
5225 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005226 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005227
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005228 normal 10G
5229 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005230 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005231 normal 10G$
5232 2Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005233 call assert_equal(['X2', 10, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005234
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005235 normal 10G
5236 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005237 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005238 normal 9G
5239 5Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005240 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005241
5242 " Invalid range
5243 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02005244 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005245 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005246 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02005247 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005248 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005249 endif
5250
5251 call delete('X1')
5252 call delete('X2')
5253 call delete('X3')
5254 call delete('X4')
5255endfunc
5256
5257func Test_cbelow()
5258 call Xtest_below('c')
5259 call Xtest_below('l')
5260endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02005261
5262func Test_quickfix_count()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005263 let commands =<< trim END
5264 cNext
5265 cNfile
5266 cabove
5267 cbelow
5268 cfirst
5269 clast
5270 cnewer
5271 cnext
5272 cnfile
5273 colder
5274 cprevious
5275 crewind
5276 lNext
5277 lNfile
5278 labove
5279 lbelow
5280 lfirst
5281 llast
5282 lnewer
5283 lnext
5284 lnfile
5285 lolder
5286 lprevious
5287 lrewind
5288 END
Bram Moolenaar25190db2019-05-04 15:05:28 +02005289 for cmd in commands
5290 call assert_fails('-1' .. cmd, 'E16:')
5291 call assert_fails('.' .. cmd, 'E16:')
5292 call assert_fails('%' .. cmd, 'E16:')
5293 call assert_fails('$' .. cmd, 'E16:')
5294 endfor
5295endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005296
5297" Test for aborting quickfix commands using QuickFixCmdPre
5298func Xtest_qfcmd_abort(cchar)
5299 call s:setup_commands(a:cchar)
5300
5301 call g:Xsetlist([], 'f')
5302
5303 " cexpr/lexpr
5304 let e = ''
5305 try
5306 Xexpr ["F1:10:Line10", "F2:20:Line20"]
5307 catch /.*/
5308 let e = v:exception
5309 endtry
5310 call assert_equal('AbortCmd', e)
5311 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5312
5313 " cfile/lfile
5314 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
5315 let e = ''
5316 try
5317 Xfile Xfile1
5318 catch /.*/
5319 let e = v:exception
5320 endtry
5321 call assert_equal('AbortCmd', e)
5322 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5323 call delete('Xfile1')
5324
5325 " cgetbuffer/lgetbuffer
5326 enew!
5327 call append(0, ["F1:10:Line10", "F2:20:Line20"])
5328 let e = ''
5329 try
5330 Xgetbuffer
5331 catch /.*/
5332 let e = v:exception
5333 endtry
5334 call assert_equal('AbortCmd', e)
5335 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5336 enew!
5337
5338 " vimgrep/lvimgrep
5339 let e = ''
5340 try
5341 Xvimgrep /func/ test_quickfix.vim
5342 catch /.*/
5343 let e = v:exception
5344 endtry
5345 call assert_equal('AbortCmd', e)
5346 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5347
5348 " helpgrep/lhelpgrep
5349 let e = ''
5350 try
5351 Xhelpgrep quickfix
5352 catch /.*/
5353 let e = v:exception
5354 endtry
5355 call assert_equal('AbortCmd', e)
5356 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5357
5358 " grep/lgrep
5359 if has('unix')
5360 let e = ''
5361 try
5362 silent Xgrep func test_quickfix.vim
5363 catch /.*/
5364 let e = v:exception
5365 endtry
5366 call assert_equal('AbortCmd', e)
5367 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5368 endif
5369endfunc
5370
5371func Test_qfcmd_abort()
5372 augroup QF_Test
5373 au!
5374 autocmd QuickFixCmdPre * throw "AbortCmd"
5375 augroup END
5376
5377 call Xtest_qfcmd_abort('c')
5378 call Xtest_qfcmd_abort('l')
5379
5380 augroup QF_Test
5381 au!
5382 augroup END
5383endfunc
5384
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005385" Test for using a file in one of the parent directories.
5386func Test_search_in_dirstack()
5387 call mkdir('Xtestdir/a/b/c', 'p')
5388 let save_cwd = getcwd()
5389 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
5390 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
5391 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
5392 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
5393
5394 let lines = "Entering dir Xtestdir\n" .
5395 \ "Entering dir a\n" .
5396 \ "Entering dir b\n" .
5397 \ "Xfile2:2:X2_L2\n" .
5398 \ "Leaving dir a\n" .
5399 \ "Xfile1:2:X1_L2\n" .
5400 \ "Xfile3:1:X3_L1\n" .
5401 \ "Entering dir c\n" .
5402 \ "Xfile4:2:X4_L2\n" .
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005403 \ "Leaving dir c\n"
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005404 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005405 cexpr lines .. "Leaving dir Xtestdir|\n" | let next = 1
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005406 call assert_equal(11, getqflist({'size' : 0}).size)
5407 call assert_equal(4, getqflist({'idx' : 0}).idx)
5408 call assert_equal('X2_L2', getline('.'))
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005409 call assert_equal(1, next)
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005410 cnext
5411 call assert_equal(6, getqflist({'idx' : 0}).idx)
5412 call assert_equal('X1_L2', getline('.'))
5413 cnext
5414 call assert_equal(7, getqflist({'idx' : 0}).idx)
5415 call assert_equal(1, line('$'))
5416 call assert_equal('', getline(1))
5417 cnext
5418 call assert_equal(9, getqflist({'idx' : 0}).idx)
5419 call assert_equal(1, line('$'))
5420 call assert_equal('', getline(1))
5421
5422 set efm&
5423 exe 'cd ' . save_cwd
5424 call delete('Xtestdir', 'rf')
5425endfunc
5426
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005427" Test for :cquit
5428func Test_cquit()
5429 " Exit Vim with a non-zero value
5430 if RunVim([], ["cquit 7"], '')
5431 call assert_equal(7, v:shell_error)
5432 endif
5433
5434 if RunVim([], ["50cquit"], '')
5435 call assert_equal(50, v:shell_error)
5436 endif
5437
5438 " Exit Vim with default value
5439 if RunVim([], ["cquit"], '')
5440 call assert_equal(1, v:shell_error)
5441 endif
5442
5443 " Exit Vim with zero value
5444 if RunVim([], ["cquit 0"], '')
5445 call assert_equal(0, v:shell_error)
5446 endif
5447
5448 " Exit Vim with negative value
5449 call assert_fails('-3cquit', 'E16:')
5450endfunc
5451
Bram Moolenaar858ba062020-05-31 23:11:59 +02005452" Test for getting a specific item from a quickfix list
5453func Xtest_getqflist_by_idx(cchar)
5454 call s:setup_commands(a:cchar)
5455 " Empty list
5456 call assert_equal([], g:Xgetlist({'idx' : 1, 'items' : 0}).items)
5457 Xexpr ['F1:10:L10', 'F1:20:L20']
5458 let l = g:Xgetlist({'idx' : 2, 'items' : 0}).items
5459 call assert_equal(bufnr('F1'), l[0].bufnr)
5460 call assert_equal(20, l[0].lnum)
5461 call assert_equal('L20', l[0].text)
5462 call assert_equal([], g:Xgetlist({'idx' : -1, 'items' : 0}).items)
5463 call assert_equal([], g:Xgetlist({'idx' : 3, 'items' : 0}).items)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005464 call assert_equal({}, g:Xgetlist(#{idx: "abc"}))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005465 %bwipe!
5466endfunc
5467
5468func Test_getqflist_by_idx()
5469 call Xtest_getqflist_by_idx('c')
5470 call Xtest_getqflist_by_idx('l')
5471endfunc
5472
5473" Test for the 'quickfixtextfunc' setting
5474func Tqfexpr(info)
5475 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005476 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005477 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005478 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005479 endif
5480
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005481 let l = []
5482 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5483 let e = qfl[idx]
5484 let s = ''
5485 if e.bufnr != 0
5486 let bname = bufname(e.bufnr)
5487 let s ..= fnamemodify(bname, ':.')
5488 endif
5489 let s ..= '-'
5490 let s ..= 'L' .. string(e.lnum) .. 'C' .. string(e.col) .. '-'
5491 let s ..= e.text
5492 call add(l, s)
5493 endfor
Bram Moolenaar858ba062020-05-31 23:11:59 +02005494
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005495 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005496endfunc
5497
5498func Xtest_qftextfunc(cchar)
5499 call s:setup_commands(a:cchar)
5500
5501 set efm=%f:%l:%c:%m
5502 set quickfixtextfunc=Tqfexpr
Bram Moolenaard43906d2020-07-20 21:31:32 +02005503 call assert_equal('Tqfexpr', &quickfixtextfunc)
5504 call assert_equal('',
5505 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
thinca6864efa2021-06-19 20:45:20 +02005506 call g:Xsetlist([
5507 \ { 'filename': 'F1', 'lnum': 10, 'col': 2,
5508 \ 'end_col': 7, 'text': 'green'},
5509 \ { 'filename': 'F1', 'lnum': 20, 'end_lnum': 25, 'col': 4,
5510 \ 'end_col': 8, 'text': 'blue'},
5511 \ ])
5512
Bram Moolenaar858ba062020-05-31 23:11:59 +02005513 Xwindow
5514 call assert_equal('F1-L10C2-green', getline(1))
5515 call assert_equal('F1-L20C4-blue', getline(2))
5516 Xclose
5517 set quickfixtextfunc&vim
5518 Xwindow
thinca6864efa2021-06-19 20:45:20 +02005519 call assert_equal('F1|10 col 2-7| green', getline(1))
5520 call assert_equal('F1|20-25 col 4-8| blue', getline(2))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005521 Xclose
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005522
5523 set efm=%f:%l:%c:%m
5524 set quickfixtextfunc=Tqfexpr
5525 " Update the list with only the cwindow
5526 Xwindow
5527 only
5528 call g:Xsetlist([
5529 \ { 'filename': 'F2', 'lnum': 20, 'col': 2,
5530 \ 'end_col': 7, 'text': 'red'}
5531 \ ])
5532 call assert_equal(['F2-L20C2-red'], getline(1, '$'))
5533 new
5534 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005535 set efm&
5536 set quickfixtextfunc&
5537
5538 " Test for per list 'quickfixtextfunc' setting
5539 func PerQfText(info)
5540 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005541 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005542 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005543 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005544 endif
5545 if empty(qfl)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005546 return []
Bram Moolenaar858ba062020-05-31 23:11:59 +02005547 endif
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005548 let l = []
5549 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5550 call add(l, 'Line ' .. qfl[idx].lnum .. ', Col ' .. qfl[idx].col)
5551 endfor
5552 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005553 endfunc
5554 set quickfixtextfunc=Tqfexpr
5555 call g:Xsetlist([], ' ', {'quickfixtextfunc' : "PerQfText"})
5556 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5557 Xwindow
5558 call assert_equal('Line 10, Col 2', getline(1))
5559 call assert_equal('Line 20, Col 4', getline(2))
5560 Xclose
Bram Moolenaard43906d2020-07-20 21:31:32 +02005561 call assert_equal(function('PerQfText'),
5562 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005563 " Add entries to the list when the quickfix buffer is hidden
5564 Xaddexpr ['F1:30:6:red']
5565 Xwindow
5566 call assert_equal('Line 30, Col 6', getline(3))
5567 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005568 call g:Xsetlist([], 'r', {'quickfixtextfunc' : ''})
Bram Moolenaard43906d2020-07-20 21:31:32 +02005569 call assert_equal('', g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar858ba062020-05-31 23:11:59 +02005570 set quickfixtextfunc&
5571 delfunc PerQfText
5572
5573 " Non-existing function
5574 set quickfixtextfunc=Tabc
5575 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5576 call assert_fails("Xwindow", 'E117:')
5577 Xclose
5578 set quickfixtextfunc&
5579
5580 " set option to a non-function
5581 set quickfixtextfunc=[10,\ 20]
5582 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5583 call assert_fails("Xwindow", 'E117:')
5584 Xclose
5585 set quickfixtextfunc&
5586
5587 " set option to a function with different set of arguments
5588 func Xqftext(a, b, c)
5589 return a:a .. a:b .. a:c
5590 endfunc
5591 set quickfixtextfunc=Xqftext
5592 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E119:')
5593 call assert_fails("Xwindow", 'E119:')
5594 Xclose
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005595
5596 " set option to a function that returns a list with non-strings
5597 func Xqftext2(d)
5598 return ['one', [], 'two']
5599 endfunc
5600 set quickfixtextfunc=Xqftext2
5601 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue', 'F1:30:6:red']",
5602 \ 'E730:')
5603 call assert_fails('Xwindow', 'E730:')
Bram Moolenaard43906d2020-07-20 21:31:32 +02005604 call assert_equal(['one', 'F1|20 col 4| blue', 'F1|30 col 6| red'],
5605 \ getline(1, '$'))
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005606 Xclose
5607
Bram Moolenaar858ba062020-05-31 23:11:59 +02005608 set quickfixtextfunc&
5609 delfunc Xqftext
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005610 delfunc Xqftext2
Bram Moolenaard43906d2020-07-20 21:31:32 +02005611
5612 " set the global option to a lambda function
5613 set quickfixtextfunc={d\ ->\ map(g:Xgetlist({'id'\ :\ d.id,\ 'items'\ :\ 1}).items[d.start_idx-1:d.end_idx-1],\ 'v:val.text')}
5614 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5615 Xwindow
5616 call assert_equal(['green', 'blue'], getline(1, '$'))
5617 Xclose
5618 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)
5619 set quickfixtextfunc&
5620
5621 " use a lambda function that returns an empty list
5622 set quickfixtextfunc={d\ ->\ []}
5623 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5624 Xwindow
5625 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5626 \ getline(1, '$'))
5627 Xclose
5628 set quickfixtextfunc&
5629
5630 " use a lambda function that returns a list with empty strings
5631 set quickfixtextfunc={d\ ->\ ['',\ '']}
5632 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5633 Xwindow
5634 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5635 \ getline(1, '$'))
5636 Xclose
5637 set quickfixtextfunc&
5638
5639 " set the per-quickfix list text function to a lambda function
5640 call g:Xsetlist([], ' ',
5641 \ {'quickfixtextfunc' :
5642 \ {d -> map(g:Xgetlist({'id' : d.id, 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5643 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5644 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5645 Xwindow
5646 call assert_equal('Line 10, Col 2', getline(1))
5647 call assert_equal('Line 20, Col 4', getline(2))
5648 Xclose
5649 call assert_match("function('<lambda>\\d\\+')", string(g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc))
5650 call g:Xsetlist([], 'f')
Bram Moolenaar858ba062020-05-31 23:11:59 +02005651endfunc
5652
5653func Test_qftextfunc()
5654 call Xtest_qftextfunc('c')
5655 call Xtest_qftextfunc('l')
5656endfunc
5657
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005658func Test_qftextfunc_callback()
5659 let lines =<< trim END
5660 set efm=%f:%l:%c:%m
5661
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00005662 #" Test for using a function name
5663 LET &qftf = 'g:Tqfexpr'
5664 cexpr "F0:0:0:L0"
5665 copen
5666 call assert_equal('F0-L0C0-L0', getline(1))
5667 cclose
5668
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005669 #" Test for using a function()
5670 set qftf=function('g:Tqfexpr')
5671 cexpr "F1:1:1:L1"
5672 copen
5673 call assert_equal('F1-L1C1-L1', getline(1))
5674 cclose
5675
5676 #" Using a funcref variable to set 'quickfixtextfunc'
5677 VAR Fn = function('g:Tqfexpr')
5678 LET &qftf = Fn
5679 cexpr "F2:2:2:L2"
5680 copen
5681 call assert_equal('F2-L2C2-L2', getline(1))
5682 cclose
5683
5684 #" Using string(funcref_variable) to set 'quickfixtextfunc'
5685 LET Fn = function('g:Tqfexpr')
5686 LET &qftf = string(Fn)
5687 cexpr "F3:3:3:L3"
5688 copen
5689 call assert_equal('F3-L3C3-L3', getline(1))
5690 cclose
5691
5692 #" Test for using a funcref()
5693 set qftf=funcref('g:Tqfexpr')
5694 cexpr "F4:4:4:L4"
5695 copen
5696 call assert_equal('F4-L4C4-L4', getline(1))
5697 cclose
5698
5699 #" Using a funcref variable to set 'quickfixtextfunc'
5700 LET Fn = funcref('g:Tqfexpr')
5701 LET &qftf = Fn
5702 cexpr "F5:5:5:L5"
5703 copen
5704 call assert_equal('F5-L5C5-L5', getline(1))
5705 cclose
5706
5707 #" Using a string(funcref_variable) to set 'quickfixtextfunc'
5708 LET Fn = funcref('g:Tqfexpr')
5709 LET &qftf = string(Fn)
5710 cexpr "F5:5:5:L5"
5711 copen
5712 call assert_equal('F5-L5C5-L5', getline(1))
5713 cclose
5714
5715 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00005716 VAR optval = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005717 LET optval = substitute(optval, ' ', '\\ ', 'g')
5718 exe "set qftf=" .. optval
5719 cexpr "F6:6:6:L6"
5720 copen
5721 call assert_equal('F6-L6C6-L6', getline(1))
5722 cclose
5723
5724 #" Set 'quickfixtextfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005725 LET &qftf = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005726 cexpr "F7:7:7:L7"
5727 copen
5728 call assert_equal('F7-L7C7-L7', getline(1))
5729 cclose
5730
5731 #" Set 'quickfixtextfunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005732 LET &qftf = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005733 cexpr "F8:8:8:L8"
5734 copen
5735 call assert_equal('F8-L8C8-L8', getline(1))
5736 cclose
5737
5738 #" Set 'quickfixtextfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005739 VAR Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005740 LET &qftf = Lambda
5741 cexpr "F9:9:9:L9"
5742 copen
5743 call assert_equal('F9-L9C9-L9', getline(1))
5744 cclose
5745
5746 #" Set 'quickfixtextfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005747 LET Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005748 LET &qftf = string(Lambda)
5749 cexpr "F9:9:9:L9"
5750 copen
5751 call assert_equal('F9-L9C9-L9', getline(1))
5752 cclose
5753 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00005754 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005755
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00005756 " Test for using a script-local function name
5757 func s:TqfFunc2(info)
5758 let g:TqfFunc2Args = [a:info.start_idx, a:info.end_idx]
5759 return ''
5760 endfunc
5761 let g:TqfFunc2Args = []
5762 set quickfixtextfunc=s:TqfFunc2
5763 cexpr "F10:10:10:L10"
5764 cclose
5765 call assert_equal([1, 1], g:TqfFunc2Args)
5766
5767 let &quickfixtextfunc = 's:TqfFunc2'
5768 cexpr "F11:11:11:L11"
5769 cclose
5770 call assert_equal([1, 1], g:TqfFunc2Args)
5771 delfunc s:TqfFunc2
5772
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005773 " set 'quickfixtextfunc' to a partial with dict. This used to cause a crash.
5774 func SetQftfFunc()
5775 let params = {'qftf': function('g:DictQftfFunc')}
5776 let &quickfixtextfunc = params.qftf
5777 endfunc
5778 func g:DictQftfFunc(_) dict
5779 endfunc
5780 call SetQftfFunc()
5781 new
5782 call SetQftfFunc()
5783 bw
5784 call test_garbagecollect_now()
5785 new
5786 set qftf=
5787 wincmd w
5788 set qftf=
5789 :%bw!
5790
5791 " set per-quickfix list 'quickfixtextfunc' to a partial with dict. This used
5792 " to cause a crash.
5793 let &qftf = ''
5794 func SetLocalQftfFunc()
5795 let params = {'qftf': function('g:DictQftfFunc')}
5796 call setqflist([], 'a', {'quickfixtextfunc' : params.qftf})
5797 endfunc
5798 call SetLocalQftfFunc()
5799 call test_garbagecollect_now()
5800 call setqflist([], 'a', {'quickfixtextfunc' : ''})
5801 delfunc g:DictQftfFunc
5802 delfunc SetQftfFunc
5803 delfunc SetLocalQftfFunc
5804 set efm&
5805endfunc
5806
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02005807" Test for updating a location list for some other window and check that
5808" 'qftextfunc' uses the correct location list.
5809func Test_qftextfunc_other_loclist()
5810 %bw!
5811 call setloclist(0, [], 'f')
5812
5813 " create a window and a location list for it and open the location list
5814 " window
5815 lexpr ['F1:10:12:one', 'F1:20:14:two']
5816 let w1_id = win_getid()
5817 call setloclist(0, [], ' ',
5818 \ {'lines': ['F1:10:12:one', 'F1:20:14:two'],
5819 \ 'quickfixtextfunc':
5820 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5821 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5822 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5823 lwindow
5824 let w2_id = win_getid()
5825
5826 " create another window and a location list for it and open the location
5827 " list window
5828 topleft new
5829 let w3_id = win_getid()
5830 call setloclist(0, [], ' ',
5831 \ {'lines': ['F2:30:32:eleven', 'F2:40:34:twelve'],
5832 \ 'quickfixtextfunc':
5833 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5834 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5835 \ "'Ligne ' .. v:val.lnum .. ', Colonne ' .. v:val.col")}})
5836 lwindow
5837 let w4_id = win_getid()
5838
5839 topleft new
5840 lexpr ['F3:50:52:green', 'F3:60:54:blue']
5841 let w5_id = win_getid()
5842
5843 " change the location list for some other window
5844 call setloclist(0, [], 'r', {'lines': ['F3:55:56:aaa', 'F3:57:58:bbb']})
5845 call setloclist(w1_id, [], 'r', {'lines': ['F1:62:63:bbb', 'F1:64:65:ccc']})
5846 call setloclist(w3_id, [], 'r', {'lines': ['F2:76:77:ddd', 'F2:78:79:eee']})
5847 call assert_equal(['Line 62, Col 63', 'Line 64, Col 65'],
5848 \ getbufline(winbufnr(w2_id), 1, '$'))
5849 call assert_equal(['Ligne 76, Colonne 77', 'Ligne 78, Colonne 79'],
5850 \ getbufline(winbufnr(w4_id), 1, '$'))
5851 call setloclist(w2_id, [], 'r', {'lines': ['F1:32:33:fff', 'F1:34:35:ggg']})
5852 call setloclist(w4_id, [], 'r', {'lines': ['F2:46:47:hhh', 'F2:48:49:jjj']})
5853 call assert_equal(['Line 32, Col 33', 'Line 34, Col 35'],
5854 \ getbufline(winbufnr(w2_id), 1, '$'))
5855 call assert_equal(['Ligne 46, Colonne 47', 'Ligne 48, Colonne 49'],
5856 \ getbufline(winbufnr(w4_id), 1, '$'))
5857
5858 call win_gotoid(w5_id)
5859 lwindow
5860 call assert_equal(['F3|55 col 56| aaa', 'F3|57 col 58| bbb'],
5861 \ getline(1, '$'))
5862 %bw!
5863endfunc
5864
Bram Moolenaarec98e932020-06-08 19:35:59 +02005865" Running :lhelpgrep command more than once in a help window, doesn't jump to
5866" the help topic
5867func Test_lhelpgrep_from_help_window()
5868 call mkdir('Xtestdir/doc', 'p')
5869 call writefile(['window'], 'Xtestdir/doc/a.txt')
5870 call writefile(['buffer'], 'Xtestdir/doc/b.txt')
5871 let save_rtp = &rtp
5872 let &rtp = 'Xtestdir'
5873 lhelpgrep window
5874 lhelpgrep buffer
5875 call assert_equal('b.txt', fnamemodify(@%, ":p:t"))
5876 lhelpgrep window
5877 call assert_equal('a.txt', fnamemodify(@%, ":p:t"))
5878 let &rtp = save_rtp
5879 call delete('Xtestdir', 'rf')
5880 new | only!
5881endfunc
5882
Bram Moolenaarf7c4d832020-08-11 20:42:19 +02005883" Test for the crash fixed by 7.3.715
5884func Test_setloclist_crash()
5885 %bw!
5886 let g:BufNum = bufnr()
5887 augroup QF_Test
5888 au!
5889 au BufUnload * call setloclist(0, [{'bufnr':g:BufNum, 'lnum':1, 'col':1, 'text': 'tango down'}])
5890 augroup END
5891
5892 try
5893 lvimgrep /.*/ *.mak
5894 catch /E926:/
5895 endtry
5896 call assert_equal('tango down', getloclist(0, {'items' : 0}).items[0].text)
5897 call assert_equal(1, getloclist(0, {'size' : 0}).size)
5898
5899 augroup QF_Test
5900 au!
5901 augroup END
5902 unlet g:BufNum
5903 %bw!
5904endfunc
5905
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005906" Test for adding an invalid entry with the quickfix window open and making
5907" sure that the window contents are not changed
5908func Test_add_invalid_entry_with_qf_window()
5909 call setqflist([], 'f')
5910 cexpr "Xfile1:10:aa"
5911 copen
5912 call setqflist(['bb'], 'a')
5913 call assert_equal(1, line('$'))
5914 call assert_equal(['Xfile1|10| aa'], getline(1, '$'))
shane.xb.qian0d5e1ec2021-06-20 16:31:00 +02005915 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())
5916
5917 call setqflist([{'lnum': 10 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5918 call assert_equal(1 , line('$'))
5919 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5920 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())
5921
5922 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')
5923 call assert_equal(1 , line('$'))
5924 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5925 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())
5926
5927 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')
5928 call assert_equal(1 , line('$'))
5929 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5930 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())
5931
5932 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')
5933 call assert_equal(1 , line('$'))
5934 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5935 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())
5936
5937 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')
5938 call assert_equal(1 , line('$'))
5939 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5940 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())
5941
5942 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')
5943 call assert_equal(1 , line('$'))
5944 call assert_equal(['Xfile1|10 col 666-222| aa'] , getline(1 , '$'))
5945 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())
5946
5947 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')
5948 call assert_equal(1 , line('$'))
5949 call assert_equal(['Xfile1|10-6 col 666-222| aa'] , getline(1 , '$'))
5950 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 +01005951 cclose
5952endfunc
5953
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01005954" Test for very weird problem: autocommand causes a failure, resulting opening
5955" the quickfix window to fail. This still splits the window, but otherwise
5956" should not mess up buffers.
5957func Test_quickfix_window_fails_to_open()
5958 CheckScreendump
5959
5960 let lines =<< trim END
5961 anything
5962 try
5963 anything
5964 endtry
5965 END
5966 call writefile(lines, 'XquickfixFails')
5967
5968 let lines =<< trim END
5969 split XquickfixFails
5970 silent vimgrep anything %
5971 normal o
5972 au BufLeave * ++once source XquickfixFails
5973 " This will trigger the autocommand, which causes an error, what follows
5974 " is aborted but the window was already split.
5975 silent! cwindow
5976 END
5977 call writefile(lines, 'XtestWinFails')
5978 let buf = RunVimInTerminal('-S XtestWinFails', #{rows: 13})
5979 call VerifyScreenDump(buf, 'Test_quickfix_window_fails', {})
5980
5981 " clean up
5982 call term_sendkeys(buf, ":bwipe!\<CR>")
5983 call term_wait(buf)
5984 call StopVimInTerminal(buf)
5985 call delete('XtestWinFails')
5986 call delete('XquickfixFails')
5987endfunc
5988
Bram Moolenaar2d870f82020-12-05 13:41:01 +01005989" Test for updating the quickfix buffer whenever the associated quickfix list
Bram Moolenaar287153c2020-11-29 14:20:27 +01005990" is changed.
5991func Xqfbuf_update(cchar)
5992 call s:setup_commands(a:cchar)
5993
5994 Xexpr "F1:1:line1"
5995 Xopen
5996 call assert_equal(['F1|1| line1'], getline(1, '$'))
5997 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5998
5999 " Test setqflist() using the 'lines' key in 'what'
6000 " add a new entry
6001 call g:Xsetlist([], 'a', {'lines' : ['F2:2: line2']})
6002 call assert_equal(['F1|1| line1', 'F2|2| line2'], getline(1, '$'))
6003 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6004 " replace all the entries with a single entry
6005 call g:Xsetlist([], 'r', {'lines' : ['F3:3: line3']})
6006 call assert_equal(['F3|3| line3'], getline(1, '$'))
6007 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6008 " remove all the entries
6009 call g:Xsetlist([], 'r', {'lines' : []})
6010 call assert_equal([''], getline(1, '$'))
6011 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6012 " add a new list
6013 call g:Xsetlist([], ' ', {'lines' : ['F4:4: line4']})
6014 call assert_equal(['F4|4| line4'], getline(1, '$'))
6015 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6016
6017 " Test setqflist() using the 'items' key in 'what'
6018 " add a new entry
6019 call g:Xsetlist([], 'a', {'items' : [{'filename' : 'F5', 'lnum' : 5, 'text' : 'line5'}]})
6020 call assert_equal(['F4|4| line4', 'F5|5| line5'], getline(1, '$'))
6021 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6022 " replace all the entries with a single entry
6023 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F6', 'lnum' : 6, 'text' : 'line6'}]})
6024 call assert_equal(['F6|6| line6'], getline(1, '$'))
6025 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6026 " remove all the entries
6027 call g:Xsetlist([], 'r', {'items' : []})
6028 call assert_equal([''], getline(1, '$'))
6029 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6030 " add a new list
6031 call g:Xsetlist([], ' ', {'items' : [{'filename' : 'F7', 'lnum' : 7, 'text' : 'line7'}]})
6032 call assert_equal(['F7|7| line7'], getline(1, '$'))
6033 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6034
6035 call g:Xsetlist([], ' ', {})
6036 call assert_equal([''], getline(1, '$'))
6037 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6038
6039 Xclose
6040endfunc
6041
6042func Test_qfbuf_update()
6043 call Xqfbuf_update('c')
6044 call Xqfbuf_update('l')
6045endfunc
6046
Bram Moolenaar8c801b32021-03-05 20:58:22 +01006047func Test_vimgrep_noswapfile()
6048 set noswapfile
6049 call writefile(['one', 'two', 'three'], 'Xgreppie')
6050 vimgrep two Xgreppie
6051 call assert_equal('two', getline('.'))
6052
6053 call delete('Xgreppie')
6054 set swapfile
6055endfunc
6056
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006057" Test for the :vimgrep 'f' flag (fuzzy match)
6058func Xvimgrep_fuzzy_match(cchar)
6059 call s:setup_commands(a:cchar)
6060
6061 Xvimgrep /three one/f Xfile*
6062 let l = g:Xgetlist()
6063 call assert_equal(2, len(l))
6064 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6065 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6066 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6067 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6068
6069 Xvimgrep /the/f Xfile*
6070 let l = g:Xgetlist()
6071 call assert_equal(3, len(l))
6072 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6073 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6074 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6075 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6076 call assert_equal(['Xfile2', 4, 4, 'aaathreeaaa'],
6077 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6078
6079 Xvimgrep /aaa/fg Xfile*
6080 let l = g:Xgetlist()
6081 call assert_equal(4, len(l))
6082 call assert_equal(['Xfile1', 2, 1, 'aaaaaa'],
6083 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6084 call assert_equal(['Xfile1', 2, 4, 'aaaaaa'],
6085 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6086 call assert_equal(['Xfile2', 4, 1, 'aaathreeaaa'],
6087 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6088 call assert_equal(['Xfile2', 4, 9, 'aaathreeaaa'],
6089 \ [bufname(l[3].bufnr), l[3].lnum, l[3].col, l[3].text])
6090
6091 call assert_fails('Xvimgrep /xyz/fg Xfile*', 'E480:')
6092endfunc
6093
6094func Test_vimgrep_fuzzy_match()
6095 call writefile(['one two three', 'aaaaaa'], 'Xfile1')
6096 call writefile(['one', 'three one two', 'two', 'aaathreeaaa'], 'Xfile2')
6097 call Xvimgrep_fuzzy_match('c')
6098 call Xvimgrep_fuzzy_match('l')
6099 call delete('Xfile1')
6100 call delete('Xfile2')
6101endfunc
6102
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006103func Test_locationlist_open_in_newtab()
6104 call s:create_test_file('Xqftestfile1')
6105 call s:create_test_file('Xqftestfile2')
6106 call s:create_test_file('Xqftestfile3')
6107
6108 %bwipe!
6109
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01006110 let lines =<< trim END
6111 Xqftestfile1:5:Line5
6112 Xqftestfile2:10:Line10
6113 Xqftestfile3:16:Line16
6114 END
6115 lgetexpr lines
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006116
6117 silent! llast
6118 call assert_equal(1, tabpagenr('$'))
6119 call assert_equal('Xqftestfile3', bufname())
6120
6121 set switchbuf=newtab
6122
6123 silent! lfirst
6124 call assert_equal(2, tabpagenr('$'))
6125 call assert_equal('Xqftestfile1', bufname())
6126
6127 silent! lnext
6128 call assert_equal(3, tabpagenr('$'))
6129 call assert_equal('Xqftestfile2', bufname())
6130
6131 call delete('Xqftestfile1')
6132 call delete('Xqftestfile2')
6133 call delete('Xqftestfile3')
6134 set switchbuf&vim
6135
6136 %bwipe!
6137endfunc
6138
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02006139" Test for win_gettype() in quickfix and location list windows
6140func Test_win_gettype()
6141 copen
6142 call assert_equal("quickfix", win_gettype())
6143 let wid = win_getid()
6144 wincmd p
6145 call assert_equal("quickfix", win_gettype(wid))
6146 cclose
6147 lexpr ''
6148 lopen
6149 call assert_equal("loclist", win_gettype())
6150 let wid = win_getid()
6151 wincmd p
6152 call assert_equal("loclist", win_gettype(wid))
6153 lclose
6154endfunc
6155
Christian Brabandt0b226f62021-12-01 10:54:24 +00006156fun Test_vimgrep_nomatch()
6157 call XexprTests('c')
6158 call g:Xsetlist([{'lnum':10,'text':'Line1'}])
6159 copen
6160 if has("win32")
6161 call assert_fails('vimgrep foo *.zzz', 'E479:')
6162 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'}]
6163 else
6164 call assert_fails('vimgrep foo *.zzz', 'E480:')
6165 let expected = []
6166 endif
6167 call assert_equal(expected, getqflist())
6168 cclose
6169endfunc
6170
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00006171" Test for opening the quickfix window in two tab pages and then closing one
6172" of the quickfix windows. This should not make the quickfix buffer unlisted.
6173" (github issue #9300).
6174func Test_two_qf_windows()
6175 cexpr "F1:1:line1"
6176 copen
6177 tabnew
6178 copen
6179 call assert_true(&buflisted)
6180 cclose
6181 tabfirst
6182 call assert_true(&buflisted)
6183 let bnum = bufnr()
6184 cclose
6185 " if all the quickfix windows are closed, then buffer should be unlisted.
6186 call assert_false(buflisted(bnum))
6187 %bw!
6188
6189 " Repeat the test for a location list
6190 lexpr "F2:2:line2"
6191 lopen
6192 let bnum = bufnr()
6193 tabnew
6194 exe "buffer" bnum
6195 tabfirst
6196 lclose
6197 tablast
6198 call assert_true(buflisted(bnum))
6199 tabclose
6200 lopen
6201 call assert_true(buflisted(bnum))
6202 lclose
6203 call assert_false(buflisted(bnum))
6204 %bw!
6205endfunc
6206
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006207" Weird sequence of commands that caused entering a wiped-out buffer
6208func Test_lopen_bwipe()
6209 func R()
6210 silent! tab lopen
6211 e x
6212 silent! lfile
6213 endfunc
6214
6215 cal R()
6216 cal R()
6217 cal R()
6218 bw!
6219 delfunc R
6220endfunc
6221
Bram Moolenaare3537ae2022-02-08 15:05:20 +00006222" Another sequence of commands that caused all buffers to be wiped out
6223func Test_lopen_bwipe_all()
6224 let lines =<< trim END
6225 func R()
6226 silent! tab lopen
6227 e foo
6228 silent! lfile
6229 endfunc
6230 cal R()
6231 exe "norm \<C-W>\<C-V>0"
6232 cal R()
6233 bwipe
6234
6235 call writefile(['done'], 'Xresult')
6236 qall!
6237 END
6238 call writefile(lines, 'Xscript')
6239 if RunVim([], [], '-u NONE -n -X -Z -e -m -s -S Xscript')
6240 call assert_equal(['done'], readfile('Xresult'))
6241 endif
6242
6243 call delete('Xscript')
6244 call delete('Xresult')
6245endfunc
6246
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00006247" Test for calling setqflist() function recursively
6248func Test_recursive_setqflist()
6249 augroup QF_Test
6250 au!
6251 autocmd BufWinEnter quickfix call setqflist([], 'r')
6252 augroup END
6253
6254 copen
6255 call assert_fails("call setqflist([], 'a')", 'E952:')
6256
6257 augroup QF_Test
6258 au!
6259 augroup END
6260 %bw!
6261endfunc
6262
6263" Test for failure to create a new window when selecting a file from the
6264" quickfix window
6265func Test_cwindow_newwin_fails()
6266 cgetexpr ["Xfile1:10:L10", "Xfile1:20:L20"]
6267 cwindow
6268 only
6269 let qf_wid = win_getid()
6270 " create the maximum number of scratch windows
6271 let hor_win_count = (&lines - 1)/2
6272 let hor_split_count = hor_win_count - 1
6273 for s in range(1, hor_split_count) | new | set buftype=nofile | endfor
6274 call win_gotoid(qf_wid)
6275 call assert_fails('exe "normal \<CR>"', 'E36:')
6276 %bw!
6277endfunc
6278
6279" Test for updating the location list when only the location list window is
6280" present and the corresponding file window is closed.
6281func Test_loclist_update_with_llwin_only()
6282 %bw!
6283 new
6284 wincmd w
6285 lexpr ["Xfile1:1:Line1"]
6286 lopen
6287 wincmd p
6288 close
6289 call setloclist(2, [], 'r', {'lines': ["Xtest2:2:Line2"]})
6290 call assert_equal(['Xtest2|2| Line2'], getbufline(winbufnr(2), 1, '$'))
6291 %bw!
6292endfunc
6293
6294" Test for getting the quickfix list after a buffer with an error is wiped out
6295func Test_getqflist_wiped_out_buffer()
6296 %bw!
6297 cexpr ["Xtest1:34:Wiped out"]
6298 let bnum = bufnr('Xtest1')
6299 call assert_equal(bnum, getqflist()[0].bufnr)
6300 bw Xtest1
6301 call assert_equal(0, getqflist()[0].bufnr)
6302 %bw!
6303endfunc
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006304
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00006305" Test for the status message that is displayed when opening a new quickfix
6306" list
6307func Test_qflist_statusmsg()
6308 cexpr "1\n2"
6309 cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"
6310 call assert_equal('(4 of 4): msg', v:statusmsg)
6311 call setqflist([], 'f')
6312 %bw!
6313
6314 " When creating a new quickfix list, if an autocmd changes the quickfix list
6315 " in the stack, then an error message should be displayed.
6316 augroup QF_Test
6317 au!
6318 au BufEnter test_quickfix.vim colder
6319 augroup END
6320 cexpr "1\n2"
6321 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6322 call setqflist([], 'f')
6323 augroup QF_Test
6324 au!
6325 augroup END
6326 %bw!
6327
6328 augroup QF_Test
6329 au!
6330 au BufEnter test_quickfix.vim caddexpr "4"
6331 augroup END
6332 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6333 call setqflist([], 'f')
6334 augroup QF_Test
6335 au!
6336 augroup END
6337 %bw!
6338endfunc
6339
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006340" vim: shiftwidth=2 sts=2 expandtab