blob: d3f2e3abd7bf423fe180dc767fe1ed54c53941c6 [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'
264 \ && winheight('.') == 5)
265
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 &&
270 \ winheight('.') == 7 &&
271 \ 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 Moolenaarb6fa30c2017-03-29 14:19:25 +0200289 if a:cchar == 'c'
290 " Opening the quickfix window in multiple tab pages should reuse the
291 " quickfix buffer
292 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
293 \ 'Xtestfile3:3:1:Line3']
294 Xopen
295 let qfbufnum = bufnr('%')
296 tabnew
297 Xopen
298 call assert_equal(qfbufnum, bufnr('%'))
299 new | only | tabonly
300 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100301endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100302
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100303func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100304 call XwindowTests('c')
305 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100306endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100307
Bram Moolenaar36d50222019-05-02 20:17:40 +0200308func Test_copenHeight()
309 copen
310 wincmd H
311 let height = winheight(0)
312 copen 10
313 call assert_equal(height, winheight(0))
314 quit
315endfunc
316
Bram Moolenaar1142a312019-10-16 14:51:39 +0200317func Test_copenHeight_tabline()
318 set tabline=foo showtabline=2
319 copen
320 wincmd H
321 let height = winheight(0)
322 copen 10
323 call assert_equal(height, winheight(0))
324 quit
325 set tabline& showtabline&
326endfunc
327
328
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100329" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
330" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100331func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200332 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100333
334 call writefile(['Xtestfile1:700:10:Line 700',
335 \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
336
337 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200338 Xfile Xqftestfile1
339 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100340 call assert_true(len(l) == 2 &&
341 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
342 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
343
Bram Moolenaar049cba92016-06-26 14:38:04 +0200344 " Test with a non existent file
345 call assert_fails('Xfile non_existent_file', 'E40')
346
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100347 " Run cfile/lfile from a modified buffer
348 enew!
349 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200350 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100351 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
352
353 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200354 Xaddfile Xqftestfile1
355 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100356 call assert_true(len(l) == 3 &&
357 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
358
359 call writefile(['Xtestfile1:222:77:Line 222',
360 \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
361
362 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200363 Xgetfile Xqftestfile1
364 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100365 call assert_true(len(l) == 2 &&
366 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
367 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
368
Bram Moolenaar70077dd2019-11-24 12:12:42 +0100369 " Test for a file with a long line and without a newline at the end
370 let text = repeat('x', 1024)
371 let t = 'a.txt:18:' . text
372 call writefile([t], 'Xqftestfile1', 'b')
373 silent! Xfile Xqftestfile1
374 call assert_equal(text, g:Xgetlist()[0].text)
375
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100376 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100377endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100378
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100379func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100380 call XfileTests('c')
381 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100382endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100383
384" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
385" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100386func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200387 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100388
389 enew!
390 silent! call setline(1, ['Xtestfile7:700:10:Line 700',
391 \ 'Xtestfile8:800:15:Line 800'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200392 Xbuffer!
393 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100394 call assert_true(len(l) == 2 &&
395 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
396 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
397
398 enew!
399 silent! call setline(1, ['Xtestfile9:900:55:Line 900',
400 \ 'Xtestfile10:950:66:Line 950'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200401 Xgetbuffer
402 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100403 call assert_true(len(l) == 2 &&
404 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
405 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
406
407 enew!
408 silent! call setline(1, ['Xtestfile11:700:20:Line 700',
409 \ 'Xtestfile12:750:25:Line 750'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200410 Xaddbuffer
411 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100412 call assert_true(len(l) == 4 &&
413 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
414 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
415 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200416 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100417
Bram Moolenaaree85df32017-03-19 14:19:50 +0100418 " Check for invalid buffer
419 call assert_fails('Xbuffer 199', 'E474:')
420
421 " Check for unloaded buffer
422 edit Xtestfile1
423 let bnr = bufnr('%')
424 enew!
425 call assert_fails('Xbuffer ' . bnr, 'E681:')
426
427 " Check for invalid range
428 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
429 " commands. So directly call the commands.
430 if (a:cchar == 'c')
431 call assert_fails('900,999cbuffer', 'E16:')
432 else
433 call assert_fails('900,999lbuffer', 'E16:')
434 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100435endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100436
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100437func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100438 call XbufferTests('c')
439 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100440endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100441
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100442func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200443 call s:setup_commands(a:cchar)
444
445 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100446endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200447
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100448func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200449 call XexprTests('c')
450 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100451endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200452
453" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100454func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200455 call s:setup_commands(a:cchar)
456
Bram Moolenaar74240d32017-12-10 15:26:15 +0100457 call g:Xsetlist([], 'f')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200458 " Jumping to first or next location list entry without any error should
459 " result in failure
Bram Moolenaar74240d32017-12-10 15:26:15 +0100460 if a:cchar == 'c'
461 let err = 'E42:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100462 let cmd = '$cc'
Bram Moolenaar74240d32017-12-10 15:26:15 +0100463 else
464 let err = 'E776:'
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100465 let cmd = '$ll'
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200466 endif
Bram Moolenaar74240d32017-12-10 15:26:15 +0100467 call assert_fails('Xnext', err)
468 call assert_fails('Xprev', err)
469 call assert_fails('Xnfile', err)
470 call assert_fails('Xpfile', err)
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +0100471 call assert_fails(cmd, err)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200472
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100473 Xexpr ''
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100474 call assert_fails(cmd, 'E42:')
475
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200476 call s:create_test_file('Xqftestfile1')
477 call s:create_test_file('Xqftestfile2')
478
479 Xgetexpr ['Xqftestfile1:5:Line5',
480 \ 'Xqftestfile1:6:Line6',
481 \ 'Xqftestfile2:10:Line10',
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200482 \ 'Xqftestfile2:11:Line11',
483 \ 'RegularLine1',
484 \ 'RegularLine2']
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200485
486 Xfirst
487 call assert_fails('Xprev', 'E553')
488 call assert_fails('Xpfile', 'E553')
489 Xnfile
490 call assert_equal('Xqftestfile2', bufname('%'))
491 call assert_equal(10, line('.'))
492 Xpfile
493 call assert_equal('Xqftestfile1', bufname('%'))
494 call assert_equal(6, line('.'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100495 5Xcc
496 call assert_equal(5, g:Xgetlist({'idx':0}).idx)
497 2Xcc
498 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100499 if a:cchar == 'c'
500 cc
501 else
502 ll
503 endif
504 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100505 10Xcc
506 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200507 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200508 Xprev
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200509 call assert_equal('Xqftestfile2', bufname('%'))
510 call assert_equal(11, line('.'))
511 call assert_fails('Xnext', 'E553')
512 call assert_fails('Xnfile', 'E553')
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100513 " To process the range using quickfix list entries, directly use the
514 " quickfix commands (don't use the user defined commands)
515 if a:cchar == 'c'
516 $cc
517 else
518 $ll
519 endif
520 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200521 Xrewind
522 call assert_equal('Xqftestfile1', bufname('%'))
523 call assert_equal(5, line('.'))
524
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200525 10Xnext
526 call assert_equal('Xqftestfile2', bufname('%'))
527 call assert_equal(11, line('.'))
528 10Xprev
529 call assert_equal('Xqftestfile1', bufname('%'))
530 call assert_equal(5, line('.'))
531
Bram Moolenaar74240d32017-12-10 15:26:15 +0100532 " Jumping to an error from the error window using cc command
533 Xgetexpr ['Xqftestfile1:5:Line5',
534 \ 'Xqftestfile1:6:Line6',
535 \ 'Xqftestfile2:10:Line10',
536 \ 'Xqftestfile2:11:Line11']
537 Xopen
538 10Xcc
539 call assert_equal(11, line('.'))
540 call assert_equal('Xqftestfile2', bufname('%'))
Bram Moolenaarf0cee192020-02-16 13:33:56 +0100541 Xopen
542 call cursor(2, 1)
543 if a:cchar == 'c'
544 .cc
545 else
546 .ll
547 endif
548 call assert_equal(6, line('.'))
549 call assert_equal('Xqftestfile1', bufname('%'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100550
551 " Jumping to an error from the error window (when only the error window is
552 " present)
553 Xopen | only
554 Xlast 1
555 call assert_equal(5, line('.'))
556 call assert_equal('Xqftestfile1', bufname('%'))
557
Bram Moolenaaree85df32017-03-19 14:19:50 +0100558 Xexpr ""
559 call assert_fails('Xnext', 'E42:')
560
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200561 call delete('Xqftestfile1')
562 call delete('Xqftestfile2')
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200563
564 " Should be able to use next/prev with invalid entries
565 Xexpr ""
566 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
567 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +0100568 Xaddexpr ['foo', 'bar', 'baz', 'quux', 'sh|moo']
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200569 call assert_equal(5, g:Xgetlist({'size' : 0}).size)
570 Xlast
571 call assert_equal(5, g:Xgetlist({'idx' : 0}).idx)
572 Xfirst
573 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
574 2Xnext
575 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100576endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200577
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100578func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200579 call Xtest_browse('c')
580 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100581endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200582
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100583func Test_nomem()
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200584 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100585 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100586
Bram Moolenaarce90e362019-09-08 18:58:44 +0200587 call GetAllocId('qf_dirname_now')->test_alloc_fail(0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100588 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100589
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200590 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100591 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100592
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200593 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100594 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100595
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200596 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100597 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100598
599endfunc
600
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100601func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200602 call s:setup_commands(a:cchar)
603 Xhelpgrep quickfix
604 Xopen
605 if a:cchar == 'c'
606 let title_text = ':helpgrep quickfix'
607 else
608 let title_text = ':lhelpgrep quickfix'
609 endif
610 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200611
612 " Jumping to a help topic should open the help window
613 only
614 Xnext
615 call assert_true(&buftype == 'help')
616 call assert_true(winnr('$') == 2)
617 " Jumping to the next match should reuse the help window
618 Xnext
619 call assert_true(&buftype == 'help')
620 call assert_true(winnr() == 1)
621 call assert_true(winnr('$') == 2)
622 " Jumping to the next match from the quickfix window should reuse the help
623 " window
624 Xopen
625 Xnext
626 call assert_true(&buftype == 'help')
627 call assert_true(winnr() == 1)
628 call assert_true(winnr('$') == 2)
629
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100630 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200631 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100632
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100633 " When the current window is vertically split, jumping to a help match
634 " should open the help window at the top.
635 only | enew
636 let w1 = win_getid()
637 vert new
638 let w2 = win_getid()
639 Xnext
640 let w3 = win_getid()
641 call assert_true(&buftype == 'help')
642 call assert_true(winnr() == 1)
Bram Moolenaara106e6c2019-11-24 22:13:58 +0100643 " See jump_to_help_window() for details
644 let w2_width = winwidth(w2)
645 if w2_width != &columns && w2_width < 80
646 call assert_equal(['col', [['leaf', w3],
647 \ ['row', [['leaf', w2], ['leaf', w1]]]]], winlayout())
648 else
649 call assert_equal(['row', [['col', [['leaf', w3], ['leaf', w2]]],
650 \ ['leaf', w1]]] , winlayout())
651 endif
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100652
653 new | only
654 set buftype=help
655 set modified
656 call assert_fails('Xnext', 'E37:')
657 set nomodified
658 new | only
659
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200660 if a:cchar == 'l'
661 " When a help window is present, running :lhelpgrep should reuse the
662 " help window and not the current window
663 new | only
664 call g:Xsetlist([], 'f')
665 help index.txt
666 wincmd w
667 lhelpgrep quickfix
668 call assert_equal(1, winnr())
669 call assert_notequal([], getloclist(1))
670 call assert_equal([], getloclist(2))
671 endif
672
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200673 new | only
674
Bram Moolenaaree85df32017-03-19 14:19:50 +0100675 " Search for non existing help string
676 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar108e7b42018-10-11 17:39:12 +0200677 " Invalid regular expression
678 call assert_fails('Xhelpgrep \@<!', 'E480:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100679endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200680
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100681func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200682 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200683 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200684 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100685endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100686
Bram Moolenaar6920c722016-01-22 22:44:10 +0100687func Test_errortitle()
688 augroup QfBufWinEnter
689 au!
690 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
691 augroup END
692 copen
693 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'')'}]
694 call setqflist(a)
695 call assert_equal(':setqflist()', g:a)
696 augroup QfBufWinEnter
697 au!
698 augroup END
699 augroup! QfBufWinEnter
700endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100701
Bram Moolenaar5584df62016-03-18 21:00:51 +0100702func Test_vimgreptitle()
703 augroup QfBufWinEnter
704 au!
705 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
706 augroup END
707 try
708 vimgrep /pattern/j file
709 catch /E480/
710 endtry
711 copen
712 call assert_equal(': vimgrep /pattern/j file', g:a)
713 augroup QfBufWinEnter
714 au!
715 augroup END
716 augroup! QfBufWinEnter
717endfunc
718
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100719func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200720 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +0100721
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200722 Xgetexpr ['file:1:1:message']
723 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100724 if a:cchar == 'c'
725 call setqflist(l, 'r')
726 else
727 call setloclist(0, l, 'r')
728 endif
729
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200730 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +0100731 if a:cchar == 'c'
732 let title = ':setqflist()'
733 else
734 let title = ':setloclist()'
735 endif
736 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200737 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100738endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100739
740" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100741func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200742 call XqfTitleTests('c')
743 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100744endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100745
746" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100747func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100748 let save_efm = &efm
749 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
750 cgetexpr ['WWWW', 'EEEE', 'CCCC']
751 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
752 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
753 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
754 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
755 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
756 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
757 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
758 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
759 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100760endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100761
762" This will test for problems in quickfix:
763" A. incorrectly copying location lists which caused the location list to show
764" a different name than the file that was actually being displayed.
765" B. not reusing the window for which the location list window is opened but
766" instead creating new windows.
767" C. make sure that the location list window is not reused instead of the
768" window it belongs to.
769"
770" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100771func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100772 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
773 let word = substitute(base, '\v(.*)\..*', '\1', '')
774
775 setl modifiable
776 setl noreadonly
777 setl noswapfile
778 setl bufhidden=delete
779 %del _
780 " For problem 2:
781 " 'buftype' has to be set to reproduce the constant opening of new windows
782 setl buftype=nofile
783
784 call setline(1, word)
785
786 setl nomodified
787 setl nomodifiable
788 setl readonly
789 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100790endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100791
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100792func Test_locationlist()
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100793 enew
794
795 augroup testgroup
796 au!
797 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
798 augroup END
799
800 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
801
802 let qflist = []
803 for word in words
804 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
805 " NOTE: problem 1:
806 " intentionally not setting 'lnum' so that the quickfix entries are not
807 " valid
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200808 eval qflist->setloclist(0, ' ')
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100809 endfor
810
811 " Test A
812 lrewind
813 enew
814 lopen
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200815 4lnext
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100816 vert split
817 wincmd L
818 lopen
819 wincmd p
820 lnext
821 let fileName = expand("%")
822 wincmd p
823 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
824 let fileName = substitute(fileName, '\\', '/', 'g')
825 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
826 call assert_equal("test://bar.txt", fileName)
827 call assert_equal("test://bar.txt", locationListFileName)
828
829 wincmd n | only
830
831 " Test B:
832 lrewind
833 lopen
834 2
835 exe "normal \<CR>"
836 wincmd p
837 3
838 exe "normal \<CR>"
839 wincmd p
840 4
841 exe "normal \<CR>"
842 call assert_equal(2, winnr('$'))
843 wincmd n | only
844
845 " Test C:
846 lrewind
847 lopen
848 " Let's move the location list window to the top to check whether it (the
849 " first window found) will be reused when we try to open new windows:
850 wincmd K
851 2
852 exe "normal \<CR>"
853 wincmd p
854 3
855 exe "normal \<CR>"
856 wincmd p
857 4
858 exe "normal \<CR>"
859 1wincmd w
860 call assert_equal('quickfix', &buftype)
861 2wincmd w
862 let bufferName = expand("%")
863 let bufferName = substitute(bufferName, '\\', '/', 'g')
864 call assert_equal('test://quux.txt', bufferName)
865
866 wincmd n | only
867
868 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100869endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +0100870
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100871func Test_locationlist_curwin_was_closed()
Bram Moolenaar0899d692016-03-19 13:35:03 +0100872 augroup testgroup
873 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200874 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
Bram Moolenaar0899d692016-03-19 13:35:03 +0100875 augroup END
876
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100877 func! R(n)
Bram Moolenaar0899d692016-03-19 13:35:03 +0100878 quit
879 endfunc
880
881 new
882 let q = []
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200883 call add(q, {'filename': 'test_curwin.txt' })
Bram Moolenaar0899d692016-03-19 13:35:03 +0100884 call setloclist(0, q)
885 call assert_fails('lrewind', 'E924:')
886
887 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100888endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100889
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100890func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200891 call writefile(['loclistfoo'], 'loclistfoo')
892 call writefile(['loclistbar'], 'loclistbar')
893 set switchbuf=usetab
894
895 edit loclistfoo
896 tabedit loclistbar
897 silent lgrep loclistfoo loclist*
898 call assert_equal(1, tabpagenr())
899
900 enew | only | tabonly
901 set switchbuf&vim
902 call delete('loclistfoo')
903 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100904endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200905
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100906" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100907func Test_efm1()
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100908 if !has('unix')
909 " The 'errorformat' setting is different on non-Unix systems.
910 " This test works only on Unix-like systems.
911 return
912 endif
913
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200914 let l =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200915 "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.
916 "Xtestfile", line 6 col 19; this is an error
917 gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c
918 Xtestfile:9: parse error before `asd'
919 make: *** [vim] Error 1
920 in file "Xtestfile" linenr 10: there is an error
921
922 2 returned
923 "Xtestfile", line 11 col 1; this is an error
924 "Xtestfile", line 12 col 2; this is another error
925 "Xtestfile", line 14:10; this is an error in column 10
926 =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time
927 "Xtestfile", linenr 16: yet another problem
928 Error in "Xtestfile" at line 17:
929 x should be a dot
930 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
931 ^
932 Error in "Xtestfile" at line 18:
933 x should be a dot
934 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
935 .............^
936 Error in "Xtestfile" at line 19:
937 x should be a dot
938 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
939 --------------^
940 Error in "Xtestfile" at line 20:
941 x should be a dot
942 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
943 ^
944
945 Does anyone know what is the problem and how to correction it?
946 "Xtestfile", line 21 col 9: What is the title of the quickfix window?
947 "Xtestfile", line 22 col 9: What is the title of the quickfix window?
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200948 [DATA]
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100949
950 call writefile(l, 'Xerrorfile1')
951 call writefile(l[:-2], 'Xerrorfile2')
952
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200953 let m =<< [DATA]
954 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2
955 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3
956 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4
957 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5
958 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6
959 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7
960 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8
961 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9
962 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10
963 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11
964 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12
965 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13
966 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14
967 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15
968 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16
969 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
970 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
971 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
972 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
973 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21
974 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22
975[DATA]
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100976 call writefile(m, 'Xtestfile')
977
978 let save_efm = &efm
979 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
980 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
981
982 exe 'cf Xerrorfile2'
983 clast
984 copen
985 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
986 wincmd p
987
988 exe 'cf Xerrorfile1'
989 call assert_equal([4, 12], [line('.'), col('.')])
990 cn
991 call assert_equal([6, 19], [line('.'), col('.')])
992 cn
993 call assert_equal([9, 2], [line('.'), col('.')])
994 cn
995 call assert_equal([10, 2], [line('.'), col('.')])
996 cn
997 call assert_equal([11, 1], [line('.'), col('.')])
998 cn
999 call assert_equal([12, 2], [line('.'), col('.')])
1000 cn
1001 call assert_equal([14, 10], [line('.'), col('.')])
1002 cn
1003 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
1004 cn
1005 call assert_equal([16, 2], [line('.'), col('.')])
1006 cn
1007 call assert_equal([17, 6], [line('.'), col('.')])
1008 cn
1009 call assert_equal([18, 7], [line('.'), col('.')])
1010 cn
1011 call assert_equal([19, 8], [line('.'), col('.')])
1012 cn
1013 call assert_equal([20, 9], [line('.'), col('.')])
1014 clast
1015 cprev
1016 cprev
1017 wincmd w
1018 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
1019 wincmd p
1020
1021 let &efm = save_efm
1022 call delete('Xerrorfile1')
1023 call delete('Xerrorfile2')
1024 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +01001025endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001026
Bram Moolenaarab47c612016-06-14 22:02:26 +02001027" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001028func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001029 call s:setup_commands(a:cchar)
1030
Bram Moolenaarab47c612016-06-14 22:02:26 +02001031 let save_efm=&efm
1032 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1033
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001034 let lines = ["Entering dir 'dir1/a'",
1035 \ 'habits2.txt:1:Nine Healthy Habits',
1036 \ "Entering dir 'b'",
1037 \ 'habits3.txt:2:0 Hours of television',
1038 \ 'habits2.txt:7:5 Small meals',
1039 \ "Entering dir 'dir1/c'",
1040 \ 'habits4.txt:3:1 Hour of exercise',
1041 \ "Leaving dir 'dir1/c'",
1042 \ "Leaving dir 'dir1/a'",
1043 \ 'habits1.txt:4:2 Liters of water',
1044 \ "Entering dir 'dir2'",
1045 \ 'habits5.txt:5:3 Cups of hot green tea',
1046 \ "Leaving dir 'dir2'"
1047 \]
Bram Moolenaarab47c612016-06-14 22:02:26 +02001048
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001049 Xexpr ""
1050 for l in lines
1051 Xaddexpr l
1052 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +02001053
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001054 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001055
1056 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
1057 call assert_equal(1, qf[1].lnum)
1058 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
1059 call assert_equal(2, qf[3].lnum)
1060 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
1061 call assert_equal(7, qf[4].lnum)
1062 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
1063 call assert_equal(3, qf[6].lnum)
1064 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
1065 call assert_equal(4, qf[9].lnum)
1066 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
1067 call assert_equal(5, qf[11].lnum)
1068
1069 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001070endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001071
1072" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001073func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001074 " Create the directory stack and files
1075 call mkdir('dir1')
1076 call mkdir('dir1/a')
1077 call mkdir('dir1/a/b')
1078 call mkdir('dir1/c')
1079 call mkdir('dir2')
1080
1081 let lines = ["Nine Healthy Habits",
1082 \ "0 Hours of television",
1083 \ "1 Hour of exercise",
1084 \ "2 Liters of water",
1085 \ "3 Cups of hot green tea",
1086 \ "4 Short mental breaks",
1087 \ "5 Small meals",
1088 \ "6 AM wake up time",
1089 \ "7 Minutes of laughter",
1090 \ "8 Hours of sleep (at least)",
1091 \ "9 PM end of the day and off to bed"
1092 \ ]
1093 call writefile(lines, 'habits1.txt')
1094 call writefile(lines, 'dir1/a/habits2.txt')
1095 call writefile(lines, 'dir1/a/b/habits3.txt')
1096 call writefile(lines, 'dir1/c/habits4.txt')
1097 call writefile(lines, 'dir2/habits5.txt')
1098
1099 call s:dir_stack_tests('c')
1100 call s:dir_stack_tests('l')
1101
1102 call delete('dir1', 'rf')
1103 call delete('dir2', 'rf')
1104 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001105endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001106
Bram Moolenaar9b457942016-10-09 16:10:05 +02001107" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001108func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001109 call s:setup_commands(a:cchar)
1110
1111 let save_efm = &efm
1112
1113 let &efm =
1114 \ '%Eerror %m %l,' .
1115 \ '%-Wignored %m %l,' .
1116 \ '%+Cmore ignored %m %l,' .
1117 \ '%Zignored end'
1118 Xgetexpr ['ignored warning 1', 'more ignored continuation 2', 'ignored end', 'error resync 4']
1119 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
1120 call assert_equal([['resync', 1, 4, 'E']], l)
1121
1122 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001123endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001124
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001125func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +02001126 call Xefm_ignore_continuations('c')
1127 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001128endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001129
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001130" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001131func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001132 call s:setup_commands(a:cchar)
1133
Bram Moolenaar049cba92016-06-26 14:38:04 +02001134 let save_efm = &efm
1135
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001136 set efm=%f:%l:%m,%f:%f:%l:%m
1137 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
1138
1139 set efm=%f:%l:%m,%f:%l:%r:%m
1140 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1141
1142 set efm=%f:%l:%m,%O:%f:%l:%m
1143 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1144
1145 set efm=%f:%l:%m,%f:%l:%*[^a-z
1146 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
1147
1148 set efm=%f:%l:%m,%f:%l:%*c
1149 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
1150
1151 set efm=%f:%l:%m,%L%M%N
1152 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
1153
1154 set efm=%f:%l:%m,%f:%l:%m:%R
1155 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
1156
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001157 " Invalid regular expression
1158 set efm=%\\%%k
1159 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E867:')
1160
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001161 set efm=
1162 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
1163
1164 set efm=%DEntering\ dir\ abc,%f:%l:%m
1165 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
1166
1167 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001168endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001169
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001170func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001171 call Xinvalid_efm_Tests('c')
1172 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001173endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001174
1175" TODO:
1176" Add tests for the following formats in 'errorformat'
1177" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001178func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001179 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001180
1181 " Test for %s format in efm
1182 set efm=%f:%s
1183 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001184 let l = getqflist()
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001185 call assert_equal('^\VLine search text\$', l[0].pattern)
1186 call assert_equal(0, l[0].lnum)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001187
Bram Moolenaaree85df32017-03-19 14:19:50 +01001188 let l = split(execute('clist', ''), "\n")
1189 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1190
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001191 " Test for a long line
1192 cexpr 'Xtestfile:' . repeat('a', 1026)
1193 let l = getqflist()
1194 call assert_equal('^\V' . repeat('a', 1019) . '\$', l[0].pattern)
1195
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001196 " Test for %P, %Q and %t format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001197 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001198 [Xtestfile1]
1199 (1,17) error: ';' missing
1200 (21,2) warning: variable 'z' not defined
1201 (67,3) error: end of file found before string ended
1202 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001203
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001204 [Xtestfile2]
1205 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001206
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001207 [Xtestfile3]
1208 NEW compiler v1.1
1209 (2,2) warning: variable 'x' not defined
1210 (67,3) warning: 's' already defined
1211 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001212 [DATA]
1213
Bram Moolenaaree85df32017-03-19 14:19:50 +01001214 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001215 " To exercise the push/pop file functionality in quickfix, the test files
1216 " need to be created.
1217 call writefile(['Line1'], 'Xtestfile1')
1218 call writefile(['Line2'], 'Xtestfile2')
1219 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001220 cexpr ""
1221 for l in lines
1222 caddexpr l
1223 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001224 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001225 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001226 call assert_equal(21, l[2].lnum)
1227 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001228 call assert_equal('w', l[2].type)
1229 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001230 call delete('Xtestfile1')
1231 call delete('Xtestfile2')
1232 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001233
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001234 " Test for %P, %Q with non-existing files
1235 cexpr lines
1236 let l = getqflist()
1237 call assert_equal(14, len(l))
1238 call assert_equal('[Xtestfile1]', l[0].text)
1239 call assert_equal('[Xtestfile2]', l[6].text)
1240 call assert_equal('[Xtestfile3]', l[9].text)
1241
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001242 " Tests for %E, %C and %Z format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001243 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001244 Error 275
1245 line 42
1246 column 3
1247 ' ' expected after '--'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001248 [DATA]
1249
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001250 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1251 cgetexpr lines
1252 let l = getqflist()
1253 call assert_equal(275, l[0].nr)
1254 call assert_equal(42, l[0].lnum)
1255 call assert_equal(3, l[0].col)
1256 call assert_equal('E', l[0].type)
1257 call assert_equal("\n' ' expected after '--'", l[0].text)
1258
1259 " Test for %>
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001260 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001261 Error in line 147 of foo.c:
1262 unknown variable 'i'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001263 [DATA]
1264
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001265 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1266 cgetexpr lines
1267 let l = getqflist()
1268 call assert_equal(147, l[0].lnum)
1269 call assert_equal('E', l[0].type)
1270 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001271
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001272 " Test for %A, %C and other formats
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001273 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001274 ==============================================================
1275 FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)
1276 --------------------------------------------------------------
1277 Traceback (most recent call last):
1278 File "unittests/dbfacadeTest.py", line 89, in testFoo
1279 self.assertEquals(34, dtid)
1280 File "/usr/lib/python2.2/unittest.py", line 286, in
1281 failUnlessEqual
1282 raise self.failureException, \\
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001283 W:AssertionError: 34 != 33
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001284
1285 --------------------------------------------------------------
1286 Ran 27 tests in 0.063s
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001287 [DATA]
1288
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001289 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%t:%m
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001290 cgetexpr lines
1291 let l = getqflist()
1292 call assert_equal(8, len(l))
1293 call assert_equal(89, l[4].lnum)
1294 call assert_equal(1, l[4].valid)
1295 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001296 call assert_equal('W', l[4].type)
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001297
Bram Moolenaard76ce852018-05-01 15:02:04 +02001298 " Test for %o
1299 set efm=%f(%o):%l\ %m
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001300 cgetexpr ['Xotestfile(Language.PureScript.Types):20 Error']
1301 call writefile(['Line1'], 'Xotestfile')
Bram Moolenaard76ce852018-05-01 15:02:04 +02001302 let l = getqflist()
1303 call assert_equal(1, len(l), string(l))
1304 call assert_equal('Language.PureScript.Types', l[0].module)
1305 copen
1306 call assert_equal('Language.PureScript.Types|20| Error', getline(1))
1307 call feedkeys("\<CR>", 'xn')
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001308 call assert_equal('Xotestfile', expand('%:t'))
Bram Moolenaard76ce852018-05-01 15:02:04 +02001309 cclose
1310 bd
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001311 call delete("Xotestfile")
Bram Moolenaard76ce852018-05-01 15:02:04 +02001312
Bram Moolenaar70077dd2019-11-24 12:12:42 +01001313 " Test for a long module name
1314 cexpr 'Xtest(' . repeat('m', 1026) . '):15 message'
1315 let l = getqflist()
1316 call assert_equal(repeat('m', 1024), l[0].module)
1317 call assert_equal(15, l[0].lnum)
1318 call assert_equal('message', l[0].text)
1319
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001320 " The following sequence of commands used to crash Vim
1321 set efm=%W%m
1322 cgetexpr ['msg1']
1323 let l = getqflist()
1324 call assert_equal(1, len(l), string(l))
1325 call assert_equal('msg1', l[0].text)
1326 set efm=%C%m
1327 lexpr 'msg2'
1328 let l = getloclist(0)
1329 call assert_equal(1, len(l), string(l))
1330 call assert_equal('msg2', l[0].text)
1331 lopen
1332 call setqflist([], 'r')
1333 caddbuf
1334 let l = getqflist()
1335 call assert_equal(1, len(l), string(l))
1336 call assert_equal('|| msg2', l[0].text)
1337
Bram Moolenaar78ddc062018-05-15 21:56:34 +02001338 " When matching error lines, case should be ignored. Test for this.
1339 set noignorecase
1340 let l=getqflist({'lines' : ['Xtest:FOO10:Line 20'], 'efm':'%f:foo%l:%m'})
1341 call assert_equal(10, l.items[0].lnum)
1342 call assert_equal('Line 20', l.items[0].text)
1343 set ignorecase&
1344
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001345 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001346 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001347endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001348
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001349func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001350 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001351 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001352 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001353 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001354 colder
1355 cgetexpr []
1356 endfunc
1357 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001358 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001359 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001360 lolder
1361 lgetexpr []
1362 endfunc
1363 endif
1364
1365 augroup testgroup
1366 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001367 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001368 augroup END
1369
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001370 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001371 let words = [ "a", "b" ]
1372 let qflist = []
1373 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001374 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001375 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001376 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001377 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001378
1379 augroup! testgroup
1380endfunc
1381
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001382func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001383 call XquickfixChangedByAutocmd('c')
1384 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001385endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001386
1387func Test_caddbuffer_to_empty()
1388 helpgr quickfix
1389 call setqflist([], 'r')
1390 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001391 try
1392 cn
1393 catch
1394 " number of matches is unknown
1395 call assert_true(v:exception =~ 'E553:')
1396 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001397 quit!
1398endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001399
1400func Test_cgetexpr_works()
1401 " this must not crash Vim
1402 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001403 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001404endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001405
1406" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001407func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001408 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001409
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001410 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001411 \ {'bufnr': a:bnum, 'lnum': 2}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001412 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001413 call assert_equal(2, len(l))
1414 call assert_equal(2, l[1].lnum)
1415
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001416 Xnext
1417 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1418 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001419 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001420 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001421 call assert_equal(3, line('.'))
1422
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001423 " Appending entries to the list should not change the cursor position
1424 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001425 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001426 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001427 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001428 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1429 call assert_equal(1, line('.'))
1430 close
1431
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001432 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001433 \ {'bufnr': a:bnum, 'lnum': 4},
1434 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001435 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001436 call assert_equal(3, len(l))
1437 call assert_equal(5, l[2].lnum)
1438
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001439 call g:Xsetlist([])
1440 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001441 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001442
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001443 " Tests for setting the 'valid' flag
1444 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1445 Xwindow
1446 call assert_equal(1, winnr('$'))
1447 let l = g:Xgetlist()
1448 call g:Xsetlist(l)
1449 call assert_equal(0, g:Xgetlist()[0].valid)
Bram Moolenaar9752c722018-12-22 16:49:34 +01001450 " Adding a non-valid entry should not mark the list as having valid entries
1451 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':5, 'valid':0}], 'a')
1452 Xwindow
1453 call assert_equal(1, winnr('$'))
1454
1455 " :cnext/:cprev should still work even with invalid entries in the list
1456 let l = [{'bufnr' : a:bnum, 'lnum' : 1, 'text' : '1', 'valid' : 0},
1457 \ {'bufnr' : a:bnum, 'lnum' : 2, 'text' : '2', 'valid' : 0}]
1458 call g:Xsetlist(l)
1459 Xnext
1460 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1461 Xprev
1462 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1463 " :cnext/:cprev should still work after appending invalid entries to an
1464 " empty list
1465 call g:Xsetlist([])
1466 call g:Xsetlist(l, 'a')
1467 Xnext
1468 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1469 Xprev
1470 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1471
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001472 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1473 Xwindow
1474 call assert_equal(2, winnr('$'))
1475 Xclose
1476 let save_efm = &efm
1477 set efm=%m
1478 Xgetexpr 'TestMessage'
1479 let l = g:Xgetlist()
1480 call g:Xsetlist(l)
1481 call assert_equal(1, g:Xgetlist()[0].valid)
1482 let &efm = save_efm
1483
Bram Moolenaaree85df32017-03-19 14:19:50 +01001484 " Error cases:
1485 " Refer to a non-existing buffer and pass a non-dictionary type
1486 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1487 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1488 call g:Xsetlist([[1, 2,3]])
1489 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001490endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001491
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001492func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001493 new Xtestfile | only
1494 let bnum = bufnr('%')
1495 call setline(1, range(1,5))
1496
1497 call SetXlistTests('c', bnum)
1498 call SetXlistTests('l', bnum)
1499
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001500 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001501 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001502endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001503
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001504func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001505 call s:setup_commands(a:cchar)
1506
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001507 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001508 let @/ = 'Test_'
1509 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001510 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001511 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001512 Xvimgrep empty test_quickfix.vim
1513 call assert_true(len(g:Xgetlist()) > 0)
1514 Xvimgrep matches test_quickfix.vim
1515 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001516 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001517 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001518 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001519 call g:Xsetlist([], 'r')
1520 call assert_true(len(g:Xgetlist()) == 0)
1521 Xolder
1522 call assert_equal(testlen, len(g:Xgetlist()))
1523 Xnewer
1524 Xnewer
1525 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001526endfunc
1527
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001528func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001529 call Xlist_empty_middle('c')
1530 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001531endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001532
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001533func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001534 call s:setup_commands(a:cchar)
1535
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001536 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001537 Xvimgrep one test_quickfix.vim
1538 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001539 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001540 Xvimgrep two test_quickfix.vim
1541 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001542 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001543 Xvimgrep three test_quickfix.vim
1544 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001545 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001546 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001547 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001548 call g:Xsetlist([], 'r')
1549 call assert_true(len(g:Xgetlist()) == 0)
1550 Xnewer
1551 call assert_equal(twolen, len(g:Xgetlist()))
1552 Xnewer
1553 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001554endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001555
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001556func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001557 call Xlist_empty_older('c')
1558 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001559endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001560
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001561func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001562 call s:setup_commands(a:cchar)
1563
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001564 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1565 \ {'filename': 'fnameB', 'text': 'B'}]
1566 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1567 \ {'filename': 'fnameD', 'text': 'D'},
1568 \ {'filename': 'fnameE', 'text': 'E'}]
1569
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001570 " {action} is unspecified. Same as specifying ' '.
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001571 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001572 silent! Xnewer 99
1573 call g:Xsetlist(list1)
1574 call g:Xsetlist(list2)
1575 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001576 call assert_equal(3, len(li))
1577 call assert_equal('C', li[0]['text'])
1578 call assert_equal('D', li[1]['text'])
1579 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001580 silent! Xolder
1581 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001582 call assert_equal(2, len(li))
1583 call assert_equal('A', li[0]['text'])
1584 call assert_equal('B', li[1]['text'])
1585
1586 " {action} is specified ' '.
1587 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001588 silent! Xnewer 99
1589 call g:Xsetlist(list1)
1590 call g:Xsetlist(list2, ' ')
1591 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001592 call assert_equal(3, len(li))
1593 call assert_equal('C', li[0]['text'])
1594 call assert_equal('D', li[1]['text'])
1595 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001596 silent! Xolder
1597 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001598 call assert_equal(2, len(li))
1599 call assert_equal('A', li[0]['text'])
1600 call assert_equal('B', li[1]['text'])
1601
1602 " {action} is specified 'a'.
1603 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001604 silent! Xnewer 99
1605 call g:Xsetlist(list1)
1606 call g:Xsetlist(list2, 'a')
1607 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001608 call assert_equal(5, len(li))
1609 call assert_equal('A', li[0]['text'])
1610 call assert_equal('B', li[1]['text'])
1611 call assert_equal('C', li[2]['text'])
1612 call assert_equal('D', li[3]['text'])
1613 call assert_equal('E', li[4]['text'])
1614
1615 " {action} is specified 'r'.
1616 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001617 silent! Xnewer 99
1618 call g:Xsetlist(list1)
1619 call g:Xsetlist(list2, 'r')
1620 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001621 call assert_equal(3, len(li))
1622 call assert_equal('C', li[0]['text'])
1623 call assert_equal('D', li[1]['text'])
1624 call assert_equal('E', li[2]['text'])
1625
1626 " Test for wrong value.
1627 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001628 call assert_fails("call g:Xsetlist(0)", 'E714:')
1629 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
1630 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
1631 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
1632 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001633endfunc
1634
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001635func Test_setqflist_invalid_nr()
1636 " The following command used to crash Vim
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001637 eval []->setqflist(' ', {'nr' : $XXX_DOES_NOT_EXIST})
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001638endfunc
1639
Bram Moolenaar99234f22020-02-10 22:56:54 +01001640func Test_setqflist_user_sets_buftype()
1641 call setqflist([{'text': 'foo'}, {'text': 'bar'}])
1642 set buftype=quickfix
1643 call setqflist([], 'a')
1644 enew
1645endfunc
1646
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001647func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001648 call XquickfixSetListWithAct('c')
1649 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001650endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001651
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001652func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001653 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001654
Bram Moolenaar049cba92016-06-26 14:38:04 +02001655 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001656 call assert_equal(1, l[0].lnum)
1657 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001658 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001659 call assert_equal(2, l[1].lnum)
1660 call assert_equal(1, l[1].col)
1661 call assert_equal(4070, len(l[1].text))
1662 call assert_equal(3, l[2].lnum)
1663 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001664 call assert_equal(4070, len(l[2].text))
1665 call assert_equal(4, l[3].lnum)
1666 call assert_equal(1, l[3].col)
1667 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001668
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001669 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001670endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001671
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001672func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001673 call s:setup_commands(a:cchar)
1674
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001675 let testfile = 'samples/quickfix.txt'
1676
1677 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001678 exe 'Xgetfile' testfile
1679 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001680
1681 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001682 Xexpr readfile(testfile)
1683 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001684
1685 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001686 Xexpr join(readfile(testfile), "\n")
1687 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001688
1689 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001690 exe 'edit' testfile
1691 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02001692 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001693endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001694
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001695func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001696 call s:long_lines_tests('c')
1697 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001698endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001699
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001700func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001701 let l = []
1702 for i in range(1, 20)
1703 call add(l, 'Line' . i)
1704 endfor
1705 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001706endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001707
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001708func Test_switchbuf()
Bram Moolenaar9134f1e2019-11-29 20:26:13 +01001709 CheckNotBSD
Bram Moolenaar049cba92016-06-26 14:38:04 +02001710 call s:create_test_file('Xqftestfile1')
1711 call s:create_test_file('Xqftestfile2')
1712 call s:create_test_file('Xqftestfile3')
1713
1714 new | only
1715 edit Xqftestfile1
1716 let file1_winid = win_getid()
1717 new Xqftestfile2
1718 let file2_winid = win_getid()
1719 cgetexpr ['Xqftestfile1:5:Line5',
1720 \ 'Xqftestfile1:6:Line6',
1721 \ 'Xqftestfile2:10:Line10',
1722 \ 'Xqftestfile2:11:Line11',
1723 \ 'Xqftestfile3:15:Line15',
1724 \ 'Xqftestfile3:16:Line16']
1725
1726 new
1727 let winid = win_getid()
1728 cfirst | cnext
1729 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001730 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001731 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001732 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001733 call assert_equal(winid, win_getid())
Bram Moolenaar049cba92016-06-26 14:38:04 +02001734
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001735 " Test for 'switchbuf' set to search for files in windows in the current
1736 " tabpage and jump to an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001737 set switchbuf=useopen
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001738 enew
Bram Moolenaar049cba92016-06-26 14:38:04 +02001739 cfirst | cnext
1740 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001741 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001742 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001743 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001744 call assert_equal(file2_winid, win_getid())
1745
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001746 " Test for 'switchbuf' set to search for files in tabpages and jump to an
1747 " existing tabpage (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001748 enew | only
1749 set switchbuf=usetab
1750 tabedit Xqftestfile1
1751 tabedit Xqftestfile2
Bram Moolenaar74240d32017-12-10 15:26:15 +01001752 tabedit Xqftestfile3
Bram Moolenaar049cba92016-06-26 14:38:04 +02001753 tabfirst
1754 cfirst | cnext
1755 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001756 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001757 call assert_equal(3, tabpagenr())
Bram Moolenaar74240d32017-12-10 15:26:15 +01001758 6cnext
1759 call assert_equal(4, tabpagenr())
1760 2cpfile
1761 call assert_equal(2, tabpagenr())
1762 2cnfile
1763 call assert_equal(4, tabpagenr())
Bram Moolenaar049cba92016-06-26 14:38:04 +02001764 tabfirst | tabonly | enew
1765
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001766 " Test for 'switchbuf' set to open a new window for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02001767 set switchbuf=split
1768 cfirst | cnext
1769 call assert_equal(1, winnr('$'))
1770 cnext | cnext
1771 call assert_equal(2, winnr('$'))
1772 cnext | cnext
1773 call assert_equal(3, winnr('$'))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001774
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001775 " Test for 'switchbuf' set to open a new tabpage for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02001776 set switchbuf=newtab
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001777 enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001778 cfirst | cnext
1779 call assert_equal(1, tabpagenr('$'))
1780 cnext | cnext
1781 call assert_equal(2, tabpagenr('$'))
1782 cnext | cnext
1783 call assert_equal(3, tabpagenr('$'))
1784 tabfirst | enew | tabonly | only
1785
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01001786 set switchbuf=uselast
1787 split
1788 let last_winid = win_getid()
1789 copen
1790 exe "normal 1G\<CR>"
1791 call assert_equal(last_winid, win_getid())
1792 enew | only
1793
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001794 " With an empty 'switchbuf', jumping to a quickfix entry should open the
1795 " file in an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001796 set switchbuf=
1797 edit Xqftestfile1
1798 let file1_winid = win_getid()
1799 new Xqftestfile2
1800 let file2_winid = win_getid()
1801 copen
1802 exe "normal 1G\<CR>"
1803 call assert_equal(file1_winid, win_getid())
1804 copen
1805 exe "normal 3G\<CR>"
1806 call assert_equal(file2_winid, win_getid())
1807 copen | only
1808 exe "normal 5G\<CR>"
1809 call assert_equal(2, winnr('$'))
1810 call assert_equal(1, bufwinnr('Xqftestfile3'))
1811
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001812 " If only quickfix window is open in the current tabpage, jumping to an
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001813 " entry with 'switchbuf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02001814 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001815 set switchbuf=usetab
1816 tabedit Xqftestfile1
1817 tabedit Xqftestfile2
1818 tabedit Xqftestfile3
1819 tabfirst
1820 copen | only
1821 clast
1822 call assert_equal(4, tabpagenr())
1823 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001824
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001825 " Jumping to a file that is not present in any of the tabpages and the
1826 " current tabpage doesn't have any usable windows, should open it in a new
1827 " window in the current tabpage.
1828 copen | only
1829 cfirst
1830 call assert_equal(1, tabpagenr())
1831 call assert_equal('Xqftestfile1', bufname(''))
1832
1833 " If opening a file changes 'switchbuf', then the new value should be
1834 " retained.
1835 call writefile(["vim: switchbuf=split"], 'Xqftestfile1')
1836 enew | only
1837 set switchbuf&vim
1838 cexpr "Xqftestfile1:1:10"
1839 call assert_equal('split', &switchbuf)
1840 call writefile(["vim: switchbuf=usetab"], 'Xqftestfile1')
1841 enew | only
1842 set switchbuf=useopen
1843 cexpr "Xqftestfile1:1:10"
1844 call assert_equal('usetab', &switchbuf)
1845 call writefile(["vim: switchbuf&vim"], 'Xqftestfile1')
1846 enew | only
1847 set switchbuf=useopen
1848 cexpr "Xqftestfile1:1:10"
1849 call assert_equal('', &switchbuf)
1850
Bram Moolenaar049cba92016-06-26 14:38:04 +02001851 call delete('Xqftestfile1')
1852 call delete('Xqftestfile2')
1853 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001854 set switchbuf&vim
1855
1856 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001857endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001858
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001859func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001860 call s:setup_commands(a:cchar)
1861
1862 enew | only
1863
Bram Moolenaarc1542742016-07-20 21:44:37 +02001864 let fname = 'Xqftestfile' . a:cchar
1865 call s:create_test_file(fname)
1866 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02001867
Bram Moolenaarc1542742016-07-20 21:44:37 +02001868 Xgetexpr [fname . ':5:Line5',
1869 \ fname . ':10:Line10',
1870 \ fname . ':15:Line15',
1871 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02001872
1873 6,14delete
1874 call append(6, ['Buffer', 'Window'])
1875
1876 let l = g:Xgetlist()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001877 call assert_equal(5, l[0].lnum)
1878 call assert_equal(6, l[2].lnum)
1879 call assert_equal(13, l[3].lnum)
1880
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001881 " If a file doesn't have any quickfix entries, then deleting lines in the
1882 " file should not update the quickfix list
1883 call g:Xsetlist([], 'f')
1884 1,2delete
1885 call assert_equal([], g:Xgetlist())
1886
Bram Moolenaar049cba92016-06-26 14:38:04 +02001887 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02001888 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001889endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001890
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001891func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02001892 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001893 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02001894 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001895 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001896endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001897
1898" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001899func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001900 call s:setup_commands(a:cchar)
1901
1902 " The following lines are used for the grep test. Don't remove.
1903 " Grep_Test_Text: Match 1
1904 " Grep_Test_Text: Match 2
1905 " GrepAdd_Test_Text: Match 1
1906 " GrepAdd_Test_Text: Match 2
1907 enew! | only
1908 set makeef&vim
1909 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01001910 call assert_true(len(g:Xgetlist()) == 5)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001911 Xopen
1912 call assert_true(w:quickfix_title =~ '^:grep')
1913 Xclose
1914 enew
1915 set makeef=Temp_File_##
1916 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01001917 call assert_true(len(g:Xgetlist()) == 9)
1918
1919 " Try with 'grepprg' set to 'internal'
1920 set grepprg=internal
1921 silent Xgrep Grep_Test_Text: test_quickfix.vim
1922 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
1923 call assert_true(len(g:Xgetlist()) == 9)
1924 set grepprg&vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001925
1926 call writefile(['Vim'], 'XtestTempFile')
1927 set makeef=XtestTempFile
1928 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01001929 call assert_equal(5, len(g:Xgetlist()))
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001930 call assert_false(filereadable('XtestTempFile'))
1931 set makeef&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001932endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001933
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001934func Test_grep()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001935 if !has('unix')
1936 " The grepprg may not be set on non-Unix systems
1937 return
1938 endif
1939
1940 call s:test_xgrep('c')
1941 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001942endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001943
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001944func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001945 " Use one 'errorformat' for two windows. Add an expression to each of them,
1946 " make sure they each keep their own state.
1947 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1948 call mkdir('Xone/a', 'p')
1949 call mkdir('Xtwo/a', 'p')
1950 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
1951 call writefile(lines, 'Xone/a/one.txt')
1952 call writefile(lines, 'Xtwo/a/two.txt')
1953
1954 new one
1955 let one_id = win_getid()
1956 lexpr ""
1957 new two
1958 let two_id = win_getid()
1959 lexpr ""
1960
1961 laddexpr "Entering dir 'Xtwo/a'"
1962 call win_gotoid(one_id)
1963 laddexpr "Entering dir 'Xone/a'"
1964 call win_gotoid(two_id)
1965 laddexpr 'two.txt:5:two two two'
1966 call win_gotoid(one_id)
1967 laddexpr 'one.txt:3:one one one'
1968
1969 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001970 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
1971 call assert_equal(3, loc_one[1].lnum)
1972
1973 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001974 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
1975 call assert_equal(5, loc_two[1].lnum)
1976
1977 call win_gotoid(one_id)
1978 bwipe!
1979 call win_gotoid(two_id)
1980 bwipe!
1981 call delete('Xone', 'rf')
1982 call delete('Xtwo', 'rf')
1983endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02001984
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001985func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02001986 call s:setup_commands(a:cchar)
1987
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001988 " Calling lbottom without any errors should fail
1989 if a:cchar == 'l'
1990 call assert_fails('lbottom', 'E776:')
1991 endif
1992
Bram Moolenaar875feea2017-06-11 16:07:51 +02001993 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02001994 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02001995 let wid = win_getid()
1996 call assert_equal(1, line('.'))
1997 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02001998 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02001999 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02002000 call win_gotoid(wid)
2001 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02002002 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02002003endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02002004
2005" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002006func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02002007 call XbottomTests('c')
2008 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002009endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002010
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002011func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002012 call s:setup_commands(a:cchar)
2013
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002014 " clear all lists after the first one, then replace the first one.
2015 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002016 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002017 let entry = {'filename': 'foo', 'lnum': 42}
2018 call g:Xsetlist([entry], 'r')
2019 call g:Xsetlist([entry, entry])
2020 call g:Xsetlist([entry, entry, entry])
2021 let res = split(execute(a:cchar . 'hist'), "\n")
2022 call assert_equal(3, len(res))
2023 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
2024 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
2025 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
2026 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002027
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002028 " Test for changing the quickfix lists
2029 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2030 exe '1' . a:cchar . 'hist'
2031 call assert_equal(1, g:Xgetlist({'nr' : 0}).nr)
2032 exe '3' . a:cchar . 'hist'
2033 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
2034 call assert_fails('-2' . a:cchar . 'hist', 'E16:')
2035 call assert_fails('4' . a:cchar . 'hist', 'E16:')
2036
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002037 call g:Xsetlist([], 'f')
2038 let l = split(execute(a:cchar . 'hist'), "\n")
2039 call assert_equal('No entries', l[0])
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02002040 if a:cchar == 'c'
2041 call assert_fails('4chist', 'E16:')
2042 else
2043 call assert_fails('4lhist', 'E776:')
2044 endif
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002045
2046 " An empty list should still show the stack history
2047 call g:Xsetlist([])
2048 let res = split(execute(a:cchar . 'hist'), "\n")
2049 call assert_equal('> error list 1 of 1; 0 ' . common, res[0])
2050
2051 call g:Xsetlist([], 'f')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02002052endfunc
2053
2054func Test_history()
2055 call HistoryTest('c')
2056 call HistoryTest('l')
2057endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02002058
2059func Test_duplicate_buf()
2060 " make sure we can get the highest buffer number
2061 edit DoesNotExist
2062 edit DoesNotExist2
2063 let last_buffer = bufnr("$")
2064
2065 " make sure only one buffer is created
2066 call writefile(['this one', 'that one'], 'Xgrepthis')
2067 vimgrep one Xgrepthis
2068 vimgrep one Xgrepthis
2069 call assert_equal(last_buffer + 1, bufnr("$"))
2070
2071 call delete('Xgrepthis')
2072endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002073
2074" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002075func Xproperty_tests(cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002076 call s:setup_commands(a:cchar)
2077
2078 " Error cases
2079 call assert_fails('call g:Xgetlist(99)', 'E715:')
2080 call assert_fails('call g:Xsetlist(99)', 'E714:')
2081 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
2082
2083 " Set and get the title
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002084 call g:Xsetlist([])
Bram Moolenaard823fa92016-08-12 16:29:27 +02002085 Xopen
2086 wincmd p
2087 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002088 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
2089 call assert_equal(0, s)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002090 let d = g:Xgetlist({"title":1})
2091 call assert_equal('Sample', d.title)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02002092 " Try setting title to a non-string value
2093 call assert_equal(-1, g:Xsetlist([], 'a', {'title' : ['Test']}))
2094 call assert_equal('Sample', g:Xgetlist({"title":1}).title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002095
2096 Xopen
2097 call assert_equal('Sample', w:quickfix_title)
2098 Xclose
2099
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02002100 " Tests for action argument
2101 silent! Xolder 999
2102 let qfnr = g:Xgetlist({'all':1}).nr
2103 call g:Xsetlist([], 'r', {'title' : 'N1'})
2104 call assert_equal('N1', g:Xgetlist({'all':1}).title)
2105 call g:Xsetlist([], ' ', {'title' : 'N2'})
2106 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
Bram Moolenaar890680c2016-09-27 21:28:56 +02002107
2108 let res = g:Xgetlist({'nr': 0})
2109 call assert_equal(qfnr + 1, res.nr)
2110 call assert_equal(['nr'], keys(res))
2111
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02002112 call g:Xsetlist([], ' ', {'title' : 'N3'})
2113 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
2114
Bram Moolenaaree85df32017-03-19 14:19:50 +01002115 " Changing the title of an earlier quickfix list
Bram Moolenaar55b69262017-08-13 13:42:01 +02002116 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
Bram Moolenaaree85df32017-03-19 14:19:50 +01002117 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
2118
2119 " Changing the title of an invalid quickfix list
2120 call assert_equal(-1, g:Xsetlist([], ' ',
2121 \ {'title' : 'SomeTitle', 'nr' : 99}))
2122 call assert_equal(-1, g:Xsetlist([], ' ',
2123 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
2124
2125 if a:cchar == 'c'
2126 copen
2127 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
2128 cclose
2129 endif
2130
Bram Moolenaard823fa92016-08-12 16:29:27 +02002131 " Invalid arguments
2132 call assert_fails('call g:Xgetlist([])', 'E715')
2133 call assert_fails('call g:Xsetlist([], "a", [])', 'E715')
2134 let s = g:Xsetlist([], 'a', {'abc':1})
2135 call assert_equal(-1, s)
2136
2137 call assert_equal({}, g:Xgetlist({'abc':1}))
Bram Moolenaara6d48492017-12-12 22:45:31 +01002138 call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
2139 call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002140
2141 if a:cchar == 'l'
Bram Moolenaar890680c2016-09-27 21:28:56 +02002142 call assert_equal({}, getloclist(99, {'title': 1}))
Bram Moolenaard823fa92016-08-12 16:29:27 +02002143 endif
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002144
2145 " Context related tests
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002146 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
2147 call assert_equal(0, s)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002148 call test_garbagecollect_now()
2149 let d = g:Xgetlist({'context':1})
2150 call assert_equal([1,2,3], d.context)
2151 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
2152 let d = g:Xgetlist({'context':1})
2153 call assert_equal({'color':'green'}, d.context)
2154 call g:Xsetlist([], 'a', {'context':"Context info"})
2155 let d = g:Xgetlist({'context':1})
2156 call assert_equal("Context info", d.context)
2157 call g:Xsetlist([], 'a', {'context':246})
2158 let d = g:Xgetlist({'context':1})
2159 call assert_equal(246, d.context)
2160 if a:cchar == 'l'
2161 " Test for copying context across two different location lists
2162 new | only
2163 let w1_id = win_getid()
2164 let l = [1]
2165 call setloclist(0, [], 'a', {'context':l})
2166 new
2167 let w2_id = win_getid()
2168 call add(l, 2)
2169 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
2170 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2171 unlet! l
2172 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2173 only
2174 call setloclist(0, [], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01002175 call assert_equal('', getloclist(0, {'context':1}).context)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002176 endif
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002177
2178 " Test for changing the context of previous quickfix lists
2179 call g:Xsetlist([], 'f')
2180 Xexpr "One"
2181 Xexpr "Two"
2182 Xexpr "Three"
Bram Moolenaar55b69262017-08-13 13:42:01 +02002183 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
2184 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002185 " Also, check for setting the context using quickfix list number zero.
Bram Moolenaar55b69262017-08-13 13:42:01 +02002186 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002187 call test_garbagecollect_now()
2188 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
2189 call assert_equal([1], l.context)
2190 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
2191 call assert_equal([2], l.context)
2192 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
2193 call assert_equal([3], l.context)
2194
2195 " Test for changing the context through reference and for garbage
2196 " collection of quickfix context
2197 let l = ["red"]
2198 call g:Xsetlist([], ' ', {'context' : l})
2199 call add(l, "blue")
2200 let x = g:Xgetlist({'context' : 1})
2201 call add(x.context, "green")
2202 call assert_equal(["red", "blue", "green"], l)
2203 call assert_equal(["red", "blue", "green"], x.context)
2204 unlet l
2205 call test_garbagecollect_now()
2206 let m = g:Xgetlist({'context' : 1})
2207 call assert_equal(["red", "blue", "green"], m.context)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002208
2209 " Test for setting/getting items
2210 Xexpr ""
2211 let qfprev = g:Xgetlist({'nr':0})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002212 let s = g:Xsetlist([], ' ', {'title':'Green',
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002213 \ 'items' : [{'filename':'F1', 'lnum':10}]})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002214 call assert_equal(0, s)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002215 let qfcur = g:Xgetlist({'nr':0})
2216 call assert_true(qfcur.nr == qfprev.nr + 1)
2217 let l = g:Xgetlist({'items':1})
2218 call assert_equal('F1', bufname(l.items[0].bufnr))
2219 call assert_equal(10, l.items[0].lnum)
2220 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
2221 \ {'filename':'F2', 'lnum':30}]})
2222 let l = g:Xgetlist({'items':1})
2223 call assert_equal('F2', bufname(l.items[2].bufnr))
2224 call assert_equal(30, l.items[2].lnum)
2225 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
2226 let l = g:Xgetlist({'items':1})
2227 call assert_equal('F3', bufname(l.items[0].bufnr))
2228 call assert_equal(40, l.items[0].lnum)
2229 call g:Xsetlist([], 'r', {'items' : []})
2230 let l = g:Xgetlist({'items':1})
2231 call assert_equal(0, len(l.items))
2232
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002233 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
2234 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2235 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2236 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
2237
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02002238 " Test for getting id of window associated with a location list window
2239 if a:cchar == 'l'
2240 only
2241 call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid)
2242 let wid = win_getid()
2243 Xopen
2244 call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid)
2245 wincmd w
2246 call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid)
2247 only
2248 endif
2249
Bram Moolenaarae338332017-08-11 20:25:26 +02002250 " The following used to crash Vim with address sanitizer
2251 call g:Xsetlist([], 'f')
2252 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
2253 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
2254
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02002255 " Try setting the items using a string
2256 call assert_equal(-1, g:Xsetlist([], ' ', {'items' : 'Test'}))
2257
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002258 " Save and restore the quickfix stack
2259 call g:Xsetlist([], 'f')
2260 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2261 Xexpr "File1:10:Line1"
2262 Xexpr "File2:20:Line2"
2263 Xexpr "File3:30:Line3"
2264 let last_qf = g:Xgetlist({'nr':'$'}).nr
2265 call assert_equal(3, last_qf)
2266 let qstack = []
2267 for i in range(1, last_qf)
2268 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
2269 endfor
2270 call g:Xsetlist([], 'f')
2271 for i in range(len(qstack))
2272 call g:Xsetlist([], ' ', qstack[i])
2273 endfor
2274 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2275 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
2276 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
2277 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
2278 call g:Xsetlist([], 'f')
2279
2280 " Swap two quickfix lists
2281 Xexpr "File1:10:Line10"
2282 Xexpr "File2:20:Line20"
2283 Xexpr "File3:30:Line30"
2284 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
2285 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
2286 let l1=g:Xgetlist({'nr':1,'all':1})
2287 let l2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002288 let save_id = l1.id
2289 let l1.id=l2.id
2290 let l2.id=save_id
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002291 call g:Xsetlist([], 'r', l1)
2292 call g:Xsetlist([], 'r', l2)
2293 let newl1=g:Xgetlist({'nr':1,'all':1})
2294 let newl2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002295 call assert_equal('Fruits', newl1.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002296 call assert_equal(['Fruits'], newl1.context)
2297 call assert_equal('Line20', newl1.items[0].text)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002298 call assert_equal('Colors', newl2.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002299 call assert_equal(['Colors'], newl2.context)
2300 call assert_equal('Line10', newl2.items[0].text)
2301 call g:Xsetlist([], 'f')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002302endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002303
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002304func Test_qf_property()
Bram Moolenaard823fa92016-08-12 16:29:27 +02002305 call Xproperty_tests('c')
2306 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002307endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002308
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002309" Test for setting the current index in the location/quickfix list
2310func Xtest_setqfidx(cchar)
2311 call s:setup_commands(a:cchar)
2312
2313 Xgetexpr "F1:10:1:Line1\nF2:20:2:Line2\nF3:30:3:Line3"
2314 Xgetexpr "F4:10:1:Line1\nF5:20:2:Line2\nF6:30:3:Line3"
2315 Xgetexpr "F7:10:1:Line1\nF8:20:2:Line2\nF9:30:3:Line3"
2316
2317 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 2})
2318 call g:Xsetlist([], 'a', {'nr' : 2, 'idx' : 2})
2319 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 3})
2320 Xolder 2
2321 Xopen
2322 call assert_equal(3, line('.'))
2323 Xnewer
2324 call assert_equal(2, line('.'))
2325 Xnewer
2326 call assert_equal(2, line('.'))
2327 " Update the current index with the quickfix window open
2328 wincmd w
2329 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 3})
2330 Xopen
2331 call assert_equal(3, line('.'))
2332 Xclose
2333
2334 " Set the current index to the last entry
2335 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : '$'})
2336 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2337 " A large value should set the index to the last index
2338 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 1})
2339 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 999})
2340 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2341 " Invalid index values
2342 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : -1})
2343 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2344 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 0})
2345 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2346 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 'xx'})
2347 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2348 call assert_fails("call g:Xsetlist([], 'a', {'nr':1, 'idx':[]})", 'E745:')
2349
2350 call g:Xsetlist([], 'f')
2351 new | only
2352endfunc
2353
2354func Test_setqfidx()
2355 call Xtest_setqfidx('c')
2356 call Xtest_setqfidx('l')
2357endfunc
2358
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002359" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002360func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002361 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002362endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002363
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002364func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002365 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
2366 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
2367
2368 let g:acmds = []
2369 cexpr "F1:10:Line 10"
2370 caddexpr "F1:20:Line 20"
2371 cgetexpr "F1:30:Line 30"
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002372 cexpr ""
2373 caddexpr ""
2374 cgetexpr ""
2375 silent! cexpr non_existing_func()
2376 silent! caddexpr non_existing_func()
2377 silent! cgetexpr non_existing_func()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002378 let l = ['precexpr',
2379 \ 'postcexpr',
2380 \ 'precaddexpr',
2381 \ 'postcaddexpr',
2382 \ 'precgetexpr',
2383 \ 'postcgetexpr',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002384 \ 'precexpr',
2385 \ 'postcexpr',
2386 \ 'precaddexpr',
2387 \ 'postcaddexpr',
2388 \ 'precgetexpr',
2389 \ 'postcgetexpr',
2390 \ 'precexpr',
2391 \ 'precaddexpr',
2392 \ 'precgetexpr']
2393 call assert_equal(l, g:acmds)
2394
2395 let g:acmds = []
2396 enew! | call append(0, "F2:10:Line 10")
2397 cbuffer!
2398 enew! | call append(0, "F2:20:Line 20")
2399 cgetbuffer
2400 enew! | call append(0, "F2:30:Line 30")
2401 caddbuffer
2402 new
2403 let bnum = bufnr('%')
2404 bunload
2405 exe 'silent! cbuffer! ' . bnum
2406 exe 'silent! cgetbuffer ' . bnum
2407 exe 'silent! caddbuffer ' . bnum
2408 enew!
2409 let l = ['precbuffer',
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002410 \ 'postcbuffer',
2411 \ 'precgetbuffer',
2412 \ 'postcgetbuffer',
2413 \ 'precaddbuffer',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002414 \ 'postcaddbuffer',
2415 \ 'precbuffer',
2416 \ 'precgetbuffer',
2417 \ 'precaddbuffer']
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002418 call assert_equal(l, g:acmds)
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002419
2420 call writefile(['Xtest:1:Line1'], 'Xtest')
2421 call writefile([], 'Xempty')
2422 let g:acmds = []
2423 cfile Xtest
2424 caddfile Xtest
2425 cgetfile Xtest
2426 cfile Xempty
2427 caddfile Xempty
2428 cgetfile Xempty
2429 silent! cfile do_not_exist
2430 silent! caddfile do_not_exist
2431 silent! cgetfile do_not_exist
2432 let l = ['precfile',
2433 \ 'postcfile',
2434 \ 'precaddfile',
2435 \ 'postcaddfile',
2436 \ 'precgetfile',
2437 \ 'postcgetfile',
2438 \ 'precfile',
2439 \ 'postcfile',
2440 \ 'precaddfile',
2441 \ 'postcaddfile',
2442 \ 'precgetfile',
2443 \ 'postcgetfile',
2444 \ 'precfile',
2445 \ 'postcfile',
2446 \ 'precaddfile',
2447 \ 'postcaddfile',
2448 \ 'precgetfile',
2449 \ 'postcgetfile']
2450 call assert_equal(l, g:acmds)
2451
2452 let g:acmds = []
2453 helpgrep quickfix
2454 silent! helpgrep non_existing_help_topic
2455 vimgrep test Xtest
2456 vimgrepadd test Xtest
2457 silent! vimgrep non_existing_test Xtest
2458 silent! vimgrepadd non_existing_test Xtest
2459 set makeprg=
2460 silent! make
2461 set makeprg&
2462 let l = ['prehelpgrep',
2463 \ 'posthelpgrep',
2464 \ 'prehelpgrep',
2465 \ 'posthelpgrep',
2466 \ 'previmgrep',
2467 \ 'postvimgrep',
2468 \ 'previmgrepadd',
2469 \ 'postvimgrepadd',
2470 \ 'previmgrep',
2471 \ 'postvimgrep',
2472 \ 'previmgrepadd',
2473 \ 'postvimgrepadd',
2474 \ 'premake',
2475 \ 'postmake']
2476 call assert_equal(l, g:acmds)
2477
2478 if has('unix')
2479 " Run this test only on Unix-like systems. The grepprg may not be set on
2480 " non-Unix systems.
2481 " The following lines are used for the grep test. Don't remove.
2482 " Grep_Autocmd_Text: Match 1
2483 " GrepAdd_Autocmd_Text: Match 2
2484 let g:acmds = []
2485 silent grep Grep_Autocmd_Text test_quickfix.vim
2486 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2487 silent grep abc123def Xtest
2488 silent grepadd abc123def Xtest
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002489 set grepprg=internal
2490 silent grep Grep_Autocmd_Text test_quickfix.vim
2491 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2492 silent lgrep Grep_Autocmd_Text test_quickfix.vim
2493 silent lgrepadd GrepAdd_Autocmd_Text test_quickfix.vim
2494 set grepprg&vim
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002495 let l = ['pregrep',
2496 \ 'postgrep',
2497 \ 'pregrepadd',
2498 \ 'postgrepadd',
2499 \ 'pregrep',
2500 \ 'postgrep',
2501 \ 'pregrepadd',
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002502 \ 'postgrepadd',
2503 \ 'pregrep',
2504 \ 'postgrep',
2505 \ 'pregrepadd',
2506 \ 'postgrepadd',
2507 \ 'prelgrep',
2508 \ 'postlgrep',
2509 \ 'prelgrepadd',
2510 \ 'postlgrepadd']
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002511 call assert_equal(l, g:acmds)
2512 endif
2513
2514 call delete('Xtest')
2515 call delete('Xempty')
Bram Moolenaarb254af32017-12-18 19:48:58 +01002516 au! QuickFixCmdPre
2517 au! QuickFixCmdPost
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002518endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01002519
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002520func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01002521 set efm=%m
2522 lgetexpr '?'
2523
2524 try
2525 call DoesNotExit()
2526 catch
2527 lgetexpr '1'
2528 finally
2529 lgetexpr '1'
2530 endtry
2531
2532 call assert_equal('1', getloclist(0)[0].text)
2533
2534 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002535endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002536
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002537func Test_caddbuffer_wrong()
2538 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002539 let save_efm = &efm
2540 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
2541 cgetexpr ['WWWW', 'EEEE', 'CCCC']
2542 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002543 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002544 bwipe!
2545endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002546
2547func Test_caddexpr_wrong()
2548 " This used to cause a memory access in freed memory.
2549 cbuffer
2550 cbuffer
2551 copen
2552 let save_efm = &efm
2553 set efm=%
2554 call assert_fails('caddexpr ""', 'E376:')
2555 let &efm = save_efm
2556endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01002557
2558func Test_dirstack_cleanup()
2559 " This used to cause a memory access in freed memory.
2560 let save_efm = &efm
2561 lexpr '0'
2562 lopen
2563 fun X(c)
2564 let save_efm=&efm
2565 set efm=%D%f
2566 if a:c == 'c'
2567 caddexpr '::'
2568 else
2569 laddexpr ':0:0'
2570 endif
2571 let &efm=save_efm
2572 endfun
2573 call X('c')
2574 call X('l')
2575 call setqflist([], 'r')
2576 caddbuffer
2577 let &efm = save_efm
2578endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002579
2580" Tests for jumping to entries from the location list window and quickfix
2581" window
2582func Test_cwindow_jump()
2583 set efm=%f%%%l%%%m
2584 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2585 lopen | only
2586 lfirst
2587 call assert_true(winnr('$') == 2)
2588 call assert_true(winnr() == 1)
2589 " Location list for the new window should be set
2590 call assert_true(getloclist(0)[2].text == 'Line 30')
2591
2592 " Open a scratch buffer
2593 " Open a new window and create a location list
2594 " Open the location list window and close the other window
2595 " Jump to an entry.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002596 " Should create a new window and jump to the entry. The scratch buffer
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002597 " should not be used.
2598 enew | only
2599 set buftype=nofile
2600 below new
2601 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2602 lopen
2603 2wincmd c
2604 lnext
2605 call assert_true(winnr('$') == 3)
2606 call assert_true(winnr() == 2)
2607
2608 " Open two windows with two different location lists
2609 " Open the location list window and close the previous window
2610 " Jump to an entry in the location list window
2611 " Should open the file in the first window and not set the location list.
2612 enew | only
2613 lgetexpr ["F1%5%Line 5"]
2614 below new
2615 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2616 lopen
2617 2wincmd c
2618 lnext
2619 call assert_true(winnr() == 1)
2620 call assert_true(getloclist(0)[0].text == 'Line 5')
2621
2622 enew | only
2623 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2624 copen
2625 cnext
2626 call assert_true(winnr('$') == 2)
2627 call assert_true(winnr() == 1)
2628
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002629 " open the quickfix buffer in two windows and jump to an entry. Should open
2630 " the file in the first quickfix window.
2631 enew | only
2632 copen
2633 let bnum = bufnr('')
2634 exe 'sbuffer ' . bnum
2635 wincmd b
2636 cfirst
2637 call assert_equal(2, winnr())
2638 call assert_equal('F1', bufname(''))
2639 enew | only
2640 exe 'sb' bnum
2641 exe 'botright sb' bnum
2642 wincmd t
2643 clast
2644 call assert_equal(2, winnr())
2645 call assert_equal('quickfix', getwinvar(1, '&buftype'))
2646 call assert_equal('quickfix', getwinvar(3, '&buftype'))
2647
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002648 " Jumping to a file from the location list window should find a usable
Bram Moolenaar15a7bdc2019-11-29 22:06:47 +01002649 " window by wrapping around the window list.
2650 enew | only
2651 call setloclist(0, [], 'f')
2652 new | new
2653 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2654 lopen
2655 1close
2656 call assert_equal(0, getloclist(3, {'id' : 0}).id)
2657 lnext
2658 call assert_equal(3, winnr())
2659 call assert_equal(getloclist(1, {'id' : 0}).id, getloclist(3, {'id' : 0}).id)
2660
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002661 enew | only
2662 set efm&vim
2663endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01002664
Bram Moolenaare00fdf32019-09-15 19:09:42 +02002665func Test_cwindow_highlight()
2666 CheckScreendump
2667
2668 let lines =<< trim END
2669 set t_u7=
2670 call setline(1, ['some', 'text', 'with', 'matches'])
2671 write XCwindow
2672 vimgrep e XCwindow
2673 redraw
2674 cwindow 4
2675 END
2676 call writefile(lines, 'XtestCwindow')
2677 let buf = RunVimInTerminal('-S XtestCwindow', #{rows: 12})
2678 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_1', {})
2679
2680 call term_sendkeys(buf, ":cnext\<CR>")
2681 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_2', {})
2682
2683 " clean up
2684 call StopVimInTerminal(buf)
2685 call delete('XtestCwindow')
2686 call delete('XCwindow')
2687endfunc
2688
Bram Moolenaaree85df32017-03-19 14:19:50 +01002689func XvimgrepTests(cchar)
2690 call s:setup_commands(a:cchar)
2691
2692 call writefile(['Editor:VIM vim',
2693 \ 'Editor:Emacs EmAcS',
2694 \ 'Editor:Notepad NOTEPAD'], 'Xtestfile1')
2695 call writefile(['Linux', 'MacOS', 'MS-Windows'], 'Xtestfile2')
2696
2697 " Error cases
2698 call assert_fails('Xvimgrep /abc *', 'E682:')
2699
2700 let @/=''
2701 call assert_fails('Xvimgrep // *', 'E35:')
2702
2703 call assert_fails('Xvimgrep abc', 'E683:')
2704 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
2705 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
2706
2707 Xexpr ""
2708 Xvimgrepadd Notepad Xtestfile1
2709 Xvimgrepadd MacOS Xtestfile2
2710 let l = g:Xgetlist()
2711 call assert_equal(2, len(l))
2712 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
2713
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002714 10Xvimgrep #\cvim#g Xtestfile?
Bram Moolenaaree85df32017-03-19 14:19:50 +01002715 let l = g:Xgetlist()
2716 call assert_equal(2, len(l))
2717 call assert_equal(8, l[0].col)
2718 call assert_equal(12, l[1].col)
2719
2720 1Xvimgrep ?Editor? Xtestfile*
2721 let l = g:Xgetlist()
2722 call assert_equal(1, len(l))
2723 call assert_equal('Editor:VIM vim', l[0].text)
2724
2725 edit +3 Xtestfile2
2726 Xvimgrep +\cemacs+j Xtestfile1
2727 let l = g:Xgetlist()
2728 call assert_equal('Xtestfile2', bufname(''))
2729 call assert_equal('Editor:Emacs EmAcS', l[0].text)
2730
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02002731 " Test for unloading a buffer after vimgrep searched the buffer
2732 %bwipe
2733 Xvimgrep /Editor/j Xtestfile*
2734 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
2735 call assert_equal([], getbufinfo('Xtestfile2'))
2736
Bram Moolenaaree85df32017-03-19 14:19:50 +01002737 call delete('Xtestfile1')
2738 call delete('Xtestfile2')
2739endfunc
2740
2741" Tests for the :vimgrep command
2742func Test_vimgrep()
2743 call XvimgrepTests('c')
2744 call XvimgrepTests('l')
2745endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002746
Bram Moolenaar1c299432018-10-28 14:36:09 +01002747" Test for incsearch highlighting of the :vimgrep pattern
2748" This test used to cause "E315: ml_get: invalid lnum" errors.
2749func Test_vimgrep_incsearch()
2750 enew
2751 set incsearch
2752 call test_override("char_avail", 1)
2753
2754 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
2755 let l = getqflist()
2756 call assert_equal(2, len(l))
2757
2758 call test_override("ALL", 0)
2759 set noincsearch
2760endfunc
2761
Bram Moolenaar9f6277b2020-02-11 22:04:02 +01002762" Test vimgrep with the last search pattern not set
2763func Test_vimgrep_with_no_last_search_pat()
2764 let lines =<< trim [SCRIPT]
2765 call assert_fails('vimgrep // *', 'E35:')
2766 call writefile(v:errors, 'Xresult')
2767 qall!
2768 [SCRIPT]
2769 call writefile(lines, 'Xscript')
2770 if RunVim([], [], '--clean -S Xscript')
2771 call assert_equal([], readfile('Xresult'))
2772 endif
2773 call delete('Xscript')
2774 call delete('Xresult')
2775endfunc
2776
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002777func XfreeTests(cchar)
2778 call s:setup_commands(a:cchar)
2779
2780 enew | only
2781
2782 " Deleting the quickfix stack should work even When the current list is
2783 " somewhere in the middle of the stack
2784 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2785 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2786 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2787 Xolder
2788 call g:Xsetlist([], 'f')
2789 call assert_equal(0, len(g:Xgetlist()))
2790
2791 " After deleting the stack, adding a new list should create a stack with a
2792 " single list.
2793 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2794 call assert_equal(1, g:Xgetlist({'all':1}).nr)
2795
2796 " Deleting the stack from a quickfix window should update/clear the
2797 " quickfix/location list window.
2798 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2799 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2800 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2801 Xolder
2802 Xwindow
2803 call g:Xsetlist([], 'f')
2804 call assert_equal(2, winnr('$'))
2805 call assert_equal(1, line('$'))
2806 Xclose
2807
2808 " Deleting the stack from a non-quickfix window should update/clear the
2809 " quickfix/location list window.
2810 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2811 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2812 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2813 Xolder
2814 Xwindow
2815 wincmd p
2816 call g:Xsetlist([], 'f')
2817 call assert_equal(0, len(g:Xgetlist()))
2818 wincmd p
2819 call assert_equal(2, winnr('$'))
2820 call assert_equal(1, line('$'))
2821
2822 " After deleting the location list stack, if the location list window is
2823 " opened, then a new location list should be created. So opening the
2824 " location list window again should not create a new window.
2825 if a:cchar == 'l'
2826 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2827 wincmd p
2828 lopen
2829 call assert_equal(2, winnr('$'))
2830 endif
2831 Xclose
2832endfunc
2833
Bram Moolenaar74240d32017-12-10 15:26:15 +01002834" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002835func Test_qf_free()
2836 call XfreeTests('c')
2837 call XfreeTests('l')
2838endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002839
2840" Test for buffer overflow when parsing lines and adding new entries to
2841" the quickfix list.
2842func Test_bufoverflow()
2843 set efm=%f:%l:%m
2844 cgetexpr ['File1:100:' . repeat('x', 1025)]
2845
2846 set efm=%+GCompiler:\ %.%#,%f:%l:%m
2847 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
2848
2849 set efm=%DEntering\ directory\ %f,%f:%l:%m
2850 cgetexpr ['Entering directory ' . repeat('a', 1006),
2851 \ 'File1:10:Hello World']
2852 set efm&vim
2853endfunc
2854
Bram Moolenaar875feea2017-06-11 16:07:51 +02002855" Tests for getting the quickfix stack size
2856func XsizeTests(cchar)
2857 call s:setup_commands(a:cchar)
2858
2859 call g:Xsetlist([], 'f')
2860 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01002861 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
2862 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02002863
2864 Xexpr "File1:10:Line1"
2865 Xexpr "File2:20:Line2"
2866 Xexpr "File3:30:Line3"
2867 Xolder | Xolder
2868 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2869 call g:Xsetlist([], 'f')
2870
2871 Xexpr "File1:10:Line1"
2872 Xexpr "File2:20:Line2"
2873 Xexpr "File3:30:Line3"
2874 Xolder | Xolder
2875 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
2876 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
2877endfunc
2878
2879func Test_Qf_Size()
2880 call XsizeTests('c')
2881 call XsizeTests('l')
2882endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02002883
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002884func Test_cclose_from_copen()
2885 augroup QF_Test
2886 au!
2887 au FileType qf :call assert_fails(':cclose', 'E788')
2888 augroup END
2889 copen
2890 augroup QF_Test
2891 au!
2892 augroup END
2893 augroup! QF_Test
2894endfunc
2895
Bram Moolenaar18141832017-06-25 21:17:25 +02002896func Test_cclose_in_autocmd()
2897 " Problem is only triggered if "starting" is zero, so that the OptionsSet
2898 " event will be triggered.
2899 call test_override('starting', 1)
2900 augroup QF_Test
2901 au!
2902 au FileType qf :call assert_fails(':cclose', 'E788')
2903 augroup END
2904 copen
2905 augroup QF_Test
2906 au!
2907 augroup END
2908 augroup! QF_Test
2909 call test_override('starting', 0)
2910endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002911
Bram Moolenaar379fb762018-08-30 15:58:28 +02002912" Check that ":file" without an argument is possible even when "curbuf_lock"
2913" is set.
2914func Test_file_from_copen()
2915 " Works without argument.
2916 augroup QF_Test
2917 au!
2918 au FileType qf file
2919 augroup END
2920 copen
2921
2922 augroup QF_Test
2923 au!
2924 augroup END
2925 cclose
2926
2927 " Fails with argument.
2928 augroup QF_Test
2929 au!
2930 au FileType qf call assert_fails(':file foo', 'E788')
2931 augroup END
2932 copen
2933 augroup QF_Test
2934 au!
2935 augroup END
2936 cclose
2937
2938 augroup! QF_Test
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02002939endfunc
Bram Moolenaar379fb762018-08-30 15:58:28 +02002940
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002941func Test_resize_from_copen()
2942 augroup QF_Test
2943 au!
2944 au FileType qf resize 5
2945 augroup END
2946 try
2947 " This should succeed without any exception. No other buffers are
2948 " involved in the autocmd.
2949 copen
2950 finally
2951 augroup QF_Test
2952 au!
2953 augroup END
2954 augroup! QF_Test
2955 endtry
2956endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02002957
2958" Tests for the quickfix buffer b:changedtick variable
2959func Xchangedtick_tests(cchar)
2960 call s:setup_commands(a:cchar)
2961
2962 new | only
2963
2964 Xexpr "" | Xexpr "" | Xexpr ""
2965
2966 Xopen
2967 Xolder
2968 Xolder
2969 Xaddexpr "F1:10:Line10"
2970 Xaddexpr "F2:20:Line20"
2971 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
2972 call g:Xsetlist([], 'f')
2973 call assert_equal(8, getbufvar('%', 'changedtick'))
2974 Xclose
2975endfunc
2976
2977func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02002978 call Xchangedtick_tests('c')
2979 call Xchangedtick_tests('l')
2980endfunc
2981
2982" Tests for parsing an expression using setqflist()
2983func Xsetexpr_tests(cchar)
2984 call s:setup_commands(a:cchar)
2985
2986 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002987 call g:Xsetlist([], ' ', {'lines' : t})
2988 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002989
2990 let l = g:Xgetlist()
2991 call assert_equal(3, len(l))
2992 call assert_equal(20, l[1].lnum)
2993 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002994 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002995 let l = g:Xgetlist()
2996 call assert_equal(1, len(l))
2997 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002998 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
2999 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003000
3001 call g:Xsetlist([], 'f')
3002 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003003 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
3004 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
3005 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
3006 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003007 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
3008 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02003009
3010 " Adding entries using a custom efm
3011 set efm&
3012 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
3013 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
3014 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3015 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
3016 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
3017 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
3018 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
3019 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02003020endfunc
3021
3022func Test_setexpr()
3023 call Xsetexpr_tests('c')
3024 call Xsetexpr_tests('l')
3025endfunc
3026
3027" Tests for per quickfix/location list directory stack
3028func Xmultidirstack_tests(cchar)
3029 call s:setup_commands(a:cchar)
3030
3031 call g:Xsetlist([], 'f')
3032 Xexpr "" | Xexpr ""
3033
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003034 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
3035 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
3036 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
3037 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003038
3039 let l1 = g:Xgetlist({'nr':1, 'items':1})
3040 let l2 = g:Xgetlist({'nr':2, 'items':1})
3041 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
3042 call assert_equal(3, l1.items[1].lnum)
3043 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
3044 call assert_equal(5, l2.items[1].lnum)
3045endfunc
3046
3047func Test_multidirstack()
3048 call mkdir('Xone/a', 'p')
3049 call mkdir('Xtwo/a', 'p')
3050 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3051 call writefile(lines, 'Xone/a/one.txt')
3052 call writefile(lines, 'Xtwo/a/two.txt')
3053 let save_efm = &efm
3054 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
3055
3056 call Xmultidirstack_tests('c')
3057 call Xmultidirstack_tests('l')
3058
3059 let &efm = save_efm
3060 call delete('Xone', 'rf')
3061 call delete('Xtwo', 'rf')
3062endfunc
3063
3064" Tests for per quickfix/location list file stack
3065func Xmultifilestack_tests(cchar)
3066 call s:setup_commands(a:cchar)
3067
3068 call g:Xsetlist([], 'f')
3069 Xexpr "" | Xexpr ""
3070
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003071 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
3072 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
3073 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
3074 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02003075
3076 let l1 = g:Xgetlist({'nr':1, 'items':1})
3077 let l2 = g:Xgetlist({'nr':2, 'items':1})
3078 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
3079 call assert_equal(3, l1.items[1].lnum)
3080 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
3081 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02003082
3083 " Test for start of a new error line in the same line where a previous
3084 " error line ends with a file stack.
3085 let efm_val = 'Error\ l%l\ in\ %f,'
3086 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
3087 let l = g:Xgetlist({'lines' : [
3088 \ '(one.txt',
3089 \ 'Error l4 in one.txt',
3090 \ ') (two.txt',
3091 \ 'Error l6 in two.txt',
3092 \ ')',
3093 \ 'Error l8 in one.txt'
3094 \ ], 'efm' : efm_val})
3095 call assert_equal(3, len(l.items))
3096 call assert_equal('one.txt', bufname(l.items[0].bufnr))
3097 call assert_equal(4, l.items[0].lnum)
3098 call assert_equal('one.txt', l.items[0].text)
3099 call assert_equal('two.txt', bufname(l.items[1].bufnr))
3100 call assert_equal(6, l.items[1].lnum)
3101 call assert_equal('two.txt', l.items[1].text)
3102 call assert_equal('one.txt', bufname(l.items[2].bufnr))
3103 call assert_equal(8, l.items[2].lnum)
3104 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02003105endfunc
3106
3107func Test_multifilestack()
3108 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3109 call writefile(lines, 'one.txt')
3110 call writefile(lines, 'two.txt')
3111 let save_efm = &efm
3112 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
3113
3114 call Xmultifilestack_tests('c')
3115 call Xmultifilestack_tests('l')
3116
3117 let &efm = save_efm
3118 call delete('one.txt')
3119 call delete('two.txt')
3120endfunc
3121
3122" Tests for per buffer 'efm' setting
3123func Test_perbuf_efm()
3124 call writefile(["File1-10-Line10"], 'one.txt')
3125 call writefile(["File2#20#Line20"], 'two.txt')
3126 set efm=%f#%l#%m
3127 new | only
3128 new
3129 setlocal efm=%f-%l-%m
3130 cfile one.txt
3131 wincmd w
3132 caddfile two.txt
3133
3134 let l = getqflist()
3135 call assert_equal(10, l[0].lnum)
3136 call assert_equal('Line20', l[1].text)
3137
3138 set efm&
3139 new | only
3140 call delete('one.txt')
3141 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02003142endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003143
3144" Open multiple help windows using ":lhelpgrep
3145" This test used to crash Vim
3146func Test_Multi_LL_Help()
3147 new | only
3148 lhelpgrep window
3149 lopen
3150 e#
3151 lhelpgrep buffer
3152 call assert_equal(3, winnr('$'))
3153 call assert_true(len(getloclist(1)) != 0)
3154 call assert_true(len(getloclist(2)) != 0)
3155 new | only
3156endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02003157
3158" Tests for adding new quickfix lists using setqflist()
3159func XaddQf_tests(cchar)
3160 call s:setup_commands(a:cchar)
3161
3162 " Create a new list using ' ' for action
3163 call g:Xsetlist([], 'f')
3164 call g:Xsetlist([], ' ', {'title' : 'Test1'})
3165 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3166 call assert_equal(1, l.nr)
3167 call assert_equal('Test1', l.title)
3168
3169 " Create a new list using ' ' for action and '$' for 'nr'
3170 call g:Xsetlist([], 'f')
3171 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
3172 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3173 call assert_equal(1, l.nr)
3174 call assert_equal('Test2', l.title)
3175
3176 " Create a new list using 'a' for action
3177 call g:Xsetlist([], 'f')
3178 call g:Xsetlist([], 'a', {'title' : 'Test3'})
3179 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3180 call assert_equal(1, l.nr)
3181 call assert_equal('Test3', l.title)
3182
3183 " Create a new list using 'a' for action and '$' for 'nr'
3184 call g:Xsetlist([], 'f')
3185 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
3186 call g:Xsetlist([], 'a', {'title' : 'Test4'})
3187 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3188 call assert_equal(1, l.nr)
3189 call assert_equal('Test4', l.title)
3190
3191 " Adding a quickfix list should remove all the lists following the current
3192 " list.
3193 Xexpr "" | Xexpr "" | Xexpr ""
3194 silent! 10Xolder
3195 call g:Xsetlist([], ' ', {'title' : 'Test5'})
3196 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3197 call assert_equal(2, l.nr)
3198 call assert_equal('Test5', l.title)
3199
3200 " Add a quickfix list using '$' as the list number.
3201 let lastqf = g:Xgetlist({'nr':'$'}).nr
3202 silent! 99Xolder
3203 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
3204 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3205 call assert_equal(lastqf + 1, l.nr)
3206 call assert_equal('Test6', l.title)
3207
3208 " Add a quickfix list using 'nr' set to one more than the quickfix
3209 " list size.
3210 let lastqf = g:Xgetlist({'nr':'$'}).nr
3211 silent! 99Xolder
3212 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
3213 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3214 call assert_equal(lastqf + 1, l.nr)
3215 call assert_equal('Test7', l.title)
3216
3217 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
3218 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
3219 silent! 99Xolder
3220 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
3221 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3222 call assert_equal(10, l.nr)
3223 call assert_equal('Test8', l.title)
3224
3225 " Add a quickfix list using 'nr' set to a value greater than 10
3226 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
3227
3228 " Try adding a quickfix list with 'nr' set to a value greater than the
3229 " quickfix list size but less than 10.
3230 call g:Xsetlist([], 'f')
3231 Xexpr "" | Xexpr "" | Xexpr ""
3232 silent! 99Xolder
3233 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
3234
3235 " Add a quickfix list using 'nr' set to a some string or list
3236 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
3237endfunc
3238
3239func Test_add_qf()
3240 call XaddQf_tests('c')
3241 call XaddQf_tests('l')
3242endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003243
3244" Test for getting the quickfix list items from some text without modifying
3245" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003246func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003247 call s:setup_commands(a:cchar)
3248 call g:Xsetlist([], 'f')
3249
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003250 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003251 call assert_equal(2, len(l))
3252 call assert_equal(30, l[1].lnum)
3253
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003254 call assert_equal({}, g:Xgetlist({'lines' : 10}))
3255 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
3256 call assert_equal([], g:Xgetlist({'lines' : []}).items)
3257 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003258
Bram Moolenaar36538222017-09-02 19:51:44 +02003259 " Parse text using a custom efm
3260 set efm&
3261 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
3262 call assert_equal('Line30', l[0].text)
3263 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
3264 call assert_equal('File3:30:Line30', l[0].text)
3265 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
3266 call assert_equal({}, l)
3267 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
3268 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
3269
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003270 " Make sure that the quickfix stack is not modified
3271 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
3272endfunc
3273
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003274func Test_get_list_from_lines()
3275 call XgetListFromLines('c')
3276 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003277endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003278
3279" Tests for the quickfix list id
3280func Xqfid_tests(cchar)
3281 call s:setup_commands(a:cchar)
3282
3283 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003284 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003285 Xexpr ''
3286 let start_id = g:Xgetlist({'id' : 0}).id
3287 Xexpr '' | Xexpr ''
3288 Xolder
3289 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
3290 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
3291 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003292 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003293 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003294 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3295 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003296
3297 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3298 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003299 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003300 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3301 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3302 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3303
3304 let qfid = g:Xgetlist({'id':0, 'nr':0})
3305 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003306 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003307endfunc
3308
3309func Test_qf_id()
3310 call Xqfid_tests('c')
3311 call Xqfid_tests('l')
3312endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003313
3314func Xqfjump_tests(cchar)
3315 call s:setup_commands(a:cchar)
3316
3317 call writefile(["Line1\tFoo", "Line2"], 'F1')
3318 call writefile(["Line1\tBar", "Line2"], 'F2')
3319 call writefile(["Line1\tBaz", "Line2"], 'F3')
3320
3321 call g:Xsetlist([], 'f')
3322
3323 " Tests for
3324 " Jumping to a line using a pattern
3325 " Jumping to a column greater than the last column in a line
3326 " Jumping to a line greater than the last line in the file
3327 let l = []
3328 for i in range(1, 7)
3329 call add(l, {})
3330 endfor
3331 let l[0].filename='F1'
3332 let l[0].pattern='Line1'
3333 let l[1].filename='F2'
3334 let l[1].pattern='Line1'
3335 let l[2].filename='F3'
3336 let l[2].pattern='Line1'
3337 let l[3].filename='F3'
3338 let l[3].lnum=1
3339 let l[3].col=9
3340 let l[3].vcol=1
3341 let l[4].filename='F3'
3342 let l[4].lnum=99
3343 let l[5].filename='F3'
3344 let l[5].lnum=1
3345 let l[5].col=99
3346 let l[5].vcol=1
3347 let l[6].filename='F3'
3348 let l[6].pattern='abcxyz'
3349
3350 call g:Xsetlist([], ' ', {'items' : l})
3351 Xopen | only
3352 2Xnext
3353 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
3354 call assert_equal('F3', bufname('%'))
3355 Xnext
3356 call assert_equal(7, col('.'))
3357 Xnext
3358 call assert_equal(2, line('.'))
3359 Xnext
3360 call assert_equal(9, col('.'))
3361 2
3362 Xnext
3363 call assert_equal(2, line('.'))
3364
3365 if a:cchar == 'l'
3366 " When jumping to a location list entry in the location list window and
3367 " no usable windows are available, then a new window should be opened.
3368 enew! | new | only
3369 call g:Xsetlist([], 'f')
3370 setlocal buftype=nofile
3371 new
3372 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']})
3373 Xopen
3374 let winid = win_getid()
3375 wincmd p
3376 close
3377 call win_gotoid(winid)
3378 Xnext
3379 call assert_equal(3, winnr('$'))
3380 call assert_equal(1, winnr())
3381 call assert_equal(2, line('.'))
3382
3383 " When jumping to an entry in the location list window and the window
3384 " associated with the location list is not present and a window containing
3385 " the file is already present, then that window should be used.
3386 close
3387 belowright new
3388 call g:Xsetlist([], 'f')
3389 edit F3
3390 call win_gotoid(winid)
3391 Xlast
3392 call assert_equal(3, winnr())
3393 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3394 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3395 endif
3396
3397 " Cleanup
3398 enew!
3399 new | only
3400
3401 call delete('F1')
3402 call delete('F2')
3403 call delete('F3')
3404endfunc
3405
3406func Test_qfjump()
3407 call Xqfjump_tests('c')
3408 call Xqfjump_tests('l')
3409endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01003410
3411" Tests for the getqflist() and getloclist() functions when the list is not
3412" present or is empty
3413func Xgetlist_empty_tests(cchar)
3414 call s:setup_commands(a:cchar)
3415
3416 " Empty quickfix stack
3417 call g:Xsetlist([], 'f')
3418 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3419 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
3420 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3421 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3422 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
3423 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3424 call assert_equal('', g:Xgetlist({'title' : 0}).title)
3425 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003426 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003427 if a:cchar == 'c'
3428 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003429 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003430 \ 'title' : '', 'winid' : 0, 'changedtick': 0},
3431 \ g:Xgetlist({'all' : 0}))
3432 else
3433 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
3434 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003435 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
3436 \ 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003437 \ g:Xgetlist({'all' : 0}))
3438 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003439
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003440 " Quickfix window with empty stack
3441 silent! Xopen
3442 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003443 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003444 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
3445 Xclose
3446
Bram Moolenaara6d48492017-12-12 22:45:31 +01003447 " Empty quickfix list
3448 Xexpr ""
3449 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3450 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
3451 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3452 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3453 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
3454 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3455 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
3456 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003457 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003458
3459 let qfid = g:Xgetlist({'id' : 0}).id
3460 call g:Xsetlist([], 'f')
3461
3462 " Non-existing quickfix identifier
3463 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
3464 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
3465 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
3466 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
3467 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
3468 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
3469 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
3470 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003471 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003472 if a:cchar == 'c'
3473 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3474 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003475 \ 'qfbufnr' : qfbufnr,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003476 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
3477 else
3478 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3479 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003480 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003481 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
3482 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003483
3484 " Non-existing quickfix list number
3485 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
3486 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
3487 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
3488 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
3489 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
3490 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
3491 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
3492 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003493 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003494 if a:cchar == 'c'
3495 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3496 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003497 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr},
3498 \ g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003499 else
3500 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3501 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003502 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003503 \ g:Xgetlist({'nr' : 5, 'all' : 0}))
3504 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003505endfunc
3506
3507func Test_getqflist()
3508 call Xgetlist_empty_tests('c')
3509 call Xgetlist_empty_tests('l')
3510endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01003511
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01003512func Test_getqflist_invalid_nr()
3513 " The following commands used to crash Vim
3514 cexpr ""
3515 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
3516
3517 " Cleanup
3518 call setqflist([], 'r')
3519endfunc
3520
Bram Moolenaarb254af32017-12-18 19:48:58 +01003521" Tests for the quickfix/location list changedtick
3522func Xqftick_tests(cchar)
3523 call s:setup_commands(a:cchar)
3524
3525 call g:Xsetlist([], 'f')
3526
3527 Xexpr "F1:10:Line10"
3528 let qfid = g:Xgetlist({'id' : 0}).id
3529 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3530 Xaddexpr "F2:20:Line20\nF2:21:Line21"
3531 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3532 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
3533 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
3534 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
3535 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
3536 call g:Xsetlist([], 'a', {'title' : 'New Title'})
3537 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
3538
3539 enew!
3540 call append(0, ["F5:50:L50", "F6:60:L60"])
3541 Xaddbuffer
3542 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
3543 enew!
3544
3545 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
3546 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
3547 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3548 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
3549 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
3550 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3551 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
3552 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3553 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3554 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
3555 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3556
3557 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
3558 Xfile Xone
3559 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3560 Xaddfile Xone
3561 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3562
3563 " Test case for updating a non-current quickfix list
3564 call g:Xsetlist([], 'f')
3565 Xexpr "F1:1:L1"
3566 Xexpr "F2:2:L2"
3567 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
3568 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3569 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
3570
3571 call delete("Xone")
3572endfunc
3573
3574func Test_qf_tick()
3575 call Xqftick_tests('c')
3576 call Xqftick_tests('l')
3577endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01003578
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02003579" Test helpgrep with lang specifier
3580func Xtest_helpgrep_with_lang_specifier(cchar)
3581 call s:setup_commands(a:cchar)
3582 Xhelpgrep Vim@en
3583 call assert_equal('help', &filetype)
3584 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
3585 new | only
3586endfunc
3587
3588func Test_helpgrep_with_lang_specifier()
3589 call Xtest_helpgrep_with_lang_specifier('c')
3590 call Xtest_helpgrep_with_lang_specifier('l')
3591endfunc
3592
Bram Moolenaar12237442017-12-19 12:38:52 +01003593" The following test used to crash Vim.
3594" Open the location list window and close the regular window associated with
3595" the location list. When the garbage collection runs now, it incorrectly
3596" marks the location list context as not in use and frees the context.
3597func Test_ll_window_ctx()
3598 call setloclist(0, [], 'f')
3599 call setloclist(0, [], 'a', {'context' : []})
3600 lopen | only
3601 call test_garbagecollect_now()
3602 echo getloclist(0, {'context' : 1}).context
3603 enew | only
3604endfunc
3605
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01003606" The following test used to crash vim
3607func Test_lfile_crash()
3608 sp Xtest
3609 au QuickFixCmdPre * bw
3610 call assert_fails('lfile', 'E40')
3611 au! QuickFixCmdPre
3612endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01003613
3614" The following test used to crash vim
3615func Test_lbuffer_crash()
3616 sv Xtest
3617 augroup QF_Test
3618 au!
3619 au * * bw
3620 augroup END
3621 lbuffer
3622 augroup QF_Test
3623 au!
3624 augroup END
3625endfunc
3626
3627" The following test used to crash vim
3628func Test_lexpr_crash()
3629 augroup QF_Test
3630 au!
3631 au * * call setloclist(0, [], 'f')
3632 augroup END
3633 lexpr ""
3634 augroup QF_Test
3635 au!
3636 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003637
Bram Moolenaar3c097222017-12-21 20:54:49 +01003638 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003639 augroup QF_Test
3640 au!
3641 au BufNew * call setloclist(0, [], 'f')
3642 augroup END
3643 lexpr 'x:1:x'
3644 augroup QF_Test
3645 au!
3646 augroup END
3647
3648 enew | only
3649 lexpr ''
3650 lopen
3651 augroup QF_Test
3652 au!
3653 au FileType * call setloclist(0, [], 'f')
3654 augroup END
3655 lexpr ''
3656 augroup QF_Test
3657 au!
3658 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01003659endfunc
3660
3661" The following test used to crash Vim
3662func Test_lvimgrep_crash()
3663 sv Xtest
3664 augroup QF_Test
3665 au!
3666 au * * call setloclist(0, [], 'f')
3667 augroup END
3668 lvimgrep quickfix test_quickfix.vim
3669 augroup QF_Test
3670 au!
3671 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003672
3673 new | only
3674 augroup QF_Test
3675 au!
3676 au BufEnter * call setloclist(0, [], 'r')
3677 augroup END
3678 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
3679 augroup QF_Test
3680 au!
3681 augroup END
3682
Bram Moolenaar3c097222017-12-21 20:54:49 +01003683 enew | only
3684endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01003685
3686" Test for the position of the quickfix and location list window
3687func Test_qfwin_pos()
3688 " Open two windows
3689 new | only
3690 new
3691 cexpr ['F1:10:L10']
3692 copen
3693 " Quickfix window should be the bottom most window
3694 call assert_equal(3, winnr())
3695 close
3696 " Open at the very top
3697 wincmd t
3698 topleft copen
3699 call assert_equal(1, winnr())
3700 close
3701 " open left of the current window
3702 wincmd t
3703 below new
3704 leftabove copen
3705 call assert_equal(2, winnr())
3706 close
3707 " open right of the current window
3708 rightbelow copen
3709 call assert_equal(3, winnr())
3710 close
3711endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02003712
3713" Tests for quickfix/location lists changed by autocommands when
3714" :vimgrep/:lvimgrep commands are running.
3715func Test_vimgrep_autocmd()
3716 call setqflist([], 'f')
3717 call writefile(['stars'], 'Xtest1.txt')
3718 call writefile(['stars'], 'Xtest2.txt')
3719
3720 " Test 1:
3721 " When searching for a pattern using :vimgrep, if the quickfix list is
3722 " changed by an autocmd, the results should be added to the correct quickfix
3723 " list.
3724 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
3725 silent vimgrep stars Xtest*.txt
3726 call assert_equal(1, getqflist({'nr' : 0}).nr)
3727 call assert_equal(3, getqflist({'nr' : '$'}).nr)
3728 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
3729 au! BufRead Xtest2.txt
3730
3731 " Test 2:
3732 " When searching for a pattern using :vimgrep, if the quickfix list is
3733 " freed, then a error should be given.
3734 silent! %bwipe!
3735 call setqflist([], 'f')
3736 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
3737 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
3738 au! BufRead Xtest2.txt
3739
3740 " Test 3:
3741 " When searching for a pattern using :lvimgrep, if the location list is
3742 " freed, then the command should error out.
3743 silent! %bwipe!
3744 let g:save_winid = win_getid()
3745 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
3746 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
3747 au! BufRead Xtest2.txt
3748
3749 call delete('Xtest1.txt')
3750 call delete('Xtest2.txt')
3751 call setqflist([], 'f')
3752endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003753
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003754" Test for an autocmd changing the current directory when running vimgrep
3755func Xvimgrep_autocmd_cd(cchar)
3756 call s:setup_commands(a:cchar)
3757
3758 %bwipe
3759 let save_cwd = getcwd()
3760
3761 augroup QF_Test
3762 au!
3763 autocmd BufRead * silent cd %:p:h
3764 augroup END
3765
3766 10Xvimgrep /vim/ Xdir/**
3767 let l = g:Xgetlist()
3768 call assert_equal('f1.txt', bufname(l[0].bufnr))
3769 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
3770
3771 augroup QF_Test
3772 au!
3773 augroup END
3774
3775 exe 'cd ' . save_cwd
3776endfunc
3777
3778func Test_vimgrep_autocmd_cd()
3779 call mkdir('Xdir/a', 'p')
3780 call mkdir('Xdir/b', 'p')
3781 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xdir/a/f1.txt')
3782 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xdir/b/f2.txt')
3783 call Xvimgrep_autocmd_cd('c')
3784 call Xvimgrep_autocmd_cd('l')
3785 %bwipe
3786 call delete('Xdir', 'rf')
3787endfunc
3788
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003789" The following test used to crash Vim
3790func Test_lhelpgrep_autocmd()
3791 lhelpgrep quickfix
3792 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
3793 lhelpgrep buffer
3794 call assert_equal('help', &filetype)
3795 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
3796 lhelpgrep tabpage
3797 call assert_equal('help', &filetype)
3798 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
3799 au! QuickFixCmdPost
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003800
3801 new | only
3802 augroup QF_Test
3803 au!
3804 au BufEnter * call setqflist([], 'f')
3805 augroup END
3806 call assert_fails('helpgrep quickfix', 'E925:')
3807 augroup QF_Test
3808 au! BufEnter
3809 augroup END
3810
3811 new | only
3812 augroup QF_Test
3813 au!
3814 au BufEnter * call setqflist([], 'r')
3815 augroup END
3816 call assert_fails('helpgrep quickfix', 'E925:')
3817 augroup QF_Test
3818 au! BufEnter
3819 augroup END
3820
3821 new | only
3822 augroup QF_Test
3823 au!
3824 au BufEnter * call setloclist(0, [], 'r')
3825 augroup END
3826 call assert_fails('lhelpgrep quickfix', 'E926:')
3827 augroup QF_Test
3828 au! BufEnter
3829 augroup END
3830
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003831 new | only
3832endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02003833
3834" Test for shortening/simplifying the file name when opening the
3835" quickfix window or when displaying the quickfix list
3836func Test_shorten_fname()
3837 if !has('unix')
3838 return
3839 endif
3840 %bwipe
3841 " Create a quickfix list with a absolute path filename
3842 let fname = getcwd() . '/test_quickfix.vim'
3843 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
3844 call assert_equal(fname, bufname('test_quickfix.vim'))
3845 " Opening the quickfix window should simplify the file path
3846 cwindow
3847 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
3848 cclose
3849 %bwipe
3850 " Create a quickfix list with a absolute path filename
3851 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
3852 call assert_equal(fname, bufname('test_quickfix.vim'))
3853 " Displaying the quickfix list should simplify the file path
3854 silent! clist
3855 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
3856endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02003857
3858" Quickfix title tests
3859" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
3860" Otherwise due to indentation, the title is set with spaces at the beginning
3861" of the command.
3862func Test_qftitle()
3863 call writefile(["F1:1:Line1"], 'Xerr')
3864
3865 " :cexpr
3866 exe "cexpr readfile('Xerr')"
3867 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
3868
3869 " :cgetexpr
3870 exe "cgetexpr readfile('Xerr')"
3871 call assert_equal(":cgetexpr readfile('Xerr')",
3872 \ getqflist({'title' : 1}).title)
3873
3874 " :caddexpr
3875 call setqflist([], 'f')
3876 exe "caddexpr readfile('Xerr')"
3877 call assert_equal(":caddexpr readfile('Xerr')",
3878 \ getqflist({'title' : 1}).title)
3879
3880 " :cbuffer
3881 new Xerr
3882 exe "cbuffer"
3883 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
3884
3885 " :cgetbuffer
3886 edit Xerr
3887 exe "cgetbuffer"
3888 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
3889
3890 " :caddbuffer
3891 call setqflist([], 'f')
3892 edit Xerr
3893 exe "caddbuffer"
3894 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
3895
3896 " :cfile
3897 exe "cfile Xerr"
3898 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
3899
3900 " :cgetfile
3901 exe "cgetfile Xerr"
3902 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
3903
3904 " :caddfile
3905 call setqflist([], 'f')
3906 exe "caddfile Xerr"
3907 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
3908
3909 " :grep
3910 set grepprg=internal
3911 exe "grep F1 Xerr"
3912 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
3913
3914 " :grepadd
3915 call setqflist([], 'f')
3916 exe "grepadd F1 Xerr"
3917 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
3918 set grepprg&vim
3919
3920 " :vimgrep
3921 exe "vimgrep F1 Xerr"
3922 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
3923
3924 " :vimgrepadd
3925 call setqflist([], 'f')
3926 exe "vimgrepadd F1 Xerr"
3927 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
3928
3929 call setqflist(['F1:10:L10'], ' ')
3930 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3931
3932 call setqflist([], 'f')
3933 call setqflist(['F1:10:L10'], 'a')
3934 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3935
3936 call setqflist([], 'f')
3937 call setqflist(['F1:10:L10'], 'r')
3938 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3939
3940 close
3941 call delete('Xerr')
3942
3943 call setqflist([], ' ', {'title' : 'Errors'})
3944 copen
3945 call assert_equal('Errors', w:quickfix_title)
3946 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
3947 call assert_equal('Errors', w:quickfix_title)
3948 cclose
3949endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02003950
3951func Test_lbuffer_with_bwipe()
3952 new
3953 new
3954 augroup nasty
3955 au * * bwipe
3956 augroup END
3957 lbuffer
3958 augroup nasty
3959 au!
3960 augroup END
3961endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02003962
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003963" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
3964" running
3965func Xexpr_acmd_freelist(cchar)
3966 call s:setup_commands(a:cchar)
3967
Bram Moolenaar0366c012018-06-18 20:52:13 +02003968 " This was using freed memory.
3969 augroup nasty
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003970 au * * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02003971 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003972 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02003973 augroup nasty
3974 au!
3975 augroup END
3976endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003977
3978func Test_cexpr_acmd_freelist()
3979 call Xexpr_acmd_freelist('c')
3980 call Xexpr_acmd_freelist('l')
3981endfunc
3982
3983" Test for commands that create a new quickfix/location list and jump to the
3984" first error automatically.
3985func Xjumpto_first_error_test(cchar)
3986 call s:setup_commands(a:cchar)
3987
3988 call s:create_test_file('Xtestfile1')
3989 call s:create_test_file('Xtestfile2')
3990 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
3991
3992 " Test for cexpr/lexpr
3993 enew
3994 Xexpr l
3995 call assert_equal('Xtestfile1', bufname(''))
3996 call assert_equal(2, line('.'))
3997
3998 " Test for cfile/lfile
3999 enew
4000 call writefile(l, 'Xerr')
4001 Xfile Xerr
4002 call assert_equal('Xtestfile1', bufname(''))
4003 call assert_equal(2, line('.'))
4004
4005 " Test for cbuffer/lbuffer
4006 edit Xerr
4007 Xbuffer
4008 call assert_equal('Xtestfile1', bufname(''))
4009 call assert_equal(2, line('.'))
4010
4011 call delete('Xerr')
4012 call delete('Xtestfile1')
4013 call delete('Xtestfile2')
4014endfunc
4015
4016func Test_jumpto_first_error()
4017 call Xjumpto_first_error_test('c')
4018 call Xjumpto_first_error_test('l')
4019endfunc
4020
4021" Test for a quickfix autocmd changing the quickfix/location list before
4022" jumping to the first error in the new list.
4023func Xautocmd_changelist(cchar)
4024 call s:setup_commands(a:cchar)
4025
4026 " Test for cfile/lfile
4027 call s:create_test_file('Xtestfile1')
4028 call s:create_test_file('Xtestfile2')
4029 Xexpr 'Xtestfile1:2:Line2'
4030 autocmd QuickFixCmdPost * Xolder
4031 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4032 Xfile Xerr
4033 call assert_equal('Xtestfile2', bufname(''))
4034 call assert_equal(4, line('.'))
4035 autocmd! QuickFixCmdPost
4036
4037 " Test for cbuffer/lbuffer
4038 call g:Xsetlist([], 'f')
4039 Xexpr 'Xtestfile1:2:Line2'
4040 autocmd QuickFixCmdPost * Xolder
4041 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
4042 edit Xerr
4043 Xbuffer
4044 call assert_equal('Xtestfile2', bufname(''))
4045 call assert_equal(4, line('.'))
4046 autocmd! QuickFixCmdPost
4047
4048 " Test for cexpr/lexpr
4049 call g:Xsetlist([], 'f')
4050 Xexpr 'Xtestfile1:2:Line2'
4051 autocmd QuickFixCmdPost * Xolder
4052 Xexpr 'Xtestfile2:4:Line4'
4053 call assert_equal('Xtestfile2', bufname(''))
4054 call assert_equal(4, line('.'))
4055 autocmd! QuickFixCmdPost
4056
Bram Moolenaar851332e2018-07-03 19:16:00 +02004057 " The grepprg may not be set on non-Unix systems
4058 if has('unix')
4059 " Test for grep/lgrep
4060 call g:Xsetlist([], 'f')
4061 Xexpr 'Xtestfile1:2:Line2'
4062 autocmd QuickFixCmdPost * Xolder
4063 silent Xgrep Line5 Xtestfile2
4064 call assert_equal('Xtestfile2', bufname(''))
4065 call assert_equal(5, line('.'))
4066 autocmd! QuickFixCmdPost
4067 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004068
4069 " Test for vimgrep/lvimgrep
4070 call g:Xsetlist([], 'f')
4071 Xexpr 'Xtestfile1:2:Line2'
4072 autocmd QuickFixCmdPost * Xolder
4073 silent Xvimgrep Line5 Xtestfile2
4074 call assert_equal('Xtestfile2', bufname(''))
4075 call assert_equal(5, line('.'))
4076 autocmd! QuickFixCmdPost
4077
Bram Moolenaar3f347e42018-08-09 21:19:20 +02004078 " Test for autocommands clearing the quickfix list before jumping to the
4079 " first error. This should not result in an error
4080 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
4081 let v:errmsg = ''
4082 " Test for cfile/lfile
4083 Xfile Xerr
4084 call assert_true(v:errmsg !~# 'E42:')
4085 " Test for cbuffer/lbuffer
4086 edit Xerr
4087 Xbuffer
4088 call assert_true(v:errmsg !~# 'E42:')
4089 " Test for cexpr/lexpr
4090 Xexpr 'Xtestfile2:4:Line4'
4091 call assert_true(v:errmsg !~# 'E42:')
4092 " Test for grep/lgrep
4093 " The grepprg may not be set on non-Unix systems
4094 if has('unix')
4095 silent Xgrep Line5 Xtestfile2
4096 call assert_true(v:errmsg !~# 'E42:')
4097 endif
4098 " Test for vimgrep/lvimgrep
4099 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
4100 autocmd! QuickFixCmdPost
4101
Bram Moolenaar531b9a32018-07-03 16:54:23 +02004102 call delete('Xerr')
4103 call delete('Xtestfile1')
4104 call delete('Xtestfile2')
4105endfunc
4106
4107func Test_autocmd_changelist()
4108 call Xautocmd_changelist('c')
4109 call Xautocmd_changelist('l')
4110endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004111
4112" Tests for the ':filter /pat/ clist' command
4113func Test_filter_clist()
4114 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4115 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4116 \ split(execute('filter /Line 15/ clist'), "\n"))
4117 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4118 \ split(execute('filter /Xfile1/ clist'), "\n"))
4119 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4120
4121 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4122 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4123 call assert_equal([' 2 pqr:pat2: '],
4124 \ split(execute('filter /pqr/ clist'), "\n"))
4125 call assert_equal([' 1 abc:pat1: '],
4126 \ split(execute('filter /pat1/ clist'), "\n"))
4127endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004128
4129" Tests for the "CTRL-W <CR>" command.
4130func Xview_result_split_tests(cchar)
4131 call s:setup_commands(a:cchar)
4132
4133 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4134 call g:Xsetlist([])
4135 Xopen
4136 let l:win_count = winnr('$')
4137 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42')
4138 call assert_equal(l:win_count, winnr('$'))
4139 Xclose
4140endfunc
4141
4142func Test_view_result_split()
4143 call Xview_result_split_tests('c')
4144 call Xview_result_split_tests('l')
4145endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004146
4147" Test that :cc sets curswant
4148func Test_curswant()
4149 helpgrep quickfix
4150 normal! llll
4151 1cc
4152 call assert_equal(getcurpos()[4], virtcol('.'))
4153 cclose | helpclose
4154endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004155
4156" Test for opening a file from the quickfix window using CTRL-W <Enter>
4157" doesn't leave an empty buffer around.
4158func Test_splitview()
4159 call s:create_test_file('Xtestfile1')
4160 call s:create_test_file('Xtestfile2')
4161 new | only
4162 let last_bufnr = bufnr('Test_sv_1', 1)
4163 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4164 cgetexpr l
4165 copen
4166 let numbufs = len(getbufinfo())
4167 exe "normal \<C-W>\<CR>"
4168 copen
4169 exe "normal j\<C-W>\<CR>"
4170 " Make sure new empty buffers are not created
4171 call assert_equal(numbufs, len(getbufinfo()))
4172 " Creating a new buffer should use the next available buffer number
4173 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4174 bwipe Test_sv_1
4175 bwipe Test_sv_2
4176 new | only
4177
4178 " When split opening files from location list window, make sure that two
4179 " windows doesn't refer to the same location list
4180 lgetexpr l
4181 let locid = getloclist(0, {'id' : 0}).id
4182 lopen
4183 exe "normal \<C-W>\<CR>"
4184 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4185 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4186 new | only
4187
4188 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004189 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004190 lhelpgrep window
4191 let locid = getloclist(0, {'id' : 0}).id
4192 lwindow
4193 exe "normal j\<C-W>\<CR>"
4194 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4195 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4196 new | only
4197
4198 call delete('Xtestfile1')
4199 call delete('Xtestfile2')
4200endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004201
4202" Test for parsing entries using visual screen column
4203func Test_viscol()
4204 enew
4205 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4206 edit Xfile1
4207
4208 " Use byte offset for column number
4209 set efm&
4210 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
4211 call assert_equal([5, 8], [col('.'), virtcol('.')])
4212 cnext
4213 call assert_equal([9, 12], [col('.'), virtcol('.')])
4214 cnext
4215 call assert_equal([14, 20], [col('.'), virtcol('.')])
4216
4217 " Use screen column offset for column number
4218 set efm=%f:%l:%v:%m
4219 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4220 call assert_equal([5, 8], [col('.'), virtcol('.')])
4221 cnext
4222 call assert_equal([9, 12], [col('.'), virtcol('.')])
4223 cnext
4224 call assert_equal([14, 20], [col('.'), virtcol('.')])
4225 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4226 call assert_equal([5, 8], [col('.'), virtcol('.')])
4227 cnext
4228 call assert_equal([10, 16], [col('.'), virtcol('.')])
4229 cnext
4230 call assert_equal([14, 20], [col('.'), virtcol('.')])
4231
4232 enew
4233 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4234
4235 " Use byte offset for column number
4236 set efm&
4237 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
4238 call assert_equal([8, 10], [col('.'), virtcol('.')])
4239 cnext
4240 call assert_equal([11, 17], [col('.'), virtcol('.')])
4241 cnext
4242 call assert_equal([16, 25], [col('.'), virtcol('.')])
4243
4244 " Use screen column offset for column number
4245 set efm=%f:%l:%v:%m
4246 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4247 call assert_equal([8, 10], [col('.'), virtcol('.')])
4248 cnext
4249 call assert_equal([11, 17], [col('.'), virtcol('.')])
4250 cnext
4251 call assert_equal([16, 25], [col('.'), virtcol('.')])
4252
4253 enew | only
4254 set efm&
4255 call delete('Xfile1')
4256endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004257
4258" Test for the quickfix window buffer
4259func Xqfbuf_test(cchar)
4260 call s:setup_commands(a:cchar)
4261
4262 " Quickfix buffer should be reused across closing and opening a quickfix
4263 " window
4264 Xexpr "F1:10:Line10"
4265 Xopen
4266 let qfbnum = bufnr('')
4267 Xclose
4268 " Even after the quickfix window is closed, the buffer should be loaded
4269 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004270 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004271 Xopen
4272 " Buffer should be reused when opening the window again
4273 call assert_equal(qfbnum, bufnr(''))
4274 Xclose
4275
4276 if a:cchar == 'l'
4277 %bwipe
4278 " For a location list, when both the file window and the location list
4279 " window for the list are closed, then the buffer should be freed.
4280 new | only
4281 lexpr "F1:10:Line10"
4282 let wid = win_getid()
4283 lopen
4284 let qfbnum = bufnr('')
4285 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
4286 close
4287 " When the location list window is closed, the buffer name should not
4288 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004289 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004290 call assert_true(bufloaded(qfbnum))
4291
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004292 " After deleting a location list buffer using ":bdelete", opening the
4293 " location list window should mark the buffer as a location list buffer.
4294 exe "bdelete " . qfbnum
4295 lopen
4296 call assert_equal("quickfix", &buftype)
4297 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
4298 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
4299 call assert_false(&swapfile)
4300 lclose
4301
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004302 " When the location list is cleared for the window, the buffer should be
4303 " removed
4304 call setloclist(0, [], 'f')
4305 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004306 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004307
4308 " When the location list is freed with the location list window open, the
4309 " location list buffer should not be lost. It should be reused when the
4310 " location list is again populated.
4311 lexpr "F1:10:Line10"
4312 lopen
4313 let wid = win_getid()
4314 let qfbnum = bufnr('')
4315 wincmd p
4316 call setloclist(0, [], 'f')
4317 lexpr "F1:10:Line10"
4318 lopen
4319 call assert_equal(wid, win_getid())
4320 call assert_equal(qfbnum, bufnr(''))
4321 lclose
4322
4323 " When the window with the location list is closed, the buffer should be
4324 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004325 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004326 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004327 endif
4328endfunc
4329
4330func Test_qfbuf()
4331 call Xqfbuf_test('c')
4332 call Xqfbuf_test('l')
4333endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004334
4335" If there is an autocmd to use only one window, then opening the location
4336" list window used to crash Vim.
4337func Test_winonly_autocmd()
4338 call s:create_test_file('Xtest1')
4339 " Autocmd to show only one Vim window at a time
4340 autocmd WinEnter * only
4341 new
4342 " Load the location list
4343 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
4344 let loclistid = getloclist(0, {'id' : 0}).id
4345 " Open the location list window. Only this window will be shown and the file
4346 " window is closed.
4347 lopen
4348 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4349 " Jump to an entry in the location list and make sure that the cursor is
4350 " positioned correctly.
4351 ll 3
4352 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4353 call assert_equal('Xtest1', bufname(''))
4354 call assert_equal(15, line('.'))
4355 " Cleanup
4356 autocmd! WinEnter
4357 new | only
4358 call delete('Xtest1')
4359endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02004360
4361" Test to make sure that an empty quickfix buffer is not reused for loading
4362" a normal buffer.
4363func Test_empty_qfbuf()
4364 enew | only
4365 call writefile(["Test"], 'Xfile1')
4366 call setqflist([], 'f')
4367 copen | only
4368 let qfbuf = bufnr('')
4369 edit Xfile1
4370 call assert_notequal(qfbuf, bufnr(''))
4371 enew
4372 call delete('Xfile1')
4373endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004374
4375" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004376" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004377func Xtest_below(cchar)
4378 call s:setup_commands(a:cchar)
4379
4380 " No quickfix/location list
4381 call assert_fails('Xbelow', 'E42:')
4382 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004383 call assert_fails('Xbefore', 'E42:')
4384 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004385
4386 " Empty quickfix/location list
4387 call g:Xsetlist([])
4388 call assert_fails('Xbelow', 'E42:')
4389 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004390 call assert_fails('Xbefore', 'E42:')
4391 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004392
4393 call s:create_test_file('X1')
4394 call s:create_test_file('X2')
4395 call s:create_test_file('X3')
4396 call s:create_test_file('X4')
4397
4398 " Invalid entries
4399 edit X1
4400 call g:Xsetlist(["E1", "E2"])
4401 call assert_fails('Xbelow', 'E42:')
4402 call assert_fails('Xabove', 'E42:')
4403 call assert_fails('3Xbelow', 'E42:')
4404 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004405 call assert_fails('Xbefore', 'E42:')
4406 call assert_fails('Xafter', 'E42:')
4407 call assert_fails('3Xbefore', 'E42:')
4408 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004409
4410 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004411 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 +02004412 edit +7 X2
4413 Xabove
4414 call assert_equal(['X2', 5], [bufname(''), line('.')])
4415 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004416 normal 7G
4417 Xbefore
4418 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4419 call assert_fails('Xbefore', 'E553:')
4420
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004421 normal 2j
4422 Xbelow
4423 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004424 normal 7G
4425 Xafter
4426 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4427
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004428 " Last error in this file
4429 Xbelow 99
4430 call assert_equal(['X2', 15], [bufname(''), line('.')])
4431 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004432 normal gg
4433 Xafter 99
4434 call assert_equal(['X2', 15, 4], [bufname(''), line('.'), col('.')])
4435 call assert_fails('Xafter', 'E553:')
4436
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004437 " First error in this file
4438 Xabove 99
4439 call assert_equal(['X2', 5], [bufname(''), line('.')])
4440 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004441 normal G
4442 Xbefore 99
4443 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4444 call assert_fails('Xbefore', 'E553:')
4445
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004446 normal gg
4447 Xbelow 2
4448 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004449 normal gg
4450 Xafter 2
4451 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4452
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004453 normal G
4454 Xabove 2
4455 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004456 normal G
4457 Xbefore 2
4458 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4459
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004460 edit X4
4461 call assert_fails('Xabove', 'E42:')
4462 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004463 call assert_fails('Xbefore', 'E42:')
4464 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004465 if a:cchar == 'l'
4466 " If a buffer has location list entries from some other window but not
4467 " from the current window, then the commands should fail.
4468 edit X1 | split | call setloclist(0, [], 'f')
4469 call assert_fails('Xabove', 'E776:')
4470 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004471 call assert_fails('Xbefore', 'E776:')
4472 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004473 close
4474 endif
4475
4476 " Test for lines with multiple quickfix entries
4477 Xexpr ["X1:5:L5", "X2:5:1:L5_1", "X2:5:2:L5_2", "X2:5:3:L5_3",
4478 \ "X2:10:1:L10_1", "X2:10:2:L10_2", "X2:10:3:L10_3",
4479 \ "X2:15:1:L15_1", "X2:15:2:L15_2", "X2:15:3:L15_3", "X3:3:L3"]
4480 edit +1 X2
4481 Xbelow 2
4482 call assert_equal(['X2', 10, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004483 normal 1G
4484 Xafter 2
4485 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4486
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004487 normal gg
4488 Xbelow 99
4489 call assert_equal(['X2', 15, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004490 normal gg
4491 Xafter 99
4492 call assert_equal(['X2', 15, 3], [bufname(''), line('.'), col('.')])
4493
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004494 normal G
4495 Xabove 2
4496 call assert_equal(['X2', 10, 1], [bufname(''), line('.'), col('.')])
4497 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004498 Xbefore 2
4499 call assert_equal(['X2', 15, 2], [bufname(''), line('.'), col('.')])
4500
4501 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004502 Xabove 99
4503 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004504 normal G
4505 Xbefore 99
4506 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
4507
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004508 normal 10G
4509 Xabove
4510 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004511 normal 10G$
4512 2Xbefore
4513 call assert_equal(['X2', 10, 2], [bufname(''), line('.'), col('.')])
4514
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004515 normal 10G
4516 Xbelow
4517 call assert_equal(['X2', 15, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004518 normal 9G
4519 5Xafter
4520 call assert_equal(['X2', 15, 2], [bufname(''), line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004521
4522 " Invalid range
4523 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02004524 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004525 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004526 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02004527 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004528 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004529 endif
4530
4531 call delete('X1')
4532 call delete('X2')
4533 call delete('X3')
4534 call delete('X4')
4535endfunc
4536
4537func Test_cbelow()
4538 call Xtest_below('c')
4539 call Xtest_below('l')
4540endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02004541
4542func Test_quickfix_count()
4543 let commands = [
4544 \ 'cNext',
4545 \ 'cNfile',
4546 \ 'cabove',
4547 \ 'cbelow',
4548 \ 'cfirst',
4549 \ 'clast',
4550 \ 'cnewer',
4551 \ 'cnext',
4552 \ 'cnfile',
4553 \ 'colder',
4554 \ 'cprevious',
4555 \ 'crewind',
4556 \
4557 \ 'lNext',
4558 \ 'lNfile',
4559 \ 'labove',
4560 \ 'lbelow',
4561 \ 'lfirst',
4562 \ 'llast',
4563 \ 'lnewer',
4564 \ 'lnext',
4565 \ 'lnfile',
4566 \ 'lolder',
4567 \ 'lprevious',
4568 \ 'lrewind',
4569 \ ]
4570 for cmd in commands
4571 call assert_fails('-1' .. cmd, 'E16:')
4572 call assert_fails('.' .. cmd, 'E16:')
4573 call assert_fails('%' .. cmd, 'E16:')
4574 call assert_fails('$' .. cmd, 'E16:')
4575 endfor
4576endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02004577
4578" Test for aborting quickfix commands using QuickFixCmdPre
4579func Xtest_qfcmd_abort(cchar)
4580 call s:setup_commands(a:cchar)
4581
4582 call g:Xsetlist([], 'f')
4583
4584 " cexpr/lexpr
4585 let e = ''
4586 try
4587 Xexpr ["F1:10:Line10", "F2:20:Line20"]
4588 catch /.*/
4589 let e = v:exception
4590 endtry
4591 call assert_equal('AbortCmd', e)
4592 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4593
4594 " cfile/lfile
4595 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
4596 let e = ''
4597 try
4598 Xfile Xfile1
4599 catch /.*/
4600 let e = v:exception
4601 endtry
4602 call assert_equal('AbortCmd', e)
4603 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4604 call delete('Xfile1')
4605
4606 " cgetbuffer/lgetbuffer
4607 enew!
4608 call append(0, ["F1:10:Line10", "F2:20:Line20"])
4609 let e = ''
4610 try
4611 Xgetbuffer
4612 catch /.*/
4613 let e = v:exception
4614 endtry
4615 call assert_equal('AbortCmd', e)
4616 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4617 enew!
4618
4619 " vimgrep/lvimgrep
4620 let e = ''
4621 try
4622 Xvimgrep /func/ test_quickfix.vim
4623 catch /.*/
4624 let e = v:exception
4625 endtry
4626 call assert_equal('AbortCmd', e)
4627 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4628
4629 " helpgrep/lhelpgrep
4630 let e = ''
4631 try
4632 Xhelpgrep quickfix
4633 catch /.*/
4634 let e = v:exception
4635 endtry
4636 call assert_equal('AbortCmd', e)
4637 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4638
4639 " grep/lgrep
4640 if has('unix')
4641 let e = ''
4642 try
4643 silent Xgrep func test_quickfix.vim
4644 catch /.*/
4645 let e = v:exception
4646 endtry
4647 call assert_equal('AbortCmd', e)
4648 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4649 endif
4650endfunc
4651
4652func Test_qfcmd_abort()
4653 augroup QF_Test
4654 au!
4655 autocmd QuickFixCmdPre * throw "AbortCmd"
4656 augroup END
4657
4658 call Xtest_qfcmd_abort('c')
4659 call Xtest_qfcmd_abort('l')
4660
4661 augroup QF_Test
4662 au!
4663 augroup END
4664endfunc
4665
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004666" Test for using a file in one of the parent directories.
4667func Test_search_in_dirstack()
4668 call mkdir('Xtestdir/a/b/c', 'p')
4669 let save_cwd = getcwd()
4670 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
4671 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
4672 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
4673 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
4674
4675 let lines = "Entering dir Xtestdir\n" .
4676 \ "Entering dir a\n" .
4677 \ "Entering dir b\n" .
4678 \ "Xfile2:2:X2_L2\n" .
4679 \ "Leaving dir a\n" .
4680 \ "Xfile1:2:X1_L2\n" .
4681 \ "Xfile3:1:X3_L1\n" .
4682 \ "Entering dir c\n" .
4683 \ "Xfile4:2:X4_L2\n" .
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01004684 \ "Leaving dir c\n"
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004685 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01004686 cexpr lines .. "Leaving dir Xtestdir|\n" | let next = 1
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004687 call assert_equal(11, getqflist({'size' : 0}).size)
4688 call assert_equal(4, getqflist({'idx' : 0}).idx)
4689 call assert_equal('X2_L2', getline('.'))
Bram Moolenaar88a3e2b2019-12-06 21:11:39 +01004690 call assert_equal(1, next)
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004691 cnext
4692 call assert_equal(6, getqflist({'idx' : 0}).idx)
4693 call assert_equal('X1_L2', getline('.'))
4694 cnext
4695 call assert_equal(7, getqflist({'idx' : 0}).idx)
4696 call assert_equal(1, line('$'))
4697 call assert_equal('', getline(1))
4698 cnext
4699 call assert_equal(9, getqflist({'idx' : 0}).idx)
4700 call assert_equal(1, line('$'))
4701 call assert_equal('', getline(1))
4702
4703 set efm&
4704 exe 'cd ' . save_cwd
4705 call delete('Xtestdir', 'rf')
4706endfunc
4707
Bram Moolenaar1860bde2020-01-06 21:47:21 +01004708" Test for :cquit
4709func Test_cquit()
4710 " Exit Vim with a non-zero value
4711 if RunVim([], ["cquit 7"], '')
4712 call assert_equal(7, v:shell_error)
4713 endif
4714
4715 if RunVim([], ["50cquit"], '')
4716 call assert_equal(50, v:shell_error)
4717 endif
4718
4719 " Exit Vim with default value
4720 if RunVim([], ["cquit"], '')
4721 call assert_equal(1, v:shell_error)
4722 endif
4723
4724 " Exit Vim with zero value
4725 if RunVim([], ["cquit 0"], '')
4726 call assert_equal(0, v:shell_error)
4727 endif
4728
4729 " Exit Vim with negative value
4730 call assert_fails('-3cquit', 'E16:')
4731endfunc
4732
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02004733" vim: shiftwidth=2 sts=2 expandtab