blob: 42b819376752db886d8ec4d19c5c80b46dc34226 [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
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200108 Xgetexpr ['non-error 1', 'Xtestfile1:1:3:Line1',
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100109 \ 'non-error 2', 'Xtestfile2:2:2:Line2',
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100110 \ 'non-error| 3', 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100111
112 " List only valid entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100113 let l = split(execute('Xlist', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100114 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
115 \ ' 4 Xtestfile2:2 col 2: Line2',
116 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
117
118 " List all the 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([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1',
121 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2',
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100122 \ ' 5: non-error| 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100123
124 " List a range of errors
Bram Moolenaaree85df32017-03-19 14:19:50 +0100125 let l = split(execute('Xlist 3,6', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100126 call assert_equal([' 4 Xtestfile2:2 col 2: Line2',
127 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
128
Bram Moolenaaree85df32017-03-19 14:19:50 +0100129 let l = split(execute('Xlist! 3,4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100130 call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
131
Bram Moolenaaree85df32017-03-19 14:19:50 +0100132 let l = split(execute('Xlist -6,-4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100133 call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l)
134
Bram Moolenaaree85df32017-03-19 14:19:50 +0100135 let l = split(execute('Xlist! -5,-3', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100136 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
137 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100138
139 " Test for '+'
Bram Moolenaaree85df32017-03-19 14:19:50 +0100140 let l = split(execute('Xlist! +2', ''), "\n")
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100141 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
142 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaaree85df32017-03-19 14:19:50 +0100143
thinca6864efa2021-06-19 20:45:20 +0200144 " Ranged entries
145 call g:Xsetlist([{'lnum':10,'text':'Line1'},
146 \ {'lnum':20,'col':10,'text':'Line2'},
147 \ {'lnum':30,'col':15,'end_col':20,'text':'Line3'},
148 \ {'lnum':40,'end_lnum':45,'text':'Line4'},
149 \ {'lnum':50,'end_lnum':55,'col':15,'text':'Line5'},
150 \ {'lnum':60,'end_lnum':65,'col':25,'end_col':35,'text':'Line6'}])
151 let l = split(execute('Xlist', ""), "\n")
152 call assert_equal([' 1:10: Line1',
153 \ ' 2:20 col 10: Line2',
154 \ ' 3:30 col 15-20: Line3',
155 \ ' 4:40-45: Line4',
156 \ ' 5:50-55 col 15: Line5',
157 \ ' 6:60-65 col 25-35: Line6'], l)
158
Bram Moolenaaree85df32017-03-19 14:19:50 +0100159 " Different types of errors
160 call g:Xsetlist([{'lnum':10,'col':5,'type':'W', 'text':'Warning','nr':11},
161 \ {'lnum':20,'col':10,'type':'e','text':'Error','nr':22},
162 \ {'lnum':30,'col':15,'type':'i','text':'Info','nr':33},
163 \ {'lnum':40,'col':20,'type':'x', 'text':'Other','nr':44},
164 \ {'lnum':50,'col':25,'type':"\<C-A>",'text':'one','nr':55}])
165 let l = split(execute('Xlist', ""), "\n")
166 call assert_equal([' 1:10 col 5 warning 11: Warning',
167 \ ' 2:20 col 10 error 22: Error',
168 \ ' 3:30 col 15 info 33: Info',
169 \ ' 4:40 col 20 x 44: Other',
170 \ ' 5:50 col 25 55: one'], l)
171
Bram Moolenaard76ce852018-05-01 15:02:04 +0200172 " Test for module names, one needs to explicitly set `'valid':v:true` so
173 call g:Xsetlist([
174 \ {'lnum':10,'col':5,'type':'W','module':'Data.Text','text':'ModuleWarning','nr':11,'valid':v:true},
175 \ {'lnum':20,'col':10,'type':'W','module':'Data.Text','filename':'Data/Text.hs','text':'ModuleWarning','nr':22,'valid':v:true},
176 \ {'lnum':30,'col':15,'type':'W','filename':'Data/Text.hs','text':'FileWarning','nr':33,'valid':v:true}])
177 let l = split(execute('Xlist', ""), "\n")
178 call assert_equal([' 1 Data.Text:10 col 5 warning 11: ModuleWarning',
179 \ ' 2 Data.Text:20 col 10 warning 22: ModuleWarning',
180 \ ' 3 Data/Text.hs:30 col 15 warning 33: FileWarning'], l)
181
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100182 " For help entries in the quickfix list, only the filename without directory
183 " should be displayed
184 Xhelpgrep setqflist()
185 let l = split(execute('Xlist 1', ''), "\n")
186 call assert_match('^ 1 [^\\/]\{-}:', l[0])
187
Bram Moolenaaree85df32017-03-19 14:19:50 +0100188 " Error cases
189 call assert_fails('Xlist abc', 'E488:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100190endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100191
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100192func Test_clist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100193 call XlistTests('c')
194 call XlistTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100195endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100196
197" Tests for the :colder, :cnewer, :lolder and :lnewer commands
198" Note that this test assumes that a quickfix/location list is
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100199" already set by the caller.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100200func XageTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200201 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100202
Bram Moolenaar87f59b02019-04-04 14:04:11 +0200203 if a:cchar == 'l'
204 " No location list for the current window
205 call assert_fails('lolder', 'E776:')
206 call assert_fails('lnewer', 'E776:')
207 endif
208
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200209 let list = [{'bufnr': bufnr('%'), 'lnum': 1}]
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200210 call g:Xsetlist(list)
211
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100212 " Jumping to a non existent list should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200213 silent! Xolder 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100214 call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
215
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200216 silent! Xnewer 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100217 call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
218
219 " Add three quickfix/location lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200220 Xgetexpr ['Xtestfile1:1:3:Line1']
221 Xgetexpr ['Xtestfile2:2:2:Line2']
222 Xgetexpr ['Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100223
224 " Go back two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200225 Xolder
226 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100227 call assert_equal('Line2', l[0].text)
228
229 " Go forward two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200230 Xnewer
231 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100232 call assert_equal('Line3', l[0].text)
233
234 " Test for the optional count argument
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200235 Xolder 2
236 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100237 call assert_equal('Line1', l[0].text)
238
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200239 Xnewer 2
240 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100241 call assert_equal('Line3', l[0].text)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100242endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100243
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100244func Test_cage()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100245 call XageTests('c')
246 call XageTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100247endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100248
249" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
250" commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100251func XwindowTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200252 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100253
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200254 " Opening the location list window without any errors should fail
255 if a:cchar == 'l'
256 call assert_fails('lopen', 'E776:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000257 call assert_fails('lwindow', 'E776:')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200258 endif
259
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100260 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200261 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100262
263 " Quickfix/Location window should not open with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200264 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100265 call assert_true(winnr('$') == 1)
266
267 " Create a list with valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200268 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
269 \ 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100270
271 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200272 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100273 call assert_true(winnr('$') == 2 && winnr() == 2 &&
274 \ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
Bram Moolenaar537ef082016-07-09 17:56:19 +0200275 redraw!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100276
277 " Close the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200278 Xclose
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100279 call assert_true(winnr('$') == 1)
280
281 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200282 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100283
284 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200285 Xopen 5
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100286 call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
Bram Moolenaarc05d1c02020-09-04 18:38:06 +0200287 \ && winheight(0) == 5)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100288
289 " Opening the window again, should move the cursor to that window
290 wincmd t
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200291 Xopen 7
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100292 call assert_true(winnr('$') == 2 && winnr() == 2 &&
Bram Moolenaarc05d1c02020-09-04 18:38:06 +0200293 \ winheight(0) == 7 &&
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100294 \ getline('.') ==# '|| non-error 1')
295
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100296 " :cnext in quickfix window should move to the next entry
297 Xnext
298 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100299
300 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200301 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100302 call assert_true(winnr('$') == 1)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200303
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100304 " Specifying the width should adjust the width for a vertically split
305 " quickfix window.
306 vert Xopen
307 call assert_equal(10, winwidth(0))
308 vert Xopen 12
309 call assert_equal(12, winwidth(0))
310 Xclose
311
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200312 " Horizontally or vertically splitting the quickfix window should create a
313 " normal window/buffer
314 Xopen
315 wincmd s
316 call assert_equal(0, getwininfo(win_getid())[0].quickfix)
317 call assert_equal(0, getwininfo(win_getid())[0].loclist)
318 call assert_notequal('quickfix', &buftype)
319 close
320 Xopen
321 wincmd v
322 call assert_equal(0, getwininfo(win_getid())[0].quickfix)
323 call assert_equal(0, getwininfo(win_getid())[0].loclist)
324 call assert_notequal('quickfix', &buftype)
325 close
326 Xopen
327 Xclose
328
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200329 if a:cchar == 'c'
330 " Opening the quickfix window in multiple tab pages should reuse the
331 " quickfix buffer
332 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
333 \ 'Xtestfile3:3:1:Line3']
334 Xopen
335 let qfbufnum = bufnr('%')
336 tabnew
337 Xopen
338 call assert_equal(qfbufnum, bufnr('%'))
339 new | only | tabonly
340 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100341endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100342
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100343func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100344 call XwindowTests('c')
345 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100346endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100347
Bram Moolenaar36d50222019-05-02 20:17:40 +0200348func Test_copenHeight()
349 copen
350 wincmd H
351 let height = winheight(0)
352 copen 10
353 call assert_equal(height, winheight(0))
354 quit
355endfunc
356
Bram Moolenaar1142a312019-10-16 14:51:39 +0200357func Test_copenHeight_tabline()
358 set tabline=foo showtabline=2
359 copen
360 wincmd H
361 let height = winheight(0)
362 copen 10
363 call assert_equal(height, winheight(0))
364 quit
365 set tabline& showtabline&
366endfunc
367
368
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100369" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
370" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100371func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200372 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100373
374 call writefile(['Xtestfile1:700:10:Line 700',
375 \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
376
377 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200378 Xfile Xqftestfile1
379 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100380 call assert_true(len(l) == 2 &&
381 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
382 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
383
Bram Moolenaar049cba92016-06-26 14:38:04 +0200384 " Test with a non existent file
Bram Moolenaare2e40752020-09-04 21:18:46 +0200385 call assert_fails('Xfile non_existent_file', 'E40:')
Bram Moolenaar049cba92016-06-26 14:38:04 +0200386
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100387 " Run cfile/lfile from a modified buffer
388 enew!
389 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200390 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100391 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
392
393 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200394 Xaddfile Xqftestfile1
395 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100396 call assert_true(len(l) == 3 &&
397 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
398
399 call writefile(['Xtestfile1:222:77:Line 222',
400 \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
401
402 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200403 Xgetfile Xqftestfile1
404 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100405 call assert_true(len(l) == 2 &&
406 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
407 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
408
Bram Moolenaar70077dd2019-11-24 12:12:42 +0100409 " Test for a file with a long line and without a newline at the end
410 let text = repeat('x', 1024)
411 let t = 'a.txt:18:' . text
412 call writefile([t], 'Xqftestfile1', 'b')
413 silent! Xfile Xqftestfile1
414 call assert_equal(text, g:Xgetlist()[0].text)
415
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100416 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100417endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100418
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100419func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100420 call XfileTests('c')
421 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100422endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100423
424" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
425" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100426func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200427 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100428
429 enew!
430 silent! call setline(1, ['Xtestfile7:700:10:Line 700',
431 \ 'Xtestfile8:800:15:Line 800'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200432 Xbuffer!
433 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100434 call assert_true(len(l) == 2 &&
435 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
436 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
437
438 enew!
439 silent! call setline(1, ['Xtestfile9:900:55:Line 900',
440 \ 'Xtestfile10:950:66:Line 950'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200441 Xgetbuffer
442 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100443 call assert_true(len(l) == 2 &&
444 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
445 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
446
447 enew!
448 silent! call setline(1, ['Xtestfile11:700:20:Line 700',
449 \ 'Xtestfile12:750:25:Line 750'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200450 Xaddbuffer
451 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100452 call assert_true(len(l) == 4 &&
453 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
454 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
455 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200456 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100457
Bram Moolenaaree85df32017-03-19 14:19:50 +0100458 " Check for invalid buffer
459 call assert_fails('Xbuffer 199', 'E474:')
460
461 " Check for unloaded buffer
462 edit Xtestfile1
463 let bnr = bufnr('%')
464 enew!
465 call assert_fails('Xbuffer ' . bnr, 'E681:')
466
467 " Check for invalid range
468 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
469 " commands. So directly call the commands.
470 if (a:cchar == 'c')
471 call assert_fails('900,999cbuffer', 'E16:')
472 else
473 call assert_fails('900,999lbuffer', 'E16:')
474 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100475endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100476
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100477func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100478 call XbufferTests('c')
479 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100480endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100481
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100482func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200483 call s:setup_commands(a:cchar)
484
485 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100486endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200487
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100488func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200489 call XexprTests('c')
490 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100491endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200492
493" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100494func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200495 call s:setup_commands(a:cchar)
496
Bram Moolenaar74240d32017-12-10 15:26:15 +0100497 call g:Xsetlist([], 'f')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200498 " Jumping to first or next location list entry without any error should
499 " result in failure
Bram Moolenaar74240d32017-12-10 15:26:15 +0100500 if a:cchar == 'c'
501 let err = 'E42:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100502 let cmd = '$cc'
Bram Moolenaar74240d32017-12-10 15:26:15 +0100503 else
504 let err = 'E776:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100505 let cmd = '$ll'
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200506 endif
Bram Moolenaar74240d32017-12-10 15:26:15 +0100507 call assert_fails('Xnext', err)
508 call assert_fails('Xprev', err)
509 call assert_fails('Xnfile', err)
510 call assert_fails('Xpfile', err)
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100511 call assert_fails(cmd, err)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200512
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100513 Xexpr ''
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100514 call assert_fails(cmd, 'E42:')
515
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200516 call s:create_test_file('Xqftestfile1')
517 call s:create_test_file('Xqftestfile2')
518
519 Xgetexpr ['Xqftestfile1:5:Line5',
520 \ 'Xqftestfile1:6:Line6',
521 \ 'Xqftestfile2:10:Line10',
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200522 \ 'Xqftestfile2:11:Line11',
523 \ 'RegularLine1',
524 \ 'RegularLine2']
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200525
526 Xfirst
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100527 call assert_fails('-5Xcc', 'E16:')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200528 call assert_fails('Xprev', 'E553:')
529 call assert_fails('Xpfile', 'E553:')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200530 Xnfile
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200531 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200532 call assert_equal(10, line('.'))
533 Xpfile
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200534 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200535 call assert_equal(6, line('.'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100536 5Xcc
537 call assert_equal(5, g:Xgetlist({'idx':0}).idx)
538 2Xcc
539 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100540 if a:cchar == 'c'
541 cc
542 else
543 ll
544 endif
545 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100546 10Xcc
547 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200548 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200549 Xprev
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200550 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200551 call assert_equal(11, line('.'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200552 call assert_fails('Xnext', 'E553:')
553 call assert_fails('Xnfile', 'E553:')
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100554 " To process the range using quickfix list entries, directly use the
555 " quickfix commands (don't use the user defined commands)
556 if a:cchar == 'c'
557 $cc
558 else
559 $ll
560 endif
561 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200562 Xrewind
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200563 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200564 call assert_equal(5, line('.'))
565
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200566 10Xnext
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200567 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200568 call assert_equal(11, line('.'))
569 10Xprev
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200570 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200571 call assert_equal(5, line('.'))
572
Bram Moolenaar74240d32017-12-10 15:26:15 +0100573 " Jumping to an error from the error window using cc command
574 Xgetexpr ['Xqftestfile1:5:Line5',
575 \ 'Xqftestfile1:6:Line6',
576 \ 'Xqftestfile2:10:Line10',
577 \ 'Xqftestfile2:11:Line11']
578 Xopen
579 10Xcc
580 call assert_equal(11, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200581 call assert_equal('Xqftestfile2', @%)
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100582 Xopen
583 call cursor(2, 1)
584 if a:cchar == 'c'
585 .cc
586 else
587 .ll
588 endif
589 call assert_equal(6, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200590 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100591
592 " Jumping to an error from the error window (when only the error window is
593 " present)
594 Xopen | only
595 Xlast 1
596 call assert_equal(5, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200597 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100598
Bram Moolenaaree85df32017-03-19 14:19:50 +0100599 Xexpr ""
600 call assert_fails('Xnext', 'E42:')
601
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200602 call delete('Xqftestfile1')
603 call delete('Xqftestfile2')
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200604
605 " Should be able to use next/prev with invalid entries
606 Xexpr ""
607 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
608 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100609 Xaddexpr ['foo', 'bar', 'baz', 'quux', 'sh|moo']
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200610 call assert_equal(5, g:Xgetlist({'size' : 0}).size)
611 Xlast
612 call assert_equal(5, g:Xgetlist({'idx' : 0}).idx)
613 Xfirst
614 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
615 2Xnext
616 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100617endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200618
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100619func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200620 call Xtest_browse('c')
621 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100622endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200623
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000624" Test for memory allocation failures
625func Xnomem_tests(cchar)
626 call s:setup_commands(a:cchar)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100627
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000628 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
629 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
630
631 call test_alloc_fail(GetAllocId('qf_dirname_now'), 0, 0)
632 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100633
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200634 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000635 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100636
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200637 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000638 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100639
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200640 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000641 call assert_fails('Xfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100642
Yegappan Lakshmanan5a2d4a32022-02-26 10:31:32 +0000643 call test_alloc_fail(GetAllocId('qf_efm_fmtstr'), 0, 0)
644 set efm=%f
645 call assert_fails('Xexpr ["Xfile1"]', 'E342:')
646 set efm&
647
648 call test_alloc_fail(GetAllocId('qf_efm_fmtpart'), 0, 0)
649 set efm=%f:%l:%m,%f-%l-%m
650 call assert_fails('Xaddexpr ["Xfile2", "Xfile3"]', 'E342:')
651 set efm&
652
653 call test_alloc_fail(GetAllocId('qf_title'), 0, 0)
654 call assert_fails('Xexpr ""', 'E342:')
655 call assert_equal('', g:Xgetlist({'all': 1}).title)
656
657 call test_alloc_fail(GetAllocId('qf_mef_name'), 0, 0)
658 set makeef=Xtmp##.err
659 call assert_fails('Xgrep needle haystack', 'E342:')
660 set makeef&
661
662 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
663 call assert_fails('Xexpr "Xfile1:10:Line10"', 'E342:')
664
665 if a:cchar == 'l'
666 for id in ['qf_qfline', 'qf_qfinfo']
667 lgetexpr ["Xfile1:10:L10", "Xfile2:20:L20"]
668 call test_alloc_fail(GetAllocId(id), 0, 0)
669 call assert_fails('new', 'E342:')
670 call assert_equal(2, winnr('$'))
671 call assert_equal([], getloclist(0))
672 %bw!
673 endfor
674 endif
675
676 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
677 try
678 call assert_fails('Xvimgrep vim runtest.vim', 'E342:')
679 catch /^Vim:Interrupt$/
680 endtry
681
682 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
683 try
684 call assert_fails('Xvimgrep /vim/f runtest.vim', 'E342:')
685 catch /^Vim:Interrupt$/
686 endtry
687
688 let l = getqflist({"lines": ["Xfile1:10:L10"]})
689 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
690 call assert_fails('call g:Xsetlist(l.items)', 'E342:')
691
692 call test_alloc_fail(GetAllocId('qf_qfline'), 0, 0)
693 try
694 call assert_fails('Xhelpgrep quickfix', 'E342:')
695 catch /^Vim:Interrupt$/
696 endtry
697
698 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
699 call assert_fails('let l = g:Xgetlist({"lines": ["Xfile1:10:L10"]})', 'E342:')
700 call assert_equal(#{items: []}, l)
701
702 if a:cchar == 'l'
703 call setqflist([], 'f')
704 call setloclist(0, [], 'f')
705 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
706 call assert_fails('lhelpgrep quickfix', 'E342:')
707 call assert_equal([], getloclist(0))
708
709 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
710 call assert_fails('lvimgrep vim runtest.vim', 'E342:')
711
712 let l = getqflist({"lines": ["Xfile1:10:L10"]})
713 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
714 call assert_fails('call setloclist(0, l.items)', 'E342:')
715
716 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
717 call assert_fails('lbuffer', 'E342:')
718
719 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
720 call assert_fails('lexpr ["Xfile1:10:L10", "Xfile2:20:L20"]', 'E342:')
721
722 call test_alloc_fail(GetAllocId('qf_qfinfo'), 0, 0)
723 call assert_fails('lfile runtest.vim', 'E342:')
724 endif
725
726 call test_alloc_fail(GetAllocId('qf_dirstack'), 0, 0)
727 set efm=%DEntering\ dir\ %f,%f:%l:%m
728 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E342:')
729 set efm&
730
731 call test_alloc_fail(GetAllocId('qf_dirstack'), 0, 0)
732 set efm=%+P[%f],(%l)%m
733 call assert_fails('Xexpr ["[runtest.vim]", "(1)Hello"]', 'E342:')
734 set efm&
735
736 call test_alloc_fail(GetAllocId('qf_multiline_pfx'), 0, 0)
737 set efm=%EError,%Cline\ %l,%Z%m
738 call assert_fails('Xexpr ["Error", "line 1", "msg"]', 'E342:')
739 set efm&
740
741 call test_alloc_fail(GetAllocId('qf_makecmd'), 0, 0)
742 call assert_fails('Xgrep vim runtest.vim', 'E342:')
743
744 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
745 call assert_fails('Xexpr repeat("a", 8192)', 'E342:')
746
747 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
748 call assert_fails('Xexpr [repeat("a", 8192)]', 'E342:')
749
750 new
751 call setline(1, repeat('a', 8192))
752 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
753 call assert_fails('Xbuffer', 'E342:')
754 %bw!
755
756 call writefile([repeat('a', 8192)], 'Xtest')
757 call test_alloc_fail(GetAllocId('qf_linebuf'), 0, 0)
758 call assert_fails('Xfile Xtest', 'E342:')
759 call delete('Xtest')
760endfunc
761
762func Test_nomem()
763 call Xnomem_tests('c')
764 call Xnomem_tests('l')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100765endfunc
766
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100767func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200768 call s:setup_commands(a:cchar)
769 Xhelpgrep quickfix
770 Xopen
771 if a:cchar == 'c'
772 let title_text = ':helpgrep quickfix'
773 else
774 let title_text = ':lhelpgrep quickfix'
775 endif
776 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200777
778 " Jumping to a help topic should open the help window
779 only
780 Xnext
781 call assert_true(&buftype == 'help')
782 call assert_true(winnr('$') == 2)
783 " Jumping to the next match should reuse the help window
784 Xnext
785 call assert_true(&buftype == 'help')
786 call assert_true(winnr() == 1)
787 call assert_true(winnr('$') == 2)
788 " Jumping to the next match from the quickfix window should reuse the help
789 " window
790 Xopen
791 Xnext
792 call assert_true(&buftype == 'help')
793 call assert_true(winnr() == 1)
794 call assert_true(winnr('$') == 2)
thinca6864efa2021-06-19 20:45:20 +0200795 call assert_match('|\d\+ col \d\+-\d\+|', getbufline(winbufnr(2), 1)[0])
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200796
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100797 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200798 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100799
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100800 " When the current window is vertically split, jumping to a help match
801 " should open the help window at the top.
802 only | enew
803 let w1 = win_getid()
804 vert new
805 let w2 = win_getid()
806 Xnext
807 let w3 = win_getid()
808 call assert_true(&buftype == 'help')
809 call assert_true(winnr() == 1)
Bram Moolenaara106e6c2019-11-24 22:13:58 +0100810 " See jump_to_help_window() for details
811 let w2_width = winwidth(w2)
812 if w2_width != &columns && w2_width < 80
813 call assert_equal(['col', [['leaf', w3],
814 \ ['row', [['leaf', w2], ['leaf', w1]]]]], winlayout())
815 else
816 call assert_equal(['row', [['col', [['leaf', w3], ['leaf', w2]]],
817 \ ['leaf', w1]]] , winlayout())
818 endif
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100819
820 new | only
821 set buftype=help
822 set modified
823 call assert_fails('Xnext', 'E37:')
824 set nomodified
825 new | only
826
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200827 if a:cchar == 'l'
828 " When a help window is present, running :lhelpgrep should reuse the
829 " help window and not the current window
830 new | only
831 call g:Xsetlist([], 'f')
832 help index.txt
833 wincmd w
834 lhelpgrep quickfix
835 call assert_equal(1, winnr())
836 call assert_notequal([], getloclist(1))
837 call assert_equal([], getloclist(2))
838 endif
839
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200840 new | only
841
Bram Moolenaaree85df32017-03-19 14:19:50 +0100842 " Search for non existing help string
843 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar108e7b42018-10-11 17:39:12 +0200844 " Invalid regular expression
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200845 call assert_fails('Xhelpgrep \@<!', 'E866:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100846endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200847
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100848func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200849 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200850 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200851 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100852endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100853
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100854def Test_helpgrep_vim9_restore_cpo()
855 assert_equal('aABceFs', &cpo)
856
857 var rtp_save = &rtp
858 var dir = 'Xruntime/after'
859 &rtp ..= ',' .. dir
860 mkdir(dir .. '/ftplugin', 'p')
Bram Moolenaar39f3b142021-02-14 12:57:36 +0100861 writefile(['vim9script'], dir .. '/ftplugin/qf.vim')
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100862 filetype plugin on
863 silent helpgrep grail
864 cwindow
865 silent helpgrep grail
866
867 assert_equal('aABceFs', &cpo)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000868 delete('Xruntime', 'rf')
Bram Moolenaare5a2dc82021-01-03 19:52:05 +0100869 &rtp = rtp_save
870 cclose
871 helpclose
872enddef
873
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +0000874" When running the :helpgrep command, if an autocmd modifies the 'cpoptions'
875" value, then Vim crashes. (issue fixed by 7.2b-004 and 8.2.4453)
Bram Moolenaar4791fcd2022-02-23 12:06:00 +0000876func Test_helpgrep_restore_cpo_aucmd()
877 let save_cpo = &cpo
878 augroup QF_Test
879 au!
880 autocmd BufNew * set cpo=acd
881 augroup END
882
883 helpgrep quickfix
884 call assert_equal('acd', &cpo)
885 %bw!
886
887 set cpo&vim
888 augroup QF_Test
889 au!
890 autocmd BufReadPost * set cpo=
891 augroup END
892
893 helpgrep buffer
894 call assert_equal('', &cpo)
895
896 augroup QF_Test
897 au!
898 augroup END
899 %bw!
900 let &cpo = save_cpo
901endfunc
902
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +0200903def Test_vim9_cexpr()
904 var text = 'somefile:95:error'
905 cexpr text
906 var l = getqflist()
907 assert_equal(1, l->len())
908 assert_equal(95, l[0].lnum)
909 assert_equal('error', l[0].text)
910
911 text = 'somefile:77:warning'
912 caddexpr text
913 l = getqflist()
914 assert_equal(2, l->len())
915 assert_equal(77, l[1].lnum)
916 assert_equal('warning', l[1].text)
917enddef
918
Bram Moolenaar6920c722016-01-22 22:44:10 +0100919func Test_errortitle()
920 augroup QfBufWinEnter
921 au!
922 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
923 augroup END
924 copen
925 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'')'}]
926 call setqflist(a)
927 call assert_equal(':setqflist()', g:a)
928 augroup QfBufWinEnter
929 au!
930 augroup END
931 augroup! QfBufWinEnter
932endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100933
Bram Moolenaar5584df62016-03-18 21:00:51 +0100934func Test_vimgreptitle()
935 augroup QfBufWinEnter
936 au!
937 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
938 augroup END
939 try
940 vimgrep /pattern/j file
941 catch /E480/
942 endtry
943 copen
944 call assert_equal(': vimgrep /pattern/j file', g:a)
945 augroup QfBufWinEnter
946 au!
947 augroup END
948 augroup! QfBufWinEnter
949endfunc
950
Bram Moolenaar1d30fde2021-10-20 21:58:42 +0100951func Test_bufwinenter_once()
952 augroup QfBufWinEnter
953 au!
954 au BufWinEnter * let g:got_afile ..= 'got ' .. expand('<afile>')
955 augroup END
956 let g:got_afile = ''
957 copen
958 call assert_equal('got quickfix', g:got_afile)
959
960 cclose
961 unlet g:got_afile
962 augroup QfBufWinEnter
963 au!
964 augroup END
965 augroup! QfBufWinEnter
966endfunc
967
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100968func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200969 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +0100970
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200971 Xgetexpr ['file:1:1:message']
972 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100973 if a:cchar == 'c'
974 call setqflist(l, 'r')
975 else
976 call setloclist(0, l, 'r')
977 endif
978
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200979 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +0100980 if a:cchar == 'c'
981 let title = ':setqflist()'
982 else
983 let title = ':setloclist()'
984 endif
985 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200986 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100987endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100988
989" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100990func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200991 call XqfTitleTests('c')
992 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100993endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100994
995" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100996func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100997 let save_efm = &efm
998 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
999 cgetexpr ['WWWW', 'EEEE', 'CCCC']
1000 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1001 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
1002 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
1003 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1004 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
1005 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
1006 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
1007 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
1008 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001009endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001010
1011" This will test for problems in quickfix:
1012" A. incorrectly copying location lists which caused the location list to show
1013" a different name than the file that was actually being displayed.
1014" B. not reusing the window for which the location list window is opened but
1015" instead creating new windows.
1016" C. make sure that the location list window is not reused instead of the
1017" window it belongs to.
1018"
1019" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001020func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001021 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
1022 let word = substitute(base, '\v(.*)\..*', '\1', '')
1023
1024 setl modifiable
1025 setl noreadonly
1026 setl noswapfile
1027 setl bufhidden=delete
1028 %del _
1029 " For problem 2:
1030 " 'buftype' has to be set to reproduce the constant opening of new windows
1031 setl buftype=nofile
1032
1033 call setline(1, word)
1034
1035 setl nomodified
1036 setl nomodifiable
1037 setl readonly
1038 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001039endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001040
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001041func Test_locationlist()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001042 enew
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001043
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001044 augroup testgroup
1045 au!
1046 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
1047 augroup END
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001048
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001049 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001050
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001051 let qflist = []
1052 for word in words
1053 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
1054 " NOTE: problem 1:
1055 " intentionally not setting 'lnum' so that the quickfix entries are not
1056 " valid
1057 eval qflist->setloclist(0, ' ')
1058 endfor
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001059
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001060 " Test A
1061 lrewind
1062 enew
1063 lopen
1064 4lnext
1065 vert split
1066 wincmd L
1067 lopen
1068 wincmd p
1069 lnext
1070 let fileName = expand("%")
1071 wincmd p
1072 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
1073 let fileName = substitute(fileName, '\\', '/', 'g')
1074 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
1075 call assert_equal("test://bar.txt", fileName)
1076 call assert_equal("test://bar.txt", locationListFileName)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001077
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001078 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001079
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001080 " Test B:
1081 lrewind
1082 lopen
1083 2
1084 exe "normal \<CR>"
1085 wincmd p
1086 3
1087 exe "normal \<CR>"
1088 wincmd p
1089 4
1090 exe "normal \<CR>"
1091 call assert_equal(2, winnr('$'))
1092 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001093
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001094 " Test C:
1095 lrewind
1096 lopen
1097 " Let's move the location list window to the top to check whether it (the
1098 " first window found) will be reused when we try to open new windows:
1099 wincmd K
1100 2
1101 exe "normal \<CR>"
1102 wincmd p
1103 3
1104 exe "normal \<CR>"
1105 wincmd p
1106 4
1107 exe "normal \<CR>"
1108 1wincmd w
1109 call assert_equal('quickfix', &buftype)
1110 2wincmd w
1111 let bufferName = expand("%")
1112 let bufferName = substitute(bufferName, '\\', '/', 'g')
1113 call assert_equal('test://quux.txt', bufferName)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001114
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001115 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +01001116
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001117 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +01001118endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +01001119
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001120func Test_locationlist_curwin_was_closed()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001121 augroup testgroup
1122 au!
1123 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
1124 augroup END
Bram Moolenaar0899d692016-03-19 13:35:03 +01001125
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001126 func! R(n)
1127 quit
1128 endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +01001129
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001130 new
1131 let q = []
1132 call add(q, {'filename': 'test_curwin.txt' })
1133 call setloclist(0, q)
1134 call assert_fails('lrewind', 'E924:')
Bram Moolenaar0899d692016-03-19 13:35:03 +01001135
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001136 augroup! testgroup
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00001137 delfunc R
Bram Moolenaaree85df32017-03-19 14:19:50 +01001138endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001139
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001140func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02001141 call writefile(['loclistfoo'], 'loclistfoo')
1142 call writefile(['loclistbar'], 'loclistbar')
1143 set switchbuf=usetab
1144
1145 edit loclistfoo
1146 tabedit loclistbar
1147 silent lgrep loclistfoo loclist*
1148 call assert_equal(1, tabpagenr())
1149
1150 enew | only | tabonly
1151 set switchbuf&vim
1152 call delete('loclistfoo')
1153 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001154endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +02001155
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001156" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001157func Test_efm1()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001158 " The 'errorformat' setting is different on non-Unix systems.
1159 " This test works only on Unix-like systems.
1160 CheckUnix
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001161
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001162 let l =<< trim [DATA]
1163 "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.
1164 "Xtestfile", line 6 col 19; this is an error
1165 gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c
1166 Xtestfile:9: parse error before `asd'
1167 make: *** [vim] Error 1
1168 in file "Xtestfile" linenr 10: there is an error
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001169
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001170 2 returned
1171 "Xtestfile", line 11 col 1; this is an error
1172 "Xtestfile", line 12 col 2; this is another error
1173 "Xtestfile", line 14:10; this is an error in column 10
1174 =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time
1175 "Xtestfile", linenr 16: yet another problem
1176 Error in "Xtestfile" at line 17:
1177 x should be a dot
1178 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
1179 ^
1180 Error in "Xtestfile" at line 18:
1181 x should be a dot
1182 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
1183 .............^
1184 Error in "Xtestfile" at line 19:
1185 x should be a dot
1186 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
1187 --------------^
1188 Error in "Xtestfile" at line 20:
1189 x should be a dot
1190 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
1191 ^
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001192
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001193 Does anyone know what is the problem and how to correction it?
1194 "Xtestfile", line 21 col 9: What is the title of the quickfix window?
1195 "Xtestfile", line 22 col 9: What is the title of the quickfix window?
1196 [DATA]
1197
1198 call writefile(l, 'Xerrorfile1')
1199 call writefile(l[:-2], 'Xerrorfile2')
1200
1201 let m =<< [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001202 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2
1203 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3
1204 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4
1205 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5
1206 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6
1207 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7
1208 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8
1209 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9
1210 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10
1211 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11
1212 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12
1213 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13
1214 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14
1215 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15
1216 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16
1217 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
1218 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
1219 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
1220 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
1221 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21
1222 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22
1223[DATA]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001224 call writefile(m, 'Xtestfile')
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001225
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001226 let save_efm = &efm
1227 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
1228 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001229
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001230 exe 'cf Xerrorfile2'
1231 clast
1232 copen
1233 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
1234 wincmd p
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001235
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001236 exe 'cf Xerrorfile1'
1237 call assert_equal([4, 12], [line('.'), col('.')])
1238 cn
1239 call assert_equal([6, 19], [line('.'), col('.')])
1240 cn
1241 call assert_equal([9, 2], [line('.'), col('.')])
1242 cn
1243 call assert_equal([10, 2], [line('.'), col('.')])
1244 cn
1245 call assert_equal([11, 1], [line('.'), col('.')])
1246 cn
1247 call assert_equal([12, 2], [line('.'), col('.')])
1248 cn
1249 call assert_equal([14, 10], [line('.'), col('.')])
1250 cn
1251 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
1252 cn
1253 call assert_equal([16, 2], [line('.'), col('.')])
1254 cn
1255 call assert_equal([17, 6], [line('.'), col('.')])
1256 cn
1257 call assert_equal([18, 7], [line('.'), col('.')])
1258 cn
1259 call assert_equal([19, 8], [line('.'), col('.')])
1260 cn
1261 call assert_equal([20, 9], [line('.'), col('.')])
1262 clast
1263 cprev
1264 cprev
1265 wincmd w
1266 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
1267 wincmd p
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001268
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001269 let &efm = save_efm
1270 call delete('Xerrorfile1')
1271 call delete('Xerrorfile2')
1272 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +01001273endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001274
Bram Moolenaarab47c612016-06-14 22:02:26 +02001275" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001276func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001277 call s:setup_commands(a:cchar)
1278
Bram Moolenaarab47c612016-06-14 22:02:26 +02001279 let save_efm=&efm
1280 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1281
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001282 let lines = ["Entering dir 'dir1/a'",
1283 \ 'habits2.txt:1:Nine Healthy Habits',
1284 \ "Entering dir 'b'",
1285 \ 'habits3.txt:2:0 Hours of television',
1286 \ 'habits2.txt:7:5 Small meals',
1287 \ "Entering dir 'dir1/c'",
1288 \ 'habits4.txt:3:1 Hour of exercise',
1289 \ "Leaving dir 'dir1/c'",
1290 \ "Leaving dir 'dir1/a'",
1291 \ 'habits1.txt:4:2 Liters of water',
1292 \ "Entering dir 'dir2'",
1293 \ 'habits5.txt:5:3 Cups of hot green tea',
1294 \ "Leaving dir 'dir2'"
1295 \]
Bram Moolenaarab47c612016-06-14 22:02:26 +02001296
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001297 Xexpr ""
1298 for l in lines
1299 Xaddexpr l
1300 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +02001301
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001302 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001303
1304 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
1305 call assert_equal(1, qf[1].lnum)
1306 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
1307 call assert_equal(2, qf[3].lnum)
1308 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
1309 call assert_equal(7, qf[4].lnum)
1310 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
1311 call assert_equal(3, qf[6].lnum)
1312 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
1313 call assert_equal(4, qf[9].lnum)
1314 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
1315 call assert_equal(5, qf[11].lnum)
1316
1317 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001318endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001319
1320" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001321func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001322 " Create the directory stack and files
1323 call mkdir('dir1')
1324 call mkdir('dir1/a')
1325 call mkdir('dir1/a/b')
1326 call mkdir('dir1/c')
1327 call mkdir('dir2')
1328
1329 let lines = ["Nine Healthy Habits",
1330 \ "0 Hours of television",
1331 \ "1 Hour of exercise",
1332 \ "2 Liters of water",
1333 \ "3 Cups of hot green tea",
1334 \ "4 Short mental breaks",
1335 \ "5 Small meals",
1336 \ "6 AM wake up time",
1337 \ "7 Minutes of laughter",
1338 \ "8 Hours of sleep (at least)",
1339 \ "9 PM end of the day and off to bed"
1340 \ ]
1341 call writefile(lines, 'habits1.txt')
1342 call writefile(lines, 'dir1/a/habits2.txt')
1343 call writefile(lines, 'dir1/a/b/habits3.txt')
1344 call writefile(lines, 'dir1/c/habits4.txt')
1345 call writefile(lines, 'dir2/habits5.txt')
1346
1347 call s:dir_stack_tests('c')
1348 call s:dir_stack_tests('l')
1349
1350 call delete('dir1', 'rf')
1351 call delete('dir2', 'rf')
1352 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001353endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001354
Bram Moolenaar9b457942016-10-09 16:10:05 +02001355" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001356func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001357 call s:setup_commands(a:cchar)
1358
1359 let save_efm = &efm
1360
1361 let &efm =
1362 \ '%Eerror %m %l,' .
1363 \ '%-Wignored %m %l,' .
1364 \ '%+Cmore ignored %m %l,' .
1365 \ '%Zignored end'
1366 Xgetexpr ['ignored warning 1', 'more ignored continuation 2', 'ignored end', 'error resync 4']
1367 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
1368 call assert_equal([['resync', 1, 4, 'E']], l)
1369
1370 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001371endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001372
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001373func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +02001374 call Xefm_ignore_continuations('c')
1375 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001376endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001377
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001378" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001379func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001380 call s:setup_commands(a:cchar)
1381
Bram Moolenaar049cba92016-06-26 14:38:04 +02001382 let save_efm = &efm
1383
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001384 set efm=%f:%l:%m,%f:%f:%l:%m
1385 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
1386
1387 set efm=%f:%l:%m,%f:%l:%r:%m
1388 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1389
1390 set efm=%f:%l:%m,%O:%f:%l:%m
1391 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1392
1393 set efm=%f:%l:%m,%f:%l:%*[^a-z
1394 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
1395
1396 set efm=%f:%l:%m,%f:%l:%*c
1397 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
1398
1399 set efm=%f:%l:%m,%L%M%N
1400 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
1401
1402 set efm=%f:%l:%m,%f:%l:%m:%R
1403 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
1404
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001405 " Invalid regular expression
1406 set efm=%\\%%k
1407 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E867:')
1408
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001409 set efm=
1410 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
1411
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001412 " Empty directory name. When there is an error in parsing new entries, make
1413 " sure the previous quickfix list is made the current list.
1414 set efm&
1415 cexpr ["one", "two"]
1416 let qf_id = getqflist(#{id: 0}).id
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001417 set efm=%DEntering\ dir\ abc,%f:%l:%m
1418 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001419 call assert_equal(qf_id, getqflist(#{id: 0}).id)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001420
1421 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001422endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001423
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001424func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001425 call Xinvalid_efm_Tests('c')
1426 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001427endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001428
1429" TODO:
1430" Add tests for the following formats in 'errorformat'
1431" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001432func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001433 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001434
1435 " Test for %s format in efm
1436 set efm=%f:%s
1437 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001438 let l = getqflist()
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001439 call assert_equal('^\VLine search text\$', l[0].pattern)
1440 call assert_equal(0, l[0].lnum)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001441
Bram Moolenaaree85df32017-03-19 14:19:50 +01001442 let l = split(execute('clist', ''), "\n")
1443 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1444
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001445 " Test for a long line
1446 cexpr 'Xtestfile:' . repeat('a', 1026)
1447 let l = getqflist()
1448 call assert_equal('^\V' . repeat('a', 1019) . '\$', l[0].pattern)
1449
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001450 " Test for %P, %Q and %t format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001451 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001452 [Xtestfile1]
1453 (1,17) error: ';' missing
1454 (21,2) warning: variable 'z' not defined
1455 (67,3) error: end of file found before string ended
1456 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001457
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001458 [Xtestfile2]
1459 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001460
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001461 [Xtestfile3]
1462 NEW compiler v1.1
1463 (2,2) warning: variable 'x' not defined
1464 (67,3) warning: 's' already defined
1465 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001466 [DATA]
1467
Bram Moolenaaree85df32017-03-19 14:19:50 +01001468 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001469 " To exercise the push/pop file functionality in quickfix, the test files
1470 " need to be created.
1471 call writefile(['Line1'], 'Xtestfile1')
1472 call writefile(['Line2'], 'Xtestfile2')
1473 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001474 cexpr ""
1475 for l in lines
1476 caddexpr l
1477 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001478 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001479 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001480 call assert_equal(21, l[2].lnum)
1481 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001482 call assert_equal('w', l[2].type)
1483 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001484 call delete('Xtestfile1')
1485 call delete('Xtestfile2')
1486 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001487
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001488 " Test for %P, %Q with non-existing files
1489 cexpr lines
1490 let l = getqflist()
1491 call assert_equal(14, len(l))
1492 call assert_equal('[Xtestfile1]', l[0].text)
1493 call assert_equal('[Xtestfile2]', l[6].text)
1494 call assert_equal('[Xtestfile3]', l[9].text)
1495
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001496 " Tests for %E, %C and %Z format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001497 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001498 Error 275
1499 line 42
1500 column 3
1501 ' ' expected after '--'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001502 [DATA]
1503
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001504 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1505 cgetexpr lines
1506 let l = getqflist()
1507 call assert_equal(275, l[0].nr)
1508 call assert_equal(42, l[0].lnum)
1509 call assert_equal(3, l[0].col)
1510 call assert_equal('E', l[0].type)
1511 call assert_equal("\n' ' expected after '--'", l[0].text)
1512
1513 " Test for %>
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001514 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001515 Error in line 147 of foo.c:
1516 unknown variable 'i'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001517 [DATA]
1518
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001519 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1520 cgetexpr lines
1521 let l = getqflist()
1522 call assert_equal(147, l[0].lnum)
1523 call assert_equal('E', l[0].type)
1524 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001525
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001526 " Test for %A, %C and other formats
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001527 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001528 ==============================================================
1529 FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)
1530 --------------------------------------------------------------
1531 Traceback (most recent call last):
1532 File "unittests/dbfacadeTest.py", line 89, in testFoo
1533 self.assertEquals(34, dtid)
1534 File "/usr/lib/python2.2/unittest.py", line 286, in
1535 failUnlessEqual
1536 raise self.failureException, \\
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001537 W:AssertionError: 34 != 33
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001538
1539 --------------------------------------------------------------
1540 Ran 27 tests in 0.063s
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001541 [DATA]
1542
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001543 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%t:%m
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001544 cgetexpr lines
1545 let l = getqflist()
1546 call assert_equal(8, len(l))
1547 call assert_equal(89, l[4].lnum)
1548 call assert_equal(1, l[4].valid)
1549 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001550 call assert_equal('W', l[4].type)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001551
Bram Moolenaard76ce852018-05-01 15:02:04 +02001552 " Test for %o
1553 set efm=%f(%o):%l\ %m
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001554 cgetexpr ['Xotestfile(Language.PureScript.Types):20 Error']
1555 call writefile(['Line1'], 'Xotestfile')
Bram Moolenaard76ce852018-05-01 15:02:04 +02001556 let l = getqflist()
1557 call assert_equal(1, len(l), string(l))
1558 call assert_equal('Language.PureScript.Types', l[0].module)
1559 copen
1560 call assert_equal('Language.PureScript.Types|20| Error', getline(1))
1561 call feedkeys("\<CR>", 'xn')
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001562 call assert_equal('Xotestfile', expand('%:t'))
Bram Moolenaard76ce852018-05-01 15:02:04 +02001563 cclose
1564 bd
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001565 call delete("Xotestfile")
Bram Moolenaard76ce852018-05-01 15:02:04 +02001566
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001567 " Test for a long module name
1568 cexpr 'Xtest(' . repeat('m', 1026) . '):15 message'
1569 let l = getqflist()
1570 call assert_equal(repeat('m', 1024), l[0].module)
1571 call assert_equal(15, l[0].lnum)
1572 call assert_equal('message', l[0].text)
1573
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001574 " The following sequence of commands used to crash Vim
1575 set efm=%W%m
1576 cgetexpr ['msg1']
1577 let l = getqflist()
1578 call assert_equal(1, len(l), string(l))
1579 call assert_equal('msg1', l[0].text)
1580 set efm=%C%m
1581 lexpr 'msg2'
1582 let l = getloclist(0)
1583 call assert_equal(1, len(l), string(l))
1584 call assert_equal('msg2', l[0].text)
1585 lopen
1586 call setqflist([], 'r')
1587 caddbuf
1588 let l = getqflist()
1589 call assert_equal(1, len(l), string(l))
1590 call assert_equal('|| msg2', l[0].text)
1591
Bram Moolenaar78ddc062018-05-15 21:56:34 +02001592 " When matching error lines, case should be ignored. Test for this.
1593 set noignorecase
1594 let l=getqflist({'lines' : ['Xtest:FOO10:Line 20'], 'efm':'%f:foo%l:%m'})
1595 call assert_equal(10, l.items[0].lnum)
1596 call assert_equal('Line 20', l.items[0].text)
1597 set ignorecase&
1598
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001599 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001600 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001601endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001602
Bram Moolenaare9283662020-06-07 14:10:47 +02001603" Test for '%t' (error type) field in 'efm'
1604func Test_efm_error_type()
1605 let save_efm = &efm
1606
1607 " error type
1608 set efm=%f:%l:%t:%m
1609 cexpr ["Xfile1:10:E:msg1", "Xfile1:20:W:msg2", "Xfile1:30:I:msg3",
1610 \ "Xfile1:40:N:msg4", "Xfile1:50:R:msg5"]
1611 let output = split(execute('clist'), "\n")
1612 call assert_equal([
1613 \ ' 1 Xfile1:10 error: msg1',
1614 \ ' 2 Xfile1:20 warning: msg2',
1615 \ ' 3 Xfile1:30 info: msg3',
1616 \ ' 4 Xfile1:40 note: msg4',
1617 \ ' 5 Xfile1:50 R: msg5'], output)
1618
1619 " error type and a error number
1620 set efm=%f:%l:%t:%n:%m
1621 cexpr ["Xfile1:10:E:2:msg1", "Xfile1:20:W:4:msg2", "Xfile1:30:I:6:msg3",
1622 \ "Xfile1:40:N:8:msg4", "Xfile1:50:R:3:msg5"]
1623 let output = split(execute('clist'), "\n")
1624 call assert_equal([
1625 \ ' 1 Xfile1:10 error 2: msg1',
1626 \ ' 2 Xfile1:20 warning 4: msg2',
1627 \ ' 3 Xfile1:30 info 6: msg3',
1628 \ ' 4 Xfile1:40 note 8: msg4',
1629 \ ' 5 Xfile1:50 R 3: msg5'], output)
1630 let &efm = save_efm
1631endfunc
1632
haya14busae023d492022-02-08 18:09:29 +00001633" Test for end_lnum ('%e') and end_col ('%k') fields in 'efm'
1634func Test_efm_end_lnum_col()
1635 let save_efm = &efm
1636
1637 " single line
1638 set efm=%f:%l-%e:%c-%k:%t:%m
1639 cexpr ["Xfile1:10-20:1-2:E:msg1", "Xfile1:20-30:2-3:W:msg2",]
1640 let output = split(execute('clist'), "\n")
1641 call assert_equal([
1642 \ ' 1 Xfile1:10-20 col 1-2 error: msg1',
1643 \ ' 2 Xfile1:20-30 col 2-3 warning: msg2'], output)
1644
1645 " multiple lines
1646 set efm=%A%n)%m,%Z%f:%l-%e:%c-%k
1647 cexpr ["1)msg1", "Xfile1:14-24:1-2",
1648 \ "2)msg2", "Xfile1:24-34:3-4"]
1649 let output = split(execute('clist'), "\n")
1650 call assert_equal([
1651 \ ' 1 Xfile1:14-24 col 1-2 error 1: msg1',
1652 \ ' 2 Xfile1:24-34 col 3-4 error 2: msg2'], output)
1653 let &efm = save_efm
1654endfunc
1655
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001656func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001657 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001658 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001659 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001660 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001661 colder
1662 cgetexpr []
1663 endfunc
1664 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001665 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001666 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001667 lolder
1668 lgetexpr []
1669 endfunc
1670 endif
1671
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001672 augroup QF_Test
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001673 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001674 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001675 augroup END
1676
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001677 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001678 let words = [ "a", "b" ]
1679 let qflist = []
1680 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001681 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001682 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001683 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001684 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001685
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001686 augroup QF_Test
1687 au!
1688 augroup END
1689
1690 if a:cchar == 'c'
1691 cexpr ["Xtest1:1:Line"]
1692 cwindow
1693 only
1694 augroup QF_Test
1695 au!
1696 autocmd WinEnter * call setqflist([], 'f')
1697 augroup END
1698 call assert_fails('exe "normal \<CR>"', 'E925:')
1699 augroup QF_Test
1700 au!
1701 augroup END
1702 endif
1703 %bw!
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001704endfunc
1705
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001706func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001707 call XquickfixChangedByAutocmd('c')
1708 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001709endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001710
Bram Moolenaar4d170af2020-09-13 22:21:22 +02001711func Test_setloclist_in_autocommand()
1712 call writefile(['test1', 'test2'], 'Xfile')
1713 edit Xfile
1714 let s:bufnr = bufnr()
1715 call setloclist(1,
1716 \ [{'bufnr' : s:bufnr, 'lnum' : 1, 'text' : 'test1'},
1717 \ {'bufnr' : s:bufnr, 'lnum' : 2, 'text' : 'test2'}])
1718
1719 augroup Test_LocList
1720 au!
1721 autocmd BufEnter * call setloclist(1,
1722 \ [{'bufnr' : s:bufnr, 'lnum' : 1, 'text' : 'test1'},
1723 \ {'bufnr' : s:bufnr, 'lnum' : 2, 'text' : 'test2'}], 'r')
1724 augroup END
1725
1726 lopen
1727 call assert_fails('exe "normal j\<CR>"', 'E926:')
1728
1729 augroup Test_LocList
1730 au!
1731 augroup END
1732 call delete('Xfile')
1733endfunc
1734
Bram Moolenaar8b201792016-03-25 15:01:10 +01001735func Test_caddbuffer_to_empty()
1736 helpgr quickfix
1737 call setqflist([], 'r')
1738 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001739 try
1740 cn
1741 catch
1742 " number of matches is unknown
1743 call assert_true(v:exception =~ 'E553:')
1744 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001745 quit!
1746endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001747
1748func Test_cgetexpr_works()
1749 " this must not crash Vim
1750 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001751 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001752endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001753
1754" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001755func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001756 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001757
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001758 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
thinca6864efa2021-06-19 20:45:20 +02001759 \ {'bufnr': a:bnum, 'lnum': 2, 'end_lnum': 3, 'col': 4, 'end_col': 5}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001760 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001761 call assert_equal(2, len(l))
1762 call assert_equal(2, l[1].lnum)
thinca6864efa2021-06-19 20:45:20 +02001763 call assert_equal(3, l[1].end_lnum)
1764 call assert_equal(4, l[1].col)
1765 call assert_equal(5, l[1].end_col)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001766
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001767 Xnext
1768 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1769 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001770 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001771 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001772 call assert_equal(3, line('.'))
1773
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001774 " Appending entries to the list should not change the cursor position
1775 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001776 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001777 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001778 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001779 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1780 call assert_equal(1, line('.'))
1781 close
1782
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001783 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001784 \ {'bufnr': a:bnum, 'lnum': 4},
1785 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001786 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001787 call assert_equal(3, len(l))
1788 call assert_equal(5, l[2].lnum)
1789
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001790 call g:Xsetlist([])
1791 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001792 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001793
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001794 " Tests for setting the 'valid' flag
1795 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1796 Xwindow
1797 call assert_equal(1, winnr('$'))
1798 let l = g:Xgetlist()
1799 call g:Xsetlist(l)
1800 call assert_equal(0, g:Xgetlist()[0].valid)
Bram Moolenaar9752c722018-12-22 16:49:34 +01001801 " Adding a non-valid entry should not mark the list as having valid entries
1802 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':5, 'valid':0}], 'a')
1803 Xwindow
1804 call assert_equal(1, winnr('$'))
1805
1806 " :cnext/:cprev should still work even with invalid entries in the list
1807 let l = [{'bufnr' : a:bnum, 'lnum' : 1, 'text' : '1', 'valid' : 0},
1808 \ {'bufnr' : a:bnum, 'lnum' : 2, 'text' : '2', 'valid' : 0}]
1809 call g:Xsetlist(l)
1810 Xnext
1811 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1812 Xprev
1813 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1814 " :cnext/:cprev should still work after appending invalid entries to an
1815 " empty list
1816 call g:Xsetlist([])
1817 call g:Xsetlist(l, 'a')
1818 Xnext
1819 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1820 Xprev
1821 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1822
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001823 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1824 Xwindow
1825 call assert_equal(2, winnr('$'))
1826 Xclose
1827 let save_efm = &efm
1828 set efm=%m
1829 Xgetexpr 'TestMessage'
1830 let l = g:Xgetlist()
1831 call g:Xsetlist(l)
1832 call assert_equal(1, g:Xgetlist()[0].valid)
1833 let &efm = save_efm
1834
Bram Moolenaaree85df32017-03-19 14:19:50 +01001835 " Error cases:
1836 " Refer to a non-existing buffer and pass a non-dictionary type
1837 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1838 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1839 call g:Xsetlist([[1, 2,3]])
1840 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001841 call assert_fails('call g:Xsetlist([], [])', 'E928:')
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00001842 call g:Xsetlist([test_null_dict()])
1843 call assert_equal([], g:Xgetlist())
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001844endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001845
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001846func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001847 new Xtestfile | only
1848 let bnum = bufnr('%')
1849 call setline(1, range(1,5))
1850
1851 call SetXlistTests('c', bnum)
1852 call SetXlistTests('l', bnum)
1853
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001854 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001855 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001856endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001857
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001858func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001859 call s:setup_commands(a:cchar)
1860
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001861 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001862 let @/ = 'Test_'
1863 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001864 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001865 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001866 Xvimgrep empty test_quickfix.vim
1867 call assert_true(len(g:Xgetlist()) > 0)
1868 Xvimgrep matches test_quickfix.vim
1869 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001870 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001871 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001872 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001873 call g:Xsetlist([], 'r')
1874 call assert_true(len(g:Xgetlist()) == 0)
1875 Xolder
1876 call assert_equal(testlen, len(g:Xgetlist()))
1877 Xnewer
1878 Xnewer
1879 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001880endfunc
1881
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001882func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001883 call Xlist_empty_middle('c')
1884 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001885endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001886
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001887func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001888 call s:setup_commands(a:cchar)
1889
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001890 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001891 Xvimgrep one test_quickfix.vim
1892 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001893 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001894 Xvimgrep two test_quickfix.vim
1895 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001896 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001897 Xvimgrep three test_quickfix.vim
1898 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001899 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001900 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001901 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001902 call g:Xsetlist([], 'r')
1903 call assert_true(len(g:Xgetlist()) == 0)
1904 Xnewer
1905 call assert_equal(twolen, len(g:Xgetlist()))
1906 Xnewer
1907 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001908endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001909
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001910func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001911 call Xlist_empty_older('c')
1912 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001913endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001914
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001915func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001916 call s:setup_commands(a:cchar)
1917
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001918 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1919 \ {'filename': 'fnameB', 'text': 'B'}]
1920 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1921 \ {'filename': 'fnameD', 'text': 'D'},
1922 \ {'filename': 'fnameE', 'text': 'E'}]
1923
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001924 " {action} is unspecified. Same as specifying ' '.
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001925 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001926 silent! Xnewer 99
1927 call g:Xsetlist(list1)
1928 call g:Xsetlist(list2)
1929 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001930 call assert_equal(3, len(li))
1931 call assert_equal('C', li[0]['text'])
1932 call assert_equal('D', li[1]['text'])
1933 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001934 silent! Xolder
1935 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001936 call assert_equal(2, len(li))
1937 call assert_equal('A', li[0]['text'])
1938 call assert_equal('B', li[1]['text'])
1939
1940 " {action} is specified ' '.
1941 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001942 silent! Xnewer 99
1943 call g:Xsetlist(list1)
1944 call g:Xsetlist(list2, ' ')
1945 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001946 call assert_equal(3, len(li))
1947 call assert_equal('C', li[0]['text'])
1948 call assert_equal('D', li[1]['text'])
1949 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001950 silent! Xolder
1951 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001952 call assert_equal(2, len(li))
1953 call assert_equal('A', li[0]['text'])
1954 call assert_equal('B', li[1]['text'])
1955
1956 " {action} is specified 'a'.
1957 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001958 silent! Xnewer 99
1959 call g:Xsetlist(list1)
1960 call g:Xsetlist(list2, 'a')
1961 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001962 call assert_equal(5, len(li))
1963 call assert_equal('A', li[0]['text'])
1964 call assert_equal('B', li[1]['text'])
1965 call assert_equal('C', li[2]['text'])
1966 call assert_equal('D', li[3]['text'])
1967 call assert_equal('E', li[4]['text'])
1968
1969 " {action} is specified 'r'.
1970 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001971 silent! Xnewer 99
1972 call g:Xsetlist(list1)
1973 call g:Xsetlist(list2, 'r')
1974 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001975 call assert_equal(3, len(li))
1976 call assert_equal('C', li[0]['text'])
1977 call assert_equal('D', li[1]['text'])
1978 call assert_equal('E', li[2]['text'])
1979
1980 " Test for wrong value.
1981 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001982 call assert_fails("call g:Xsetlist(0)", 'E714:')
1983 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
1984 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
1985 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
1986 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001987endfunc
1988
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001989func Test_setqflist_invalid_nr()
1990 " The following command used to crash Vim
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001991 eval []->setqflist(' ', {'nr' : $XXX_DOES_NOT_EXIST})
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001992endfunc
1993
Bram Moolenaar99234f22020-02-10 22:56:54 +01001994func Test_setqflist_user_sets_buftype()
1995 call setqflist([{'text': 'foo'}, {'text': 'bar'}])
1996 set buftype=quickfix
1997 call setqflist([], 'a')
1998 enew
1999endfunc
2000
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002001func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02002002 call XquickfixSetListWithAct('c')
2003 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002004endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002005
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002006func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002007 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002008
Bram Moolenaar049cba92016-06-26 14:38:04 +02002009 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002010 call assert_equal(1, l[0].lnum)
2011 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002012 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002013 call assert_equal(2, l[1].lnum)
2014 call assert_equal(1, l[1].col)
2015 call assert_equal(4070, len(l[1].text))
2016 call assert_equal(3, l[2].lnum)
2017 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002018 call assert_equal(4070, len(l[2].text))
2019 call assert_equal(4, l[3].lnum)
2020 call assert_equal(1, l[3].col)
2021 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002022
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002023 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002024endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002025
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002026func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002027 call s:setup_commands(a:cchar)
2028
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002029 let testfile = 'samples/quickfix.txt'
2030
2031 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002032 exe 'Xgetfile' testfile
2033 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002034
2035 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002036 Xexpr readfile(testfile)
2037 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002038
2039 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002040 Xexpr join(readfile(testfile), "\n")
2041 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02002042
2043 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002044 exe 'edit' testfile
2045 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02002046 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002047endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002048
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002049func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02002050 call s:long_lines_tests('c')
2051 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002052endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002053
Bram Moolenaar59941cb2020-09-05 17:03:40 +02002054func Test_cgetfile_on_long_lines()
2055 " Problematic values if the line is longer than 4096 bytes. Then 1024 bytes
2056 " are read at a time.
2057 for len in [4078, 4079, 4080, 5102, 5103, 5104, 6126, 6127, 6128, 7150, 7151, 7152]
2058 let lines = [
2059 \ '/tmp/file1:1:1:aaa',
2060 \ '/tmp/file2:1:1:%s',
2061 \ '/tmp/file3:1:1:bbb',
2062 \ '/tmp/file4:1:1:ccc',
2063 \ ]
2064 let lines[1] = substitute(lines[1], '%s', repeat('x', len), '')
2065 call writefile(lines, 'Xcqetfile.txt')
2066 cgetfile Xcqetfile.txt
2067 call assert_equal(4, getqflist(#{size: v:true}).size, 'with length ' .. len)
2068 endfor
2069 call delete('Xcqetfile.txt')
2070endfunc
2071
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002072func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002073 let l = []
2074 for i in range(1, 20)
2075 call add(l, 'Line' . i)
2076 endfor
2077 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002078endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002079
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002080func Test_switchbuf()
Bram Moolenaar049cba92016-06-26 14:38:04 +02002081 call s:create_test_file('Xqftestfile1')
2082 call s:create_test_file('Xqftestfile2')
2083 call s:create_test_file('Xqftestfile3')
2084
2085 new | only
2086 edit Xqftestfile1
2087 let file1_winid = win_getid()
2088 new Xqftestfile2
2089 let file2_winid = win_getid()
2090 cgetexpr ['Xqftestfile1:5:Line5',
2091 \ 'Xqftestfile1:6:Line6',
2092 \ 'Xqftestfile2:10:Line10',
2093 \ 'Xqftestfile2:11:Line11',
2094 \ 'Xqftestfile3:15:Line15',
2095 \ 'Xqftestfile3:16:Line16']
2096
2097 new
2098 let winid = win_getid()
2099 cfirst | cnext
2100 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002101 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002102 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002103 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002104 call assert_equal(winid, win_getid())
Bram Moolenaar049cba92016-06-26 14:38:04 +02002105
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002106 " Test for 'switchbuf' set to search for files in windows in the current
2107 " tabpage and jump to an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002108 set switchbuf=useopen
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002109 enew
Bram Moolenaar049cba92016-06-26 14:38:04 +02002110 cfirst | cnext
2111 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002112 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002113 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002114 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002115 call assert_equal(file2_winid, win_getid())
2116
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002117 " Test for 'switchbuf' set to search for files in tabpages and jump to an
2118 " existing tabpage (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002119 enew | only
2120 set switchbuf=usetab
2121 tabedit Xqftestfile1
2122 tabedit Xqftestfile2
Bram Moolenaar74240d32017-12-10 15:26:15 +01002123 tabedit Xqftestfile3
Bram Moolenaar049cba92016-06-26 14:38:04 +02002124 tabfirst
2125 cfirst | cnext
2126 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02002127 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02002128 call assert_equal(3, tabpagenr())
Bram Moolenaar74240d32017-12-10 15:26:15 +01002129 6cnext
2130 call assert_equal(4, tabpagenr())
2131 2cpfile
2132 call assert_equal(2, tabpagenr())
2133 2cnfile
2134 call assert_equal(4, tabpagenr())
Bram Moolenaar049cba92016-06-26 14:38:04 +02002135 tabfirst | tabonly | enew
2136
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002137 " Test for 'switchbuf' set to open a new window for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02002138 set switchbuf=split
2139 cfirst | cnext
2140 call assert_equal(1, winnr('$'))
2141 cnext | cnext
2142 call assert_equal(2, winnr('$'))
2143 cnext | cnext
2144 call assert_equal(3, winnr('$'))
Bram Moolenaar049cba92016-06-26 14:38:04 +02002145
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002146 " Test for 'switchbuf' set to open a new tabpage for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02002147 set switchbuf=newtab
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002148 enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02002149 cfirst | cnext
2150 call assert_equal(1, tabpagenr('$'))
2151 cnext | cnext
2152 call assert_equal(2, tabpagenr('$'))
2153 cnext | cnext
2154 call assert_equal(3, tabpagenr('$'))
2155 tabfirst | enew | tabonly | only
2156
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01002157 set switchbuf=uselast
2158 split
2159 let last_winid = win_getid()
2160 copen
2161 exe "normal 1G\<CR>"
2162 call assert_equal(last_winid, win_getid())
2163 enew | only
2164
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002165 " With an empty 'switchbuf', jumping to a quickfix entry should open the
2166 " file in an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002167 set switchbuf=
2168 edit Xqftestfile1
2169 let file1_winid = win_getid()
2170 new Xqftestfile2
2171 let file2_winid = win_getid()
2172 copen
2173 exe "normal 1G\<CR>"
2174 call assert_equal(file1_winid, win_getid())
2175 copen
2176 exe "normal 3G\<CR>"
2177 call assert_equal(file2_winid, win_getid())
2178 copen | only
2179 exe "normal 5G\<CR>"
2180 call assert_equal(2, winnr('$'))
2181 call assert_equal(1, bufwinnr('Xqftestfile3'))
2182
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002183 " If only quickfix window is open in the current tabpage, jumping to an
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02002184 " entry with 'switchbuf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02002185 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002186 set switchbuf=usetab
2187 tabedit Xqftestfile1
2188 tabedit Xqftestfile2
2189 tabedit Xqftestfile3
2190 tabfirst
2191 copen | only
2192 clast
2193 call assert_equal(4, tabpagenr())
2194 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02002195
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002196 " Jumping to a file that is not present in any of the tabpages and the
2197 " current tabpage doesn't have any usable windows, should open it in a new
2198 " window in the current tabpage.
2199 copen | only
2200 cfirst
2201 call assert_equal(1, tabpagenr())
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02002202 call assert_equal('Xqftestfile1', @%)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002203
2204 " If opening a file changes 'switchbuf', then the new value should be
2205 " retained.
Bram Moolenaar41d42992020-05-03 16:29:50 +02002206 set modeline&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002207 call writefile(["vim: switchbuf=split"], 'Xqftestfile1')
2208 enew | only
2209 set switchbuf&vim
2210 cexpr "Xqftestfile1:1:10"
2211 call assert_equal('split', &switchbuf)
2212 call writefile(["vim: switchbuf=usetab"], 'Xqftestfile1')
2213 enew | only
2214 set switchbuf=useopen
2215 cexpr "Xqftestfile1:1:10"
2216 call assert_equal('usetab', &switchbuf)
2217 call writefile(["vim: switchbuf&vim"], 'Xqftestfile1')
2218 enew | only
2219 set switchbuf=useopen
2220 cexpr "Xqftestfile1:1:10"
2221 call assert_equal('', &switchbuf)
2222
Bram Moolenaar049cba92016-06-26 14:38:04 +02002223 call delete('Xqftestfile1')
2224 call delete('Xqftestfile2')
2225 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002226 set switchbuf&vim
2227
2228 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002229endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002230
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002231func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002232 call s:setup_commands(a:cchar)
2233
2234 enew | only
2235
Bram Moolenaarc1542742016-07-20 21:44:37 +02002236 let fname = 'Xqftestfile' . a:cchar
2237 call s:create_test_file(fname)
2238 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02002239
Bram Moolenaarc1542742016-07-20 21:44:37 +02002240 Xgetexpr [fname . ':5:Line5',
2241 \ fname . ':10:Line10',
2242 \ fname . ':15:Line15',
2243 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02002244
2245 6,14delete
2246 call append(6, ['Buffer', 'Window'])
2247
2248 let l = g:Xgetlist()
Bram Moolenaar049cba92016-06-26 14:38:04 +02002249 call assert_equal(5, l[0].lnum)
2250 call assert_equal(6, l[2].lnum)
2251 call assert_equal(13, l[3].lnum)
2252
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002253 " If a file doesn't have any quickfix entries, then deleting lines in the
2254 " file should not update the quickfix list
2255 call g:Xsetlist([], 'f')
2256 1,2delete
2257 call assert_equal([], g:Xgetlist())
2258
Bram Moolenaar049cba92016-06-26 14:38:04 +02002259 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02002260 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002261endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002262
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002263func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02002264 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02002265 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02002266 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02002267 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002268endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002269
2270" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002271func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002272 call s:setup_commands(a:cchar)
2273
2274 " The following lines are used for the grep test. Don't remove.
2275 " Grep_Test_Text: Match 1
2276 " Grep_Test_Text: Match 2
2277 " GrepAdd_Test_Text: Match 1
2278 " GrepAdd_Test_Text: Match 2
2279 enew! | only
2280 set makeef&vim
2281 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002282 call assert_true(len(g:Xgetlist()) == 5)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002283 Xopen
2284 call assert_true(w:quickfix_title =~ '^:grep')
2285 Xclose
2286 enew
2287 set makeef=Temp_File_##
2288 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002289 call assert_true(len(g:Xgetlist()) == 9)
2290
2291 " Try with 'grepprg' set to 'internal'
2292 set grepprg=internal
2293 silent Xgrep Grep_Test_Text: test_quickfix.vim
2294 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
2295 call assert_true(len(g:Xgetlist()) == 9)
2296 set grepprg&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002297
2298 call writefile(['Vim'], 'XtestTempFile')
2299 set makeef=XtestTempFile
2300 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002301 call assert_equal(5, len(g:Xgetlist()))
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002302 call assert_false(filereadable('XtestTempFile'))
2303 set makeef&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002304endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002305
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002306func Test_grep()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002307 " The grepprg may not be set on non-Unix systems
2308 CheckUnix
Bram Moolenaar049cba92016-06-26 14:38:04 +02002309
2310 call s:test_xgrep('c')
2311 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002312endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002313
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002314func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002315 " Use one 'errorformat' for two windows. Add an expression to each of them,
2316 " make sure they each keep their own state.
2317 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
2318 call mkdir('Xone/a', 'p')
2319 call mkdir('Xtwo/a', 'p')
2320 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2321 call writefile(lines, 'Xone/a/one.txt')
2322 call writefile(lines, 'Xtwo/a/two.txt')
2323
2324 new one
2325 let one_id = win_getid()
2326 lexpr ""
2327 new two
2328 let two_id = win_getid()
2329 lexpr ""
2330
2331 laddexpr "Entering dir 'Xtwo/a'"
2332 call win_gotoid(one_id)
2333 laddexpr "Entering dir 'Xone/a'"
2334 call win_gotoid(two_id)
2335 laddexpr 'two.txt:5:two two two'
2336 call win_gotoid(one_id)
2337 laddexpr 'one.txt:3:one one one'
2338
2339 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002340 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
2341 call assert_equal(3, loc_one[1].lnum)
2342
2343 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002344 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
2345 call assert_equal(5, loc_two[1].lnum)
2346
2347 call win_gotoid(one_id)
2348 bwipe!
2349 call win_gotoid(two_id)
2350 bwipe!
2351 call delete('Xone', 'rf')
2352 call delete('Xtwo', 'rf')
2353endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02002354
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002355func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02002356 call s:setup_commands(a:cchar)
2357
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002358 " Calling lbottom without any errors should fail
2359 if a:cchar == 'l'
2360 call assert_fails('lbottom', 'E776:')
2361 endif
2362
Bram Moolenaar875feea2017-06-11 16:07:51 +02002363 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02002364 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02002365 let wid = win_getid()
2366 call assert_equal(1, line('.'))
2367 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02002368 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02002369 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02002370 call win_gotoid(wid)
2371 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02002372 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02002373endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02002374
2375" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002376func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02002377 call XbottomTests('c')
2378 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002379endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002380
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002381func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002382 call s:setup_commands(a:cchar)
2383
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002384 " clear all lists after the first one, then replace the first one.
2385 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002386 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002387 let entry = {'filename': 'foo', 'lnum': 42}
2388 call g:Xsetlist([entry], 'r')
2389 call g:Xsetlist([entry, entry])
2390 call g:Xsetlist([entry, entry, entry])
2391 let res = split(execute(a:cchar . 'hist'), "\n")
2392 call assert_equal(3, len(res))
2393 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
2394 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
2395 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
2396 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002397
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002398 " Test for changing the quickfix lists
2399 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2400 exe '1' . a:cchar . 'hist'
2401 call assert_equal(1, g:Xgetlist({'nr' : 0}).nr)
2402 exe '3' . a:cchar . 'hist'
2403 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2404 call assert_fails('-2' . a:cchar . 'hist', 'E16:')
2405 call assert_fails('4' . a:cchar . 'hist', 'E16:')
2406
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002407 call g:Xsetlist([], 'f')
2408 let l = split(execute(a:cchar . 'hist'), "\n")
2409 call assert_equal('No entries', l[0])
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002410 if a:cchar == 'c'
2411 call assert_fails('4chist', 'E16:')
2412 else
2413 call assert_fails('4lhist', 'E776:')
2414 endif
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002415
2416 " An empty list should still show the stack history
2417 call g:Xsetlist([])
2418 let res = split(execute(a:cchar . 'hist'), "\n")
2419 call assert_equal('> error list 1 of 1; 0 ' . common, res[0])
2420
2421 call g:Xsetlist([], 'f')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002422endfunc
2423
2424func Test_history()
2425 call HistoryTest('c')
2426 call HistoryTest('l')
2427endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02002428
2429func Test_duplicate_buf()
2430 " make sure we can get the highest buffer number
2431 edit DoesNotExist
2432 edit DoesNotExist2
2433 let last_buffer = bufnr("$")
2434
2435 " make sure only one buffer is created
2436 call writefile(['this one', 'that one'], 'Xgrepthis')
2437 vimgrep one Xgrepthis
2438 vimgrep one Xgrepthis
2439 call assert_equal(last_buffer + 1, bufnr("$"))
2440
2441 call delete('Xgrepthis')
2442endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002443
2444" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002445func Xproperty_tests(cchar)
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002446 call s:setup_commands(a:cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002447
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002448 " Error cases
2449 call assert_fails('call g:Xgetlist(99)', 'E715:')
2450 call assert_fails('call g:Xsetlist(99)', 'E714:')
2451 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
Bram Moolenaard823fa92016-08-12 16:29:27 +02002452
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002453 " Set and get the title
2454 call g:Xsetlist([])
2455 Xopen
2456 wincmd p
2457 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
2458 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
2459 call assert_equal(0, s)
2460 let d = g:Xgetlist({"title":1})
2461 call assert_equal('Sample', d.title)
2462 " Try setting title to a non-string value
2463 call assert_equal(-1, g:Xsetlist([], 'a', {'title' : ['Test']}))
2464 call assert_equal('Sample', g:Xgetlist({"title":1}).title)
2465
2466 Xopen
2467 call assert_equal('Sample', w:quickfix_title)
2468 Xclose
2469
2470 " Tests for action argument
2471 silent! Xolder 999
2472 let qfnr = g:Xgetlist({'all':1}).nr
2473 call g:Xsetlist([], 'r', {'title' : 'N1'})
2474 call assert_equal('N1', g:Xgetlist({'all':1}).title)
2475 call g:Xsetlist([], ' ', {'title' : 'N2'})
2476 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
2477
2478 let res = g:Xgetlist({'nr': 0})
2479 call assert_equal(qfnr + 1, res.nr)
2480 call assert_equal(['nr'], keys(res))
2481
2482 call g:Xsetlist([], ' ', {'title' : 'N3'})
2483 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
2484
2485 " Changing the title of an earlier quickfix list
2486 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
2487 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
2488
2489 " Changing the title of an invalid quickfix list
2490 call assert_equal(-1, g:Xsetlist([], ' ',
2491 \ {'title' : 'SomeTitle', 'nr' : 99}))
2492 call assert_equal(-1, g:Xsetlist([], ' ',
2493 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
2494
2495 if a:cchar == 'c'
2496 copen
2497 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
2498 cclose
2499 endif
2500
2501 " Invalid arguments
Bram Moolenaare2e40752020-09-04 21:18:46 +02002502 call assert_fails('call g:Xgetlist([])', 'E715:')
2503 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002504 let s = g:Xsetlist([], 'a', {'abc':1})
2505 call assert_equal(-1, s)
2506
2507 call assert_equal({}, g:Xgetlist({'abc':1}))
2508 call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
2509 call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
2510
2511 if a:cchar == 'l'
2512 call assert_equal({}, getloclist(99, {'title': 1}))
2513 endif
2514
2515 " Context related tests
2516 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
2517 call assert_equal(0, s)
2518 call test_garbagecollect_now()
2519 let d = g:Xgetlist({'context':1})
2520 call assert_equal([1,2,3], d.context)
2521 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
2522 let d = g:Xgetlist({'context':1})
2523 call assert_equal({'color':'green'}, d.context)
2524 call g:Xsetlist([], 'a', {'context':"Context info"})
2525 let d = g:Xgetlist({'context':1})
2526 call assert_equal("Context info", d.context)
2527 call g:Xsetlist([], 'a', {'context':246})
2528 let d = g:Xgetlist({'context':1})
2529 call assert_equal(246, d.context)
2530 " set other Vim data types as context
2531 call g:Xsetlist([], 'a', {'context' : test_null_blob()})
2532 if has('channel')
2533 call g:Xsetlist([], 'a', {'context' : test_null_channel()})
2534 endif
2535 if has('job')
2536 call g:Xsetlist([], 'a', {'context' : test_null_job()})
2537 endif
2538 call g:Xsetlist([], 'a', {'context' : test_null_function()})
2539 call g:Xsetlist([], 'a', {'context' : test_null_partial()})
2540 call g:Xsetlist([], 'a', {'context' : ''})
2541 call test_garbagecollect_now()
2542 if a:cchar == 'l'
2543 " Test for copying context across two different location lists
2544 new | only
2545 let w1_id = win_getid()
2546 let l = [1]
2547 call setloclist(0, [], 'a', {'context':l})
2548 new
2549 let w2_id = win_getid()
2550 call add(l, 2)
2551 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
2552 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2553 unlet! l
2554 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2555 only
2556 call setloclist(0, [], 'f')
2557 call assert_equal('', getloclist(0, {'context':1}).context)
2558 endif
2559
2560 " Test for changing the context of previous quickfix lists
2561 call g:Xsetlist([], 'f')
2562 Xexpr "One"
2563 Xexpr "Two"
2564 Xexpr "Three"
2565 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
2566 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
2567 " Also, check for setting the context using quickfix list number zero.
2568 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
2569 call test_garbagecollect_now()
2570 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
2571 call assert_equal([1], l.context)
2572 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
2573 call assert_equal([2], l.context)
2574 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
2575 call assert_equal([3], l.context)
2576
2577 " Test for changing the context through reference and for garbage
2578 " collection of quickfix context
2579 let l = ["red"]
2580 call g:Xsetlist([], ' ', {'context' : l})
2581 call add(l, "blue")
2582 let x = g:Xgetlist({'context' : 1})
2583 call add(x.context, "green")
2584 call assert_equal(["red", "blue", "green"], l)
2585 call assert_equal(["red", "blue", "green"], x.context)
2586 unlet l
2587 call test_garbagecollect_now()
2588 let m = g:Xgetlist({'context' : 1})
2589 call assert_equal(["red", "blue", "green"], m.context)
2590
2591 " Test for setting/getting items
2592 Xexpr ""
2593 let qfprev = g:Xgetlist({'nr':0})
2594 let s = g:Xsetlist([], ' ', {'title':'Green',
2595 \ 'items' : [{'filename':'F1', 'lnum':10}]})
2596 call assert_equal(0, s)
2597 let qfcur = g:Xgetlist({'nr':0})
2598 call assert_true(qfcur.nr == qfprev.nr + 1)
2599 let l = g:Xgetlist({'items':1})
2600 call assert_equal('F1', bufname(l.items[0].bufnr))
2601 call assert_equal(10, l.items[0].lnum)
2602 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
2603 \ {'filename':'F2', 'lnum':30}]})
2604 let l = g:Xgetlist({'items':1})
2605 call assert_equal('F2', bufname(l.items[2].bufnr))
2606 call assert_equal(30, l.items[2].lnum)
2607 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
2608 let l = g:Xgetlist({'items':1})
2609 call assert_equal('F3', bufname(l.items[0].bufnr))
2610 call assert_equal(40, l.items[0].lnum)
2611 call g:Xsetlist([], 'r', {'items' : []})
2612 let l = g:Xgetlist({'items':1})
2613 call assert_equal(0, len(l.items))
2614
2615 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
2616 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2617 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2618 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
2619
2620 " Test for getting id of window associated with a location list window
2621 if a:cchar == 'l'
2622 only
2623 call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid)
2624 let wid = win_getid()
Bram Moolenaard823fa92016-08-12 16:29:27 +02002625 Xopen
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002626 call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid)
2627 wincmd w
2628 call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid)
2629 only
2630 endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02002631
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002632 " The following used to crash Vim with address sanitizer
2633 call g:Xsetlist([], 'f')
2634 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
2635 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002636
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002637 " Try setting the items using a string
2638 call assert_equal(-1, g:Xsetlist([], ' ', {'items' : 'Test'}))
Bram Moolenaar890680c2016-09-27 21:28:56 +02002639
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002640 " Save and restore the quickfix stack
2641 call g:Xsetlist([], 'f')
2642 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2643 Xexpr "File1:10:Line1"
2644 Xexpr "File2:20:Line2"
2645 Xexpr "File3:30:Line3"
2646 let last_qf = g:Xgetlist({'nr':'$'}).nr
2647 call assert_equal(3, last_qf)
2648 let qstack = []
2649 for i in range(1, last_qf)
2650 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
2651 endfor
2652 call g:Xsetlist([], 'f')
2653 for i in range(len(qstack))
2654 call g:Xsetlist([], ' ', qstack[i])
2655 endfor
2656 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2657 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
2658 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
2659 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
2660 call g:Xsetlist([], 'f')
Bram Moolenaar890680c2016-09-27 21:28:56 +02002661
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002662 " Swap two quickfix lists
2663 Xexpr "File1:10:Line10"
2664 Xexpr "File2:20:Line20"
2665 Xexpr "File3:30:Line30"
2666 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
2667 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
2668 let l1=g:Xgetlist({'nr':1,'all':1})
2669 let l2=g:Xgetlist({'nr':2,'all':1})
2670 let save_id = l1.id
2671 let l1.id=l2.id
2672 let l2.id=save_id
2673 call g:Xsetlist([], 'r', l1)
2674 call g:Xsetlist([], 'r', l2)
2675 let newl1=g:Xgetlist({'nr':1,'all':1})
2676 let newl2=g:Xgetlist({'nr':2,'all':1})
2677 call assert_equal('Fruits', newl1.title)
2678 call assert_equal(['Fruits'], newl1.context)
2679 call assert_equal('Line20', newl1.items[0].text)
2680 call assert_equal('Colors', newl2.title)
2681 call assert_equal(['Colors'], newl2.context)
2682 call assert_equal('Line10', newl2.items[0].text)
2683 call g:Xsetlist([], 'f')
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02002684
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002685 " Cannot specify both a non-empty list argument and a dict argument
2686 call assert_fails("call g:Xsetlist([{}], ' ', {})", 'E475:')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002687endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002688
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002689func Test_qf_property()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002690 call Xproperty_tests('c')
2691 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002692endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002693
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002694" Test for setting the current index in the location/quickfix list
2695func Xtest_setqfidx(cchar)
2696 call s:setup_commands(a:cchar)
2697
2698 Xgetexpr "F1:10:1:Line1\nF2:20:2:Line2\nF3:30:3:Line3"
2699 Xgetexpr "F4:10:1:Line1\nF5:20:2:Line2\nF6:30:3:Line3"
2700 Xgetexpr "F7:10:1:Line1\nF8:20:2:Line2\nF9:30:3:Line3"
2701
2702 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 2})
2703 call g:Xsetlist([], 'a', {'nr' : 2, 'idx' : 2})
2704 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 3})
2705 Xolder 2
2706 Xopen
2707 call assert_equal(3, line('.'))
2708 Xnewer
2709 call assert_equal(2, line('.'))
2710 Xnewer
2711 call assert_equal(2, line('.'))
2712 " Update the current index with the quickfix window open
2713 wincmd w
2714 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 3})
2715 Xopen
2716 call assert_equal(3, line('.'))
2717 Xclose
2718
2719 " Set the current index to the last entry
2720 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : '$'})
2721 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2722 " A large value should set the index to the last index
2723 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 1})
2724 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 999})
2725 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2726 " Invalid index values
2727 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : -1})
2728 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2729 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 0})
2730 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2731 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 'xx'})
2732 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2733 call assert_fails("call g:Xsetlist([], 'a', {'nr':1, 'idx':[]})", 'E745:')
2734
2735 call g:Xsetlist([], 'f')
2736 new | only
2737endfunc
2738
2739func Test_setqfidx()
2740 call Xtest_setqfidx('c')
2741 call Xtest_setqfidx('l')
2742endfunc
2743
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002744" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002745func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002746 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002747endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002748
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002749func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002750 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
2751 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
2752
2753 let g:acmds = []
2754 cexpr "F1:10:Line 10"
2755 caddexpr "F1:20:Line 20"
2756 cgetexpr "F1:30:Line 30"
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002757 cexpr ""
2758 caddexpr ""
2759 cgetexpr ""
2760 silent! cexpr non_existing_func()
2761 silent! caddexpr non_existing_func()
2762 silent! cgetexpr non_existing_func()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002763 let l = ['precexpr',
2764 \ 'postcexpr',
2765 \ 'precaddexpr',
2766 \ 'postcaddexpr',
2767 \ 'precgetexpr',
2768 \ 'postcgetexpr',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002769 \ 'precexpr',
2770 \ 'postcexpr',
2771 \ 'precaddexpr',
2772 \ 'postcaddexpr',
2773 \ 'precgetexpr',
2774 \ 'postcgetexpr',
2775 \ 'precexpr',
2776 \ 'precaddexpr',
2777 \ 'precgetexpr']
2778 call assert_equal(l, g:acmds)
2779
2780 let g:acmds = []
2781 enew! | call append(0, "F2:10:Line 10")
2782 cbuffer!
2783 enew! | call append(0, "F2:20:Line 20")
2784 cgetbuffer
2785 enew! | call append(0, "F2:30:Line 30")
2786 caddbuffer
2787 new
2788 let bnum = bufnr('%')
2789 bunload
2790 exe 'silent! cbuffer! ' . bnum
2791 exe 'silent! cgetbuffer ' . bnum
2792 exe 'silent! caddbuffer ' . bnum
2793 enew!
2794 let l = ['precbuffer',
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002795 \ 'postcbuffer',
2796 \ 'precgetbuffer',
2797 \ 'postcgetbuffer',
2798 \ 'precaddbuffer',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002799 \ 'postcaddbuffer',
2800 \ 'precbuffer',
2801 \ 'precgetbuffer',
2802 \ 'precaddbuffer']
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002803 call assert_equal(l, g:acmds)
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002804
2805 call writefile(['Xtest:1:Line1'], 'Xtest')
2806 call writefile([], 'Xempty')
2807 let g:acmds = []
2808 cfile Xtest
2809 caddfile Xtest
2810 cgetfile Xtest
2811 cfile Xempty
2812 caddfile Xempty
2813 cgetfile Xempty
2814 silent! cfile do_not_exist
2815 silent! caddfile do_not_exist
2816 silent! cgetfile do_not_exist
2817 let l = ['precfile',
2818 \ 'postcfile',
2819 \ 'precaddfile',
2820 \ 'postcaddfile',
2821 \ 'precgetfile',
2822 \ 'postcgetfile',
2823 \ 'precfile',
2824 \ 'postcfile',
2825 \ 'precaddfile',
2826 \ 'postcaddfile',
2827 \ 'precgetfile',
2828 \ 'postcgetfile',
2829 \ 'precfile',
2830 \ 'postcfile',
2831 \ 'precaddfile',
2832 \ 'postcaddfile',
2833 \ 'precgetfile',
2834 \ 'postcgetfile']
2835 call assert_equal(l, g:acmds)
2836
2837 let g:acmds = []
2838 helpgrep quickfix
2839 silent! helpgrep non_existing_help_topic
2840 vimgrep test Xtest
2841 vimgrepadd test Xtest
2842 silent! vimgrep non_existing_test Xtest
2843 silent! vimgrepadd non_existing_test Xtest
2844 set makeprg=
2845 silent! make
2846 set makeprg&
2847 let l = ['prehelpgrep',
2848 \ 'posthelpgrep',
2849 \ 'prehelpgrep',
2850 \ 'posthelpgrep',
2851 \ 'previmgrep',
2852 \ 'postvimgrep',
2853 \ 'previmgrepadd',
2854 \ 'postvimgrepadd',
2855 \ 'previmgrep',
2856 \ 'postvimgrep',
2857 \ 'previmgrepadd',
2858 \ 'postvimgrepadd',
2859 \ 'premake',
2860 \ 'postmake']
2861 call assert_equal(l, g:acmds)
2862
2863 if has('unix')
2864 " Run this test only on Unix-like systems. The grepprg may not be set on
2865 " non-Unix systems.
2866 " The following lines are used for the grep test. Don't remove.
2867 " Grep_Autocmd_Text: Match 1
2868 " GrepAdd_Autocmd_Text: Match 2
2869 let g:acmds = []
2870 silent grep Grep_Autocmd_Text test_quickfix.vim
2871 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2872 silent grep abc123def Xtest
2873 silent grepadd abc123def Xtest
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002874 set grepprg=internal
2875 silent grep Grep_Autocmd_Text test_quickfix.vim
2876 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2877 silent lgrep Grep_Autocmd_Text test_quickfix.vim
2878 silent lgrepadd GrepAdd_Autocmd_Text test_quickfix.vim
2879 set grepprg&vim
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002880 let l = ['pregrep',
2881 \ 'postgrep',
2882 \ 'pregrepadd',
2883 \ 'postgrepadd',
2884 \ 'pregrep',
2885 \ 'postgrep',
2886 \ 'pregrepadd',
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002887 \ 'postgrepadd',
2888 \ 'pregrep',
2889 \ 'postgrep',
2890 \ 'pregrepadd',
2891 \ 'postgrepadd',
2892 \ 'prelgrep',
2893 \ 'postlgrep',
2894 \ 'prelgrepadd',
2895 \ 'postlgrepadd']
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002896 call assert_equal(l, g:acmds)
2897 endif
2898
2899 call delete('Xtest')
2900 call delete('Xempty')
Bram Moolenaarb254af32017-12-18 19:48:58 +01002901 au! QuickFixCmdPre
2902 au! QuickFixCmdPost
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002903endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01002904
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002905func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01002906 set efm=%m
2907 lgetexpr '?'
2908
2909 try
2910 call DoesNotExit()
2911 catch
2912 lgetexpr '1'
2913 finally
2914 lgetexpr '1'
2915 endtry
2916
2917 call assert_equal('1', getloclist(0)[0].text)
2918
2919 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002920endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002921
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002922func Test_caddbuffer_wrong()
2923 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002924 let save_efm = &efm
2925 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
2926 cgetexpr ['WWWW', 'EEEE', 'CCCC']
2927 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002928 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002929 bwipe!
2930endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002931
2932func Test_caddexpr_wrong()
2933 " This used to cause a memory access in freed memory.
2934 cbuffer
2935 cbuffer
2936 copen
2937 let save_efm = &efm
2938 set efm=%
2939 call assert_fails('caddexpr ""', 'E376:')
2940 let &efm = save_efm
2941endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01002942
2943func Test_dirstack_cleanup()
2944 " This used to cause a memory access in freed memory.
2945 let save_efm = &efm
2946 lexpr '0'
2947 lopen
2948 fun X(c)
2949 let save_efm=&efm
2950 set efm=%D%f
2951 if a:c == 'c'
2952 caddexpr '::'
2953 else
2954 laddexpr ':0:0'
2955 endif
2956 let &efm=save_efm
2957 endfun
2958 call X('c')
2959 call X('l')
2960 call setqflist([], 'r')
2961 caddbuffer
2962 let &efm = save_efm
2963endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002964
2965" Tests for jumping to entries from the location list window and quickfix
2966" window
2967func Test_cwindow_jump()
2968 set efm=%f%%%l%%%m
2969 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2970 lopen | only
2971 lfirst
2972 call assert_true(winnr('$') == 2)
2973 call assert_true(winnr() == 1)
2974 " Location list for the new window should be set
2975 call assert_true(getloclist(0)[2].text == 'Line 30')
2976
2977 " Open a scratch buffer
2978 " Open a new window and create a location list
2979 " Open the location list window and close the other window
2980 " Jump to an entry.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002981 " Should create a new window and jump to the entry. The scratch buffer
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002982 " should not be used.
2983 enew | only
2984 set buftype=nofile
2985 below new
2986 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2987 lopen
2988 2wincmd c
2989 lnext
2990 call assert_true(winnr('$') == 3)
2991 call assert_true(winnr() == 2)
2992
2993 " Open two windows with two different location lists
2994 " Open the location list window and close the previous window
2995 " Jump to an entry in the location list window
2996 " Should open the file in the first window and not set the location list.
2997 enew | only
2998 lgetexpr ["F1%5%Line 5"]
2999 below new
3000 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3001 lopen
3002 2wincmd c
3003 lnext
3004 call assert_true(winnr() == 1)
3005 call assert_true(getloclist(0)[0].text == 'Line 5')
3006
3007 enew | only
3008 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3009 copen
3010 cnext
3011 call assert_true(winnr('$') == 2)
3012 call assert_true(winnr() == 1)
3013
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003014 " open the quickfix buffer in two windows and jump to an entry. Should open
3015 " the file in the first quickfix window.
3016 enew | only
3017 copen
3018 let bnum = bufnr('')
3019 exe 'sbuffer ' . bnum
3020 wincmd b
3021 cfirst
3022 call assert_equal(2, winnr())
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003023 call assert_equal('F1', @%)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003024 enew | only
3025 exe 'sb' bnum
3026 exe 'botright sb' bnum
3027 wincmd t
3028 clast
3029 call assert_equal(2, winnr())
3030 call assert_equal('quickfix', getwinvar(1, '&buftype'))
3031 call assert_equal('quickfix', getwinvar(3, '&buftype'))
3032
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003033 " Jumping to a file from the location list window should find a usable
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01003034 " window by wrapping around the window list.
3035 enew | only
3036 call setloclist(0, [], 'f')
3037 new | new
3038 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
3039 lopen
3040 1close
3041 call assert_equal(0, getloclist(3, {'id' : 0}).id)
3042 lnext
3043 call assert_equal(3, winnr())
3044 call assert_equal(getloclist(1, {'id' : 0}).id, getloclist(3, {'id' : 0}).id)
3045
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01003046 enew | only
3047 set efm&vim
3048endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01003049
Bram Moolenaare00fdf32019-09-15 19:09:42 +02003050func Test_cwindow_highlight()
3051 CheckScreendump
3052
3053 let lines =<< trim END
Bram Moolenaare00fdf32019-09-15 19:09:42 +02003054 call setline(1, ['some', 'text', 'with', 'matches'])
3055 write XCwindow
3056 vimgrep e XCwindow
3057 redraw
3058 cwindow 4
3059 END
3060 call writefile(lines, 'XtestCwindow')
3061 let buf = RunVimInTerminal('-S XtestCwindow', #{rows: 12})
3062 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_1', {})
3063
3064 call term_sendkeys(buf, ":cnext\<CR>")
3065 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_2', {})
3066
3067 " clean up
3068 call StopVimInTerminal(buf)
3069 call delete('XtestCwindow')
3070 call delete('XCwindow')
3071endfunc
3072
Bram Moolenaaree85df32017-03-19 14:19:50 +01003073func XvimgrepTests(cchar)
3074 call s:setup_commands(a:cchar)
3075
3076 call writefile(['Editor:VIM vim',
3077 \ 'Editor:Emacs EmAcS',
3078 \ 'Editor:Notepad NOTEPAD'], 'Xtestfile1')
Dominique Pelle923dce22021-11-21 11:36:04 +00003079 call writefile(['Linux', 'macOS', 'MS-Windows'], 'Xtestfile2')
Bram Moolenaaree85df32017-03-19 14:19:50 +01003080
3081 " Error cases
3082 call assert_fails('Xvimgrep /abc *', 'E682:')
3083
3084 let @/=''
3085 call assert_fails('Xvimgrep // *', 'E35:')
3086
3087 call assert_fails('Xvimgrep abc', 'E683:')
3088 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
3089 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
3090
3091 Xexpr ""
3092 Xvimgrepadd Notepad Xtestfile1
Dominique Pelle923dce22021-11-21 11:36:04 +00003093 Xvimgrepadd macOS Xtestfile2
Bram Moolenaaree85df32017-03-19 14:19:50 +01003094 let l = g:Xgetlist()
3095 call assert_equal(2, len(l))
3096 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
3097
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003098 10Xvimgrep #\cvim#g Xtestfile?
Bram Moolenaaree85df32017-03-19 14:19:50 +01003099 let l = g:Xgetlist()
3100 call assert_equal(2, len(l))
3101 call assert_equal(8, l[0].col)
thinca6864efa2021-06-19 20:45:20 +02003102 call assert_equal(11, l[0].end_col)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003103 call assert_equal(12, l[1].col)
thinca6864efa2021-06-19 20:45:20 +02003104 call assert_equal(15, l[1].end_col)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003105
3106 1Xvimgrep ?Editor? Xtestfile*
3107 let l = g:Xgetlist()
3108 call assert_equal(1, len(l))
3109 call assert_equal('Editor:VIM vim', l[0].text)
3110
3111 edit +3 Xtestfile2
3112 Xvimgrep +\cemacs+j Xtestfile1
3113 let l = g:Xgetlist()
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003114 call assert_equal('Xtestfile2', @%)
Bram Moolenaaree85df32017-03-19 14:19:50 +01003115 call assert_equal('Editor:Emacs EmAcS', l[0].text)
3116
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02003117 " Test for unloading a buffer after vimgrep searched the buffer
3118 %bwipe
3119 Xvimgrep /Editor/j Xtestfile*
3120 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
3121 call assert_equal([], getbufinfo('Xtestfile2'))
3122
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00003123 " Test for opening the dummy buffer used by vimgrep in a window. The new
3124 " window should be closed
3125 %bw!
3126 augroup QF_Test
3127 au!
3128 autocmd BufReadPre * exe "sb " .. expand("<abuf>")
3129 augroup END
3130 call assert_fails("Xvimgrep /sublime/ Xtestfile1", 'E480:')
3131 call assert_equal(1, winnr('$'))
3132 augroup QF_Test
3133 au!
3134 augroup END
3135
Bram Moolenaaree85df32017-03-19 14:19:50 +01003136 call delete('Xtestfile1')
3137 call delete('Xtestfile2')
3138endfunc
3139
3140" Tests for the :vimgrep command
3141func Test_vimgrep()
3142 call XvimgrepTests('c')
3143 call XvimgrepTests('l')
3144endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003145
Bram Moolenaarf8c6a172021-01-30 18:09:06 +01003146func Test_vimgrep_wildcards_expanded_once()
3147 new X[id-01] file.txt
3148 call setline(1, 'some text to search for')
3149 vimgrep text %
3150 bwipe!
3151endfunc
3152
Bram Moolenaar1c299432018-10-28 14:36:09 +01003153" Test for incsearch highlighting of the :vimgrep pattern
3154" This test used to cause "E315: ml_get: invalid lnum" errors.
3155func Test_vimgrep_incsearch()
3156 enew
3157 set incsearch
3158 call test_override("char_avail", 1)
3159
3160 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
3161 let l = getqflist()
3162 call assert_equal(2, len(l))
3163
3164 call test_override("ALL", 0)
3165 set noincsearch
3166endfunc
3167
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01003168" Test vimgrep with the last search pattern not set
3169func Test_vimgrep_with_no_last_search_pat()
3170 let lines =<< trim [SCRIPT]
3171 call assert_fails('vimgrep // *', 'E35:')
3172 call writefile(v:errors, 'Xresult')
3173 qall!
3174 [SCRIPT]
3175 call writefile(lines, 'Xscript')
3176 if RunVim([], [], '--clean -S Xscript')
3177 call assert_equal([], readfile('Xresult'))
3178 endif
3179 call delete('Xscript')
3180 call delete('Xresult')
3181endfunc
3182
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02003183" Test vimgrep without swap file
3184func Test_vimgrep_without_swap_file()
3185 let lines =<< trim [SCRIPT]
3186 vimgrep grep test_c*
3187 call writefile(['done'], 'Xresult')
3188 qall!
3189 [SCRIPT]
3190 call writefile(lines, 'Xscript')
3191 if RunVim([], [], '--clean -n -S Xscript Xscript')
3192 call assert_equal(['done'], readfile('Xresult'))
3193 endif
3194 call delete('Xscript')
3195 call delete('Xresult')
3196endfunc
3197
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02003198func Test_vimgrep_existing_swapfile()
3199 call writefile(['match apple with apple'], 'Xapple')
3200 call writefile(['swapfile'], '.Xapple.swp')
3201 let g:foundSwap = 0
3202 let g:ignoreSwapExists = 1
3203 augroup grep
3204 au SwapExists * let foundSwap = 1 | let v:swapchoice = 'e'
3205 augroup END
3206 vimgrep apple Xapple
3207 call assert_equal(1, g:foundSwap)
3208 call assert_match('.Xapple.swo', swapname(''))
3209
3210 call delete('Xapple')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003211 call delete('.Xapple.swp')
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02003212 augroup grep
3213 au! SwapExists
3214 augroup END
3215 unlet g:ignoreSwapExists
3216endfunc
3217
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003218func XfreeTests(cchar)
3219 call s:setup_commands(a:cchar)
3220
3221 enew | only
3222
3223 " Deleting the quickfix stack should work even When the current list is
3224 " somewhere in the middle of the stack
3225 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3226 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3227 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3228 Xolder
3229 call g:Xsetlist([], 'f')
3230 call assert_equal(0, len(g:Xgetlist()))
3231
3232 " After deleting the stack, adding a new list should create a stack with a
3233 " single list.
3234 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3235 call assert_equal(1, g:Xgetlist({'all':1}).nr)
3236
3237 " Deleting the stack from a quickfix window should update/clear the
3238 " quickfix/location list window.
3239 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3240 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3241 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3242 Xolder
3243 Xwindow
3244 call g:Xsetlist([], 'f')
3245 call assert_equal(2, winnr('$'))
3246 call assert_equal(1, line('$'))
3247 Xclose
3248
3249 " Deleting the stack from a non-quickfix window should update/clear the
3250 " quickfix/location list window.
3251 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3252 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
3253 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
3254 Xolder
3255 Xwindow
3256 wincmd p
3257 call g:Xsetlist([], 'f')
3258 call assert_equal(0, len(g:Xgetlist()))
3259 wincmd p
3260 call assert_equal(2, winnr('$'))
3261 call assert_equal(1, line('$'))
3262
3263 " After deleting the location list stack, if the location list window is
3264 " opened, then a new location list should be created. So opening the
3265 " location list window again should not create a new window.
3266 if a:cchar == 'l'
3267 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
3268 wincmd p
3269 lopen
3270 call assert_equal(2, winnr('$'))
3271 endif
3272 Xclose
3273endfunc
3274
Bram Moolenaar74240d32017-12-10 15:26:15 +01003275" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02003276func Test_qf_free()
3277 call XfreeTests('c')
3278 call XfreeTests('l')
3279endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02003280
3281" Test for buffer overflow when parsing lines and adding new entries to
3282" the quickfix list.
3283func Test_bufoverflow()
3284 set efm=%f:%l:%m
3285 cgetexpr ['File1:100:' . repeat('x', 1025)]
3286
3287 set efm=%+GCompiler:\ %.%#,%f:%l:%m
3288 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
3289
3290 set efm=%DEntering\ directory\ %f,%f:%l:%m
3291 cgetexpr ['Entering directory ' . repeat('a', 1006),
3292 \ 'File1:10:Hello World']
3293 set efm&vim
3294endfunc
3295
Bram Moolenaar875feea2017-06-11 16:07:51 +02003296" Tests for getting the quickfix stack size
3297func XsizeTests(cchar)
3298 call s:setup_commands(a:cchar)
3299
3300 call g:Xsetlist([], 'f')
3301 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003302 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
3303 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02003304
3305 Xexpr "File1:10:Line1"
3306 Xexpr "File2:20:Line2"
3307 Xexpr "File3:30:Line3"
3308 Xolder | Xolder
3309 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
3310 call g:Xsetlist([], 'f')
3311
3312 Xexpr "File1:10:Line1"
3313 Xexpr "File2:20:Line2"
3314 Xexpr "File3:30:Line3"
3315 Xolder | Xolder
3316 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
3317 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
3318endfunc
3319
3320func Test_Qf_Size()
3321 call XsizeTests('c')
3322 call XsizeTests('l')
3323endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02003324
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003325func Test_cclose_from_copen()
3326 augroup QF_Test
3327 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003328 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003329 augroup END
3330 copen
3331 augroup QF_Test
3332 au!
3333 augroup END
3334 augroup! QF_Test
3335endfunc
3336
Bram Moolenaar18141832017-06-25 21:17:25 +02003337func Test_cclose_in_autocmd()
3338 " Problem is only triggered if "starting" is zero, so that the OptionsSet
3339 " event will be triggered.
3340 call test_override('starting', 1)
3341 augroup QF_Test
3342 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003343 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar18141832017-06-25 21:17:25 +02003344 augroup END
3345 copen
3346 augroup QF_Test
3347 au!
3348 augroup END
3349 augroup! QF_Test
3350 call test_override('starting', 0)
3351endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003352
Bram Moolenaar379fb762018-08-30 15:58:28 +02003353" Check that ":file" without an argument is possible even when "curbuf_lock"
3354" is set.
3355func Test_file_from_copen()
3356 " Works without argument.
3357 augroup QF_Test
3358 au!
3359 au FileType qf file
3360 augroup END
3361 copen
3362
3363 augroup QF_Test
3364 au!
3365 augroup END
3366 cclose
3367
3368 " Fails with argument.
3369 augroup QF_Test
3370 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003371 au FileType qf call assert_fails(':file foo', 'E788:')
Bram Moolenaar379fb762018-08-30 15:58:28 +02003372 augroup END
3373 copen
3374 augroup QF_Test
3375 au!
3376 augroup END
3377 cclose
3378
3379 augroup! QF_Test
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02003380endfunc
Bram Moolenaar379fb762018-08-30 15:58:28 +02003381
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003382func Test_resize_from_copen()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003383 augroup QF_Test
3384 au!
3385 au FileType qf resize 5
3386 augroup END
3387 try
3388 " This should succeed without any exception. No other buffers are
3389 " involved in the autocmd.
3390 copen
3391 finally
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003392 augroup QF_Test
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003393 au!
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003394 augroup END
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003395 augroup! QF_Test
3396 endtry
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003397endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02003398
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003399func Test_vimgrep_with_textlock()
3400 new
3401
3402 " Simple way to execute something with "textwinlock" set.
3403 " Check that vimgrep without jumping can be executed.
3404 au InsertCharPre * vimgrep /RunTheTest/j runtest.vim
3405 normal ax
3406 let qflist = getqflist()
3407 call assert_true(len(qflist) > 0)
3408 call assert_match('RunTheTest', qflist[0].text)
3409 call setqflist([], 'r')
3410 au! InsertCharPre
3411
3412 " Check that vimgrepadd without jumping can be executed.
3413 au InsertCharPre * vimgrepadd /RunTheTest/j runtest.vim
3414 normal ax
3415 let qflist = getqflist()
3416 call assert_true(len(qflist) > 0)
3417 call assert_match('RunTheTest', qflist[0].text)
3418 call setqflist([], 'r')
3419 au! InsertCharPre
3420
3421 " Check that lvimgrep without jumping can be executed.
3422 au InsertCharPre * lvimgrep /RunTheTest/j runtest.vim
3423 normal ax
3424 let qflist = getloclist(0)
3425 call assert_true(len(qflist) > 0)
3426 call assert_match('RunTheTest', qflist[0].text)
3427 call setloclist(0, [], 'r')
3428 au! InsertCharPre
3429
3430 " Check that lvimgrepadd without jumping can be executed.
3431 au InsertCharPre * lvimgrepadd /RunTheTest/j runtest.vim
3432 normal ax
3433 let qflist = getloclist(0)
3434 call assert_true(len(qflist) > 0)
3435 call assert_match('RunTheTest', qflist[0].text)
3436 call setloclist(0, [], 'r')
3437 au! InsertCharPre
3438
3439 " trying to jump will give an error
3440 au InsertCharPre * vimgrep /RunTheTest/ runtest.vim
3441 call assert_fails('normal ax', 'E565:')
3442 au! InsertCharPre
3443
3444 au InsertCharPre * vimgrepadd /RunTheTest/ runtest.vim
3445 call assert_fails('normal ax', 'E565:')
3446 au! InsertCharPre
3447
3448 au InsertCharPre * lvimgrep /RunTheTest/ runtest.vim
3449 call assert_fails('normal ax', 'E565:')
3450 au! InsertCharPre
3451
3452 au InsertCharPre * lvimgrepadd /RunTheTest/ runtest.vim
3453 call assert_fails('normal ax', 'E565:')
3454 au! InsertCharPre
3455
3456 bwipe!
3457endfunc
3458
Bram Moolenaara8788f42017-07-19 17:06:20 +02003459" Tests for the quickfix buffer b:changedtick variable
3460func Xchangedtick_tests(cchar)
3461 call s:setup_commands(a:cchar)
3462
3463 new | only
3464
3465 Xexpr "" | Xexpr "" | Xexpr ""
3466
3467 Xopen
3468 Xolder
3469 Xolder
3470 Xaddexpr "F1:10:Line10"
3471 Xaddexpr "F2:20:Line20"
3472 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
3473 call g:Xsetlist([], 'f')
3474 call assert_equal(8, getbufvar('%', 'changedtick'))
3475 Xclose
3476endfunc
3477
3478func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02003479 call Xchangedtick_tests('c')
3480 call Xchangedtick_tests('l')
3481endfunc
3482
3483" Tests for parsing an expression using setqflist()
3484func Xsetexpr_tests(cchar)
3485 call s:setup_commands(a:cchar)
3486
3487 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003488 call g:Xsetlist([], ' ', {'lines' : t})
3489 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003490
3491 let l = g:Xgetlist()
3492 call assert_equal(3, len(l))
3493 call assert_equal(20, l[1].lnum)
3494 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003495 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003496 let l = g:Xgetlist()
3497 call assert_equal(1, len(l))
3498 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003499 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
3500 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003501
3502 call g:Xsetlist([], 'f')
3503 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003504 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
3505 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
3506 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
3507 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003508 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
3509 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02003510
3511 " Adding entries using a custom efm
3512 set efm&
3513 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
3514 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
3515 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3516 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
3517 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
3518 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3519 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
3520 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003521endfunc
3522
3523func Test_setexpr()
3524 call Xsetexpr_tests('c')
3525 call Xsetexpr_tests('l')
3526endfunc
3527
3528" Tests for per quickfix/location list directory stack
3529func Xmultidirstack_tests(cchar)
3530 call s:setup_commands(a:cchar)
3531
3532 call g:Xsetlist([], 'f')
3533 Xexpr "" | Xexpr ""
3534
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003535 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
3536 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
3537 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
3538 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003539
3540 let l1 = g:Xgetlist({'nr':1, 'items':1})
3541 let l2 = g:Xgetlist({'nr':2, 'items':1})
3542 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
3543 call assert_equal(3, l1.items[1].lnum)
3544 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
3545 call assert_equal(5, l2.items[1].lnum)
3546endfunc
3547
3548func Test_multidirstack()
3549 call mkdir('Xone/a', 'p')
3550 call mkdir('Xtwo/a', 'p')
3551 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3552 call writefile(lines, 'Xone/a/one.txt')
3553 call writefile(lines, 'Xtwo/a/two.txt')
3554 let save_efm = &efm
3555 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
3556
3557 call Xmultidirstack_tests('c')
3558 call Xmultidirstack_tests('l')
3559
3560 let &efm = save_efm
3561 call delete('Xone', 'rf')
3562 call delete('Xtwo', 'rf')
3563endfunc
3564
3565" Tests for per quickfix/location list file stack
3566func Xmultifilestack_tests(cchar)
3567 call s:setup_commands(a:cchar)
3568
3569 call g:Xsetlist([], 'f')
3570 Xexpr "" | Xexpr ""
3571
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003572 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
3573 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
3574 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
3575 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003576
3577 let l1 = g:Xgetlist({'nr':1, 'items':1})
3578 let l2 = g:Xgetlist({'nr':2, 'items':1})
3579 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
3580 call assert_equal(3, l1.items[1].lnum)
3581 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
3582 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02003583
3584 " Test for start of a new error line in the same line where a previous
3585 " error line ends with a file stack.
3586 let efm_val = 'Error\ l%l\ in\ %f,'
3587 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
3588 let l = g:Xgetlist({'lines' : [
3589 \ '(one.txt',
3590 \ 'Error l4 in one.txt',
3591 \ ') (two.txt',
3592 \ 'Error l6 in two.txt',
3593 \ ')',
3594 \ 'Error l8 in one.txt'
3595 \ ], 'efm' : efm_val})
3596 call assert_equal(3, len(l.items))
3597 call assert_equal('one.txt', bufname(l.items[0].bufnr))
3598 call assert_equal(4, l.items[0].lnum)
3599 call assert_equal('one.txt', l.items[0].text)
3600 call assert_equal('two.txt', bufname(l.items[1].bufnr))
3601 call assert_equal(6, l.items[1].lnum)
3602 call assert_equal('two.txt', l.items[1].text)
3603 call assert_equal('one.txt', bufname(l.items[2].bufnr))
3604 call assert_equal(8, l.items[2].lnum)
3605 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02003606endfunc
3607
3608func Test_multifilestack()
3609 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3610 call writefile(lines, 'one.txt')
3611 call writefile(lines, 'two.txt')
3612 let save_efm = &efm
3613 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
3614
3615 call Xmultifilestack_tests('c')
3616 call Xmultifilestack_tests('l')
3617
3618 let &efm = save_efm
3619 call delete('one.txt')
3620 call delete('two.txt')
3621endfunc
3622
3623" Tests for per buffer 'efm' setting
3624func Test_perbuf_efm()
3625 call writefile(["File1-10-Line10"], 'one.txt')
3626 call writefile(["File2#20#Line20"], 'two.txt')
3627 set efm=%f#%l#%m
3628 new | only
3629 new
3630 setlocal efm=%f-%l-%m
3631 cfile one.txt
3632 wincmd w
3633 caddfile two.txt
3634
3635 let l = getqflist()
3636 call assert_equal(10, l[0].lnum)
3637 call assert_equal('Line20', l[1].text)
3638
3639 set efm&
3640 new | only
3641 call delete('one.txt')
3642 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02003643endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003644
3645" Open multiple help windows using ":lhelpgrep
3646" This test used to crash Vim
3647func Test_Multi_LL_Help()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003648 new | only
3649 lhelpgrep window
3650 lopen
3651 e#
3652 lhelpgrep buffer
3653 call assert_equal(3, winnr('$'))
3654 call assert_true(len(getloclist(1)) != 0)
3655 call assert_true(len(getloclist(2)) != 0)
3656 new | only
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003657endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02003658
3659" Tests for adding new quickfix lists using setqflist()
3660func XaddQf_tests(cchar)
3661 call s:setup_commands(a:cchar)
3662
3663 " Create a new list using ' ' for action
3664 call g:Xsetlist([], 'f')
3665 call g:Xsetlist([], ' ', {'title' : 'Test1'})
3666 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3667 call assert_equal(1, l.nr)
3668 call assert_equal('Test1', l.title)
3669
3670 " Create a new list using ' ' for action and '$' for 'nr'
3671 call g:Xsetlist([], 'f')
3672 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
3673 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3674 call assert_equal(1, l.nr)
3675 call assert_equal('Test2', l.title)
3676
3677 " Create a new list using 'a' for action
3678 call g:Xsetlist([], 'f')
3679 call g:Xsetlist([], 'a', {'title' : 'Test3'})
3680 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3681 call assert_equal(1, l.nr)
3682 call assert_equal('Test3', l.title)
3683
3684 " Create a new list using 'a' for action and '$' for 'nr'
3685 call g:Xsetlist([], 'f')
3686 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
3687 call g:Xsetlist([], 'a', {'title' : 'Test4'})
3688 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3689 call assert_equal(1, l.nr)
3690 call assert_equal('Test4', l.title)
3691
3692 " Adding a quickfix list should remove all the lists following the current
3693 " list.
3694 Xexpr "" | Xexpr "" | Xexpr ""
3695 silent! 10Xolder
3696 call g:Xsetlist([], ' ', {'title' : 'Test5'})
3697 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3698 call assert_equal(2, l.nr)
3699 call assert_equal('Test5', l.title)
3700
3701 " Add a quickfix list using '$' as the list number.
3702 let lastqf = g:Xgetlist({'nr':'$'}).nr
3703 silent! 99Xolder
3704 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
3705 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3706 call assert_equal(lastqf + 1, l.nr)
3707 call assert_equal('Test6', l.title)
3708
3709 " Add a quickfix list using 'nr' set to one more than the quickfix
3710 " list size.
3711 let lastqf = g:Xgetlist({'nr':'$'}).nr
3712 silent! 99Xolder
3713 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
3714 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3715 call assert_equal(lastqf + 1, l.nr)
3716 call assert_equal('Test7', l.title)
3717
3718 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
3719 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
3720 silent! 99Xolder
3721 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
3722 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3723 call assert_equal(10, l.nr)
3724 call assert_equal('Test8', l.title)
3725
3726 " Add a quickfix list using 'nr' set to a value greater than 10
3727 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
3728
3729 " Try adding a quickfix list with 'nr' set to a value greater than the
3730 " quickfix list size but less than 10.
3731 call g:Xsetlist([], 'f')
3732 Xexpr "" | Xexpr "" | Xexpr ""
3733 silent! 99Xolder
3734 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
3735
3736 " Add a quickfix list using 'nr' set to a some string or list
3737 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
3738endfunc
3739
3740func Test_add_qf()
3741 call XaddQf_tests('c')
3742 call XaddQf_tests('l')
3743endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003744
3745" Test for getting the quickfix list items from some text without modifying
3746" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003747func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003748 call s:setup_commands(a:cchar)
3749 call g:Xsetlist([], 'f')
3750
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003751 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003752 call assert_equal(2, len(l))
3753 call assert_equal(30, l[1].lnum)
3754
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003755 call assert_equal({}, g:Xgetlist({'lines' : 10}))
3756 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
3757 call assert_equal([], g:Xgetlist({'lines' : []}).items)
3758 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003759
Bram Moolenaar36538222017-09-02 19:51:44 +02003760 " Parse text using a custom efm
3761 set efm&
3762 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
3763 call assert_equal('Line30', l[0].text)
3764 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
3765 call assert_equal('File3:30:Line30', l[0].text)
3766 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
3767 call assert_equal({}, l)
3768 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
3769 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
3770
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003771 " Make sure that the quickfix stack is not modified
3772 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
3773endfunc
3774
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003775func Test_get_list_from_lines()
3776 call XgetListFromLines('c')
3777 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003778endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003779
3780" Tests for the quickfix list id
3781func Xqfid_tests(cchar)
3782 call s:setup_commands(a:cchar)
3783
3784 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003785 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003786 Xexpr ''
3787 let start_id = g:Xgetlist({'id' : 0}).id
3788 Xexpr '' | Xexpr ''
3789 Xolder
3790 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
3791 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
3792 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003793 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003794 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003795 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3796 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003797
3798 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3799 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003800 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003801 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3802 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3803 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3804
3805 let qfid = g:Xgetlist({'id':0, 'nr':0})
3806 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003807 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003808endfunc
3809
3810func Test_qf_id()
3811 call Xqfid_tests('c')
3812 call Xqfid_tests('l')
3813endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003814
3815func Xqfjump_tests(cchar)
3816 call s:setup_commands(a:cchar)
3817
3818 call writefile(["Line1\tFoo", "Line2"], 'F1')
3819 call writefile(["Line1\tBar", "Line2"], 'F2')
3820 call writefile(["Line1\tBaz", "Line2"], 'F3')
3821
3822 call g:Xsetlist([], 'f')
3823
3824 " Tests for
3825 " Jumping to a line using a pattern
3826 " Jumping to a column greater than the last column in a line
3827 " Jumping to a line greater than the last line in the file
3828 let l = []
3829 for i in range(1, 7)
3830 call add(l, {})
3831 endfor
3832 let l[0].filename='F1'
3833 let l[0].pattern='Line1'
3834 let l[1].filename='F2'
3835 let l[1].pattern='Line1'
3836 let l[2].filename='F3'
3837 let l[2].pattern='Line1'
3838 let l[3].filename='F3'
3839 let l[3].lnum=1
3840 let l[3].col=9
3841 let l[3].vcol=1
3842 let l[4].filename='F3'
3843 let l[4].lnum=99
3844 let l[5].filename='F3'
3845 let l[5].lnum=1
3846 let l[5].col=99
3847 let l[5].vcol=1
3848 let l[6].filename='F3'
3849 let l[6].pattern='abcxyz'
3850
3851 call g:Xsetlist([], ' ', {'items' : l})
3852 Xopen | only
3853 2Xnext
3854 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003855 call assert_equal('F3', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +01003856 Xnext
3857 call assert_equal(7, col('.'))
3858 Xnext
3859 call assert_equal(2, line('.'))
3860 Xnext
3861 call assert_equal(9, col('.'))
3862 2
3863 Xnext
3864 call assert_equal(2, line('.'))
3865
3866 if a:cchar == 'l'
3867 " When jumping to a location list entry in the location list window and
3868 " no usable windows are available, then a new window should be opened.
3869 enew! | new | only
3870 call g:Xsetlist([], 'f')
3871 setlocal buftype=nofile
3872 new
3873 call g:Xsetlist([], ' ', {'lines' : ['F1:1:1:Line1', 'F1:2:2:Line2', 'F2:1:1:Line1', 'F2:2:2:Line2', 'F3:1:1:Line1', 'F3:2:2:Line2']})
3874 Xopen
3875 let winid = win_getid()
3876 wincmd p
3877 close
3878 call win_gotoid(winid)
3879 Xnext
3880 call assert_equal(3, winnr('$'))
3881 call assert_equal(1, winnr())
3882 call assert_equal(2, line('.'))
3883
3884 " When jumping to an entry in the location list window and the window
3885 " associated with the location list is not present and a window containing
3886 " the file is already present, then that window should be used.
3887 close
3888 belowright new
3889 call g:Xsetlist([], 'f')
3890 edit F3
3891 call win_gotoid(winid)
3892 Xlast
3893 call assert_equal(3, winnr())
3894 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3895 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3896 endif
3897
3898 " Cleanup
3899 enew!
3900 new | only
3901
3902 call delete('F1')
3903 call delete('F2')
3904 call delete('F3')
3905endfunc
3906
3907func Test_qfjump()
3908 call Xqfjump_tests('c')
3909 call Xqfjump_tests('l')
3910endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01003911
3912" Tests for the getqflist() and getloclist() functions when the list is not
3913" present or is empty
3914func Xgetlist_empty_tests(cchar)
3915 call s:setup_commands(a:cchar)
3916
3917 " Empty quickfix stack
3918 call g:Xsetlist([], 'f')
3919 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3920 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
3921 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3922 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3923 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
3924 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3925 call assert_equal('', g:Xgetlist({'title' : 0}).title)
3926 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003927 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003928 if a:cchar == 'c'
3929 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003930 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003931 \ 'title' : '', 'winid' : 0, 'changedtick': 0,
3932 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003933 else
3934 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
3935 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003936 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003937 \ 'qfbufnr' : 0, 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003938 \ g:Xgetlist({'all' : 0}))
3939 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003940
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003941 " Quickfix window with empty stack
3942 silent! Xopen
3943 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003944 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003945 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
3946 Xclose
3947
Bram Moolenaara6d48492017-12-12 22:45:31 +01003948 " Empty quickfix list
3949 Xexpr ""
3950 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3951 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
3952 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3953 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3954 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
3955 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3956 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
3957 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003958 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003959
3960 let qfid = g:Xgetlist({'id' : 0}).id
3961 call g:Xsetlist([], 'f')
3962
3963 " Non-existing quickfix identifier
3964 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
3965 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
3966 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
3967 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
3968 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
3969 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
3970 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
3971 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003972 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003973 if a:cchar == 'c'
3974 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3975 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003976 \ 'qfbufnr' : qfbufnr, 'quickfixtextfunc' : '',
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003977 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
3978 else
3979 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3980 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003981 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
3982 \ 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003983 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
3984 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003985
3986 " Non-existing quickfix list number
3987 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
3988 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
3989 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
3990 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
3991 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
3992 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
3993 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
3994 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003995 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003996 if a:cchar == 'c'
3997 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3998 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003999 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr,
4000 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004001 else
4002 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
4003 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02004004 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
4005 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02004006 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01004007endfunc
4008
4009func Test_getqflist()
4010 call Xgetlist_empty_tests('c')
4011 call Xgetlist_empty_tests('l')
4012endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01004013
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01004014func Test_getqflist_invalid_nr()
4015 " The following commands used to crash Vim
4016 cexpr ""
4017 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
4018
4019 " Cleanup
4020 call setqflist([], 'r')
4021endfunc
4022
Bram Moolenaarb254af32017-12-18 19:48:58 +01004023" Tests for the quickfix/location list changedtick
4024func Xqftick_tests(cchar)
4025 call s:setup_commands(a:cchar)
4026
4027 call g:Xsetlist([], 'f')
4028
4029 Xexpr "F1:10:Line10"
4030 let qfid = g:Xgetlist({'id' : 0}).id
4031 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4032 Xaddexpr "F2:20:Line20\nF2:21:Line21"
4033 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4034 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
4035 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
4036 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
4037 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
4038 call g:Xsetlist([], 'a', {'title' : 'New Title'})
4039 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
4040
4041 enew!
4042 call append(0, ["F5:50:L50", "F6:60:L60"])
4043 Xaddbuffer
4044 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
4045 enew!
4046
4047 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
4048 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
4049 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4050 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
4051 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
4052 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4053 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
4054 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4055 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
4056 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
4057 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4058
4059 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
4060 Xfile Xone
4061 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4062 Xaddfile Xone
4063 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
4064
4065 " Test case for updating a non-current quickfix list
4066 call g:Xsetlist([], 'f')
4067 Xexpr "F1:1:L1"
4068 Xexpr "F2:2:L2"
4069 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
4070 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
4071 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
4072
4073 call delete("Xone")
4074endfunc
4075
4076func Test_qf_tick()
4077 call Xqftick_tests('c')
4078 call Xqftick_tests('l')
4079endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01004080
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02004081" Test helpgrep with lang specifier
4082func Xtest_helpgrep_with_lang_specifier(cchar)
4083 call s:setup_commands(a:cchar)
4084 Xhelpgrep Vim@en
4085 call assert_equal('help', &filetype)
4086 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
4087 new | only
4088endfunc
4089
4090func Test_helpgrep_with_lang_specifier()
4091 call Xtest_helpgrep_with_lang_specifier('c')
4092 call Xtest_helpgrep_with_lang_specifier('l')
4093endfunc
4094
Bram Moolenaar12237442017-12-19 12:38:52 +01004095" The following test used to crash Vim.
4096" Open the location list window and close the regular window associated with
4097" the location list. When the garbage collection runs now, it incorrectly
4098" marks the location list context as not in use and frees the context.
4099func Test_ll_window_ctx()
4100 call setloclist(0, [], 'f')
4101 call setloclist(0, [], 'a', {'context' : []})
4102 lopen | only
4103 call test_garbagecollect_now()
4104 echo getloclist(0, {'context' : 1}).context
4105 enew | only
4106endfunc
4107
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004108" The following test used to crash vim
4109func Test_lfile_crash()
4110 sp Xtest
4111 au QuickFixCmdPre * bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02004112 call assert_fails('lfile', 'E40:')
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01004113 au! QuickFixCmdPre
4114endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01004115
4116" The following test used to crash vim
4117func Test_lbuffer_crash()
4118 sv Xtest
4119 augroup QF_Test
4120 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004121 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bw
Bram Moolenaar3c097222017-12-21 20:54:49 +01004122 augroup END
4123 lbuffer
4124 augroup QF_Test
4125 au!
4126 augroup END
4127endfunc
4128
4129" The following test used to crash vim
4130func Test_lexpr_crash()
4131 augroup QF_Test
4132 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004133 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004134 augroup END
4135 lexpr ""
4136 augroup QF_Test
4137 au!
4138 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004139
Bram Moolenaar3c097222017-12-21 20:54:49 +01004140 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02004141 augroup QF_Test
4142 au!
4143 au BufNew * call setloclist(0, [], 'f')
4144 augroup END
4145 lexpr 'x:1:x'
4146 augroup QF_Test
4147 au!
4148 augroup END
4149
4150 enew | only
4151 lexpr ''
4152 lopen
4153 augroup QF_Test
4154 au!
4155 au FileType * call setloclist(0, [], 'f')
4156 augroup END
4157 lexpr ''
4158 augroup QF_Test
4159 au!
4160 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01004161endfunc
4162
4163" The following test used to crash Vim
4164func Test_lvimgrep_crash()
4165 sv Xtest
4166 augroup QF_Test
4167 au!
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004168 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call setloclist(0, [], 'f')
Bram Moolenaar3c097222017-12-21 20:54:49 +01004169 augroup END
4170 lvimgrep quickfix test_quickfix.vim
4171 augroup QF_Test
4172 au!
4173 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004174
4175 new | only
4176 augroup QF_Test
4177 au!
4178 au BufEnter * call setloclist(0, [], 'r')
4179 augroup END
4180 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
4181 augroup QF_Test
4182 au!
4183 augroup END
4184
Bram Moolenaar3c097222017-12-21 20:54:49 +01004185 enew | only
4186endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01004187
Bram Moolenaar2573af32020-03-14 17:21:34 +01004188func Test_lvimgrep_crash2()
4189 au BufNewFile x sfind
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004190 call assert_fails('lvimgrep x x', 'E471:')
4191 call assert_fails('lvimgrep x x x', 'E471:')
Bram Moolenaar2573af32020-03-14 17:21:34 +01004192
4193 au! BufNewFile
4194endfunc
4195
Bram Moolenaarde046542017-12-26 13:53:11 +01004196" Test for the position of the quickfix and location list window
4197func Test_qfwin_pos()
4198 " Open two windows
4199 new | only
4200 new
4201 cexpr ['F1:10:L10']
4202 copen
4203 " Quickfix window should be the bottom most window
4204 call assert_equal(3, winnr())
4205 close
4206 " Open at the very top
4207 wincmd t
4208 topleft copen
4209 call assert_equal(1, winnr())
4210 close
4211 " open left of the current window
4212 wincmd t
4213 below new
4214 leftabove copen
4215 call assert_equal(2, winnr())
4216 close
4217 " open right of the current window
4218 rightbelow copen
4219 call assert_equal(3, winnr())
4220 close
4221endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02004222
4223" Tests for quickfix/location lists changed by autocommands when
4224" :vimgrep/:lvimgrep commands are running.
4225func Test_vimgrep_autocmd()
4226 call setqflist([], 'f')
4227 call writefile(['stars'], 'Xtest1.txt')
4228 call writefile(['stars'], 'Xtest2.txt')
4229
4230 " Test 1:
4231 " When searching for a pattern using :vimgrep, if the quickfix list is
4232 " changed by an autocmd, the results should be added to the correct quickfix
4233 " list.
4234 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
4235 silent vimgrep stars Xtest*.txt
4236 call assert_equal(1, getqflist({'nr' : 0}).nr)
4237 call assert_equal(3, getqflist({'nr' : '$'}).nr)
4238 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
4239 au! BufRead Xtest2.txt
4240
4241 " Test 2:
4242 " When searching for a pattern using :vimgrep, if the quickfix list is
4243 " freed, then a error should be given.
4244 silent! %bwipe!
4245 call setqflist([], 'f')
4246 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
4247 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
4248 au! BufRead Xtest2.txt
4249
4250 " Test 3:
4251 " When searching for a pattern using :lvimgrep, if the location list is
4252 " freed, then the command should error out.
4253 silent! %bwipe!
4254 let g:save_winid = win_getid()
4255 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
4256 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
4257 au! BufRead Xtest2.txt
4258
4259 call delete('Xtest1.txt')
4260 call delete('Xtest2.txt')
4261 call setqflist([], 'f')
4262endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004263
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01004264" Test for an autocmd changing the current directory when running vimgrep
4265func Xvimgrep_autocmd_cd(cchar)
4266 call s:setup_commands(a:cchar)
4267
4268 %bwipe
4269 let save_cwd = getcwd()
4270
4271 augroup QF_Test
4272 au!
4273 autocmd BufRead * silent cd %:p:h
4274 augroup END
4275
4276 10Xvimgrep /vim/ Xdir/**
4277 let l = g:Xgetlist()
4278 call assert_equal('f1.txt', bufname(l[0].bufnr))
4279 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
4280
4281 augroup QF_Test
4282 au!
4283 augroup END
4284
4285 exe 'cd ' . save_cwd
4286endfunc
4287
4288func Test_vimgrep_autocmd_cd()
4289 call mkdir('Xdir/a', 'p')
4290 call mkdir('Xdir/b', 'p')
4291 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xdir/a/f1.txt')
4292 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xdir/b/f2.txt')
4293 call Xvimgrep_autocmd_cd('c')
4294 call Xvimgrep_autocmd_cd('l')
4295 %bwipe
4296 call delete('Xdir', 'rf')
4297endfunc
4298
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004299" The following test used to crash Vim
4300func Test_lhelpgrep_autocmd()
4301 lhelpgrep quickfix
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004302 augroup QF_Test
4303 au!
4304 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
4305 augroup END
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004306 lhelpgrep buffer
4307 call assert_equal('help', &filetype)
4308 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
4309 lhelpgrep tabpage
4310 call assert_equal('help', &filetype)
4311 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004312 augroup QF_Test
4313 au!
4314 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004315
4316 new | only
4317 augroup QF_Test
4318 au!
4319 au BufEnter * call setqflist([], 'f')
4320 augroup END
4321 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaardbfa7952020-11-02 20:04:22 +01004322 " run the test with a help window already open
4323 help
4324 wincmd w
4325 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004326 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004327 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004328 augroup END
4329
4330 new | only
4331 augroup QF_Test
4332 au!
4333 au BufEnter * call setqflist([], 'r')
4334 augroup END
4335 call assert_fails('helpgrep quickfix', 'E925:')
4336 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004337 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004338 augroup END
4339
4340 new | only
4341 augroup QF_Test
4342 au!
4343 au BufEnter * call setloclist(0, [], 'r')
4344 augroup END
4345 call assert_fails('lhelpgrep quickfix', 'E926:')
4346 augroup QF_Test
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004347 au!
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004348 augroup END
4349
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004350 " Replace the contents of a help window location list when it is still in
4351 " use.
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004352 new | only
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00004353 lhelpgrep quickfix
4354 wincmd w
4355 augroup QF_Test
4356 au!
4357 autocmd WinEnter * call setloclist(0, [], 'r')
4358 augroup END
4359 call assert_fails('lhelpgrep win_getid', 'E926:')
4360 augroup QF_Test
4361 au!
4362 augroup END
4363
4364 %bw!
4365endfunc
4366
4367" The following test used to crash Vim
4368func Test_lhelpgrep_autocmd_free_loclist()
4369 %bw!
4370 lhelpgrep quickfix
4371 wincmd w
4372 augroup QF_Test
4373 au!
4374 autocmd WinEnter * call setloclist(0, [], 'f')
4375 augroup END
4376 lhelpgrep win_getid
4377 wincmd w
4378 wincmd w
4379 wincmd w
4380 augroup QF_Test
4381 au!
4382 augroup END
4383 %bw!
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004384endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02004385
4386" Test for shortening/simplifying the file name when opening the
4387" quickfix window or when displaying the quickfix list
4388func Test_shorten_fname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02004389 CheckUnix
Bram Moolenaara796d462018-05-01 14:30:36 +02004390 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004391 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004392 let fname = getcwd() . '/test_quickfix.vim'
4393 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4394 call assert_equal(fname, bufname('test_quickfix.vim'))
4395 " Opening the quickfix window should simplify the file path
4396 cwindow
4397 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
4398 cclose
4399 %bwipe
Dominique Pelle923dce22021-11-21 11:36:04 +00004400 " Create a quickfix list with an absolute path filename
Bram Moolenaara796d462018-05-01 14:30:36 +02004401 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4402 call assert_equal(fname, bufname('test_quickfix.vim'))
4403 " Displaying the quickfix list should simplify the file path
4404 silent! clist
4405 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004406 " Add a few entries for the same file with different paths and check whether
4407 " the buffer name is shortened
4408 %bwipe
4409 call setqflist([], 'f')
4410 call setqflist([{'filename' : 'test_quickfix.vim', 'lnum' : 10},
4411 \ {'filename' : '../testdir/test_quickfix.vim', 'lnum' : 20},
4412 \ {'filename' : fname, 'lnum' : 30}], ' ')
4413 copen
4414 call assert_equal(['test_quickfix.vim|10| ',
4415 \ 'test_quickfix.vim|20| ',
4416 \ 'test_quickfix.vim|30| '], getline(1, '$'))
4417 cclose
Bram Moolenaara796d462018-05-01 14:30:36 +02004418endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004419
4420" Quickfix title tests
4421" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
4422" Otherwise due to indentation, the title is set with spaces at the beginning
4423" of the command.
4424func Test_qftitle()
4425 call writefile(["F1:1:Line1"], 'Xerr')
4426
4427 " :cexpr
4428 exe "cexpr readfile('Xerr')"
4429 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
4430
4431 " :cgetexpr
4432 exe "cgetexpr readfile('Xerr')"
4433 call assert_equal(":cgetexpr readfile('Xerr')",
4434 \ getqflist({'title' : 1}).title)
4435
4436 " :caddexpr
4437 call setqflist([], 'f')
4438 exe "caddexpr readfile('Xerr')"
4439 call assert_equal(":caddexpr readfile('Xerr')",
4440 \ getqflist({'title' : 1}).title)
4441
4442 " :cbuffer
4443 new Xerr
4444 exe "cbuffer"
4445 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
4446
4447 " :cgetbuffer
4448 edit Xerr
4449 exe "cgetbuffer"
4450 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
4451
4452 " :caddbuffer
4453 call setqflist([], 'f')
4454 edit Xerr
4455 exe "caddbuffer"
4456 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
4457
4458 " :cfile
4459 exe "cfile Xerr"
4460 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
4461
4462 " :cgetfile
4463 exe "cgetfile Xerr"
4464 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
4465
4466 " :caddfile
4467 call setqflist([], 'f')
4468 exe "caddfile Xerr"
4469 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
4470
4471 " :grep
4472 set grepprg=internal
4473 exe "grep F1 Xerr"
4474 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
4475
4476 " :grepadd
4477 call setqflist([], 'f')
4478 exe "grepadd F1 Xerr"
4479 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
4480 set grepprg&vim
4481
4482 " :vimgrep
4483 exe "vimgrep F1 Xerr"
4484 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
4485
4486 " :vimgrepadd
4487 call setqflist([], 'f')
4488 exe "vimgrepadd F1 Xerr"
4489 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
4490
4491 call setqflist(['F1:10:L10'], ' ')
4492 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4493
4494 call setqflist([], 'f')
4495 call setqflist(['F1:10:L10'], 'a')
4496 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4497
4498 call setqflist([], 'f')
4499 call setqflist(['F1:10:L10'], 'r')
4500 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4501
4502 close
4503 call delete('Xerr')
4504
4505 call setqflist([], ' ', {'title' : 'Errors'})
4506 copen
4507 call assert_equal('Errors', w:quickfix_title)
4508 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
4509 call assert_equal('Errors', w:quickfix_title)
4510 cclose
Bram Moolenaar530bed92020-12-16 21:02:56 +01004511
4512 " Switching to another quickfix list in one tab page should update the
4513 " quickfix window title and statusline in all the other tab pages also
4514 call setqflist([], 'f')
4515 %bw!
4516 cgetexpr ['file_one:1:1: error in the first quickfix list']
4517 call setqflist([], 'a', {'title': 'first quickfix list'})
4518 cgetexpr ['file_two:2:1: error in the second quickfix list']
4519 call setqflist([], 'a', {'title': 'second quickfix list'})
4520 copen
4521 wincmd t
4522 tabnew two
4523 copen
4524 wincmd t
4525 colder
4526 call assert_equal('first quickfix list', gettabwinvar(1, 2, 'quickfix_title'))
4527 call assert_equal('first quickfix list', gettabwinvar(2, 2, 'quickfix_title'))
4528 call assert_equal(1, tabpagewinnr(1))
4529 call assert_equal(1, tabpagewinnr(2))
4530 tabnew
4531 call setqflist([], 'a', {'title': 'new quickfix title'})
4532 call assert_equal('new quickfix title', gettabwinvar(1, 2, 'quickfix_title'))
4533 call assert_equal('new quickfix title', gettabwinvar(2, 2, 'quickfix_title'))
4534 %bw!
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004535endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02004536
4537func Test_lbuffer_with_bwipe()
4538 new
4539 new
4540 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004541 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * bwipe
Bram Moolenaar600323b2018-06-16 22:16:47 +02004542 augroup END
4543 lbuffer
4544 augroup nasty
4545 au!
4546 augroup END
4547endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02004548
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004549" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
4550" running
4551func Xexpr_acmd_freelist(cchar)
4552 call s:setup_commands(a:cchar)
4553
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004554 " This was using freed memory (but with what events?)
Bram Moolenaar0366c012018-06-18 20:52:13 +02004555 augroup nasty
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01004556 au QuickFixCmdPre,QuickFixCmdPost,BufEnter,BufLeave * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02004557 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004558 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02004559 augroup nasty
4560 au!
4561 augroup END
4562endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004563
4564func Test_cexpr_acmd_freelist()
4565 call Xexpr_acmd_freelist('c')
4566 call Xexpr_acmd_freelist('l')
4567endfunc
4568
4569" Test for commands that create a new quickfix/location list and jump to the
4570" first error automatically.
4571func Xjumpto_first_error_test(cchar)
4572 call s:setup_commands(a:cchar)
4573
4574 call s:create_test_file('Xtestfile1')
4575 call s:create_test_file('Xtestfile2')
4576 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4577
4578 " Test for cexpr/lexpr
4579 enew
4580 Xexpr l
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004581 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004582 call assert_equal(2, line('.'))
4583
4584 " Test for cfile/lfile
4585 enew
4586 call writefile(l, 'Xerr')
4587 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004588 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004589 call assert_equal(2, line('.'))
4590
4591 " Test for cbuffer/lbuffer
4592 edit Xerr
4593 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004594 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004595 call assert_equal(2, line('.'))
4596
4597 call delete('Xerr')
4598 call delete('Xtestfile1')
4599 call delete('Xtestfile2')
4600endfunc
4601
4602func Test_jumpto_first_error()
4603 call Xjumpto_first_error_test('c')
4604 call Xjumpto_first_error_test('l')
4605endfunc
4606
4607" Test for a quickfix autocmd changing the quickfix/location list before
4608" jumping to the first error in the new list.
4609func Xautocmd_changelist(cchar)
4610 call s:setup_commands(a:cchar)
4611
4612 " Test for cfile/lfile
4613 call s:create_test_file('Xtestfile1')
4614 call s:create_test_file('Xtestfile2')
4615 Xexpr 'Xtestfile1:2:Line2'
4616 autocmd QuickFixCmdPost * Xolder
4617 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4618 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004619 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004620 call assert_equal(4, line('.'))
4621 autocmd! QuickFixCmdPost
4622
4623 " Test for cbuffer/lbuffer
4624 call g:Xsetlist([], 'f')
4625 Xexpr 'Xtestfile1:2:Line2'
4626 autocmd QuickFixCmdPost * Xolder
4627 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4628 edit Xerr
4629 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004630 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004631 call assert_equal(4, line('.'))
4632 autocmd! QuickFixCmdPost
4633
4634 " Test for cexpr/lexpr
4635 call g:Xsetlist([], 'f')
4636 Xexpr 'Xtestfile1:2:Line2'
4637 autocmd QuickFixCmdPost * Xolder
4638 Xexpr 'Xtestfile2:4:Line4'
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004639 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004640 call assert_equal(4, line('.'))
4641 autocmd! QuickFixCmdPost
4642
Bram Moolenaar851332e2018-07-03 19:16:00 +02004643 " The grepprg may not be set on non-Unix systems
4644 if has('unix')
4645 " Test for grep/lgrep
4646 call g:Xsetlist([], 'f')
4647 Xexpr 'Xtestfile1:2:Line2'
4648 autocmd QuickFixCmdPost * Xolder
4649 silent Xgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004650 call assert_equal('Xtestfile2', @%)
Bram Moolenaar851332e2018-07-03 19:16:00 +02004651 call assert_equal(5, line('.'))
4652 autocmd! QuickFixCmdPost
4653 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004654
4655 " Test for vimgrep/lvimgrep
4656 call g:Xsetlist([], 'f')
4657 Xexpr 'Xtestfile1:2:Line2'
4658 autocmd QuickFixCmdPost * Xolder
4659 silent Xvimgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004660 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004661 call assert_equal(5, line('.'))
4662 autocmd! QuickFixCmdPost
4663
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004664 " Test for autocommands clearing the quickfix list before jumping to the
4665 " first error. This should not result in an error
4666 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
4667 let v:errmsg = ''
4668 " Test for cfile/lfile
4669 Xfile Xerr
4670 call assert_true(v:errmsg !~# 'E42:')
4671 " Test for cbuffer/lbuffer
4672 edit Xerr
4673 Xbuffer
4674 call assert_true(v:errmsg !~# 'E42:')
4675 " Test for cexpr/lexpr
4676 Xexpr 'Xtestfile2:4:Line4'
4677 call assert_true(v:errmsg !~# 'E42:')
4678 " Test for grep/lgrep
4679 " The grepprg may not be set on non-Unix systems
4680 if has('unix')
4681 silent Xgrep Line5 Xtestfile2
4682 call assert_true(v:errmsg !~# 'E42:')
4683 endif
4684 " Test for vimgrep/lvimgrep
4685 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
4686 autocmd! QuickFixCmdPost
4687
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004688 call delete('Xerr')
4689 call delete('Xtestfile1')
4690 call delete('Xtestfile2')
4691endfunc
4692
4693func Test_autocmd_changelist()
4694 call Xautocmd_changelist('c')
4695 call Xautocmd_changelist('l')
4696endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004697
4698" Tests for the ':filter /pat/ clist' command
4699func Test_filter_clist()
4700 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4701 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4702 \ split(execute('filter /Line 15/ clist'), "\n"))
4703 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4704 \ split(execute('filter /Xfile1/ clist'), "\n"))
4705 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4706
4707 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4708 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4709 call assert_equal([' 2 pqr:pat2: '],
4710 \ split(execute('filter /pqr/ clist'), "\n"))
4711 call assert_equal([' 1 abc:pat1: '],
4712 \ split(execute('filter /pat1/ clist'), "\n"))
4713endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004714
4715" Tests for the "CTRL-W <CR>" command.
4716func Xview_result_split_tests(cchar)
4717 call s:setup_commands(a:cchar)
4718
4719 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4720 call g:Xsetlist([])
4721 Xopen
4722 let l:win_count = winnr('$')
Bram Moolenaare2e40752020-09-04 21:18:46 +02004723 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42:')
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004724 call assert_equal(l:win_count, winnr('$'))
4725 Xclose
4726endfunc
4727
4728func Test_view_result_split()
4729 call Xview_result_split_tests('c')
4730 call Xview_result_split_tests('l')
4731endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004732
4733" Test that :cc sets curswant
4734func Test_curswant()
4735 helpgrep quickfix
4736 normal! llll
4737 1cc
4738 call assert_equal(getcurpos()[4], virtcol('.'))
4739 cclose | helpclose
4740endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004741
4742" Test for opening a file from the quickfix window using CTRL-W <Enter>
4743" doesn't leave an empty buffer around.
4744func Test_splitview()
4745 call s:create_test_file('Xtestfile1')
4746 call s:create_test_file('Xtestfile2')
4747 new | only
4748 let last_bufnr = bufnr('Test_sv_1', 1)
4749 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4750 cgetexpr l
4751 copen
4752 let numbufs = len(getbufinfo())
4753 exe "normal \<C-W>\<CR>"
4754 copen
4755 exe "normal j\<C-W>\<CR>"
4756 " Make sure new empty buffers are not created
4757 call assert_equal(numbufs, len(getbufinfo()))
4758 " Creating a new buffer should use the next available buffer number
4759 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4760 bwipe Test_sv_1
4761 bwipe Test_sv_2
4762 new | only
4763
4764 " When split opening files from location list window, make sure that two
4765 " windows doesn't refer to the same location list
4766 lgetexpr l
4767 let locid = getloclist(0, {'id' : 0}).id
4768 lopen
4769 exe "normal \<C-W>\<CR>"
4770 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4771 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4772 new | only
4773
4774 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004775 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004776 lhelpgrep window
4777 let locid = getloclist(0, {'id' : 0}).id
4778 lwindow
4779 exe "normal j\<C-W>\<CR>"
4780 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4781 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4782 new | only
4783
Bram Moolenaar406cd902020-02-18 21:54:41 +01004784 " Using :split or :vsplit from a quickfix window should behave like a :new
4785 " or a :vnew command
4786 copen
4787 split
4788 call assert_equal(3, winnr('$'))
4789 let l = getwininfo()
4790 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4791 close
4792 copen
4793 vsplit
4794 let l = getwininfo()
4795 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4796 new | only
4797
Bram Moolenaarb2443732018-11-11 22:50:27 +01004798 call delete('Xtestfile1')
4799 call delete('Xtestfile2')
4800endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004801
4802" Test for parsing entries using visual screen column
4803func Test_viscol()
4804 enew
4805 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4806 edit Xfile1
4807
4808 " Use byte offset for column number
4809 set efm&
4810 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
4811 call assert_equal([5, 8], [col('.'), virtcol('.')])
4812 cnext
4813 call assert_equal([9, 12], [col('.'), virtcol('.')])
4814 cnext
4815 call assert_equal([14, 20], [col('.'), virtcol('.')])
4816
4817 " Use screen column offset for column number
4818 set efm=%f:%l:%v:%m
4819 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4820 call assert_equal([5, 8], [col('.'), virtcol('.')])
4821 cnext
4822 call assert_equal([9, 12], [col('.'), virtcol('.')])
4823 cnext
4824 call assert_equal([14, 20], [col('.'), virtcol('.')])
4825 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4826 call assert_equal([5, 8], [col('.'), virtcol('.')])
4827 cnext
4828 call assert_equal([10, 16], [col('.'), virtcol('.')])
4829 cnext
4830 call assert_equal([14, 20], [col('.'), virtcol('.')])
4831
4832 enew
4833 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4834
4835 " Use byte offset for column number
4836 set efm&
4837 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
4838 call assert_equal([8, 10], [col('.'), virtcol('.')])
4839 cnext
4840 call assert_equal([11, 17], [col('.'), virtcol('.')])
4841 cnext
4842 call assert_equal([16, 25], [col('.'), virtcol('.')])
4843
4844 " Use screen column offset for column number
4845 set efm=%f:%l:%v:%m
4846 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4847 call assert_equal([8, 10], [col('.'), virtcol('.')])
4848 cnext
4849 call assert_equal([11, 17], [col('.'), virtcol('.')])
4850 cnext
4851 call assert_equal([16, 25], [col('.'), virtcol('.')])
4852
Bram Moolenaarc95940c2020-10-20 14:59:12 +02004853 " Use screen column number with a multi-line error message
4854 enew
4855 call writefile(["à test"], 'Xfile1')
4856 set efm=%E===\ %f\ ===,%C%l:%v,%Z%m
4857 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4858 call assert_equal('Xfile1', @%)
4859 call assert_equal([0, 1, 4, 0], getpos('.'))
4860
4861 " Repeat previous test with byte offset %c: ensure that fix to issue #7145
4862 " does not break this
4863 set efm=%E===\ %f\ ===,%C%l:%c,%Z%m
4864 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4865 call assert_equal('Xfile1', @%)
4866 call assert_equal([0, 1, 3, 0], getpos('.'))
4867
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004868 enew | only
4869 set efm&
4870 call delete('Xfile1')
4871endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004872
4873" Test for the quickfix window buffer
4874func Xqfbuf_test(cchar)
4875 call s:setup_commands(a:cchar)
4876
4877 " Quickfix buffer should be reused across closing and opening a quickfix
4878 " window
4879 Xexpr "F1:10:Line10"
4880 Xopen
4881 let qfbnum = bufnr('')
4882 Xclose
4883 " Even after the quickfix window is closed, the buffer should be loaded
4884 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004885 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004886 Xopen
4887 " Buffer should be reused when opening the window again
4888 call assert_equal(qfbnum, bufnr(''))
4889 Xclose
4890
Yegappan Lakshmanan56150da2021-12-09 09:27:06 +00004891 " When quickfix buffer is wiped out, getqflist() should return 0
4892 %bw!
4893 Xexpr ""
4894 Xopen
4895 bw!
4896 call assert_equal(0, g:Xgetlist({'qfbufnr': 0}).qfbufnr)
4897
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004898 if a:cchar == 'l'
4899 %bwipe
4900 " For a location list, when both the file window and the location list
4901 " window for the list are closed, then the buffer should be freed.
4902 new | only
4903 lexpr "F1:10:Line10"
4904 let wid = win_getid()
4905 lopen
4906 let qfbnum = bufnr('')
4907 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
4908 close
4909 " When the location list window is closed, the buffer name should not
4910 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004911 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004912 call assert_true(bufloaded(qfbnum))
4913
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004914 " After deleting a location list buffer using ":bdelete", opening the
4915 " location list window should mark the buffer as a location list buffer.
4916 exe "bdelete " . qfbnum
4917 lopen
4918 call assert_equal("quickfix", &buftype)
4919 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
4920 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
4921 call assert_false(&swapfile)
4922 lclose
4923
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004924 " When the location list is cleared for the window, the buffer should be
4925 " removed
4926 call setloclist(0, [], 'f')
4927 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004928 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004929
4930 " When the location list is freed with the location list window open, the
4931 " location list buffer should not be lost. It should be reused when the
4932 " location list is again populated.
4933 lexpr "F1:10:Line10"
4934 lopen
4935 let wid = win_getid()
4936 let qfbnum = bufnr('')
4937 wincmd p
4938 call setloclist(0, [], 'f')
4939 lexpr "F1:10:Line10"
4940 lopen
4941 call assert_equal(wid, win_getid())
4942 call assert_equal(qfbnum, bufnr(''))
4943 lclose
4944
4945 " When the window with the location list is closed, the buffer should be
4946 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004947 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004948 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004949 endif
4950endfunc
4951
4952func Test_qfbuf()
4953 call Xqfbuf_test('c')
4954 call Xqfbuf_test('l')
4955endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004956
4957" If there is an autocmd to use only one window, then opening the location
4958" list window used to crash Vim.
4959func Test_winonly_autocmd()
4960 call s:create_test_file('Xtest1')
4961 " Autocmd to show only one Vim window at a time
4962 autocmd WinEnter * only
4963 new
4964 " Load the location list
4965 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
4966 let loclistid = getloclist(0, {'id' : 0}).id
4967 " Open the location list window. Only this window will be shown and the file
4968 " window is closed.
4969 lopen
4970 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4971 " Jump to an entry in the location list and make sure that the cursor is
4972 " positioned correctly.
4973 ll 3
4974 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004975 call assert_equal('Xtest1', @%)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004976 call assert_equal(15, line('.'))
4977 " Cleanup
4978 autocmd! WinEnter
4979 new | only
4980 call delete('Xtest1')
4981endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02004982
4983" Test to make sure that an empty quickfix buffer is not reused for loading
4984" a normal buffer.
4985func Test_empty_qfbuf()
4986 enew | only
4987 call writefile(["Test"], 'Xfile1')
4988 call setqflist([], 'f')
4989 copen | only
4990 let qfbuf = bufnr('')
4991 edit Xfile1
4992 call assert_notequal(qfbuf, bufnr(''))
4993 enew
4994 call delete('Xfile1')
4995endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004996
4997" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004998" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004999func Xtest_below(cchar)
5000 call s:setup_commands(a:cchar)
5001
5002 " No quickfix/location list
5003 call assert_fails('Xbelow', 'E42:')
5004 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005005 call assert_fails('Xbefore', 'E42:')
5006 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005007
5008 " Empty quickfix/location list
5009 call g:Xsetlist([])
5010 call assert_fails('Xbelow', 'E42:')
5011 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005012 call assert_fails('Xbefore', 'E42:')
5013 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005014
5015 call s:create_test_file('X1')
5016 call s:create_test_file('X2')
5017 call s:create_test_file('X3')
5018 call s:create_test_file('X4')
5019
5020 " Invalid entries
5021 edit X1
5022 call g:Xsetlist(["E1", "E2"])
5023 call assert_fails('Xbelow', 'E42:')
5024 call assert_fails('Xabove', 'E42:')
5025 call assert_fails('3Xbelow', 'E42:')
5026 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005027 call assert_fails('Xbefore', 'E42:')
5028 call assert_fails('Xafter', 'E42:')
5029 call assert_fails('3Xbefore', 'E42:')
5030 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005031
5032 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005033 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 +02005034 edit +7 X2
5035 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005036 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005037 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005038 normal 7G
5039 Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005040 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005041 call assert_fails('Xbefore', 'E553:')
5042
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005043 normal 2j
5044 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005045 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005046 normal 7G
5047 Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005048 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005049
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005050 " Last error in this file
5051 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005052 call assert_equal(['X2', 15], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005053 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005054 normal gg
5055 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005056 call assert_equal(['X2', 15, 4], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005057 call assert_fails('Xafter', 'E553:')
5058
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005059 " First error in this file
5060 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005061 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005062 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005063 normal G
5064 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005065 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005066 call assert_fails('Xbefore', 'E553:')
5067
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005068 normal gg
5069 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005070 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005071 normal gg
5072 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005073 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005074
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005075 normal G
5076 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005077 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005078 normal G
5079 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005080 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005081
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005082 edit X4
5083 call assert_fails('Xabove', 'E42:')
5084 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005085 call assert_fails('Xbefore', 'E42:')
5086 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005087 if a:cchar == 'l'
5088 " If a buffer has location list entries from some other window but not
5089 " from the current window, then the commands should fail.
5090 edit X1 | split | call setloclist(0, [], 'f')
5091 call assert_fails('Xabove', 'E776:')
5092 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005093 call assert_fails('Xbefore', 'E776:')
5094 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005095 close
5096 endif
5097
5098 " Test for lines with multiple quickfix entries
5099 Xexpr ["X1:5:L5", "X2:5:1:L5_1", "X2:5:2:L5_2", "X2:5:3:L5_3",
5100 \ "X2:10:1:L10_1", "X2:10:2:L10_2", "X2:10:3:L10_3",
5101 \ "X2:15:1:L15_1", "X2:15:2:L15_2", "X2:15:3:L15_3", "X3:3:L3"]
5102 edit +1 X2
5103 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005104 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005105 normal 1G
5106 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005107 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005108
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005109 normal gg
5110 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005111 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005112 normal gg
5113 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005114 call assert_equal(['X2', 15, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005115
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005116 normal G
5117 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005118 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005119 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005120 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005121 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005122
5123 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005124 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005125 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005126 normal G
5127 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005128 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005129
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005130 normal 10G
5131 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005132 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005133 normal 10G$
5134 2Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005135 call assert_equal(['X2', 10, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005136
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005137 normal 10G
5138 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005139 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005140 normal 9G
5141 5Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02005142 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005143
5144 " Invalid range
5145 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02005146 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005147 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005148 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02005149 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02005150 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02005151 endif
5152
5153 call delete('X1')
5154 call delete('X2')
5155 call delete('X3')
5156 call delete('X4')
5157endfunc
5158
5159func Test_cbelow()
5160 call Xtest_below('c')
5161 call Xtest_below('l')
5162endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02005163
5164func Test_quickfix_count()
5165 let commands = [
5166 \ 'cNext',
5167 \ 'cNfile',
5168 \ 'cabove',
5169 \ 'cbelow',
5170 \ 'cfirst',
5171 \ 'clast',
5172 \ 'cnewer',
5173 \ 'cnext',
5174 \ 'cnfile',
5175 \ 'colder',
5176 \ 'cprevious',
5177 \ 'crewind',
5178 \
5179 \ 'lNext',
5180 \ 'lNfile',
5181 \ 'labove',
5182 \ 'lbelow',
5183 \ 'lfirst',
5184 \ 'llast',
5185 \ 'lnewer',
5186 \ 'lnext',
5187 \ 'lnfile',
5188 \ 'lolder',
5189 \ 'lprevious',
5190 \ 'lrewind',
5191 \ ]
5192 for cmd in commands
5193 call assert_fails('-1' .. cmd, 'E16:')
5194 call assert_fails('.' .. cmd, 'E16:')
5195 call assert_fails('%' .. cmd, 'E16:')
5196 call assert_fails('$' .. cmd, 'E16:')
5197 endfor
5198endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005199
5200" Test for aborting quickfix commands using QuickFixCmdPre
5201func Xtest_qfcmd_abort(cchar)
5202 call s:setup_commands(a:cchar)
5203
5204 call g:Xsetlist([], 'f')
5205
5206 " cexpr/lexpr
5207 let e = ''
5208 try
5209 Xexpr ["F1:10:Line10", "F2:20:Line20"]
5210 catch /.*/
5211 let e = v:exception
5212 endtry
5213 call assert_equal('AbortCmd', e)
5214 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5215
5216 " cfile/lfile
5217 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
5218 let e = ''
5219 try
5220 Xfile Xfile1
5221 catch /.*/
5222 let e = v:exception
5223 endtry
5224 call assert_equal('AbortCmd', e)
5225 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5226 call delete('Xfile1')
5227
5228 " cgetbuffer/lgetbuffer
5229 enew!
5230 call append(0, ["F1:10:Line10", "F2:20:Line20"])
5231 let e = ''
5232 try
5233 Xgetbuffer
5234 catch /.*/
5235 let e = v:exception
5236 endtry
5237 call assert_equal('AbortCmd', e)
5238 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5239 enew!
5240
5241 " vimgrep/lvimgrep
5242 let e = ''
5243 try
5244 Xvimgrep /func/ test_quickfix.vim
5245 catch /.*/
5246 let e = v:exception
5247 endtry
5248 call assert_equal('AbortCmd', e)
5249 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5250
5251 " helpgrep/lhelpgrep
5252 let e = ''
5253 try
5254 Xhelpgrep quickfix
5255 catch /.*/
5256 let e = v:exception
5257 endtry
5258 call assert_equal('AbortCmd', e)
5259 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5260
5261 " grep/lgrep
5262 if has('unix')
5263 let e = ''
5264 try
5265 silent Xgrep func test_quickfix.vim
5266 catch /.*/
5267 let e = v:exception
5268 endtry
5269 call assert_equal('AbortCmd', e)
5270 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
5271 endif
5272endfunc
5273
5274func Test_qfcmd_abort()
5275 augroup QF_Test
5276 au!
5277 autocmd QuickFixCmdPre * throw "AbortCmd"
5278 augroup END
5279
5280 call Xtest_qfcmd_abort('c')
5281 call Xtest_qfcmd_abort('l')
5282
5283 augroup QF_Test
5284 au!
5285 augroup END
5286endfunc
5287
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005288" Test for using a file in one of the parent directories.
5289func Test_search_in_dirstack()
5290 call mkdir('Xtestdir/a/b/c', 'p')
5291 let save_cwd = getcwd()
5292 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
5293 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
5294 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
5295 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
5296
5297 let lines = "Entering dir Xtestdir\n" .
5298 \ "Entering dir a\n" .
5299 \ "Entering dir b\n" .
5300 \ "Xfile2:2:X2_L2\n" .
5301 \ "Leaving dir a\n" .
5302 \ "Xfile1:2:X1_L2\n" .
5303 \ "Xfile3:1:X3_L1\n" .
5304 \ "Entering dir c\n" .
5305 \ "Xfile4:2:X4_L2\n" .
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005306 \ "Leaving dir c\n"
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005307 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005308 cexpr lines .. "Leaving dir Xtestdir|\n" | let next = 1
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005309 call assert_equal(11, getqflist({'size' : 0}).size)
5310 call assert_equal(4, getqflist({'idx' : 0}).idx)
5311 call assert_equal('X2_L2', getline('.'))
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01005312 call assert_equal(1, next)
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01005313 cnext
5314 call assert_equal(6, getqflist({'idx' : 0}).idx)
5315 call assert_equal('X1_L2', getline('.'))
5316 cnext
5317 call assert_equal(7, getqflist({'idx' : 0}).idx)
5318 call assert_equal(1, line('$'))
5319 call assert_equal('', getline(1))
5320 cnext
5321 call assert_equal(9, getqflist({'idx' : 0}).idx)
5322 call assert_equal(1, line('$'))
5323 call assert_equal('', getline(1))
5324
5325 set efm&
5326 exe 'cd ' . save_cwd
5327 call delete('Xtestdir', 'rf')
5328endfunc
5329
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005330" Test for :cquit
5331func Test_cquit()
5332 " Exit Vim with a non-zero value
5333 if RunVim([], ["cquit 7"], '')
5334 call assert_equal(7, v:shell_error)
5335 endif
5336
5337 if RunVim([], ["50cquit"], '')
5338 call assert_equal(50, v:shell_error)
5339 endif
5340
5341 " Exit Vim with default value
5342 if RunVim([], ["cquit"], '')
5343 call assert_equal(1, v:shell_error)
5344 endif
5345
5346 " Exit Vim with zero value
5347 if RunVim([], ["cquit 0"], '')
5348 call assert_equal(0, v:shell_error)
5349 endif
5350
5351 " Exit Vim with negative value
5352 call assert_fails('-3cquit', 'E16:')
5353endfunc
5354
Bram Moolenaar858ba062020-05-31 23:11:59 +02005355" Test for getting a specific item from a quickfix list
5356func Xtest_getqflist_by_idx(cchar)
5357 call s:setup_commands(a:cchar)
5358 " Empty list
5359 call assert_equal([], g:Xgetlist({'idx' : 1, 'items' : 0}).items)
5360 Xexpr ['F1:10:L10', 'F1:20:L20']
5361 let l = g:Xgetlist({'idx' : 2, 'items' : 0}).items
5362 call assert_equal(bufnr('F1'), l[0].bufnr)
5363 call assert_equal(20, l[0].lnum)
5364 call assert_equal('L20', l[0].text)
5365 call assert_equal([], g:Xgetlist({'idx' : -1, 'items' : 0}).items)
5366 call assert_equal([], g:Xgetlist({'idx' : 3, 'items' : 0}).items)
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005367 call assert_equal({}, g:Xgetlist(#{idx: "abc"}))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005368 %bwipe!
5369endfunc
5370
5371func Test_getqflist_by_idx()
5372 call Xtest_getqflist_by_idx('c')
5373 call Xtest_getqflist_by_idx('l')
5374endfunc
5375
5376" Test for the 'quickfixtextfunc' setting
5377func Tqfexpr(info)
5378 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005379 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005380 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005381 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005382 endif
5383
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005384 let l = []
5385 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5386 let e = qfl[idx]
5387 let s = ''
5388 if e.bufnr != 0
5389 let bname = bufname(e.bufnr)
5390 let s ..= fnamemodify(bname, ':.')
5391 endif
5392 let s ..= '-'
5393 let s ..= 'L' .. string(e.lnum) .. 'C' .. string(e.col) .. '-'
5394 let s ..= e.text
5395 call add(l, s)
5396 endfor
Bram Moolenaar858ba062020-05-31 23:11:59 +02005397
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005398 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005399endfunc
5400
5401func Xtest_qftextfunc(cchar)
5402 call s:setup_commands(a:cchar)
5403
5404 set efm=%f:%l:%c:%m
5405 set quickfixtextfunc=Tqfexpr
Bram Moolenaard43906d2020-07-20 21:31:32 +02005406 call assert_equal('Tqfexpr', &quickfixtextfunc)
5407 call assert_equal('',
5408 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
thinca6864efa2021-06-19 20:45:20 +02005409 call g:Xsetlist([
5410 \ { 'filename': 'F1', 'lnum': 10, 'col': 2,
5411 \ 'end_col': 7, 'text': 'green'},
5412 \ { 'filename': 'F1', 'lnum': 20, 'end_lnum': 25, 'col': 4,
5413 \ 'end_col': 8, 'text': 'blue'},
5414 \ ])
5415
Bram Moolenaar858ba062020-05-31 23:11:59 +02005416 Xwindow
5417 call assert_equal('F1-L10C2-green', getline(1))
5418 call assert_equal('F1-L20C4-blue', getline(2))
5419 Xclose
5420 set quickfixtextfunc&vim
5421 Xwindow
thinca6864efa2021-06-19 20:45:20 +02005422 call assert_equal('F1|10 col 2-7| green', getline(1))
5423 call assert_equal('F1|20-25 col 4-8| blue', getline(2))
Bram Moolenaar858ba062020-05-31 23:11:59 +02005424 Xclose
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00005425
5426 set efm=%f:%l:%c:%m
5427 set quickfixtextfunc=Tqfexpr
5428 " Update the list with only the cwindow
5429 Xwindow
5430 only
5431 call g:Xsetlist([
5432 \ { 'filename': 'F2', 'lnum': 20, 'col': 2,
5433 \ 'end_col': 7, 'text': 'red'}
5434 \ ])
5435 call assert_equal(['F2-L20C2-red'], getline(1, '$'))
5436 new
5437 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005438 set efm&
5439 set quickfixtextfunc&
5440
5441 " Test for per list 'quickfixtextfunc' setting
5442 func PerQfText(info)
5443 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005444 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005445 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005446 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005447 endif
5448 if empty(qfl)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005449 return []
Bram Moolenaar858ba062020-05-31 23:11:59 +02005450 endif
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005451 let l = []
5452 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5453 call add(l, 'Line ' .. qfl[idx].lnum .. ', Col ' .. qfl[idx].col)
5454 endfor
5455 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005456 endfunc
5457 set quickfixtextfunc=Tqfexpr
5458 call g:Xsetlist([], ' ', {'quickfixtextfunc' : "PerQfText"})
5459 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5460 Xwindow
5461 call assert_equal('Line 10, Col 2', getline(1))
5462 call assert_equal('Line 20, Col 4', getline(2))
5463 Xclose
Bram Moolenaard43906d2020-07-20 21:31:32 +02005464 call assert_equal(function('PerQfText'),
5465 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005466 " Add entries to the list when the quickfix buffer is hidden
5467 Xaddexpr ['F1:30:6:red']
5468 Xwindow
5469 call assert_equal('Line 30, Col 6', getline(3))
5470 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005471 call g:Xsetlist([], 'r', {'quickfixtextfunc' : ''})
Bram Moolenaard43906d2020-07-20 21:31:32 +02005472 call assert_equal('', g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar858ba062020-05-31 23:11:59 +02005473 set quickfixtextfunc&
5474 delfunc PerQfText
5475
5476 " Non-existing function
5477 set quickfixtextfunc=Tabc
5478 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5479 call assert_fails("Xwindow", 'E117:')
5480 Xclose
5481 set quickfixtextfunc&
5482
5483 " set option to a non-function
5484 set quickfixtextfunc=[10,\ 20]
5485 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5486 call assert_fails("Xwindow", 'E117:')
5487 Xclose
5488 set quickfixtextfunc&
5489
5490 " set option to a function with different set of arguments
5491 func Xqftext(a, b, c)
5492 return a:a .. a:b .. a:c
5493 endfunc
5494 set quickfixtextfunc=Xqftext
5495 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E119:')
5496 call assert_fails("Xwindow", 'E119:')
5497 Xclose
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005498
5499 " set option to a function that returns a list with non-strings
5500 func Xqftext2(d)
5501 return ['one', [], 'two']
5502 endfunc
5503 set quickfixtextfunc=Xqftext2
5504 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue', 'F1:30:6:red']",
5505 \ 'E730:')
5506 call assert_fails('Xwindow', 'E730:')
Bram Moolenaard43906d2020-07-20 21:31:32 +02005507 call assert_equal(['one', 'F1|20 col 4| blue', 'F1|30 col 6| red'],
5508 \ getline(1, '$'))
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005509 Xclose
5510
Bram Moolenaar858ba062020-05-31 23:11:59 +02005511 set quickfixtextfunc&
5512 delfunc Xqftext
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005513 delfunc Xqftext2
Bram Moolenaard43906d2020-07-20 21:31:32 +02005514
5515 " set the global option to a lambda function
5516 set quickfixtextfunc={d\ ->\ map(g:Xgetlist({'id'\ :\ d.id,\ 'items'\ :\ 1}).items[d.start_idx-1:d.end_idx-1],\ 'v:val.text')}
5517 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5518 Xwindow
5519 call assert_equal(['green', 'blue'], getline(1, '$'))
5520 Xclose
5521 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)
5522 set quickfixtextfunc&
5523
5524 " use a lambda function that returns an empty list
5525 set quickfixtextfunc={d\ ->\ []}
5526 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5527 Xwindow
5528 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5529 \ getline(1, '$'))
5530 Xclose
5531 set quickfixtextfunc&
5532
5533 " use a lambda function that returns a list with empty strings
5534 set quickfixtextfunc={d\ ->\ ['',\ '']}
5535 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5536 Xwindow
5537 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5538 \ getline(1, '$'))
5539 Xclose
5540 set quickfixtextfunc&
5541
5542 " set the per-quickfix list text function to a lambda function
5543 call g:Xsetlist([], ' ',
5544 \ {'quickfixtextfunc' :
5545 \ {d -> map(g:Xgetlist({'id' : d.id, 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5546 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5547 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5548 Xwindow
5549 call assert_equal('Line 10, Col 2', getline(1))
5550 call assert_equal('Line 20, Col 4', getline(2))
5551 Xclose
5552 call assert_match("function('<lambda>\\d\\+')", string(g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc))
5553 call g:Xsetlist([], 'f')
Bram Moolenaar858ba062020-05-31 23:11:59 +02005554endfunc
5555
5556func Test_qftextfunc()
5557 call Xtest_qftextfunc('c')
5558 call Xtest_qftextfunc('l')
5559endfunc
5560
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005561func Test_qftextfunc_callback()
5562 let lines =<< trim END
5563 set efm=%f:%l:%c:%m
5564
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +00005565 #" Test for using a function name
5566 LET &qftf = 'g:Tqfexpr'
5567 cexpr "F0:0:0:L0"
5568 copen
5569 call assert_equal('F0-L0C0-L0', getline(1))
5570 cclose
5571
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005572 #" Test for using a function()
5573 set qftf=function('g:Tqfexpr')
5574 cexpr "F1:1:1:L1"
5575 copen
5576 call assert_equal('F1-L1C1-L1', getline(1))
5577 cclose
5578
5579 #" Using a funcref variable to set 'quickfixtextfunc'
5580 VAR Fn = function('g:Tqfexpr')
5581 LET &qftf = Fn
5582 cexpr "F2:2:2:L2"
5583 copen
5584 call assert_equal('F2-L2C2-L2', getline(1))
5585 cclose
5586
5587 #" Using string(funcref_variable) to set 'quickfixtextfunc'
5588 LET Fn = function('g:Tqfexpr')
5589 LET &qftf = string(Fn)
5590 cexpr "F3:3:3:L3"
5591 copen
5592 call assert_equal('F3-L3C3-L3', getline(1))
5593 cclose
5594
5595 #" Test for using a funcref()
5596 set qftf=funcref('g:Tqfexpr')
5597 cexpr "F4:4:4:L4"
5598 copen
5599 call assert_equal('F4-L4C4-L4', getline(1))
5600 cclose
5601
5602 #" Using a funcref variable to set 'quickfixtextfunc'
5603 LET Fn = funcref('g:Tqfexpr')
5604 LET &qftf = Fn
5605 cexpr "F5:5:5:L5"
5606 copen
5607 call assert_equal('F5-L5C5-L5', getline(1))
5608 cclose
5609
5610 #" Using a string(funcref_variable) to set 'quickfixtextfunc'
5611 LET Fn = funcref('g:Tqfexpr')
5612 LET &qftf = string(Fn)
5613 cexpr "F5:5:5:L5"
5614 copen
5615 call assert_equal('F5-L5C5-L5', getline(1))
5616 cclose
5617
5618 #" Test for using a lambda function with set
Bram Moolenaar62aec932022-01-29 21:45:34 +00005619 VAR optval = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005620 LET optval = substitute(optval, ' ', '\\ ', 'g')
5621 exe "set qftf=" .. optval
5622 cexpr "F6:6:6:L6"
5623 copen
5624 call assert_equal('F6-L6C6-L6', getline(1))
5625 cclose
5626
5627 #" Set 'quickfixtextfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005628 LET &qftf = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005629 cexpr "F7:7:7:L7"
5630 copen
5631 call assert_equal('F7-L7C7-L7', getline(1))
5632 cclose
5633
5634 #" Set 'quickfixtextfunc' to string(lambda_expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005635 LET &qftf = "LSTART a LMIDDLE g:Tqfexpr(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005636 cexpr "F8:8:8:L8"
5637 copen
5638 call assert_equal('F8-L8C8-L8', getline(1))
5639 cclose
5640
5641 #" Set 'quickfixtextfunc' to a variable with a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +00005642 VAR Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005643 LET &qftf = Lambda
5644 cexpr "F9:9:9:L9"
5645 copen
5646 call assert_equal('F9-L9C9-L9', getline(1))
5647 cclose
5648
5649 #" Set 'quickfixtextfunc' to a string(variable with a lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +00005650 LET Lambda = LSTART a LMIDDLE g:Tqfexpr(a) LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005651 LET &qftf = string(Lambda)
5652 cexpr "F9:9:9:L9"
5653 copen
5654 call assert_equal('F9-L9C9-L9', getline(1))
5655 cclose
5656 END
Bram Moolenaar62aec932022-01-29 21:45:34 +00005657 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005658
Yegappan Lakshmanandb1a4102021-12-17 16:21:20 +00005659 " Test for using a script-local function name
5660 func s:TqfFunc2(info)
5661 let g:TqfFunc2Args = [a:info.start_idx, a:info.end_idx]
5662 return ''
5663 endfunc
5664 let g:TqfFunc2Args = []
5665 set quickfixtextfunc=s:TqfFunc2
5666 cexpr "F10:10:10:L10"
5667 cclose
5668 call assert_equal([1, 1], g:TqfFunc2Args)
5669
5670 let &quickfixtextfunc = 's:TqfFunc2'
5671 cexpr "F11:11:11:L11"
5672 cclose
5673 call assert_equal([1, 1], g:TqfFunc2Args)
5674 delfunc s:TqfFunc2
5675
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005676 " set 'quickfixtextfunc' to a partial with dict. This used to cause a crash.
5677 func SetQftfFunc()
5678 let params = {'qftf': function('g:DictQftfFunc')}
5679 let &quickfixtextfunc = params.qftf
5680 endfunc
5681 func g:DictQftfFunc(_) dict
5682 endfunc
5683 call SetQftfFunc()
5684 new
5685 call SetQftfFunc()
5686 bw
5687 call test_garbagecollect_now()
5688 new
5689 set qftf=
5690 wincmd w
5691 set qftf=
5692 :%bw!
5693
5694 " set per-quickfix list 'quickfixtextfunc' to a partial with dict. This used
5695 " to cause a crash.
5696 let &qftf = ''
5697 func SetLocalQftfFunc()
5698 let params = {'qftf': function('g:DictQftfFunc')}
5699 call setqflist([], 'a', {'quickfixtextfunc' : params.qftf})
5700 endfunc
5701 call SetLocalQftfFunc()
5702 call test_garbagecollect_now()
5703 call setqflist([], 'a', {'quickfixtextfunc' : ''})
5704 delfunc g:DictQftfFunc
5705 delfunc SetQftfFunc
5706 delfunc SetLocalQftfFunc
5707 set efm&
5708endfunc
5709
Yegappan Lakshmananad52f962021-06-19 18:22:53 +02005710" Test for updating a location list for some other window and check that
5711" 'qftextfunc' uses the correct location list.
5712func Test_qftextfunc_other_loclist()
5713 %bw!
5714 call setloclist(0, [], 'f')
5715
5716 " create a window and a location list for it and open the location list
5717 " window
5718 lexpr ['F1:10:12:one', 'F1:20:14:two']
5719 let w1_id = win_getid()
5720 call setloclist(0, [], ' ',
5721 \ {'lines': ['F1:10:12:one', 'F1:20:14:two'],
5722 \ 'quickfixtextfunc':
5723 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5724 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5725 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5726 lwindow
5727 let w2_id = win_getid()
5728
5729 " create another window and a location list for it and open the location
5730 " list window
5731 topleft new
5732 let w3_id = win_getid()
5733 call setloclist(0, [], ' ',
5734 \ {'lines': ['F2:30:32:eleven', 'F2:40:34:twelve'],
5735 \ 'quickfixtextfunc':
5736 \ {d -> map(getloclist(d.winid, {'id' : d.id,
5737 \ 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5738 \ "'Ligne ' .. v:val.lnum .. ', Colonne ' .. v:val.col")}})
5739 lwindow
5740 let w4_id = win_getid()
5741
5742 topleft new
5743 lexpr ['F3:50:52:green', 'F3:60:54:blue']
5744 let w5_id = win_getid()
5745
5746 " change the location list for some other window
5747 call setloclist(0, [], 'r', {'lines': ['F3:55:56:aaa', 'F3:57:58:bbb']})
5748 call setloclist(w1_id, [], 'r', {'lines': ['F1:62:63:bbb', 'F1:64:65:ccc']})
5749 call setloclist(w3_id, [], 'r', {'lines': ['F2:76:77:ddd', 'F2:78:79:eee']})
5750 call assert_equal(['Line 62, Col 63', 'Line 64, Col 65'],
5751 \ getbufline(winbufnr(w2_id), 1, '$'))
5752 call assert_equal(['Ligne 76, Colonne 77', 'Ligne 78, Colonne 79'],
5753 \ getbufline(winbufnr(w4_id), 1, '$'))
5754 call setloclist(w2_id, [], 'r', {'lines': ['F1:32:33:fff', 'F1:34:35:ggg']})
5755 call setloclist(w4_id, [], 'r', {'lines': ['F2:46:47:hhh', 'F2:48:49:jjj']})
5756 call assert_equal(['Line 32, Col 33', 'Line 34, Col 35'],
5757 \ getbufline(winbufnr(w2_id), 1, '$'))
5758 call assert_equal(['Ligne 46, Colonne 47', 'Ligne 48, Colonne 49'],
5759 \ getbufline(winbufnr(w4_id), 1, '$'))
5760
5761 call win_gotoid(w5_id)
5762 lwindow
5763 call assert_equal(['F3|55 col 56| aaa', 'F3|57 col 58| bbb'],
5764 \ getline(1, '$'))
5765 %bw!
5766endfunc
5767
Bram Moolenaarec98e932020-06-08 19:35:59 +02005768" Running :lhelpgrep command more than once in a help window, doesn't jump to
5769" the help topic
5770func Test_lhelpgrep_from_help_window()
5771 call mkdir('Xtestdir/doc', 'p')
5772 call writefile(['window'], 'Xtestdir/doc/a.txt')
5773 call writefile(['buffer'], 'Xtestdir/doc/b.txt')
5774 let save_rtp = &rtp
5775 let &rtp = 'Xtestdir'
5776 lhelpgrep window
5777 lhelpgrep buffer
5778 call assert_equal('b.txt', fnamemodify(@%, ":p:t"))
5779 lhelpgrep window
5780 call assert_equal('a.txt', fnamemodify(@%, ":p:t"))
5781 let &rtp = save_rtp
5782 call delete('Xtestdir', 'rf')
5783 new | only!
5784endfunc
5785
Bram Moolenaarf7c4d832020-08-11 20:42:19 +02005786" Test for the crash fixed by 7.3.715
5787func Test_setloclist_crash()
5788 %bw!
5789 let g:BufNum = bufnr()
5790 augroup QF_Test
5791 au!
5792 au BufUnload * call setloclist(0, [{'bufnr':g:BufNum, 'lnum':1, 'col':1, 'text': 'tango down'}])
5793 augroup END
5794
5795 try
5796 lvimgrep /.*/ *.mak
5797 catch /E926:/
5798 endtry
5799 call assert_equal('tango down', getloclist(0, {'items' : 0}).items[0].text)
5800 call assert_equal(1, getloclist(0, {'size' : 0}).size)
5801
5802 augroup QF_Test
5803 au!
5804 augroup END
5805 unlet g:BufNum
5806 %bw!
5807endfunc
5808
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005809" Test for adding an invalid entry with the quickfix window open and making
5810" sure that the window contents are not changed
5811func Test_add_invalid_entry_with_qf_window()
5812 call setqflist([], 'f')
5813 cexpr "Xfile1:10:aa"
5814 copen
5815 call setqflist(['bb'], 'a')
5816 call assert_equal(1, line('$'))
5817 call assert_equal(['Xfile1|10| aa'], getline(1, '$'))
shane.xb.qian0d5e1ec2021-06-20 16:31:00 +02005818 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())
5819
5820 call setqflist([{'lnum': 10 , 'bufnr': bufnr('Xfile1') , 'col': 0 , 'pattern': '' , 'valid': 1 , 'vcol': 0 , 'nr': -1 , 'type': '' , 'module': '' , 'text': 'aa'}] , 'r')
5821 call assert_equal(1 , line('$'))
5822 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5823 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())
5824
5825 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')
5826 call assert_equal(1 , line('$'))
5827 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5828 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())
5829
5830 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')
5831 call assert_equal(1 , line('$'))
5832 call assert_equal(['Xfile1|10| aa'] , getline(1 , '$'))
5833 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())
5834
5835 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')
5836 call assert_equal(1 , line('$'))
5837 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5838 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())
5839
5840 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')
5841 call assert_equal(1 , line('$'))
5842 call assert_equal(['Xfile1|10 col 666| aa'] , getline(1 , '$'))
5843 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())
5844
5845 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')
5846 call assert_equal(1 , line('$'))
5847 call assert_equal(['Xfile1|10 col 666-222| aa'] , getline(1 , '$'))
5848 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())
5849
5850 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')
5851 call assert_equal(1 , line('$'))
5852 call assert_equal(['Xfile1|10-6 col 666-222| aa'] , getline(1 , '$'))
5853 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 +01005854 cclose
5855endfunc
5856
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01005857" Test for very weird problem: autocommand causes a failure, resulting opening
5858" the quickfix window to fail. This still splits the window, but otherwise
5859" should not mess up buffers.
5860func Test_quickfix_window_fails_to_open()
5861 CheckScreendump
5862
5863 let lines =<< trim END
5864 anything
5865 try
5866 anything
5867 endtry
5868 END
5869 call writefile(lines, 'XquickfixFails')
5870
5871 let lines =<< trim END
5872 split XquickfixFails
5873 silent vimgrep anything %
5874 normal o
5875 au BufLeave * ++once source XquickfixFails
5876 " This will trigger the autocommand, which causes an error, what follows
5877 " is aborted but the window was already split.
5878 silent! cwindow
5879 END
5880 call writefile(lines, 'XtestWinFails')
5881 let buf = RunVimInTerminal('-S XtestWinFails', #{rows: 13})
5882 call VerifyScreenDump(buf, 'Test_quickfix_window_fails', {})
5883
5884 " clean up
5885 call term_sendkeys(buf, ":bwipe!\<CR>")
5886 call term_wait(buf)
5887 call StopVimInTerminal(buf)
5888 call delete('XtestWinFails')
5889 call delete('XquickfixFails')
5890endfunc
5891
Bram Moolenaar2d870f82020-12-05 13:41:01 +01005892" Test for updating the quickfix buffer whenever the associated quickfix list
Bram Moolenaar287153c2020-11-29 14:20:27 +01005893" is changed.
5894func Xqfbuf_update(cchar)
5895 call s:setup_commands(a:cchar)
5896
5897 Xexpr "F1:1:line1"
5898 Xopen
5899 call assert_equal(['F1|1| line1'], getline(1, '$'))
5900 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5901
5902 " Test setqflist() using the 'lines' key in 'what'
5903 " add a new entry
5904 call g:Xsetlist([], 'a', {'lines' : ['F2:2: line2']})
5905 call assert_equal(['F1|1| line1', 'F2|2| line2'], getline(1, '$'))
5906 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
5907 " replace all the entries with a single entry
5908 call g:Xsetlist([], 'r', {'lines' : ['F3:3: line3']})
5909 call assert_equal(['F3|3| line3'], getline(1, '$'))
5910 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
5911 " remove all the entries
5912 call g:Xsetlist([], 'r', {'lines' : []})
5913 call assert_equal([''], getline(1, '$'))
5914 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
5915 " add a new list
5916 call g:Xsetlist([], ' ', {'lines' : ['F4:4: line4']})
5917 call assert_equal(['F4|4| line4'], getline(1, '$'))
5918 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5919
5920 " Test setqflist() using the 'items' key in 'what'
5921 " add a new entry
5922 call g:Xsetlist([], 'a', {'items' : [{'filename' : 'F5', 'lnum' : 5, 'text' : 'line5'}]})
5923 call assert_equal(['F4|4| line4', 'F5|5| line5'], getline(1, '$'))
5924 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
5925 " replace all the entries with a single entry
5926 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F6', 'lnum' : 6, 'text' : 'line6'}]})
5927 call assert_equal(['F6|6| line6'], getline(1, '$'))
5928 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
5929 " remove all the entries
5930 call g:Xsetlist([], 'r', {'items' : []})
5931 call assert_equal([''], getline(1, '$'))
5932 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
5933 " add a new list
5934 call g:Xsetlist([], ' ', {'items' : [{'filename' : 'F7', 'lnum' : 7, 'text' : 'line7'}]})
5935 call assert_equal(['F7|7| line7'], getline(1, '$'))
5936 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5937
5938 call g:Xsetlist([], ' ', {})
5939 call assert_equal([''], getline(1, '$'))
5940 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5941
5942 Xclose
5943endfunc
5944
5945func Test_qfbuf_update()
5946 call Xqfbuf_update('c')
5947 call Xqfbuf_update('l')
5948endfunc
5949
Bram Moolenaar8c801b32021-03-05 20:58:22 +01005950func Test_vimgrep_noswapfile()
5951 set noswapfile
5952 call writefile(['one', 'two', 'three'], 'Xgreppie')
5953 vimgrep two Xgreppie
5954 call assert_equal('two', getline('.'))
5955
5956 call delete('Xgreppie')
5957 set swapfile
5958endfunc
5959
Yegappan Lakshmananbb01a1e2021-04-26 21:17:52 +02005960" Test for the :vimgrep 'f' flag (fuzzy match)
5961func Xvimgrep_fuzzy_match(cchar)
5962 call s:setup_commands(a:cchar)
5963
5964 Xvimgrep /three one/f Xfile*
5965 let l = g:Xgetlist()
5966 call assert_equal(2, len(l))
5967 call assert_equal(['Xfile1', 1, 9, 'one two three'],
5968 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
5969 call assert_equal(['Xfile2', 2, 1, 'three one two'],
5970 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
5971
5972 Xvimgrep /the/f Xfile*
5973 let l = g:Xgetlist()
5974 call assert_equal(3, len(l))
5975 call assert_equal(['Xfile1', 1, 9, 'one two three'],
5976 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
5977 call assert_equal(['Xfile2', 2, 1, 'three one two'],
5978 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
5979 call assert_equal(['Xfile2', 4, 4, 'aaathreeaaa'],
5980 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
5981
5982 Xvimgrep /aaa/fg Xfile*
5983 let l = g:Xgetlist()
5984 call assert_equal(4, len(l))
5985 call assert_equal(['Xfile1', 2, 1, 'aaaaaa'],
5986 \ [bufname(l[0].bufnr), l[0].lnum, l[0].col, l[0].text])
5987 call assert_equal(['Xfile1', 2, 4, 'aaaaaa'],
5988 \ [bufname(l[1].bufnr), l[1].lnum, l[1].col, l[1].text])
5989 call assert_equal(['Xfile2', 4, 1, 'aaathreeaaa'],
5990 \ [bufname(l[2].bufnr), l[2].lnum, l[2].col, l[2].text])
5991 call assert_equal(['Xfile2', 4, 9, 'aaathreeaaa'],
5992 \ [bufname(l[3].bufnr), l[3].lnum, l[3].col, l[3].text])
5993
5994 call assert_fails('Xvimgrep /xyz/fg Xfile*', 'E480:')
5995endfunc
5996
5997func Test_vimgrep_fuzzy_match()
5998 call writefile(['one two three', 'aaaaaa'], 'Xfile1')
5999 call writefile(['one', 'three one two', 'two', 'aaathreeaaa'], 'Xfile2')
6000 call Xvimgrep_fuzzy_match('c')
6001 call Xvimgrep_fuzzy_match('l')
6002 call delete('Xfile1')
6003 call delete('Xfile2')
6004endfunc
6005
Wei-Chung Wen1557b162021-07-15 13:13:39 +02006006func Test_locationlist_open_in_newtab()
6007 call s:create_test_file('Xqftestfile1')
6008 call s:create_test_file('Xqftestfile2')
6009 call s:create_test_file('Xqftestfile3')
6010
6011 %bwipe!
6012
6013 lgetexpr ['Xqftestfile1:5:Line5',
6014 \ 'Xqftestfile2:10:Line10',
6015 \ 'Xqftestfile3:16:Line16']
6016
6017 silent! llast
6018 call assert_equal(1, tabpagenr('$'))
6019 call assert_equal('Xqftestfile3', bufname())
6020
6021 set switchbuf=newtab
6022
6023 silent! lfirst
6024 call assert_equal(2, tabpagenr('$'))
6025 call assert_equal('Xqftestfile1', bufname())
6026
6027 silent! lnext
6028 call assert_equal(3, tabpagenr('$'))
6029 call assert_equal('Xqftestfile2', bufname())
6030
6031 call delete('Xqftestfile1')
6032 call delete('Xqftestfile2')
6033 call delete('Xqftestfile3')
6034 set switchbuf&vim
6035
6036 %bwipe!
6037endfunc
6038
Yegappan Lakshmanan28d84212021-07-31 12:43:23 +02006039" Test for win_gettype() in quickfix and location list windows
6040func Test_win_gettype()
6041 copen
6042 call assert_equal("quickfix", win_gettype())
6043 let wid = win_getid()
6044 wincmd p
6045 call assert_equal("quickfix", win_gettype(wid))
6046 cclose
6047 lexpr ''
6048 lopen
6049 call assert_equal("loclist", win_gettype())
6050 let wid = win_getid()
6051 wincmd p
6052 call assert_equal("loclist", win_gettype(wid))
6053 lclose
6054endfunc
6055
Christian Brabandt0b226f62021-12-01 10:54:24 +00006056fun Test_vimgrep_nomatch()
6057 call XexprTests('c')
6058 call g:Xsetlist([{'lnum':10,'text':'Line1'}])
6059 copen
6060 if has("win32")
6061 call assert_fails('vimgrep foo *.zzz', 'E479:')
6062 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'}]
6063 else
6064 call assert_fails('vimgrep foo *.zzz', 'E480:')
6065 let expected = []
6066 endif
6067 call assert_equal(expected, getqflist())
6068 cclose
6069endfunc
6070
Yegappan Lakshmanan78a61062021-12-08 20:03:31 +00006071" Test for opening the quickfix window in two tab pages and then closing one
6072" of the quickfix windows. This should not make the quickfix buffer unlisted.
6073" (github issue #9300).
6074func Test_two_qf_windows()
6075 cexpr "F1:1:line1"
6076 copen
6077 tabnew
6078 copen
6079 call assert_true(&buflisted)
6080 cclose
6081 tabfirst
6082 call assert_true(&buflisted)
6083 let bnum = bufnr()
6084 cclose
6085 " if all the quickfix windows are closed, then buffer should be unlisted.
6086 call assert_false(buflisted(bnum))
6087 %bw!
6088
6089 " Repeat the test for a location list
6090 lexpr "F2:2:line2"
6091 lopen
6092 let bnum = bufnr()
6093 tabnew
6094 exe "buffer" bnum
6095 tabfirst
6096 lclose
6097 tablast
6098 call assert_true(buflisted(bnum))
6099 tabclose
6100 lopen
6101 call assert_true(buflisted(bnum))
6102 lclose
6103 call assert_false(buflisted(bnum))
6104 %bw!
6105endfunc
6106
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006107" Weird sequence of commands that caused entering a wiped-out buffer
6108func Test_lopen_bwipe()
6109 func R()
6110 silent! tab lopen
6111 e x
6112 silent! lfile
6113 endfunc
6114
6115 cal R()
6116 cal R()
6117 cal R()
6118 bw!
6119 delfunc R
6120endfunc
6121
Bram Moolenaare3537ae2022-02-08 15:05:20 +00006122" Another sequence of commands that caused all buffers to be wiped out
6123func Test_lopen_bwipe_all()
6124 let lines =<< trim END
6125 func R()
6126 silent! tab lopen
6127 e foo
6128 silent! lfile
6129 endfunc
6130 cal R()
6131 exe "norm \<C-W>\<C-V>0"
6132 cal R()
6133 bwipe
6134
6135 call writefile(['done'], 'Xresult')
6136 qall!
6137 END
6138 call writefile(lines, 'Xscript')
6139 if RunVim([], [], '-u NONE -n -X -Z -e -m -s -S Xscript')
6140 call assert_equal(['done'], readfile('Xresult'))
6141 endif
6142
6143 call delete('Xscript')
6144 call delete('Xresult')
6145endfunc
6146
Yegappan Lakshmanan9c9be052022-02-24 12:33:17 +00006147" Test for calling setqflist() function recursively
6148func Test_recursive_setqflist()
6149 augroup QF_Test
6150 au!
6151 autocmd BufWinEnter quickfix call setqflist([], 'r')
6152 augroup END
6153
6154 copen
6155 call assert_fails("call setqflist([], 'a')", 'E952:')
6156
6157 augroup QF_Test
6158 au!
6159 augroup END
6160 %bw!
6161endfunc
6162
6163" Test for failure to create a new window when selecting a file from the
6164" quickfix window
6165func Test_cwindow_newwin_fails()
6166 cgetexpr ["Xfile1:10:L10", "Xfile1:20:L20"]
6167 cwindow
6168 only
6169 let qf_wid = win_getid()
6170 " create the maximum number of scratch windows
6171 let hor_win_count = (&lines - 1)/2
6172 let hor_split_count = hor_win_count - 1
6173 for s in range(1, hor_split_count) | new | set buftype=nofile | endfor
6174 call win_gotoid(qf_wid)
6175 call assert_fails('exe "normal \<CR>"', 'E36:')
6176 %bw!
6177endfunc
6178
6179" Test for updating the location list when only the location list window is
6180" present and the corresponding file window is closed.
6181func Test_loclist_update_with_llwin_only()
6182 %bw!
6183 new
6184 wincmd w
6185 lexpr ["Xfile1:1:Line1"]
6186 lopen
6187 wincmd p
6188 close
6189 call setloclist(2, [], 'r', {'lines': ["Xtest2:2:Line2"]})
6190 call assert_equal(['Xtest2|2| Line2'], getbufline(winbufnr(2), 1, '$'))
6191 %bw!
6192endfunc
6193
6194" Test for getting the quickfix list after a buffer with an error is wiped out
6195func Test_getqflist_wiped_out_buffer()
6196 %bw!
6197 cexpr ["Xtest1:34:Wiped out"]
6198 let bnum = bufnr('Xtest1')
6199 call assert_equal(bnum, getqflist()[0].bufnr)
6200 bw Xtest1
6201 call assert_equal(0, getqflist()[0].bufnr)
6202 %bw!
6203endfunc
Bram Moolenaar9b4a80a2022-02-01 13:54:17 +00006204
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02006205" vim: shiftwidth=2 sts=2 expandtab