blob: 2431a46fc9c737f0125ff9fb5ab4ee0244dc1851 [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
Bram Moolenaar7fe956d2022-07-03 14:21:09 +01003139 call term_sendkeys(buf, "\<C-W>j:set cursorline\<CR>")
3140 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_3', {})
3141
3142 call term_sendkeys(buf, "j")
3143 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_4', {})
3144
Bram Moolenaare00fdf32019-09-15 19:09:42 +02003145 " clean up
3146 call StopVimInTerminal(buf)
3147 call delete('XtestCwindow')
3148 call delete('XCwindow')
3149endfunc
3150
Bram Moolenaaree85df32017-03-19 14:19:50 +01003151func XvimgrepTests(cchar)
3152 call s:setup_commands(a:cchar)
3153
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003154 let lines =<< trim END
3155 Editor:VIM vim
3156 Editor:Emacs EmAcS
3157 Editor:Notepad NOTEPAD
3158 END
3159 call writefile(lines, 'Xtestfile1')
Dominique Pelle923dce22021-11-21 11:36:04 +00003160 call writefile(['Linux', 'macOS', 'MS-Windows'], 'Xtestfile2')
Bram Moolenaaree85df32017-03-19 14:19:50 +01003161
3162 " Error cases
3163 call assert_fails('Xvimgrep /abc *', 'E682:')
3164
3165 let @/=''
3166 call assert_fails('Xvimgrep // *', 'E35:')
3167
3168 call assert_fails('Xvimgrep abc', 'E683:')
3169 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
3170 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
3171
3172 Xexpr ""
3173 Xvimgrepadd Notepad Xtestfile1
Dominique Pelle923dce22021-11-21 11:36:04 +00003174 Xvimgrepadd macOS Xtestfile2
Bram Moolenaaree85df32017-03-19 14:19:50 +01003175 let l = g:Xgetlist()
3176 call assert_equal(2, len(l))
3177 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
3178
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003179 10Xvimgrep #\cvim#g Xtestfile?
Bram Moolenaaree85df32017-03-19 14:19:50 +01003180 let l = g:Xgetlist()
3181 call assert_equal(2, len(l))
3182 call assert_equal(8, l[0].col)
thinca6864efa2021-06-19 20:45:20 +02003183 call assert_equal(11, l[0].end_col)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003184 call assert_equal(12, l[1].col)
thinca6864efa2021-06-19 20:45:20 +02003185 call assert_equal(15, l[1].end_col)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003186
3187 1Xvimgrep ?Editor? Xtestfile*
3188 let l = g:Xgetlist()
3189 call assert_equal(1, len(l))
3190 call assert_equal('Editor:VIM vim', l[0].text)
3191
3192 edit +3 Xtestfile2
3193 Xvimgrep +\cemacs+j Xtestfile1
3194 let l = g:Xgetlist()
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003195 call assert_equal('Xtestfile2', @%)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003196 call assert_equal('Editor:Emacs EmAcS', l[0].text)
3197
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02003198 " Test for unloading a buffer after vimgrep searched the buffer
3199 %bwipe
3200 Xvimgrep /Editor/j Xtestfile*
3201 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
3202 call assert_equal([], getbufinfo('Xtestfile2'))
3203
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00003204 " Test for opening the dummy buffer used by vimgrep in a window. The new
3205 " window should be closed
3206 %bw!
3207 augroup QF_Test
3208 au!
3209 autocmd BufReadPre * exe "sb " .. expand("<abuf>")
3210 augroup END
3211 call assert_fails("Xvimgrep /sublime/ Xtestfile1", 'E480:')
3212 call assert_equal(1, winnr('$'))
3213 augroup QF_Test
3214 au!
3215 augroup END
3216
Bram Moolenaaree85df32017-03-19 14:19:50 +01003217 call delete('Xtestfile1')
3218 call delete('Xtestfile2')
3219endfunc
3220
3221" Tests for the :vimgrep command
3222func Test_vimgrep()
3223 call XvimgrepTests('c')
3224 call XvimgrepTests('l')
3225endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003226
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01003227func Test_vimgrep_wildcards_expanded_once()
3228 new X[id-01] file.txt
3229 call setline(1, 'some text to search for')
3230 vimgrep text %
3231 bwipe!
3232endfunc
3233
Bram Moolenaar1c299432018-10-28 14:36:09 +01003234" Test for incsearch highlighting of the :vimgrep pattern
3235" This test used to cause "E315: ml_get: invalid lnum" errors.
3236func Test_vimgrep_incsearch()
3237 enew
3238 set incsearch
3239 call test_override("char_avail", 1)
3240
3241 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
3242 let l = getqflist()
3243 call assert_equal(2, len(l))
3244
3245 call test_override("ALL", 0)
3246 set noincsearch
3247endfunc
3248
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003249" Test vimgrep with the last search pattern not set
3250func Test_vimgrep_with_no_last_search_pat()
3251 let lines =<< trim [SCRIPT]
3252 call assert_fails('vimgrep // *', 'E35:')
3253 call writefile(v:errors, 'Xresult')
3254 qall!
3255 [SCRIPT]
3256 call writefile(lines, 'Xscript')
3257 if RunVim([], [], '--clean -S Xscript')
3258 call assert_equal([], readfile('Xresult'))
3259 endif
3260 call delete('Xscript')
3261 call delete('Xresult')
3262endfunc
3263
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02003264" Test vimgrep without swap file
3265func Test_vimgrep_without_swap_file()
3266 let lines =<< trim [SCRIPT]
3267 vimgrep grep test_c*
3268 call writefile(['done'], 'Xresult')
3269 qall!
3270 [SCRIPT]
3271 call writefile(lines, 'Xscript')
3272 if RunVim([], [], '--clean -n -S Xscript Xscript')
3273 call assert_equal(['done'], readfile('Xresult'))
3274 endif
3275 call delete('Xscript')
3276 call delete('Xresult')
3277endfunc
3278
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02003279func Test_vimgrep_existing_swapfile()
3280 call writefile(['match apple with apple'], 'Xapple')
3281 call writefile(['swapfile'], '.Xapple.swp')
3282 let g:foundSwap = 0
3283 let g:ignoreSwapExists = 1
3284 augroup grep
3285 au SwapExists * let foundSwap = 1 | let v:swapchoice = 'e'
3286 augroup END
3287 vimgrep apple Xapple
3288 call assert_equal(1, g:foundSwap)
3289 call assert_match('.Xapple.swo', swapname(''))
3290
3291 call delete('Xapple')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003292 call delete('.Xapple.swp')
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02003293 augroup grep
3294 au! SwapExists
3295 augroup END
3296 unlet g:ignoreSwapExists
3297endfunc
3298
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003299func XfreeTests(cchar)
3300 call s:setup_commands(a:cchar)
3301
3302 enew | only
3303
3304 " Deleting the quickfix stack should work even When the current list is
3305 " somewhere in the middle of the stack
3306 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3307 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3308 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3309 Xolder
3310 call g:Xsetlist([], 'f')
3311 call assert_equal(0, len(g:Xgetlist()))
3312
3313 " After deleting the stack, adding a new list should create a stack with a
3314 " single list.
3315 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3316 call assert_equal(1, g:Xgetlist({'all':1}).nr)
3317
3318 " Deleting the stack from a quickfix window should update/clear the
3319 " quickfix/location list window.
3320 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3321 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3322 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3323 Xolder
3324 Xwindow
3325 call g:Xsetlist([], 'f')
3326 call assert_equal(2, winnr('$'))
3327 call assert_equal(1, line('$'))
3328 Xclose
3329
3330 " Deleting the stack from a non-quickfix window should update/clear the
3331 " quickfix/location list window.
3332 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3333 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3334 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3335 Xolder
3336 Xwindow
3337 wincmd p
3338 call g:Xsetlist([], 'f')
3339 call assert_equal(0, len(g:Xgetlist()))
3340 wincmd p
3341 call assert_equal(2, winnr('$'))
3342 call assert_equal(1, line('$'))
3343
3344 " After deleting the location list stack, if the location list window is
3345 " opened, then a new location list should be created. So opening the
3346 " location list window again should not create a new window.
3347 if a:cchar == 'l'
3348 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3349 wincmd p
3350 lopen
3351 call assert_equal(2, winnr('$'))
3352 endif
3353 Xclose
3354endfunc
3355
Bram Moolenaar74240d32017-12-10 15:26:15 +01003356" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003357func Test_qf_free()
3358 call XfreeTests('c')
3359 call XfreeTests('l')
3360endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02003361
3362" Test for buffer overflow when parsing lines and adding new entries to
3363" the quickfix list.
3364func Test_bufoverflow()
3365 set efm=%f:%l:%m
3366 cgetexpr ['File1:100:' . repeat('x', 1025)]
3367
3368 set efm=%+GCompiler:\ %.%#,%f:%l:%m
3369 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
3370
3371 set efm=%DEntering\ directory\ %f,%f:%l:%m
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +01003372 let lines =<< trim eval END
3373 Entering directory $"{repeat('a', 1006)}"
3374 File1:10:Hello World
3375 END
3376 cgetexpr lines
Bram Moolenaar6e62da32017-05-28 08:16:25 +02003377 set efm&vim
3378endfunc
3379
Bram Moolenaar875feea2017-06-11 16:07:51 +02003380" Tests for getting the quickfix stack size
3381func XsizeTests(cchar)
3382 call s:setup_commands(a:cchar)
3383
3384 call g:Xsetlist([], 'f')
3385 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003386 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
3387 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02003388
3389 Xexpr "File1:10:Line1"
3390 Xexpr "File2:20:Line2"
3391 Xexpr "File3:30:Line3"
3392 Xolder | Xolder
3393 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
3394 call g:Xsetlist([], 'f')
3395
3396 Xexpr "File1:10:Line1"
3397 Xexpr "File2:20:Line2"
3398 Xexpr "File3:30:Line3"
3399 Xolder | Xolder
3400 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
3401 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
3402endfunc
3403
3404func Test_Qf_Size()
3405 call XsizeTests('c')
3406 call XsizeTests('l')
3407endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02003408
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003409func Test_cclose_from_copen()
3410 augroup QF_Test
3411 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003412 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003413 augroup END
3414 copen
3415 augroup QF_Test
3416 au!
3417 augroup END
3418 augroup! QF_Test
3419endfunc
3420
Bram Moolenaar18141832017-06-25 21:17:25 +02003421func Test_cclose_in_autocmd()
3422 " Problem is only triggered if "starting" is zero, so that the OptionsSet
3423 " event will be triggered.
3424 call test_override('starting', 1)
3425 augroup QF_Test
3426 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003427 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar18141832017-06-25 21:17:25 +02003428 augroup END
3429 copen
3430 augroup QF_Test
3431 au!
3432 augroup END
3433 augroup! QF_Test
3434 call test_override('starting', 0)
3435endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003436
Bram Moolenaar379fb762018-08-30 15:58:28 +02003437" Check that ":file" without an argument is possible even when "curbuf_lock"
3438" is set.
3439func Test_file_from_copen()
3440 " Works without argument.
3441 augroup QF_Test
3442 au!
3443 au FileType qf file
3444 augroup END
3445 copen
3446
3447 augroup QF_Test
3448 au!
3449 augroup END
3450 cclose
3451
3452 " Fails with argument.
3453 augroup QF_Test
3454 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003455 au FileType qf call assert_fails(':file foo', 'E788:')
Bram Moolenaar379fb762018-08-30 15:58:28 +02003456 augroup END
3457 copen
3458 augroup QF_Test
3459 au!
3460 augroup END
3461 cclose
3462
3463 augroup! QF_Test
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02003464endfunc
Bram Moolenaar379fb762018-08-30 15:58:28 +02003465
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003466func Test_resize_from_copen()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003467 augroup QF_Test
3468 au!
3469 au FileType qf resize 5
3470 augroup END
3471 try
3472 " This should succeed without any exception. No other buffers are
3473 " involved in the autocmd.
3474 copen
3475 finally
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003476 augroup QF_Test
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003477 au!
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003478 augroup END
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003479 augroup! QF_Test
3480 endtry
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003481endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02003482
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003483func Test_vimgrep_with_textlock()
3484 new
3485
zeertzjqcfe45652022-05-27 17:26:55 +01003486 " Simple way to execute something with "textlock" set.
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003487 " Check that vimgrep without jumping can be executed.
3488 au InsertCharPre * vimgrep /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 vimgrepadd without jumping can be executed.
3497 au InsertCharPre * vimgrepadd /RunTheTest/j runtest.vim
3498 normal ax
3499 let qflist = getqflist()
3500 call assert_true(len(qflist) > 0)
3501 call assert_match('RunTheTest', qflist[0].text)
3502 call setqflist([], 'r')
3503 au! InsertCharPre
3504
3505 " Check that lvimgrep without jumping can be executed.
3506 au InsertCharPre * lvimgrep /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 " Check that lvimgrepadd without jumping can be executed.
3515 au InsertCharPre * lvimgrepadd /RunTheTest/j runtest.vim
3516 normal ax
3517 let qflist = getloclist(0)
3518 call assert_true(len(qflist) > 0)
3519 call assert_match('RunTheTest', qflist[0].text)
3520 call setloclist(0, [], 'r')
3521 au! InsertCharPre
3522
3523 " trying to jump will give an error
3524 au InsertCharPre * vimgrep /RunTheTest/ runtest.vim
3525 call assert_fails('normal ax', 'E565:')
3526 au! InsertCharPre
3527
3528 au InsertCharPre * vimgrepadd /RunTheTest/ runtest.vim
3529 call assert_fails('normal ax', 'E565:')
3530 au! InsertCharPre
3531
3532 au InsertCharPre * lvimgrep /RunTheTest/ runtest.vim
3533 call assert_fails('normal ax', 'E565:')
3534 au! InsertCharPre
3535
3536 au InsertCharPre * lvimgrepadd /RunTheTest/ runtest.vim
3537 call assert_fails('normal ax', 'E565:')
3538 au! InsertCharPre
3539
3540 bwipe!
3541endfunc
3542
Bram Moolenaara8788f42017-07-19 17:06:20 +02003543" Tests for the quickfix buffer b:changedtick variable
3544func Xchangedtick_tests(cchar)
3545 call s:setup_commands(a:cchar)
3546
3547 new | only
3548
3549 Xexpr "" | Xexpr "" | Xexpr ""
3550
3551 Xopen
3552 Xolder
3553 Xolder
3554 Xaddexpr "F1:10:Line10"
3555 Xaddexpr "F2:20:Line20"
3556 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
3557 call g:Xsetlist([], 'f')
3558 call assert_equal(8, getbufvar('%', 'changedtick'))
3559 Xclose
3560endfunc
3561
3562func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02003563 call Xchangedtick_tests('c')
3564 call Xchangedtick_tests('l')
3565endfunc
3566
3567" Tests for parsing an expression using setqflist()
3568func Xsetexpr_tests(cchar)
3569 call s:setup_commands(a:cchar)
3570
3571 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003572 call g:Xsetlist([], ' ', {'lines' : t})
3573 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003574
3575 let l = g:Xgetlist()
3576 call assert_equal(3, len(l))
3577 call assert_equal(20, l[1].lnum)
3578 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003579 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003580 let l = g:Xgetlist()
3581 call assert_equal(1, len(l))
3582 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003583 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
3584 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003585
3586 call g:Xsetlist([], 'f')
3587 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003588 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
3589 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
3590 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
3591 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003592 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
3593 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02003594
3595 " Adding entries using a custom efm
3596 set efm&
3597 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
3598 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
3599 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3600 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
3601 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
3602 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3603 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
3604 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003605endfunc
3606
3607func Test_setexpr()
3608 call Xsetexpr_tests('c')
3609 call Xsetexpr_tests('l')
3610endfunc
3611
3612" Tests for per quickfix/location list directory stack
3613func Xmultidirstack_tests(cchar)
3614 call s:setup_commands(a:cchar)
3615
3616 call g:Xsetlist([], 'f')
3617 Xexpr "" | Xexpr ""
3618
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003619 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
3620 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
3621 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
3622 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003623
3624 let l1 = g:Xgetlist({'nr':1, 'items':1})
3625 let l2 = g:Xgetlist({'nr':2, 'items':1})
3626 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
3627 call assert_equal(3, l1.items[1].lnum)
3628 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
3629 call assert_equal(5, l2.items[1].lnum)
3630endfunc
3631
3632func Test_multidirstack()
3633 call mkdir('Xone/a', 'p')
3634 call mkdir('Xtwo/a', 'p')
3635 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3636 call writefile(lines, 'Xone/a/one.txt')
3637 call writefile(lines, 'Xtwo/a/two.txt')
3638 let save_efm = &efm
3639 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
3640
3641 call Xmultidirstack_tests('c')
3642 call Xmultidirstack_tests('l')
3643
3644 let &efm = save_efm
3645 call delete('Xone', 'rf')
3646 call delete('Xtwo', 'rf')
3647endfunc
3648
3649" Tests for per quickfix/location list file stack
3650func Xmultifilestack_tests(cchar)
3651 call s:setup_commands(a:cchar)
3652
3653 call g:Xsetlist([], 'f')
3654 Xexpr "" | Xexpr ""
3655
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003656 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
3657 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
3658 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
3659 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003660
3661 let l1 = g:Xgetlist({'nr':1, 'items':1})
3662 let l2 = g:Xgetlist({'nr':2, 'items':1})
3663 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
3664 call assert_equal(3, l1.items[1].lnum)
3665 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
3666 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02003667
3668 " Test for start of a new error line in the same line where a previous
3669 " error line ends with a file stack.
3670 let efm_val = 'Error\ l%l\ in\ %f,'
3671 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003672 let lines =<< trim END
3673 (one.txt
3674 Error l4 in one.txt
3675 ) (two.txt
3676 Error l6 in two.txt
3677 )
3678 Error l8 in one.txt
3679 END
3680 let l = g:Xgetlist({'lines': lines, 'efm' : efm_val})
Bram Moolenaare333e792018-04-08 13:27:39 +02003681 call assert_equal(3, len(l.items))
3682 call assert_equal('one.txt', bufname(l.items[0].bufnr))
3683 call assert_equal(4, l.items[0].lnum)
3684 call assert_equal('one.txt', l.items[0].text)
3685 call assert_equal('two.txt', bufname(l.items[1].bufnr))
3686 call assert_equal(6, l.items[1].lnum)
3687 call assert_equal('two.txt', l.items[1].text)
3688 call assert_equal('one.txt', bufname(l.items[2].bufnr))
3689 call assert_equal(8, l.items[2].lnum)
3690 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02003691endfunc
3692
3693func Test_multifilestack()
3694 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3695 call writefile(lines, 'one.txt')
3696 call writefile(lines, 'two.txt')
3697 let save_efm = &efm
3698 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
3699
3700 call Xmultifilestack_tests('c')
3701 call Xmultifilestack_tests('l')
3702
3703 let &efm = save_efm
3704 call delete('one.txt')
3705 call delete('two.txt')
3706endfunc
3707
3708" Tests for per buffer 'efm' setting
3709func Test_perbuf_efm()
3710 call writefile(["File1-10-Line10"], 'one.txt')
3711 call writefile(["File2#20#Line20"], 'two.txt')
3712 set efm=%f#%l#%m
3713 new | only
3714 new
3715 setlocal efm=%f-%l-%m
3716 cfile one.txt
3717 wincmd w
3718 caddfile two.txt
3719
3720 let l = getqflist()
3721 call assert_equal(10, l[0].lnum)
3722 call assert_equal('Line20', l[1].text)
3723
3724 set efm&
3725 new | only
3726 call delete('one.txt')
3727 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02003728endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003729
3730" Open multiple help windows using ":lhelpgrep
3731" This test used to crash Vim
3732func Test_Multi_LL_Help()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003733 new | only
3734 lhelpgrep window
3735 lopen
3736 e#
3737 lhelpgrep buffer
3738 call assert_equal(3, winnr('$'))
3739 call assert_true(len(getloclist(1)) != 0)
3740 call assert_true(len(getloclist(2)) != 0)
3741 new | only
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003742endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02003743
3744" Tests for adding new quickfix lists using setqflist()
3745func XaddQf_tests(cchar)
3746 call s:setup_commands(a:cchar)
3747
3748 " Create a new list using ' ' for action
3749 call g:Xsetlist([], 'f')
3750 call g:Xsetlist([], ' ', {'title' : 'Test1'})
3751 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3752 call assert_equal(1, l.nr)
3753 call assert_equal('Test1', l.title)
3754
3755 " Create a new list using ' ' for action and '$' for 'nr'
3756 call g:Xsetlist([], 'f')
3757 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
3758 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3759 call assert_equal(1, l.nr)
3760 call assert_equal('Test2', l.title)
3761
3762 " Create a new list using 'a' for action
3763 call g:Xsetlist([], 'f')
3764 call g:Xsetlist([], 'a', {'title' : 'Test3'})
3765 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3766 call assert_equal(1, l.nr)
3767 call assert_equal('Test3', l.title)
3768
3769 " Create a new list using 'a' for action and '$' for 'nr'
3770 call g:Xsetlist([], 'f')
3771 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
3772 call g:Xsetlist([], 'a', {'title' : 'Test4'})
3773 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3774 call assert_equal(1, l.nr)
3775 call assert_equal('Test4', l.title)
3776
3777 " Adding a quickfix list should remove all the lists following the current
3778 " list.
3779 Xexpr "" | Xexpr "" | Xexpr ""
3780 silent! 10Xolder
3781 call g:Xsetlist([], ' ', {'title' : 'Test5'})
3782 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3783 call assert_equal(2, l.nr)
3784 call assert_equal('Test5', l.title)
3785
3786 " Add a quickfix list using '$' as the list number.
3787 let lastqf = g:Xgetlist({'nr':'$'}).nr
3788 silent! 99Xolder
3789 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
3790 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3791 call assert_equal(lastqf + 1, l.nr)
3792 call assert_equal('Test6', l.title)
3793
3794 " Add a quickfix list using 'nr' set to one more than the quickfix
3795 " list size.
3796 let lastqf = g:Xgetlist({'nr':'$'}).nr
3797 silent! 99Xolder
3798 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
3799 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3800 call assert_equal(lastqf + 1, l.nr)
3801 call assert_equal('Test7', l.title)
3802
3803 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
3804 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
3805 silent! 99Xolder
3806 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
3807 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3808 call assert_equal(10, l.nr)
3809 call assert_equal('Test8', l.title)
3810
3811 " Add a quickfix list using 'nr' set to a value greater than 10
3812 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
3813
3814 " Try adding a quickfix list with 'nr' set to a value greater than the
3815 " quickfix list size but less than 10.
3816 call g:Xsetlist([], 'f')
3817 Xexpr "" | Xexpr "" | Xexpr ""
3818 silent! 99Xolder
3819 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
3820
3821 " Add a quickfix list using 'nr' set to a some string or list
3822 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
3823endfunc
3824
3825func Test_add_qf()
3826 call XaddQf_tests('c')
3827 call XaddQf_tests('l')
3828endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003829
3830" Test for getting the quickfix list items from some text without modifying
3831" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003832func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003833 call s:setup_commands(a:cchar)
3834 call g:Xsetlist([], 'f')
3835
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003836 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003837 call assert_equal(2, len(l))
3838 call assert_equal(30, l[1].lnum)
3839
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003840 call assert_equal({}, g:Xgetlist({'lines' : 10}))
3841 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
3842 call assert_equal([], g:Xgetlist({'lines' : []}).items)
3843 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003844
Bram Moolenaar36538222017-09-02 19:51:44 +02003845 " Parse text using a custom efm
3846 set efm&
3847 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
3848 call assert_equal('Line30', l[0].text)
3849 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
3850 call assert_equal('File3:30:Line30', l[0].text)
3851 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
3852 call assert_equal({}, l)
3853 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
3854 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
3855
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003856 " Make sure that the quickfix stack is not modified
3857 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
3858endfunc
3859
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003860func Test_get_list_from_lines()
3861 call XgetListFromLines('c')
3862 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003863endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003864
3865" Tests for the quickfix list id
3866func Xqfid_tests(cchar)
3867 call s:setup_commands(a:cchar)
3868
3869 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003870 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003871 Xexpr ''
3872 let start_id = g:Xgetlist({'id' : 0}).id
3873 Xexpr '' | Xexpr ''
3874 Xolder
3875 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
3876 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
3877 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003878 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003879 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003880 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3881 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003882
3883 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3884 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003885 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003886 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3887 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3888 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3889
3890 let qfid = g:Xgetlist({'id':0, 'nr':0})
3891 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003892 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003893endfunc
3894
3895func Test_qf_id()
3896 call Xqfid_tests('c')
3897 call Xqfid_tests('l')
3898endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003899
3900func Xqfjump_tests(cchar)
3901 call s:setup_commands(a:cchar)
3902
3903 call writefile(["Line1\tFoo", "Line2"], 'F1')
3904 call writefile(["Line1\tBar", "Line2"], 'F2')
3905 call writefile(["Line1\tBaz", "Line2"], 'F3')
3906
3907 call g:Xsetlist([], 'f')
3908
3909 " Tests for
3910 " Jumping to a line using a pattern
3911 " Jumping to a column greater than the last column in a line
3912 " Jumping to a line greater than the last line in the file
3913 let l = []
3914 for i in range(1, 7)
3915 call add(l, {})
3916 endfor
3917 let l[0].filename='F1'
3918 let l[0].pattern='Line1'
3919 let l[1].filename='F2'
3920 let l[1].pattern='Line1'
3921 let l[2].filename='F3'
3922 let l[2].pattern='Line1'
3923 let l[3].filename='F3'
3924 let l[3].lnum=1
3925 let l[3].col=9
3926 let l[3].vcol=1
3927 let l[4].filename='F3'
3928 let l[4].lnum=99
3929 let l[5].filename='F3'
3930 let l[5].lnum=1
3931 let l[5].col=99
3932 let l[5].vcol=1
3933 let l[6].filename='F3'
3934 let l[6].pattern='abcxyz'
3935
3936 call g:Xsetlist([], ' ', {'items' : l})
3937 Xopen | only
3938 2Xnext
3939 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003940 call assert_equal('F3', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +01003941 Xnext
3942 call assert_equal(7, col('.'))
3943 Xnext
3944 call assert_equal(2, line('.'))
3945 Xnext
3946 call assert_equal(9, col('.'))
3947 2
3948 Xnext
3949 call assert_equal(2, line('.'))
3950
3951 if a:cchar == 'l'
3952 " When jumping to a location list entry in the location list window and
3953 " no usable windows are available, then a new window should be opened.
3954 enew! | new | only
3955 call g:Xsetlist([], 'f')
3956 setlocal buftype=nofile
3957 new
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01003958 let lines =<< trim END
3959 F1:1:1:Line1
3960 F1:2:2:Line2
3961 F2:1:1:Line1
3962 F2:2:2:Line2
3963 F3:1:1:Line1
3964 F3:2:2:Line2
3965 END
3966 call g:Xsetlist([], ' ', {'lines': lines})
Bram Moolenaar74240d32017-12-10 15:26:15 +01003967 Xopen
3968 let winid = win_getid()
3969 wincmd p
3970 close
3971 call win_gotoid(winid)
3972 Xnext
3973 call assert_equal(3, winnr('$'))
3974 call assert_equal(1, winnr())
3975 call assert_equal(2, line('.'))
3976
3977 " When jumping to an entry in the location list window and the window
3978 " associated with the location list is not present and a window containing
3979 " the file is already present, then that window should be used.
3980 close
3981 belowright new
3982 call g:Xsetlist([], 'f')
3983 edit F3
3984 call win_gotoid(winid)
3985 Xlast
3986 call assert_equal(3, winnr())
3987 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3988 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3989 endif
3990
3991 " Cleanup
3992 enew!
3993 new | only
3994
3995 call delete('F1')
3996 call delete('F2')
3997 call delete('F3')
3998endfunc
3999
4000func Test_qfjump()
4001 call Xqfjump_tests('c')
4002 call Xqfjump_tests('l')
4003endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01004004
4005" Tests for the getqflist() and getloclist() functions when the list is not
4006" present or is empty
4007func Xgetlist_empty_tests(cchar)
4008 call s:setup_commands(a:cchar)
4009
4010 " Empty quickfix stack
4011 call g:Xsetlist([], 'f')
4012 call assert_equal('', g:Xgetlist({'context' : 0}).context)
4013 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
4014 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
4015 call assert_equal([], g:Xgetlist({'items' : 0}).items)
4016 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
4017 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
4018 call assert_equal('', g:Xgetlist({'title' : 0}).title)
4019 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004020 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004021 if a:cchar == 'c'
4022 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004023 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004024 \ 'title' : '', 'winid' : 0, 'changedtick': 0,
4025 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004026 else
4027 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
4028 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004029 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004030 \ 'qfbufnr' : 0, 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004031 \ g:Xgetlist({'all' : 0}))
4032 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004033
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004034 " Quickfix window with empty stack
4035 silent! Xopen
4036 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004037 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01004038 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
4039 Xclose
4040
Bram Moolenaara6d48492017-12-12 22:45:31 +01004041 " Empty quickfix list
4042 Xexpr ""
4043 call assert_equal('', g:Xgetlist({'context' : 0}).context)
4044 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
4045 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
4046 call assert_equal([], g:Xgetlist({'items' : 0}).items)
4047 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
4048 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
4049 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
4050 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004051 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01004052
4053 let qfid = g:Xgetlist({'id' : 0}).id
4054 call g:Xsetlist([], 'f')
4055
4056 " Non-existing quickfix identifier
4057 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
4058 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
4059 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
4060 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
4061 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
4062 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
4063 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
4064 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004065 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004066 if a:cchar == 'c'
4067 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4068 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004069 \ 'qfbufnr' : qfbufnr, 'quickfixtextfunc' : '',
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004070 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
4071 else
4072 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4073 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004074 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4075 \ 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004076 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
4077 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004078
4079 " Non-existing quickfix list number
4080 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
4081 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
4082 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
4083 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
4084 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
4085 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
4086 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
4087 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01004088 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004089 if a:cchar == 'c'
4090 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4091 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004092 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr,
4093 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004094 else
4095 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4096 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004097 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4098 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004099 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004100endfunc
4101
4102func Test_getqflist()
4103 call Xgetlist_empty_tests('c')
4104 call Xgetlist_empty_tests('l')
4105endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01004106
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01004107func Test_getqflist_invalid_nr()
4108 " The following commands used to crash Vim
4109 cexpr ""
4110 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
4111
4112 " Cleanup
4113 call setqflist([], 'r')
4114endfunc
4115
Bram Moolenaarb254af32017-12-18 19:48:58 +01004116" Tests for the quickfix/location list changedtick
4117func Xqftick_tests(cchar)
4118 call s:setup_commands(a:cchar)
4119
4120 call g:Xsetlist([], 'f')
4121
4122 Xexpr "F1:10:Line10"
4123 let qfid = g:Xgetlist({'id' : 0}).id
4124 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4125 Xaddexpr "F2:20:Line20\nF2:21:Line21"
4126 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4127 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
4128 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
4129 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
4130 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
4131 call g:Xsetlist([], 'a', {'title' : 'New Title'})
4132 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
4133
4134 enew!
4135 call append(0, ["F5:50:L50", "F6:60:L60"])
4136 Xaddbuffer
4137 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
4138 enew!
4139
4140 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
4141 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
4142 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4143 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
4144 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
4145 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4146 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
4147 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4148 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4149 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
4150 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4151
4152 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
4153 Xfile Xone
4154 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4155 Xaddfile Xone
4156 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4157
4158 " Test case for updating a non-current quickfix list
4159 call g:Xsetlist([], 'f')
4160 Xexpr "F1:1:L1"
4161 Xexpr "F2:2:L2"
4162 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
4163 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4164 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
4165
4166 call delete("Xone")
4167endfunc
4168
4169func Test_qf_tick()
4170 call Xqftick_tests('c')
4171 call Xqftick_tests('l')
4172endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01004173
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02004174" Test helpgrep with lang specifier
4175func Xtest_helpgrep_with_lang_specifier(cchar)
4176 call s:setup_commands(a:cchar)
4177 Xhelpgrep Vim@en
4178 call assert_equal('help', &filetype)
4179 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
4180 new | only
4181endfunc
4182
4183func Test_helpgrep_with_lang_specifier()
4184 call Xtest_helpgrep_with_lang_specifier('c')
4185 call Xtest_helpgrep_with_lang_specifier('l')
4186endfunc
4187
Bram Moolenaar12237442017-12-19 12:38:52 +01004188" The following test used to crash Vim.
4189" Open the location list window and close the regular window associated with
4190" the location list. When the garbage collection runs now, it incorrectly
4191" marks the location list context as not in use and frees the context.
4192func Test_ll_window_ctx()
4193 call setloclist(0, [], 'f')
4194 call setloclist(0, [], 'a', {'context' : []})
4195 lopen | only
4196 call test_garbagecollect_now()
4197 echo getloclist(0, {'context' : 1}).context
4198 enew | only
4199endfunc
4200
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004201" The following test used to crash vim
4202func Test_lfile_crash()
4203 sp Xtest
4204 au QuickFixCmdPre * bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02004205 call assert_fails('lfile', 'E40:')
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004206 au! QuickFixCmdPre
4207endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01004208
4209" The following test used to crash vim
4210func Test_lbuffer_crash()
4211 sv Xtest
4212 augroup QF_Test
4213 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004214 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bw
Bram Moolenaar3c097222017-12-21 20:54:49 +01004215 augroup END
4216 lbuffer
4217 augroup QF_Test
4218 au!
4219 augroup END
4220endfunc
4221
4222" The following test used to crash vim
4223func Test_lexpr_crash()
4224 augroup QF_Test
4225 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004226 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004227 augroup END
4228 lexpr ""
4229 augroup QF_Test
4230 au!
4231 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004232
Bram Moolenaar3c097222017-12-21 20:54:49 +01004233 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004234 augroup QF_Test
4235 au!
4236 au BufNew * call setloclist(0, [], 'f')
4237 augroup END
4238 lexpr 'x:1:x'
4239 augroup QF_Test
4240 au!
4241 augroup END
4242
4243 enew | only
4244 lexpr ''
4245 lopen
4246 augroup QF_Test
4247 au!
4248 au FileType * call setloclist(0, [], 'f')
4249 augroup END
4250 lexpr ''
4251 augroup QF_Test
4252 au!
4253 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01004254endfunc
4255
4256" The following test used to crash Vim
4257func Test_lvimgrep_crash()
4258 sv Xtest
4259 augroup QF_Test
4260 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004261 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004262 augroup END
4263 lvimgrep quickfix test_quickfix.vim
4264 augroup QF_Test
4265 au!
4266 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004267
4268 new | only
4269 augroup QF_Test
4270 au!
4271 au BufEnter * call setloclist(0, [], 'r')
4272 augroup END
4273 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
4274 augroup QF_Test
4275 au!
4276 augroup END
4277
Bram Moolenaar3c097222017-12-21 20:54:49 +01004278 enew | only
4279endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01004280
Bram Moolenaar2573af32020-03-14 17:21:34 +01004281func Test_lvimgrep_crash2()
4282 au BufNewFile x sfind
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004283 call assert_fails('lvimgrep x x', 'E471:')
4284 call assert_fails('lvimgrep x x x', 'E471:')
Bram Moolenaar2573af32020-03-14 17:21:34 +01004285
4286 au! BufNewFile
4287endfunc
4288
Bram Moolenaarde046542017-12-26 13:53:11 +01004289" Test for the position of the quickfix and location list window
4290func Test_qfwin_pos()
4291 " Open two windows
4292 new | only
4293 new
4294 cexpr ['F1:10:L10']
4295 copen
4296 " Quickfix window should be the bottom most window
4297 call assert_equal(3, winnr())
4298 close
4299 " Open at the very top
4300 wincmd t
4301 topleft copen
4302 call assert_equal(1, winnr())
4303 close
4304 " open left of the current window
4305 wincmd t
4306 below new
4307 leftabove copen
4308 call assert_equal(2, winnr())
4309 close
4310 " open right of the current window
4311 rightbelow copen
4312 call assert_equal(3, winnr())
4313 close
4314endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004315
4316" Tests for quickfix/location lists changed by autocommands when
4317" :vimgrep/:lvimgrep commands are running.
4318func Test_vimgrep_autocmd()
4319 call setqflist([], 'f')
4320 call writefile(['stars'], 'Xtest1.txt')
4321 call writefile(['stars'], 'Xtest2.txt')
4322
4323 " Test 1:
4324 " When searching for a pattern using :vimgrep, if the quickfix list is
4325 " changed by an autocmd, the results should be added to the correct quickfix
4326 " list.
4327 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
4328 silent vimgrep stars Xtest*.txt
4329 call assert_equal(1, getqflist({'nr' : 0}).nr)
4330 call assert_equal(3, getqflist({'nr' : '$'}).nr)
4331 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
4332 au! BufRead Xtest2.txt
4333
4334 " Test 2:
4335 " When searching for a pattern using :vimgrep, if the quickfix list is
4336 " freed, then a error should be given.
4337 silent! %bwipe!
4338 call setqflist([], 'f')
4339 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
4340 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
4341 au! BufRead Xtest2.txt
4342
4343 " Test 3:
4344 " When searching for a pattern using :lvimgrep, if the location list is
4345 " freed, then the command should error out.
4346 silent! %bwipe!
4347 let g:save_winid = win_getid()
4348 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
4349 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
4350 au! BufRead Xtest2.txt
4351
4352 call delete('Xtest1.txt')
4353 call delete('Xtest2.txt')
4354 call setqflist([], 'f')
4355endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004356
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004357" Test for an autocmd changing the current directory when running vimgrep
4358func Xvimgrep_autocmd_cd(cchar)
4359 call s:setup_commands(a:cchar)
4360
4361 %bwipe
4362 let save_cwd = getcwd()
4363
4364 augroup QF_Test
4365 au!
4366 autocmd BufRead * silent cd %:p:h
4367 augroup END
4368
4369 10Xvimgrep /vim/ Xdir/**
4370 let l = g:Xgetlist()
4371 call assert_equal('f1.txt', bufname(l[0].bufnr))
4372 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
4373
4374 augroup QF_Test
4375 au!
4376 augroup END
4377
4378 exe 'cd ' . save_cwd
4379endfunc
4380
4381func Test_vimgrep_autocmd_cd()
4382 call mkdir('Xdir/a', 'p')
4383 call mkdir('Xdir/b', 'p')
4384 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xdir/a/f1.txt')
4385 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xdir/b/f2.txt')
4386 call Xvimgrep_autocmd_cd('c')
4387 call Xvimgrep_autocmd_cd('l')
4388 %bwipe
4389 call delete('Xdir', 'rf')
4390endfunc
4391
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004392" The following test used to crash Vim
4393func Test_lhelpgrep_autocmd()
4394 lhelpgrep quickfix
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004395 augroup QF_Test
4396 au!
4397 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
4398 augroup END
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004399 lhelpgrep buffer
4400 call assert_equal('help', &filetype)
4401 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
4402 lhelpgrep tabpage
4403 call assert_equal('help', &filetype)
4404 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004405 augroup QF_Test
4406 au!
4407 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004408
4409 new | only
4410 augroup QF_Test
4411 au!
4412 au BufEnter * call setqflist([], 'f')
4413 augroup END
4414 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaardbfa7952020-11-02 20:04:22 +01004415 " run the test with a help window already open
4416 help
4417 wincmd w
4418 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004419 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004420 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004421 augroup END
4422
4423 new | only
4424 augroup QF_Test
4425 au!
4426 au BufEnter * call setqflist([], 'r')
4427 augroup END
4428 call assert_fails('helpgrep quickfix', 'E925:')
4429 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004430 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004431 augroup END
4432
4433 new | only
4434 augroup QF_Test
4435 au!
4436 au BufEnter * call setloclist(0, [], 'r')
4437 augroup END
4438 call assert_fails('lhelpgrep quickfix', 'E926:')
4439 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004440 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004441 augroup END
4442
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004443 " Replace the contents of a help window location list when it is still in
4444 " use.
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004445 new | only
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004446 lhelpgrep quickfix
4447 wincmd w
4448 augroup QF_Test
4449 au!
4450 autocmd WinEnter * call setloclist(0, [], 'r')
4451 augroup END
4452 call assert_fails('lhelpgrep win_getid', 'E926:')
4453 augroup QF_Test
4454 au!
4455 augroup END
4456
4457 %bw!
4458endfunc
4459
4460" The following test used to crash Vim
4461func Test_lhelpgrep_autocmd_free_loclist()
4462 %bw!
4463 lhelpgrep quickfix
4464 wincmd w
4465 augroup QF_Test
4466 au!
4467 autocmd WinEnter * call setloclist(0, [], 'f')
4468 augroup END
4469 lhelpgrep win_getid
4470 wincmd w
4471 wincmd w
4472 wincmd w
4473 augroup QF_Test
4474 au!
4475 augroup END
4476 %bw!
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004477endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02004478
4479" Test for shortening/simplifying the file name when opening the
4480" quickfix window or when displaying the quickfix list
4481func Test_shorten_fname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02004482 CheckUnix
Bram Moolenaara796d462018-05-01 14:30:36 +02004483 %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 let fname = getcwd() . '/test_quickfix.vim'
4486 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4487 call assert_equal(fname, bufname('test_quickfix.vim'))
4488 " Opening the quickfix window should simplify the file path
4489 cwindow
4490 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
4491 cclose
4492 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004493 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004494 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4495 call assert_equal(fname, bufname('test_quickfix.vim'))
4496 " Displaying the quickfix list should simplify the file path
4497 silent! clist
4498 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004499 " Add a few entries for the same file with different paths and check whether
4500 " the buffer name is shortened
4501 %bwipe
4502 call setqflist([], 'f')
4503 call setqflist([{'filename' : 'test_quickfix.vim', 'lnum' : 10},
4504 \ {'filename' : '../testdir/test_quickfix.vim', 'lnum' : 20},
4505 \ {'filename' : fname, 'lnum' : 30}], ' ')
4506 copen
4507 call assert_equal(['test_quickfix.vim|10| ',
4508 \ 'test_quickfix.vim|20| ',
4509 \ 'test_quickfix.vim|30| '], getline(1, '$'))
4510 cclose
Bram Moolenaara796d462018-05-01 14:30:36 +02004511endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004512
4513" Quickfix title tests
4514" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
4515" Otherwise due to indentation, the title is set with spaces at the beginning
4516" of the command.
4517func Test_qftitle()
4518 call writefile(["F1:1:Line1"], 'Xerr')
4519
4520 " :cexpr
4521 exe "cexpr readfile('Xerr')"
4522 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
4523
4524 " :cgetexpr
4525 exe "cgetexpr readfile('Xerr')"
4526 call assert_equal(":cgetexpr readfile('Xerr')",
4527 \ getqflist({'title' : 1}).title)
4528
4529 " :caddexpr
4530 call setqflist([], 'f')
4531 exe "caddexpr readfile('Xerr')"
4532 call assert_equal(":caddexpr readfile('Xerr')",
4533 \ getqflist({'title' : 1}).title)
4534
4535 " :cbuffer
4536 new Xerr
4537 exe "cbuffer"
4538 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
4539
4540 " :cgetbuffer
4541 edit Xerr
4542 exe "cgetbuffer"
4543 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
4544
4545 " :caddbuffer
4546 call setqflist([], 'f')
4547 edit Xerr
4548 exe "caddbuffer"
4549 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
4550
4551 " :cfile
4552 exe "cfile Xerr"
4553 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
4554
4555 " :cgetfile
4556 exe "cgetfile Xerr"
4557 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
4558
4559 " :caddfile
4560 call setqflist([], 'f')
4561 exe "caddfile Xerr"
4562 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
4563
4564 " :grep
4565 set grepprg=internal
4566 exe "grep F1 Xerr"
4567 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
4568
4569 " :grepadd
4570 call setqflist([], 'f')
4571 exe "grepadd F1 Xerr"
4572 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
4573 set grepprg&vim
4574
4575 " :vimgrep
4576 exe "vimgrep F1 Xerr"
4577 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
4578
4579 " :vimgrepadd
4580 call setqflist([], 'f')
4581 exe "vimgrepadd F1 Xerr"
4582 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
4583
4584 call setqflist(['F1:10:L10'], ' ')
4585 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4586
4587 call setqflist([], 'f')
4588 call setqflist(['F1:10:L10'], 'a')
4589 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4590
4591 call setqflist([], 'f')
4592 call setqflist(['F1:10:L10'], 'r')
4593 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4594
4595 close
4596 call delete('Xerr')
4597
4598 call setqflist([], ' ', {'title' : 'Errors'})
4599 copen
4600 call assert_equal('Errors', w:quickfix_title)
4601 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
4602 call assert_equal('Errors', w:quickfix_title)
4603 cclose
Bram Moolenaar530bed92020-12-16 21:02:56 +01004604
4605 " Switching to another quickfix list in one tab page should update the
4606 " quickfix window title and statusline in all the other tab pages also
4607 call setqflist([], 'f')
4608 %bw!
4609 cgetexpr ['file_one:1:1: error in the first quickfix list']
4610 call setqflist([], 'a', {'title': 'first quickfix list'})
4611 cgetexpr ['file_two:2:1: error in the second quickfix list']
4612 call setqflist([], 'a', {'title': 'second quickfix list'})
4613 copen
4614 wincmd t
4615 tabnew two
4616 copen
4617 wincmd t
4618 colder
4619 call assert_equal('first quickfix list', gettabwinvar(1, 2, 'quickfix_title'))
4620 call assert_equal('first quickfix list', gettabwinvar(2, 2, 'quickfix_title'))
4621 call assert_equal(1, tabpagewinnr(1))
4622 call assert_equal(1, tabpagewinnr(2))
4623 tabnew
4624 call setqflist([], 'a', {'title': 'new quickfix title'})
4625 call assert_equal('new quickfix title', gettabwinvar(1, 2, 'quickfix_title'))
4626 call assert_equal('new quickfix title', gettabwinvar(2, 2, 'quickfix_title'))
4627 %bw!
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004628endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02004629
4630func Test_lbuffer_with_bwipe()
4631 new
4632 new
4633 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004634 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bwipe
Bram Moolenaar600323b2018-06-16 22:16:47 +02004635 augroup END
4636 lbuffer
4637 augroup nasty
4638 au!
4639 augroup END
4640endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02004641
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004642" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
4643" running
4644func Xexpr_acmd_freelist(cchar)
4645 call s:setup_commands(a:cchar)
4646
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004647 " This was using freed memory (but with what events?)
Bram Moolenaar0366c012018-06-18 20:52:13 +02004648 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004649 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02004650 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004651 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02004652 augroup nasty
4653 au!
4654 augroup END
4655endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004656
4657func Test_cexpr_acmd_freelist()
4658 call Xexpr_acmd_freelist('c')
4659 call Xexpr_acmd_freelist('l')
4660endfunc
4661
4662" Test for commands that create a new quickfix/location list and jump to the
4663" first error automatically.
4664func Xjumpto_first_error_test(cchar)
4665 call s:setup_commands(a:cchar)
4666
4667 call s:create_test_file('Xtestfile1')
4668 call s:create_test_file('Xtestfile2')
4669 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4670
4671 " Test for cexpr/lexpr
4672 enew
4673 Xexpr l
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004674 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004675 call assert_equal(2, line('.'))
4676
4677 " Test for cfile/lfile
4678 enew
4679 call writefile(l, 'Xerr')
4680 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004681 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004682 call assert_equal(2, line('.'))
4683
4684 " Test for cbuffer/lbuffer
4685 edit Xerr
4686 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004687 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004688 call assert_equal(2, line('.'))
4689
4690 call delete('Xerr')
4691 call delete('Xtestfile1')
4692 call delete('Xtestfile2')
4693endfunc
4694
4695func Test_jumpto_first_error()
4696 call Xjumpto_first_error_test('c')
4697 call Xjumpto_first_error_test('l')
4698endfunc
4699
4700" Test for a quickfix autocmd changing the quickfix/location list before
4701" jumping to the first error in the new list.
4702func Xautocmd_changelist(cchar)
4703 call s:setup_commands(a:cchar)
4704
4705 " Test for cfile/lfile
4706 call s:create_test_file('Xtestfile1')
4707 call s:create_test_file('Xtestfile2')
4708 Xexpr 'Xtestfile1:2:Line2'
4709 autocmd QuickFixCmdPost * Xolder
4710 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4711 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004712 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004713 call assert_equal(4, line('.'))
4714 autocmd! QuickFixCmdPost
4715
4716 " Test for cbuffer/lbuffer
4717 call g:Xsetlist([], 'f')
4718 Xexpr 'Xtestfile1:2:Line2'
4719 autocmd QuickFixCmdPost * Xolder
4720 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4721 edit Xerr
4722 Xbuffer
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
4727 " Test for cexpr/lexpr
4728 call g:Xsetlist([], 'f')
4729 Xexpr 'Xtestfile1:2:Line2'
4730 autocmd QuickFixCmdPost * Xolder
4731 Xexpr 'Xtestfile2:4:Line4'
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004732 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004733 call assert_equal(4, line('.'))
4734 autocmd! QuickFixCmdPost
4735
Bram Moolenaar851332e2018-07-03 19:16:00 +02004736 " The grepprg may not be set on non-Unix systems
4737 if has('unix')
4738 " Test for grep/lgrep
4739 call g:Xsetlist([], 'f')
4740 Xexpr 'Xtestfile1:2:Line2'
4741 autocmd QuickFixCmdPost * Xolder
4742 silent Xgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004743 call assert_equal('Xtestfile2', @%)
Bram Moolenaar851332e2018-07-03 19:16:00 +02004744 call assert_equal(5, line('.'))
4745 autocmd! QuickFixCmdPost
4746 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004747
4748 " Test for vimgrep/lvimgrep
4749 call g:Xsetlist([], 'f')
4750 Xexpr 'Xtestfile1:2:Line2'
4751 autocmd QuickFixCmdPost * Xolder
4752 silent Xvimgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004753 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004754 call assert_equal(5, line('.'))
4755 autocmd! QuickFixCmdPost
4756
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004757 " Test for autocommands clearing the quickfix list before jumping to the
4758 " first error. This should not result in an error
4759 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
4760 let v:errmsg = ''
4761 " Test for cfile/lfile
4762 Xfile Xerr
4763 call assert_true(v:errmsg !~# 'E42:')
4764 " Test for cbuffer/lbuffer
4765 edit Xerr
4766 Xbuffer
4767 call assert_true(v:errmsg !~# 'E42:')
4768 " Test for cexpr/lexpr
4769 Xexpr 'Xtestfile2:4:Line4'
4770 call assert_true(v:errmsg !~# 'E42:')
4771 " Test for grep/lgrep
4772 " The grepprg may not be set on non-Unix systems
4773 if has('unix')
4774 silent Xgrep Line5 Xtestfile2
4775 call assert_true(v:errmsg !~# 'E42:')
4776 endif
4777 " Test for vimgrep/lvimgrep
4778 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
4779 autocmd! QuickFixCmdPost
4780
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004781 call delete('Xerr')
4782 call delete('Xtestfile1')
4783 call delete('Xtestfile2')
4784endfunc
4785
4786func Test_autocmd_changelist()
4787 call Xautocmd_changelist('c')
4788 call Xautocmd_changelist('l')
4789endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004790
4791" Tests for the ':filter /pat/ clist' command
4792func Test_filter_clist()
4793 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4794 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4795 \ split(execute('filter /Line 15/ clist'), "\n"))
4796 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4797 \ split(execute('filter /Xfile1/ clist'), "\n"))
4798 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4799
4800 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4801 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4802 call assert_equal([' 2 pqr:pat2: '],
4803 \ split(execute('filter /pqr/ clist'), "\n"))
4804 call assert_equal([' 1 abc:pat1: '],
4805 \ split(execute('filter /pat1/ clist'), "\n"))
4806endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004807
4808" Tests for the "CTRL-W <CR>" command.
4809func Xview_result_split_tests(cchar)
4810 call s:setup_commands(a:cchar)
4811
4812 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4813 call g:Xsetlist([])
4814 Xopen
4815 let l:win_count = winnr('$')
Bram Moolenaare2e40752020-09-04 21:18:46 +02004816 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42:')
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004817 call assert_equal(l:win_count, winnr('$'))
4818 Xclose
4819endfunc
4820
4821func Test_view_result_split()
4822 call Xview_result_split_tests('c')
4823 call Xview_result_split_tests('l')
4824endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004825
4826" Test that :cc sets curswant
4827func Test_curswant()
4828 helpgrep quickfix
4829 normal! llll
4830 1cc
4831 call assert_equal(getcurpos()[4], virtcol('.'))
4832 cclose | helpclose
4833endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004834
4835" Test for opening a file from the quickfix window using CTRL-W <Enter>
4836" doesn't leave an empty buffer around.
4837func Test_splitview()
4838 call s:create_test_file('Xtestfile1')
4839 call s:create_test_file('Xtestfile2')
4840 new | only
4841 let last_bufnr = bufnr('Test_sv_1', 1)
4842 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4843 cgetexpr l
4844 copen
4845 let numbufs = len(getbufinfo())
4846 exe "normal \<C-W>\<CR>"
4847 copen
4848 exe "normal j\<C-W>\<CR>"
4849 " Make sure new empty buffers are not created
4850 call assert_equal(numbufs, len(getbufinfo()))
4851 " Creating a new buffer should use the next available buffer number
4852 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4853 bwipe Test_sv_1
4854 bwipe Test_sv_2
4855 new | only
4856
4857 " When split opening files from location list window, make sure that two
4858 " windows doesn't refer to the same location list
4859 lgetexpr l
4860 let locid = getloclist(0, {'id' : 0}).id
4861 lopen
4862 exe "normal \<C-W>\<CR>"
4863 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4864 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4865 new | only
4866
4867 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004868 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004869 lhelpgrep window
4870 let locid = getloclist(0, {'id' : 0}).id
4871 lwindow
4872 exe "normal j\<C-W>\<CR>"
4873 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4874 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4875 new | only
4876
Bram Moolenaar406cd902020-02-18 21:54:41 +01004877 " Using :split or :vsplit from a quickfix window should behave like a :new
4878 " or a :vnew command
4879 copen
4880 split
4881 call assert_equal(3, winnr('$'))
4882 let l = getwininfo()
4883 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4884 close
4885 copen
4886 vsplit
4887 let l = getwininfo()
4888 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4889 new | only
4890
Bram Moolenaarb2443732018-11-11 22:50:27 +01004891 call delete('Xtestfile1')
4892 call delete('Xtestfile2')
4893endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004894
4895" Test for parsing entries using visual screen column
4896func Test_viscol()
4897 enew
4898 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4899 edit Xfile1
4900
4901 " Use byte offset for column number
4902 set efm&
4903 cexpr "Xfile1:1:5:XX\nXfile1:1:9: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
4910 " Use screen column offset for column number
4911 set efm=%f:%l:%v:%m
4912 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4913 call assert_equal([5, 8], [col('.'), virtcol('.')])
4914 cnext
4915 call assert_equal([9, 12], [col('.'), virtcol('.')])
4916 cnext
4917 call assert_equal([14, 20], [col('.'), virtcol('.')])
4918 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4919 call assert_equal([5, 8], [col('.'), virtcol('.')])
4920 cnext
4921 call assert_equal([10, 16], [col('.'), virtcol('.')])
4922 cnext
4923 call assert_equal([14, 20], [col('.'), virtcol('.')])
4924
4925 enew
4926 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4927
4928 " Use byte offset for column number
4929 set efm&
4930 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16: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
4937 " Use screen column offset for column number
4938 set efm=%f:%l:%v:%m
4939 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4940 call assert_equal([8, 10], [col('.'), virtcol('.')])
4941 cnext
4942 call assert_equal([11, 17], [col('.'), virtcol('.')])
4943 cnext
4944 call assert_equal([16, 25], [col('.'), virtcol('.')])
4945
Bram Moolenaarc95940c2020-10-20 14:59:12 +02004946 " Use screen column number with a multi-line error message
4947 enew
4948 call writefile(["à test"], 'Xfile1')
4949 set efm=%E===\ %f\ ===,%C%l:%v,%Z%m
4950 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4951 call assert_equal('Xfile1', @%)
4952 call assert_equal([0, 1, 4, 0], getpos('.'))
4953
4954 " Repeat previous test with byte offset %c: ensure that fix to issue #7145
4955 " does not break this
4956 set efm=%E===\ %f\ ===,%C%l:%c,%Z%m
4957 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4958 call assert_equal('Xfile1', @%)
4959 call assert_equal([0, 1, 3, 0], getpos('.'))
4960
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004961 enew | only
4962 set efm&
4963 call delete('Xfile1')
4964endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004965
4966" Test for the quickfix window buffer
4967func Xqfbuf_test(cchar)
4968 call s:setup_commands(a:cchar)
4969
4970 " Quickfix buffer should be reused across closing and opening a quickfix
4971 " window
4972 Xexpr "F1:10:Line10"
4973 Xopen
4974 let qfbnum = bufnr('')
4975 Xclose
4976 " Even after the quickfix window is closed, the buffer should be loaded
4977 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004978 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004979 Xopen
4980 " Buffer should be reused when opening the window again
4981 call assert_equal(qfbnum, bufnr(''))
4982 Xclose
4983
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00004984 " When quickfix buffer is wiped out, getqflist() should return 0
4985 %bw!
4986 Xexpr ""
4987 Xopen
4988 bw!
4989 call assert_equal(0, g:Xgetlist({'qfbufnr': 0}).qfbufnr)
4990
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004991 if a:cchar == 'l'
4992 %bwipe
4993 " For a location list, when both the file window and the location list
4994 " window for the list are closed, then the buffer should be freed.
4995 new | only
4996 lexpr "F1:10:Line10"
4997 let wid = win_getid()
4998 lopen
4999 let qfbnum = bufnr('')
5000 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
5001 close
5002 " When the location list window is closed, the buffer name should not
5003 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005004 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005005 call assert_true(bufloaded(qfbnum))
5006
Bram Moolenaard82a81c2019-03-02 07:57:18 +01005007 " After deleting a location list buffer using ":bdelete", opening the
5008 " location list window should mark the buffer as a location list buffer.
5009 exe "bdelete " . qfbnum
5010 lopen
5011 call assert_equal("quickfix", &buftype)
5012 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
5013 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
5014 call assert_false(&swapfile)
5015 lclose
5016
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005017 " When the location list is cleared for the window, the buffer should be
5018 " removed
5019 call setloclist(0, [], 'f')
5020 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01005021 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005022
5023 " When the location list is freed with the location list window open, the
5024 " location list buffer should not be lost. It should be reused when the
5025 " location list is again populated.
5026 lexpr "F1:10:Line10"
5027 lopen
5028 let wid = win_getid()
5029 let qfbnum = bufnr('')
5030 wincmd p
5031 call setloclist(0, [], 'f')
5032 lexpr "F1:10:Line10"
5033 lopen
5034 call assert_equal(wid, win_getid())
5035 call assert_equal(qfbnum, bufnr(''))
5036 lclose
5037
5038 " When the window with the location list is closed, the buffer should be
5039 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005040 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005041 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01005042 endif
5043endfunc
5044
5045func Test_qfbuf()
5046 call Xqfbuf_test('c')
5047 call Xqfbuf_test('l')
5048endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005049
5050" If there is an autocmd to use only one window, then opening the location
5051" list window used to crash Vim.
5052func Test_winonly_autocmd()
5053 call s:create_test_file('Xtest1')
5054 " Autocmd to show only one Vim window at a time
5055 autocmd WinEnter * only
5056 new
5057 " Load the location list
5058 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
5059 let loclistid = getloclist(0, {'id' : 0}).id
5060 " Open the location list window. Only this window will be shown and the file
5061 " window is closed.
5062 lopen
5063 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
5064 " Jump to an entry in the location list and make sure that the cursor is
5065 " positioned correctly.
5066 ll 3
5067 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005068 call assert_equal('Xtest1', @%)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01005069 call assert_equal(15, line('.'))
5070 " Cleanup
5071 autocmd! WinEnter
5072 new | only
5073 call delete('Xtest1')
5074endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02005075
5076" Test to make sure that an empty quickfix buffer is not reused for loading
5077" a normal buffer.
5078func Test_empty_qfbuf()
5079 enew | only
5080 call writefile(["Test"], 'Xfile1')
5081 call setqflist([], 'f')
5082 copen | only
5083 let qfbuf = bufnr('')
5084 edit Xfile1
5085 call assert_notequal(qfbuf, bufnr(''))
5086 enew
5087 call delete('Xfile1')
5088endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005089
5090" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005091" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005092func Xtest_below(cchar)
5093 call s:setup_commands(a:cchar)
5094
5095 " No quickfix/location list
5096 call assert_fails('Xbelow', 'E42:')
5097 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005098 call assert_fails('Xbefore', 'E42:')
5099 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005100
5101 " Empty quickfix/location list
5102 call g:Xsetlist([])
5103 call assert_fails('Xbelow', 'E42:')
5104 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005105 call assert_fails('Xbefore', 'E42:')
5106 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005107
5108 call s:create_test_file('X1')
5109 call s:create_test_file('X2')
5110 call s:create_test_file('X3')
5111 call s:create_test_file('X4')
5112
5113 " Invalid entries
5114 edit X1
5115 call g:Xsetlist(["E1", "E2"])
5116 call assert_fails('Xbelow', 'E42:')
5117 call assert_fails('Xabove', 'E42:')
5118 call assert_fails('3Xbelow', 'E42:')
5119 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005120 call assert_fails('Xbefore', 'E42:')
5121 call assert_fails('Xafter', 'E42:')
5122 call assert_fails('3Xbefore', 'E42:')
5123 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005124
5125 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005126 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 +02005127 edit +7 X2
5128 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005129 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005130 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005131 normal 7G
5132 Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005133 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005134 call assert_fails('Xbefore', 'E553:')
5135
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005136 normal 2j
5137 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005138 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005139 normal 7G
5140 Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005141 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005142
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005143 " Last error in this file
5144 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005145 call assert_equal(['X2', 15], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005146 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005147 normal gg
5148 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005149 call assert_equal(['X2', 15, 4], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005150 call assert_fails('Xafter', 'E553:')
5151
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005152 " First error in this file
5153 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005154 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005155 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005156 normal G
5157 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005158 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005159 call assert_fails('Xbefore', 'E553:')
5160
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005161 normal gg
5162 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005163 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005164 normal gg
5165 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005166 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005167
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005168 normal G
5169 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005170 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005171 normal G
5172 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005173 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005174
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005175 edit X4
5176 call assert_fails('Xabove', 'E42:')
5177 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005178 call assert_fails('Xbefore', 'E42:')
5179 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005180 if a:cchar == 'l'
5181 " If a buffer has location list entries from some other window but not
5182 " from the current window, then the commands should fail.
5183 edit X1 | split | call setloclist(0, [], 'f')
5184 call assert_fails('Xabove', 'E776:')
5185 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005186 call assert_fails('Xbefore', 'E776:')
5187 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005188 close
5189 endif
5190
5191 " Test for lines with multiple quickfix entries
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005192 let lines =<< trim END
5193 X1:5:L5
5194 X2:5:1:L5_1
5195 X2:5:2:L5_2
5196 X2:5:3:L5_3
5197 X2:10:1:L10_1
5198 X2:10:2:L10_2
5199 X2:10:3:L10_3
5200 X2:15:1:L15_1
5201 X2:15:2:L15_2
5202 X2:15:3:L15_3
5203 X3:3:L3
5204 END
5205 Xexpr lines
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005206 edit +1 X2
5207 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005208 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005209 normal 1G
5210 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005211 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005212
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005213 normal gg
5214 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005215 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005216 normal gg
5217 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005218 call assert_equal(['X2', 15, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005219
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005220 normal G
5221 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005222 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005223 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005224 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005225 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005226
5227 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005228 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005229 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005230 normal G
5231 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005232 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005233
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005234 normal 10G
5235 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005236 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005237 normal 10G$
5238 2Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005239 call assert_equal(['X2', 10, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005240
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005241 normal 10G
5242 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005243 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005244 normal 9G
5245 5Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005246 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005247
5248 " Invalid range
5249 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02005250 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005251 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005252 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02005253 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005254 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005255 endif
5256
5257 call delete('X1')
5258 call delete('X2')
5259 call delete('X3')
5260 call delete('X4')
5261endfunc
5262
5263func Test_cbelow()
5264 call Xtest_below('c')
5265 call Xtest_below('l')
5266endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02005267
5268func Test_quickfix_count()
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01005269 let commands =<< trim END
5270 cNext
5271 cNfile
5272 cabove
5273 cbelow
5274 cfirst
5275 clast
5276 cnewer
5277 cnext
5278 cnfile
5279 colder
5280 cprevious
5281 crewind
5282 lNext
5283 lNfile
5284 labove
5285 lbelow
5286 lfirst
5287 llast
5288 lnewer
5289 lnext
5290 lnfile
5291 lolder
5292 lprevious
5293 lrewind
5294 END
Bram Moolenaar25190db2019-05-04 15:05:28 +02005295 for cmd in commands
5296 call assert_fails('-1' .. cmd, 'E16:')
5297 call assert_fails('.' .. cmd, 'E16:')
5298 call assert_fails('%' .. cmd, 'E16:')
5299 call assert_fails('$' .. cmd, 'E16:')
5300 endfor
5301endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005302
5303" Test for aborting quickfix commands using QuickFixCmdPre
5304func Xtest_qfcmd_abort(cchar)
5305 call s:setup_commands(a:cchar)
5306
5307 call g:Xsetlist([], 'f')
5308
5309 " cexpr/lexpr
5310 let e = ''
5311 try
5312 Xexpr ["F1:10:Line10", "F2:20:Line20"]
5313 catch /.*/
5314 let e = v:exception
5315 endtry
5316 call assert_equal('AbortCmd', e)
5317 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5318
5319 " cfile/lfile
5320 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
5321 let e = ''
5322 try
5323 Xfile Xfile1
5324 catch /.*/
5325 let e = v:exception
5326 endtry
5327 call assert_equal('AbortCmd', e)
5328 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5329 call delete('Xfile1')
5330
5331 " cgetbuffer/lgetbuffer
5332 enew!
5333 call append(0, ["F1:10:Line10", "F2:20:Line20"])
5334 let e = ''
5335 try
5336 Xgetbuffer
5337 catch /.*/
5338 let e = v:exception
5339 endtry
5340 call assert_equal('AbortCmd', e)
5341 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5342 enew!
5343
5344 " vimgrep/lvimgrep
5345 let e = ''
5346 try
5347 Xvimgrep /func/ test_quickfix.vim
5348 catch /.*/
5349 let e = v:exception
5350 endtry
5351 call assert_equal('AbortCmd', e)
5352 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5353
5354 " helpgrep/lhelpgrep
5355 let e = ''
5356 try
5357 Xhelpgrep quickfix
5358 catch /.*/
5359 let e = v:exception
5360 endtry
5361 call assert_equal('AbortCmd', e)
5362 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5363
5364 " grep/lgrep
5365 if has('unix')
5366 let e = ''
5367 try
5368 silent Xgrep func test_quickfix.vim
5369 catch /.*/
5370 let e = v:exception
5371 endtry
5372 call assert_equal('AbortCmd', e)
5373 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5374 endif
5375endfunc
5376
5377func Test_qfcmd_abort()
5378 augroup QF_Test
5379 au!
5380 autocmd QuickFixCmdPre * throw "AbortCmd"
5381 augroup END
5382
5383 call Xtest_qfcmd_abort('c')
5384 call Xtest_qfcmd_abort('l')
5385
5386 augroup QF_Test
5387 au!
5388 augroup END
5389endfunc
5390
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005391" Test for using a file in one of the parent directories.
5392func Test_search_in_dirstack()
5393 call mkdir('Xtestdir/a/b/c', 'p')
5394 let save_cwd = getcwd()
5395 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
5396 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
5397 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
5398 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
5399
5400 let lines = "Entering dir Xtestdir\n" .
5401 \ "Entering dir a\n" .
5402 \ "Entering dir b\n" .
5403 \ "Xfile2:2:X2_L2\n" .
5404 \ "Leaving dir a\n" .
5405 \ "Xfile1:2:X1_L2\n" .
5406 \ "Xfile3:1:X3_L1\n" .
5407 \ "Entering dir c\n" .
5408 \ "Xfile4:2:X4_L2\n" .
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005409 \ "Leaving dir c\n"
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005410 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005411 cexpr lines .. "Leaving dir Xtestdir|\n" | let next = 1
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005412 call assert_equal(11, getqflist({'size' : 0}).size)
5413 call assert_equal(4, getqflist({'idx' : 0}).idx)
5414 call assert_equal('X2_L2', getline('.'))
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005415 call assert_equal(1, next)
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005416 cnext
5417 call assert_equal(6, getqflist({'idx' : 0}).idx)
5418 call assert_equal('X1_L2', getline('.'))
5419 cnext
5420 call assert_equal(7, getqflist({'idx' : 0}).idx)
5421 call assert_equal(1, line('$'))
5422 call assert_equal('', getline(1))
5423 cnext
5424 call assert_equal(9, getqflist({'idx' : 0}).idx)
5425 call assert_equal(1, line('$'))
5426 call assert_equal('', getline(1))
5427
5428 set efm&
5429 exe 'cd ' . save_cwd
5430 call delete('Xtestdir', 'rf')
5431endfunc
5432
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005433" Test for :cquit
5434func Test_cquit()
5435 " Exit Vim with a non-zero value
5436 if RunVim([], ["cquit 7"], '')
5437 call assert_equal(7, v:shell_error)
5438 endif
5439
5440 if RunVim([], ["50cquit"], '')
5441 call assert_equal(50, v:shell_error)
5442 endif
5443
5444 " Exit Vim with default value
5445 if RunVim([], ["cquit"], '')
5446 call assert_equal(1, v:shell_error)
5447 endif
5448
5449 " Exit Vim with zero value
5450 if RunVim([], ["cquit 0"], '')
5451 call assert_equal(0, v:shell_error)
5452 endif
5453
5454 " Exit Vim with negative value
5455 call assert_fails('-3cquit', 'E16:')
5456endfunc
5457
Bram Moolenaar858ba062020-05-31 23:11:59 +02005458" Test for getting a specific item from a quickfix list
5459func Xtest_getqflist_by_idx(cchar)
5460 call s:setup_commands(a:cchar)
5461 " Empty list
5462 call assert_equal([], g:Xgetlist({'idx' : 1, 'items' : 0}).items)
5463 Xexpr ['F1:10:L10', 'F1:20:L20']
5464 let l = g:Xgetlist({'idx' : 2, 'items' : 0}).items
5465 call assert_equal(bufnr('F1'), l[0].bufnr)
5466 call assert_equal(20, l[0].lnum)
5467 call assert_equal('L20', l[0].text)
5468 call assert_equal([], g:Xgetlist({'idx' : -1, 'items' : 0}).items)
5469 call assert_equal([], g:Xgetlist({'idx' : 3, 'items' : 0}).items)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005470 call assert_equal({}, g:Xgetlist(#{idx: "abc"}))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005471 %bwipe!
5472endfunc
5473
5474func Test_getqflist_by_idx()
5475 call Xtest_getqflist_by_idx('c')
5476 call Xtest_getqflist_by_idx('l')
5477endfunc
5478
5479" Test for the 'quickfixtextfunc' setting
5480func Tqfexpr(info)
5481 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005482 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005483 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005484 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005485 endif
5486
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005487 let l = []
5488 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5489 let e = qfl[idx]
5490 let s = ''
5491 if e.bufnr != 0
5492 let bname = bufname(e.bufnr)
5493 let s ..= fnamemodify(bname, ':.')
5494 endif
5495 let s ..= '-'
5496 let s ..= 'L' .. string(e.lnum) .. 'C' .. string(e.col) .. '-'
5497 let s ..= e.text
5498 call add(l, s)
5499 endfor
Bram Moolenaar858ba062020-05-31 23:11:59 +02005500
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005501 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005502endfunc
5503
5504func Xtest_qftextfunc(cchar)
5505 call s:setup_commands(a:cchar)
5506
5507 set efm=%f:%l:%c:%m
5508 set quickfixtextfunc=Tqfexpr
Bram Moolenaard43906d2020-07-20 21:31:32 +02005509 call assert_equal('Tqfexpr', &quickfixtextfunc)
5510 call assert_equal('',
5511 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
thinca6864efa2021-06-19 20:45:20 +02005512 call g:Xsetlist([
5513 \ { 'filename': 'F1', 'lnum': 10, 'col': 2,
5514 \ 'end_col': 7, 'text': 'green'},
5515 \ { 'filename': 'F1', 'lnum': 20, 'end_lnum': 25, 'col': 4,
5516 \ 'end_col': 8, 'text': 'blue'},
5517 \ ])
5518
Bram Moolenaar858ba062020-05-31 23:11:59 +02005519 Xwindow
5520 call assert_equal('F1-L10C2-green', getline(1))
5521 call assert_equal('F1-L20C4-blue', getline(2))
5522 Xclose
5523 set quickfixtextfunc&vim
5524 Xwindow
thinca6864efa2021-06-19 20:45:20 +02005525 call assert_equal('F1|10 col 2-7| green', getline(1))
5526 call assert_equal('F1|20-25 col 4-8| blue', getline(2))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005527 Xclose
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005528
5529 set efm=%f:%l:%c:%m
5530 set quickfixtextfunc=Tqfexpr
5531 " Update the list with only the cwindow
5532 Xwindow
5533 only
5534 call g:Xsetlist([
5535 \ { 'filename': 'F2', 'lnum': 20, 'col': 2,
5536 \ 'end_col': 7, 'text': 'red'}
5537 \ ])
5538 call assert_equal(['F2-L20C2-red'], getline(1, '$'))
5539 new
5540 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005541 set efm&
5542 set quickfixtextfunc&
5543
5544 " Test for per list 'quickfixtextfunc' setting
5545 func PerQfText(info)
5546 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005547 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005548 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005549 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005550 endif
5551 if empty(qfl)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005552 return []
Bram Moolenaar858ba062020-05-31 23:11:59 +02005553 endif
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005554 let l = []
5555 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5556 call add(l, 'Line ' .. qfl[idx].lnum .. ', Col ' .. qfl[idx].col)
5557 endfor
5558 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005559 endfunc
5560 set quickfixtextfunc=Tqfexpr
5561 call g:Xsetlist([], ' ', {'quickfixtextfunc' : "PerQfText"})
5562 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5563 Xwindow
5564 call assert_equal('Line 10, Col 2', getline(1))
5565 call assert_equal('Line 20, Col 4', getline(2))
5566 Xclose
Bram Moolenaard43906d2020-07-20 21:31:32 +02005567 call assert_equal(function('PerQfText'),
5568 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005569 " Add entries to the list when the quickfix buffer is hidden
5570 Xaddexpr ['F1:30:6:red']
5571 Xwindow
5572 call assert_equal('Line 30, Col 6', getline(3))
5573 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005574 call g:Xsetlist([], 'r', {'quickfixtextfunc' : ''})
Bram Moolenaard43906d2020-07-20 21:31:32 +02005575 call assert_equal('', g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar858ba062020-05-31 23:11:59 +02005576 set quickfixtextfunc&
5577 delfunc PerQfText
5578
5579 " Non-existing function
5580 set quickfixtextfunc=Tabc
5581 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5582 call assert_fails("Xwindow", 'E117:')
5583 Xclose
5584 set quickfixtextfunc&
5585
5586 " set option to a non-function
5587 set quickfixtextfunc=[10,\ 20]
5588 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5589 call assert_fails("Xwindow", 'E117:')
5590 Xclose
5591 set quickfixtextfunc&
5592
5593 " set option to a function with different set of arguments
5594 func Xqftext(a, b, c)
5595 return a:a .. a:b .. a:c
5596 endfunc
5597 set quickfixtextfunc=Xqftext
5598 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E119:')
5599 call assert_fails("Xwindow", 'E119:')
5600 Xclose
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005601
5602 " set option to a function that returns a list with non-strings
5603 func Xqftext2(d)
5604 return ['one', [], 'two']
5605 endfunc
5606 set quickfixtextfunc=Xqftext2
5607 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue', 'F1:30:6:red']",
5608 \ 'E730:')
5609 call assert_fails('Xwindow', 'E730:')
Bram Moolenaard43906d2020-07-20 21:31:32 +02005610 call assert_equal(['one', 'F1|20 col 4| blue', 'F1|30 col 6| red'],
5611 \ getline(1, '$'))
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005612 Xclose
5613
Bram Moolenaar858ba062020-05-31 23:11:59 +02005614 set quickfixtextfunc&
5615 delfunc Xqftext
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005616 delfunc Xqftext2
Bram Moolenaard43906d2020-07-20 21:31:32 +02005617
5618 " set the global option to a lambda function
5619 set quickfixtextfunc={d\ ->\ map(g:Xgetlist({'id'\ :\ d.id,\ 'items'\ :\ 1}).items[d.start_idx-1:d.end_idx-1],\ 'v:val.text')}
5620 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5621 Xwindow
5622 call assert_equal(['green', 'blue'], getline(1, '$'))
5623 Xclose
5624 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)
5625 set quickfixtextfunc&
5626
5627 " use a lambda function that returns an empty list
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 " use a lambda function that returns a list with empty strings
5637 set quickfixtextfunc={d\ ->\ ['',\ '']}
5638 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5639 Xwindow
5640 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5641 \ getline(1, '$'))
5642 Xclose
5643 set quickfixtextfunc&
5644
5645 " set the per-quickfix list text function to a lambda function
5646 call g:Xsetlist([], ' ',
5647 \ {'quickfixtextfunc' :
5648 \ {d -> map(g:Xgetlist({'id' : d.id, 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5649 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5650 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5651 Xwindow
5652 call assert_equal('Line 10, Col 2', getline(1))
5653 call assert_equal('Line 20, Col 4', getline(2))
5654 Xclose
5655 call assert_match("function('<lambda>\\d\\+')", string(g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc))
5656 call g:Xsetlist([], 'f')
Bram Moolenaar858ba062020-05-31 23:11:59 +02005657endfunc
5658
5659func Test_qftextfunc()
5660 call Xtest_qftextfunc('c')
5661 call Xtest_qftextfunc('l')
5662endfunc
5663
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005664func Test_qftextfunc_callback()
5665 let lines =<< trim END
5666 set efm=%f:%l:%c:%m
5667
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00005668 #" Test for using a function name
5669 LET &qftf = 'g:Tqfexpr'
5670 cexpr "F0:0:0:L0"
5671 copen
5672 call assert_equal('F0-L0C0-L0', getline(1))
5673 cclose
5674
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005675 #" Test for using a function()
5676 set qftf=function('g:Tqfexpr')
5677 cexpr "F1:1:1:L1"
5678 copen
5679 call assert_equal('F1-L1C1-L1', getline(1))
5680 cclose
5681
5682 #" Using a funcref variable to set 'quickfixtextfunc'
5683 VAR Fn = function('g:Tqfexpr')
5684 LET &qftf = Fn
5685 cexpr "F2:2:2:L2"
5686 copen
5687 call assert_equal('F2-L2C2-L2', getline(1))
5688 cclose
5689
5690 #" Using string(funcref_variable) to set 'quickfixtextfunc'
5691 LET Fn = function('g:Tqfexpr')
5692 LET &qftf = string(Fn)
5693 cexpr "F3:3:3:L3"
5694 copen
5695 call assert_equal('F3-L3C3-L3', getline(1))
5696 cclose
5697
5698 #" Test for using a funcref()
5699 set qftf=funcref('g:Tqfexpr')
5700 cexpr "F4:4:4:L4"
5701 copen
5702 call assert_equal('F4-L4C4-L4', getline(1))
5703 cclose
5704
5705 #" Using a funcref variable to set 'quickfixtextfunc'
5706 LET Fn = funcref('g:Tqfexpr')
5707 LET &qftf = Fn
5708 cexpr "F5:5:5:L5"
5709 copen
5710 call assert_equal('F5-L5C5-L5', getline(1))
5711 cclose
5712
5713 #" Using a string(funcref_variable) to set 'quickfixtextfunc'
5714 LET Fn = funcref('g:Tqfexpr')
5715 LET &qftf = string(Fn)
5716 cexpr "F5:5:5:L5"
5717 copen
5718 call assert_equal('F5-L5C5-L5', getline(1))
5719 cclose
5720
5721 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00005722 VAR optval = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005723 LET optval = substitute(optval, ' ', '\\ ', 'g')
5724 exe "set qftf=" .. optval
5725 cexpr "F6:6:6:L6"
5726 copen
5727 call assert_equal('F6-L6C6-L6', getline(1))
5728 cclose
5729
5730 #" Set 'quickfixtextfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005731 LET &qftf = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005732 cexpr "F7:7:7:L7"
5733 copen
5734 call assert_equal('F7-L7C7-L7', getline(1))
5735 cclose
5736
5737 #" Set 'quickfixtextfunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005738 LET &qftf = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005739 cexpr "F8:8:8:L8"
5740 copen
5741 call assert_equal('F8-L8C8-L8', getline(1))
5742 cclose
5743
5744 #" Set 'quickfixtextfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005745 VAR Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005746 LET &qftf = Lambda
5747 cexpr "F9:9:9:L9"
5748 copen
5749 call assert_equal('F9-L9C9-L9', getline(1))
5750 cclose
5751
5752 #" Set 'quickfixtextfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005753 LET Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005754 LET &qftf = string(Lambda)
5755 cexpr "F9:9:9:L9"
5756 copen
5757 call assert_equal('F9-L9C9-L9', getline(1))
5758 cclose
5759 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00005760 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005761
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00005762 " Test for using a script-local function name
5763 func s:TqfFunc2(info)
5764 let g:TqfFunc2Args = [a:info.start_idx, a:info.end_idx]
5765 return ''
5766 endfunc
5767 let g:TqfFunc2Args = []
5768 set quickfixtextfunc=s:TqfFunc2
5769 cexpr "F10:10:10:L10"
5770 cclose
5771 call assert_equal([1, 1], g:TqfFunc2Args)
5772
5773 let &quickfixtextfunc = 's:TqfFunc2'
5774 cexpr "F11:11:11:L11"
5775 cclose
5776 call assert_equal([1, 1], g:TqfFunc2Args)
5777 delfunc s:TqfFunc2
5778
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005779 " set 'quickfixtextfunc' to a partial with dict. This used to cause a crash.
5780 func SetQftfFunc()
5781 let params = {'qftf': function('g:DictQftfFunc')}
5782 let &quickfixtextfunc = params.qftf
5783 endfunc
5784 func g:DictQftfFunc(_) dict
5785 endfunc
5786 call SetQftfFunc()
5787 new
5788 call SetQftfFunc()
5789 bw
5790 call test_garbagecollect_now()
5791 new
5792 set qftf=
5793 wincmd w
5794 set qftf=
5795 :%bw!
5796
5797 " set per-quickfix list 'quickfixtextfunc' to a partial with dict. This used
5798 " to cause a crash.
5799 let &qftf = ''
5800 func SetLocalQftfFunc()
5801 let params = {'qftf': function('g:DictQftfFunc')}
5802 call setqflist([], 'a', {'quickfixtextfunc' : params.qftf})
5803 endfunc
5804 call SetLocalQftfFunc()
5805 call test_garbagecollect_now()
5806 call setqflist([], 'a', {'quickfixtextfunc' : ''})
5807 delfunc g:DictQftfFunc
5808 delfunc SetQftfFunc
5809 delfunc SetLocalQftfFunc
5810 set efm&
5811endfunc
5812
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02005813" Test for updating a location list for some other window and check that
5814" 'qftextfunc' uses the correct location list.
5815func Test_qftextfunc_other_loclist()
5816 %bw!
5817 call setloclist(0, [], 'f')
5818
5819 " create a window and a location list for it and open the location list
5820 " window
5821 lexpr ['F1:10:12:one', 'F1:20:14:two']
5822 let w1_id = win_getid()
5823 call setloclist(0, [], ' ',
5824 \ {'lines': ['F1:10:12:one', 'F1:20:14:two'],
5825 \ 'quickfixtextfunc':
5826 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5827 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5828 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5829 lwindow
5830 let w2_id = win_getid()
5831
5832 " create another window and a location list for it and open the location
5833 " list window
5834 topleft new
5835 let w3_id = win_getid()
5836 call setloclist(0, [], ' ',
5837 \ {'lines': ['F2:30:32:eleven', 'F2:40:34:twelve'],
5838 \ 'quickfixtextfunc':
5839 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5840 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5841 \ "'Ligne ' .. v:val.lnum .. ', Colonne ' .. v:val.col")}})
5842 lwindow
5843 let w4_id = win_getid()
5844
5845 topleft new
5846 lexpr ['F3:50:52:green', 'F3:60:54:blue']
5847 let w5_id = win_getid()
5848
5849 " change the location list for some other window
5850 call setloclist(0, [], 'r', {'lines': ['F3:55:56:aaa', 'F3:57:58:bbb']})
5851 call setloclist(w1_id, [], 'r', {'lines': ['F1:62:63:bbb', 'F1:64:65:ccc']})
5852 call setloclist(w3_id, [], 'r', {'lines': ['F2:76:77:ddd', 'F2:78:79:eee']})
5853 call assert_equal(['Line 62, Col 63', 'Line 64, Col 65'],
5854 \ getbufline(winbufnr(w2_id), 1, '$'))
5855 call assert_equal(['Ligne 76, Colonne 77', 'Ligne 78, Colonne 79'],
5856 \ getbufline(winbufnr(w4_id), 1, '$'))
5857 call setloclist(w2_id, [], 'r', {'lines': ['F1:32:33:fff', 'F1:34:35:ggg']})
5858 call setloclist(w4_id, [], 'r', {'lines': ['F2:46:47:hhh', 'F2:48:49:jjj']})
5859 call assert_equal(['Line 32, Col 33', 'Line 34, Col 35'],
5860 \ getbufline(winbufnr(w2_id), 1, '$'))
5861 call assert_equal(['Ligne 46, Colonne 47', 'Ligne 48, Colonne 49'],
5862 \ getbufline(winbufnr(w4_id), 1, '$'))
5863
5864 call win_gotoid(w5_id)
5865 lwindow
5866 call assert_equal(['F3|55 col 56| aaa', 'F3|57 col 58| bbb'],
5867 \ getline(1, '$'))
5868 %bw!
5869endfunc
5870
Bram Moolenaarec98e932020-06-08 19:35:59 +02005871" Running :lhelpgrep command more than once in a help window, doesn't jump to
5872" the help topic
5873func Test_lhelpgrep_from_help_window()
5874 call mkdir('Xtestdir/doc', 'p')
5875 call writefile(['window'], 'Xtestdir/doc/a.txt')
5876 call writefile(['buffer'], 'Xtestdir/doc/b.txt')
5877 let save_rtp = &rtp
5878 let &rtp = 'Xtestdir'
5879 lhelpgrep window
5880 lhelpgrep buffer
5881 call assert_equal('b.txt', fnamemodify(@%, ":p:t"))
5882 lhelpgrep window
5883 call assert_equal('a.txt', fnamemodify(@%, ":p:t"))
5884 let &rtp = save_rtp
5885 call delete('Xtestdir', 'rf')
5886 new | only!
5887endfunc
5888
Bram Moolenaarf7c4d832020-08-11 20:42:19 +02005889" Test for the crash fixed by 7.3.715
5890func Test_setloclist_crash()
5891 %bw!
5892 let g:BufNum = bufnr()
5893 augroup QF_Test
5894 au!
5895 au BufUnload * call setloclist(0, [{'bufnr':g:BufNum, 'lnum':1, 'col':1, 'text': 'tango down'}])
5896 augroup END
5897
5898 try
5899 lvimgrep /.*/ *.mak
5900 catch /E926:/
5901 endtry
5902 call assert_equal('tango down', getloclist(0, {'items' : 0}).items[0].text)
5903 call assert_equal(1, getloclist(0, {'size' : 0}).size)
5904
5905 augroup QF_Test
5906 au!
5907 augroup END
5908 unlet g:BufNum
5909 %bw!
5910endfunc
5911
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005912" Test for adding an invalid entry with the quickfix window open and making
5913" sure that the window contents are not changed
5914func Test_add_invalid_entry_with_qf_window()
5915 call setqflist([], 'f')
5916 cexpr "Xfile1:10:aa"
5917 copen
5918 call setqflist(['bb'], 'a')
5919 call assert_equal(1, line('$'))
5920 call assert_equal(['Xfile1|10| aa'], getline(1, '$'))
shane.xb.qian0d5e1ec2021-06-20 16:31:00 +02005921 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())
5922
5923 call setqflist([{'lnum': 10 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5924 call assert_equal(1 , line('$'))
5925 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5926 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())
5927
5928 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')
5929 call assert_equal(1 , line('$'))
5930 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5931 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())
5932
5933 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')
5934 call assert_equal(1 , line('$'))
5935 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5936 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())
5937
5938 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')
5939 call assert_equal(1 , line('$'))
5940 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5941 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())
5942
5943 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')
5944 call assert_equal(1 , line('$'))
5945 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5946 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())
5947
5948 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')
5949 call assert_equal(1 , line('$'))
5950 call assert_equal(['Xfile1|10 col 666-222| aa'] , getline(1 , '$'))
5951 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())
5952
5953 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')
5954 call assert_equal(1 , line('$'))
5955 call assert_equal(['Xfile1|10-6 col 666-222| aa'] , getline(1 , '$'))
5956 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 +01005957 cclose
5958endfunc
5959
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01005960" Test for very weird problem: autocommand causes a failure, resulting opening
5961" the quickfix window to fail. This still splits the window, but otherwise
5962" should not mess up buffers.
5963func Test_quickfix_window_fails_to_open()
5964 CheckScreendump
5965
5966 let lines =<< trim END
5967 anything
5968 try
5969 anything
5970 endtry
5971 END
5972 call writefile(lines, 'XquickfixFails')
5973
5974 let lines =<< trim END
5975 split XquickfixFails
5976 silent vimgrep anything %
5977 normal o
5978 au BufLeave * ++once source XquickfixFails
5979 " This will trigger the autocommand, which causes an error, what follows
5980 " is aborted but the window was already split.
5981 silent! cwindow
5982 END
5983 call writefile(lines, 'XtestWinFails')
5984 let buf = RunVimInTerminal('-S XtestWinFails', #{rows: 13})
5985 call VerifyScreenDump(buf, 'Test_quickfix_window_fails', {})
5986
5987 " clean up
5988 call term_sendkeys(buf, ":bwipe!\<CR>")
5989 call term_wait(buf)
5990 call StopVimInTerminal(buf)
5991 call delete('XtestWinFails')
5992 call delete('XquickfixFails')
5993endfunc
5994
Bram Moolenaar2d870f82020-12-05 13:41:01 +01005995" Test for updating the quickfix buffer whenever the associated quickfix list
Bram Moolenaar287153c2020-11-29 14:20:27 +01005996" is changed.
5997func Xqfbuf_update(cchar)
5998 call s:setup_commands(a:cchar)
5999
6000 Xexpr "F1:1:line1"
6001 Xopen
6002 call assert_equal(['F1|1| line1'], getline(1, '$'))
6003 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6004
6005 " Test setqflist() using the 'lines' key in 'what'
6006 " add a new entry
6007 call g:Xsetlist([], 'a', {'lines' : ['F2:2: line2']})
6008 call assert_equal(['F1|1| line1', 'F2|2| line2'], getline(1, '$'))
6009 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6010 " replace all the entries with a single entry
6011 call g:Xsetlist([], 'r', {'lines' : ['F3:3: line3']})
6012 call assert_equal(['F3|3| line3'], getline(1, '$'))
6013 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6014 " remove all the entries
6015 call g:Xsetlist([], 'r', {'lines' : []})
6016 call assert_equal([''], getline(1, '$'))
6017 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6018 " add a new list
6019 call g:Xsetlist([], ' ', {'lines' : ['F4:4: line4']})
6020 call assert_equal(['F4|4| line4'], getline(1, '$'))
6021 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6022
6023 " Test setqflist() using the 'items' key in 'what'
6024 " add a new entry
6025 call g:Xsetlist([], 'a', {'items' : [{'filename' : 'F5', 'lnum' : 5, 'text' : 'line5'}]})
6026 call assert_equal(['F4|4| line4', 'F5|5| line5'], getline(1, '$'))
6027 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
6028 " replace all the entries with a single entry
6029 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F6', 'lnum' : 6, 'text' : 'line6'}]})
6030 call assert_equal(['F6|6| line6'], getline(1, '$'))
6031 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
6032 " remove all the entries
6033 call g:Xsetlist([], 'r', {'items' : []})
6034 call assert_equal([''], getline(1, '$'))
6035 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
6036 " add a new list
6037 call g:Xsetlist([], ' ', {'items' : [{'filename' : 'F7', 'lnum' : 7, 'text' : 'line7'}]})
6038 call assert_equal(['F7|7| line7'], getline(1, '$'))
6039 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6040
6041 call g:Xsetlist([], ' ', {})
6042 call assert_equal([''], getline(1, '$'))
6043 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
6044
6045 Xclose
6046endfunc
6047
6048func Test_qfbuf_update()
6049 call Xqfbuf_update('c')
6050 call Xqfbuf_update('l')
6051endfunc
6052
Bram Moolenaar8c801b32021-03-05 20:58:22 +01006053func Test_vimgrep_noswapfile()
6054 set noswapfile
6055 call writefile(['one', 'two', 'three'], 'Xgreppie')
6056 vimgrep two Xgreppie
6057 call assert_equal('two', getline('.'))
6058
6059 call delete('Xgreppie')
6060 set swapfile
6061endfunc
6062
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02006063" Test for the :vimgrep 'f' flag (fuzzy match)
6064func Xvimgrep_fuzzy_match(cchar)
6065 call s:setup_commands(a:cchar)
6066
6067 Xvimgrep /three one/f Xfile*
6068 let l = g:Xgetlist()
6069 call assert_equal(2, len(l))
6070 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6071 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6072 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6073 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6074
6075 Xvimgrep /the/f Xfile*
6076 let l = g:Xgetlist()
6077 call assert_equal(3, len(l))
6078 call assert_equal(['Xfile1', 1, 9, 'one two three'],
6079 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6080 call assert_equal(['Xfile2', 2, 1, 'three one two'],
6081 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6082 call assert_equal(['Xfile2', 4, 4, 'aaathreeaaa'],
6083 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6084
6085 Xvimgrep /aaa/fg Xfile*
6086 let l = g:Xgetlist()
6087 call assert_equal(4, len(l))
6088 call assert_equal(['Xfile1', 2, 1, 'aaaaaa'],
6089 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
6090 call assert_equal(['Xfile1', 2, 4, 'aaaaaa'],
6091 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
6092 call assert_equal(['Xfile2', 4, 1, 'aaathreeaaa'],
6093 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
6094 call assert_equal(['Xfile2', 4, 9, 'aaathreeaaa'],
6095 \ [bufname(l[3].bufnr), l[3].lnum, l[3].col, l[3].text])
6096
6097 call assert_fails('Xvimgrep /xyz/fg Xfile*', 'E480:')
6098endfunc
6099
6100func Test_vimgrep_fuzzy_match()
6101 call writefile(['one two three', 'aaaaaa'], 'Xfile1')
6102 call writefile(['one', 'three one two', 'two', 'aaathreeaaa'], 'Xfile2')
6103 call Xvimgrep_fuzzy_match('c')
6104 call Xvimgrep_fuzzy_match('l')
6105 call delete('Xfile1')
6106 call delete('Xfile2')
6107endfunc
6108
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006109func Test_locationlist_open_in_newtab()
6110 call s:create_test_file('Xqftestfile1')
6111 call s:create_test_file('Xqftestfile2')
6112 call s:create_test_file('Xqftestfile3')
6113
6114 %bwipe!
6115
Yegappan Lakshmanan4a7724a2022-04-11 15:11:39 +01006116 let lines =<< trim END
6117 Xqftestfile1:5:Line5
6118 Xqftestfile2:10:Line10
6119 Xqftestfile3:16:Line16
6120 END
6121 lgetexpr lines
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006122
6123 silent! llast
6124 call assert_equal(1, tabpagenr('$'))
6125 call assert_equal('Xqftestfile3', bufname())
6126
6127 set switchbuf=newtab
6128
6129 silent! lfirst
6130 call assert_equal(2, tabpagenr('$'))
6131 call assert_equal('Xqftestfile1', bufname())
6132
6133 silent! lnext
6134 call assert_equal(3, tabpagenr('$'))
6135 call assert_equal('Xqftestfile2', bufname())
6136
6137 call delete('Xqftestfile1')
6138 call delete('Xqftestfile2')
6139 call delete('Xqftestfile3')
6140 set switchbuf&vim
6141
6142 %bwipe!
6143endfunc
6144
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02006145" Test for win_gettype() in quickfix and location list windows
6146func Test_win_gettype()
6147 copen
6148 call assert_equal("quickfix", win_gettype())
6149 let wid = win_getid()
6150 wincmd p
6151 call assert_equal("quickfix", win_gettype(wid))
6152 cclose
6153 lexpr ''
6154 lopen
6155 call assert_equal("loclist", win_gettype())
6156 let wid = win_getid()
6157 wincmd p
6158 call assert_equal("loclist", win_gettype(wid))
6159 lclose
6160endfunc
6161
Christian Brabandt0b226f62021-12-01 10:54:24 +00006162fun Test_vimgrep_nomatch()
6163 call XexprTests('c')
6164 call g:Xsetlist([{'lnum':10,'text':'Line1'}])
6165 copen
6166 if has("win32")
6167 call assert_fails('vimgrep foo *.zzz', 'E479:')
6168 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'}]
6169 else
6170 call assert_fails('vimgrep foo *.zzz', 'E480:')
6171 let expected = []
6172 endif
6173 call assert_equal(expected, getqflist())
6174 cclose
6175endfunc
6176
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00006177" Test for opening the quickfix window in two tab pages and then closing one
6178" of the quickfix windows. This should not make the quickfix buffer unlisted.
6179" (github issue #9300).
6180func Test_two_qf_windows()
6181 cexpr "F1:1:line1"
6182 copen
6183 tabnew
6184 copen
6185 call assert_true(&buflisted)
6186 cclose
6187 tabfirst
6188 call assert_true(&buflisted)
6189 let bnum = bufnr()
6190 cclose
6191 " if all the quickfix windows are closed, then buffer should be unlisted.
6192 call assert_false(buflisted(bnum))
6193 %bw!
6194
6195 " Repeat the test for a location list
6196 lexpr "F2:2:line2"
6197 lopen
6198 let bnum = bufnr()
6199 tabnew
6200 exe "buffer" bnum
6201 tabfirst
6202 lclose
6203 tablast
6204 call assert_true(buflisted(bnum))
6205 tabclose
6206 lopen
6207 call assert_true(buflisted(bnum))
6208 lclose
6209 call assert_false(buflisted(bnum))
6210 %bw!
6211endfunc
6212
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006213" Weird sequence of commands that caused entering a wiped-out buffer
6214func Test_lopen_bwipe()
6215 func R()
6216 silent! tab lopen
6217 e x
6218 silent! lfile
6219 endfunc
6220
6221 cal R()
6222 cal R()
6223 cal R()
6224 bw!
6225 delfunc R
6226endfunc
6227
Bram Moolenaare3537ae2022-02-08 15:05:20 +00006228" Another sequence of commands that caused all buffers to be wiped out
6229func Test_lopen_bwipe_all()
6230 let lines =<< trim END
6231 func R()
6232 silent! tab lopen
6233 e foo
6234 silent! lfile
6235 endfunc
6236 cal R()
6237 exe "norm \<C-W>\<C-V>0"
6238 cal R()
6239 bwipe
6240
6241 call writefile(['done'], 'Xresult')
6242 qall!
6243 END
6244 call writefile(lines, 'Xscript')
6245 if RunVim([], [], '-u NONE -n -X -Z -e -m -s -S Xscript')
6246 call assert_equal(['done'], readfile('Xresult'))
6247 endif
6248
6249 call delete('Xscript')
6250 call delete('Xresult')
6251endfunc
6252
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00006253" Test for calling setqflist() function recursively
6254func Test_recursive_setqflist()
6255 augroup QF_Test
6256 au!
6257 autocmd BufWinEnter quickfix call setqflist([], 'r')
6258 augroup END
6259
6260 copen
6261 call assert_fails("call setqflist([], 'a')", 'E952:')
6262
6263 augroup QF_Test
6264 au!
6265 augroup END
6266 %bw!
6267endfunc
6268
6269" Test for failure to create a new window when selecting a file from the
6270" quickfix window
6271func Test_cwindow_newwin_fails()
6272 cgetexpr ["Xfile1:10:L10", "Xfile1:20:L20"]
6273 cwindow
6274 only
6275 let qf_wid = win_getid()
6276 " create the maximum number of scratch windows
6277 let hor_win_count = (&lines - 1)/2
6278 let hor_split_count = hor_win_count - 1
6279 for s in range(1, hor_split_count) | new | set buftype=nofile | endfor
6280 call win_gotoid(qf_wid)
6281 call assert_fails('exe "normal \<CR>"', 'E36:')
6282 %bw!
6283endfunc
6284
6285" Test for updating the location list when only the location list window is
6286" present and the corresponding file window is closed.
6287func Test_loclist_update_with_llwin_only()
6288 %bw!
6289 new
6290 wincmd w
6291 lexpr ["Xfile1:1:Line1"]
6292 lopen
6293 wincmd p
6294 close
6295 call setloclist(2, [], 'r', {'lines': ["Xtest2:2:Line2"]})
6296 call assert_equal(['Xtest2|2| Line2'], getbufline(winbufnr(2), 1, '$'))
6297 %bw!
6298endfunc
6299
6300" Test for getting the quickfix list after a buffer with an error is wiped out
6301func Test_getqflist_wiped_out_buffer()
6302 %bw!
6303 cexpr ["Xtest1:34:Wiped out"]
6304 let bnum = bufnr('Xtest1')
6305 call assert_equal(bnum, getqflist()[0].bufnr)
6306 bw Xtest1
6307 call assert_equal(0, getqflist()[0].bufnr)
6308 %bw!
6309endfunc
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006310
Yegappan Lakshmanan454ce672022-03-24 11:22:13 +00006311" Test for the status message that is displayed when opening a new quickfix
6312" list
6313func Test_qflist_statusmsg()
6314 cexpr "1\n2"
6315 cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"
6316 call assert_equal('(4 of 4): msg', v:statusmsg)
6317 call setqflist([], 'f')
6318 %bw!
6319
6320 " When creating a new quickfix list, if an autocmd changes the quickfix list
6321 " in the stack, then an error message should be displayed.
6322 augroup QF_Test
6323 au!
6324 au BufEnter test_quickfix.vim colder
6325 augroup END
6326 cexpr "1\n2"
6327 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6328 call setqflist([], 'f')
6329 augroup QF_Test
6330 au!
6331 augroup END
6332 %bw!
6333
6334 augroup QF_Test
6335 au!
6336 au BufEnter test_quickfix.vim caddexpr "4"
6337 augroup END
6338 call assert_fails('cexpr "1\n2\n3\ntest_quickfix.vim:1:msg"', 'E925:')
6339 call setqflist([], 'f')
6340 augroup QF_Test
6341 au!
6342 augroup END
6343 %bw!
6344endfunc
6345
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006346" vim: shiftwidth=2 sts=2 expandtab