blob: 78fd6c90fb18fcac0a939992c32498754c64c978 [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
4CheckFeature quickfix
Bram Moolenaarda59dd52016-01-05 21:59:58 +01005
Bram Moolenaare00fdf32019-09-15 19:09:42 +02006source screendump.vim
7
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01008set encoding=utf-8
9
Bram Moolenaar2b946c92016-11-12 18:14:44 +010010func s:setup_commands(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020011 if a:cchar == 'c'
12 command! -nargs=* -bang Xlist <mods>clist<bang> <args>
13 command! -nargs=* Xgetexpr <mods>cgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020014 command! -nargs=* Xaddexpr <mods>caddexpr <args>
Bram Moolenaar55b69262017-08-13 13:42:01 +020015 command! -nargs=* -count Xolder <mods><count>colder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020016 command! -nargs=* Xnewer <mods>cnewer <args>
Bram Moolenaarf9ae1542019-11-18 22:02:16 +010017 command! -nargs=* Xopen <mods> copen <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020018 command! -nargs=* Xwindow <mods>cwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020019 command! -nargs=* Xbottom <mods>cbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020020 command! -nargs=* Xclose <mods>cclose <args>
21 command! -nargs=* -bang Xfile <mods>cfile<bang> <args>
22 command! -nargs=* Xgetfile <mods>cgetfile <args>
23 command! -nargs=* Xaddfile <mods>caddfile <args>
24 command! -nargs=* -bang Xbuffer <mods>cbuffer<bang> <args>
25 command! -nargs=* Xgetbuffer <mods>cgetbuffer <args>
26 command! -nargs=* Xaddbuffer <mods>caddbuffer <args>
27 command! -nargs=* Xrewind <mods>crewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020028 command! -count -nargs=* -bang Xnext <mods><count>cnext<bang> <args>
29 command! -count -nargs=* -bang Xprev <mods><count>cprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020030 command! -nargs=* -bang Xfirst <mods>cfirst<bang> <args>
31 command! -nargs=* -bang Xlast <mods>clast<bang> <args>
Bram Moolenaar25190db2019-05-04 15:05:28 +020032 command! -count -nargs=* -bang Xnfile <mods><count>cnfile<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020033 command! -nargs=* -bang Xpfile <mods>cpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020034 command! -nargs=* Xexpr <mods>cexpr <args>
Bram Moolenaarf9ae1542019-11-18 22:02:16 +010035 command! -count -nargs=* Xvimgrep <mods> <count>vimgrep <args>
36 command! -nargs=* Xvimgrepadd <mods> vimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020037 command! -nargs=* Xgrep <mods> grep <args>
38 command! -nargs=* Xgrepadd <mods> grepadd <args>
39 command! -nargs=* Xhelpgrep helpgrep <args>
Bram Moolenaar74240d32017-12-10 15:26:15 +010040 command! -nargs=0 -count Xcc <count>cc
Bram Moolenaar3ff33112019-05-03 21:56:35 +020041 command! -count=1 -nargs=0 Xbelow <mods><count>cbelow
42 command! -count=1 -nargs=0 Xabove <mods><count>cabove
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +020043 command! -count=1 -nargs=0 Xbefore <mods><count>cbefore
44 command! -count=1 -nargs=0 Xafter <mods><count>cafter
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020045 let g:Xgetlist = function('getqflist')
46 let g:Xsetlist = function('setqflist')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020047 call setqflist([], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020048 else
49 command! -nargs=* -bang Xlist <mods>llist<bang> <args>
50 command! -nargs=* Xgetexpr <mods>lgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020051 command! -nargs=* Xaddexpr <mods>laddexpr <args>
Bram Moolenaar55b69262017-08-13 13:42:01 +020052 command! -nargs=* -count Xolder <mods><count>lolder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020053 command! -nargs=* Xnewer <mods>lnewer <args>
Bram Moolenaarf9ae1542019-11-18 22:02:16 +010054 command! -nargs=* Xopen <mods> lopen <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020055 command! -nargs=* Xwindow <mods>lwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020056 command! -nargs=* Xbottom <mods>lbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020057 command! -nargs=* Xclose <mods>lclose <args>
58 command! -nargs=* -bang Xfile <mods>lfile<bang> <args>
59 command! -nargs=* Xgetfile <mods>lgetfile <args>
60 command! -nargs=* Xaddfile <mods>laddfile <args>
61 command! -nargs=* -bang Xbuffer <mods>lbuffer<bang> <args>
62 command! -nargs=* Xgetbuffer <mods>lgetbuffer <args>
63 command! -nargs=* Xaddbuffer <mods>laddbuffer <args>
64 command! -nargs=* Xrewind <mods>lrewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020065 command! -count -nargs=* -bang Xnext <mods><count>lnext<bang> <args>
66 command! -count -nargs=* -bang Xprev <mods><count>lprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020067 command! -nargs=* -bang Xfirst <mods>lfirst<bang> <args>
68 command! -nargs=* -bang Xlast <mods>llast<bang> <args>
Bram Moolenaar25190db2019-05-04 15:05:28 +020069 command! -count -nargs=* -bang Xnfile <mods><count>lnfile<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020070 command! -nargs=* -bang Xpfile <mods>lpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020071 command! -nargs=* Xexpr <mods>lexpr <args>
Bram Moolenaarf9ae1542019-11-18 22:02:16 +010072 command! -count -nargs=* Xvimgrep <mods> <count>lvimgrep <args>
73 command! -nargs=* Xvimgrepadd <mods> lvimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020074 command! -nargs=* Xgrep <mods> lgrep <args>
75 command! -nargs=* Xgrepadd <mods> lgrepadd <args>
76 command! -nargs=* Xhelpgrep lhelpgrep <args>
Bram Moolenaar74240d32017-12-10 15:26:15 +010077 command! -nargs=0 -count Xcc <count>ll
Bram Moolenaar3ff33112019-05-03 21:56:35 +020078 command! -count=1 -nargs=0 Xbelow <mods><count>lbelow
79 command! -count=1 -nargs=0 Xabove <mods><count>labove
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +020080 command! -count=1 -nargs=0 Xbefore <mods><count>lbefore
81 command! -count=1 -nargs=0 Xafter <mods><count>lafter
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020082 let g:Xgetlist = function('getloclist', [0])
83 let g:Xsetlist = function('setloclist', [0])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020084 call setloclist(0, [], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020085 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +010086endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020087
Bram Moolenaarda59dd52016-01-05 21:59:58 +010088" Tests for the :clist and :llist commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +010089func XlistTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020090 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +010091
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020092 if a:cchar == 'l'
93 call assert_fails('llist', 'E776:')
94 endif
Bram Moolenaarda59dd52016-01-05 21:59:58 +010095 " With an empty list, command should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020096 Xgetexpr []
97 silent! Xlist
Bram Moolenaarda59dd52016-01-05 21:59:58 +010098 call assert_true(v:errmsg ==# 'E42: No Errors')
99
100 " Populate the list and then try
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200101 Xgetexpr ['non-error 1', 'Xtestfile1:1:3:Line1',
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100102 \ 'non-error 2', 'Xtestfile2:2:2:Line2',
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100103 \ 'non-error| 3', 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100104
105 " List only valid entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100106 let l = split(execute('Xlist', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100107 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
108 \ ' 4 Xtestfile2:2 col 2: Line2',
109 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
110
111 " List all the entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100112 let l = split(execute('Xlist!', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100113 call assert_equal([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1',
114 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2',
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100115 \ ' 5: non-error| 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100116
117 " List a range of errors
Bram Moolenaaree85df32017-03-19 14:19:50 +0100118 let l = split(execute('Xlist 3,6', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100119 call assert_equal([' 4 Xtestfile2:2 col 2: Line2',
120 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
121
Bram Moolenaaree85df32017-03-19 14:19:50 +0100122 let l = split(execute('Xlist! 3,4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100123 call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
124
Bram Moolenaaree85df32017-03-19 14:19:50 +0100125 let l = split(execute('Xlist -6,-4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100126 call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l)
127
Bram Moolenaaree85df32017-03-19 14:19:50 +0100128 let l = split(execute('Xlist! -5,-3', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100129 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
130 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100131
132 " Test for '+'
Bram Moolenaaree85df32017-03-19 14:19:50 +0100133 let l = split(execute('Xlist! +2', ''), "\n")
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100134 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
135 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaaree85df32017-03-19 14:19:50 +0100136
137 " Different types of errors
138 call g:Xsetlist([{'lnum':10,'col':5,'type':'W', 'text':'Warning','nr':11},
139 \ {'lnum':20,'col':10,'type':'e','text':'Error','nr':22},
140 \ {'lnum':30,'col':15,'type':'i','text':'Info','nr':33},
141 \ {'lnum':40,'col':20,'type':'x', 'text':'Other','nr':44},
142 \ {'lnum':50,'col':25,'type':"\<C-A>",'text':'one','nr':55}])
143 let l = split(execute('Xlist', ""), "\n")
144 call assert_equal([' 1:10 col 5 warning 11: Warning',
145 \ ' 2:20 col 10 error 22: Error',
146 \ ' 3:30 col 15 info 33: Info',
147 \ ' 4:40 col 20 x 44: Other',
148 \ ' 5:50 col 25 55: one'], l)
149
Bram Moolenaard76ce852018-05-01 15:02:04 +0200150 " Test for module names, one needs to explicitly set `'valid':v:true` so
151 call g:Xsetlist([
152 \ {'lnum':10,'col':5,'type':'W','module':'Data.Text','text':'ModuleWarning','nr':11,'valid':v:true},
153 \ {'lnum':20,'col':10,'type':'W','module':'Data.Text','filename':'Data/Text.hs','text':'ModuleWarning','nr':22,'valid':v:true},
154 \ {'lnum':30,'col':15,'type':'W','filename':'Data/Text.hs','text':'FileWarning','nr':33,'valid':v:true}])
155 let l = split(execute('Xlist', ""), "\n")
156 call assert_equal([' 1 Data.Text:10 col 5 warning 11: ModuleWarning',
157 \ ' 2 Data.Text:20 col 10 warning 22: ModuleWarning',
158 \ ' 3 Data/Text.hs:30 col 15 warning 33: FileWarning'], l)
159
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100160 " For help entries in the quickfix list, only the filename without directory
161 " should be displayed
162 Xhelpgrep setqflist()
163 let l = split(execute('Xlist 1', ''), "\n")
164 call assert_match('^ 1 [^\\/]\{-}:', l[0])
165
Bram Moolenaaree85df32017-03-19 14:19:50 +0100166 " Error cases
167 call assert_fails('Xlist abc', 'E488:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100168endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100169
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100170func Test_clist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100171 call XlistTests('c')
172 call XlistTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100173endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100174
175" Tests for the :colder, :cnewer, :lolder and :lnewer commands
176" Note that this test assumes that a quickfix/location list is
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100177" already set by the caller.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100178func XageTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200179 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100180
Bram Moolenaar87f59b02019-04-04 14:04:11 +0200181 if a:cchar == 'l'
182 " No location list for the current window
183 call assert_fails('lolder', 'E776:')
184 call assert_fails('lnewer', 'E776:')
185 endif
186
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200187 let list = [{'bufnr': bufnr('%'), 'lnum': 1}]
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200188 call g:Xsetlist(list)
189
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100190 " Jumping to a non existent list should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200191 silent! Xolder 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100192 call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
193
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200194 silent! Xnewer 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100195 call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
196
197 " Add three quickfix/location lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200198 Xgetexpr ['Xtestfile1:1:3:Line1']
199 Xgetexpr ['Xtestfile2:2:2:Line2']
200 Xgetexpr ['Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100201
202 " Go back two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200203 Xolder
204 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100205 call assert_equal('Line2', l[0].text)
206
207 " Go forward two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200208 Xnewer
209 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100210 call assert_equal('Line3', l[0].text)
211
212 " Test for the optional count argument
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200213 Xolder 2
214 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100215 call assert_equal('Line1', l[0].text)
216
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200217 Xnewer 2
218 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100219 call assert_equal('Line3', l[0].text)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100220endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100221
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100222func Test_cage()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100223 call XageTests('c')
224 call XageTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100225endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100226
227" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
228" commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100229func XwindowTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200230 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100231
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200232 " Opening the location list window without any errors should fail
233 if a:cchar == 'l'
234 call assert_fails('lopen', 'E776:')
235 endif
236
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100237 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200238 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100239
240 " Quickfix/Location window should not open with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200241 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100242 call assert_true(winnr('$') == 1)
243
244 " Create a list with valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200245 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
246 \ 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100247
248 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200249 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100250 call assert_true(winnr('$') == 2 && winnr() == 2 &&
251 \ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
Bram Moolenaar537ef082016-07-09 17:56:19 +0200252 redraw!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100253
254 " Close the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200255 Xclose
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100256 call assert_true(winnr('$') == 1)
257
258 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200259 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100260
261 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200262 Xopen 5
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100263 call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
Bram Moolenaarc05d1c02020-09-04 18:38:06 +0200264 \ && winheight(0) == 5)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100265
266 " Opening the window again, should move the cursor to that window
267 wincmd t
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200268 Xopen 7
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100269 call assert_true(winnr('$') == 2 && winnr() == 2 &&
Bram Moolenaarc05d1c02020-09-04 18:38:06 +0200270 \ winheight(0) == 7 &&
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100271 \ getline('.') ==# '|| non-error 1')
272
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100273 " :cnext in quickfix window should move to the next entry
274 Xnext
275 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100276
277 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200278 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100279 call assert_true(winnr('$') == 1)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200280
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100281 " Specifying the width should adjust the width for a vertically split
282 " quickfix window.
283 vert Xopen
284 call assert_equal(10, winwidth(0))
285 vert Xopen 12
286 call assert_equal(12, winwidth(0))
287 Xclose
288
Bram Moolenaar5d3c9f82020-06-26 20:41:39 +0200289 " Horizontally or vertically splitting the quickfix window should create a
290 " normal window/buffer
291 Xopen
292 wincmd s
293 call assert_equal(0, getwininfo(win_getid())[0].quickfix)
294 call assert_equal(0, getwininfo(win_getid())[0].loclist)
295 call assert_notequal('quickfix', &buftype)
296 close
297 Xopen
298 wincmd v
299 call assert_equal(0, getwininfo(win_getid())[0].quickfix)
300 call assert_equal(0, getwininfo(win_getid())[0].loclist)
301 call assert_notequal('quickfix', &buftype)
302 close
303 Xopen
304 Xclose
305
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200306 if a:cchar == 'c'
307 " Opening the quickfix window in multiple tab pages should reuse the
308 " quickfix buffer
309 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
310 \ 'Xtestfile3:3:1:Line3']
311 Xopen
312 let qfbufnum = bufnr('%')
313 tabnew
314 Xopen
315 call assert_equal(qfbufnum, bufnr('%'))
316 new | only | tabonly
317 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100318endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100319
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100320func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100321 call XwindowTests('c')
322 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100323endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100324
Bram Moolenaar36d50222019-05-02 20:17:40 +0200325func Test_copenHeight()
326 copen
327 wincmd H
328 let height = winheight(0)
329 copen 10
330 call assert_equal(height, winheight(0))
331 quit
332endfunc
333
Bram Moolenaar1142a312019-10-16 14:51:39 +0200334func Test_copenHeight_tabline()
335 set tabline=foo showtabline=2
336 copen
337 wincmd H
338 let height = winheight(0)
339 copen 10
340 call assert_equal(height, winheight(0))
341 quit
342 set tabline& showtabline&
343endfunc
344
345
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100346" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
347" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100348func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200349 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100350
351 call writefile(['Xtestfile1:700:10:Line 700',
352 \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
353
354 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200355 Xfile Xqftestfile1
356 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100357 call assert_true(len(l) == 2 &&
358 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
359 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
360
Bram Moolenaar049cba92016-06-26 14:38:04 +0200361 " Test with a non existent file
Bram Moolenaare2e40752020-09-04 21:18:46 +0200362 call assert_fails('Xfile non_existent_file', 'E40:')
Bram Moolenaar049cba92016-06-26 14:38:04 +0200363
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100364 " Run cfile/lfile from a modified buffer
365 enew!
366 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200367 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100368 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
369
370 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200371 Xaddfile Xqftestfile1
372 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100373 call assert_true(len(l) == 3 &&
374 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
375
376 call writefile(['Xtestfile1:222:77:Line 222',
377 \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
378
379 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200380 Xgetfile Xqftestfile1
381 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100382 call assert_true(len(l) == 2 &&
383 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
384 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
385
Bram Moolenaar70077dd2019-11-24 12:12:42 +0100386 " Test for a file with a long line and without a newline at the end
387 let text = repeat('x', 1024)
388 let t = 'a.txt:18:' . text
389 call writefile([t], 'Xqftestfile1', 'b')
390 silent! Xfile Xqftestfile1
391 call assert_equal(text, g:Xgetlist()[0].text)
392
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100393 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100394endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100395
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100396func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100397 call XfileTests('c')
398 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100399endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100400
401" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
402" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100403func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200404 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100405
406 enew!
407 silent! call setline(1, ['Xtestfile7:700:10:Line 700',
408 \ 'Xtestfile8:800:15:Line 800'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200409 Xbuffer!
410 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100411 call assert_true(len(l) == 2 &&
412 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
413 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
414
415 enew!
416 silent! call setline(1, ['Xtestfile9:900:55:Line 900',
417 \ 'Xtestfile10:950:66:Line 950'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200418 Xgetbuffer
419 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100420 call assert_true(len(l) == 2 &&
421 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
422 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
423
424 enew!
425 silent! call setline(1, ['Xtestfile11:700:20:Line 700',
426 \ 'Xtestfile12:750:25:Line 750'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200427 Xaddbuffer
428 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100429 call assert_true(len(l) == 4 &&
430 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
431 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
432 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200433 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100434
Bram Moolenaaree85df32017-03-19 14:19:50 +0100435 " Check for invalid buffer
436 call assert_fails('Xbuffer 199', 'E474:')
437
438 " Check for unloaded buffer
439 edit Xtestfile1
440 let bnr = bufnr('%')
441 enew!
442 call assert_fails('Xbuffer ' . bnr, 'E681:')
443
444 " Check for invalid range
445 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
446 " commands. So directly call the commands.
447 if (a:cchar == 'c')
448 call assert_fails('900,999cbuffer', 'E16:')
449 else
450 call assert_fails('900,999lbuffer', 'E16:')
451 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100452endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100453
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100454func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100455 call XbufferTests('c')
456 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100457endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100458
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100459func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200460 call s:setup_commands(a:cchar)
461
462 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100463endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200464
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100465func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200466 call XexprTests('c')
467 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100468endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200469
470" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100471func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200472 call s:setup_commands(a:cchar)
473
Bram Moolenaar74240d32017-12-10 15:26:15 +0100474 call g:Xsetlist([], 'f')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200475 " Jumping to first or next location list entry without any error should
476 " result in failure
Bram Moolenaar74240d32017-12-10 15:26:15 +0100477 if a:cchar == 'c'
478 let err = 'E42:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100479 let cmd = '$cc'
Bram Moolenaar74240d32017-12-10 15:26:15 +0100480 else
481 let err = 'E776:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100482 let cmd = '$ll'
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200483 endif
Bram Moolenaar74240d32017-12-10 15:26:15 +0100484 call assert_fails('Xnext', err)
485 call assert_fails('Xprev', err)
486 call assert_fails('Xnfile', err)
487 call assert_fails('Xpfile', err)
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100488 call assert_fails(cmd, err)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200489
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100490 Xexpr ''
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100491 call assert_fails(cmd, 'E42:')
492
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200493 call s:create_test_file('Xqftestfile1')
494 call s:create_test_file('Xqftestfile2')
495
496 Xgetexpr ['Xqftestfile1:5:Line5',
497 \ 'Xqftestfile1:6:Line6',
498 \ 'Xqftestfile2:10:Line10',
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200499 \ 'Xqftestfile2:11:Line11',
500 \ 'RegularLine1',
501 \ 'RegularLine2']
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200502
503 Xfirst
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100504 call assert_fails('-5Xcc', 'E16:')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200505 call assert_fails('Xprev', 'E553:')
506 call assert_fails('Xpfile', 'E553:')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200507 Xnfile
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200508 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200509 call assert_equal(10, line('.'))
510 Xpfile
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200511 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200512 call assert_equal(6, line('.'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100513 5Xcc
514 call assert_equal(5, g:Xgetlist({'idx':0}).idx)
515 2Xcc
516 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100517 if a:cchar == 'c'
518 cc
519 else
520 ll
521 endif
522 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100523 10Xcc
524 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200525 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200526 Xprev
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200527 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200528 call assert_equal(11, line('.'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200529 call assert_fails('Xnext', 'E553:')
530 call assert_fails('Xnfile', 'E553:')
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100531 " To process the range using quickfix list entries, directly use the
532 " quickfix commands (don't use the user defined commands)
533 if a:cchar == 'c'
534 $cc
535 else
536 $ll
537 endif
538 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200539 Xrewind
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200540 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200541 call assert_equal(5, line('.'))
542
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200543 10Xnext
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200544 call assert_equal('Xqftestfile2', @%)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200545 call assert_equal(11, line('.'))
546 10Xprev
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200547 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200548 call assert_equal(5, line('.'))
549
Bram Moolenaar74240d32017-12-10 15:26:15 +0100550 " Jumping to an error from the error window using cc command
551 Xgetexpr ['Xqftestfile1:5:Line5',
552 \ 'Xqftestfile1:6:Line6',
553 \ 'Xqftestfile2:10:Line10',
554 \ 'Xqftestfile2:11:Line11']
555 Xopen
556 10Xcc
557 call assert_equal(11, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200558 call assert_equal('Xqftestfile2', @%)
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100559 Xopen
560 call cursor(2, 1)
561 if a:cchar == 'c'
562 .cc
563 else
564 .ll
565 endif
566 call assert_equal(6, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200567 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100568
569 " Jumping to an error from the error window (when only the error window is
570 " present)
571 Xopen | only
572 Xlast 1
573 call assert_equal(5, line('.'))
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200574 call assert_equal('Xqftestfile1', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100575
Bram Moolenaaree85df32017-03-19 14:19:50 +0100576 Xexpr ""
577 call assert_fails('Xnext', 'E42:')
578
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200579 call delete('Xqftestfile1')
580 call delete('Xqftestfile2')
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200581
582 " Should be able to use next/prev with invalid entries
583 Xexpr ""
584 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
585 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100586 Xaddexpr ['foo', 'bar', 'baz', 'quux', 'sh|moo']
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200587 call assert_equal(5, g:Xgetlist({'size' : 0}).size)
588 Xlast
589 call assert_equal(5, g:Xgetlist({'idx' : 0}).idx)
590 Xfirst
591 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
592 2Xnext
593 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100594endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200595
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100596func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200597 call Xtest_browse('c')
598 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100599endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200600
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100601func Test_nomem()
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200602 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100603 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100604
Bram Moolenaarce90e362019-09-08 18:58:44 +0200605 call GetAllocId('qf_dirname_now')->test_alloc_fail(0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100606 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100607
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200608 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100609 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100610
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200611 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100612 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100613
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200614 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100615 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100616
617endfunc
618
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100619func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200620 call s:setup_commands(a:cchar)
621 Xhelpgrep quickfix
622 Xopen
623 if a:cchar == 'c'
624 let title_text = ':helpgrep quickfix'
625 else
626 let title_text = ':lhelpgrep quickfix'
627 endif
628 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200629
630 " Jumping to a help topic should open the help window
631 only
632 Xnext
633 call assert_true(&buftype == 'help')
634 call assert_true(winnr('$') == 2)
635 " Jumping to the next match should reuse the help window
636 Xnext
637 call assert_true(&buftype == 'help')
638 call assert_true(winnr() == 1)
639 call assert_true(winnr('$') == 2)
640 " Jumping to the next match from the quickfix window should reuse the help
641 " window
642 Xopen
643 Xnext
644 call assert_true(&buftype == 'help')
645 call assert_true(winnr() == 1)
646 call assert_true(winnr('$') == 2)
647
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100648 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200649 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100650
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100651 " When the current window is vertically split, jumping to a help match
652 " should open the help window at the top.
653 only | enew
654 let w1 = win_getid()
655 vert new
656 let w2 = win_getid()
657 Xnext
658 let w3 = win_getid()
659 call assert_true(&buftype == 'help')
660 call assert_true(winnr() == 1)
Bram Moolenaara106e6c2019-11-24 22:13:58 +0100661 " See jump_to_help_window() for details
662 let w2_width = winwidth(w2)
663 if w2_width != &columns && w2_width < 80
664 call assert_equal(['col', [['leaf', w3],
665 \ ['row', [['leaf', w2], ['leaf', w1]]]]], winlayout())
666 else
667 call assert_equal(['row', [['col', [['leaf', w3], ['leaf', w2]]],
668 \ ['leaf', w1]]] , winlayout())
669 endif
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100670
671 new | only
672 set buftype=help
673 set modified
674 call assert_fails('Xnext', 'E37:')
675 set nomodified
676 new | only
677
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200678 if a:cchar == 'l'
679 " When a help window is present, running :lhelpgrep should reuse the
680 " help window and not the current window
681 new | only
682 call g:Xsetlist([], 'f')
683 help index.txt
684 wincmd w
685 lhelpgrep quickfix
686 call assert_equal(1, winnr())
687 call assert_notequal([], getloclist(1))
688 call assert_equal([], getloclist(2))
689 endif
690
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200691 new | only
692
Bram Moolenaaree85df32017-03-19 14:19:50 +0100693 " Search for non existing help string
694 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar108e7b42018-10-11 17:39:12 +0200695 " Invalid regular expression
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200696 call assert_fails('Xhelpgrep \@<!', 'E866:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100697endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200698
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100699func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200700 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200701 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200702 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100703endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100704
Bram Moolenaar6920c722016-01-22 22:44:10 +0100705func Test_errortitle()
706 augroup QfBufWinEnter
707 au!
708 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
709 augroup END
710 copen
711 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'')'}]
712 call setqflist(a)
713 call assert_equal(':setqflist()', g:a)
714 augroup QfBufWinEnter
715 au!
716 augroup END
717 augroup! QfBufWinEnter
718endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100719
Bram Moolenaar5584df62016-03-18 21:00:51 +0100720func Test_vimgreptitle()
721 augroup QfBufWinEnter
722 au!
723 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
724 augroup END
725 try
726 vimgrep /pattern/j file
727 catch /E480/
728 endtry
729 copen
730 call assert_equal(': vimgrep /pattern/j file', g:a)
731 augroup QfBufWinEnter
732 au!
733 augroup END
734 augroup! QfBufWinEnter
735endfunc
736
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100737func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200738 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +0100739
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200740 Xgetexpr ['file:1:1:message']
741 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100742 if a:cchar == 'c'
743 call setqflist(l, 'r')
744 else
745 call setloclist(0, l, 'r')
746 endif
747
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200748 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +0100749 if a:cchar == 'c'
750 let title = ':setqflist()'
751 else
752 let title = ':setloclist()'
753 endif
754 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200755 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100756endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100757
758" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100759func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200760 call XqfTitleTests('c')
761 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100762endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100763
764" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100765func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100766 let save_efm = &efm
767 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
768 cgetexpr ['WWWW', 'EEEE', 'CCCC']
769 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
770 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
771 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
772 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
773 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
774 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
775 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
776 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
777 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100778endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100779
780" This will test for problems in quickfix:
781" A. incorrectly copying location lists which caused the location list to show
782" a different name than the file that was actually being displayed.
783" B. not reusing the window for which the location list window is opened but
784" instead creating new windows.
785" C. make sure that the location list window is not reused instead of the
786" window it belongs to.
787"
788" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100789func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100790 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
791 let word = substitute(base, '\v(.*)\..*', '\1', '')
792
793 setl modifiable
794 setl noreadonly
795 setl noswapfile
796 setl bufhidden=delete
797 %del _
798 " For problem 2:
799 " 'buftype' has to be set to reproduce the constant opening of new windows
800 setl buftype=nofile
801
802 call setline(1, word)
803
804 setl nomodified
805 setl nomodifiable
806 setl readonly
807 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100808endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100809
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100810func Test_locationlist()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200811 enew
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100812
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200813 augroup testgroup
814 au!
815 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
816 augroup END
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100817
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200818 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100819
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200820 let qflist = []
821 for word in words
822 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
823 " NOTE: problem 1:
824 " intentionally not setting 'lnum' so that the quickfix entries are not
825 " valid
826 eval qflist->setloclist(0, ' ')
827 endfor
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100828
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200829 " Test A
830 lrewind
831 enew
832 lopen
833 4lnext
834 vert split
835 wincmd L
836 lopen
837 wincmd p
838 lnext
839 let fileName = expand("%")
840 wincmd p
841 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
842 let fileName = substitute(fileName, '\\', '/', 'g')
843 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
844 call assert_equal("test://bar.txt", fileName)
845 call assert_equal("test://bar.txt", locationListFileName)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100846
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200847 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100848
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200849 " Test B:
850 lrewind
851 lopen
852 2
853 exe "normal \<CR>"
854 wincmd p
855 3
856 exe "normal \<CR>"
857 wincmd p
858 4
859 exe "normal \<CR>"
860 call assert_equal(2, winnr('$'))
861 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100862
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200863 " Test C:
864 lrewind
865 lopen
866 " Let's move the location list window to the top to check whether it (the
867 " first window found) will be reused when we try to open new windows:
868 wincmd K
869 2
870 exe "normal \<CR>"
871 wincmd p
872 3
873 exe "normal \<CR>"
874 wincmd p
875 4
876 exe "normal \<CR>"
877 1wincmd w
878 call assert_equal('quickfix', &buftype)
879 2wincmd w
880 let bufferName = expand("%")
881 let bufferName = substitute(bufferName, '\\', '/', 'g')
882 call assert_equal('test://quux.txt', bufferName)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100883
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200884 wincmd n | only
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100885
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200886 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100887endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +0100888
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100889func Test_locationlist_curwin_was_closed()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200890 augroup testgroup
891 au!
892 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
893 augroup END
Bram Moolenaar0899d692016-03-19 13:35:03 +0100894
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200895 func! R(n)
896 quit
897 endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +0100898
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200899 new
900 let q = []
901 call add(q, {'filename': 'test_curwin.txt' })
902 call setloclist(0, q)
903 call assert_fails('lrewind', 'E924:')
Bram Moolenaar0899d692016-03-19 13:35:03 +0100904
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200905 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100906endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100907
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100908func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200909 call writefile(['loclistfoo'], 'loclistfoo')
910 call writefile(['loclistbar'], 'loclistbar')
911 set switchbuf=usetab
912
913 edit loclistfoo
914 tabedit loclistbar
915 silent lgrep loclistfoo loclist*
916 call assert_equal(1, tabpagenr())
917
918 enew | only | tabonly
919 set switchbuf&vim
920 call delete('loclistfoo')
921 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100922endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200923
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100924" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100925func Test_efm1()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200926 " The 'errorformat' setting is different on non-Unix systems.
927 " This test works only on Unix-like systems.
928 CheckUnix
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100929
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200930 let l =<< trim [DATA]
931 "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.
932 "Xtestfile", line 6 col 19; this is an error
933 gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c
934 Xtestfile:9: parse error before `asd'
935 make: *** [vim] Error 1
936 in file "Xtestfile" linenr 10: there is an error
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100937
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200938 2 returned
939 "Xtestfile", line 11 col 1; this is an error
940 "Xtestfile", line 12 col 2; this is another error
941 "Xtestfile", line 14:10; this is an error in column 10
942 =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time
943 "Xtestfile", linenr 16: yet another problem
944 Error in "Xtestfile" at line 17:
945 x should be a dot
946 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
947 ^
948 Error in "Xtestfile" at line 18:
949 x should be a dot
950 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
951 .............^
952 Error in "Xtestfile" at line 19:
953 x should be a dot
954 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
955 --------------^
956 Error in "Xtestfile" at line 20:
957 x should be a dot
958 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
959 ^
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100960
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200961 Does anyone know what is the problem and how to correction it?
962 "Xtestfile", line 21 col 9: What is the title of the quickfix window?
963 "Xtestfile", line 22 col 9: What is the title of the quickfix window?
964 [DATA]
965
966 call writefile(l, 'Xerrorfile1')
967 call writefile(l[:-2], 'Xerrorfile2')
968
969 let m =<< [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200970 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2
971 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3
972 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4
973 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5
974 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6
975 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7
976 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8
977 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9
978 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10
979 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11
980 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12
981 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13
982 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14
983 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15
984 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16
985 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
986 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
987 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
988 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
989 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21
990 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22
991[DATA]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200992 call writefile(m, 'Xtestfile')
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100993
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200994 let save_efm = &efm
995 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
996 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100997
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200998 exe 'cf Xerrorfile2'
999 clast
1000 copen
1001 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
1002 wincmd p
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001003
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001004 exe 'cf Xerrorfile1'
1005 call assert_equal([4, 12], [line('.'), col('.')])
1006 cn
1007 call assert_equal([6, 19], [line('.'), col('.')])
1008 cn
1009 call assert_equal([9, 2], [line('.'), col('.')])
1010 cn
1011 call assert_equal([10, 2], [line('.'), col('.')])
1012 cn
1013 call assert_equal([11, 1], [line('.'), col('.')])
1014 cn
1015 call assert_equal([12, 2], [line('.'), col('.')])
1016 cn
1017 call assert_equal([14, 10], [line('.'), col('.')])
1018 cn
1019 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
1020 cn
1021 call assert_equal([16, 2], [line('.'), col('.')])
1022 cn
1023 call assert_equal([17, 6], [line('.'), col('.')])
1024 cn
1025 call assert_equal([18, 7], [line('.'), col('.')])
1026 cn
1027 call assert_equal([19, 8], [line('.'), col('.')])
1028 cn
1029 call assert_equal([20, 9], [line('.'), col('.')])
1030 clast
1031 cprev
1032 cprev
1033 wincmd w
1034 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
1035 wincmd p
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01001036
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001037 let &efm = save_efm
1038 call delete('Xerrorfile1')
1039 call delete('Xerrorfile2')
1040 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +01001041endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001042
Bram Moolenaarab47c612016-06-14 22:02:26 +02001043" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001044func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001045 call s:setup_commands(a:cchar)
1046
Bram Moolenaarab47c612016-06-14 22:02:26 +02001047 let save_efm=&efm
1048 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1049
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001050 let lines = ["Entering dir 'dir1/a'",
1051 \ 'habits2.txt:1:Nine Healthy Habits',
1052 \ "Entering dir 'b'",
1053 \ 'habits3.txt:2:0 Hours of television',
1054 \ 'habits2.txt:7:5 Small meals',
1055 \ "Entering dir 'dir1/c'",
1056 \ 'habits4.txt:3:1 Hour of exercise',
1057 \ "Leaving dir 'dir1/c'",
1058 \ "Leaving dir 'dir1/a'",
1059 \ 'habits1.txt:4:2 Liters of water',
1060 \ "Entering dir 'dir2'",
1061 \ 'habits5.txt:5:3 Cups of hot green tea',
1062 \ "Leaving dir 'dir2'"
1063 \]
Bram Moolenaarab47c612016-06-14 22:02:26 +02001064
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001065 Xexpr ""
1066 for l in lines
1067 Xaddexpr l
1068 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +02001069
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001070 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001071
1072 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
1073 call assert_equal(1, qf[1].lnum)
1074 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
1075 call assert_equal(2, qf[3].lnum)
1076 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
1077 call assert_equal(7, qf[4].lnum)
1078 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
1079 call assert_equal(3, qf[6].lnum)
1080 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
1081 call assert_equal(4, qf[9].lnum)
1082 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
1083 call assert_equal(5, qf[11].lnum)
1084
1085 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001086endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001087
1088" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001089func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001090 " Create the directory stack and files
1091 call mkdir('dir1')
1092 call mkdir('dir1/a')
1093 call mkdir('dir1/a/b')
1094 call mkdir('dir1/c')
1095 call mkdir('dir2')
1096
1097 let lines = ["Nine Healthy Habits",
1098 \ "0 Hours of television",
1099 \ "1 Hour of exercise",
1100 \ "2 Liters of water",
1101 \ "3 Cups of hot green tea",
1102 \ "4 Short mental breaks",
1103 \ "5 Small meals",
1104 \ "6 AM wake up time",
1105 \ "7 Minutes of laughter",
1106 \ "8 Hours of sleep (at least)",
1107 \ "9 PM end of the day and off to bed"
1108 \ ]
1109 call writefile(lines, 'habits1.txt')
1110 call writefile(lines, 'dir1/a/habits2.txt')
1111 call writefile(lines, 'dir1/a/b/habits3.txt')
1112 call writefile(lines, 'dir1/c/habits4.txt')
1113 call writefile(lines, 'dir2/habits5.txt')
1114
1115 call s:dir_stack_tests('c')
1116 call s:dir_stack_tests('l')
1117
1118 call delete('dir1', 'rf')
1119 call delete('dir2', 'rf')
1120 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001121endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001122
Bram Moolenaar9b457942016-10-09 16:10:05 +02001123" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001124func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001125 call s:setup_commands(a:cchar)
1126
1127 let save_efm = &efm
1128
1129 let &efm =
1130 \ '%Eerror %m %l,' .
1131 \ '%-Wignored %m %l,' .
1132 \ '%+Cmore ignored %m %l,' .
1133 \ '%Zignored end'
1134 Xgetexpr ['ignored warning 1', 'more ignored continuation 2', 'ignored end', 'error resync 4']
1135 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
1136 call assert_equal([['resync', 1, 4, 'E']], l)
1137
1138 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001139endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001140
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001141func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +02001142 call Xefm_ignore_continuations('c')
1143 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001144endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001145
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001146" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001147func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001148 call s:setup_commands(a:cchar)
1149
Bram Moolenaar049cba92016-06-26 14:38:04 +02001150 let save_efm = &efm
1151
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001152 set efm=%f:%l:%m,%f:%f:%l:%m
1153 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
1154
1155 set efm=%f:%l:%m,%f:%l:%r:%m
1156 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1157
1158 set efm=%f:%l:%m,%O:%f:%l:%m
1159 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1160
1161 set efm=%f:%l:%m,%f:%l:%*[^a-z
1162 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
1163
1164 set efm=%f:%l:%m,%f:%l:%*c
1165 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
1166
1167 set efm=%f:%l:%m,%L%M%N
1168 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
1169
1170 set efm=%f:%l:%m,%f:%l:%m:%R
1171 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
1172
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001173 " Invalid regular expression
1174 set efm=%\\%%k
1175 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E867:')
1176
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001177 set efm=
1178 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
1179
1180 set efm=%DEntering\ dir\ abc,%f:%l:%m
1181 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
1182
1183 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001184endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001185
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001186func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001187 call Xinvalid_efm_Tests('c')
1188 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001189endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001190
1191" TODO:
1192" Add tests for the following formats in 'errorformat'
1193" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001194func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001195 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001196
1197 " Test for %s format in efm
1198 set efm=%f:%s
1199 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001200 let l = getqflist()
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001201 call assert_equal('^\VLine search text\$', l[0].pattern)
1202 call assert_equal(0, l[0].lnum)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001203
Bram Moolenaaree85df32017-03-19 14:19:50 +01001204 let l = split(execute('clist', ''), "\n")
1205 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1206
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001207 " Test for a long line
1208 cexpr 'Xtestfile:' . repeat('a', 1026)
1209 let l = getqflist()
1210 call assert_equal('^\V' . repeat('a', 1019) . '\$', l[0].pattern)
1211
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001212 " Test for %P, %Q and %t format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001213 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001214 [Xtestfile1]
1215 (1,17) error: ';' missing
1216 (21,2) warning: variable 'z' not defined
1217 (67,3) error: end of file found before string ended
1218 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001219
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001220 [Xtestfile2]
1221 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001222
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001223 [Xtestfile3]
1224 NEW compiler v1.1
1225 (2,2) warning: variable 'x' not defined
1226 (67,3) warning: 's' already defined
1227 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001228 [DATA]
1229
Bram Moolenaaree85df32017-03-19 14:19:50 +01001230 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001231 " To exercise the push/pop file functionality in quickfix, the test files
1232 " need to be created.
1233 call writefile(['Line1'], 'Xtestfile1')
1234 call writefile(['Line2'], 'Xtestfile2')
1235 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001236 cexpr ""
1237 for l in lines
1238 caddexpr l
1239 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001240 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001241 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001242 call assert_equal(21, l[2].lnum)
1243 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001244 call assert_equal('w', l[2].type)
1245 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001246 call delete('Xtestfile1')
1247 call delete('Xtestfile2')
1248 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001249
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001250 " Test for %P, %Q with non-existing files
1251 cexpr lines
1252 let l = getqflist()
1253 call assert_equal(14, len(l))
1254 call assert_equal('[Xtestfile1]', l[0].text)
1255 call assert_equal('[Xtestfile2]', l[6].text)
1256 call assert_equal('[Xtestfile3]', l[9].text)
1257
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001258 " Tests for %E, %C and %Z format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001259 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001260 Error 275
1261 line 42
1262 column 3
1263 ' ' expected after '--'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001264 [DATA]
1265
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001266 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1267 cgetexpr lines
1268 let l = getqflist()
1269 call assert_equal(275, l[0].nr)
1270 call assert_equal(42, l[0].lnum)
1271 call assert_equal(3, l[0].col)
1272 call assert_equal('E', l[0].type)
1273 call assert_equal("\n' ' expected after '--'", l[0].text)
1274
1275 " Test for %>
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001276 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001277 Error in line 147 of foo.c:
1278 unknown variable 'i'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001279 [DATA]
1280
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001281 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1282 cgetexpr lines
1283 let l = getqflist()
1284 call assert_equal(147, l[0].lnum)
1285 call assert_equal('E', l[0].type)
1286 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001287
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001288 " Test for %A, %C and other formats
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001289 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001290 ==============================================================
1291 FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)
1292 --------------------------------------------------------------
1293 Traceback (most recent call last):
1294 File "unittests/dbfacadeTest.py", line 89, in testFoo
1295 self.assertEquals(34, dtid)
1296 File "/usr/lib/python2.2/unittest.py", line 286, in
1297 failUnlessEqual
1298 raise self.failureException, \\
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001299 W:AssertionError: 34 != 33
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001300
1301 --------------------------------------------------------------
1302 Ran 27 tests in 0.063s
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001303 [DATA]
1304
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001305 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%t:%m
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001306 cgetexpr lines
1307 let l = getqflist()
1308 call assert_equal(8, len(l))
1309 call assert_equal(89, l[4].lnum)
1310 call assert_equal(1, l[4].valid)
1311 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001312 call assert_equal('W', l[4].type)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001313
Bram Moolenaard76ce852018-05-01 15:02:04 +02001314 " Test for %o
1315 set efm=%f(%o):%l\ %m
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001316 cgetexpr ['Xotestfile(Language.PureScript.Types):20 Error']
1317 call writefile(['Line1'], 'Xotestfile')
Bram Moolenaard76ce852018-05-01 15:02:04 +02001318 let l = getqflist()
1319 call assert_equal(1, len(l), string(l))
1320 call assert_equal('Language.PureScript.Types', l[0].module)
1321 copen
1322 call assert_equal('Language.PureScript.Types|20| Error', getline(1))
1323 call feedkeys("\<CR>", 'xn')
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001324 call assert_equal('Xotestfile', expand('%:t'))
Bram Moolenaard76ce852018-05-01 15:02:04 +02001325 cclose
1326 bd
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001327 call delete("Xotestfile")
Bram Moolenaard76ce852018-05-01 15:02:04 +02001328
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001329 " Test for a long module name
1330 cexpr 'Xtest(' . repeat('m', 1026) . '):15 message'
1331 let l = getqflist()
1332 call assert_equal(repeat('m', 1024), l[0].module)
1333 call assert_equal(15, l[0].lnum)
1334 call assert_equal('message', l[0].text)
1335
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001336 " The following sequence of commands used to crash Vim
1337 set efm=%W%m
1338 cgetexpr ['msg1']
1339 let l = getqflist()
1340 call assert_equal(1, len(l), string(l))
1341 call assert_equal('msg1', l[0].text)
1342 set efm=%C%m
1343 lexpr 'msg2'
1344 let l = getloclist(0)
1345 call assert_equal(1, len(l), string(l))
1346 call assert_equal('msg2', l[0].text)
1347 lopen
1348 call setqflist([], 'r')
1349 caddbuf
1350 let l = getqflist()
1351 call assert_equal(1, len(l), string(l))
1352 call assert_equal('|| msg2', l[0].text)
1353
Bram Moolenaar78ddc062018-05-15 21:56:34 +02001354 " When matching error lines, case should be ignored. Test for this.
1355 set noignorecase
1356 let l=getqflist({'lines' : ['Xtest:FOO10:Line 20'], 'efm':'%f:foo%l:%m'})
1357 call assert_equal(10, l.items[0].lnum)
1358 call assert_equal('Line 20', l.items[0].text)
1359 set ignorecase&
1360
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001361 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001362 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001363endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001364
Bram Moolenaare9283662020-06-07 14:10:47 +02001365" Test for '%t' (error type) field in 'efm'
1366func Test_efm_error_type()
1367 let save_efm = &efm
1368
1369 " error type
1370 set efm=%f:%l:%t:%m
1371 cexpr ["Xfile1:10:E:msg1", "Xfile1:20:W:msg2", "Xfile1:30:I:msg3",
1372 \ "Xfile1:40:N:msg4", "Xfile1:50:R:msg5"]
1373 let output = split(execute('clist'), "\n")
1374 call assert_equal([
1375 \ ' 1 Xfile1:10 error: msg1',
1376 \ ' 2 Xfile1:20 warning: msg2',
1377 \ ' 3 Xfile1:30 info: msg3',
1378 \ ' 4 Xfile1:40 note: msg4',
1379 \ ' 5 Xfile1:50 R: msg5'], output)
1380
1381 " error type and a error number
1382 set efm=%f:%l:%t:%n:%m
1383 cexpr ["Xfile1:10:E:2:msg1", "Xfile1:20:W:4:msg2", "Xfile1:30:I:6:msg3",
1384 \ "Xfile1:40:N:8:msg4", "Xfile1:50:R:3:msg5"]
1385 let output = split(execute('clist'), "\n")
1386 call assert_equal([
1387 \ ' 1 Xfile1:10 error 2: msg1',
1388 \ ' 2 Xfile1:20 warning 4: msg2',
1389 \ ' 3 Xfile1:30 info 6: msg3',
1390 \ ' 4 Xfile1:40 note 8: msg4',
1391 \ ' 5 Xfile1:50 R 3: msg5'], output)
1392 let &efm = save_efm
1393endfunc
1394
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001395func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001396 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001397 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001398 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001399 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001400 colder
1401 cgetexpr []
1402 endfunc
1403 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001404 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001405 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001406 lolder
1407 lgetexpr []
1408 endfunc
1409 endif
1410
1411 augroup testgroup
1412 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001413 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001414 augroup END
1415
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001416 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001417 let words = [ "a", "b" ]
1418 let qflist = []
1419 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001420 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001421 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001422 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001423 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001424
1425 augroup! testgroup
1426endfunc
1427
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001428func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001429 call XquickfixChangedByAutocmd('c')
1430 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001431endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001432
Bram Moolenaar4d170af2020-09-13 22:21:22 +02001433func Test_setloclist_in_autocommand()
1434 call writefile(['test1', 'test2'], 'Xfile')
1435 edit Xfile
1436 let s:bufnr = bufnr()
1437 call setloclist(1,
1438 \ [{'bufnr' : s:bufnr, 'lnum' : 1, 'text' : 'test1'},
1439 \ {'bufnr' : s:bufnr, 'lnum' : 2, 'text' : 'test2'}])
1440
1441 augroup Test_LocList
1442 au!
1443 autocmd BufEnter * call setloclist(1,
1444 \ [{'bufnr' : s:bufnr, 'lnum' : 1, 'text' : 'test1'},
1445 \ {'bufnr' : s:bufnr, 'lnum' : 2, 'text' : 'test2'}], 'r')
1446 augroup END
1447
1448 lopen
1449 call assert_fails('exe "normal j\<CR>"', 'E926:')
1450
1451 augroup Test_LocList
1452 au!
1453 augroup END
1454 call delete('Xfile')
1455endfunc
1456
Bram Moolenaar8b201792016-03-25 15:01:10 +01001457func Test_caddbuffer_to_empty()
1458 helpgr quickfix
1459 call setqflist([], 'r')
1460 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001461 try
1462 cn
1463 catch
1464 " number of matches is unknown
1465 call assert_true(v:exception =~ 'E553:')
1466 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001467 quit!
1468endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001469
1470func Test_cgetexpr_works()
1471 " this must not crash Vim
1472 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001473 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001474endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001475
1476" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001477func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001478 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001479
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001480 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001481 \ {'bufnr': a:bnum, 'lnum': 2}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001482 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001483 call assert_equal(2, len(l))
1484 call assert_equal(2, l[1].lnum)
1485
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001486 Xnext
1487 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1488 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001489 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001490 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001491 call assert_equal(3, line('.'))
1492
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001493 " Appending entries to the list should not change the cursor position
1494 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001495 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001496 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001497 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001498 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1499 call assert_equal(1, line('.'))
1500 close
1501
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001502 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001503 \ {'bufnr': a:bnum, 'lnum': 4},
1504 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001505 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001506 call assert_equal(3, len(l))
1507 call assert_equal(5, l[2].lnum)
1508
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001509 call g:Xsetlist([])
1510 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001511 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001512
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001513 " Tests for setting the 'valid' flag
1514 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1515 Xwindow
1516 call assert_equal(1, winnr('$'))
1517 let l = g:Xgetlist()
1518 call g:Xsetlist(l)
1519 call assert_equal(0, g:Xgetlist()[0].valid)
Bram Moolenaar9752c722018-12-22 16:49:34 +01001520 " Adding a non-valid entry should not mark the list as having valid entries
1521 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':5, 'valid':0}], 'a')
1522 Xwindow
1523 call assert_equal(1, winnr('$'))
1524
1525 " :cnext/:cprev should still work even with invalid entries in the list
1526 let l = [{'bufnr' : a:bnum, 'lnum' : 1, 'text' : '1', 'valid' : 0},
1527 \ {'bufnr' : a:bnum, 'lnum' : 2, 'text' : '2', 'valid' : 0}]
1528 call g:Xsetlist(l)
1529 Xnext
1530 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1531 Xprev
1532 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1533 " :cnext/:cprev should still work after appending invalid entries to an
1534 " empty list
1535 call g:Xsetlist([])
1536 call g:Xsetlist(l, 'a')
1537 Xnext
1538 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1539 Xprev
1540 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1541
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001542 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1543 Xwindow
1544 call assert_equal(2, winnr('$'))
1545 Xclose
1546 let save_efm = &efm
1547 set efm=%m
1548 Xgetexpr 'TestMessage'
1549 let l = g:Xgetlist()
1550 call g:Xsetlist(l)
1551 call assert_equal(1, g:Xgetlist()[0].valid)
1552 let &efm = save_efm
1553
Bram Moolenaaree85df32017-03-19 14:19:50 +01001554 " Error cases:
1555 " Refer to a non-existing buffer and pass a non-dictionary type
1556 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1557 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1558 call g:Xsetlist([[1, 2,3]])
1559 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar99fa7212020-04-26 15:59:55 +02001560 call assert_fails('call g:Xsetlist([], [])', 'E928:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001561endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001562
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001563func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001564 new Xtestfile | only
1565 let bnum = bufnr('%')
1566 call setline(1, range(1,5))
1567
1568 call SetXlistTests('c', bnum)
1569 call SetXlistTests('l', bnum)
1570
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001571 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001572 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001573endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001574
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001575func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001576 call s:setup_commands(a:cchar)
1577
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001578 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001579 let @/ = 'Test_'
1580 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001581 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001582 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001583 Xvimgrep empty test_quickfix.vim
1584 call assert_true(len(g:Xgetlist()) > 0)
1585 Xvimgrep matches test_quickfix.vim
1586 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001587 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001588 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001589 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001590 call g:Xsetlist([], 'r')
1591 call assert_true(len(g:Xgetlist()) == 0)
1592 Xolder
1593 call assert_equal(testlen, len(g:Xgetlist()))
1594 Xnewer
1595 Xnewer
1596 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001597endfunc
1598
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001599func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001600 call Xlist_empty_middle('c')
1601 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001602endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001603
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001604func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001605 call s:setup_commands(a:cchar)
1606
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001607 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001608 Xvimgrep one test_quickfix.vim
1609 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001610 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001611 Xvimgrep two test_quickfix.vim
1612 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001613 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001614 Xvimgrep three test_quickfix.vim
1615 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001616 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001617 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001618 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001619 call g:Xsetlist([], 'r')
1620 call assert_true(len(g:Xgetlist()) == 0)
1621 Xnewer
1622 call assert_equal(twolen, len(g:Xgetlist()))
1623 Xnewer
1624 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001625endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001626
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001627func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001628 call Xlist_empty_older('c')
1629 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001630endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001631
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001632func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001633 call s:setup_commands(a:cchar)
1634
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001635 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1636 \ {'filename': 'fnameB', 'text': 'B'}]
1637 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1638 \ {'filename': 'fnameD', 'text': 'D'},
1639 \ {'filename': 'fnameE', 'text': 'E'}]
1640
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001641 " {action} is unspecified. Same as specifying ' '.
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001642 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001643 silent! Xnewer 99
1644 call g:Xsetlist(list1)
1645 call g:Xsetlist(list2)
1646 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001647 call assert_equal(3, len(li))
1648 call assert_equal('C', li[0]['text'])
1649 call assert_equal('D', li[1]['text'])
1650 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001651 silent! Xolder
1652 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001653 call assert_equal(2, len(li))
1654 call assert_equal('A', li[0]['text'])
1655 call assert_equal('B', li[1]['text'])
1656
1657 " {action} is specified ' '.
1658 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001659 silent! Xnewer 99
1660 call g:Xsetlist(list1)
1661 call g:Xsetlist(list2, ' ')
1662 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001663 call assert_equal(3, len(li))
1664 call assert_equal('C', li[0]['text'])
1665 call assert_equal('D', li[1]['text'])
1666 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001667 silent! Xolder
1668 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001669 call assert_equal(2, len(li))
1670 call assert_equal('A', li[0]['text'])
1671 call assert_equal('B', li[1]['text'])
1672
1673 " {action} is specified 'a'.
1674 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001675 silent! Xnewer 99
1676 call g:Xsetlist(list1)
1677 call g:Xsetlist(list2, 'a')
1678 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001679 call assert_equal(5, len(li))
1680 call assert_equal('A', li[0]['text'])
1681 call assert_equal('B', li[1]['text'])
1682 call assert_equal('C', li[2]['text'])
1683 call assert_equal('D', li[3]['text'])
1684 call assert_equal('E', li[4]['text'])
1685
1686 " {action} is specified 'r'.
1687 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001688 silent! Xnewer 99
1689 call g:Xsetlist(list1)
1690 call g:Xsetlist(list2, 'r')
1691 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001692 call assert_equal(3, len(li))
1693 call assert_equal('C', li[0]['text'])
1694 call assert_equal('D', li[1]['text'])
1695 call assert_equal('E', li[2]['text'])
1696
1697 " Test for wrong value.
1698 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001699 call assert_fails("call g:Xsetlist(0)", 'E714:')
1700 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
1701 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
1702 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
1703 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001704endfunc
1705
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001706func Test_setqflist_invalid_nr()
1707 " The following command used to crash Vim
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001708 eval []->setqflist(' ', {'nr' : $XXX_DOES_NOT_EXIST})
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001709endfunc
1710
Bram Moolenaar99234f22020-02-10 22:56:54 +01001711func Test_setqflist_user_sets_buftype()
1712 call setqflist([{'text': 'foo'}, {'text': 'bar'}])
1713 set buftype=quickfix
1714 call setqflist([], 'a')
1715 enew
1716endfunc
1717
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001718func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001719 call XquickfixSetListWithAct('c')
1720 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001721endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001722
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001723func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001724 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001725
Bram Moolenaar049cba92016-06-26 14:38:04 +02001726 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001727 call assert_equal(1, l[0].lnum)
1728 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001729 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001730 call assert_equal(2, l[1].lnum)
1731 call assert_equal(1, l[1].col)
1732 call assert_equal(4070, len(l[1].text))
1733 call assert_equal(3, l[2].lnum)
1734 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001735 call assert_equal(4070, len(l[2].text))
1736 call assert_equal(4, l[3].lnum)
1737 call assert_equal(1, l[3].col)
1738 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001739
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001740 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001741endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001742
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001743func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001744 call s:setup_commands(a:cchar)
1745
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001746 let testfile = 'samples/quickfix.txt'
1747
1748 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001749 exe 'Xgetfile' testfile
1750 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001751
1752 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001753 Xexpr readfile(testfile)
1754 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001755
1756 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001757 Xexpr join(readfile(testfile), "\n")
1758 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001759
1760 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001761 exe 'edit' testfile
1762 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02001763 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001764endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001765
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001766func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001767 call s:long_lines_tests('c')
1768 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001769endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001770
Bram Moolenaar59941cb2020-09-05 17:03:40 +02001771func Test_cgetfile_on_long_lines()
1772 " Problematic values if the line is longer than 4096 bytes. Then 1024 bytes
1773 " are read at a time.
1774 for len in [4078, 4079, 4080, 5102, 5103, 5104, 6126, 6127, 6128, 7150, 7151, 7152]
1775 let lines = [
1776 \ '/tmp/file1:1:1:aaa',
1777 \ '/tmp/file2:1:1:%s',
1778 \ '/tmp/file3:1:1:bbb',
1779 \ '/tmp/file4:1:1:ccc',
1780 \ ]
1781 let lines[1] = substitute(lines[1], '%s', repeat('x', len), '')
1782 call writefile(lines, 'Xcqetfile.txt')
1783 cgetfile Xcqetfile.txt
1784 call assert_equal(4, getqflist(#{size: v:true}).size, 'with length ' .. len)
1785 endfor
1786 call delete('Xcqetfile.txt')
1787endfunc
1788
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001789func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001790 let l = []
1791 for i in range(1, 20)
1792 call add(l, 'Line' . i)
1793 endfor
1794 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001795endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001796
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001797func Test_switchbuf()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001798 call s:create_test_file('Xqftestfile1')
1799 call s:create_test_file('Xqftestfile2')
1800 call s:create_test_file('Xqftestfile3')
1801
1802 new | only
1803 edit Xqftestfile1
1804 let file1_winid = win_getid()
1805 new Xqftestfile2
1806 let file2_winid = win_getid()
1807 cgetexpr ['Xqftestfile1:5:Line5',
1808 \ 'Xqftestfile1:6:Line6',
1809 \ 'Xqftestfile2:10:Line10',
1810 \ 'Xqftestfile2:11:Line11',
1811 \ 'Xqftestfile3:15:Line15',
1812 \ 'Xqftestfile3:16:Line16']
1813
1814 new
1815 let winid = win_getid()
1816 cfirst | cnext
1817 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001818 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001819 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001820 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001821 call assert_equal(winid, win_getid())
Bram Moolenaar049cba92016-06-26 14:38:04 +02001822
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001823 " Test for 'switchbuf' set to search for files in windows in the current
1824 " tabpage and jump to an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001825 set switchbuf=useopen
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001826 enew
Bram Moolenaar049cba92016-06-26 14:38:04 +02001827 cfirst | cnext
1828 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001829 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001830 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001831 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001832 call assert_equal(file2_winid, win_getid())
1833
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001834 " Test for 'switchbuf' set to search for files in tabpages and jump to an
1835 " existing tabpage (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001836 enew | only
1837 set switchbuf=usetab
1838 tabedit Xqftestfile1
1839 tabedit Xqftestfile2
Bram Moolenaar74240d32017-12-10 15:26:15 +01001840 tabedit Xqftestfile3
Bram Moolenaar049cba92016-06-26 14:38:04 +02001841 tabfirst
1842 cfirst | cnext
1843 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001844 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001845 call assert_equal(3, tabpagenr())
Bram Moolenaar74240d32017-12-10 15:26:15 +01001846 6cnext
1847 call assert_equal(4, tabpagenr())
1848 2cpfile
1849 call assert_equal(2, tabpagenr())
1850 2cnfile
1851 call assert_equal(4, tabpagenr())
Bram Moolenaar049cba92016-06-26 14:38:04 +02001852 tabfirst | tabonly | enew
1853
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001854 " Test for 'switchbuf' set to open a new window for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02001855 set switchbuf=split
1856 cfirst | cnext
1857 call assert_equal(1, winnr('$'))
1858 cnext | cnext
1859 call assert_equal(2, winnr('$'))
1860 cnext | cnext
1861 call assert_equal(3, winnr('$'))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001862
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001863 " Test for 'switchbuf' set to open a new tabpage for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02001864 set switchbuf=newtab
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001865 enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001866 cfirst | cnext
1867 call assert_equal(1, tabpagenr('$'))
1868 cnext | cnext
1869 call assert_equal(2, tabpagenr('$'))
1870 cnext | cnext
1871 call assert_equal(3, tabpagenr('$'))
1872 tabfirst | enew | tabonly | only
1873
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01001874 set switchbuf=uselast
1875 split
1876 let last_winid = win_getid()
1877 copen
1878 exe "normal 1G\<CR>"
1879 call assert_equal(last_winid, win_getid())
1880 enew | only
1881
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001882 " With an empty 'switchbuf', jumping to a quickfix entry should open the
1883 " file in an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001884 set switchbuf=
1885 edit Xqftestfile1
1886 let file1_winid = win_getid()
1887 new Xqftestfile2
1888 let file2_winid = win_getid()
1889 copen
1890 exe "normal 1G\<CR>"
1891 call assert_equal(file1_winid, win_getid())
1892 copen
1893 exe "normal 3G\<CR>"
1894 call assert_equal(file2_winid, win_getid())
1895 copen | only
1896 exe "normal 5G\<CR>"
1897 call assert_equal(2, winnr('$'))
1898 call assert_equal(1, bufwinnr('Xqftestfile3'))
1899
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001900 " If only quickfix window is open in the current tabpage, jumping to an
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001901 " entry with 'switchbuf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02001902 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001903 set switchbuf=usetab
1904 tabedit Xqftestfile1
1905 tabedit Xqftestfile2
1906 tabedit Xqftestfile3
1907 tabfirst
1908 copen | only
1909 clast
1910 call assert_equal(4, tabpagenr())
1911 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001912
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001913 " Jumping to a file that is not present in any of the tabpages and the
1914 " current tabpage doesn't have any usable windows, should open it in a new
1915 " window in the current tabpage.
1916 copen | only
1917 cfirst
1918 call assert_equal(1, tabpagenr())
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02001919 call assert_equal('Xqftestfile1', @%)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001920
1921 " If opening a file changes 'switchbuf', then the new value should be
1922 " retained.
Bram Moolenaar41d42992020-05-03 16:29:50 +02001923 set modeline&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001924 call writefile(["vim: switchbuf=split"], 'Xqftestfile1')
1925 enew | only
1926 set switchbuf&vim
1927 cexpr "Xqftestfile1:1:10"
1928 call assert_equal('split', &switchbuf)
1929 call writefile(["vim: switchbuf=usetab"], 'Xqftestfile1')
1930 enew | only
1931 set switchbuf=useopen
1932 cexpr "Xqftestfile1:1:10"
1933 call assert_equal('usetab', &switchbuf)
1934 call writefile(["vim: switchbuf&vim"], 'Xqftestfile1')
1935 enew | only
1936 set switchbuf=useopen
1937 cexpr "Xqftestfile1:1:10"
1938 call assert_equal('', &switchbuf)
1939
Bram Moolenaar049cba92016-06-26 14:38:04 +02001940 call delete('Xqftestfile1')
1941 call delete('Xqftestfile2')
1942 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001943 set switchbuf&vim
1944
1945 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001946endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001947
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001948func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001949 call s:setup_commands(a:cchar)
1950
1951 enew | only
1952
Bram Moolenaarc1542742016-07-20 21:44:37 +02001953 let fname = 'Xqftestfile' . a:cchar
1954 call s:create_test_file(fname)
1955 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02001956
Bram Moolenaarc1542742016-07-20 21:44:37 +02001957 Xgetexpr [fname . ':5:Line5',
1958 \ fname . ':10:Line10',
1959 \ fname . ':15:Line15',
1960 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02001961
1962 6,14delete
1963 call append(6, ['Buffer', 'Window'])
1964
1965 let l = g:Xgetlist()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001966 call assert_equal(5, l[0].lnum)
1967 call assert_equal(6, l[2].lnum)
1968 call assert_equal(13, l[3].lnum)
1969
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001970 " If a file doesn't have any quickfix entries, then deleting lines in the
1971 " file should not update the quickfix list
1972 call g:Xsetlist([], 'f')
1973 1,2delete
1974 call assert_equal([], g:Xgetlist())
1975
Bram Moolenaar049cba92016-06-26 14:38:04 +02001976 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02001977 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001978endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001979
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001980func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02001981 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001982 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02001983 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001984 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001985endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001986
1987" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001988func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001989 call s:setup_commands(a:cchar)
1990
1991 " The following lines are used for the grep test. Don't remove.
1992 " Grep_Test_Text: Match 1
1993 " Grep_Test_Text: Match 2
1994 " GrepAdd_Test_Text: Match 1
1995 " GrepAdd_Test_Text: Match 2
1996 enew! | only
1997 set makeef&vim
1998 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01001999 call assert_true(len(g:Xgetlist()) == 5)
Bram Moolenaar049cba92016-06-26 14:38:04 +02002000 Xopen
2001 call assert_true(w:quickfix_title =~ '^:grep')
2002 Xclose
2003 enew
2004 set makeef=Temp_File_##
2005 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002006 call assert_true(len(g:Xgetlist()) == 9)
2007
2008 " Try with 'grepprg' set to 'internal'
2009 set grepprg=internal
2010 silent Xgrep Grep_Test_Text: test_quickfix.vim
2011 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
2012 call assert_true(len(g:Xgetlist()) == 9)
2013 set grepprg&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002014
2015 call writefile(['Vim'], 'XtestTempFile')
2016 set makeef=XtestTempFile
2017 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002018 call assert_equal(5, len(g:Xgetlist()))
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002019 call assert_false(filereadable('XtestTempFile'))
2020 set makeef&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002021endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02002022
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002023func Test_grep()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002024 " The grepprg may not be set on non-Unix systems
2025 CheckUnix
Bram Moolenaar049cba92016-06-26 14:38:04 +02002026
2027 call s:test_xgrep('c')
2028 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002029endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002030
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002031func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002032 " Use one 'errorformat' for two windows. Add an expression to each of them,
2033 " make sure they each keep their own state.
2034 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
2035 call mkdir('Xone/a', 'p')
2036 call mkdir('Xtwo/a', 'p')
2037 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2038 call writefile(lines, 'Xone/a/one.txt')
2039 call writefile(lines, 'Xtwo/a/two.txt')
2040
2041 new one
2042 let one_id = win_getid()
2043 lexpr ""
2044 new two
2045 let two_id = win_getid()
2046 lexpr ""
2047
2048 laddexpr "Entering dir 'Xtwo/a'"
2049 call win_gotoid(one_id)
2050 laddexpr "Entering dir 'Xone/a'"
2051 call win_gotoid(two_id)
2052 laddexpr 'two.txt:5:two two two'
2053 call win_gotoid(one_id)
2054 laddexpr 'one.txt:3:one one one'
2055
2056 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002057 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
2058 call assert_equal(3, loc_one[1].lnum)
2059
2060 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02002061 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
2062 call assert_equal(5, loc_two[1].lnum)
2063
2064 call win_gotoid(one_id)
2065 bwipe!
2066 call win_gotoid(two_id)
2067 bwipe!
2068 call delete('Xone', 'rf')
2069 call delete('Xtwo', 'rf')
2070endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02002071
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002072func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02002073 call s:setup_commands(a:cchar)
2074
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002075 " Calling lbottom without any errors should fail
2076 if a:cchar == 'l'
2077 call assert_fails('lbottom', 'E776:')
2078 endif
2079
Bram Moolenaar875feea2017-06-11 16:07:51 +02002080 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02002081 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02002082 let wid = win_getid()
2083 call assert_equal(1, line('.'))
2084 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02002085 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02002086 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02002087 call win_gotoid(wid)
2088 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02002089 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02002090endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02002091
2092" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002093func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02002094 call XbottomTests('c')
2095 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002096endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002097
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002098func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002099 call s:setup_commands(a:cchar)
2100
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002101 " clear all lists after the first one, then replace the first one.
2102 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002103 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002104 let entry = {'filename': 'foo', 'lnum': 42}
2105 call g:Xsetlist([entry], 'r')
2106 call g:Xsetlist([entry, entry])
2107 call g:Xsetlist([entry, entry, entry])
2108 let res = split(execute(a:cchar . 'hist'), "\n")
2109 call assert_equal(3, len(res))
2110 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
2111 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
2112 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
2113 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002114
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002115 " Test for changing the quickfix lists
2116 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2117 exe '1' . a:cchar . 'hist'
2118 call assert_equal(1, g:Xgetlist({'nr' : 0}).nr)
2119 exe '3' . a:cchar . 'hist'
2120 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2121 call assert_fails('-2' . a:cchar . 'hist', 'E16:')
2122 call assert_fails('4' . a:cchar . 'hist', 'E16:')
2123
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002124 call g:Xsetlist([], 'f')
2125 let l = split(execute(a:cchar . 'hist'), "\n")
2126 call assert_equal('No entries', l[0])
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002127 if a:cchar == 'c'
2128 call assert_fails('4chist', 'E16:')
2129 else
2130 call assert_fails('4lhist', 'E776:')
2131 endif
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002132
2133 " An empty list should still show the stack history
2134 call g:Xsetlist([])
2135 let res = split(execute(a:cchar . 'hist'), "\n")
2136 call assert_equal('> error list 1 of 1; 0 ' . common, res[0])
2137
2138 call g:Xsetlist([], 'f')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002139endfunc
2140
2141func Test_history()
2142 call HistoryTest('c')
2143 call HistoryTest('l')
2144endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02002145
2146func Test_duplicate_buf()
2147 " make sure we can get the highest buffer number
2148 edit DoesNotExist
2149 edit DoesNotExist2
2150 let last_buffer = bufnr("$")
2151
2152 " make sure only one buffer is created
2153 call writefile(['this one', 'that one'], 'Xgrepthis')
2154 vimgrep one Xgrepthis
2155 vimgrep one Xgrepthis
2156 call assert_equal(last_buffer + 1, bufnr("$"))
2157
2158 call delete('Xgrepthis')
2159endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002160
2161" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002162func Xproperty_tests(cchar)
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002163 call s:setup_commands(a:cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002164
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002165 " Error cases
2166 call assert_fails('call g:Xgetlist(99)', 'E715:')
2167 call assert_fails('call g:Xsetlist(99)', 'E714:')
2168 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
Bram Moolenaard823fa92016-08-12 16:29:27 +02002169
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002170 " Set and get the title
2171 call g:Xsetlist([])
2172 Xopen
2173 wincmd p
2174 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
2175 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
2176 call assert_equal(0, s)
2177 let d = g:Xgetlist({"title":1})
2178 call assert_equal('Sample', d.title)
2179 " Try setting title to a non-string value
2180 call assert_equal(-1, g:Xsetlist([], 'a', {'title' : ['Test']}))
2181 call assert_equal('Sample', g:Xgetlist({"title":1}).title)
2182
2183 Xopen
2184 call assert_equal('Sample', w:quickfix_title)
2185 Xclose
2186
2187 " Tests for action argument
2188 silent! Xolder 999
2189 let qfnr = g:Xgetlist({'all':1}).nr
2190 call g:Xsetlist([], 'r', {'title' : 'N1'})
2191 call assert_equal('N1', g:Xgetlist({'all':1}).title)
2192 call g:Xsetlist([], ' ', {'title' : 'N2'})
2193 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
2194
2195 let res = g:Xgetlist({'nr': 0})
2196 call assert_equal(qfnr + 1, res.nr)
2197 call assert_equal(['nr'], keys(res))
2198
2199 call g:Xsetlist([], ' ', {'title' : 'N3'})
2200 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
2201
2202 " Changing the title of an earlier quickfix list
2203 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
2204 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
2205
2206 " Changing the title of an invalid quickfix list
2207 call assert_equal(-1, g:Xsetlist([], ' ',
2208 \ {'title' : 'SomeTitle', 'nr' : 99}))
2209 call assert_equal(-1, g:Xsetlist([], ' ',
2210 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
2211
2212 if a:cchar == 'c'
2213 copen
2214 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
2215 cclose
2216 endif
2217
2218 " Invalid arguments
Bram Moolenaare2e40752020-09-04 21:18:46 +02002219 call assert_fails('call g:Xgetlist([])', 'E715:')
2220 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002221 let s = g:Xsetlist([], 'a', {'abc':1})
2222 call assert_equal(-1, s)
2223
2224 call assert_equal({}, g:Xgetlist({'abc':1}))
2225 call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
2226 call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
2227
2228 if a:cchar == 'l'
2229 call assert_equal({}, getloclist(99, {'title': 1}))
2230 endif
2231
2232 " Context related tests
2233 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
2234 call assert_equal(0, s)
2235 call test_garbagecollect_now()
2236 let d = g:Xgetlist({'context':1})
2237 call assert_equal([1,2,3], d.context)
2238 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
2239 let d = g:Xgetlist({'context':1})
2240 call assert_equal({'color':'green'}, d.context)
2241 call g:Xsetlist([], 'a', {'context':"Context info"})
2242 let d = g:Xgetlist({'context':1})
2243 call assert_equal("Context info", d.context)
2244 call g:Xsetlist([], 'a', {'context':246})
2245 let d = g:Xgetlist({'context':1})
2246 call assert_equal(246, d.context)
2247 " set other Vim data types as context
2248 call g:Xsetlist([], 'a', {'context' : test_null_blob()})
2249 if has('channel')
2250 call g:Xsetlist([], 'a', {'context' : test_null_channel()})
2251 endif
2252 if has('job')
2253 call g:Xsetlist([], 'a', {'context' : test_null_job()})
2254 endif
2255 call g:Xsetlist([], 'a', {'context' : test_null_function()})
2256 call g:Xsetlist([], 'a', {'context' : test_null_partial()})
2257 call g:Xsetlist([], 'a', {'context' : ''})
2258 call test_garbagecollect_now()
2259 if a:cchar == 'l'
2260 " Test for copying context across two different location lists
2261 new | only
2262 let w1_id = win_getid()
2263 let l = [1]
2264 call setloclist(0, [], 'a', {'context':l})
2265 new
2266 let w2_id = win_getid()
2267 call add(l, 2)
2268 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
2269 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2270 unlet! l
2271 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2272 only
2273 call setloclist(0, [], 'f')
2274 call assert_equal('', getloclist(0, {'context':1}).context)
2275 endif
2276
2277 " Test for changing the context of previous quickfix lists
2278 call g:Xsetlist([], 'f')
2279 Xexpr "One"
2280 Xexpr "Two"
2281 Xexpr "Three"
2282 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
2283 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
2284 " Also, check for setting the context using quickfix list number zero.
2285 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
2286 call test_garbagecollect_now()
2287 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
2288 call assert_equal([1], l.context)
2289 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
2290 call assert_equal([2], l.context)
2291 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
2292 call assert_equal([3], l.context)
2293
2294 " Test for changing the context through reference and for garbage
2295 " collection of quickfix context
2296 let l = ["red"]
2297 call g:Xsetlist([], ' ', {'context' : l})
2298 call add(l, "blue")
2299 let x = g:Xgetlist({'context' : 1})
2300 call add(x.context, "green")
2301 call assert_equal(["red", "blue", "green"], l)
2302 call assert_equal(["red", "blue", "green"], x.context)
2303 unlet l
2304 call test_garbagecollect_now()
2305 let m = g:Xgetlist({'context' : 1})
2306 call assert_equal(["red", "blue", "green"], m.context)
2307
2308 " Test for setting/getting items
2309 Xexpr ""
2310 let qfprev = g:Xgetlist({'nr':0})
2311 let s = g:Xsetlist([], ' ', {'title':'Green',
2312 \ 'items' : [{'filename':'F1', 'lnum':10}]})
2313 call assert_equal(0, s)
2314 let qfcur = g:Xgetlist({'nr':0})
2315 call assert_true(qfcur.nr == qfprev.nr + 1)
2316 let l = g:Xgetlist({'items':1})
2317 call assert_equal('F1', bufname(l.items[0].bufnr))
2318 call assert_equal(10, l.items[0].lnum)
2319 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
2320 \ {'filename':'F2', 'lnum':30}]})
2321 let l = g:Xgetlist({'items':1})
2322 call assert_equal('F2', bufname(l.items[2].bufnr))
2323 call assert_equal(30, l.items[2].lnum)
2324 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
2325 let l = g:Xgetlist({'items':1})
2326 call assert_equal('F3', bufname(l.items[0].bufnr))
2327 call assert_equal(40, l.items[0].lnum)
2328 call g:Xsetlist([], 'r', {'items' : []})
2329 let l = g:Xgetlist({'items':1})
2330 call assert_equal(0, len(l.items))
2331
2332 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
2333 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2334 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2335 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
2336
2337 " Test for getting id of window associated with a location list window
2338 if a:cchar == 'l'
2339 only
2340 call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid)
2341 let wid = win_getid()
Bram Moolenaard823fa92016-08-12 16:29:27 +02002342 Xopen
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002343 call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid)
2344 wincmd w
2345 call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid)
2346 only
2347 endif
Bram Moolenaard823fa92016-08-12 16:29:27 +02002348
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002349 " The following used to crash Vim with address sanitizer
2350 call g:Xsetlist([], 'f')
2351 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
2352 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002353
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002354 " Try setting the items using a string
2355 call assert_equal(-1, g:Xsetlist([], ' ', {'items' : 'Test'}))
Bram Moolenaar890680c2016-09-27 21:28:56 +02002356
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002357 " Save and restore the quickfix stack
2358 call g:Xsetlist([], 'f')
2359 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2360 Xexpr "File1:10:Line1"
2361 Xexpr "File2:20:Line2"
2362 Xexpr "File3:30:Line3"
2363 let last_qf = g:Xgetlist({'nr':'$'}).nr
2364 call assert_equal(3, last_qf)
2365 let qstack = []
2366 for i in range(1, last_qf)
2367 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
2368 endfor
2369 call g:Xsetlist([], 'f')
2370 for i in range(len(qstack))
2371 call g:Xsetlist([], ' ', qstack[i])
2372 endfor
2373 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2374 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
2375 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
2376 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
2377 call g:Xsetlist([], 'f')
Bram Moolenaar890680c2016-09-27 21:28:56 +02002378
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002379 " Swap two quickfix lists
2380 Xexpr "File1:10:Line10"
2381 Xexpr "File2:20:Line20"
2382 Xexpr "File3:30:Line30"
2383 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
2384 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
2385 let l1=g:Xgetlist({'nr':1,'all':1})
2386 let l2=g:Xgetlist({'nr':2,'all':1})
2387 let save_id = l1.id
2388 let l1.id=l2.id
2389 let l2.id=save_id
2390 call g:Xsetlist([], 'r', l1)
2391 call g:Xsetlist([], 'r', l2)
2392 let newl1=g:Xgetlist({'nr':1,'all':1})
2393 let newl2=g:Xgetlist({'nr':2,'all':1})
2394 call assert_equal('Fruits', newl1.title)
2395 call assert_equal(['Fruits'], newl1.context)
2396 call assert_equal('Line20', newl1.items[0].text)
2397 call assert_equal('Colors', newl2.title)
2398 call assert_equal(['Colors'], newl2.context)
2399 call assert_equal('Line10', newl2.items[0].text)
2400 call g:Xsetlist([], 'f')
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02002401
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002402 " Cannot specify both a non-empty list argument and a dict argument
2403 call assert_fails("call g:Xsetlist([{}], ' ', {})", 'E475:')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002404endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002405
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002406func Test_qf_property()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002407 call Xproperty_tests('c')
2408 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002409endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002410
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002411" Test for setting the current index in the location/quickfix list
2412func Xtest_setqfidx(cchar)
2413 call s:setup_commands(a:cchar)
2414
2415 Xgetexpr "F1:10:1:Line1\nF2:20:2:Line2\nF3:30:3:Line3"
2416 Xgetexpr "F4:10:1:Line1\nF5:20:2:Line2\nF6:30:3:Line3"
2417 Xgetexpr "F7:10:1:Line1\nF8:20:2:Line2\nF9:30:3:Line3"
2418
2419 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 2})
2420 call g:Xsetlist([], 'a', {'nr' : 2, 'idx' : 2})
2421 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 3})
2422 Xolder 2
2423 Xopen
2424 call assert_equal(3, line('.'))
2425 Xnewer
2426 call assert_equal(2, line('.'))
2427 Xnewer
2428 call assert_equal(2, line('.'))
2429 " Update the current index with the quickfix window open
2430 wincmd w
2431 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 3})
2432 Xopen
2433 call assert_equal(3, line('.'))
2434 Xclose
2435
2436 " Set the current index to the last entry
2437 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : '$'})
2438 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2439 " A large value should set the index to the last index
2440 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 1})
2441 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 999})
2442 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2443 " Invalid index values
2444 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : -1})
2445 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2446 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 0})
2447 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2448 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 'xx'})
2449 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2450 call assert_fails("call g:Xsetlist([], 'a', {'nr':1, 'idx':[]})", 'E745:')
2451
2452 call g:Xsetlist([], 'f')
2453 new | only
2454endfunc
2455
2456func Test_setqfidx()
2457 call Xtest_setqfidx('c')
2458 call Xtest_setqfidx('l')
2459endfunc
2460
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002461" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002462func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002463 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002464endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002465
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002466func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002467 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
2468 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
2469
2470 let g:acmds = []
2471 cexpr "F1:10:Line 10"
2472 caddexpr "F1:20:Line 20"
2473 cgetexpr "F1:30:Line 30"
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002474 cexpr ""
2475 caddexpr ""
2476 cgetexpr ""
2477 silent! cexpr non_existing_func()
2478 silent! caddexpr non_existing_func()
2479 silent! cgetexpr non_existing_func()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002480 let l = ['precexpr',
2481 \ 'postcexpr',
2482 \ 'precaddexpr',
2483 \ 'postcaddexpr',
2484 \ 'precgetexpr',
2485 \ 'postcgetexpr',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002486 \ 'precexpr',
2487 \ 'postcexpr',
2488 \ 'precaddexpr',
2489 \ 'postcaddexpr',
2490 \ 'precgetexpr',
2491 \ 'postcgetexpr',
2492 \ 'precexpr',
2493 \ 'precaddexpr',
2494 \ 'precgetexpr']
2495 call assert_equal(l, g:acmds)
2496
2497 let g:acmds = []
2498 enew! | call append(0, "F2:10:Line 10")
2499 cbuffer!
2500 enew! | call append(0, "F2:20:Line 20")
2501 cgetbuffer
2502 enew! | call append(0, "F2:30:Line 30")
2503 caddbuffer
2504 new
2505 let bnum = bufnr('%')
2506 bunload
2507 exe 'silent! cbuffer! ' . bnum
2508 exe 'silent! cgetbuffer ' . bnum
2509 exe 'silent! caddbuffer ' . bnum
2510 enew!
2511 let l = ['precbuffer',
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002512 \ 'postcbuffer',
2513 \ 'precgetbuffer',
2514 \ 'postcgetbuffer',
2515 \ 'precaddbuffer',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002516 \ 'postcaddbuffer',
2517 \ 'precbuffer',
2518 \ 'precgetbuffer',
2519 \ 'precaddbuffer']
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002520 call assert_equal(l, g:acmds)
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002521
2522 call writefile(['Xtest:1:Line1'], 'Xtest')
2523 call writefile([], 'Xempty')
2524 let g:acmds = []
2525 cfile Xtest
2526 caddfile Xtest
2527 cgetfile Xtest
2528 cfile Xempty
2529 caddfile Xempty
2530 cgetfile Xempty
2531 silent! cfile do_not_exist
2532 silent! caddfile do_not_exist
2533 silent! cgetfile do_not_exist
2534 let l = ['precfile',
2535 \ 'postcfile',
2536 \ 'precaddfile',
2537 \ 'postcaddfile',
2538 \ 'precgetfile',
2539 \ 'postcgetfile',
2540 \ 'precfile',
2541 \ 'postcfile',
2542 \ 'precaddfile',
2543 \ 'postcaddfile',
2544 \ 'precgetfile',
2545 \ 'postcgetfile',
2546 \ 'precfile',
2547 \ 'postcfile',
2548 \ 'precaddfile',
2549 \ 'postcaddfile',
2550 \ 'precgetfile',
2551 \ 'postcgetfile']
2552 call assert_equal(l, g:acmds)
2553
2554 let g:acmds = []
2555 helpgrep quickfix
2556 silent! helpgrep non_existing_help_topic
2557 vimgrep test Xtest
2558 vimgrepadd test Xtest
2559 silent! vimgrep non_existing_test Xtest
2560 silent! vimgrepadd non_existing_test Xtest
2561 set makeprg=
2562 silent! make
2563 set makeprg&
2564 let l = ['prehelpgrep',
2565 \ 'posthelpgrep',
2566 \ 'prehelpgrep',
2567 \ 'posthelpgrep',
2568 \ 'previmgrep',
2569 \ 'postvimgrep',
2570 \ 'previmgrepadd',
2571 \ 'postvimgrepadd',
2572 \ 'previmgrep',
2573 \ 'postvimgrep',
2574 \ 'previmgrepadd',
2575 \ 'postvimgrepadd',
2576 \ 'premake',
2577 \ 'postmake']
2578 call assert_equal(l, g:acmds)
2579
2580 if has('unix')
2581 " Run this test only on Unix-like systems. The grepprg may not be set on
2582 " non-Unix systems.
2583 " The following lines are used for the grep test. Don't remove.
2584 " Grep_Autocmd_Text: Match 1
2585 " GrepAdd_Autocmd_Text: Match 2
2586 let g:acmds = []
2587 silent grep Grep_Autocmd_Text test_quickfix.vim
2588 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2589 silent grep abc123def Xtest
2590 silent grepadd abc123def Xtest
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002591 set grepprg=internal
2592 silent grep Grep_Autocmd_Text test_quickfix.vim
2593 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2594 silent lgrep Grep_Autocmd_Text test_quickfix.vim
2595 silent lgrepadd GrepAdd_Autocmd_Text test_quickfix.vim
2596 set grepprg&vim
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002597 let l = ['pregrep',
2598 \ 'postgrep',
2599 \ 'pregrepadd',
2600 \ 'postgrepadd',
2601 \ 'pregrep',
2602 \ 'postgrep',
2603 \ 'pregrepadd',
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002604 \ 'postgrepadd',
2605 \ 'pregrep',
2606 \ 'postgrep',
2607 \ 'pregrepadd',
2608 \ 'postgrepadd',
2609 \ 'prelgrep',
2610 \ 'postlgrep',
2611 \ 'prelgrepadd',
2612 \ 'postlgrepadd']
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002613 call assert_equal(l, g:acmds)
2614 endif
2615
2616 call delete('Xtest')
2617 call delete('Xempty')
Bram Moolenaarb254af32017-12-18 19:48:58 +01002618 au! QuickFixCmdPre
2619 au! QuickFixCmdPost
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002620endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01002621
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002622func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01002623 set efm=%m
2624 lgetexpr '?'
2625
2626 try
2627 call DoesNotExit()
2628 catch
2629 lgetexpr '1'
2630 finally
2631 lgetexpr '1'
2632 endtry
2633
2634 call assert_equal('1', getloclist(0)[0].text)
2635
2636 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002637endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002638
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002639func Test_caddbuffer_wrong()
2640 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002641 let save_efm = &efm
2642 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
2643 cgetexpr ['WWWW', 'EEEE', 'CCCC']
2644 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002645 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002646 bwipe!
2647endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002648
2649func Test_caddexpr_wrong()
2650 " This used to cause a memory access in freed memory.
2651 cbuffer
2652 cbuffer
2653 copen
2654 let save_efm = &efm
2655 set efm=%
2656 call assert_fails('caddexpr ""', 'E376:')
2657 let &efm = save_efm
2658endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01002659
2660func Test_dirstack_cleanup()
2661 " This used to cause a memory access in freed memory.
2662 let save_efm = &efm
2663 lexpr '0'
2664 lopen
2665 fun X(c)
2666 let save_efm=&efm
2667 set efm=%D%f
2668 if a:c == 'c'
2669 caddexpr '::'
2670 else
2671 laddexpr ':0:0'
2672 endif
2673 let &efm=save_efm
2674 endfun
2675 call X('c')
2676 call X('l')
2677 call setqflist([], 'r')
2678 caddbuffer
2679 let &efm = save_efm
2680endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002681
2682" Tests for jumping to entries from the location list window and quickfix
2683" window
2684func Test_cwindow_jump()
2685 set efm=%f%%%l%%%m
2686 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2687 lopen | only
2688 lfirst
2689 call assert_true(winnr('$') == 2)
2690 call assert_true(winnr() == 1)
2691 " Location list for the new window should be set
2692 call assert_true(getloclist(0)[2].text == 'Line 30')
2693
2694 " Open a scratch buffer
2695 " Open a new window and create a location list
2696 " Open the location list window and close the other window
2697 " Jump to an entry.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002698 " Should create a new window and jump to the entry. The scratch buffer
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002699 " should not be used.
2700 enew | only
2701 set buftype=nofile
2702 below new
2703 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2704 lopen
2705 2wincmd c
2706 lnext
2707 call assert_true(winnr('$') == 3)
2708 call assert_true(winnr() == 2)
2709
2710 " Open two windows with two different location lists
2711 " Open the location list window and close the previous window
2712 " Jump to an entry in the location list window
2713 " Should open the file in the first window and not set the location list.
2714 enew | only
2715 lgetexpr ["F1%5%Line 5"]
2716 below new
2717 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2718 lopen
2719 2wincmd c
2720 lnext
2721 call assert_true(winnr() == 1)
2722 call assert_true(getloclist(0)[0].text == 'Line 5')
2723
2724 enew | only
2725 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2726 copen
2727 cnext
2728 call assert_true(winnr('$') == 2)
2729 call assert_true(winnr() == 1)
2730
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002731 " open the quickfix buffer in two windows and jump to an entry. Should open
2732 " the file in the first quickfix window.
2733 enew | only
2734 copen
2735 let bnum = bufnr('')
2736 exe 'sbuffer ' . bnum
2737 wincmd b
2738 cfirst
2739 call assert_equal(2, winnr())
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02002740 call assert_equal('F1', @%)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002741 enew | only
2742 exe 'sb' bnum
2743 exe 'botright sb' bnum
2744 wincmd t
2745 clast
2746 call assert_equal(2, winnr())
2747 call assert_equal('quickfix', getwinvar(1, '&buftype'))
2748 call assert_equal('quickfix', getwinvar(3, '&buftype'))
2749
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002750 " Jumping to a file from the location list window should find a usable
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002751 " window by wrapping around the window list.
2752 enew | only
2753 call setloclist(0, [], 'f')
2754 new | new
2755 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2756 lopen
2757 1close
2758 call assert_equal(0, getloclist(3, {'id' : 0}).id)
2759 lnext
2760 call assert_equal(3, winnr())
2761 call assert_equal(getloclist(1, {'id' : 0}).id, getloclist(3, {'id' : 0}).id)
2762
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002763 enew | only
2764 set efm&vim
2765endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01002766
Bram Moolenaare00fdf32019-09-15 19:09:42 +02002767func Test_cwindow_highlight()
2768 CheckScreendump
2769
2770 let lines =<< trim END
Bram Moolenaare00fdf32019-09-15 19:09:42 +02002771 call setline(1, ['some', 'text', 'with', 'matches'])
2772 write XCwindow
2773 vimgrep e XCwindow
2774 redraw
2775 cwindow 4
2776 END
2777 call writefile(lines, 'XtestCwindow')
2778 let buf = RunVimInTerminal('-S XtestCwindow', #{rows: 12})
2779 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_1', {})
2780
2781 call term_sendkeys(buf, ":cnext\<CR>")
2782 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_2', {})
2783
2784 " clean up
2785 call StopVimInTerminal(buf)
2786 call delete('XtestCwindow')
2787 call delete('XCwindow')
2788endfunc
2789
Bram Moolenaaree85df32017-03-19 14:19:50 +01002790func XvimgrepTests(cchar)
2791 call s:setup_commands(a:cchar)
2792
2793 call writefile(['Editor:VIM vim',
2794 \ 'Editor:Emacs EmAcS',
2795 \ 'Editor:Notepad NOTEPAD'], 'Xtestfile1')
2796 call writefile(['Linux', 'MacOS', 'MS-Windows'], 'Xtestfile2')
2797
2798 " Error cases
2799 call assert_fails('Xvimgrep /abc *', 'E682:')
2800
2801 let @/=''
2802 call assert_fails('Xvimgrep // *', 'E35:')
2803
2804 call assert_fails('Xvimgrep abc', 'E683:')
2805 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
2806 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
2807
2808 Xexpr ""
2809 Xvimgrepadd Notepad Xtestfile1
2810 Xvimgrepadd MacOS Xtestfile2
2811 let l = g:Xgetlist()
2812 call assert_equal(2, len(l))
2813 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
2814
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002815 10Xvimgrep #\cvim#g Xtestfile?
Bram Moolenaaree85df32017-03-19 14:19:50 +01002816 let l = g:Xgetlist()
2817 call assert_equal(2, len(l))
2818 call assert_equal(8, l[0].col)
2819 call assert_equal(12, l[1].col)
2820
2821 1Xvimgrep ?Editor? Xtestfile*
2822 let l = g:Xgetlist()
2823 call assert_equal(1, len(l))
2824 call assert_equal('Editor:VIM vim', l[0].text)
2825
2826 edit +3 Xtestfile2
2827 Xvimgrep +\cemacs+j Xtestfile1
2828 let l = g:Xgetlist()
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02002829 call assert_equal('Xtestfile2', @%)
Bram Moolenaaree85df32017-03-19 14:19:50 +01002830 call assert_equal('Editor:Emacs EmAcS', l[0].text)
2831
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02002832 " Test for unloading a buffer after vimgrep searched the buffer
2833 %bwipe
2834 Xvimgrep /Editor/j Xtestfile*
2835 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
2836 call assert_equal([], getbufinfo('Xtestfile2'))
2837
Bram Moolenaaree85df32017-03-19 14:19:50 +01002838 call delete('Xtestfile1')
2839 call delete('Xtestfile2')
2840endfunc
2841
2842" Tests for the :vimgrep command
2843func Test_vimgrep()
2844 call XvimgrepTests('c')
2845 call XvimgrepTests('l')
2846endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002847
Bram Moolenaar1c299432018-10-28 14:36:09 +01002848" Test for incsearch highlighting of the :vimgrep pattern
2849" This test used to cause "E315: ml_get: invalid lnum" errors.
2850func Test_vimgrep_incsearch()
2851 enew
2852 set incsearch
2853 call test_override("char_avail", 1)
2854
2855 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
2856 let l = getqflist()
2857 call assert_equal(2, len(l))
2858
2859 call test_override("ALL", 0)
2860 set noincsearch
2861endfunc
2862
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01002863" Test vimgrep with the last search pattern not set
2864func Test_vimgrep_with_no_last_search_pat()
2865 let lines =<< trim [SCRIPT]
2866 call assert_fails('vimgrep // *', 'E35:')
2867 call writefile(v:errors, 'Xresult')
2868 qall!
2869 [SCRIPT]
2870 call writefile(lines, 'Xscript')
2871 if RunVim([], [], '--clean -S Xscript')
2872 call assert_equal([], readfile('Xresult'))
2873 endif
2874 call delete('Xscript')
2875 call delete('Xresult')
2876endfunc
2877
Bram Moolenaar997cd1a2020-08-31 22:16:08 +02002878" Test vimgrep without swap file
2879func Test_vimgrep_without_swap_file()
2880 let lines =<< trim [SCRIPT]
2881 vimgrep grep test_c*
2882 call writefile(['done'], 'Xresult')
2883 qall!
2884 [SCRIPT]
2885 call writefile(lines, 'Xscript')
2886 if RunVim([], [], '--clean -n -S Xscript Xscript')
2887 call assert_equal(['done'], readfile('Xresult'))
2888 endif
2889 call delete('Xscript')
2890 call delete('Xresult')
2891endfunc
2892
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02002893func Test_vimgrep_existing_swapfile()
2894 call writefile(['match apple with apple'], 'Xapple')
2895 call writefile(['swapfile'], '.Xapple.swp')
2896 let g:foundSwap = 0
2897 let g:ignoreSwapExists = 1
2898 augroup grep
2899 au SwapExists * let foundSwap = 1 | let v:swapchoice = 'e'
2900 augroup END
2901 vimgrep apple Xapple
2902 call assert_equal(1, g:foundSwap)
2903 call assert_match('.Xapple.swo', swapname(''))
2904
2905 call delete('Xapple')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002906 call delete('.Xapple.swp')
Bram Moolenaar8ce4b7e2020-08-07 18:12:18 +02002907 augroup grep
2908 au! SwapExists
2909 augroup END
2910 unlet g:ignoreSwapExists
2911endfunc
2912
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002913func XfreeTests(cchar)
2914 call s:setup_commands(a:cchar)
2915
2916 enew | only
2917
2918 " Deleting the quickfix stack should work even When the current list is
2919 " somewhere in the middle of the stack
2920 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2921 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2922 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2923 Xolder
2924 call g:Xsetlist([], 'f')
2925 call assert_equal(0, len(g:Xgetlist()))
2926
2927 " After deleting the stack, adding a new list should create a stack with a
2928 " single list.
2929 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2930 call assert_equal(1, g:Xgetlist({'all':1}).nr)
2931
2932 " Deleting the stack from a quickfix window should update/clear the
2933 " quickfix/location list window.
2934 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2935 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2936 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2937 Xolder
2938 Xwindow
2939 call g:Xsetlist([], 'f')
2940 call assert_equal(2, winnr('$'))
2941 call assert_equal(1, line('$'))
2942 Xclose
2943
2944 " Deleting the stack from a non-quickfix window should update/clear the
2945 " quickfix/location list window.
2946 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2947 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2948 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2949 Xolder
2950 Xwindow
2951 wincmd p
2952 call g:Xsetlist([], 'f')
2953 call assert_equal(0, len(g:Xgetlist()))
2954 wincmd p
2955 call assert_equal(2, winnr('$'))
2956 call assert_equal(1, line('$'))
2957
2958 " After deleting the location list stack, if the location list window is
2959 " opened, then a new location list should be created. So opening the
2960 " location list window again should not create a new window.
2961 if a:cchar == 'l'
2962 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2963 wincmd p
2964 lopen
2965 call assert_equal(2, winnr('$'))
2966 endif
2967 Xclose
2968endfunc
2969
Bram Moolenaar74240d32017-12-10 15:26:15 +01002970" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002971func Test_qf_free()
2972 call XfreeTests('c')
2973 call XfreeTests('l')
2974endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002975
2976" Test for buffer overflow when parsing lines and adding new entries to
2977" the quickfix list.
2978func Test_bufoverflow()
2979 set efm=%f:%l:%m
2980 cgetexpr ['File1:100:' . repeat('x', 1025)]
2981
2982 set efm=%+GCompiler:\ %.%#,%f:%l:%m
2983 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
2984
2985 set efm=%DEntering\ directory\ %f,%f:%l:%m
2986 cgetexpr ['Entering directory ' . repeat('a', 1006),
2987 \ 'File1:10:Hello World']
2988 set efm&vim
2989endfunc
2990
Bram Moolenaar875feea2017-06-11 16:07:51 +02002991" Tests for getting the quickfix stack size
2992func XsizeTests(cchar)
2993 call s:setup_commands(a:cchar)
2994
2995 call g:Xsetlist([], 'f')
2996 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01002997 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
2998 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02002999
3000 Xexpr "File1:10:Line1"
3001 Xexpr "File2:20:Line2"
3002 Xexpr "File3:30:Line3"
3003 Xolder | Xolder
3004 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
3005 call g:Xsetlist([], 'f')
3006
3007 Xexpr "File1:10:Line1"
3008 Xexpr "File2:20:Line2"
3009 Xexpr "File3:30:Line3"
3010 Xolder | Xolder
3011 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
3012 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
3013endfunc
3014
3015func Test_Qf_Size()
3016 call XsizeTests('c')
3017 call XsizeTests('l')
3018endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02003019
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003020func Test_cclose_from_copen()
3021 augroup QF_Test
3022 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003023 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003024 augroup END
3025 copen
3026 augroup QF_Test
3027 au!
3028 augroup END
3029 augroup! QF_Test
3030endfunc
3031
Bram Moolenaar18141832017-06-25 21:17:25 +02003032func Test_cclose_in_autocmd()
3033 " Problem is only triggered if "starting" is zero, so that the OptionsSet
3034 " event will be triggered.
3035 call test_override('starting', 1)
3036 augroup QF_Test
3037 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003038 au FileType qf :call assert_fails(':cclose', 'E788:')
Bram Moolenaar18141832017-06-25 21:17:25 +02003039 augroup END
3040 copen
3041 augroup QF_Test
3042 au!
3043 augroup END
3044 augroup! QF_Test
3045 call test_override('starting', 0)
3046endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003047
Bram Moolenaar379fb762018-08-30 15:58:28 +02003048" Check that ":file" without an argument is possible even when "curbuf_lock"
3049" is set.
3050func Test_file_from_copen()
3051 " Works without argument.
3052 augroup QF_Test
3053 au!
3054 au FileType qf file
3055 augroup END
3056 copen
3057
3058 augroup QF_Test
3059 au!
3060 augroup END
3061 cclose
3062
3063 " Fails with argument.
3064 augroup QF_Test
3065 au!
Bram Moolenaare2e40752020-09-04 21:18:46 +02003066 au FileType qf call assert_fails(':file foo', 'E788:')
Bram Moolenaar379fb762018-08-30 15:58:28 +02003067 augroup END
3068 copen
3069 augroup QF_Test
3070 au!
3071 augroup END
3072 cclose
3073
3074 augroup! QF_Test
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02003075endfunc
Bram Moolenaar379fb762018-08-30 15:58:28 +02003076
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003077func Test_resize_from_copen()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003078 augroup QF_Test
3079 au!
3080 au FileType qf resize 5
3081 augroup END
3082 try
3083 " This should succeed without any exception. No other buffers are
3084 " involved in the autocmd.
3085 copen
3086 finally
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003087 augroup QF_Test
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003088 au!
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003089 augroup END
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003090 augroup! QF_Test
3091 endtry
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02003092endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02003093
Bram Moolenaar33aecb12020-11-14 17:25:51 +01003094func Test_vimgrep_with_textlock()
3095 new
3096
3097 " Simple way to execute something with "textwinlock" set.
3098 " Check that vimgrep without jumping can be executed.
3099 au InsertCharPre * vimgrep /RunTheTest/j runtest.vim
3100 normal ax
3101 let qflist = getqflist()
3102 call assert_true(len(qflist) > 0)
3103 call assert_match('RunTheTest', qflist[0].text)
3104 call setqflist([], 'r')
3105 au! InsertCharPre
3106
3107 " Check that vimgrepadd without jumping can be executed.
3108 au InsertCharPre * vimgrepadd /RunTheTest/j runtest.vim
3109 normal ax
3110 let qflist = getqflist()
3111 call assert_true(len(qflist) > 0)
3112 call assert_match('RunTheTest', qflist[0].text)
3113 call setqflist([], 'r')
3114 au! InsertCharPre
3115
3116 " Check that lvimgrep without jumping can be executed.
3117 au InsertCharPre * lvimgrep /RunTheTest/j runtest.vim
3118 normal ax
3119 let qflist = getloclist(0)
3120 call assert_true(len(qflist) > 0)
3121 call assert_match('RunTheTest', qflist[0].text)
3122 call setloclist(0, [], 'r')
3123 au! InsertCharPre
3124
3125 " Check that lvimgrepadd without jumping can be executed.
3126 au InsertCharPre * lvimgrepadd /RunTheTest/j runtest.vim
3127 normal ax
3128 let qflist = getloclist(0)
3129 call assert_true(len(qflist) > 0)
3130 call assert_match('RunTheTest', qflist[0].text)
3131 call setloclist(0, [], 'r')
3132 au! InsertCharPre
3133
3134 " trying to jump will give an error
3135 au InsertCharPre * vimgrep /RunTheTest/ runtest.vim
3136 call assert_fails('normal ax', 'E565:')
3137 au! InsertCharPre
3138
3139 au InsertCharPre * vimgrepadd /RunTheTest/ runtest.vim
3140 call assert_fails('normal ax', 'E565:')
3141 au! InsertCharPre
3142
3143 au InsertCharPre * lvimgrep /RunTheTest/ runtest.vim
3144 call assert_fails('normal ax', 'E565:')
3145 au! InsertCharPre
3146
3147 au InsertCharPre * lvimgrepadd /RunTheTest/ runtest.vim
3148 call assert_fails('normal ax', 'E565:')
3149 au! InsertCharPre
3150
3151 bwipe!
3152endfunc
3153
Bram Moolenaara8788f42017-07-19 17:06:20 +02003154" Tests for the quickfix buffer b:changedtick variable
3155func Xchangedtick_tests(cchar)
3156 call s:setup_commands(a:cchar)
3157
3158 new | only
3159
3160 Xexpr "" | Xexpr "" | Xexpr ""
3161
3162 Xopen
3163 Xolder
3164 Xolder
3165 Xaddexpr "F1:10:Line10"
3166 Xaddexpr "F2:20:Line20"
3167 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
3168 call g:Xsetlist([], 'f')
3169 call assert_equal(8, getbufvar('%', 'changedtick'))
3170 Xclose
3171endfunc
3172
3173func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02003174 call Xchangedtick_tests('c')
3175 call Xchangedtick_tests('l')
3176endfunc
3177
3178" Tests for parsing an expression using setqflist()
3179func Xsetexpr_tests(cchar)
3180 call s:setup_commands(a:cchar)
3181
3182 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003183 call g:Xsetlist([], ' ', {'lines' : t})
3184 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003185
3186 let l = g:Xgetlist()
3187 call assert_equal(3, len(l))
3188 call assert_equal(20, l[1].lnum)
3189 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003190 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003191 let l = g:Xgetlist()
3192 call assert_equal(1, len(l))
3193 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003194 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
3195 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003196
3197 call g:Xsetlist([], 'f')
3198 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003199 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
3200 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
3201 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
3202 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003203 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
3204 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02003205
3206 " Adding entries using a custom efm
3207 set efm&
3208 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
3209 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
3210 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3211 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
3212 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
3213 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3214 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
3215 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003216endfunc
3217
3218func Test_setexpr()
3219 call Xsetexpr_tests('c')
3220 call Xsetexpr_tests('l')
3221endfunc
3222
3223" Tests for per quickfix/location list directory stack
3224func Xmultidirstack_tests(cchar)
3225 call s:setup_commands(a:cchar)
3226
3227 call g:Xsetlist([], 'f')
3228 Xexpr "" | Xexpr ""
3229
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003230 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
3231 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
3232 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
3233 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003234
3235 let l1 = g:Xgetlist({'nr':1, 'items':1})
3236 let l2 = g:Xgetlist({'nr':2, 'items':1})
3237 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
3238 call assert_equal(3, l1.items[1].lnum)
3239 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
3240 call assert_equal(5, l2.items[1].lnum)
3241endfunc
3242
3243func Test_multidirstack()
3244 call mkdir('Xone/a', 'p')
3245 call mkdir('Xtwo/a', 'p')
3246 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3247 call writefile(lines, 'Xone/a/one.txt')
3248 call writefile(lines, 'Xtwo/a/two.txt')
3249 let save_efm = &efm
3250 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
3251
3252 call Xmultidirstack_tests('c')
3253 call Xmultidirstack_tests('l')
3254
3255 let &efm = save_efm
3256 call delete('Xone', 'rf')
3257 call delete('Xtwo', 'rf')
3258endfunc
3259
3260" Tests for per quickfix/location list file stack
3261func Xmultifilestack_tests(cchar)
3262 call s:setup_commands(a:cchar)
3263
3264 call g:Xsetlist([], 'f')
3265 Xexpr "" | Xexpr ""
3266
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003267 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
3268 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
3269 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
3270 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003271
3272 let l1 = g:Xgetlist({'nr':1, 'items':1})
3273 let l2 = g:Xgetlist({'nr':2, 'items':1})
3274 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
3275 call assert_equal(3, l1.items[1].lnum)
3276 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
3277 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02003278
3279 " Test for start of a new error line in the same line where a previous
3280 " error line ends with a file stack.
3281 let efm_val = 'Error\ l%l\ in\ %f,'
3282 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
3283 let l = g:Xgetlist({'lines' : [
3284 \ '(one.txt',
3285 \ 'Error l4 in one.txt',
3286 \ ') (two.txt',
3287 \ 'Error l6 in two.txt',
3288 \ ')',
3289 \ 'Error l8 in one.txt'
3290 \ ], 'efm' : efm_val})
3291 call assert_equal(3, len(l.items))
3292 call assert_equal('one.txt', bufname(l.items[0].bufnr))
3293 call assert_equal(4, l.items[0].lnum)
3294 call assert_equal('one.txt', l.items[0].text)
3295 call assert_equal('two.txt', bufname(l.items[1].bufnr))
3296 call assert_equal(6, l.items[1].lnum)
3297 call assert_equal('two.txt', l.items[1].text)
3298 call assert_equal('one.txt', bufname(l.items[2].bufnr))
3299 call assert_equal(8, l.items[2].lnum)
3300 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02003301endfunc
3302
3303func Test_multifilestack()
3304 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3305 call writefile(lines, 'one.txt')
3306 call writefile(lines, 'two.txt')
3307 let save_efm = &efm
3308 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
3309
3310 call Xmultifilestack_tests('c')
3311 call Xmultifilestack_tests('l')
3312
3313 let &efm = save_efm
3314 call delete('one.txt')
3315 call delete('two.txt')
3316endfunc
3317
3318" Tests for per buffer 'efm' setting
3319func Test_perbuf_efm()
3320 call writefile(["File1-10-Line10"], 'one.txt')
3321 call writefile(["File2#20#Line20"], 'two.txt')
3322 set efm=%f#%l#%m
3323 new | only
3324 new
3325 setlocal efm=%f-%l-%m
3326 cfile one.txt
3327 wincmd w
3328 caddfile two.txt
3329
3330 let l = getqflist()
3331 call assert_equal(10, l[0].lnum)
3332 call assert_equal('Line20', l[1].text)
3333
3334 set efm&
3335 new | only
3336 call delete('one.txt')
3337 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02003338endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003339
3340" Open multiple help windows using ":lhelpgrep
3341" This test used to crash Vim
3342func Test_Multi_LL_Help()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02003343 new | only
3344 lhelpgrep window
3345 lopen
3346 e#
3347 lhelpgrep buffer
3348 call assert_equal(3, winnr('$'))
3349 call assert_true(len(getloclist(1)) != 0)
3350 call assert_true(len(getloclist(2)) != 0)
3351 new | only
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003352endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02003353
3354" Tests for adding new quickfix lists using setqflist()
3355func XaddQf_tests(cchar)
3356 call s:setup_commands(a:cchar)
3357
3358 " Create a new list using ' ' for action
3359 call g:Xsetlist([], 'f')
3360 call g:Xsetlist([], ' ', {'title' : 'Test1'})
3361 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3362 call assert_equal(1, l.nr)
3363 call assert_equal('Test1', l.title)
3364
3365 " Create a new list using ' ' for action and '$' for 'nr'
3366 call g:Xsetlist([], 'f')
3367 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
3368 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3369 call assert_equal(1, l.nr)
3370 call assert_equal('Test2', l.title)
3371
3372 " Create a new list using 'a' for action
3373 call g:Xsetlist([], 'f')
3374 call g:Xsetlist([], 'a', {'title' : 'Test3'})
3375 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3376 call assert_equal(1, l.nr)
3377 call assert_equal('Test3', l.title)
3378
3379 " Create a new list using 'a' for action and '$' for 'nr'
3380 call g:Xsetlist([], 'f')
3381 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
3382 call g:Xsetlist([], 'a', {'title' : 'Test4'})
3383 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3384 call assert_equal(1, l.nr)
3385 call assert_equal('Test4', l.title)
3386
3387 " Adding a quickfix list should remove all the lists following the current
3388 " list.
3389 Xexpr "" | Xexpr "" | Xexpr ""
3390 silent! 10Xolder
3391 call g:Xsetlist([], ' ', {'title' : 'Test5'})
3392 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3393 call assert_equal(2, l.nr)
3394 call assert_equal('Test5', l.title)
3395
3396 " Add a quickfix list using '$' as the list number.
3397 let lastqf = g:Xgetlist({'nr':'$'}).nr
3398 silent! 99Xolder
3399 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
3400 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3401 call assert_equal(lastqf + 1, l.nr)
3402 call assert_equal('Test6', l.title)
3403
3404 " Add a quickfix list using 'nr' set to one more than the quickfix
3405 " list size.
3406 let lastqf = g:Xgetlist({'nr':'$'}).nr
3407 silent! 99Xolder
3408 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
3409 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3410 call assert_equal(lastqf + 1, l.nr)
3411 call assert_equal('Test7', l.title)
3412
3413 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
3414 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
3415 silent! 99Xolder
3416 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
3417 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3418 call assert_equal(10, l.nr)
3419 call assert_equal('Test8', l.title)
3420
3421 " Add a quickfix list using 'nr' set to a value greater than 10
3422 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
3423
3424 " Try adding a quickfix list with 'nr' set to a value greater than the
3425 " quickfix list size but less than 10.
3426 call g:Xsetlist([], 'f')
3427 Xexpr "" | Xexpr "" | Xexpr ""
3428 silent! 99Xolder
3429 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
3430
3431 " Add a quickfix list using 'nr' set to a some string or list
3432 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
3433endfunc
3434
3435func Test_add_qf()
3436 call XaddQf_tests('c')
3437 call XaddQf_tests('l')
3438endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003439
3440" Test for getting the quickfix list items from some text without modifying
3441" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003442func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003443 call s:setup_commands(a:cchar)
3444 call g:Xsetlist([], 'f')
3445
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003446 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003447 call assert_equal(2, len(l))
3448 call assert_equal(30, l[1].lnum)
3449
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003450 call assert_equal({}, g:Xgetlist({'lines' : 10}))
3451 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
3452 call assert_equal([], g:Xgetlist({'lines' : []}).items)
3453 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003454
Bram Moolenaar36538222017-09-02 19:51:44 +02003455 " Parse text using a custom efm
3456 set efm&
3457 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
3458 call assert_equal('Line30', l[0].text)
3459 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
3460 call assert_equal('File3:30:Line30', l[0].text)
3461 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
3462 call assert_equal({}, l)
3463 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
3464 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
3465
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003466 " Make sure that the quickfix stack is not modified
3467 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
3468endfunc
3469
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003470func Test_get_list_from_lines()
3471 call XgetListFromLines('c')
3472 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003473endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003474
3475" Tests for the quickfix list id
3476func Xqfid_tests(cchar)
3477 call s:setup_commands(a:cchar)
3478
3479 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003480 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003481 Xexpr ''
3482 let start_id = g:Xgetlist({'id' : 0}).id
3483 Xexpr '' | Xexpr ''
3484 Xolder
3485 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
3486 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
3487 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003488 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003489 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003490 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3491 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003492
3493 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3494 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003495 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003496 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3497 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3498 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3499
3500 let qfid = g:Xgetlist({'id':0, 'nr':0})
3501 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003502 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003503endfunc
3504
3505func Test_qf_id()
3506 call Xqfid_tests('c')
3507 call Xqfid_tests('l')
3508endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003509
3510func Xqfjump_tests(cchar)
3511 call s:setup_commands(a:cchar)
3512
3513 call writefile(["Line1\tFoo", "Line2"], 'F1')
3514 call writefile(["Line1\tBar", "Line2"], 'F2')
3515 call writefile(["Line1\tBaz", "Line2"], 'F3')
3516
3517 call g:Xsetlist([], 'f')
3518
3519 " Tests for
3520 " Jumping to a line using a pattern
3521 " Jumping to a column greater than the last column in a line
3522 " Jumping to a line greater than the last line in the file
3523 let l = []
3524 for i in range(1, 7)
3525 call add(l, {})
3526 endfor
3527 let l[0].filename='F1'
3528 let l[0].pattern='Line1'
3529 let l[1].filename='F2'
3530 let l[1].pattern='Line1'
3531 let l[2].filename='F3'
3532 let l[2].pattern='Line1'
3533 let l[3].filename='F3'
3534 let l[3].lnum=1
3535 let l[3].col=9
3536 let l[3].vcol=1
3537 let l[4].filename='F3'
3538 let l[4].lnum=99
3539 let l[5].filename='F3'
3540 let l[5].lnum=1
3541 let l[5].col=99
3542 let l[5].vcol=1
3543 let l[6].filename='F3'
3544 let l[6].pattern='abcxyz'
3545
3546 call g:Xsetlist([], ' ', {'items' : l})
3547 Xopen | only
3548 2Xnext
3549 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02003550 call assert_equal('F3', @%)
Bram Moolenaar74240d32017-12-10 15:26:15 +01003551 Xnext
3552 call assert_equal(7, col('.'))
3553 Xnext
3554 call assert_equal(2, line('.'))
3555 Xnext
3556 call assert_equal(9, col('.'))
3557 2
3558 Xnext
3559 call assert_equal(2, line('.'))
3560
3561 if a:cchar == 'l'
3562 " When jumping to a location list entry in the location list window and
3563 " no usable windows are available, then a new window should be opened.
3564 enew! | new | only
3565 call g:Xsetlist([], 'f')
3566 setlocal buftype=nofile
3567 new
3568 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']})
3569 Xopen
3570 let winid = win_getid()
3571 wincmd p
3572 close
3573 call win_gotoid(winid)
3574 Xnext
3575 call assert_equal(3, winnr('$'))
3576 call assert_equal(1, winnr())
3577 call assert_equal(2, line('.'))
3578
3579 " When jumping to an entry in the location list window and the window
3580 " associated with the location list is not present and a window containing
3581 " the file is already present, then that window should be used.
3582 close
3583 belowright new
3584 call g:Xsetlist([], 'f')
3585 edit F3
3586 call win_gotoid(winid)
3587 Xlast
3588 call assert_equal(3, winnr())
3589 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3590 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3591 endif
3592
3593 " Cleanup
3594 enew!
3595 new | only
3596
3597 call delete('F1')
3598 call delete('F2')
3599 call delete('F3')
3600endfunc
3601
3602func Test_qfjump()
3603 call Xqfjump_tests('c')
3604 call Xqfjump_tests('l')
3605endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01003606
3607" Tests for the getqflist() and getloclist() functions when the list is not
3608" present or is empty
3609func Xgetlist_empty_tests(cchar)
3610 call s:setup_commands(a:cchar)
3611
3612 " Empty quickfix stack
3613 call g:Xsetlist([], 'f')
3614 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3615 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
3616 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3617 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3618 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
3619 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3620 call assert_equal('', g:Xgetlist({'title' : 0}).title)
3621 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003622 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003623 if a:cchar == 'c'
3624 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003625 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003626 \ 'title' : '', 'winid' : 0, 'changedtick': 0,
3627 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003628 else
3629 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
3630 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003631 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003632 \ 'qfbufnr' : 0, 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003633 \ g:Xgetlist({'all' : 0}))
3634 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003635
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003636 " Quickfix window with empty stack
3637 silent! Xopen
3638 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003639 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003640 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
3641 Xclose
3642
Bram Moolenaara6d48492017-12-12 22:45:31 +01003643 " Empty quickfix list
3644 Xexpr ""
3645 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3646 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
3647 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3648 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3649 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
3650 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3651 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
3652 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003653 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003654
3655 let qfid = g:Xgetlist({'id' : 0}).id
3656 call g:Xsetlist([], 'f')
3657
3658 " Non-existing quickfix identifier
3659 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
3660 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
3661 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
3662 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
3663 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
3664 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
3665 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
3666 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003667 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003668 if a:cchar == 'c'
3669 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3670 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003671 \ 'qfbufnr' : qfbufnr, 'quickfixtextfunc' : '',
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003672 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
3673 else
3674 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3675 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003676 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
3677 \ 'quickfixtextfunc' : ''},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003678 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
3679 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003680
3681 " Non-existing quickfix list number
3682 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
3683 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
3684 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
3685 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
3686 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
3687 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
3688 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
3689 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003690 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003691 if a:cchar == 'c'
3692 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3693 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003694 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr,
3695 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003696 else
3697 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3698 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaard43906d2020-07-20 21:31:32 +02003699 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0,
3700 \ 'quickfixtextfunc' : ''}, g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003701 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003702endfunc
3703
3704func Test_getqflist()
3705 call Xgetlist_empty_tests('c')
3706 call Xgetlist_empty_tests('l')
3707endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01003708
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01003709func Test_getqflist_invalid_nr()
3710 " The following commands used to crash Vim
3711 cexpr ""
3712 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
3713
3714 " Cleanup
3715 call setqflist([], 'r')
3716endfunc
3717
Bram Moolenaarb254af32017-12-18 19:48:58 +01003718" Tests for the quickfix/location list changedtick
3719func Xqftick_tests(cchar)
3720 call s:setup_commands(a:cchar)
3721
3722 call g:Xsetlist([], 'f')
3723
3724 Xexpr "F1:10:Line10"
3725 let qfid = g:Xgetlist({'id' : 0}).id
3726 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3727 Xaddexpr "F2:20:Line20\nF2:21:Line21"
3728 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3729 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
3730 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
3731 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
3732 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
3733 call g:Xsetlist([], 'a', {'title' : 'New Title'})
3734 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
3735
3736 enew!
3737 call append(0, ["F5:50:L50", "F6:60:L60"])
3738 Xaddbuffer
3739 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
3740 enew!
3741
3742 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
3743 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
3744 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3745 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
3746 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
3747 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3748 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
3749 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3750 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3751 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
3752 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3753
3754 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
3755 Xfile Xone
3756 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3757 Xaddfile Xone
3758 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3759
3760 " Test case for updating a non-current quickfix list
3761 call g:Xsetlist([], 'f')
3762 Xexpr "F1:1:L1"
3763 Xexpr "F2:2:L2"
3764 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
3765 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3766 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
3767
3768 call delete("Xone")
3769endfunc
3770
3771func Test_qf_tick()
3772 call Xqftick_tests('c')
3773 call Xqftick_tests('l')
3774endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01003775
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02003776" Test helpgrep with lang specifier
3777func Xtest_helpgrep_with_lang_specifier(cchar)
3778 call s:setup_commands(a:cchar)
3779 Xhelpgrep Vim@en
3780 call assert_equal('help', &filetype)
3781 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
3782 new | only
3783endfunc
3784
3785func Test_helpgrep_with_lang_specifier()
3786 call Xtest_helpgrep_with_lang_specifier('c')
3787 call Xtest_helpgrep_with_lang_specifier('l')
3788endfunc
3789
Bram Moolenaar12237442017-12-19 12:38:52 +01003790" The following test used to crash Vim.
3791" Open the location list window and close the regular window associated with
3792" the location list. When the garbage collection runs now, it incorrectly
3793" marks the location list context as not in use and frees the context.
3794func Test_ll_window_ctx()
3795 call setloclist(0, [], 'f')
3796 call setloclist(0, [], 'a', {'context' : []})
3797 lopen | only
3798 call test_garbagecollect_now()
3799 echo getloclist(0, {'context' : 1}).context
3800 enew | only
3801endfunc
3802
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01003803" The following test used to crash vim
3804func Test_lfile_crash()
3805 sp Xtest
3806 au QuickFixCmdPre * bw
Bram Moolenaare2e40752020-09-04 21:18:46 +02003807 call assert_fails('lfile', 'E40:')
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01003808 au! QuickFixCmdPre
3809endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01003810
3811" The following test used to crash vim
3812func Test_lbuffer_crash()
3813 sv Xtest
3814 augroup QF_Test
3815 au!
3816 au * * bw
3817 augroup END
3818 lbuffer
3819 augroup QF_Test
3820 au!
3821 augroup END
3822endfunc
3823
3824" The following test used to crash vim
3825func Test_lexpr_crash()
3826 augroup QF_Test
3827 au!
3828 au * * call setloclist(0, [], 'f')
3829 augroup END
3830 lexpr ""
3831 augroup QF_Test
3832 au!
3833 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003834
Bram Moolenaar3c097222017-12-21 20:54:49 +01003835 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003836 augroup QF_Test
3837 au!
3838 au BufNew * call setloclist(0, [], 'f')
3839 augroup END
3840 lexpr 'x:1:x'
3841 augroup QF_Test
3842 au!
3843 augroup END
3844
3845 enew | only
3846 lexpr ''
3847 lopen
3848 augroup QF_Test
3849 au!
3850 au FileType * call setloclist(0, [], 'f')
3851 augroup END
3852 lexpr ''
3853 augroup QF_Test
3854 au!
3855 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01003856endfunc
3857
3858" The following test used to crash Vim
3859func Test_lvimgrep_crash()
3860 sv Xtest
3861 augroup QF_Test
3862 au!
3863 au * * call setloclist(0, [], 'f')
3864 augroup END
3865 lvimgrep quickfix test_quickfix.vim
3866 augroup QF_Test
3867 au!
3868 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003869
3870 new | only
3871 augroup QF_Test
3872 au!
3873 au BufEnter * call setloclist(0, [], 'r')
3874 augroup END
3875 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
3876 augroup QF_Test
3877 au!
3878 augroup END
3879
Bram Moolenaar3c097222017-12-21 20:54:49 +01003880 enew | only
3881endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01003882
Bram Moolenaar2573af32020-03-14 17:21:34 +01003883func Test_lvimgrep_crash2()
3884 au BufNewFile x sfind
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003885 call assert_fails('lvimgrep x x', 'E471:')
3886 call assert_fails('lvimgrep x x x', 'E471:')
Bram Moolenaar2573af32020-03-14 17:21:34 +01003887
3888 au! BufNewFile
3889endfunc
3890
Bram Moolenaarde046542017-12-26 13:53:11 +01003891" Test for the position of the quickfix and location list window
3892func Test_qfwin_pos()
3893 " Open two windows
3894 new | only
3895 new
3896 cexpr ['F1:10:L10']
3897 copen
3898 " Quickfix window should be the bottom most window
3899 call assert_equal(3, winnr())
3900 close
3901 " Open at the very top
3902 wincmd t
3903 topleft copen
3904 call assert_equal(1, winnr())
3905 close
3906 " open left of the current window
3907 wincmd t
3908 below new
3909 leftabove copen
3910 call assert_equal(2, winnr())
3911 close
3912 " open right of the current window
3913 rightbelow copen
3914 call assert_equal(3, winnr())
3915 close
3916endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02003917
3918" Tests for quickfix/location lists changed by autocommands when
3919" :vimgrep/:lvimgrep commands are running.
3920func Test_vimgrep_autocmd()
3921 call setqflist([], 'f')
3922 call writefile(['stars'], 'Xtest1.txt')
3923 call writefile(['stars'], 'Xtest2.txt')
3924
3925 " Test 1:
3926 " When searching for a pattern using :vimgrep, if the quickfix list is
3927 " changed by an autocmd, the results should be added to the correct quickfix
3928 " list.
3929 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
3930 silent vimgrep stars Xtest*.txt
3931 call assert_equal(1, getqflist({'nr' : 0}).nr)
3932 call assert_equal(3, getqflist({'nr' : '$'}).nr)
3933 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
3934 au! BufRead Xtest2.txt
3935
3936 " Test 2:
3937 " When searching for a pattern using :vimgrep, if the quickfix list is
3938 " freed, then a error should be given.
3939 silent! %bwipe!
3940 call setqflist([], 'f')
3941 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
3942 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
3943 au! BufRead Xtest2.txt
3944
3945 " Test 3:
3946 " When searching for a pattern using :lvimgrep, if the location list is
3947 " freed, then the command should error out.
3948 silent! %bwipe!
3949 let g:save_winid = win_getid()
3950 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
3951 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
3952 au! BufRead Xtest2.txt
3953
3954 call delete('Xtest1.txt')
3955 call delete('Xtest2.txt')
3956 call setqflist([], 'f')
3957endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003958
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003959" Test for an autocmd changing the current directory when running vimgrep
3960func Xvimgrep_autocmd_cd(cchar)
3961 call s:setup_commands(a:cchar)
3962
3963 %bwipe
3964 let save_cwd = getcwd()
3965
3966 augroup QF_Test
3967 au!
3968 autocmd BufRead * silent cd %:p:h
3969 augroup END
3970
3971 10Xvimgrep /vim/ Xdir/**
3972 let l = g:Xgetlist()
3973 call assert_equal('f1.txt', bufname(l[0].bufnr))
3974 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
3975
3976 augroup QF_Test
3977 au!
3978 augroup END
3979
3980 exe 'cd ' . save_cwd
3981endfunc
3982
3983func Test_vimgrep_autocmd_cd()
3984 call mkdir('Xdir/a', 'p')
3985 call mkdir('Xdir/b', 'p')
3986 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xdir/a/f1.txt')
3987 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xdir/b/f2.txt')
3988 call Xvimgrep_autocmd_cd('c')
3989 call Xvimgrep_autocmd_cd('l')
3990 %bwipe
3991 call delete('Xdir', 'rf')
3992endfunc
3993
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003994" The following test used to crash Vim
3995func Test_lhelpgrep_autocmd()
3996 lhelpgrep quickfix
3997 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
3998 lhelpgrep buffer
3999 call assert_equal('help', &filetype)
4000 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
4001 lhelpgrep tabpage
4002 call assert_equal('help', &filetype)
4003 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
4004 au! QuickFixCmdPost
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004005
4006 new | only
4007 augroup QF_Test
4008 au!
4009 au BufEnter * call setqflist([], 'f')
4010 augroup END
4011 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaardbfa7952020-11-02 20:04:22 +01004012 " run the test with a help window already open
4013 help
4014 wincmd w
4015 call assert_fails('helpgrep quickfix', 'E925:')
Bram Moolenaarb6f14802018-10-21 18:47:43 +02004016 augroup QF_Test
4017 au! BufEnter
4018 augroup END
4019
4020 new | only
4021 augroup QF_Test
4022 au!
4023 au BufEnter * call setqflist([], 'r')
4024 augroup END
4025 call assert_fails('helpgrep quickfix', 'E925:')
4026 augroup QF_Test
4027 au! BufEnter
4028 augroup END
4029
4030 new | only
4031 augroup QF_Test
4032 au!
4033 au BufEnter * call setloclist(0, [], 'r')
4034 augroup END
4035 call assert_fails('lhelpgrep quickfix', 'E926:')
4036 augroup QF_Test
4037 au! BufEnter
4038 augroup END
4039
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02004040 new | only
4041endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02004042
4043" Test for shortening/simplifying the file name when opening the
4044" quickfix window or when displaying the quickfix list
4045func Test_shorten_fname()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02004046 CheckUnix
Bram Moolenaara796d462018-05-01 14:30:36 +02004047 %bwipe
4048 " Create a quickfix list with a absolute path filename
4049 let fname = getcwd() . '/test_quickfix.vim'
4050 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4051 call assert_equal(fname, bufname('test_quickfix.vim'))
4052 " Opening the quickfix window should simplify the file path
4053 cwindow
4054 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
4055 cclose
4056 %bwipe
4057 " Create a quickfix list with a absolute path filename
4058 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
4059 call assert_equal(fname, bufname('test_quickfix.vim'))
4060 " Displaying the quickfix list should simplify the file path
4061 silent! clist
4062 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
Bram Moolenaar8ec92c92020-09-29 22:47:03 +02004063 " Add a few entries for the same file with different paths and check whether
4064 " the buffer name is shortened
4065 %bwipe
4066 call setqflist([], 'f')
4067 call setqflist([{'filename' : 'test_quickfix.vim', 'lnum' : 10},
4068 \ {'filename' : '../testdir/test_quickfix.vim', 'lnum' : 20},
4069 \ {'filename' : fname, 'lnum' : 30}], ' ')
4070 copen
4071 call assert_equal(['test_quickfix.vim|10| ',
4072 \ 'test_quickfix.vim|20| ',
4073 \ 'test_quickfix.vim|30| '], getline(1, '$'))
4074 cclose
Bram Moolenaara796d462018-05-01 14:30:36 +02004075endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02004076
4077" Quickfix title tests
4078" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
4079" Otherwise due to indentation, the title is set with spaces at the beginning
4080" of the command.
4081func Test_qftitle()
4082 call writefile(["F1:1:Line1"], 'Xerr')
4083
4084 " :cexpr
4085 exe "cexpr readfile('Xerr')"
4086 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
4087
4088 " :cgetexpr
4089 exe "cgetexpr readfile('Xerr')"
4090 call assert_equal(":cgetexpr readfile('Xerr')",
4091 \ getqflist({'title' : 1}).title)
4092
4093 " :caddexpr
4094 call setqflist([], 'f')
4095 exe "caddexpr readfile('Xerr')"
4096 call assert_equal(":caddexpr readfile('Xerr')",
4097 \ getqflist({'title' : 1}).title)
4098
4099 " :cbuffer
4100 new Xerr
4101 exe "cbuffer"
4102 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
4103
4104 " :cgetbuffer
4105 edit Xerr
4106 exe "cgetbuffer"
4107 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
4108
4109 " :caddbuffer
4110 call setqflist([], 'f')
4111 edit Xerr
4112 exe "caddbuffer"
4113 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
4114
4115 " :cfile
4116 exe "cfile Xerr"
4117 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
4118
4119 " :cgetfile
4120 exe "cgetfile Xerr"
4121 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
4122
4123 " :caddfile
4124 call setqflist([], 'f')
4125 exe "caddfile Xerr"
4126 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
4127
4128 " :grep
4129 set grepprg=internal
4130 exe "grep F1 Xerr"
4131 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
4132
4133 " :grepadd
4134 call setqflist([], 'f')
4135 exe "grepadd F1 Xerr"
4136 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
4137 set grepprg&vim
4138
4139 " :vimgrep
4140 exe "vimgrep F1 Xerr"
4141 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
4142
4143 " :vimgrepadd
4144 call setqflist([], 'f')
4145 exe "vimgrepadd F1 Xerr"
4146 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
4147
4148 call setqflist(['F1:10:L10'], ' ')
4149 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4150
4151 call setqflist([], 'f')
4152 call setqflist(['F1:10:L10'], 'a')
4153 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4154
4155 call setqflist([], 'f')
4156 call setqflist(['F1:10:L10'], 'r')
4157 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
4158
4159 close
4160 call delete('Xerr')
4161
4162 call setqflist([], ' ', {'title' : 'Errors'})
4163 copen
4164 call assert_equal('Errors', w:quickfix_title)
4165 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
4166 call assert_equal('Errors', w:quickfix_title)
4167 cclose
4168endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02004169
4170func Test_lbuffer_with_bwipe()
4171 new
4172 new
4173 augroup nasty
4174 au * * bwipe
4175 augroup END
4176 lbuffer
4177 augroup nasty
4178 au!
4179 augroup END
4180endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02004181
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004182" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
4183" running
4184func Xexpr_acmd_freelist(cchar)
4185 call s:setup_commands(a:cchar)
4186
Bram Moolenaar0366c012018-06-18 20:52:13 +02004187 " This was using freed memory.
4188 augroup nasty
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004189 au * * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02004190 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004191 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02004192 augroup nasty
4193 au!
4194 augroup END
4195endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004196
4197func Test_cexpr_acmd_freelist()
4198 call Xexpr_acmd_freelist('c')
4199 call Xexpr_acmd_freelist('l')
4200endfunc
4201
4202" Test for commands that create a new quickfix/location list and jump to the
4203" first error automatically.
4204func Xjumpto_first_error_test(cchar)
4205 call s:setup_commands(a:cchar)
4206
4207 call s:create_test_file('Xtestfile1')
4208 call s:create_test_file('Xtestfile2')
4209 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4210
4211 " Test for cexpr/lexpr
4212 enew
4213 Xexpr l
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004214 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004215 call assert_equal(2, line('.'))
4216
4217 " Test for cfile/lfile
4218 enew
4219 call writefile(l, 'Xerr')
4220 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004221 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004222 call assert_equal(2, line('.'))
4223
4224 " Test for cbuffer/lbuffer
4225 edit Xerr
4226 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004227 call assert_equal('Xtestfile1', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004228 call assert_equal(2, line('.'))
4229
4230 call delete('Xerr')
4231 call delete('Xtestfile1')
4232 call delete('Xtestfile2')
4233endfunc
4234
4235func Test_jumpto_first_error()
4236 call Xjumpto_first_error_test('c')
4237 call Xjumpto_first_error_test('l')
4238endfunc
4239
4240" Test for a quickfix autocmd changing the quickfix/location list before
4241" jumping to the first error in the new list.
4242func Xautocmd_changelist(cchar)
4243 call s:setup_commands(a:cchar)
4244
4245 " Test for cfile/lfile
4246 call s:create_test_file('Xtestfile1')
4247 call s:create_test_file('Xtestfile2')
4248 Xexpr 'Xtestfile1:2:Line2'
4249 autocmd QuickFixCmdPost * Xolder
4250 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4251 Xfile Xerr
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004252 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004253 call assert_equal(4, line('.'))
4254 autocmd! QuickFixCmdPost
4255
4256 " Test for cbuffer/lbuffer
4257 call g:Xsetlist([], 'f')
4258 Xexpr 'Xtestfile1:2:Line2'
4259 autocmd QuickFixCmdPost * Xolder
4260 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4261 edit Xerr
4262 Xbuffer
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004263 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004264 call assert_equal(4, line('.'))
4265 autocmd! QuickFixCmdPost
4266
4267 " Test for cexpr/lexpr
4268 call g:Xsetlist([], 'f')
4269 Xexpr 'Xtestfile1:2:Line2'
4270 autocmd QuickFixCmdPost * Xolder
4271 Xexpr 'Xtestfile2:4:Line4'
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004272 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004273 call assert_equal(4, line('.'))
4274 autocmd! QuickFixCmdPost
4275
Bram Moolenaar851332e2018-07-03 19:16:00 +02004276 " The grepprg may not be set on non-Unix systems
4277 if has('unix')
4278 " Test for grep/lgrep
4279 call g:Xsetlist([], 'f')
4280 Xexpr 'Xtestfile1:2:Line2'
4281 autocmd QuickFixCmdPost * Xolder
4282 silent Xgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004283 call assert_equal('Xtestfile2', @%)
Bram Moolenaar851332e2018-07-03 19:16:00 +02004284 call assert_equal(5, line('.'))
4285 autocmd! QuickFixCmdPost
4286 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004287
4288 " Test for vimgrep/lvimgrep
4289 call g:Xsetlist([], 'f')
4290 Xexpr 'Xtestfile1:2:Line2'
4291 autocmd QuickFixCmdPost * Xolder
4292 silent Xvimgrep Line5 Xtestfile2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004293 call assert_equal('Xtestfile2', @%)
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004294 call assert_equal(5, line('.'))
4295 autocmd! QuickFixCmdPost
4296
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004297 " Test for autocommands clearing the quickfix list before jumping to the
4298 " first error. This should not result in an error
4299 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
4300 let v:errmsg = ''
4301 " Test for cfile/lfile
4302 Xfile Xerr
4303 call assert_true(v:errmsg !~# 'E42:')
4304 " Test for cbuffer/lbuffer
4305 edit Xerr
4306 Xbuffer
4307 call assert_true(v:errmsg !~# 'E42:')
4308 " Test for cexpr/lexpr
4309 Xexpr 'Xtestfile2:4:Line4'
4310 call assert_true(v:errmsg !~# 'E42:')
4311 " Test for grep/lgrep
4312 " The grepprg may not be set on non-Unix systems
4313 if has('unix')
4314 silent Xgrep Line5 Xtestfile2
4315 call assert_true(v:errmsg !~# 'E42:')
4316 endif
4317 " Test for vimgrep/lvimgrep
4318 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
4319 autocmd! QuickFixCmdPost
4320
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004321 call delete('Xerr')
4322 call delete('Xtestfile1')
4323 call delete('Xtestfile2')
4324endfunc
4325
4326func Test_autocmd_changelist()
4327 call Xautocmd_changelist('c')
4328 call Xautocmd_changelist('l')
4329endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004330
4331" Tests for the ':filter /pat/ clist' command
4332func Test_filter_clist()
4333 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4334 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4335 \ split(execute('filter /Line 15/ clist'), "\n"))
4336 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4337 \ split(execute('filter /Xfile1/ clist'), "\n"))
4338 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4339
4340 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4341 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4342 call assert_equal([' 2 pqr:pat2: '],
4343 \ split(execute('filter /pqr/ clist'), "\n"))
4344 call assert_equal([' 1 abc:pat1: '],
4345 \ split(execute('filter /pat1/ clist'), "\n"))
4346endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004347
4348" Tests for the "CTRL-W <CR>" command.
4349func Xview_result_split_tests(cchar)
4350 call s:setup_commands(a:cchar)
4351
4352 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4353 call g:Xsetlist([])
4354 Xopen
4355 let l:win_count = winnr('$')
Bram Moolenaare2e40752020-09-04 21:18:46 +02004356 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42:')
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004357 call assert_equal(l:win_count, winnr('$'))
4358 Xclose
4359endfunc
4360
4361func Test_view_result_split()
4362 call Xview_result_split_tests('c')
4363 call Xview_result_split_tests('l')
4364endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004365
4366" Test that :cc sets curswant
4367func Test_curswant()
4368 helpgrep quickfix
4369 normal! llll
4370 1cc
4371 call assert_equal(getcurpos()[4], virtcol('.'))
4372 cclose | helpclose
4373endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004374
4375" Test for opening a file from the quickfix window using CTRL-W <Enter>
4376" doesn't leave an empty buffer around.
4377func Test_splitview()
4378 call s:create_test_file('Xtestfile1')
4379 call s:create_test_file('Xtestfile2')
4380 new | only
4381 let last_bufnr = bufnr('Test_sv_1', 1)
4382 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4383 cgetexpr l
4384 copen
4385 let numbufs = len(getbufinfo())
4386 exe "normal \<C-W>\<CR>"
4387 copen
4388 exe "normal j\<C-W>\<CR>"
4389 " Make sure new empty buffers are not created
4390 call assert_equal(numbufs, len(getbufinfo()))
4391 " Creating a new buffer should use the next available buffer number
4392 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4393 bwipe Test_sv_1
4394 bwipe Test_sv_2
4395 new | only
4396
4397 " When split opening files from location list window, make sure that two
4398 " windows doesn't refer to the same location list
4399 lgetexpr l
4400 let locid = getloclist(0, {'id' : 0}).id
4401 lopen
4402 exe "normal \<C-W>\<CR>"
4403 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4404 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4405 new | only
4406
4407 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004408 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004409 lhelpgrep window
4410 let locid = getloclist(0, {'id' : 0}).id
4411 lwindow
4412 exe "normal j\<C-W>\<CR>"
4413 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4414 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4415 new | only
4416
Bram Moolenaar406cd902020-02-18 21:54:41 +01004417 " Using :split or :vsplit from a quickfix window should behave like a :new
4418 " or a :vnew command
4419 copen
4420 split
4421 call assert_equal(3, winnr('$'))
4422 let l = getwininfo()
4423 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4424 close
4425 copen
4426 vsplit
4427 let l = getwininfo()
4428 call assert_equal([0, 0, 1], [l[0].quickfix, l[1].quickfix, l[2].quickfix])
4429 new | only
4430
Bram Moolenaarb2443732018-11-11 22:50:27 +01004431 call delete('Xtestfile1')
4432 call delete('Xtestfile2')
4433endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004434
4435" Test for parsing entries using visual screen column
4436func Test_viscol()
4437 enew
4438 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4439 edit Xfile1
4440
4441 " Use byte offset for column number
4442 set efm&
4443 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
4444 call assert_equal([5, 8], [col('.'), virtcol('.')])
4445 cnext
4446 call assert_equal([9, 12], [col('.'), virtcol('.')])
4447 cnext
4448 call assert_equal([14, 20], [col('.'), virtcol('.')])
4449
4450 " Use screen column offset for column number
4451 set efm=%f:%l:%v:%m
4452 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4453 call assert_equal([5, 8], [col('.'), virtcol('.')])
4454 cnext
4455 call assert_equal([9, 12], [col('.'), virtcol('.')])
4456 cnext
4457 call assert_equal([14, 20], [col('.'), virtcol('.')])
4458 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4459 call assert_equal([5, 8], [col('.'), virtcol('.')])
4460 cnext
4461 call assert_equal([10, 16], [col('.'), virtcol('.')])
4462 cnext
4463 call assert_equal([14, 20], [col('.'), virtcol('.')])
4464
4465 enew
4466 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4467
4468 " Use byte offset for column number
4469 set efm&
4470 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
4471 call assert_equal([8, 10], [col('.'), virtcol('.')])
4472 cnext
4473 call assert_equal([11, 17], [col('.'), virtcol('.')])
4474 cnext
4475 call assert_equal([16, 25], [col('.'), virtcol('.')])
4476
4477 " Use screen column offset for column number
4478 set efm=%f:%l:%v:%m
4479 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4480 call assert_equal([8, 10], [col('.'), virtcol('.')])
4481 cnext
4482 call assert_equal([11, 17], [col('.'), virtcol('.')])
4483 cnext
4484 call assert_equal([16, 25], [col('.'), virtcol('.')])
4485
Bram Moolenaarc95940c2020-10-20 14:59:12 +02004486 " Use screen column number with a multi-line error message
4487 enew
4488 call writefile(["à test"], 'Xfile1')
4489 set efm=%E===\ %f\ ===,%C%l:%v,%Z%m
4490 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4491 call assert_equal('Xfile1', @%)
4492 call assert_equal([0, 1, 4, 0], getpos('.'))
4493
4494 " Repeat previous test with byte offset %c: ensure that fix to issue #7145
4495 " does not break this
4496 set efm=%E===\ %f\ ===,%C%l:%c,%Z%m
4497 cexpr ["=== Xfile1 ===", "1:3", "errormsg"]
4498 call assert_equal('Xfile1', @%)
4499 call assert_equal([0, 1, 3, 0], getpos('.'))
4500
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004501 enew | only
4502 set efm&
4503 call delete('Xfile1')
4504endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004505
4506" Test for the quickfix window buffer
4507func Xqfbuf_test(cchar)
4508 call s:setup_commands(a:cchar)
4509
4510 " Quickfix buffer should be reused across closing and opening a quickfix
4511 " window
4512 Xexpr "F1:10:Line10"
4513 Xopen
4514 let qfbnum = bufnr('')
4515 Xclose
4516 " Even after the quickfix window is closed, the buffer should be loaded
4517 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004518 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004519 Xopen
4520 " Buffer should be reused when opening the window again
4521 call assert_equal(qfbnum, bufnr(''))
4522 Xclose
4523
4524 if a:cchar == 'l'
4525 %bwipe
4526 " For a location list, when both the file window and the location list
4527 " window for the list are closed, then the buffer should be freed.
4528 new | only
4529 lexpr "F1:10:Line10"
4530 let wid = win_getid()
4531 lopen
4532 let qfbnum = bufnr('')
4533 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
4534 close
4535 " When the location list window is closed, the buffer name should not
4536 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004537 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004538 call assert_true(bufloaded(qfbnum))
4539
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004540 " After deleting a location list buffer using ":bdelete", opening the
4541 " location list window should mark the buffer as a location list buffer.
4542 exe "bdelete " . qfbnum
4543 lopen
4544 call assert_equal("quickfix", &buftype)
4545 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
4546 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
4547 call assert_false(&swapfile)
4548 lclose
4549
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004550 " When the location list is cleared for the window, the buffer should be
4551 " removed
4552 call setloclist(0, [], 'f')
4553 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004554 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004555
4556 " When the location list is freed with the location list window open, the
4557 " location list buffer should not be lost. It should be reused when the
4558 " location list is again populated.
4559 lexpr "F1:10:Line10"
4560 lopen
4561 let wid = win_getid()
4562 let qfbnum = bufnr('')
4563 wincmd p
4564 call setloclist(0, [], 'f')
4565 lexpr "F1:10:Line10"
4566 lopen
4567 call assert_equal(wid, win_getid())
4568 call assert_equal(qfbnum, bufnr(''))
4569 lclose
4570
4571 " When the window with the location list is closed, the buffer should be
4572 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004573 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004574 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004575 endif
4576endfunc
4577
4578func Test_qfbuf()
4579 call Xqfbuf_test('c')
4580 call Xqfbuf_test('l')
4581endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004582
4583" If there is an autocmd to use only one window, then opening the location
4584" list window used to crash Vim.
4585func Test_winonly_autocmd()
4586 call s:create_test_file('Xtest1')
4587 " Autocmd to show only one Vim window at a time
4588 autocmd WinEnter * only
4589 new
4590 " Load the location list
4591 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
4592 let loclistid = getloclist(0, {'id' : 0}).id
4593 " Open the location list window. Only this window will be shown and the file
4594 " window is closed.
4595 lopen
4596 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4597 " Jump to an entry in the location list and make sure that the cursor is
4598 " positioned correctly.
4599 ll 3
4600 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004601 call assert_equal('Xtest1', @%)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004602 call assert_equal(15, line('.'))
4603 " Cleanup
4604 autocmd! WinEnter
4605 new | only
4606 call delete('Xtest1')
4607endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02004608
4609" Test to make sure that an empty quickfix buffer is not reused for loading
4610" a normal buffer.
4611func Test_empty_qfbuf()
4612 enew | only
4613 call writefile(["Test"], 'Xfile1')
4614 call setqflist([], 'f')
4615 copen | only
4616 let qfbuf = bufnr('')
4617 edit Xfile1
4618 call assert_notequal(qfbuf, bufnr(''))
4619 enew
4620 call delete('Xfile1')
4621endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004622
4623" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004624" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004625func Xtest_below(cchar)
4626 call s:setup_commands(a:cchar)
4627
4628 " No quickfix/location list
4629 call assert_fails('Xbelow', 'E42:')
4630 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004631 call assert_fails('Xbefore', 'E42:')
4632 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004633
4634 " Empty quickfix/location list
4635 call g:Xsetlist([])
4636 call assert_fails('Xbelow', 'E42:')
4637 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004638 call assert_fails('Xbefore', 'E42:')
4639 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004640
4641 call s:create_test_file('X1')
4642 call s:create_test_file('X2')
4643 call s:create_test_file('X3')
4644 call s:create_test_file('X4')
4645
4646 " Invalid entries
4647 edit X1
4648 call g:Xsetlist(["E1", "E2"])
4649 call assert_fails('Xbelow', 'E42:')
4650 call assert_fails('Xabove', 'E42:')
4651 call assert_fails('3Xbelow', 'E42:')
4652 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004653 call assert_fails('Xbefore', 'E42:')
4654 call assert_fails('Xafter', 'E42:')
4655 call assert_fails('3Xbefore', 'E42:')
4656 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004657
4658 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004659 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 +02004660 edit +7 X2
4661 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004662 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004663 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004664 normal 7G
4665 Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004666 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004667 call assert_fails('Xbefore', 'E553:')
4668
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004669 normal 2j
4670 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004671 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004672 normal 7G
4673 Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004674 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004675
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004676 " Last error in this file
4677 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004678 call assert_equal(['X2', 15], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004679 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004680 normal gg
4681 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004682 call assert_equal(['X2', 15, 4], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004683 call assert_fails('Xafter', 'E553:')
4684
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004685 " First error in this file
4686 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004687 call assert_equal(['X2', 5], [@%, line('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004688 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004689 normal G
4690 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004691 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004692 call assert_fails('Xbefore', 'E553:')
4693
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004694 normal gg
4695 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004696 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004697 normal gg
4698 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004699 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004700
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004701 normal G
4702 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004703 call assert_equal(['X2', 10], [@%, line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004704 normal G
4705 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004706 call assert_equal(['X2', 10, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004707
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004708 edit X4
4709 call assert_fails('Xabove', 'E42:')
4710 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004711 call assert_fails('Xbefore', 'E42:')
4712 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004713 if a:cchar == 'l'
4714 " If a buffer has location list entries from some other window but not
4715 " from the current window, then the commands should fail.
4716 edit X1 | split | call setloclist(0, [], 'f')
4717 call assert_fails('Xabove', 'E776:')
4718 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004719 call assert_fails('Xbefore', 'E776:')
4720 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004721 close
4722 endif
4723
4724 " Test for lines with multiple quickfix entries
4725 Xexpr ["X1:5:L5", "X2:5:1:L5_1", "X2:5:2:L5_2", "X2:5:3:L5_3",
4726 \ "X2:10:1:L10_1", "X2:10:2:L10_2", "X2:10:3:L10_3",
4727 \ "X2:15:1:L15_1", "X2:15:2:L15_2", "X2:15:3:L15_3", "X3:3:L3"]
4728 edit +1 X2
4729 Xbelow 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004730 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004731 normal 1G
4732 Xafter 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004733 call assert_equal(['X2', 5, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004734
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004735 normal gg
4736 Xbelow 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004737 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004738 normal gg
4739 Xafter 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004740 call assert_equal(['X2', 15, 3], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004741
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004742 normal G
4743 Xabove 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004744 call assert_equal(['X2', 10, 1], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004745 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004746 Xbefore 2
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004747 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004748
4749 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004750 Xabove 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004751 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004752 normal G
4753 Xbefore 99
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004754 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004755
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004756 normal 10G
4757 Xabove
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004758 call assert_equal(['X2', 5, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004759 normal 10G$
4760 2Xbefore
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004761 call assert_equal(['X2', 10, 2], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004762
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004763 normal 10G
4764 Xbelow
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004765 call assert_equal(['X2', 15, 1], [@%, line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004766 normal 9G
4767 5Xafter
Bram Moolenaarbdd2c292020-06-22 21:34:30 +02004768 call assert_equal(['X2', 15, 2], [@%, line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004769
4770 " Invalid range
4771 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02004772 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004773 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004774 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02004775 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004776 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004777 endif
4778
4779 call delete('X1')
4780 call delete('X2')
4781 call delete('X3')
4782 call delete('X4')
4783endfunc
4784
4785func Test_cbelow()
4786 call Xtest_below('c')
4787 call Xtest_below('l')
4788endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02004789
4790func Test_quickfix_count()
4791 let commands = [
4792 \ 'cNext',
4793 \ 'cNfile',
4794 \ 'cabove',
4795 \ 'cbelow',
4796 \ 'cfirst',
4797 \ 'clast',
4798 \ 'cnewer',
4799 \ 'cnext',
4800 \ 'cnfile',
4801 \ 'colder',
4802 \ 'cprevious',
4803 \ 'crewind',
4804 \
4805 \ 'lNext',
4806 \ 'lNfile',
4807 \ 'labove',
4808 \ 'lbelow',
4809 \ 'lfirst',
4810 \ 'llast',
4811 \ 'lnewer',
4812 \ 'lnext',
4813 \ 'lnfile',
4814 \ 'lolder',
4815 \ 'lprevious',
4816 \ 'lrewind',
4817 \ ]
4818 for cmd in commands
4819 call assert_fails('-1' .. cmd, 'E16:')
4820 call assert_fails('.' .. cmd, 'E16:')
4821 call assert_fails('%' .. cmd, 'E16:')
4822 call assert_fails('$' .. cmd, 'E16:')
4823 endfor
4824endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02004825
4826" Test for aborting quickfix commands using QuickFixCmdPre
4827func Xtest_qfcmd_abort(cchar)
4828 call s:setup_commands(a:cchar)
4829
4830 call g:Xsetlist([], 'f')
4831
4832 " cexpr/lexpr
4833 let e = ''
4834 try
4835 Xexpr ["F1:10:Line10", "F2:20:Line20"]
4836 catch /.*/
4837 let e = v:exception
4838 endtry
4839 call assert_equal('AbortCmd', e)
4840 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4841
4842 " cfile/lfile
4843 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
4844 let e = ''
4845 try
4846 Xfile Xfile1
4847 catch /.*/
4848 let e = v:exception
4849 endtry
4850 call assert_equal('AbortCmd', e)
4851 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4852 call delete('Xfile1')
4853
4854 " cgetbuffer/lgetbuffer
4855 enew!
4856 call append(0, ["F1:10:Line10", "F2:20:Line20"])
4857 let e = ''
4858 try
4859 Xgetbuffer
4860 catch /.*/
4861 let e = v:exception
4862 endtry
4863 call assert_equal('AbortCmd', e)
4864 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4865 enew!
4866
4867 " vimgrep/lvimgrep
4868 let e = ''
4869 try
4870 Xvimgrep /func/ test_quickfix.vim
4871 catch /.*/
4872 let e = v:exception
4873 endtry
4874 call assert_equal('AbortCmd', e)
4875 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4876
4877 " helpgrep/lhelpgrep
4878 let e = ''
4879 try
4880 Xhelpgrep quickfix
4881 catch /.*/
4882 let e = v:exception
4883 endtry
4884 call assert_equal('AbortCmd', e)
4885 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4886
4887 " grep/lgrep
4888 if has('unix')
4889 let e = ''
4890 try
4891 silent Xgrep func test_quickfix.vim
4892 catch /.*/
4893 let e = v:exception
4894 endtry
4895 call assert_equal('AbortCmd', e)
4896 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4897 endif
4898endfunc
4899
4900func Test_qfcmd_abort()
4901 augroup QF_Test
4902 au!
4903 autocmd QuickFixCmdPre * throw "AbortCmd"
4904 augroup END
4905
4906 call Xtest_qfcmd_abort('c')
4907 call Xtest_qfcmd_abort('l')
4908
4909 augroup QF_Test
4910 au!
4911 augroup END
4912endfunc
4913
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004914" Test for using a file in one of the parent directories.
4915func Test_search_in_dirstack()
4916 call mkdir('Xtestdir/a/b/c', 'p')
4917 let save_cwd = getcwd()
4918 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
4919 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
4920 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
4921 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
4922
4923 let lines = "Entering dir Xtestdir\n" .
4924 \ "Entering dir a\n" .
4925 \ "Entering dir b\n" .
4926 \ "Xfile2:2:X2_L2\n" .
4927 \ "Leaving dir a\n" .
4928 \ "Xfile1:2:X1_L2\n" .
4929 \ "Xfile3:1:X3_L1\n" .
4930 \ "Entering dir c\n" .
4931 \ "Xfile4:2:X4_L2\n" .
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01004932 \ "Leaving dir c\n"
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004933 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01004934 cexpr lines .. "Leaving dir Xtestdir|\n" | let next = 1
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004935 call assert_equal(11, getqflist({'size' : 0}).size)
4936 call assert_equal(4, getqflist({'idx' : 0}).idx)
4937 call assert_equal('X2_L2', getline('.'))
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01004938 call assert_equal(1, next)
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004939 cnext
4940 call assert_equal(6, getqflist({'idx' : 0}).idx)
4941 call assert_equal('X1_L2', getline('.'))
4942 cnext
4943 call assert_equal(7, getqflist({'idx' : 0}).idx)
4944 call assert_equal(1, line('$'))
4945 call assert_equal('', getline(1))
4946 cnext
4947 call assert_equal(9, getqflist({'idx' : 0}).idx)
4948 call assert_equal(1, line('$'))
4949 call assert_equal('', getline(1))
4950
4951 set efm&
4952 exe 'cd ' . save_cwd
4953 call delete('Xtestdir', 'rf')
4954endfunc
4955
Bram Moolenaar1860bde2020-01-06 21:47:21 +01004956" Test for :cquit
4957func Test_cquit()
4958 " Exit Vim with a non-zero value
4959 if RunVim([], ["cquit 7"], '')
4960 call assert_equal(7, v:shell_error)
4961 endif
4962
4963 if RunVim([], ["50cquit"], '')
4964 call assert_equal(50, v:shell_error)
4965 endif
4966
4967 " Exit Vim with default value
4968 if RunVim([], ["cquit"], '')
4969 call assert_equal(1, v:shell_error)
4970 endif
4971
4972 " Exit Vim with zero value
4973 if RunVim([], ["cquit 0"], '')
4974 call assert_equal(0, v:shell_error)
4975 endif
4976
4977 " Exit Vim with negative value
4978 call assert_fails('-3cquit', 'E16:')
4979endfunc
4980
Bram Moolenaar858ba062020-05-31 23:11:59 +02004981" Test for getting a specific item from a quickfix list
4982func Xtest_getqflist_by_idx(cchar)
4983 call s:setup_commands(a:cchar)
4984 " Empty list
4985 call assert_equal([], g:Xgetlist({'idx' : 1, 'items' : 0}).items)
4986 Xexpr ['F1:10:L10', 'F1:20:L20']
4987 let l = g:Xgetlist({'idx' : 2, 'items' : 0}).items
4988 call assert_equal(bufnr('F1'), l[0].bufnr)
4989 call assert_equal(20, l[0].lnum)
4990 call assert_equal('L20', l[0].text)
4991 call assert_equal([], g:Xgetlist({'idx' : -1, 'items' : 0}).items)
4992 call assert_equal([], g:Xgetlist({'idx' : 3, 'items' : 0}).items)
4993 %bwipe!
4994endfunc
4995
4996func Test_getqflist_by_idx()
4997 call Xtest_getqflist_by_idx('c')
4998 call Xtest_getqflist_by_idx('l')
4999endfunc
5000
5001" Test for the 'quickfixtextfunc' setting
5002func Tqfexpr(info)
5003 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005004 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005005 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005006 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005007 endif
5008
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005009 let l = []
5010 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5011 let e = qfl[idx]
5012 let s = ''
5013 if e.bufnr != 0
5014 let bname = bufname(e.bufnr)
5015 let s ..= fnamemodify(bname, ':.')
5016 endif
5017 let s ..= '-'
5018 let s ..= 'L' .. string(e.lnum) .. 'C' .. string(e.col) .. '-'
5019 let s ..= e.text
5020 call add(l, s)
5021 endfor
Bram Moolenaar858ba062020-05-31 23:11:59 +02005022
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005023 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005024endfunc
5025
5026func Xtest_qftextfunc(cchar)
5027 call s:setup_commands(a:cchar)
5028
5029 set efm=%f:%l:%c:%m
5030 set quickfixtextfunc=Tqfexpr
Bram Moolenaard43906d2020-07-20 21:31:32 +02005031 call assert_equal('Tqfexpr', &quickfixtextfunc)
5032 call assert_equal('',
5033 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar858ba062020-05-31 23:11:59 +02005034 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5035 Xwindow
5036 call assert_equal('F1-L10C2-green', getline(1))
5037 call assert_equal('F1-L20C4-blue', getline(2))
5038 Xclose
5039 set quickfixtextfunc&vim
5040 Xwindow
5041 call assert_equal('F1|10 col 2| green', getline(1))
5042 call assert_equal('F1|20 col 4| blue', getline(2))
5043 Xclose
5044 set efm&
5045 set quickfixtextfunc&
5046
5047 " Test for per list 'quickfixtextfunc' setting
5048 func PerQfText(info)
5049 if a:info.quickfix
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005050 let qfl = getqflist({'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005051 else
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005052 let qfl = getloclist(a:info.winid, {'id' : a:info.id, 'items' : 1}).items
Bram Moolenaar858ba062020-05-31 23:11:59 +02005053 endif
5054 if empty(qfl)
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005055 return []
Bram Moolenaar858ba062020-05-31 23:11:59 +02005056 endif
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005057 let l = []
5058 for idx in range(a:info.start_idx - 1, a:info.end_idx - 1)
5059 call add(l, 'Line ' .. qfl[idx].lnum .. ', Col ' .. qfl[idx].col)
5060 endfor
5061 return l
Bram Moolenaar858ba062020-05-31 23:11:59 +02005062 endfunc
5063 set quickfixtextfunc=Tqfexpr
5064 call g:Xsetlist([], ' ', {'quickfixtextfunc' : "PerQfText"})
5065 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5066 Xwindow
5067 call assert_equal('Line 10, Col 2', getline(1))
5068 call assert_equal('Line 20, Col 4', getline(2))
5069 Xclose
Bram Moolenaard43906d2020-07-20 21:31:32 +02005070 call assert_equal(function('PerQfText'),
5071 \ g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar7ba5a7e2020-06-08 19:20:27 +02005072 " Add entries to the list when the quickfix buffer is hidden
5073 Xaddexpr ['F1:30:6:red']
5074 Xwindow
5075 call assert_equal('Line 30, Col 6', getline(3))
5076 Xclose
Bram Moolenaar858ba062020-05-31 23:11:59 +02005077 call g:Xsetlist([], 'r', {'quickfixtextfunc' : ''})
Bram Moolenaard43906d2020-07-20 21:31:32 +02005078 call assert_equal('', g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc)
Bram Moolenaar858ba062020-05-31 23:11:59 +02005079 set quickfixtextfunc&
5080 delfunc PerQfText
5081
5082 " Non-existing function
5083 set quickfixtextfunc=Tabc
5084 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5085 call assert_fails("Xwindow", 'E117:')
5086 Xclose
5087 set quickfixtextfunc&
5088
5089 " set option to a non-function
5090 set quickfixtextfunc=[10,\ 20]
5091 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E117:')
5092 call assert_fails("Xwindow", 'E117:')
5093 Xclose
5094 set quickfixtextfunc&
5095
5096 " set option to a function with different set of arguments
5097 func Xqftext(a, b, c)
5098 return a:a .. a:b .. a:c
5099 endfunc
5100 set quickfixtextfunc=Xqftext
5101 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue']", 'E119:')
5102 call assert_fails("Xwindow", 'E119:')
5103 Xclose
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005104
5105 " set option to a function that returns a list with non-strings
5106 func Xqftext2(d)
5107 return ['one', [], 'two']
5108 endfunc
5109 set quickfixtextfunc=Xqftext2
5110 call assert_fails("Xexpr ['F1:10:2:green', 'F1:20:4:blue', 'F1:30:6:red']",
5111 \ 'E730:')
5112 call assert_fails('Xwindow', 'E730:')
Bram Moolenaard43906d2020-07-20 21:31:32 +02005113 call assert_equal(['one', 'F1|20 col 4| blue', 'F1|30 col 6| red'],
5114 \ getline(1, '$'))
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005115 Xclose
5116
Bram Moolenaar858ba062020-05-31 23:11:59 +02005117 set quickfixtextfunc&
5118 delfunc Xqftext
Bram Moolenaar00e260b2020-06-11 19:35:52 +02005119 delfunc Xqftext2
Bram Moolenaard43906d2020-07-20 21:31:32 +02005120
5121 " set the global option to a lambda function
5122 set quickfixtextfunc={d\ ->\ map(g:Xgetlist({'id'\ :\ d.id,\ 'items'\ :\ 1}).items[d.start_idx-1:d.end_idx-1],\ 'v:val.text')}
5123 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5124 Xwindow
5125 call assert_equal(['green', 'blue'], getline(1, '$'))
5126 Xclose
5127 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)
5128 set quickfixtextfunc&
5129
5130 " use a lambda function that returns an empty list
5131 set quickfixtextfunc={d\ ->\ []}
5132 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5133 Xwindow
5134 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5135 \ getline(1, '$'))
5136 Xclose
5137 set quickfixtextfunc&
5138
5139 " use a lambda function that returns a list with empty strings
5140 set quickfixtextfunc={d\ ->\ ['',\ '']}
5141 Xexpr ['F1:10:2:green', 'F1:20:4:blue']
5142 Xwindow
5143 call assert_equal(['F1|10 col 2| green', 'F1|20 col 4| blue'],
5144 \ getline(1, '$'))
5145 Xclose
5146 set quickfixtextfunc&
5147
5148 " set the per-quickfix list text function to a lambda function
5149 call g:Xsetlist([], ' ',
5150 \ {'quickfixtextfunc' :
5151 \ {d -> map(g:Xgetlist({'id' : d.id, 'items' : 1}).items[d.start_idx-1:d.end_idx-1],
5152 \ "'Line ' .. v:val.lnum .. ', Col ' .. v:val.col")}})
5153 Xaddexpr ['F1:10:2:green', 'F1:20:4:blue']
5154 Xwindow
5155 call assert_equal('Line 10, Col 2', getline(1))
5156 call assert_equal('Line 20, Col 4', getline(2))
5157 Xclose
5158 call assert_match("function('<lambda>\\d\\+')", string(g:Xgetlist({'quickfixtextfunc' : 1}).quickfixtextfunc))
5159 call g:Xsetlist([], 'f')
Bram Moolenaar858ba062020-05-31 23:11:59 +02005160endfunc
5161
5162func Test_qftextfunc()
5163 call Xtest_qftextfunc('c')
5164 call Xtest_qftextfunc('l')
5165endfunc
5166
Bram Moolenaarec98e932020-06-08 19:35:59 +02005167" Running :lhelpgrep command more than once in a help window, doesn't jump to
5168" the help topic
5169func Test_lhelpgrep_from_help_window()
5170 call mkdir('Xtestdir/doc', 'p')
5171 call writefile(['window'], 'Xtestdir/doc/a.txt')
5172 call writefile(['buffer'], 'Xtestdir/doc/b.txt')
5173 let save_rtp = &rtp
5174 let &rtp = 'Xtestdir'
5175 lhelpgrep window
5176 lhelpgrep buffer
5177 call assert_equal('b.txt', fnamemodify(@%, ":p:t"))
5178 lhelpgrep window
5179 call assert_equal('a.txt', fnamemodify(@%, ":p:t"))
5180 let &rtp = save_rtp
5181 call delete('Xtestdir', 'rf')
5182 new | only!
5183endfunc
5184
Bram Moolenaarf7c4d832020-08-11 20:42:19 +02005185" Test for the crash fixed by 7.3.715
5186func Test_setloclist_crash()
5187 %bw!
5188 let g:BufNum = bufnr()
5189 augroup QF_Test
5190 au!
5191 au BufUnload * call setloclist(0, [{'bufnr':g:BufNum, 'lnum':1, 'col':1, 'text': 'tango down'}])
5192 augroup END
5193
5194 try
5195 lvimgrep /.*/ *.mak
5196 catch /E926:/
5197 endtry
5198 call assert_equal('tango down', getloclist(0, {'items' : 0}).items[0].text)
5199 call assert_equal(1, getloclist(0, {'size' : 0}).size)
5200
5201 augroup QF_Test
5202 au!
5203 augroup END
5204 unlet g:BufNum
5205 %bw!
5206endfunc
5207
Bram Moolenaar2ce77902020-11-14 13:15:24 +01005208" Test for adding an invalid entry with the quickfix window open and making
5209" sure that the window contents are not changed
5210func Test_add_invalid_entry_with_qf_window()
5211 call setqflist([], 'f')
5212 cexpr "Xfile1:10:aa"
5213 copen
5214 call setqflist(['bb'], 'a')
5215 call assert_equal(1, line('$'))
5216 call assert_equal(['Xfile1|10| aa'], getline(1, '$'))
5217 call assert_equal([{'lnum': 10, 'bufnr': bufnr('Xfile1'), 'col': 0, 'pattern': '', 'valid': 1, 'vcol': 0, 'nr': -1, 'type': '', 'module': '', 'text': 'aa'}], getqflist())
5218 cclose
5219endfunc
5220
Bram Moolenaar9e40c4b2020-11-23 20:15:08 +01005221" Test for very weird problem: autocommand causes a failure, resulting opening
5222" the quickfix window to fail. This still splits the window, but otherwise
5223" should not mess up buffers.
5224func Test_quickfix_window_fails_to_open()
5225 CheckScreendump
5226
5227 let lines =<< trim END
5228 anything
5229 try
5230 anything
5231 endtry
5232 END
5233 call writefile(lines, 'XquickfixFails')
5234
5235 let lines =<< trim END
5236 split XquickfixFails
5237 silent vimgrep anything %
5238 normal o
5239 au BufLeave * ++once source XquickfixFails
5240 " This will trigger the autocommand, which causes an error, what follows
5241 " is aborted but the window was already split.
5242 silent! cwindow
5243 END
5244 call writefile(lines, 'XtestWinFails')
5245 let buf = RunVimInTerminal('-S XtestWinFails', #{rows: 13})
5246 call VerifyScreenDump(buf, 'Test_quickfix_window_fails', {})
5247
5248 " clean up
5249 call term_sendkeys(buf, ":bwipe!\<CR>")
5250 call term_wait(buf)
5251 call StopVimInTerminal(buf)
5252 call delete('XtestWinFails')
5253 call delete('XquickfixFails')
5254endfunc
5255
Bram Moolenaar287153c2020-11-29 14:20:27 +01005256" Test for updating the quickfix buffer whenever the assocaited quickfix list
5257" is changed.
5258func Xqfbuf_update(cchar)
5259 call s:setup_commands(a:cchar)
5260
5261 Xexpr "F1:1:line1"
5262 Xopen
5263 call assert_equal(['F1|1| line1'], getline(1, '$'))
5264 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5265
5266 " Test setqflist() using the 'lines' key in 'what'
5267 " add a new entry
5268 call g:Xsetlist([], 'a', {'lines' : ['F2:2: line2']})
5269 call assert_equal(['F1|1| line1', 'F2|2| line2'], getline(1, '$'))
5270 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
5271 " replace all the entries with a single entry
5272 call g:Xsetlist([], 'r', {'lines' : ['F3:3: line3']})
5273 call assert_equal(['F3|3| line3'], getline(1, '$'))
5274 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
5275 " remove all the entries
5276 call g:Xsetlist([], 'r', {'lines' : []})
5277 call assert_equal([''], getline(1, '$'))
5278 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
5279 " add a new list
5280 call g:Xsetlist([], ' ', {'lines' : ['F4:4: line4']})
5281 call assert_equal(['F4|4| line4'], getline(1, '$'))
5282 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5283
5284 " Test setqflist() using the 'items' key in 'what'
5285 " add a new entry
5286 call g:Xsetlist([], 'a', {'items' : [{'filename' : 'F5', 'lnum' : 5, 'text' : 'line5'}]})
5287 call assert_equal(['F4|4| line4', 'F5|5| line5'], getline(1, '$'))
5288 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
5289 " replace all the entries with a single entry
5290 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F6', 'lnum' : 6, 'text' : 'line6'}]})
5291 call assert_equal(['F6|6| line6'], getline(1, '$'))
5292 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
5293 " remove all the entries
5294 call g:Xsetlist([], 'r', {'items' : []})
5295 call assert_equal([''], getline(1, '$'))
5296 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
5297 " add a new list
5298 call g:Xsetlist([], ' ', {'items' : [{'filename' : 'F7', 'lnum' : 7, 'text' : 'line7'}]})
5299 call assert_equal(['F7|7| line7'], getline(1, '$'))
5300 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5301
5302 call g:Xsetlist([], ' ', {})
5303 call assert_equal([''], getline(1, '$'))
5304 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
5305
5306 Xclose
5307endfunc
5308
5309func Test_qfbuf_update()
5310 call Xqfbuf_update('c')
5311 call Xqfbuf_update('l')
5312endfunc
5313
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02005314" vim: shiftwidth=2 sts=2 expandtab