blob: 6970bdcd507fec241ecd5a4b07f2c0f5564bf2ba [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
3366 cgetexpr ['Entering directory ' . repeat('a', 1006),
3367 \ 'File1:10:Hello World']
3368 set efm&vim
3369endfunc
3370
Bram Moolenaar875feea2017-06-11 16:07:51 +02003371" Tests for getting the quickfix stack size
3372func XsizeTests(cchar)
3373 call s:setup_commands(a:cchar)
3374
3375 call g:Xsetlist([], 'f')
3376 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003377 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
3378 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02003379
3380 Xexpr "File1:10:Line1"
3381 Xexpr "File2:20:Line2"
3382 Xexpr "File3:30:Line3"
3383 Xolder | Xolder
3384 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
3385 call g:Xsetlist([], 'f')
3386
3387 Xexpr "File1:10:Line1"
3388 Xexpr "File2:20:Line2"
3389 Xexpr "File3:30:Line3"
3390 Xolder | Xolder
3391 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
3392 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
3393endfunc
3394
3395func Test_Qf_Size()
3396 call XsizeTests('c')
3397 call XsizeTests('l')
3398endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02003399
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003400func Test_cclose_from_copen()
3401 augroup QF_Test
3402 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003403 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003404 augroup END
3405 copen
3406 augroup QF_Test
3407 au!
3408 augroup END
3409 augroup! QF_Test
3410endfunc
3411
Bram Moolenaar18141832017-06-25 21:17:25 +02003412func Test_cclose_in_autocmd()
3413 " Problem is only triggered if "starting" is zero, so that the OptionsSet
3414 " event will be triggered.
3415 call test_override('starting', 1)
3416 augroup QF_Test
3417 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003418 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar18141832017-06-25 21:17:25 +02003419 augroup END
3420 copen
3421 augroup QF_Test
3422 au!
3423 augroup END
3424 augroup! QF_Test
3425 call test_override('starting', 0)
3426endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003427
Bram Moolenaar379fb762018-08-30 15:58:28 +02003428" Check that ":file" without an argument is possible even when "curbuf_lock"
3429" is set.
3430func Test_file_from_copen()
3431 " Works without argument.
3432 augroup QF_Test
3433 au!
3434 au FileType qf file
3435 augroup END
3436 copen
3437
3438 augroup QF_Test
3439 au!
3440 augroup END
3441 cclose
3442
3443 " Fails with argument.
3444 augroup QF_Test
3445 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003446 au FileType qf call assert_fails(':file foo', 'E788:')
Bram Moolenaar379fb762018-08-30 15:58:28 +02003447 augroup END
3448 copen
3449 augroup QF_Test
3450 au!
3451 augroup END
3452 cclose
3453
3454 augroup! QF_Test
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02003455endfunc
Bram Moolenaar379fb762018-08-30 15:58:28 +02003456
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003457func Test_resize_from_copen()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003458 augroup QF_Test
3459 au!
3460 au FileType qf resize 5
3461 augroup END
3462 try
3463 " This should succeed without any exception. No other buffers are
3464 " involved in the autocmd.
3465 copen
3466 finally
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003467 augroup QF_Test
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003468 au!
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003469 augroup END
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003470 augroup! QF_Test
3471 endtry
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003472endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02003473
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003474func Test_vimgrep_with_textlock()
3475 new
3476
3477 " Simple way to execute something with "textwinlock" set.
3478 " Check that vimgrep without jumping can be executed.
3479 au InsertCharPre * vimgrep /RunTheTest/j runtest.vim
3480 normal ax
3481 let qflist = getqflist()
3482 call assert_true(len(qflist) > 0)
3483 call assert_match('RunTheTest', qflist[0].text)
3484 call setqflist([], 'r')
3485 au! InsertCharPre
3486
3487 " Check that vimgrepadd without jumping can be executed.
3488 au InsertCharPre * vimgrepadd /RunTheTest/j runtest.vim
3489 normal ax
3490 let qflist = getqflist()
3491 call assert_true(len(qflist) > 0)
3492 call assert_match('RunTheTest', qflist[0].text)
3493 call setqflist([], 'r')
3494 au! InsertCharPre
3495
3496 " Check that lvimgrep without jumping can be executed.
3497 au InsertCharPre * lvimgrep /RunTheTest/j runtest.vim
3498 normal ax
3499 let qflist = getloclist(0)
3500 call assert_true(len(qflist) > 0)
3501 call assert_match('RunTheTest', qflist[0].text)
3502 call setloclist(0, [], 'r')
3503 au! InsertCharPre
3504
3505 " Check that lvimgrepadd without jumping can be executed.
3506 au InsertCharPre * lvimgrepadd /RunTheTest/j runtest.vim
3507 normal ax
3508 let qflist = getloclist(0)
3509 call assert_true(len(qflist) > 0)
3510 call assert_match('RunTheTest', qflist[0].text)
3511 call setloclist(0, [], 'r')
3512 au! InsertCharPre
3513
3514 " trying to jump will give an error
3515 au InsertCharPre * vimgrep /RunTheTest/ runtest.vim
3516 call assert_fails('normal ax', 'E565:')
3517 au! InsertCharPre
3518
3519 au InsertCharPre * vimgrepadd /RunTheTest/ runtest.vim
3520 call assert_fails('normal ax', 'E565:')
3521 au! InsertCharPre
3522
3523 au InsertCharPre * lvimgrep /RunTheTest/ runtest.vim
3524 call assert_fails('normal ax', 'E565:')
3525 au! InsertCharPre
3526
3527 au InsertCharPre * lvimgrepadd /RunTheTest/ runtest.vim
3528 call assert_fails('normal ax', 'E565:')
3529 au! InsertCharPre
3530
3531 bwipe!
3532endfunc
3533
Bram Moolenaara8788f42017-07-19 17:06:20 +02003534" Tests for the quickfix buffer b:changedtick variable
3535func Xchangedtick_tests(cchar)
3536 call s:setup_commands(a:cchar)
3537
3538 new | only
3539
3540 Xexpr "" | Xexpr "" | Xexpr ""
3541
3542 Xopen
3543 Xolder
3544 Xolder
3545 Xaddexpr "F1:10:Line10"
3546 Xaddexpr "F2:20:Line20"
3547 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
3548 call g:Xsetlist([], 'f')
3549 call assert_equal(8, getbufvar('%', 'changedtick'))
3550 Xclose
3551endfunc
3552
3553func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02003554 call Xchangedtick_tests('c')
3555 call Xchangedtick_tests('l')
3556endfunc
3557
3558" Tests for parsing an expression using setqflist()
3559func Xsetexpr_tests(cchar)
3560 call s:setup_commands(a:cchar)
3561
3562 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003563 call g:Xsetlist([], ' ', {'lines' : t})
3564 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003565
3566 let l = g:Xgetlist()
3567 call assert_equal(3, len(l))
3568 call assert_equal(20, l[1].lnum)
3569 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003570 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003571 let l = g:Xgetlist()
3572 call assert_equal(1, len(l))
3573 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003574 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
3575 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003576
3577 call g:Xsetlist([], 'f')
3578 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003579 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
3580 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
3581 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
3582 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003583 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
3584 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02003585
3586 " Adding entries using a custom efm
3587 set efm&
3588 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
3589 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
3590 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3591 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
3592 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
3593 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3594 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
3595 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003596endfunc
3597
3598func Test_setexpr()
3599 call Xsetexpr_tests('c')
3600 call Xsetexpr_tests('l')
3601endfunc
3602
3603" Tests for per quickfix/location list directory stack
3604func Xmultidirstack_tests(cchar)
3605 call s:setup_commands(a:cchar)
3606
3607 call g:Xsetlist([], 'f')
3608 Xexpr "" | Xexpr ""
3609
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003610 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
3611 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
3612 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
3613 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003614
3615 let l1 = g:Xgetlist({'nr':1, 'items':1})
3616 let l2 = g:Xgetlist({'nr':2, 'items':1})
3617 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
3618 call assert_equal(3, l1.items[1].lnum)
3619 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
3620 call assert_equal(5, l2.items[1].lnum)
3621endfunc
3622
3623func Test_multidirstack()
3624 call mkdir('Xone/a', 'p')
3625 call mkdir('Xtwo/a', 'p')
3626 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3627 call writefile(lines, 'Xone/a/one.txt')
3628 call writefile(lines, 'Xtwo/a/two.txt')
3629 let save_efm = &efm
3630 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
3631
3632 call Xmultidirstack_tests('c')
3633 call Xmultidirstack_tests('l')
3634
3635 let &efm = save_efm
3636 call delete('Xone', 'rf')
3637 call delete('Xtwo', 'rf')
3638endfunc
3639
3640" Tests for per quickfix/location list file stack
3641func Xmultifilestack_tests(cchar)
3642 call s:setup_commands(a:cchar)
3643
3644 call g:Xsetlist([], 'f')
3645 Xexpr "" | Xexpr ""
3646
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003647 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
3648 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
3649 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
3650 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003651
3652 let l1 = g:Xgetlist({'nr':1, 'items':1})
3653 let l2 = g:Xgetlist({'nr':2, 'items':1})
3654 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
3655 call assert_equal(3, l1.items[1].lnum)
3656 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
3657 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02003658
3659 " Test for start of a new error line in the same line where a previous
3660 " error line ends with a file stack.
3661 let efm_val = 'Error\ l%l\ in\ %f,'
3662 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003663 let lines =<< trim END
3664 (one.txt
3665 Error l4 in one.txt
3666 ) (two.txt
3667 Error l6 in two.txt
3668 )
3669 Error l8 in one.txt
3670 END
3671 let l = g:Xgetlist({'lines': lines, 'efm' : efm_val})
Bram Moolenaare333e792018-04-08 13:27:39 +02003672 call assert_equal(3, len(l.items))
3673 call assert_equal('one.txt', bufname(l.items[0].bufnr))
3674 call assert_equal(4, l.items[0].lnum)
3675 call assert_equal('one.txt', l.items[0].text)
3676 call assert_equal('two.txt', bufname(l.items[1].bufnr))
3677 call assert_equal(6, l.items[1].lnum)
3678 call assert_equal('two.txt', l.items[1].text)
3679 call assert_equal('one.txt', bufname(l.items[2].bufnr))
3680 call assert_equal(8, l.items[2].lnum)
3681 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02003682endfunc
3683
3684func Test_multifilestack()
3685 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3686 call writefile(lines, 'one.txt')
3687 call writefile(lines, 'two.txt')
3688 let save_efm = &efm
3689 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
3690
3691 call Xmultifilestack_tests('c')
3692 call Xmultifilestack_tests('l')
3693
3694 let &efm = save_efm
3695 call delete('one.txt')
3696 call delete('two.txt')
3697endfunc
3698
3699" Tests for per buffer 'efm' setting
3700func Test_perbuf_efm()
3701 call writefile(["File1-10-Line10"], 'one.txt')
3702 call writefile(["File2#20#Line20"], 'two.txt')
3703 set efm=%f#%l#%m
3704 new | only
3705 new
3706 setlocal efm=%f-%l-%m
3707 cfile one.txt
3708 wincmd w
3709 caddfile two.txt
3710
3711 let l = getqflist()
3712 call assert_equal(10, l[0].lnum)
3713 call assert_equal('Line20', l[1].text)
3714
3715 set efm&
3716 new | only
3717 call delete('one.txt')
3718 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02003719endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003720
3721" Open multiple help windows using ":lhelpgrep
3722" This test used to crash Vim
3723func Test_Multi_LL_Help()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003724 new | only
3725 lhelpgrep window
3726 lopen
3727 e#
3728 lhelpgrep buffer
3729 call assert_equal(3, winnr('$'))
3730 call assert_true(len(getloclist(1)) != 0)
3731 call assert_true(len(getloclist(2)) != 0)
3732 new | only
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003733endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02003734
3735" Tests for adding new quickfix lists using setqflist()
3736func XaddQf_tests(cchar)
3737 call s:setup_commands(a:cchar)
3738
3739 " Create a new list using ' ' for action
3740 call g:Xsetlist([], 'f')
3741 call g:Xsetlist([], ' ', {'title' : 'Test1'})
3742 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3743 call assert_equal(1, l.nr)
3744 call assert_equal('Test1', l.title)
3745
3746 " Create a new list using ' ' for action and '$' for 'nr'
3747 call g:Xsetlist([], 'f')
3748 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
3749 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3750 call assert_equal(1, l.nr)
3751 call assert_equal('Test2', l.title)
3752
3753 " Create a new list using 'a' for action
3754 call g:Xsetlist([], 'f')
3755 call g:Xsetlist([], 'a', {'title' : 'Test3'})
3756 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3757 call assert_equal(1, l.nr)
3758 call assert_equal('Test3', l.title)
3759
3760 " Create a new list using 'a' for action and '$' for 'nr'
3761 call g:Xsetlist([], 'f')
3762 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
3763 call g:Xsetlist([], 'a', {'title' : 'Test4'})
3764 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3765 call assert_equal(1, l.nr)
3766 call assert_equal('Test4', l.title)
3767
3768 " Adding a quickfix list should remove all the lists following the current
3769 " list.
3770 Xexpr "" | Xexpr "" | Xexpr ""
3771 silent! 10Xolder
3772 call g:Xsetlist([], ' ', {'title' : 'Test5'})
3773 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3774 call assert_equal(2, l.nr)
3775 call assert_equal('Test5', l.title)
3776
3777 " Add a quickfix list using '$' as the list number.
3778 let lastqf = g:Xgetlist({'nr':'$'}).nr
3779 silent! 99Xolder
3780 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
3781 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3782 call assert_equal(lastqf + 1, l.nr)
3783 call assert_equal('Test6', l.title)
3784
3785 " Add a quickfix list using 'nr' set to one more than the quickfix
3786 " list size.
3787 let lastqf = g:Xgetlist({'nr':'$'}).nr
3788 silent! 99Xolder
3789 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
3790 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3791 call assert_equal(lastqf + 1, l.nr)
3792 call assert_equal('Test7', l.title)
3793
3794 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
3795 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
3796 silent! 99Xolder
3797 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
3798 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3799 call assert_equal(10, l.nr)
3800 call assert_equal('Test8', l.title)
3801
3802 " Add a quickfix list using 'nr' set to a value greater than 10
3803 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
3804
3805 " Try adding a quickfix list with 'nr' set to a value greater than the
3806 " quickfix list size but less than 10.
3807 call g:Xsetlist([], 'f')
3808 Xexpr "" | Xexpr "" | Xexpr ""
3809 silent! 99Xolder
3810 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
3811
3812 " Add a quickfix list using 'nr' set to a some string or list
3813 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
3814endfunc
3815
3816func Test_add_qf()
3817 call XaddQf_tests('c')
3818 call XaddQf_tests('l')
3819endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003820
3821" Test for getting the quickfix list items from some text without modifying
3822" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003823func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003824 call s:setup_commands(a:cchar)
3825 call g:Xsetlist([], 'f')
3826
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003827 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003828 call assert_equal(2, len(l))
3829 call assert_equal(30, l[1].lnum)
3830
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003831 call assert_equal({}, g:Xgetlist({'lines' : 10}))
3832 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
3833 call assert_equal([], g:Xgetlist({'lines' : []}).items)
3834 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003835
Bram Moolenaar36538222017-09-02 19:51:44 +02003836 " Parse text using a custom efm
3837 set efm&
3838 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
3839 call assert_equal('Line30', l[0].text)
3840 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
3841 call assert_equal('File3:30:Line30', l[0].text)
3842 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
3843 call assert_equal({}, l)
3844 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
3845 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
3846
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003847 " Make sure that the quickfix stack is not modified
3848 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
3849endfunc
3850
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003851func Test_get_list_from_lines()
3852 call XgetListFromLines('c')
3853 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003854endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003855
3856" Tests for the quickfix list id
3857func Xqfid_tests(cchar)
3858 call s:setup_commands(a:cchar)
3859
3860 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003861 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003862 Xexpr ''
3863 let start_id = g:Xgetlist({'id' : 0}).id
3864 Xexpr '' | Xexpr ''
3865 Xolder
3866 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
3867 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
3868 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003869 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003870 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003871 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3872 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003873
3874 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3875 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003876 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003877 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3878 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3879 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3880
3881 let qfid = g:Xgetlist({'id':0, 'nr':0})
3882 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003883 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003884endfunc
3885
3886func Test_qf_id()
3887 call Xqfid_tests('c')
3888 call Xqfid_tests('l')
3889endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003890
3891func Xqfjump_tests(cchar)
3892 call s:setup_commands(a:cchar)
3893
3894 call writefile(["Line1\tFoo", "Line2"], 'F1')
3895 call writefile(["Line1\tBar", "Line2"], 'F2')
3896 call writefile(["Line1\tBaz", "Line2"], 'F3')
3897
3898 call g:Xsetlist([], 'f')
3899
3900 " Tests for
3901 " Jumping to a line using a pattern
3902 " Jumping to a column greater than the last column in a line
3903 " Jumping to a line greater than the last line in the file
3904 let l = []
3905 for i in range(1, 7)
3906 call add(l, {})
3907 endfor
3908 let l[0].filename='F1'
3909 let l[0].pattern='Line1'
3910 let l[1].filename='F2'
3911 let l[1].pattern='Line1'
3912 let l[2].filename='F3'
3913 let l[2].pattern='Line1'
3914 let l[3].filename='F3'
3915 let l[3].lnum=1
3916 let l[3].col=9
3917 let l[3].vcol=1
3918 let l[4].filename='F3'
3919 let l[4].lnum=99
3920 let l[5].filename='F3'
3921 let l[5].lnum=1
3922 let l[5].col=99
3923 let l[5].vcol=1
3924 let l[6].filename='F3'
3925 let l[6].pattern='abcxyz'
3926
3927 call g:Xsetlist([], ' ', {'items' : l})
3928 Xopen | only
3929 2Xnext
3930 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003931 call assert_equal('F3', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +01003932 Xnext
3933 call assert_equal(7, col('.'))
3934 Xnext
3935 call assert_equal(2, line('.'))
3936 Xnext
3937 call assert_equal(9, col('.'))
3938 2
3939 Xnext
3940 call assert_equal(2, line('.'))
3941
3942 if a:cchar == 'l'
3943 " When jumping to a location list entry in the location list window and
3944 " no usable windows are available, then a new window should be opened.
3945 enew! | new | only
3946 call g:Xsetlist([], 'f')
3947 setlocal buftype=nofile
3948 new
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003949 let lines =<< trim END
3950 F1:1:1:Line1
3951 F1:2:2:Line2
3952 F2:1:1:Line1
3953 F2:2:2:Line2
3954 F3:1:1:Line1
3955 F3:2:2:Line2
3956 END
3957 call g:Xsetlist([], ' ', {'lines': lines})
Bram Moolenaar74240d32017-12-10 15:26:15 +01003958 Xopen
3959 let winid = win_getid()
3960 wincmd p
3961 close
3962 call win_gotoid(winid)
3963 Xnext
3964 call assert_equal(3, winnr('$'))
3965 call assert_equal(1, winnr())
3966 call assert_equal(2, line('.'))
3967
3968 " When jumping to an entry in the location list window and the window
3969 " associated with the location list is not present and a window containing
3970 " the file is already present, then that window should be used.
3971 close
3972 belowright new
3973 call g:Xsetlist([], 'f')
3974 edit F3
3975 call win_gotoid(winid)
3976 Xlast
3977 call assert_equal(3, winnr())
3978 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3979 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3980 endif
3981
3982 " Cleanup
3983 enew!
3984 new | only
3985
3986 call delete('F1')
3987 call delete('F2')
3988 call delete('F3')
3989endfunc
3990
3991func Test_qfjump()
3992 call Xqfjump_tests('c')
3993 call Xqfjump_tests('l')
3994endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01003995
3996" Tests for the getqflist() and getloclist() functions when the list is not
3997" present or is empty
3998func Xgetlist_empty_tests(cchar)
3999 call s:setup_commands(a:cchar)
4000
4001 " Empty quickfix stack
4002 call g:Xsetlist([], 'f')
4003 call assert_equal('', g:Xgetlist({'context' : 0}).context)
4004 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
4005 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
4006 call assert_equal([], g:Xgetlist({'items' : 0}).items)
4007 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
4008 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
4009 call assert_equal('', g:Xgetlist({'title' : 0}).title)
4010 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004011 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004012 if a:cchar == 'c'
4013 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004014 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004015 \ 'title' : '', 'winid' : 0, 'changedtick': 0,
4016 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004017 else
4018 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
4019 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004020 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004021 \ 'qfbufnr' : 0, 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004022 \ g:Xgetlist({'all' : 0}))
4023 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004024
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004025 " Quickfix window with empty stack
4026 silent! Xopen
4027 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004028 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004029 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
4030 Xclose
4031
Bram Moolenaara6d48492017-12-12 22:45:31 +01004032 " Empty quickfix list
4033 Xexpr ""
4034 call assert_equal('', g:Xgetlist({'context' : 0}).context)
4035 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
4036 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
4037 call assert_equal([], g:Xgetlist({'items' : 0}).items)
4038 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
4039 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
4040 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
4041 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004042 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01004043
4044 let qfid = g:Xgetlist({'id' : 0}).id
4045 call g:Xsetlist([], 'f')
4046
4047 " Non-existing quickfix identifier
4048 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
4049 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
4050 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
4051 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
4052 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
4053 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
4054 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
4055 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004056 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004057 if a:cchar == 'c'
4058 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4059 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004060 \ 'qfbufnr' : qfbufnr, 'quickfixtextfunc' : '',
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004061 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
4062 else
4063 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4064 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004065 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4066 \ 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004067 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
4068 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004069
4070 " Non-existing quickfix list number
4071 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
4072 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
4073 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
4074 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
4075 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
4076 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
4077 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
4078 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004079 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004080 if a:cchar == 'c'
4081 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4082 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004083 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr,
4084 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004085 else
4086 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4087 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004088 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4089 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004090 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004091endfunc
4092
4093func Test_getqflist()
4094 call Xgetlist_empty_tests('c')
4095 call Xgetlist_empty_tests('l')
4096endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01004097
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01004098func Test_getqflist_invalid_nr()
4099 " The following commands used to crash Vim
4100 cexpr ""
4101 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
4102
4103 " Cleanup
4104 call setqflist([], 'r')
4105endfunc
4106
Bram Moolenaarb254af32017-12-18 19:48:58 +01004107" Tests for the quickfix/location list changedtick
4108func Xqftick_tests(cchar)
4109 call s:setup_commands(a:cchar)
4110
4111 call g:Xsetlist([], 'f')
4112
4113 Xexpr "F1:10:Line10"
4114 let qfid = g:Xgetlist({'id' : 0}).id
4115 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4116 Xaddexpr "F2:20:Line20\nF2:21:Line21"
4117 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4118 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
4119 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
4120 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
4121 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
4122 call g:Xsetlist([], 'a', {'title' : 'New Title'})
4123 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
4124
4125 enew!
4126 call append(0, ["F5:50:L50", "F6:60:L60"])
4127 Xaddbuffer
4128 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
4129 enew!
4130
4131 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
4132 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
4133 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4134 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
4135 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
4136 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4137 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
4138 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4139 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4140 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
4141 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4142
4143 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
4144 Xfile Xone
4145 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4146 Xaddfile Xone
4147 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4148
4149 " Test case for updating a non-current quickfix list
4150 call g:Xsetlist([], 'f')
4151 Xexpr "F1:1:L1"
4152 Xexpr "F2:2:L2"
4153 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
4154 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4155 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
4156
4157 call delete("Xone")
4158endfunc
4159
4160func Test_qf_tick()
4161 call Xqftick_tests('c')
4162 call Xqftick_tests('l')
4163endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01004164
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02004165" Test helpgrep with lang specifier
4166func Xtest_helpgrep_with_lang_specifier(cchar)
4167 call s:setup_commands(a:cchar)
4168 Xhelpgrep Vim@en
4169 call assert_equal('help', &filetype)
4170 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
4171 new | only
4172endfunc
4173
4174func Test_helpgrep_with_lang_specifier()
4175 call Xtest_helpgrep_with_lang_specifier('c')
4176 call Xtest_helpgrep_with_lang_specifier('l')
4177endfunc
4178
Bram Moolenaar12237442017-12-19 12:38:52 +01004179" The following test used to crash Vim.
4180" Open the location list window and close the regular window associated with
4181" the location list. When the garbage collection runs now, it incorrectly
4182" marks the location list context as not in use and frees the context.
4183func Test_ll_window_ctx()
4184 call setloclist(0, [], 'f')
4185 call setloclist(0, [], 'a', {'context' : []})
4186 lopen | only
4187 call test_garbagecollect_now()
4188 echo getloclist(0, {'context' : 1}).context
4189 enew | only
4190endfunc
4191
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004192" The following test used to crash vim
4193func Test_lfile_crash()
4194 sp Xtest
4195 au QuickFixCmdPre * bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02004196 call assert_fails('lfile', 'E40:')
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004197 au! QuickFixCmdPre
4198endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01004199
4200" The following test used to crash vim
4201func Test_lbuffer_crash()
4202 sv Xtest
4203 augroup QF_Test
4204 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004205 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bw
Bram Moolenaar3c097222017-12-21 20:54:49 +01004206 augroup END
4207 lbuffer
4208 augroup QF_Test
4209 au!
4210 augroup END
4211endfunc
4212
4213" The following test used to crash vim
4214func Test_lexpr_crash()
4215 augroup QF_Test
4216 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004217 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004218 augroup END
4219 lexpr ""
4220 augroup QF_Test
4221 au!
4222 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004223
Bram Moolenaar3c097222017-12-21 20:54:49 +01004224 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004225 augroup QF_Test
4226 au!
4227 au BufNew * call setloclist(0, [], 'f')
4228 augroup END
4229 lexpr 'x:1:x'
4230 augroup QF_Test
4231 au!
4232 augroup END
4233
4234 enew | only
4235 lexpr ''
4236 lopen
4237 augroup QF_Test
4238 au!
4239 au FileType * call setloclist(0, [], 'f')
4240 augroup END
4241 lexpr ''
4242 augroup QF_Test
4243 au!
4244 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01004245endfunc
4246
4247" The following test used to crash Vim
4248func Test_lvimgrep_crash()
4249 sv Xtest
4250 augroup QF_Test
4251 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004252 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004253 augroup END
4254 lvimgrep quickfix test_quickfix.vim
4255 augroup QF_Test
4256 au!
4257 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004258
4259 new | only
4260 augroup QF_Test
4261 au!
4262 au BufEnter * call setloclist(0, [], 'r')
4263 augroup END
4264 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
4265 augroup QF_Test
4266 au!
4267 augroup END
4268
Bram Moolenaar3c097222017-12-21 20:54:49 +01004269 enew | only
4270endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01004271
Bram Moolenaar2573af32020-03-14 17:21:34 +01004272func Test_lvimgrep_crash2()
4273 au BufNewFile x sfind
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004274 call assert_fails('lvimgrep x x', 'E471:')
4275 call assert_fails('lvimgrep x x x', 'E471:')
Bram Moolenaar2573af32020-03-14 17:21:34 +01004276
4277 au! BufNewFile
4278endfunc
4279
Bram Moolenaarde046542017-12-26 13:53:11 +01004280" Test for the position of the quickfix and location list window
4281func Test_qfwin_pos()
4282 " Open two windows
4283 new | only
4284 new
4285 cexpr ['F1:10:L10']
4286 copen
4287 " Quickfix window should be the bottom most window
4288 call assert_equal(3, winnr())
4289 close
4290 " Open at the very top
4291 wincmd t
4292 topleft copen
4293 call assert_equal(1, winnr())
4294 close
4295 " open left of the current window
4296 wincmd t
4297 below new
4298 leftabove copen
4299 call assert_equal(2, winnr())
4300 close
4301 " open right of the current window
4302 rightbelow copen
4303 call assert_equal(3, winnr())
4304 close
4305endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004306
4307" Tests for quickfix/location lists changed by autocommands when
4308" :vimgrep/:lvimgrep commands are running.
4309func Test_vimgrep_autocmd()
4310 call setqflist([], 'f')
4311 call writefile(['stars'], 'Xtest1.txt')
4312 call writefile(['stars'], 'Xtest2.txt')
4313
4314 " Test 1:
4315 " When searching for a pattern using :vimgrep, if the quickfix list is
4316 " changed by an autocmd, the results should be added to the correct quickfix
4317 " list.
4318 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
4319 silent vimgrep stars Xtest*.txt
4320 call assert_equal(1, getqflist({'nr' : 0}).nr)
4321 call assert_equal(3, getqflist({'nr' : '$'}).nr)
4322 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
4323 au! BufRead Xtest2.txt
4324
4325 " Test 2:
4326 " When searching for a pattern using :vimgrep, if the quickfix list is
4327 " freed, then a error should be given.
4328 silent! %bwipe!
4329 call setqflist([], 'f')
4330 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
4331 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
4332 au! BufRead Xtest2.txt
4333
4334 " Test 3:
4335 " When searching for a pattern using :lvimgrep, if the location list is
4336 " freed, then the command should error out.
4337 silent! %bwipe!
4338 let g:save_winid = win_getid()
4339 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
4340 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
4341 au! BufRead Xtest2.txt
4342
4343 call delete('Xtest1.txt')
4344 call delete('Xtest2.txt')
4345 call setqflist([], 'f')
4346endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004347
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004348" Test for an autocmd changing the current directory when running vimgrep
4349func Xvimgrep_autocmd_cd(cchar)
4350 call s:setup_commands(a:cchar)
4351
4352 %bwipe
4353 let save_cwd = getcwd()
4354
4355 augroup QF_Test
4356 au!
4357 autocmd BufRead * silent cd %:p:h
4358 augroup END
4359
4360 10Xvimgrep /vim/ Xdir/**
4361 let l = g:Xgetlist()
4362 call assert_equal('f1.txt', bufname(l[0].bufnr))
4363 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
4364
4365 augroup QF_Test
4366 au!
4367 augroup END
4368
4369 exe 'cd ' . save_cwd
4370endfunc
4371
4372func Test_vimgrep_autocmd_cd()
4373 call mkdir('Xdir/a', 'p')
4374 call mkdir('Xdir/b', 'p')
4375 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xdir/a/f1.txt')
4376 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xdir/b/f2.txt')
4377 call Xvimgrep_autocmd_cd('c')
4378 call Xvimgrep_autocmd_cd('l')
4379 %bwipe
4380 call delete('Xdir', 'rf')
4381endfunc
4382
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004383" The following test used to crash Vim
4384func Test_lhelpgrep_autocmd()
4385 lhelpgrep quickfix
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004386 augroup QF_Test
4387 au!
4388 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
4389 augroup END
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004390 lhelpgrep buffer
4391 call assert_equal('help', &filetype)
4392 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
4393 lhelpgrep tabpage
4394 call assert_equal('help', &filetype)
4395 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004396 augroup QF_Test
4397 au!
4398 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004399
4400 new | only
4401 augroup QF_Test
4402 au!
4403 au BufEnter * call setqflist([], 'f')
4404 augroup END
4405 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaardbfa7952020-11-02 20:04:22 +01004406 " run the test with a help window already open
4407 help
4408 wincmd w
4409 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004410 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004411 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004412 augroup END
4413
4414 new | only
4415 augroup QF_Test
4416 au!
4417 au BufEnter * call setqflist([], 'r')
4418 augroup END
4419 call assert_fails('helpgrep quickfix', 'E925:')
4420 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004421 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004422 augroup END
4423
4424 new | only
4425 augroup QF_Test
4426 au!
4427 au BufEnter * call setloclist(0, [], 'r')
4428 augroup END
4429 call assert_fails('lhelpgrep quickfix', 'E926:')
4430 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004431 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004432 augroup END
4433
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004434 " Replace the contents of a help window location list when it is still in
4435 " use.
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004436 new | only
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004437 lhelpgrep quickfix
4438 wincmd w
4439 augroup QF_Test
4440 au!
4441 autocmd WinEnter * call setloclist(0, [], 'r')
4442 augroup END
4443 call assert_fails('lhelpgrep win_getid', 'E926:')
4444 augroup QF_Test
4445 au!
4446 augroup END
4447
4448 %bw!
4449endfunc
4450
4451" The following test used to crash Vim
4452func Test_lhelpgrep_autocmd_free_loclist()
4453 %bw!
4454 lhelpgrep quickfix
4455 wincmd w
4456 augroup QF_Test
4457 au!
4458 autocmd WinEnter * call setloclist(0, [], 'f')
4459 augroup END
4460 lhelpgrep win_getid
4461 wincmd w
4462 wincmd w
4463 wincmd w
4464 augroup QF_Test
4465 au!
4466 augroup END
4467 %bw!
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004468endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02004469
4470" Test for shortening/simplifying the file name when opening the
4471" quickfix window or when displaying the quickfix list
4472func Test_shorten_fname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02004473 CheckUnix
Bram Moolenaara796d462018-05-01 14:30:36 +02004474 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004475 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004476 let fname = getcwd() . '/test_quickfix.vim'
4477 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4478 call assert_equal(fname, bufname('test_quickfix.vim'))
4479 " Opening the quickfix window should simplify the file path
4480 cwindow
4481 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
4482 cclose
4483 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004484 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004485 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4486 call assert_equal(fname, bufname('test_quickfix.vim'))
4487 " Displaying the quickfix list should simplify the file path
4488 silent! clist
4489 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004490 " Add a few entries for the same file with different paths and check whether
4491 " the buffer name is shortened
4492 %bwipe
4493 call setqflist([], 'f')
4494 call setqflist([{'filename' : 'test_quickfix.vim', 'lnum' : 10},
4495 \ {'filename' : '../testdir/test_quickfix.vim', 'lnum' : 20},
4496 \ {'filename' : fname, 'lnum' : 30}], ' ')
4497 copen
4498 call assert_equal(['test_quickfix.vim|10| ',
4499 \ 'test_quickfix.vim|20| ',
4500 \ 'test_quickfix.vim|30| '], getline(1, '$'))
4501 cclose
Bram Moolenaara796d462018-05-01 14:30:36 +02004502endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004503
4504" Quickfix title tests
4505" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
4506" Otherwise due to indentation, the title is set with spaces at the beginning
4507" of the command.
4508func Test_qftitle()
4509 call writefile(["F1:1:Line1"], 'Xerr')
4510
4511 " :cexpr
4512 exe "cexpr readfile('Xerr')"
4513 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
4514
4515 " :cgetexpr
4516 exe "cgetexpr readfile('Xerr')"
4517 call assert_equal(":cgetexpr readfile('Xerr')",
4518 \ getqflist({'title' : 1}).title)
4519
4520 " :caddexpr
4521 call setqflist([], 'f')
4522 exe "caddexpr readfile('Xerr')"
4523 call assert_equal(":caddexpr readfile('Xerr')",
4524 \ getqflist({'title' : 1}).title)
4525
4526 " :cbuffer
4527 new Xerr
4528 exe "cbuffer"
4529 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
4530
4531 " :cgetbuffer
4532 edit Xerr
4533 exe "cgetbuffer"
4534 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
4535
4536 " :caddbuffer
4537 call setqflist([], 'f')
4538 edit Xerr
4539 exe "caddbuffer"
4540 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
4541
4542 " :cfile
4543 exe "cfile Xerr"
4544 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
4545
4546 " :cgetfile
4547 exe "cgetfile Xerr"
4548 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
4549
4550 " :caddfile
4551 call setqflist([], 'f')
4552 exe "caddfile Xerr"
4553 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
4554
4555 " :grep
4556 set grepprg=internal
4557 exe "grep F1 Xerr"
4558 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
4559
4560 " :grepadd
4561 call setqflist([], 'f')
4562 exe "grepadd F1 Xerr"
4563 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
4564 set grepprg&vim
4565
4566 " :vimgrep
4567 exe "vimgrep F1 Xerr"
4568 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
4569
4570 " :vimgrepadd
4571 call setqflist([], 'f')
4572 exe "vimgrepadd F1 Xerr"
4573 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
4574
4575 call setqflist(['F1:10:L10'], ' ')
4576 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4577
4578 call setqflist([], 'f')
4579 call setqflist(['F1:10:L10'], 'a')
4580 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4581
4582 call setqflist([], 'f')
4583 call setqflist(['F1:10:L10'], 'r')
4584 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4585
4586 close
4587 call delete('Xerr')
4588
4589 call setqflist([], ' ', {'title' : 'Errors'})
4590 copen
4591 call assert_equal('Errors', w:quickfix_title)
4592 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
4593 call assert_equal('Errors', w:quickfix_title)
4594 cclose
Bram Moolenaar530bed92020-12-16 21:02:56 +01004595
4596 " Switching to another quickfix list in one tab page should update the
4597 " quickfix window title and statusline in all the other tab pages also
4598 call setqflist([], 'f')
4599 %bw!
4600 cgetexpr ['file_one:1:1: error in the first quickfix list']
4601 call setqflist([], 'a', {'title': 'first quickfix list'})
4602 cgetexpr ['file_two:2:1: error in the second quickfix list']
4603 call setqflist([], 'a', {'title': 'second quickfix list'})
4604 copen
4605 wincmd t
4606 tabnew two
4607 copen
4608 wincmd t
4609 colder
4610 call assert_equal('first quickfix list', gettabwinvar(1, 2, 'quickfix_title'))
4611 call assert_equal('first quickfix list', gettabwinvar(2, 2, 'quickfix_title'))
4612 call assert_equal(1, tabpagewinnr(1))
4613 call assert_equal(1, tabpagewinnr(2))
4614 tabnew
4615 call setqflist([], 'a', {'title': 'new quickfix title'})
4616 call assert_equal('new quickfix title', gettabwinvar(1, 2, 'quickfix_title'))
4617 call assert_equal('new quickfix title', gettabwinvar(2, 2, 'quickfix_title'))
4618 %bw!
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004619endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02004620
4621func Test_lbuffer_with_bwipe()
4622 new
4623 new
4624 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004625 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bwipe
Bram Moolenaar600323b2018-06-16 22:16:47 +02004626 augroup END
4627 lbuffer
4628 augroup nasty
4629 au!
4630 augroup END
4631endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02004632
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004633" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
4634" running
4635func Xexpr_acmd_freelist(cchar)
4636 call s:setup_commands(a:cchar)
4637
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004638 " This was using freed memory (but with what events?)
Bram Moolenaar0366c012018-06-18 20:52:13 +02004639 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004640 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02004641 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004642 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02004643 augroup nasty
4644 au!
4645 augroup END
4646endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004647
4648func Test_cexpr_acmd_freelist()
4649 call Xexpr_acmd_freelist('c')
4650 call Xexpr_acmd_freelist('l')
4651endfunc
4652
4653" Test for commands that create a new quickfix/location list and jump to the
4654" first error automatically.
4655func Xjumpto_first_error_test(cchar)
4656 call s:setup_commands(a:cchar)
4657
4658 call s:create_test_file('Xtestfile1')
4659 call s:create_test_file('Xtestfile2')
4660 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4661
4662 " Test for cexpr/lexpr
4663 enew
4664 Xexpr l
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004665 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004666 call assert_equal(2, line('.'))
4667
4668 " Test for cfile/lfile
4669 enew
4670 call writefile(l, 'Xerr')
4671 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004672 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004673 call assert_equal(2, line('.'))
4674
4675 " Test for cbuffer/lbuffer
4676 edit Xerr
4677 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004678 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004679 call assert_equal(2, line('.'))
4680
4681 call delete('Xerr')
4682 call delete('Xtestfile1')
4683 call delete('Xtestfile2')
4684endfunc
4685
4686func Test_jumpto_first_error()
4687 call Xjumpto_first_error_test('c')
4688 call Xjumpto_first_error_test('l')
4689endfunc
4690
4691" Test for a quickfix autocmd changing the quickfix/location list before
4692" jumping to the first error in the new list.
4693func Xautocmd_changelist(cchar)
4694 call s:setup_commands(a:cchar)
4695
4696 " Test for cfile/lfile
4697 call s:create_test_file('Xtestfile1')
4698 call s:create_test_file('Xtestfile2')
4699 Xexpr 'Xtestfile1:2:Line2'
4700 autocmd QuickFixCmdPost * Xolder
4701 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4702 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004703 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004704 call assert_equal(4, line('.'))
4705 autocmd! QuickFixCmdPost
4706
4707 " Test for cbuffer/lbuffer
4708 call g:Xsetlist([], 'f')
4709 Xexpr 'Xtestfile1:2:Line2'
4710 autocmd QuickFixCmdPost * Xolder
4711 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4712 edit Xerr
4713 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004714 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004715 call assert_equal(4, line('.'))
4716 autocmd! QuickFixCmdPost
4717
4718 " Test for cexpr/lexpr
4719 call g:Xsetlist([], 'f')
4720 Xexpr 'Xtestfile1:2:Line2'
4721 autocmd QuickFixCmdPost * Xolder
4722 Xexpr 'Xtestfile2:4:Line4'
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004723 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004724 call assert_equal(4, line('.'))
4725 autocmd! QuickFixCmdPost
4726
Bram Moolenaar851332e2018-07-03 19:16:00 +02004727 " The grepprg may not be set on non-Unix systems
4728 if has('unix')
4729 " Test for grep/lgrep
4730 call g:Xsetlist([], 'f')
4731 Xexpr 'Xtestfile1:2:Line2'
4732 autocmd QuickFixCmdPost * Xolder
4733 silent Xgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004734 call assert_equal('Xtestfile2', @%)
Bram Moolenaar851332e2018-07-03 19:16:00 +02004735 call assert_equal(5, line('.'))
4736 autocmd! QuickFixCmdPost
4737 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004738
4739 " Test for vimgrep/lvimgrep
4740 call g:Xsetlist([], 'f')
4741 Xexpr 'Xtestfile1:2:Line2'
4742 autocmd QuickFixCmdPost * Xolder
4743 silent Xvimgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004744 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004745 call assert_equal(5, line('.'))
4746 autocmd! QuickFixCmdPost
4747
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004748 " Test for autocommands clearing the quickfix list before jumping to the
4749 " first error. This should not result in an error
4750 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
4751 let v:errmsg = ''
4752 " Test for cfile/lfile
4753 Xfile Xerr
4754 call assert_true(v:errmsg !~# 'E42:')
4755 " Test for cbuffer/lbuffer
4756 edit Xerr
4757 Xbuffer
4758 call assert_true(v:errmsg !~# 'E42:')
4759 " Test for cexpr/lexpr
4760 Xexpr 'Xtestfile2:4:Line4'
4761 call assert_true(v:errmsg !~# 'E42:')
4762 " Test for grep/lgrep
4763 " The grepprg may not be set on non-Unix systems
4764 if has('unix')
4765 silent Xgrep Line5 Xtestfile2
4766 call assert_true(v:errmsg !~# 'E42:')
4767 endif
4768 " Test for vimgrep/lvimgrep
4769 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
4770 autocmd! QuickFixCmdPost
4771
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004772 call delete('Xerr')
4773 call delete('Xtestfile1')
4774 call delete('Xtestfile2')
4775endfunc
4776
4777func Test_autocmd_changelist()
4778 call Xautocmd_changelist('c')
4779 call Xautocmd_changelist('l')
4780endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004781
4782" Tests for the ':filter /pat/ clist' command
4783func Test_filter_clist()
4784 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4785 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4786 \ split(execute('filter /Line 15/ clist'), "\n"))
4787 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4788 \ split(execute('filter /Xfile1/ clist'), "\n"))
4789 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4790
4791 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4792 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4793 call assert_equal([' 2 pqr:pat2: '],
4794 \ split(execute('filter /pqr/ clist'), "\n"))
4795 call assert_equal([' 1 abc:pat1: '],
4796 \ split(execute('filter /pat1/ clist'), "\n"))
4797endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004798
4799" Tests for the "CTRL-W <CR>" command.
4800func Xview_result_split_tests(cchar)
4801 call s:setup_commands(a:cchar)
4802
4803 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4804 call g:Xsetlist([])
4805 Xopen
4806 let l:win_count = winnr('$')
Bram Moolenaare2e40752020-09-04 21:18:46 +02004807 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42:')
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004808 call assert_equal(l:win_count, winnr('$'))
4809 Xclose
4810endfunc
4811
4812func Test_view_result_split()
4813 call Xview_result_split_tests('c')
4814 call Xview_result_split_tests('l')
4815endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004816
4817" Test that :cc sets curswant
4818func Test_curswant()
4819 helpgrep quickfix
4820 normal! llll
4821 1cc
4822 call assert_equal(getcurpos()[4], virtcol('.'))
4823 cclose | helpclose
4824endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004825
4826" Test for opening a file from the quickfix window using CTRL-W <Enter>
4827" doesn't leave an empty buffer around.
4828func Test_splitview()
4829 call s:create_test_file('Xtestfile1')
4830 call s:create_test_file('Xtestfile2')
4831 new | only
4832 let last_bufnr = bufnr('Test_sv_1', 1)
4833 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4834 cgetexpr l
4835 copen
4836 let numbufs = len(getbufinfo())
4837 exe "normal \<C-W>\<CR>"
4838 copen
4839 exe "normal j\<C-W>\<CR>"
4840 " Make sure new empty buffers are not created
4841 call assert_equal(numbufs, len(getbufinfo()))
4842 " Creating a new buffer should use the next available buffer number
4843 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4844 bwipe Test_sv_1
4845 bwipe Test_sv_2
4846 new | only
4847
4848 " When split opening files from location list window, make sure that two
4849 " windows doesn't refer to the same location list
4850 lgetexpr l
4851 let locid = getloclist(0, {'id' : 0}).id
4852 lopen
4853 exe "normal \<C-W>\<CR>"
4854 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4855 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4856 new | only
4857
4858 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004859 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004860 lhelpgrep window
4861 let locid = getloclist(0, {'id' : 0}).id
4862 lwindow
4863 exe "normal j\<C-W>\<CR>"
4864 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4865 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4866 new | only
4867
Bram Moolenaar406cd902020-02-18 21:54:41 +01004868 " Using :split or :vsplit from a quickfix window should behave like a :new
4869 " or a :vnew command
4870 copen
4871 split
4872 call assert_equal(3, winnr('$'))
4873 let l = getwininfo()
4874 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4875 close
4876 copen
4877 vsplit
4878 let l = getwininfo()
4879 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4880 new | only
4881
Bram Moolenaarb2443732018-11-11 22:50:27 +01004882 call delete('Xtestfile1')
4883 call delete('Xtestfile2')
4884endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004885
4886" Test for parsing entries using visual screen column
4887func Test_viscol()
4888 enew
4889 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4890 edit Xfile1
4891
4892 " Use byte offset for column number
4893 set efm&
4894 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
4895 call assert_equal([5, 8], [col('.'), virtcol('.')])
4896 cnext
4897 call assert_equal([9, 12], [col('.'), virtcol('.')])
4898 cnext
4899 call assert_equal([14, 20], [col('.'), virtcol('.')])
4900
4901 " Use screen column offset for column number
4902 set efm=%f:%l:%v:%m
4903 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4904 call assert_equal([5, 8], [col('.'), virtcol('.')])
4905 cnext
4906 call assert_equal([9, 12], [col('.'), virtcol('.')])
4907 cnext
4908 call assert_equal([14, 20], [col('.'), virtcol('.')])
4909 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4910 call assert_equal([5, 8], [col('.'), virtcol('.')])
4911 cnext
4912 call assert_equal([10, 16], [col('.'), virtcol('.')])
4913 cnext
4914 call assert_equal([14, 20], [col('.'), virtcol('.')])
4915
4916 enew
4917 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4918
4919 " Use byte offset for column number
4920 set efm&
4921 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
4922 call assert_equal([8, 10], [col('.'), virtcol('.')])
4923 cnext
4924 call assert_equal([11, 17], [col('.'), virtcol('.')])
4925 cnext
4926 call assert_equal([16, 25], [col('.'), virtcol('.')])
4927
4928 " Use screen column offset for column number
4929 set efm=%f:%l:%v:%m
4930 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4931 call assert_equal([8, 10], [col('.'), virtcol('.')])
4932 cnext
4933 call assert_equal([11, 17], [col('.'), virtcol('.')])
4934 cnext
4935 call assert_equal([16, 25], [col('.'), virtcol('.')])
4936
Bram Moolenaarc95940c2020-10-20 14:59:12 +02004937 " Use screen column number with a multi-line error message
4938 enew
4939 call writefile(["à test"], 'Xfile1')
4940 set efm=%E===\ %f\ ===,%C%l:%v,%Z%m
4941 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4942 call assert_equal('Xfile1', @%)
4943 call assert_equal([0, 1, 4, 0], getpos('.'))
4944
4945 " Repeat previous test with byte offset %c: ensure that fix to issue #7145
4946 " does not break this
4947 set efm=%E===\ %f\ ===,%C%l:%c,%Z%m
4948 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4949 call assert_equal('Xfile1', @%)
4950 call assert_equal([0, 1, 3, 0], getpos('.'))
4951
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004952 enew | only
4953 set efm&
4954 call delete('Xfile1')
4955endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004956
4957" Test for the quickfix window buffer
4958func Xqfbuf_test(cchar)
4959 call s:setup_commands(a:cchar)
4960
4961 " Quickfix buffer should be reused across closing and opening a quickfix
4962 " window
4963 Xexpr "F1:10:Line10"
4964 Xopen
4965 let qfbnum = bufnr('')
4966 Xclose
4967 " Even after the quickfix window is closed, the buffer should be loaded
4968 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004969 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004970 Xopen
4971 " Buffer should be reused when opening the window again
4972 call assert_equal(qfbnum, bufnr(''))
4973 Xclose
4974
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00004975 " When quickfix buffer is wiped out, getqflist() should return 0
4976 %bw!
4977 Xexpr ""
4978 Xopen
4979 bw!
4980 call assert_equal(0, g:Xgetlist({'qfbufnr': 0}).qfbufnr)
4981
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004982 if a:cchar == 'l'
4983 %bwipe
4984 " For a location list, when both the file window and the location list
4985 " window for the list are closed, then the buffer should be freed.
4986 new | only
4987 lexpr "F1:10:Line10"
4988 let wid = win_getid()
4989 lopen
4990 let qfbnum = bufnr('')
4991 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
4992 close
4993 " When the location list window is closed, the buffer name should not
4994 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004995 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004996 call assert_true(bufloaded(qfbnum))
4997
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004998 " After deleting a location list buffer using ":bdelete", opening the
4999 " location list window should mark the buffer as a location list buffer.
5000 exe "bdelete " . qfbnum
5001 lopen
5002 call assert_equal("quickfix", &buftype)
5003 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
5004 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
5005 call assert_false(&swapfile)
5006 lclose
5007
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005008 " When the location list is cleared for the window, the buffer should be
5009 " removed
5010 call setloclist(0, [], 'f')
5011 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005012 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005013
5014 " When the location list is freed with the location list window open, the
5015 " location list buffer should not be lost. It should be reused when the
5016 " location list is again populated.
5017 lexpr "F1:10:Line10"
5018 lopen
5019 let wid = win_getid()
5020 let qfbnum = bufnr('')
5021 wincmd p
5022 call setloclist(0, [], 'f')
5023 lexpr "F1:10:Line10"
5024 lopen
5025 call assert_equal(wid, win_getid())
5026 call assert_equal(qfbnum, bufnr(''))
5027 lclose
5028
5029 " When the window with the location list is closed, the buffer should be
5030 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005031 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005032 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005033 endif
5034endfunc
5035
5036func Test_qfbuf()
5037 call Xqfbuf_test('c')
5038 call Xqfbuf_test('l')
5039endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005040
5041" If there is an autocmd to use only one window, then opening the location
5042" list window used to crash Vim.
5043func Test_winonly_autocmd()
5044 call s:create_test_file('Xtest1')
5045 " Autocmd to show only one Vim window at a time
5046 autocmd WinEnter * only
5047 new
5048 " Load the location list
5049 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
5050 let loclistid = getloclist(0, {'id' : 0}).id
5051 " Open the location list window. Only this window will be shown and the file
5052 " window is closed.
5053 lopen
5054 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
5055 " Jump to an entry in the location list and make sure that the cursor is
5056 " positioned correctly.
5057 ll 3
5058 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005059 call assert_equal('Xtest1', @%)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005060 call assert_equal(15, line('.'))
5061 " Cleanup
5062 autocmd! WinEnter
5063 new | only
5064 call delete('Xtest1')
5065endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02005066
5067" Test to make sure that an empty quickfix buffer is not reused for loading
5068" a normal buffer.
5069func Test_empty_qfbuf()
5070 enew | only
5071 call writefile(["Test"], 'Xfile1')
5072 call setqflist([], 'f')
5073 copen | only
5074 let qfbuf = bufnr('')
5075 edit Xfile1
5076 call assert_notequal(qfbuf, bufnr(''))
5077 enew
5078 call delete('Xfile1')
5079endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005080
5081" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005082" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005083func Xtest_below(cchar)
5084 call s:setup_commands(a:cchar)
5085
5086 " No quickfix/location list
5087 call assert_fails('Xbelow', 'E42:')
5088 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005089 call assert_fails('Xbefore', 'E42:')
5090 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005091
5092 " Empty quickfix/location list
5093 call g:Xsetlist([])
5094 call assert_fails('Xbelow', 'E42:')
5095 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005096 call assert_fails('Xbefore', 'E42:')
5097 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005098
5099 call s:create_test_file('X1')
5100 call s:create_test_file('X2')
5101 call s:create_test_file('X3')
5102 call s:create_test_file('X4')
5103
5104 " Invalid entries
5105 edit X1
5106 call g:Xsetlist(["E1", "E2"])
5107 call assert_fails('Xbelow', 'E42:')
5108 call assert_fails('Xabove', 'E42:')
5109 call assert_fails('3Xbelow', 'E42:')
5110 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005111 call assert_fails('Xbefore', 'E42:')
5112 call assert_fails('Xafter', 'E42:')
5113 call assert_fails('3Xbefore', 'E42:')
5114 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005115
5116 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005117 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 +02005118 edit +7 X2
5119 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005120 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005121 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005122 normal 7G
5123 Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005124 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005125 call assert_fails('Xbefore', 'E553:')
5126
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005127 normal 2j
5128 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005129 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005130 normal 7G
5131 Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005132 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005133
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005134 " Last error in this file
5135 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005136 call assert_equal(['X2', 15], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005137 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005138 normal gg
5139 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005140 call assert_equal(['X2', 15, 4], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005141 call assert_fails('Xafter', 'E553:')
5142
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005143 " First error in this file
5144 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005145 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005146 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005147 normal G
5148 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005149 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005150 call assert_fails('Xbefore', 'E553:')
5151
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005152 normal gg
5153 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005154 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005155 normal gg
5156 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005157 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005158
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005159 normal G
5160 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005161 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005162 normal G
5163 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005164 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005165
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005166 edit X4
5167 call assert_fails('Xabove', 'E42:')
5168 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005169 call assert_fails('Xbefore', 'E42:')
5170 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005171 if a:cchar == 'l'
5172 " If a buffer has location list entries from some other window but not
5173 " from the current window, then the commands should fail.
5174 edit X1 | split | call setloclist(0, [], 'f')
5175 call assert_fails('Xabove', 'E776:')
5176 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005177 call assert_fails('Xbefore', 'E776:')
5178 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005179 close
5180 endif
5181
5182 " Test for lines with multiple quickfix entries
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005183 let lines =<< trim END
5184 X1:5:L5
5185 X2:5:1:L5_1
5186 X2:5:2:L5_2
5187 X2:5:3:L5_3
5188 X2:10:1:L10_1
5189 X2:10:2:L10_2
5190 X2:10:3:L10_3
5191 X2:15:1:L15_1
5192 X2:15:2:L15_2
5193 X2:15:3:L15_3
5194 X3:3:L3
5195 END
5196 Xexpr lines
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005197 edit +1 X2
5198 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005199 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005200 normal 1G
5201 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005202 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005203
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005204 normal gg
5205 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005206 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005207 normal gg
5208 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005209 call assert_equal(['X2', 15, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005210
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005211 normal G
5212 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005213 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005214 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005215 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005216 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005217
5218 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005219 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005220 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005221 normal G
5222 Xbefore 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
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005225 normal 10G
5226 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005227 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005228 normal 10G$
5229 2Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005230 call assert_equal(['X2', 10, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005231
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005232 normal 10G
5233 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005234 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005235 normal 9G
5236 5Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005237 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005238
5239 " Invalid range
5240 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02005241 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005242 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005243 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02005244 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005245 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005246 endif
5247
5248 call delete('X1')
5249 call delete('X2')
5250 call delete('X3')
5251 call delete('X4')
5252endfunc
5253
5254func Test_cbelow()
5255 call Xtest_below('c')
5256 call Xtest_below('l')
5257endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02005258
5259func Test_quickfix_count()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005260 let commands =<< trim END
5261 cNext
5262 cNfile
5263 cabove
5264 cbelow
5265 cfirst
5266 clast
5267 cnewer
5268 cnext
5269 cnfile
5270 colder
5271 cprevious
5272 crewind
5273 lNext
5274 lNfile
5275 labove
5276 lbelow
5277 lfirst
5278 llast
5279 lnewer
5280 lnext
5281 lnfile
5282 lolder
5283 lprevious
5284 lrewind
5285 END
Bram Moolenaar25190db2019-05-04 15:05:28 +02005286 for cmd in commands
5287 call assert_fails('-1' .. cmd, 'E16:')
5288 call assert_fails('.' .. cmd, 'E16:')
5289 call assert_fails('%' .. cmd, 'E16:')
5290 call assert_fails('$' .. cmd, 'E16:')
5291 endfor
5292endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005293
5294" Test for aborting quickfix commands using QuickFixCmdPre
5295func Xtest_qfcmd_abort(cchar)
5296 call s:setup_commands(a:cchar)
5297
5298 call g:Xsetlist([], 'f')
5299
5300 " cexpr/lexpr
5301 let e = ''
5302 try
5303 Xexpr ["F1:10:Line10", "F2:20:Line20"]
5304 catch /.*/
5305 let e = v:exception
5306 endtry
5307 call assert_equal('AbortCmd', e)
5308 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5309
5310 " cfile/lfile
5311 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
5312 let e = ''
5313 try
5314 Xfile Xfile1
5315 catch /.*/
5316 let e = v:exception
5317 endtry
5318 call assert_equal('AbortCmd', e)
5319 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5320 call delete('Xfile1')
5321
5322 " cgetbuffer/lgetbuffer
5323 enew!
5324 call append(0, ["F1:10:Line10", "F2:20:Line20"])
5325 let e = ''
5326 try
5327 Xgetbuffer
5328 catch /.*/
5329 let e = v:exception
5330 endtry
5331 call assert_equal('AbortCmd', e)
5332 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5333 enew!
5334
5335 " vimgrep/lvimgrep
5336 let e = ''
5337 try
5338 Xvimgrep /func/ test_quickfix.vim
5339 catch /.*/
5340 let e = v:exception
5341 endtry
5342 call assert_equal('AbortCmd', e)
5343 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5344
5345 " helpgrep/lhelpgrep
5346 let e = ''
5347 try
5348 Xhelpgrep quickfix
5349 catch /.*/
5350 let e = v:exception
5351 endtry
5352 call assert_equal('AbortCmd', e)
5353 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5354
5355 " grep/lgrep
5356 if has('unix')
5357 let e = ''
5358 try
5359 silent Xgrep func test_quickfix.vim
5360 catch /.*/
5361 let e = v:exception
5362 endtry
5363 call assert_equal('AbortCmd', e)
5364 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5365 endif
5366endfunc
5367
5368func Test_qfcmd_abort()
5369 augroup QF_Test
5370 au!
5371 autocmd QuickFixCmdPre * throw "AbortCmd"
5372 augroup END
5373
5374 call Xtest_qfcmd_abort('c')
5375 call Xtest_qfcmd_abort('l')
5376
5377 augroup QF_Test
5378 au!
5379 augroup END
5380endfunc
5381
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005382" Test for using a file in one of the parent directories.
5383func Test_search_in_dirstack()
5384 call mkdir('Xtestdir/a/b/c', 'p')
5385 let save_cwd = getcwd()
5386 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
5387 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
5388 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
5389 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
5390
5391 let lines = "Entering dir Xtestdir\n" .
5392 \ "Entering dir a\n" .
5393 \ "Entering dir b\n" .
5394 \ "Xfile2:2:X2_L2\n" .
5395 \ "Leaving dir a\n" .
5396 \ "Xfile1:2:X1_L2\n" .
5397 \ "Xfile3:1:X3_L1\n" .
5398 \ "Entering dir c\n" .
5399 \ "Xfile4:2:X4_L2\n" .
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005400 \ "Leaving dir c\n"
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005401 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005402 cexpr lines .. "Leaving dir Xtestdir|\n" | let next = 1
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005403 call assert_equal(11, getqflist({'size' : 0}).size)
5404 call assert_equal(4, getqflist({'idx' : 0}).idx)
5405 call assert_equal('X2_L2', getline('.'))
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005406 call assert_equal(1, next)
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005407 cnext
5408 call assert_equal(6, getqflist({'idx' : 0}).idx)
5409 call assert_equal('X1_L2', getline('.'))
5410 cnext
5411 call assert_equal(7, getqflist({'idx' : 0}).idx)
5412 call assert_equal(1, line('$'))
5413 call assert_equal('', getline(1))
5414 cnext
5415 call assert_equal(9, getqflist({'idx' : 0}).idx)
5416 call assert_equal(1, line('$'))
5417 call assert_equal('', getline(1))
5418
5419 set efm&
5420 exe 'cd ' . save_cwd
5421 call delete('Xtestdir', 'rf')
5422endfunc
5423
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005424" Test for :cquit
5425func Test_cquit()
5426 " Exit Vim with a non-zero value
5427 if RunVim([], ["cquit 7"], '')
5428 call assert_equal(7, v:shell_error)
5429 endif
5430
5431 if RunVim([], ["50cquit"], '')
5432 call assert_equal(50, v:shell_error)
5433 endif
5434
5435 " Exit Vim with default value
5436 if RunVim([], ["cquit"], '')
5437 call assert_equal(1, v:shell_error)
5438 endif
5439
5440 " Exit Vim with zero value
5441 if RunVim([], ["cquit 0"], '')
5442 call assert_equal(0, v:shell_error)
5443 endif
5444
5445 " Exit Vim with negative value
5446 call assert_fails('-3cquit', 'E16:')
5447endfunc
5448
Bram Moolenaar858ba062020-05-31 23:11:59 +02005449" Test for getting a specific item from a quickfix list
5450func Xtest_getqflist_by_idx(cchar)
5451 call s:setup_commands(a:cchar)
5452 " Empty list
5453 call assert_equal([], g:Xgetlist({'idx' : 1, 'items' : 0}).items)
5454 Xexpr ['F1:10:L10', 'F1:20:L20']
5455 let l = g:Xgetlist({'idx' : 2, 'items' : 0}).items
5456 call assert_equal(bufnr('F1'), l[0].bufnr)
5457 call assert_equal(20, l[0].lnum)
5458 call assert_equal('L20', l[0].text)
5459 call assert_equal([], g:Xgetlist({'idx' : -1, 'items' : 0}).items)
5460 call assert_equal([], g:Xgetlist({'idx' : 3, 'items' : 0}).items)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005461 call assert_equal({}, g:Xgetlist(#{idx: "abc"}))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005462 %bwipe!
5463endfunc
5464
5465func Test_getqflist_by_idx()
5466 call Xtest_getqflist_by_idx('c')
5467 call Xtest_getqflist_by_idx('l')
5468endfunc
5469
5470" Test for the 'quickfixtextfunc' setting
5471func Tqfexpr(info)
5472 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005473 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005474 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005475 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005476 endif
5477
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005478 let l = []
5479 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5480 let e = qfl[idx]
5481 let s = ''
5482 if e.bufnr != 0
5483 let bname = bufname(e.bufnr)
5484 let s ..= fnamemodify(bname, ':.')
5485 endif
5486 let s ..= '-'
5487 let s ..= 'L' .. string(e.lnum) .. 'C' .. string(e.col) .. '-'
5488 let s ..= e.text
5489 call add(l, s)
5490 endfor
Bram Moolenaar858ba062020-05-31 23:11:59 +02005491
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005492 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005493endfunc
5494
5495func Xtest_qftextfunc(cchar)
5496 call s:setup_commands(a:cchar)
5497
5498 set efm=%f:%l:%c:%m
5499 set quickfixtextfunc=Tqfexpr
Bram Moolenaard43906d2020-07-20 21:31:32 +02005500 call assert_equal('Tqfexpr', &quickfixtextfunc)
5501 call assert_equal('',
5502 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
thinca6864efa2021-06-19 20:45:20 +02005503 call g:Xsetlist([
5504 \ { 'filename': 'F1', 'lnum': 10, 'col': 2,
5505 \ 'end_col': 7, 'text': 'green'},
5506 \ { 'filename': 'F1', 'lnum': 20, 'end_lnum': 25, 'col': 4,
5507 \ 'end_col': 8, 'text': 'blue'},
5508 \ ])
5509
Bram Moolenaar858ba062020-05-31 23:11:59 +02005510 Xwindow
5511 call assert_equal('F1-L10C2-green', getline(1))
5512 call assert_equal('F1-L20C4-blue', getline(2))
5513 Xclose
5514 set quickfixtextfunc&vim
5515 Xwindow
thinca6864efa2021-06-19 20:45:20 +02005516 call assert_equal('F1|10 col 2-7| green', getline(1))
5517 call assert_equal('F1|20-25 col 4-8| blue', getline(2))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005518 Xclose
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005519
5520 set efm=%f:%l:%c:%m
5521 set quickfixtextfunc=Tqfexpr
5522 " Update the list with only the cwindow
5523 Xwindow
5524 only
5525 call g:Xsetlist([
5526 \ { 'filename': 'F2', 'lnum': 20, 'col': 2,
5527 \ 'end_col': 7, 'text': 'red'}
5528 \ ])
5529 call assert_equal(['F2-L20C2-red'], getline(1, '$'))
5530 new
5531 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005532 set efm&
5533 set quickfixtextfunc&
5534
5535 " Test for per list 'quickfixtextfunc' setting
5536 func PerQfText(info)
5537 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005538 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005539 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005540 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005541 endif
5542 if empty(qfl)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005543 return []
Bram Moolenaar858ba062020-05-31 23:11:59 +02005544 endif
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005545 let l = []
5546 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5547 call add(l, 'Line ' .. qfl[idx].lnum .. ', Col ' .. qfl[idx].col)
5548 endfor
5549 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005550 endfunc
5551 set quickfixtextfunc=Tqfexpr
5552 call g:Xsetlist([], ' ', {'quickfixtextfunc' : "PerQfText"})
5553 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5554 Xwindow
5555 call assert_equal('Line 10, Col 2', getline(1))
5556 call assert_equal('Line 20, Col 4', getline(2))
5557 Xclose
Bram Moolenaard43906d2020-07-20 21:31:32 +02005558 call assert_equal(function('PerQfText'),
5559 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005560 " Add entries to the list when the quickfix buffer is hidden
5561 Xaddexpr ['F1:30:6:red']
5562 Xwindow
5563 call assert_equal('Line 30, Col 6', getline(3))
5564 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005565 call g:Xsetlist([], 'r', {'quickfixtextfunc' : ''})
Bram Moolenaard43906d2020-07-20 21:31:32 +02005566 call assert_equal('', g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar858ba062020-05-31 23:11:59 +02005567 set quickfixtextfunc&
5568 delfunc PerQfText
5569
5570 " Non-existing function
5571 set quickfixtextfunc=Tabc
5572 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5573 call assert_fails("Xwindow", 'E117:')
5574 Xclose
5575 set quickfixtextfunc&
5576
5577 " set option to a non-function
5578 set quickfixtextfunc=[10,\ 20]
5579 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5580 call assert_fails("Xwindow", 'E117:')
5581 Xclose
5582 set quickfixtextfunc&
5583
5584 " set option to a function with different set of arguments
5585 func Xqftext(a, b, c)
5586 return a:a .. a:b .. a:c
5587 endfunc
5588 set quickfixtextfunc=Xqftext
5589 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E119:')
5590 call assert_fails("Xwindow", 'E119:')
5591 Xclose
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005592
5593 " set option to a function that returns a list with non-strings
5594 func Xqftext2(d)
5595 return ['one', [], 'two']
5596 endfunc
5597 set quickfixtextfunc=Xqftext2
5598 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue', 'F1:30:6:red']",
5599 \ 'E730:')
5600 call assert_fails('Xwindow', 'E730:')
Bram Moolenaard43906d2020-07-20 21:31:32 +02005601 call assert_equal(['one', 'F1|20 col 4| blue', 'F1|30 col 6| red'],
5602 \ getline(1, '$'))
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005603 Xclose
5604
Bram Moolenaar858ba062020-05-31 23:11:59 +02005605 set quickfixtextfunc&
5606 delfunc Xqftext
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005607 delfunc Xqftext2
Bram Moolenaard43906d2020-07-20 21:31:32 +02005608
5609 " set the global option to a lambda function
5610 set quickfixtextfunc={d\ ->\ map(g:Xgetlist({'id'\ :\ d.id,\ 'items'\ :\ 1}).items[d.start_idx-1:d.end_idx-1],\ 'v:val.text')}
5611 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5612 Xwindow
5613 call assert_equal(['green', 'blue'], getline(1, '$'))
5614 Xclose
5615 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)
5616 set quickfixtextfunc&
5617
5618 " use a lambda function that returns an empty list
5619 set quickfixtextfunc={d\ ->\ []}
5620 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5621 Xwindow
5622 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5623 \ getline(1, '$'))
5624 Xclose
5625 set quickfixtextfunc&
5626
5627 " use a lambda function that returns a list with empty strings
5628 set quickfixtextfunc={d\ ->\ ['',\ '']}
5629 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5630 Xwindow
5631 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5632 \ getline(1, '$'))
5633 Xclose
5634 set quickfixtextfunc&
5635
5636 " set the per-quickfix list text function to a lambda function
5637 call g:Xsetlist([], ' ',
5638 \ {'quickfixtextfunc' :
5639 \ {d -> map(g:Xgetlist({'id' : d.id, 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5640 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5641 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5642 Xwindow
5643 call assert_equal('Line 10, Col 2', getline(1))
5644 call assert_equal('Line 20, Col 4', getline(2))
5645 Xclose
5646 call assert_match("function('<lambda>\\d\\+')", string(g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc))
5647 call g:Xsetlist([], 'f')
Bram Moolenaar858ba062020-05-31 23:11:59 +02005648endfunc
5649
5650func Test_qftextfunc()
5651 call Xtest_qftextfunc('c')
5652 call Xtest_qftextfunc('l')
5653endfunc
5654
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005655func Test_qftextfunc_callback()
5656 let lines =<< trim END
5657 set efm=%f:%l:%c:%m
5658
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00005659 #" Test for using a function name
5660 LET &qftf = 'g:Tqfexpr'
5661 cexpr "F0:0:0:L0"
5662 copen
5663 call assert_equal('F0-L0C0-L0', getline(1))
5664 cclose
5665
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005666 #" Test for using a function()
5667 set qftf=function('g:Tqfexpr')
5668 cexpr "F1:1:1:L1"
5669 copen
5670 call assert_equal('F1-L1C1-L1', getline(1))
5671 cclose
5672
5673 #" Using a funcref variable to set 'quickfixtextfunc'
5674 VAR Fn = function('g:Tqfexpr')
5675 LET &qftf = Fn
5676 cexpr "F2:2:2:L2"
5677 copen
5678 call assert_equal('F2-L2C2-L2', getline(1))
5679 cclose
5680
5681 #" Using string(funcref_variable) to set 'quickfixtextfunc'
5682 LET Fn = function('g:Tqfexpr')
5683 LET &qftf = string(Fn)
5684 cexpr "F3:3:3:L3"
5685 copen
5686 call assert_equal('F3-L3C3-L3', getline(1))
5687 cclose
5688
5689 #" Test for using a funcref()
5690 set qftf=funcref('g:Tqfexpr')
5691 cexpr "F4:4:4:L4"
5692 copen
5693 call assert_equal('F4-L4C4-L4', getline(1))
5694 cclose
5695
5696 #" Using a funcref variable to set 'quickfixtextfunc'
5697 LET Fn = funcref('g:Tqfexpr')
5698 LET &qftf = Fn
5699 cexpr "F5:5:5:L5"
5700 copen
5701 call assert_equal('F5-L5C5-L5', getline(1))
5702 cclose
5703
5704 #" Using a string(funcref_variable) to set 'quickfixtextfunc'
5705 LET Fn = funcref('g:Tqfexpr')
5706 LET &qftf = string(Fn)
5707 cexpr "F5:5:5:L5"
5708 copen
5709 call assert_equal('F5-L5C5-L5', getline(1))
5710 cclose
5711
5712 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00005713 VAR optval = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005714 LET optval = substitute(optval, ' ', '\\ ', 'g')
5715 exe "set qftf=" .. optval
5716 cexpr "F6:6:6:L6"
5717 copen
5718 call assert_equal('F6-L6C6-L6', getline(1))
5719 cclose
5720
5721 #" Set 'quickfixtextfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005722 LET &qftf = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005723 cexpr "F7:7:7:L7"
5724 copen
5725 call assert_equal('F7-L7C7-L7', getline(1))
5726 cclose
5727
5728 #" Set 'quickfixtextfunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005729 LET &qftf = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005730 cexpr "F8:8:8:L8"
5731 copen
5732 call assert_equal('F8-L8C8-L8', getline(1))
5733 cclose
5734
5735 #" Set 'quickfixtextfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005736 VAR Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005737 LET &qftf = Lambda
5738 cexpr "F9:9:9:L9"
5739 copen
5740 call assert_equal('F9-L9C9-L9', getline(1))
5741 cclose
5742
5743 #" Set 'quickfixtextfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005744 LET Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005745 LET &qftf = string(Lambda)
5746 cexpr "F9:9:9:L9"
5747 copen
5748 call assert_equal('F9-L9C9-L9', getline(1))
5749 cclose
5750 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00005751 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005752
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00005753 " Test for using a script-local function name
5754 func s:TqfFunc2(info)
5755 let g:TqfFunc2Args = [a:info.start_idx, a:info.end_idx]
5756 return ''
5757 endfunc
5758 let g:TqfFunc2Args = []
5759 set quickfixtextfunc=s:TqfFunc2
5760 cexpr "F10:10:10:L10"
5761 cclose
5762 call assert_equal([1, 1], g:TqfFunc2Args)
5763
5764 let &quickfixtextfunc = 's:TqfFunc2'
5765 cexpr "F11:11:11:L11"
5766 cclose
5767 call assert_equal([1, 1], g:TqfFunc2Args)
5768 delfunc s:TqfFunc2
5769
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005770 " set 'quickfixtextfunc' to a partial with dict. This used to cause a crash.
5771 func SetQftfFunc()
5772 let params = {'qftf': function('g:DictQftfFunc')}
5773 let &quickfixtextfunc = params.qftf
5774 endfunc
5775 func g:DictQftfFunc(_) dict
5776 endfunc
5777 call SetQftfFunc()
5778 new
5779 call SetQftfFunc()
5780 bw
5781 call test_garbagecollect_now()
5782 new
5783 set qftf=
5784 wincmd w
5785 set qftf=
5786 :%bw!
5787
5788 " set per-quickfix list 'quickfixtextfunc' to a partial with dict. This used
5789 " to cause a crash.
5790 let &qftf = ''
5791 func SetLocalQftfFunc()
5792 let params = {'qftf': function('g:DictQftfFunc')}
5793 call setqflist([], 'a', {'quickfixtextfunc' : params.qftf})
5794 endfunc
5795 call SetLocalQftfFunc()
5796 call test_garbagecollect_now()
5797 call setqflist([], 'a', {'quickfixtextfunc' : ''})
5798 delfunc g:DictQftfFunc
5799 delfunc SetQftfFunc
5800 delfunc SetLocalQftfFunc
5801 set efm&
5802endfunc
5803
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02005804" Test for updating a location list for some other window and check that
5805" 'qftextfunc' uses the correct location list.
5806func Test_qftextfunc_other_loclist()
5807 %bw!
5808 call setloclist(0, [], 'f')
5809
5810 " create a window and a location list for it and open the location list
5811 " window
5812 lexpr ['F1:10:12:one', 'F1:20:14:two']
5813 let w1_id = win_getid()
5814 call setloclist(0, [], ' ',
5815 \ {'lines': ['F1:10:12:one', 'F1:20:14:two'],
5816 \ 'quickfixtextfunc':
5817 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5818 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5819 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5820 lwindow
5821 let w2_id = win_getid()
5822
5823 " create another window and a location list for it and open the location
5824 " list window
5825 topleft new
5826 let w3_id = win_getid()
5827 call setloclist(0, [], ' ',
5828 \ {'lines': ['F2:30:32:eleven', 'F2:40:34:twelve'],
5829 \ 'quickfixtextfunc':
5830 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5831 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5832 \ "'Ligne ' .. v:val.lnum .. ', Colonne ' .. v:val.col")}})
5833 lwindow
5834 let w4_id = win_getid()
5835
5836 topleft new
5837 lexpr ['F3:50:52:green', 'F3:60:54:blue']
5838 let w5_id = win_getid()
5839
5840 " change the location list for some other window
5841 call setloclist(0, [], 'r', {'lines': ['F3:55:56:aaa', 'F3:57:58:bbb']})
5842 call setloclist(w1_id, [], 'r', {'lines': ['F1:62:63:bbb', 'F1:64:65:ccc']})
5843 call setloclist(w3_id, [], 'r', {'lines': ['F2:76:77:ddd', 'F2:78:79:eee']})
5844 call assert_equal(['Line 62, Col 63', 'Line 64, Col 65'],
5845 \ getbufline(winbufnr(w2_id), 1, '$'))
5846 call assert_equal(['Ligne 76, Colonne 77', 'Ligne 78, Colonne 79'],
5847 \ getbufline(winbufnr(w4_id), 1, '$'))
5848 call setloclist(w2_id, [], 'r', {'lines': ['F1:32:33:fff', 'F1:34:35:ggg']})
5849 call setloclist(w4_id, [], 'r', {'lines': ['F2:46:47:hhh', 'F2:48:49:jjj']})
5850 call assert_equal(['Line 32, Col 33', 'Line 34, Col 35'],
5851 \ getbufline(winbufnr(w2_id), 1, '$'))
5852 call assert_equal(['Ligne 46, Colonne 47', 'Ligne 48, Colonne 49'],
5853 \ getbufline(winbufnr(w4_id), 1, '$'))
5854
5855 call win_gotoid(w5_id)
5856 lwindow
5857 call assert_equal(['F3|55 col 56| aaa', 'F3|57 col 58| bbb'],
5858 \ getline(1, '$'))
5859 %bw!
5860endfunc
5861
Bram Moolenaarec98e932020-06-08 19:35:59 +02005862" Running :lhelpgrep command more than once in a help window, doesn't jump to
5863" the help topic
5864func Test_lhelpgrep_from_help_window()
5865 call mkdir('Xtestdir/doc', 'p')
5866 call writefile(['window'], 'Xtestdir/doc/a.txt')
5867 call writefile(['buffer'], 'Xtestdir/doc/b.txt')
5868 let save_rtp = &rtp
5869 let &rtp = 'Xtestdir'
5870 lhelpgrep window
5871 lhelpgrep buffer
5872 call assert_equal('b.txt', fnamemodify(@%, ":p:t"))
5873 lhelpgrep window
5874 call assert_equal('a.txt', fnamemodify(@%, ":p:t"))
5875 let &rtp = save_rtp
5876 call delete('Xtestdir', 'rf')
5877 new | only!
5878endfunc
5879
Bram Moolenaarf7c4d832020-08-11 20:42:19 +02005880" Test for the crash fixed by 7.3.715
5881func Test_setloclist_crash()
5882 %bw!
5883 let g:BufNum = bufnr()
5884 augroup QF_Test
5885 au!
5886 au BufUnload * call setloclist(0, [{'bufnr':g:BufNum, 'lnum':1, 'col':1, 'text': 'tango down'}])
5887 augroup END
5888
5889 try
5890 lvimgrep /.*/ *.mak
5891 catch /E926:/
5892 endtry
5893 call assert_equal('tango down', getloclist(0, {'items' : 0}).items[0].text)
5894 call assert_equal(1, getloclist(0, {'size' : 0}).size)
5895
5896 augroup QF_Test
5897 au!
5898 augroup END
5899 unlet g:BufNum
5900 %bw!
5901endfunc
5902
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005903" Test for adding an invalid entry with the quickfix window open and making
5904" sure that the window contents are not changed
5905func Test_add_invalid_entry_with_qf_window()
5906 call setqflist([], 'f')
5907 cexpr "Xfile1:10:aa"
5908 copen
5909 call setqflist(['bb'], 'a')
5910 call assert_equal(1, line('$'))
5911 call assert_equal(['Xfile1|10| aa'], getline(1, '$'))
shane.xb.qian0d5e1ec2021-06-20 16:31:00 +02005912 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())
5913
5914 call setqflist([{'lnum': 10 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5915 call assert_equal(1 , line('$'))
5916 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5917 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())
5918
5919 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')
5920 call assert_equal(1 , line('$'))
5921 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5922 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())
5923
5924 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')
5925 call assert_equal(1 , line('$'))
5926 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5927 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())
5928
5929 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')
5930 call assert_equal(1 , line('$'))
5931 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5932 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())
5933
5934 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')
5935 call assert_equal(1 , line('$'))
5936 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5937 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())
5938
5939 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')
5940 call assert_equal(1 , line('$'))
5941 call assert_equal(['Xfile1|10 col 666-222| aa'] , getline(1 , '$'))
5942 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())
5943
5944 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')
5945 call assert_equal(1 , line('$'))
5946 call assert_equal(['Xfile1|10-6 col 666-222| aa'] , getline(1 , '$'))
5947 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 +01005948 cclose
5949endfunc
5950
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01005951" Test for very weird problem: autocommand causes a failure, resulting opening
5952" the quickfix window to fail. This still splits the window, but otherwise
5953" should not mess up buffers.
5954func Test_quickfix_window_fails_to_open()
5955 CheckScreendump
5956
5957 let lines =<< trim END
5958 anything
5959 try
5960 anything
5961 endtry
5962 END
5963 call writefile(lines, 'XquickfixFails')
5964
5965 let lines =<< trim END
5966 split XquickfixFails
5967 silent vimgrep anything %
5968 normal o
5969 au BufLeave * ++once source XquickfixFails
5970 " This will trigger the autocommand, which causes an error, what follows
5971 " is aborted but the window was already split.
5972 silent! cwindow
5973 END
5974 call writefile(lines, 'XtestWinFails')
5975 let buf = RunVimInTerminal('-S XtestWinFails', #{rows: 13})
5976 call VerifyScreenDump(buf, 'Test_quickfix_window_fails', {})
5977
5978 " clean up
5979 call term_sendkeys(buf, ":bwipe!\<CR>")
5980 call term_wait(buf)
5981 call StopVimInTerminal(buf)
5982 call delete('XtestWinFails')
5983 call delete('XquickfixFails')
5984endfunc
5985
Bram Moolenaar2d870f82020-12-05 13:41:01 +01005986" Test for updating the quickfix buffer whenever the associated quickfix list
Bram Moolenaar287153c2020-11-29 14:20:27 +01005987" is changed.
5988func Xqfbuf_update(cchar)
5989 call s:setup_commands(a:cchar)
5990
5991 Xexpr "F1:1:line1"
5992 Xopen
5993 call assert_equal(['F1|1| line1'], getline(1, '$'))
5994 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5995
5996 " Test setqflist() using the 'lines' key in 'what'
5997 " add a new entry
5998 call g:Xsetlist([], 'a', {'lines' : ['F2:2: line2']})
5999 call assert_equal(['F1|1| line1', 'F2|2| line2'], getline(1, '$'))
6000 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6001 " replace all the entries with a single entry
6002 call g:Xsetlist([], 'r', {'lines' : ['F3:3: line3']})
6003 call assert_equal(['F3|3| line3'], getline(1, '$'))
6004 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6005 " remove all the entries
6006 call g:Xsetlist([], 'r', {'lines' : []})
6007 call assert_equal([''], getline(1, '$'))
6008 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6009 " add a new list
6010 call g:Xsetlist([], ' ', {'lines' : ['F4:4: line4']})
6011 call assert_equal(['F4|4| line4'], getline(1, '$'))
6012 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6013
6014 " Test setqflist() using the 'items' key in 'what'
6015 " add a new entry
6016 call g:Xsetlist([], 'a', {'items' : [{'filename' : 'F5', 'lnum' : 5, 'text' : 'line5'}]})
6017 call assert_equal(['F4|4| line4', 'F5|5| line5'], getline(1, '$'))
6018 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6019 " replace all the entries with a single entry
6020 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F6', 'lnum' : 6, 'text' : 'line6'}]})
6021 call assert_equal(['F6|6| line6'], getline(1, '$'))
6022 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6023 " remove all the entries
6024 call g:Xsetlist([], 'r', {'items' : []})
6025 call assert_equal([''], getline(1, '$'))
6026 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6027 " add a new list
6028 call g:Xsetlist([], ' ', {'items' : [{'filename' : 'F7', 'lnum' : 7, 'text' : 'line7'}]})
6029 call assert_equal(['F7|7| line7'], getline(1, '$'))
6030 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6031
6032 call g:Xsetlist([], ' ', {})
6033 call assert_equal([''], getline(1, '$'))
6034 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6035
6036 Xclose
6037endfunc
6038
6039func Test_qfbuf_update()
6040 call Xqfbuf_update('c')
6041 call Xqfbuf_update('l')
6042endfunc
6043
Bram Moolenaar8c801b32021-03-05 20:58:22 +01006044func Test_vimgrep_noswapfile()
6045 set noswapfile
6046 call writefile(['one', 'two', 'three'], 'Xgreppie')
6047 vimgrep two Xgreppie
6048 call assert_equal('two', getline('.'))
6049
6050 call delete('Xgreppie')
6051 set swapfile
6052endfunc
6053
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006054" Test for the :vimgrep 'f' flag (fuzzy match)
6055func Xvimgrep_fuzzy_match(cchar)
6056 call s:setup_commands(a:cchar)
6057
6058 Xvimgrep /three one/f Xfile*
6059 let l = g:Xgetlist()
6060 call assert_equal(2, len(l))
6061 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6062 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6063 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6064 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6065
6066 Xvimgrep /the/f Xfile*
6067 let l = g:Xgetlist()
6068 call assert_equal(3, len(l))
6069 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6070 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6071 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6072 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6073 call assert_equal(['Xfile2', 4, 4, 'aaathreeaaa'],
6074 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6075
6076 Xvimgrep /aaa/fg Xfile*
6077 let l = g:Xgetlist()
6078 call assert_equal(4, len(l))
6079 call assert_equal(['Xfile1', 2, 1, 'aaaaaa'],
6080 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6081 call assert_equal(['Xfile1', 2, 4, 'aaaaaa'],
6082 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6083 call assert_equal(['Xfile2', 4, 1, 'aaathreeaaa'],
6084 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6085 call assert_equal(['Xfile2', 4, 9, 'aaathreeaaa'],
6086 \ [bufname(l[3].bufnr), l[3].lnum, l[3].col, l[3].text])
6087
6088 call assert_fails('Xvimgrep /xyz/fg Xfile*', 'E480:')
6089endfunc
6090
6091func Test_vimgrep_fuzzy_match()
6092 call writefile(['one two three', 'aaaaaa'], 'Xfile1')
6093 call writefile(['one', 'three one two', 'two', 'aaathreeaaa'], 'Xfile2')
6094 call Xvimgrep_fuzzy_match('c')
6095 call Xvimgrep_fuzzy_match('l')
6096 call delete('Xfile1')
6097 call delete('Xfile2')
6098endfunc
6099
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006100func Test_locationlist_open_in_newtab()
6101 call s:create_test_file('Xqftestfile1')
6102 call s:create_test_file('Xqftestfile2')
6103 call s:create_test_file('Xqftestfile3')
6104
6105 %bwipe!
6106
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01006107 let lines =<< trim END
6108 Xqftestfile1:5:Line5
6109 Xqftestfile2:10:Line10
6110 Xqftestfile3:16:Line16
6111 END
6112 lgetexpr lines
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006113
6114 silent! llast
6115 call assert_equal(1, tabpagenr('$'))
6116 call assert_equal('Xqftestfile3', bufname())
6117
6118 set switchbuf=newtab
6119
6120 silent! lfirst
6121 call assert_equal(2, tabpagenr('$'))
6122 call assert_equal('Xqftestfile1', bufname())
6123
6124 silent! lnext
6125 call assert_equal(3, tabpagenr('$'))
6126 call assert_equal('Xqftestfile2', bufname())
6127
6128 call delete('Xqftestfile1')
6129 call delete('Xqftestfile2')
6130 call delete('Xqftestfile3')
6131 set switchbuf&vim
6132
6133 %bwipe!
6134endfunc
6135
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02006136" Test for win_gettype() in quickfix and location list windows
6137func Test_win_gettype()
6138 copen
6139 call assert_equal("quickfix", win_gettype())
6140 let wid = win_getid()
6141 wincmd p
6142 call assert_equal("quickfix", win_gettype(wid))
6143 cclose
6144 lexpr ''
6145 lopen
6146 call assert_equal("loclist", win_gettype())
6147 let wid = win_getid()
6148 wincmd p
6149 call assert_equal("loclist", win_gettype(wid))
6150 lclose
6151endfunc
6152
Christian Brabandt0b226f62021-12-01 10:54:24 +00006153fun Test_vimgrep_nomatch()
6154 call XexprTests('c')
6155 call g:Xsetlist([{'lnum':10,'text':'Line1'}])
6156 copen
6157 if has("win32")
6158 call assert_fails('vimgrep foo *.zzz', 'E479:')
6159 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'}]
6160 else
6161 call assert_fails('vimgrep foo *.zzz', 'E480:')
6162 let expected = []
6163 endif
6164 call assert_equal(expected, getqflist())
6165 cclose
6166endfunc
6167
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00006168" Test for opening the quickfix window in two tab pages and then closing one
6169" of the quickfix windows. This should not make the quickfix buffer unlisted.
6170" (github issue #9300).
6171func Test_two_qf_windows()
6172 cexpr "F1:1:line1"
6173 copen
6174 tabnew
6175 copen
6176 call assert_true(&buflisted)
6177 cclose
6178 tabfirst
6179 call assert_true(&buflisted)
6180 let bnum = bufnr()
6181 cclose
6182 " if all the quickfix windows are closed, then buffer should be unlisted.
6183 call assert_false(buflisted(bnum))
6184 %bw!
6185
6186 " Repeat the test for a location list
6187 lexpr "F2:2:line2"
6188 lopen
6189 let bnum = bufnr()
6190 tabnew
6191 exe "buffer" bnum
6192 tabfirst
6193 lclose
6194 tablast
6195 call assert_true(buflisted(bnum))
6196 tabclose
6197 lopen
6198 call assert_true(buflisted(bnum))
6199 lclose
6200 call assert_false(buflisted(bnum))
6201 %bw!
6202endfunc
6203
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006204" Weird sequence of commands that caused entering a wiped-out buffer
6205func Test_lopen_bwipe()
6206 func R()
6207 silent! tab lopen
6208 e x
6209 silent! lfile
6210 endfunc
6211
6212 cal R()
6213 cal R()
6214 cal R()
6215 bw!
6216 delfunc R
6217endfunc
6218
Bram Moolenaare3537ae2022-02-08 15:05:20 +00006219" Another sequence of commands that caused all buffers to be wiped out
6220func Test_lopen_bwipe_all()
6221 let lines =<< trim END
6222 func R()
6223 silent! tab lopen
6224 e foo
6225 silent! lfile
6226 endfunc
6227 cal R()
6228 exe "norm \<C-W>\<C-V>0"
6229 cal R()
6230 bwipe
6231
6232 call writefile(['done'], 'Xresult')
6233 qall!
6234 END
6235 call writefile(lines, 'Xscript')
6236 if RunVim([], [], '-u NONE -n -X -Z -e -m -s -S Xscript')
6237 call assert_equal(['done'], readfile('Xresult'))
6238 endif
6239
6240 call delete('Xscript')
6241 call delete('Xresult')
6242endfunc
6243
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00006244" Test for calling setqflist() function recursively
6245func Test_recursive_setqflist()
6246 augroup QF_Test
6247 au!
6248 autocmd BufWinEnter quickfix call setqflist([], 'r')
6249 augroup END
6250
6251 copen
6252 call assert_fails("call setqflist([], 'a')", 'E952:')
6253
6254 augroup QF_Test
6255 au!
6256 augroup END
6257 %bw!
6258endfunc
6259
6260" Test for failure to create a new window when selecting a file from the
6261" quickfix window
6262func Test_cwindow_newwin_fails()
6263 cgetexpr ["Xfile1:10:L10", "Xfile1:20:L20"]
6264 cwindow
6265 only
6266 let qf_wid = win_getid()
6267 " create the maximum number of scratch windows
6268 let hor_win_count = (&lines - 1)/2
6269 let hor_split_count = hor_win_count - 1
6270 for s in range(1, hor_split_count) | new | set buftype=nofile | endfor
6271 call win_gotoid(qf_wid)
6272 call assert_fails('exe "normal \<CR>"', 'E36:')
6273 %bw!
6274endfunc
6275
6276" Test for updating the location list when only the location list window is
6277" present and the corresponding file window is closed.
6278func Test_loclist_update_with_llwin_only()
6279 %bw!
6280 new
6281 wincmd w
6282 lexpr ["Xfile1:1:Line1"]
6283 lopen
6284 wincmd p
6285 close
6286 call setloclist(2, [], 'r', {'lines': ["Xtest2:2:Line2"]})
6287 call assert_equal(['Xtest2|2| Line2'], getbufline(winbufnr(2), 1, '$'))
6288 %bw!
6289endfunc
6290
6291" Test for getting the quickfix list after a buffer with an error is wiped out
6292func Test_getqflist_wiped_out_buffer()
6293 %bw!
6294 cexpr ["Xtest1:34:Wiped out"]
6295 let bnum = bufnr('Xtest1')
6296 call assert_equal(bnum, getqflist()[0].bufnr)
6297 bw Xtest1
6298 call assert_equal(0, getqflist()[0].bufnr)
6299 %bw!
6300endfunc
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006301
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00006302" Test for the status message that is displayed when opening a new quickfix
6303" list
6304func Test_qflist_statusmsg()
6305 cexpr "1\n2"
6306 cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"
6307 call assert_equal('(4 of 4): msg', v:statusmsg)
6308 call setqflist([], 'f')
6309 %bw!
6310
6311 " When creating a new quickfix list, if an autocmd changes the quickfix list
6312 " in the stack, then an error message should be displayed.
6313 augroup QF_Test
6314 au!
6315 au BufEnter test_quickfix.vim colder
6316 augroup END
6317 cexpr "1\n2"
6318 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6319 call setqflist([], 'f')
6320 augroup QF_Test
6321 au!
6322 augroup END
6323 %bw!
6324
6325 augroup QF_Test
6326 au!
6327 au BufEnter test_quickfix.vim caddexpr "4"
6328 augroup END
6329 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6330 call setqflist([], 'f')
6331 augroup QF_Test
6332 au!
6333 augroup END
6334 %bw!
6335endfunc
6336
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006337" vim: shiftwidth=2 sts=2 expandtab