blob: 44f7aeceaed1829f61594f86334680b884af333a [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 Moolenaar3ef5bf72016-06-15 22:41:31 +0200103 \ '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',
115 \ ' 5: non-error 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
116
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
273
274 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200275 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100276 call assert_true(winnr('$') == 1)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200277
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100278 " Specifying the width should adjust the width for a vertically split
279 " quickfix window.
280 vert Xopen
281 call assert_equal(10, winwidth(0))
282 vert Xopen 12
283 call assert_equal(12, winwidth(0))
284 Xclose
285
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200286 if a:cchar == 'c'
287 " Opening the quickfix window in multiple tab pages should reuse the
288 " quickfix buffer
289 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
290 \ 'Xtestfile3:3:1:Line3']
291 Xopen
292 let qfbufnum = bufnr('%')
293 tabnew
294 Xopen
295 call assert_equal(qfbufnum, bufnr('%'))
296 new | only | tabonly
297 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100298endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100299
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100300func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100301 call XwindowTests('c')
302 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100303endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100304
Bram Moolenaar36d50222019-05-02 20:17:40 +0200305func Test_copenHeight()
306 copen
307 wincmd H
308 let height = winheight(0)
309 copen 10
310 call assert_equal(height, winheight(0))
311 quit
312endfunc
313
Bram Moolenaar1142a312019-10-16 14:51:39 +0200314func Test_copenHeight_tabline()
315 set tabline=foo showtabline=2
316 copen
317 wincmd H
318 let height = winheight(0)
319 copen 10
320 call assert_equal(height, winheight(0))
321 quit
322 set tabline& showtabline&
323endfunc
324
325
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100326" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
327" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100328func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200329 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100330
331 call writefile(['Xtestfile1:700:10:Line 700',
332 \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
333
334 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200335 Xfile Xqftestfile1
336 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100337 call assert_true(len(l) == 2 &&
338 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
339 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
340
Bram Moolenaar049cba92016-06-26 14:38:04 +0200341 " Test with a non existent file
342 call assert_fails('Xfile non_existent_file', 'E40')
343
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100344 " Run cfile/lfile from a modified buffer
345 enew!
346 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200347 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100348 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
349
350 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200351 Xaddfile Xqftestfile1
352 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100353 call assert_true(len(l) == 3 &&
354 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
355
356 call writefile(['Xtestfile1:222:77:Line 222',
357 \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
358
359 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200360 Xgetfile Xqftestfile1
361 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100362 call assert_true(len(l) == 2 &&
363 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
364 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
365
366 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100367endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100368
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100369func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100370 call XfileTests('c')
371 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100372endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100373
374" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
375" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100376func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200377 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100378
379 enew!
380 silent! call setline(1, ['Xtestfile7:700:10:Line 700',
381 \ 'Xtestfile8:800:15:Line 800'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200382 Xbuffer!
383 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100384 call assert_true(len(l) == 2 &&
385 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
386 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
387
388 enew!
389 silent! call setline(1, ['Xtestfile9:900:55:Line 900',
390 \ 'Xtestfile10:950:66:Line 950'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200391 Xgetbuffer
392 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100393 call assert_true(len(l) == 2 &&
394 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
395 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
396
397 enew!
398 silent! call setline(1, ['Xtestfile11:700:20:Line 700',
399 \ 'Xtestfile12:750:25:Line 750'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200400 Xaddbuffer
401 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100402 call assert_true(len(l) == 4 &&
403 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
404 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
405 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200406 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100407
Bram Moolenaaree85df32017-03-19 14:19:50 +0100408 " Check for invalid buffer
409 call assert_fails('Xbuffer 199', 'E474:')
410
411 " Check for unloaded buffer
412 edit Xtestfile1
413 let bnr = bufnr('%')
414 enew!
415 call assert_fails('Xbuffer ' . bnr, 'E681:')
416
417 " Check for invalid range
418 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
419 " commands. So directly call the commands.
420 if (a:cchar == 'c')
421 call assert_fails('900,999cbuffer', 'E16:')
422 else
423 call assert_fails('900,999lbuffer', 'E16:')
424 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100425endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100426
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100427func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100428 call XbufferTests('c')
429 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100430endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100431
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100432func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200433 call s:setup_commands(a:cchar)
434
435 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100436endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200437
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100438func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200439 call XexprTests('c')
440 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100441endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200442
443" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100444func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200445 call s:setup_commands(a:cchar)
446
Bram Moolenaar74240d32017-12-10 15:26:15 +0100447 call g:Xsetlist([], 'f')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200448 " Jumping to first or next location list entry without any error should
449 " result in failure
Bram Moolenaar74240d32017-12-10 15:26:15 +0100450 if a:cchar == 'c'
451 let err = 'E42:'
452 else
453 let err = 'E776:'
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200454 endif
Bram Moolenaar74240d32017-12-10 15:26:15 +0100455 call assert_fails('Xnext', err)
456 call assert_fails('Xprev', err)
457 call assert_fails('Xnfile', err)
458 call assert_fails('Xpfile', err)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200459
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100460 Xexpr ''
461 let cmd = (a:cchar == 'c') ? '$cc' : '$ll'
462 call assert_fails(cmd, 'E42:')
463
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200464 call s:create_test_file('Xqftestfile1')
465 call s:create_test_file('Xqftestfile2')
466
467 Xgetexpr ['Xqftestfile1:5:Line5',
468 \ 'Xqftestfile1:6:Line6',
469 \ 'Xqftestfile2:10:Line10',
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200470 \ 'Xqftestfile2:11:Line11',
471 \ 'RegularLine1',
472 \ 'RegularLine2']
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200473
474 Xfirst
475 call assert_fails('Xprev', 'E553')
476 call assert_fails('Xpfile', 'E553')
477 Xnfile
478 call assert_equal('Xqftestfile2', bufname('%'))
479 call assert_equal(10, line('.'))
480 Xpfile
481 call assert_equal('Xqftestfile1', bufname('%'))
482 call assert_equal(6, line('.'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100483 5Xcc
484 call assert_equal(5, g:Xgetlist({'idx':0}).idx)
485 2Xcc
486 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100487 if a:cchar == 'c'
488 cc
489 else
490 ll
491 endif
492 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar74240d32017-12-10 15:26:15 +0100493 10Xcc
494 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200495 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200496 Xprev
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200497 call assert_equal('Xqftestfile2', bufname('%'))
498 call assert_equal(11, line('.'))
499 call assert_fails('Xnext', 'E553')
500 call assert_fails('Xnfile', 'E553')
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100501 " To process the range using quickfix list entries, directly use the
502 " quickfix commands (don't use the user defined commands)
503 if a:cchar == 'c'
504 $cc
505 else
506 $ll
507 endif
508 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200509 Xrewind
510 call assert_equal('Xqftestfile1', bufname('%'))
511 call assert_equal(5, line('.'))
512
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200513 10Xnext
514 call assert_equal('Xqftestfile2', bufname('%'))
515 call assert_equal(11, line('.'))
516 10Xprev
517 call assert_equal('Xqftestfile1', bufname('%'))
518 call assert_equal(5, line('.'))
519
Bram Moolenaar74240d32017-12-10 15:26:15 +0100520 " Jumping to an error from the error window using cc command
521 Xgetexpr ['Xqftestfile1:5:Line5',
522 \ 'Xqftestfile1:6:Line6',
523 \ 'Xqftestfile2:10:Line10',
524 \ 'Xqftestfile2:11:Line11']
525 Xopen
526 10Xcc
527 call assert_equal(11, line('.'))
528 call assert_equal('Xqftestfile2', bufname('%'))
529
530 " Jumping to an error from the error window (when only the error window is
531 " present)
532 Xopen | only
533 Xlast 1
534 call assert_equal(5, line('.'))
535 call assert_equal('Xqftestfile1', bufname('%'))
536
Bram Moolenaaree85df32017-03-19 14:19:50 +0100537 Xexpr ""
538 call assert_fails('Xnext', 'E42:')
539
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200540 call delete('Xqftestfile1')
541 call delete('Xqftestfile2')
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200542
543 " Should be able to use next/prev with invalid entries
544 Xexpr ""
545 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
546 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
547 Xaddexpr ['foo', 'bar', 'baz', 'quux', 'shmoo']
548 call assert_equal(5, g:Xgetlist({'size' : 0}).size)
549 Xlast
550 call assert_equal(5, g:Xgetlist({'idx' : 0}).idx)
551 Xfirst
552 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
553 2Xnext
554 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100555endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200556
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100557func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200558 call Xtest_browse('c')
559 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100560endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200561
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100562func Test_nomem()
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200563 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100564 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100565
Bram Moolenaarce90e362019-09-08 18:58:44 +0200566 call GetAllocId('qf_dirname_now')->test_alloc_fail(0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100567 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100568
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200569 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100570 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100571
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200572 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100573 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100574
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200575 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100576 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100577
578endfunc
579
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100580func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200581 call s:setup_commands(a:cchar)
582 Xhelpgrep quickfix
583 Xopen
584 if a:cchar == 'c'
585 let title_text = ':helpgrep quickfix'
586 else
587 let title_text = ':lhelpgrep quickfix'
588 endif
589 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200590
591 " Jumping to a help topic should open the help window
592 only
593 Xnext
594 call assert_true(&buftype == 'help')
595 call assert_true(winnr('$') == 2)
596 " Jumping to the next match should reuse the help window
597 Xnext
598 call assert_true(&buftype == 'help')
599 call assert_true(winnr() == 1)
600 call assert_true(winnr('$') == 2)
601 " Jumping to the next match from the quickfix window should reuse the help
602 " window
603 Xopen
604 Xnext
605 call assert_true(&buftype == 'help')
606 call assert_true(winnr() == 1)
607 call assert_true(winnr('$') == 2)
608
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100609 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200610 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100611
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100612 " When the current window is vertically split, jumping to a help match
613 " should open the help window at the top.
614 only | enew
615 let w1 = win_getid()
616 vert new
617 let w2 = win_getid()
618 Xnext
619 let w3 = win_getid()
620 call assert_true(&buftype == 'help')
621 call assert_true(winnr() == 1)
Bram Moolenaarffc4fb82019-11-19 22:38:48 +0100622 " See jump_to_help_window() for details
623 let w2_width = winwidth(w2)
624 if w2_width != &columns && w2_width < 80
625 call assert_equal(['col', [['leaf', w3],
626 \ ['row', [['leaf', w2], ['leaf', w1]]]]], winlayout())
627 else
628 call assert_equal(['row', [['col', [['leaf', w3], ['leaf', w2]]],
629 \ ['leaf', w1]]] , winlayout())
630 endif
Bram Moolenaarf9ae1542019-11-18 22:02:16 +0100631
632 new | only
633 set buftype=help
634 set modified
635 call assert_fails('Xnext', 'E37:')
636 set nomodified
637 new | only
638
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200639 if a:cchar == 'l'
640 " When a help window is present, running :lhelpgrep should reuse the
641 " help window and not the current window
642 new | only
643 call g:Xsetlist([], 'f')
644 help index.txt
645 wincmd w
646 lhelpgrep quickfix
647 call assert_equal(1, winnr())
648 call assert_notequal([], getloclist(1))
649 call assert_equal([], getloclist(2))
650 endif
651
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200652 new | only
653
Bram Moolenaaree85df32017-03-19 14:19:50 +0100654 " Search for non existing help string
655 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar108e7b42018-10-11 17:39:12 +0200656 " Invalid regular expression
657 call assert_fails('Xhelpgrep \@<!', 'E480:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100658endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200659
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100660func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200661 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200662 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200663 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100664endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100665
Bram Moolenaar6920c722016-01-22 22:44:10 +0100666func Test_errortitle()
667 augroup QfBufWinEnter
668 au!
669 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
670 augroup END
671 copen
672 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'')'}]
673 call setqflist(a)
674 call assert_equal(':setqflist()', g:a)
675 augroup QfBufWinEnter
676 au!
677 augroup END
678 augroup! QfBufWinEnter
679endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100680
Bram Moolenaar5584df62016-03-18 21:00:51 +0100681func Test_vimgreptitle()
682 augroup QfBufWinEnter
683 au!
684 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
685 augroup END
686 try
687 vimgrep /pattern/j file
688 catch /E480/
689 endtry
690 copen
691 call assert_equal(': vimgrep /pattern/j file', g:a)
692 augroup QfBufWinEnter
693 au!
694 augroup END
695 augroup! QfBufWinEnter
696endfunc
697
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100698func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200699 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +0100700
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200701 Xgetexpr ['file:1:1:message']
702 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100703 if a:cchar == 'c'
704 call setqflist(l, 'r')
705 else
706 call setloclist(0, l, 'r')
707 endif
708
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200709 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +0100710 if a:cchar == 'c'
711 let title = ':setqflist()'
712 else
713 let title = ':setloclist()'
714 endif
715 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200716 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100717endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100718
719" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100720func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200721 call XqfTitleTests('c')
722 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100723endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100724
725" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100726func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100727 let save_efm = &efm
728 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
729 cgetexpr ['WWWW', 'EEEE', 'CCCC']
730 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
731 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
732 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
733 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
734 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
735 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
736 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
737 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
738 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100739endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100740
741" This will test for problems in quickfix:
742" A. incorrectly copying location lists which caused the location list to show
743" a different name than the file that was actually being displayed.
744" B. not reusing the window for which the location list window is opened but
745" instead creating new windows.
746" C. make sure that the location list window is not reused instead of the
747" window it belongs to.
748"
749" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100750func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100751 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
752 let word = substitute(base, '\v(.*)\..*', '\1', '')
753
754 setl modifiable
755 setl noreadonly
756 setl noswapfile
757 setl bufhidden=delete
758 %del _
759 " For problem 2:
760 " 'buftype' has to be set to reproduce the constant opening of new windows
761 setl buftype=nofile
762
763 call setline(1, word)
764
765 setl nomodified
766 setl nomodifiable
767 setl readonly
768 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100769endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100770
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100771func Test_locationlist()
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100772 enew
773
774 augroup testgroup
775 au!
776 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
777 augroup END
778
779 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
780
781 let qflist = []
782 for word in words
783 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
784 " NOTE: problem 1:
785 " intentionally not setting 'lnum' so that the quickfix entries are not
786 " valid
Bram Moolenaaraad222c2019-09-06 22:46:09 +0200787 eval qflist->setloclist(0, ' ')
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100788 endfor
789
790 " Test A
791 lrewind
792 enew
793 lopen
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200794 4lnext
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100795 vert split
796 wincmd L
797 lopen
798 wincmd p
799 lnext
800 let fileName = expand("%")
801 wincmd p
802 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
803 let fileName = substitute(fileName, '\\', '/', 'g')
804 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
805 call assert_equal("test://bar.txt", fileName)
806 call assert_equal("test://bar.txt", locationListFileName)
807
808 wincmd n | only
809
810 " Test B:
811 lrewind
812 lopen
813 2
814 exe "normal \<CR>"
815 wincmd p
816 3
817 exe "normal \<CR>"
818 wincmd p
819 4
820 exe "normal \<CR>"
821 call assert_equal(2, winnr('$'))
822 wincmd n | only
823
824 " Test C:
825 lrewind
826 lopen
827 " Let's move the location list window to the top to check whether it (the
828 " first window found) will be reused when we try to open new windows:
829 wincmd K
830 2
831 exe "normal \<CR>"
832 wincmd p
833 3
834 exe "normal \<CR>"
835 wincmd p
836 4
837 exe "normal \<CR>"
838 1wincmd w
839 call assert_equal('quickfix', &buftype)
840 2wincmd w
841 let bufferName = expand("%")
842 let bufferName = substitute(bufferName, '\\', '/', 'g')
843 call assert_equal('test://quux.txt', bufferName)
844
845 wincmd n | only
846
847 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100848endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +0100849
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100850func Test_locationlist_curwin_was_closed()
Bram Moolenaar0899d692016-03-19 13:35:03 +0100851 augroup testgroup
852 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200853 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
Bram Moolenaar0899d692016-03-19 13:35:03 +0100854 augroup END
855
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100856 func! R(n)
Bram Moolenaar0899d692016-03-19 13:35:03 +0100857 quit
858 endfunc
859
860 new
861 let q = []
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200862 call add(q, {'filename': 'test_curwin.txt' })
Bram Moolenaar0899d692016-03-19 13:35:03 +0100863 call setloclist(0, q)
864 call assert_fails('lrewind', 'E924:')
865
866 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100867endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100868
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100869func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200870 call writefile(['loclistfoo'], 'loclistfoo')
871 call writefile(['loclistbar'], 'loclistbar')
872 set switchbuf=usetab
873
874 edit loclistfoo
875 tabedit loclistbar
876 silent lgrep loclistfoo loclist*
877 call assert_equal(1, tabpagenr())
878
879 enew | only | tabonly
880 set switchbuf&vim
881 call delete('loclistfoo')
882 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100883endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200884
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100885" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100886func Test_efm1()
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100887 if !has('unix')
888 " The 'errorformat' setting is different on non-Unix systems.
889 " This test works only on Unix-like systems.
890 return
891 endif
892
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200893 let l =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200894 "Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.
895 "Xtestfile", line 6 col 19; this is an error
896 gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c
897 Xtestfile:9: parse error before `asd'
898 make: *** [vim] Error 1
899 in file "Xtestfile" linenr 10: there is an error
900
901 2 returned
902 "Xtestfile", line 11 col 1; this is an error
903 "Xtestfile", line 12 col 2; this is another error
904 "Xtestfile", line 14:10; this is an error in column 10
905 =Xtestfile=, line 15:10; this is another error, but in vcol 10 this time
906 "Xtestfile", linenr 16: yet another problem
907 Error in "Xtestfile" at line 17:
908 x should be a dot
909 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
910 ^
911 Error in "Xtestfile" at line 18:
912 x should be a dot
913 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
914 .............^
915 Error in "Xtestfile" at line 19:
916 x should be a dot
917 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
918 --------------^
919 Error in "Xtestfile" at line 20:
920 x should be a dot
921 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
922 ^
923
924 Does anyone know what is the problem and how to correction it?
925 "Xtestfile", line 21 col 9: What is the title of the quickfix window?
926 "Xtestfile", line 22 col 9: What is the title of the quickfix window?
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200927 [DATA]
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100928
929 call writefile(l, 'Xerrorfile1')
930 call writefile(l[:-2], 'Xerrorfile2')
931
Bram Moolenaare7eb9272019-06-24 00:58:07 +0200932 let m =<< [DATA]
933 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2
934 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3
935 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4
936 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5
937 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6
938 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7
939 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8
940 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9
941 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10
942 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11
943 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12
944 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13
945 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14
946 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15
947 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16
948 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17
949 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18
950 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19
951 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20
952 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21
953 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22
954[DATA]
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100955 call writefile(m, 'Xtestfile')
956
957 let save_efm = &efm
958 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
959 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
960
961 exe 'cf Xerrorfile2'
962 clast
963 copen
964 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
965 wincmd p
966
967 exe 'cf Xerrorfile1'
968 call assert_equal([4, 12], [line('.'), col('.')])
969 cn
970 call assert_equal([6, 19], [line('.'), col('.')])
971 cn
972 call assert_equal([9, 2], [line('.'), col('.')])
973 cn
974 call assert_equal([10, 2], [line('.'), col('.')])
975 cn
976 call assert_equal([11, 1], [line('.'), col('.')])
977 cn
978 call assert_equal([12, 2], [line('.'), col('.')])
979 cn
980 call assert_equal([14, 10], [line('.'), col('.')])
981 cn
982 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
983 cn
984 call assert_equal([16, 2], [line('.'), col('.')])
985 cn
986 call assert_equal([17, 6], [line('.'), col('.')])
987 cn
988 call assert_equal([18, 7], [line('.'), col('.')])
989 cn
990 call assert_equal([19, 8], [line('.'), col('.')])
991 cn
992 call assert_equal([20, 9], [line('.'), col('.')])
993 clast
994 cprev
995 cprev
996 wincmd w
997 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
998 wincmd p
999
1000 let &efm = save_efm
1001 call delete('Xerrorfile1')
1002 call delete('Xerrorfile2')
1003 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +01001004endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001005
Bram Moolenaarab47c612016-06-14 22:02:26 +02001006" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001007func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +02001008 call s:setup_commands(a:cchar)
1009
Bram Moolenaarab47c612016-06-14 22:02:26 +02001010 let save_efm=&efm
1011 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1012
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001013 let lines = ["Entering dir 'dir1/a'",
1014 \ 'habits2.txt:1:Nine Healthy Habits',
1015 \ "Entering dir 'b'",
1016 \ 'habits3.txt:2:0 Hours of television',
1017 \ 'habits2.txt:7:5 Small meals',
1018 \ "Entering dir 'dir1/c'",
1019 \ 'habits4.txt:3:1 Hour of exercise',
1020 \ "Leaving dir 'dir1/c'",
1021 \ "Leaving dir 'dir1/a'",
1022 \ 'habits1.txt:4:2 Liters of water',
1023 \ "Entering dir 'dir2'",
1024 \ 'habits5.txt:5:3 Cups of hot green tea',
1025 \ "Leaving dir 'dir2'"
1026 \]
Bram Moolenaarab47c612016-06-14 22:02:26 +02001027
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001028 Xexpr ""
1029 for l in lines
1030 Xaddexpr l
1031 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +02001032
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001033 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001034
1035 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
1036 call assert_equal(1, qf[1].lnum)
1037 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
1038 call assert_equal(2, qf[3].lnum)
1039 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
1040 call assert_equal(7, qf[4].lnum)
1041 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
1042 call assert_equal(3, qf[6].lnum)
1043 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
1044 call assert_equal(4, qf[9].lnum)
1045 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
1046 call assert_equal(5, qf[11].lnum)
1047
1048 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001049endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001050
1051" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001052func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +02001053 " Create the directory stack and files
1054 call mkdir('dir1')
1055 call mkdir('dir1/a')
1056 call mkdir('dir1/a/b')
1057 call mkdir('dir1/c')
1058 call mkdir('dir2')
1059
1060 let lines = ["Nine Healthy Habits",
1061 \ "0 Hours of television",
1062 \ "1 Hour of exercise",
1063 \ "2 Liters of water",
1064 \ "3 Cups of hot green tea",
1065 \ "4 Short mental breaks",
1066 \ "5 Small meals",
1067 \ "6 AM wake up time",
1068 \ "7 Minutes of laughter",
1069 \ "8 Hours of sleep (at least)",
1070 \ "9 PM end of the day and off to bed"
1071 \ ]
1072 call writefile(lines, 'habits1.txt')
1073 call writefile(lines, 'dir1/a/habits2.txt')
1074 call writefile(lines, 'dir1/a/b/habits3.txt')
1075 call writefile(lines, 'dir1/c/habits4.txt')
1076 call writefile(lines, 'dir2/habits5.txt')
1077
1078 call s:dir_stack_tests('c')
1079 call s:dir_stack_tests('l')
1080
1081 call delete('dir1', 'rf')
1082 call delete('dir2', 'rf')
1083 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001084endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001085
Bram Moolenaar9b457942016-10-09 16:10:05 +02001086" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001087func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001088 call s:setup_commands(a:cchar)
1089
1090 let save_efm = &efm
1091
1092 let &efm =
1093 \ '%Eerror %m %l,' .
1094 \ '%-Wignored %m %l,' .
1095 \ '%+Cmore ignored %m %l,' .
1096 \ '%Zignored end'
1097 Xgetexpr ['ignored warning 1', 'more ignored continuation 2', 'ignored end', 'error resync 4']
1098 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
1099 call assert_equal([['resync', 1, 4, 'E']], l)
1100
1101 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001102endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001103
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001104func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +02001105 call Xefm_ignore_continuations('c')
1106 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001107endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001108
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001109" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001110func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001111 call s:setup_commands(a:cchar)
1112
Bram Moolenaar049cba92016-06-26 14:38:04 +02001113 let save_efm = &efm
1114
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001115 set efm=%f:%l:%m,%f:%f:%l:%m
1116 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
1117
1118 set efm=%f:%l:%m,%f:%l:%r:%m
1119 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1120
1121 set efm=%f:%l:%m,%O:%f:%l:%m
1122 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1123
1124 set efm=%f:%l:%m,%f:%l:%*[^a-z
1125 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
1126
1127 set efm=%f:%l:%m,%f:%l:%*c
1128 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
1129
1130 set efm=%f:%l:%m,%L%M%N
1131 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
1132
1133 set efm=%f:%l:%m,%f:%l:%m:%R
1134 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
1135
1136 set efm=
1137 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
1138
1139 set efm=%DEntering\ dir\ abc,%f:%l:%m
1140 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
1141
1142 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001143endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001144
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001145func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001146 call Xinvalid_efm_Tests('c')
1147 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001148endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001149
1150" TODO:
1151" Add tests for the following formats in 'errorformat'
1152" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001153func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001154 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001155
1156 " Test for %s format in efm
1157 set efm=%f:%s
1158 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001159 let l = getqflist()
1160 call assert_equal(l[0].pattern, '^\VLine search text\$')
1161 call assert_equal(l[0].lnum, 0)
1162
Bram Moolenaaree85df32017-03-19 14:19:50 +01001163 let l = split(execute('clist', ''), "\n")
1164 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1165
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001166 " Test for %P, %Q and %t format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001167 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001168 [Xtestfile1]
1169 (1,17) error: ';' missing
1170 (21,2) warning: variable 'z' not defined
1171 (67,3) error: end of file found before string ended
1172 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001173
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001174 [Xtestfile2]
1175 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001176
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001177 [Xtestfile3]
1178 NEW compiler v1.1
1179 (2,2) warning: variable 'x' not defined
1180 (67,3) warning: 's' already defined
1181 --
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001182 [DATA]
1183
Bram Moolenaaree85df32017-03-19 14:19:50 +01001184 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001185 " To exercise the push/pop file functionality in quickfix, the test files
1186 " need to be created.
1187 call writefile(['Line1'], 'Xtestfile1')
1188 call writefile(['Line2'], 'Xtestfile2')
1189 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001190 cexpr ""
1191 for l in lines
1192 caddexpr l
1193 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001194 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001195 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001196 call assert_equal(21, l[2].lnum)
1197 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001198 call assert_equal('w', l[2].type)
1199 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001200 call delete('Xtestfile1')
1201 call delete('Xtestfile2')
1202 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001203
1204 " Tests for %E, %C and %Z format specifiers
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001205 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001206 Error 275
1207 line 42
1208 column 3
1209 ' ' expected after '--'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001210 [DATA]
1211
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001212 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1213 cgetexpr lines
1214 let l = getqflist()
1215 call assert_equal(275, l[0].nr)
1216 call assert_equal(42, l[0].lnum)
1217 call assert_equal(3, l[0].col)
1218 call assert_equal('E', l[0].type)
1219 call assert_equal("\n' ' expected after '--'", l[0].text)
1220
1221 " Test for %>
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001222 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001223 Error in line 147 of foo.c:
1224 unknown variable 'i'
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001225 [DATA]
1226
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001227 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1228 cgetexpr lines
1229 let l = getqflist()
1230 call assert_equal(147, l[0].lnum)
1231 call assert_equal('E', l[0].type)
1232 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001233
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001234 " Test for %A, %C and other formats
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001235 let lines =<< trim [DATA]
Bram Moolenaare7eb9272019-06-24 00:58:07 +02001236 ==============================================================
1237 FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)
1238 --------------------------------------------------------------
1239 Traceback (most recent call last):
1240 File "unittests/dbfacadeTest.py", line 89, in testFoo
1241 self.assertEquals(34, dtid)
1242 File "/usr/lib/python2.2/unittest.py", line 286, in
1243 failUnlessEqual
1244 raise self.failureException, \\
1245 AssertionError: 34 != 33
1246
1247 --------------------------------------------------------------
1248 Ran 27 tests in 0.063s
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001249 [DATA]
1250
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001251 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
1252 cgetexpr lines
1253 let l = getqflist()
1254 call assert_equal(8, len(l))
1255 call assert_equal(89, l[4].lnum)
1256 call assert_equal(1, l[4].valid)
1257 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
1258
Bram Moolenaard76ce852018-05-01 15:02:04 +02001259 " Test for %o
1260 set efm=%f(%o):%l\ %m
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001261 cgetexpr ['Xotestfile(Language.PureScript.Types):20 Error']
1262 call writefile(['Line1'], 'Xotestfile')
Bram Moolenaard76ce852018-05-01 15:02:04 +02001263 let l = getqflist()
1264 call assert_equal(1, len(l), string(l))
1265 call assert_equal('Language.PureScript.Types', l[0].module)
1266 copen
1267 call assert_equal('Language.PureScript.Types|20| Error', getline(1))
1268 call feedkeys("\<CR>", 'xn')
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001269 call assert_equal('Xotestfile', expand('%:t'))
Bram Moolenaard76ce852018-05-01 15:02:04 +02001270 cclose
1271 bd
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001272 call delete("Xotestfile")
Bram Moolenaard76ce852018-05-01 15:02:04 +02001273
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001274 " The following sequence of commands used to crash Vim
1275 set efm=%W%m
1276 cgetexpr ['msg1']
1277 let l = getqflist()
1278 call assert_equal(1, len(l), string(l))
1279 call assert_equal('msg1', l[0].text)
1280 set efm=%C%m
1281 lexpr 'msg2'
1282 let l = getloclist(0)
1283 call assert_equal(1, len(l), string(l))
1284 call assert_equal('msg2', l[0].text)
1285 lopen
1286 call setqflist([], 'r')
1287 caddbuf
1288 let l = getqflist()
1289 call assert_equal(1, len(l), string(l))
1290 call assert_equal('|| msg2', l[0].text)
1291
Bram Moolenaar78ddc062018-05-15 21:56:34 +02001292 " When matching error lines, case should be ignored. Test for this.
1293 set noignorecase
1294 let l=getqflist({'lines' : ['Xtest:FOO10:Line 20'], 'efm':'%f:foo%l:%m'})
1295 call assert_equal(10, l.items[0].lnum)
1296 call assert_equal('Line 20', l.items[0].text)
1297 set ignorecase&
1298
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001299 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001300 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001301endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001302
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001303func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001304 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001305 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001306 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001307 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001308 colder
1309 cgetexpr []
1310 endfunc
1311 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001312 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001313 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001314 lolder
1315 lgetexpr []
1316 endfunc
1317 endif
1318
1319 augroup testgroup
1320 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001321 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001322 augroup END
1323
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001324 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001325 let words = [ "a", "b" ]
1326 let qflist = []
1327 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001328 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001329 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001330 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001331 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001332
1333 augroup! testgroup
1334endfunc
1335
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001336func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001337 call XquickfixChangedByAutocmd('c')
1338 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001339endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001340
1341func Test_caddbuffer_to_empty()
1342 helpgr quickfix
1343 call setqflist([], 'r')
1344 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001345 try
1346 cn
1347 catch
1348 " number of matches is unknown
1349 call assert_true(v:exception =~ 'E553:')
1350 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001351 quit!
1352endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001353
1354func Test_cgetexpr_works()
1355 " this must not crash Vim
1356 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001357 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001358endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001359
1360" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001361func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001362 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001363
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001364 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001365 \ {'bufnr': a:bnum, 'lnum': 2}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001366 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001367 call assert_equal(2, len(l))
1368 call assert_equal(2, l[1].lnum)
1369
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001370 Xnext
1371 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1372 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001373 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001374 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001375 call assert_equal(3, line('.'))
1376
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001377 " Appending entries to the list should not change the cursor position
1378 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001379 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001380 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001381 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001382 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1383 call assert_equal(1, line('.'))
1384 close
1385
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001386 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001387 \ {'bufnr': a:bnum, 'lnum': 4},
1388 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001389 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001390 call assert_equal(3, len(l))
1391 call assert_equal(5, l[2].lnum)
1392
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001393 call g:Xsetlist([])
1394 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001395 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001396
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001397 " Tests for setting the 'valid' flag
1398 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1399 Xwindow
1400 call assert_equal(1, winnr('$'))
1401 let l = g:Xgetlist()
1402 call g:Xsetlist(l)
1403 call assert_equal(0, g:Xgetlist()[0].valid)
Bram Moolenaar9752c722018-12-22 16:49:34 +01001404 " Adding a non-valid entry should not mark the list as having valid entries
1405 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':5, 'valid':0}], 'a')
1406 Xwindow
1407 call assert_equal(1, winnr('$'))
1408
1409 " :cnext/:cprev should still work even with invalid entries in the list
1410 let l = [{'bufnr' : a:bnum, 'lnum' : 1, 'text' : '1', 'valid' : 0},
1411 \ {'bufnr' : a:bnum, 'lnum' : 2, 'text' : '2', 'valid' : 0}]
1412 call g:Xsetlist(l)
1413 Xnext
1414 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1415 Xprev
1416 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1417 " :cnext/:cprev should still work after appending invalid entries to an
1418 " empty list
1419 call g:Xsetlist([])
1420 call g:Xsetlist(l, 'a')
1421 Xnext
1422 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1423 Xprev
1424 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1425
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001426 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1427 Xwindow
1428 call assert_equal(2, winnr('$'))
1429 Xclose
1430 let save_efm = &efm
1431 set efm=%m
1432 Xgetexpr 'TestMessage'
1433 let l = g:Xgetlist()
1434 call g:Xsetlist(l)
1435 call assert_equal(1, g:Xgetlist()[0].valid)
1436 let &efm = save_efm
1437
Bram Moolenaaree85df32017-03-19 14:19:50 +01001438 " Error cases:
1439 " Refer to a non-existing buffer and pass a non-dictionary type
1440 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1441 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1442 call g:Xsetlist([[1, 2,3]])
1443 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001444endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001445
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001446func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001447 new Xtestfile | only
1448 let bnum = bufnr('%')
1449 call setline(1, range(1,5))
1450
1451 call SetXlistTests('c', bnum)
1452 call SetXlistTests('l', bnum)
1453
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001454 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001455 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001456endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001457
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001458func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001459 call s:setup_commands(a:cchar)
1460
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001461 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001462 let @/ = 'Test_'
1463 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001464 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001465 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001466 Xvimgrep empty test_quickfix.vim
1467 call assert_true(len(g:Xgetlist()) > 0)
1468 Xvimgrep matches test_quickfix.vim
1469 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001470 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001471 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001472 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001473 call g:Xsetlist([], 'r')
1474 call assert_true(len(g:Xgetlist()) == 0)
1475 Xolder
1476 call assert_equal(testlen, len(g:Xgetlist()))
1477 Xnewer
1478 Xnewer
1479 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001480endfunc
1481
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001482func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001483 call Xlist_empty_middle('c')
1484 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001485endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001486
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001487func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001488 call s:setup_commands(a:cchar)
1489
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001490 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001491 Xvimgrep one test_quickfix.vim
1492 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001493 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001494 Xvimgrep two test_quickfix.vim
1495 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001496 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001497 Xvimgrep three test_quickfix.vim
1498 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001499 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001500 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001501 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001502 call g:Xsetlist([], 'r')
1503 call assert_true(len(g:Xgetlist()) == 0)
1504 Xnewer
1505 call assert_equal(twolen, len(g:Xgetlist()))
1506 Xnewer
1507 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001508endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001509
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001510func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001511 call Xlist_empty_older('c')
1512 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001513endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001514
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001515func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001516 call s:setup_commands(a:cchar)
1517
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001518 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1519 \ {'filename': 'fnameB', 'text': 'B'}]
1520 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1521 \ {'filename': 'fnameD', 'text': 'D'},
1522 \ {'filename': 'fnameE', 'text': 'E'}]
1523
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001524 " {action} is unspecified. Same as specifying ' '.
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001525 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001526 silent! Xnewer 99
1527 call g:Xsetlist(list1)
1528 call g:Xsetlist(list2)
1529 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001530 call assert_equal(3, len(li))
1531 call assert_equal('C', li[0]['text'])
1532 call assert_equal('D', li[1]['text'])
1533 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001534 silent! Xolder
1535 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001536 call assert_equal(2, len(li))
1537 call assert_equal('A', li[0]['text'])
1538 call assert_equal('B', li[1]['text'])
1539
1540 " {action} is specified ' '.
1541 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001542 silent! Xnewer 99
1543 call g:Xsetlist(list1)
1544 call g:Xsetlist(list2, ' ')
1545 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001546 call assert_equal(3, len(li))
1547 call assert_equal('C', li[0]['text'])
1548 call assert_equal('D', li[1]['text'])
1549 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001550 silent! Xolder
1551 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001552 call assert_equal(2, len(li))
1553 call assert_equal('A', li[0]['text'])
1554 call assert_equal('B', li[1]['text'])
1555
1556 " {action} is specified 'a'.
1557 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001558 silent! Xnewer 99
1559 call g:Xsetlist(list1)
1560 call g:Xsetlist(list2, 'a')
1561 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001562 call assert_equal(5, len(li))
1563 call assert_equal('A', li[0]['text'])
1564 call assert_equal('B', li[1]['text'])
1565 call assert_equal('C', li[2]['text'])
1566 call assert_equal('D', li[3]['text'])
1567 call assert_equal('E', li[4]['text'])
1568
1569 " {action} is specified 'r'.
1570 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001571 silent! Xnewer 99
1572 call g:Xsetlist(list1)
1573 call g:Xsetlist(list2, 'r')
1574 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001575 call assert_equal(3, len(li))
1576 call assert_equal('C', li[0]['text'])
1577 call assert_equal('D', li[1]['text'])
1578 call assert_equal('E', li[2]['text'])
1579
1580 " Test for wrong value.
1581 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001582 call assert_fails("call g:Xsetlist(0)", 'E714:')
1583 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
1584 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
1585 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
1586 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001587endfunc
1588
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001589func Test_setqflist_invalid_nr()
1590 " The following command used to crash Vim
Bram Moolenaaraad222c2019-09-06 22:46:09 +02001591 eval []->setqflist(' ', {'nr' : $XXX_DOES_NOT_EXIST})
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001592endfunc
1593
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001594func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001595 call XquickfixSetListWithAct('c')
1596 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001597endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001598
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001599func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001600 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001601
Bram Moolenaar049cba92016-06-26 14:38:04 +02001602 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001603 call assert_equal(1, l[0].lnum)
1604 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001605 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001606 call assert_equal(2, l[1].lnum)
1607 call assert_equal(1, l[1].col)
1608 call assert_equal(4070, len(l[1].text))
1609 call assert_equal(3, l[2].lnum)
1610 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001611 call assert_equal(4070, len(l[2].text))
1612 call assert_equal(4, l[3].lnum)
1613 call assert_equal(1, l[3].col)
1614 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001615
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001616 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001617endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001618
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001619func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001620 call s:setup_commands(a:cchar)
1621
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001622 let testfile = 'samples/quickfix.txt'
1623
1624 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001625 exe 'Xgetfile' testfile
1626 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001627
1628 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001629 Xexpr readfile(testfile)
1630 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001631
1632 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001633 Xexpr join(readfile(testfile), "\n")
1634 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001635
1636 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001637 exe 'edit' testfile
1638 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02001639 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001640endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001641
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001642func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001643 call s:long_lines_tests('c')
1644 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001645endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001646
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001647func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001648 let l = []
1649 for i in range(1, 20)
1650 call add(l, 'Line' . i)
1651 endfor
1652 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001653endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001654
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001655func Test_switchbuf()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001656 call s:create_test_file('Xqftestfile1')
1657 call s:create_test_file('Xqftestfile2')
1658 call s:create_test_file('Xqftestfile3')
1659
1660 new | only
1661 edit Xqftestfile1
1662 let file1_winid = win_getid()
1663 new Xqftestfile2
1664 let file2_winid = win_getid()
1665 cgetexpr ['Xqftestfile1:5:Line5',
1666 \ 'Xqftestfile1:6:Line6',
1667 \ 'Xqftestfile2:10:Line10',
1668 \ 'Xqftestfile2:11:Line11',
1669 \ 'Xqftestfile3:15:Line15',
1670 \ 'Xqftestfile3:16:Line16']
1671
1672 new
1673 let winid = win_getid()
1674 cfirst | cnext
1675 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001676 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001677 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001678 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001679 call assert_equal(winid, win_getid())
Bram Moolenaar049cba92016-06-26 14:38:04 +02001680
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001681 " Test for 'switchbuf' set to search for files in windows in the current
1682 " tabpage and jump to an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001683 set switchbuf=useopen
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001684 enew
Bram Moolenaar049cba92016-06-26 14:38:04 +02001685 cfirst | cnext
1686 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001687 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001688 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001689 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001690 call assert_equal(file2_winid, win_getid())
1691
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001692 " Test for 'switchbuf' set to search for files in tabpages and jump to an
1693 " existing tabpage (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001694 enew | only
1695 set switchbuf=usetab
1696 tabedit Xqftestfile1
1697 tabedit Xqftestfile2
Bram Moolenaar74240d32017-12-10 15:26:15 +01001698 tabedit Xqftestfile3
Bram Moolenaar049cba92016-06-26 14:38:04 +02001699 tabfirst
1700 cfirst | cnext
1701 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001702 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001703 call assert_equal(3, tabpagenr())
Bram Moolenaar74240d32017-12-10 15:26:15 +01001704 6cnext
1705 call assert_equal(4, tabpagenr())
1706 2cpfile
1707 call assert_equal(2, tabpagenr())
1708 2cnfile
1709 call assert_equal(4, tabpagenr())
Bram Moolenaar049cba92016-06-26 14:38:04 +02001710 tabfirst | tabonly | enew
1711
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001712 " Test for 'switchbuf' set to open a new window for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02001713 set switchbuf=split
1714 cfirst | cnext
1715 call assert_equal(1, winnr('$'))
1716 cnext | cnext
1717 call assert_equal(2, winnr('$'))
1718 cnext | cnext
1719 call assert_equal(3, winnr('$'))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001720
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001721 " Test for 'switchbuf' set to open a new tabpage for every file
Bram Moolenaar049cba92016-06-26 14:38:04 +02001722 set switchbuf=newtab
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001723 enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001724 cfirst | cnext
1725 call assert_equal(1, tabpagenr('$'))
1726 cnext | cnext
1727 call assert_equal(2, tabpagenr('$'))
1728 cnext | cnext
1729 call assert_equal(3, tabpagenr('$'))
1730 tabfirst | enew | tabonly | only
1731
Bram Moolenaar539aa6b2019-11-17 18:09:38 +01001732 set switchbuf=uselast
1733 split
1734 let last_winid = win_getid()
1735 copen
1736 exe "normal 1G\<CR>"
1737 call assert_equal(last_winid, win_getid())
1738 enew | only
1739
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001740 " With an empty 'switchbuf', jumping to a quickfix entry should open the
1741 " file in an existing window (if present)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001742 set switchbuf=
1743 edit Xqftestfile1
1744 let file1_winid = win_getid()
1745 new Xqftestfile2
1746 let file2_winid = win_getid()
1747 copen
1748 exe "normal 1G\<CR>"
1749 call assert_equal(file1_winid, win_getid())
1750 copen
1751 exe "normal 3G\<CR>"
1752 call assert_equal(file2_winid, win_getid())
1753 copen | only
1754 exe "normal 5G\<CR>"
1755 call assert_equal(2, winnr('$'))
1756 call assert_equal(1, bufwinnr('Xqftestfile3'))
1757
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001758 " If only quickfix window is open in the current tabpage, jumping to an
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001759 " entry with 'switchbuf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02001760 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001761 set switchbuf=usetab
1762 tabedit Xqftestfile1
1763 tabedit Xqftestfile2
1764 tabedit Xqftestfile3
1765 tabfirst
1766 copen | only
1767 clast
1768 call assert_equal(4, tabpagenr())
1769 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001770
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001771 " Jumping to a file that is not present in any of the tabpages and the
1772 " current tabpage doesn't have any usable windows, should open it in a new
1773 " window in the current tabpage.
1774 copen | only
1775 cfirst
1776 call assert_equal(1, tabpagenr())
1777 call assert_equal('Xqftestfile1', bufname(''))
1778
1779 " If opening a file changes 'switchbuf', then the new value should be
1780 " retained.
1781 call writefile(["vim: switchbuf=split"], 'Xqftestfile1')
1782 enew | only
1783 set switchbuf&vim
1784 cexpr "Xqftestfile1:1:10"
1785 call assert_equal('split', &switchbuf)
1786 call writefile(["vim: switchbuf=usetab"], 'Xqftestfile1')
1787 enew | only
1788 set switchbuf=useopen
1789 cexpr "Xqftestfile1:1:10"
1790 call assert_equal('usetab', &switchbuf)
1791 call writefile(["vim: switchbuf&vim"], 'Xqftestfile1')
1792 enew | only
1793 set switchbuf=useopen
1794 cexpr "Xqftestfile1:1:10"
1795 call assert_equal('', &switchbuf)
1796
Bram Moolenaar049cba92016-06-26 14:38:04 +02001797 call delete('Xqftestfile1')
1798 call delete('Xqftestfile2')
1799 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001800 set switchbuf&vim
1801
1802 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001803endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001804
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001805func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001806 call s:setup_commands(a:cchar)
1807
1808 enew | only
1809
Bram Moolenaarc1542742016-07-20 21:44:37 +02001810 let fname = 'Xqftestfile' . a:cchar
1811 call s:create_test_file(fname)
1812 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02001813
Bram Moolenaarc1542742016-07-20 21:44:37 +02001814 Xgetexpr [fname . ':5:Line5',
1815 \ fname . ':10:Line10',
1816 \ fname . ':15:Line15',
1817 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02001818
1819 6,14delete
1820 call append(6, ['Buffer', 'Window'])
1821
1822 let l = g:Xgetlist()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001823 call assert_equal(5, l[0].lnum)
1824 call assert_equal(6, l[2].lnum)
1825 call assert_equal(13, l[3].lnum)
1826
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001827 " If a file doesn't have any quickfix entries, then deleting lines in the
1828 " file should not update the quickfix list
1829 call g:Xsetlist([], 'f')
1830 1,2delete
1831 call assert_equal([], g:Xgetlist())
1832
Bram Moolenaar049cba92016-06-26 14:38:04 +02001833 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02001834 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001835endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001836
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001837func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02001838 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001839 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02001840 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001841 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001842endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001843
1844" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001845func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001846 call s:setup_commands(a:cchar)
1847
1848 " The following lines are used for the grep test. Don't remove.
1849 " Grep_Test_Text: Match 1
1850 " Grep_Test_Text: Match 2
1851 " GrepAdd_Test_Text: Match 1
1852 " GrepAdd_Test_Text: Match 2
1853 enew! | only
1854 set makeef&vim
1855 silent Xgrep Grep_Test_Text: test_quickfix.vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001856 call assert_true(len(g:Xgetlist()) == 4)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001857 Xopen
1858 call assert_true(w:quickfix_title =~ '^:grep')
1859 Xclose
1860 enew
1861 set makeef=Temp_File_##
1862 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01001863 call assert_true(len(g:Xgetlist()) == 7)
1864
1865 call writefile(['Vim'], 'XtestTempFile')
1866 set makeef=XtestTempFile
1867 silent Xgrep Grep_Test_Text: test_quickfix.vim
1868 call assert_equal(4, len(g:Xgetlist()))
1869 call assert_false(filereadable('XtestTempFile'))
1870 set makeef&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001871endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001872
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001873func Test_grep()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001874 if !has('unix')
1875 " The grepprg may not be set on non-Unix systems
1876 return
1877 endif
1878
1879 call s:test_xgrep('c')
1880 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001881endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001882
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001883func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001884 " Use one 'errorformat' for two windows. Add an expression to each of them,
1885 " make sure they each keep their own state.
1886 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1887 call mkdir('Xone/a', 'p')
1888 call mkdir('Xtwo/a', 'p')
1889 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
1890 call writefile(lines, 'Xone/a/one.txt')
1891 call writefile(lines, 'Xtwo/a/two.txt')
1892
1893 new one
1894 let one_id = win_getid()
1895 lexpr ""
1896 new two
1897 let two_id = win_getid()
1898 lexpr ""
1899
1900 laddexpr "Entering dir 'Xtwo/a'"
1901 call win_gotoid(one_id)
1902 laddexpr "Entering dir 'Xone/a'"
1903 call win_gotoid(two_id)
1904 laddexpr 'two.txt:5:two two two'
1905 call win_gotoid(one_id)
1906 laddexpr 'one.txt:3:one one one'
1907
1908 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001909 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
1910 call assert_equal(3, loc_one[1].lnum)
1911
1912 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001913 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
1914 call assert_equal(5, loc_two[1].lnum)
1915
1916 call win_gotoid(one_id)
1917 bwipe!
1918 call win_gotoid(two_id)
1919 bwipe!
1920 call delete('Xone', 'rf')
1921 call delete('Xtwo', 'rf')
1922endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02001923
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001924func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02001925 call s:setup_commands(a:cchar)
1926
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001927 " Calling lbottom without any errors should fail
1928 if a:cchar == 'l'
1929 call assert_fails('lbottom', 'E776:')
1930 endif
1931
Bram Moolenaar875feea2017-06-11 16:07:51 +02001932 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02001933 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02001934 let wid = win_getid()
1935 call assert_equal(1, line('.'))
1936 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02001937 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02001938 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02001939 call win_gotoid(wid)
1940 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02001941 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02001942endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02001943
1944" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001945func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02001946 call XbottomTests('c')
1947 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001948endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001949
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001950func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001951 call s:setup_commands(a:cchar)
1952
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001953 " clear all lists after the first one, then replace the first one.
1954 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001955 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001956 let entry = {'filename': 'foo', 'lnum': 42}
1957 call g:Xsetlist([entry], 'r')
1958 call g:Xsetlist([entry, entry])
1959 call g:Xsetlist([entry, entry, entry])
1960 let res = split(execute(a:cchar . 'hist'), "\n")
1961 call assert_equal(3, len(res))
1962 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
1963 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
1964 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
1965 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02001966
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02001967 " Test for changing the quickfix lists
1968 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
1969 exe '1' . a:cchar . 'hist'
1970 call assert_equal(1, g:Xgetlist({'nr' : 0}).nr)
1971 exe '3' . a:cchar . 'hist'
1972 call assert_equal(3, g:Xgetlist({'nr' : 0}).nr)
1973 call assert_fails('-2' . a:cchar . 'hist', 'E16:')
1974 call assert_fails('4' . a:cchar . 'hist', 'E16:')
1975
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02001976 call g:Xsetlist([], 'f')
1977 let l = split(execute(a:cchar . 'hist'), "\n")
1978 call assert_equal('No entries', l[0])
Bram Moolenaar8ffc7c82019-05-05 21:00:26 +02001979 if a:cchar == 'c'
1980 call assert_fails('4chist', 'E16:')
1981 else
1982 call assert_fails('4lhist', 'E776:')
1983 endif
Bram Moolenaar5b69c222019-01-11 14:50:06 +01001984
1985 " An empty list should still show the stack history
1986 call g:Xsetlist([])
1987 let res = split(execute(a:cchar . 'hist'), "\n")
1988 call assert_equal('> error list 1 of 1; 0 ' . common, res[0])
1989
1990 call g:Xsetlist([], 'f')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001991endfunc
1992
1993func Test_history()
1994 call HistoryTest('c')
1995 call HistoryTest('l')
1996endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02001997
1998func Test_duplicate_buf()
1999 " make sure we can get the highest buffer number
2000 edit DoesNotExist
2001 edit DoesNotExist2
2002 let last_buffer = bufnr("$")
2003
2004 " make sure only one buffer is created
2005 call writefile(['this one', 'that one'], 'Xgrepthis')
2006 vimgrep one Xgrepthis
2007 vimgrep one Xgrepthis
2008 call assert_equal(last_buffer + 1, bufnr("$"))
2009
2010 call delete('Xgrepthis')
2011endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002012
2013" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002014func Xproperty_tests(cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002015 call s:setup_commands(a:cchar)
2016
2017 " Error cases
2018 call assert_fails('call g:Xgetlist(99)', 'E715:')
2019 call assert_fails('call g:Xsetlist(99)', 'E714:')
2020 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
2021
2022 " Set and get the title
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02002023 call g:Xsetlist([])
Bram Moolenaard823fa92016-08-12 16:29:27 +02002024 Xopen
2025 wincmd p
2026 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002027 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
2028 call assert_equal(0, s)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002029 let d = g:Xgetlist({"title":1})
2030 call assert_equal('Sample', d.title)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02002031 " Try setting title to a non-string value
2032 call assert_equal(-1, g:Xsetlist([], 'a', {'title' : ['Test']}))
2033 call assert_equal('Sample', g:Xgetlist({"title":1}).title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002034
2035 Xopen
2036 call assert_equal('Sample', w:quickfix_title)
2037 Xclose
2038
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02002039 " Tests for action argument
2040 silent! Xolder 999
2041 let qfnr = g:Xgetlist({'all':1}).nr
2042 call g:Xsetlist([], 'r', {'title' : 'N1'})
2043 call assert_equal('N1', g:Xgetlist({'all':1}).title)
2044 call g:Xsetlist([], ' ', {'title' : 'N2'})
2045 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
Bram Moolenaar890680c2016-09-27 21:28:56 +02002046
2047 let res = g:Xgetlist({'nr': 0})
2048 call assert_equal(qfnr + 1, res.nr)
2049 call assert_equal(['nr'], keys(res))
2050
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02002051 call g:Xsetlist([], ' ', {'title' : 'N3'})
2052 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
2053
Bram Moolenaaree85df32017-03-19 14:19:50 +01002054 " Changing the title of an earlier quickfix list
Bram Moolenaar55b69262017-08-13 13:42:01 +02002055 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
Bram Moolenaaree85df32017-03-19 14:19:50 +01002056 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
2057
2058 " Changing the title of an invalid quickfix list
2059 call assert_equal(-1, g:Xsetlist([], ' ',
2060 \ {'title' : 'SomeTitle', 'nr' : 99}))
2061 call assert_equal(-1, g:Xsetlist([], ' ',
2062 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
2063
2064 if a:cchar == 'c'
2065 copen
2066 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
2067 cclose
2068 endif
2069
Bram Moolenaard823fa92016-08-12 16:29:27 +02002070 " Invalid arguments
2071 call assert_fails('call g:Xgetlist([])', 'E715')
2072 call assert_fails('call g:Xsetlist([], "a", [])', 'E715')
2073 let s = g:Xsetlist([], 'a', {'abc':1})
2074 call assert_equal(-1, s)
2075
2076 call assert_equal({}, g:Xgetlist({'abc':1}))
Bram Moolenaara6d48492017-12-12 22:45:31 +01002077 call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
2078 call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02002079
2080 if a:cchar == 'l'
Bram Moolenaar890680c2016-09-27 21:28:56 +02002081 call assert_equal({}, getloclist(99, {'title': 1}))
Bram Moolenaard823fa92016-08-12 16:29:27 +02002082 endif
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002083
2084 " Context related tests
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002085 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
2086 call assert_equal(0, s)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002087 call test_garbagecollect_now()
2088 let d = g:Xgetlist({'context':1})
2089 call assert_equal([1,2,3], d.context)
2090 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
2091 let d = g:Xgetlist({'context':1})
2092 call assert_equal({'color':'green'}, d.context)
2093 call g:Xsetlist([], 'a', {'context':"Context info"})
2094 let d = g:Xgetlist({'context':1})
2095 call assert_equal("Context info", d.context)
2096 call g:Xsetlist([], 'a', {'context':246})
2097 let d = g:Xgetlist({'context':1})
2098 call assert_equal(246, d.context)
2099 if a:cchar == 'l'
2100 " Test for copying context across two different location lists
2101 new | only
2102 let w1_id = win_getid()
2103 let l = [1]
2104 call setloclist(0, [], 'a', {'context':l})
2105 new
2106 let w2_id = win_getid()
2107 call add(l, 2)
2108 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
2109 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2110 unlet! l
2111 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
2112 only
2113 call setloclist(0, [], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01002114 call assert_equal('', getloclist(0, {'context':1}).context)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02002115 endif
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002116
2117 " Test for changing the context of previous quickfix lists
2118 call g:Xsetlist([], 'f')
2119 Xexpr "One"
2120 Xexpr "Two"
2121 Xexpr "Three"
Bram Moolenaar55b69262017-08-13 13:42:01 +02002122 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
2123 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002124 " Also, check for setting the context using quickfix list number zero.
Bram Moolenaar55b69262017-08-13 13:42:01 +02002125 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002126 call test_garbagecollect_now()
2127 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
2128 call assert_equal([1], l.context)
2129 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
2130 call assert_equal([2], l.context)
2131 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
2132 call assert_equal([3], l.context)
2133
2134 " Test for changing the context through reference and for garbage
2135 " collection of quickfix context
2136 let l = ["red"]
2137 call g:Xsetlist([], ' ', {'context' : l})
2138 call add(l, "blue")
2139 let x = g:Xgetlist({'context' : 1})
2140 call add(x.context, "green")
2141 call assert_equal(["red", "blue", "green"], l)
2142 call assert_equal(["red", "blue", "green"], x.context)
2143 unlet l
2144 call test_garbagecollect_now()
2145 let m = g:Xgetlist({'context' : 1})
2146 call assert_equal(["red", "blue", "green"], m.context)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002147
2148 " Test for setting/getting items
2149 Xexpr ""
2150 let qfprev = g:Xgetlist({'nr':0})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002151 let s = g:Xsetlist([], ' ', {'title':'Green',
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002152 \ 'items' : [{'filename':'F1', 'lnum':10}]})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002153 call assert_equal(0, s)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002154 let qfcur = g:Xgetlist({'nr':0})
2155 call assert_true(qfcur.nr == qfprev.nr + 1)
2156 let l = g:Xgetlist({'items':1})
2157 call assert_equal('F1', bufname(l.items[0].bufnr))
2158 call assert_equal(10, l.items[0].lnum)
2159 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
2160 \ {'filename':'F2', 'lnum':30}]})
2161 let l = g:Xgetlist({'items':1})
2162 call assert_equal('F2', bufname(l.items[2].bufnr))
2163 call assert_equal(30, l.items[2].lnum)
2164 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
2165 let l = g:Xgetlist({'items':1})
2166 call assert_equal('F3', bufname(l.items[0].bufnr))
2167 call assert_equal(40, l.items[0].lnum)
2168 call g:Xsetlist([], 'r', {'items' : []})
2169 let l = g:Xgetlist({'items':1})
2170 call assert_equal(0, len(l.items))
2171
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002172 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
2173 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2174 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2175 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
2176
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02002177 " Test for getting id of window associated with a location list window
2178 if a:cchar == 'l'
2179 only
2180 call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid)
2181 let wid = win_getid()
2182 Xopen
2183 call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid)
2184 wincmd w
2185 call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid)
2186 only
2187 endif
2188
Bram Moolenaarae338332017-08-11 20:25:26 +02002189 " The following used to crash Vim with address sanitizer
2190 call g:Xsetlist([], 'f')
2191 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
2192 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
2193
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02002194 " Try setting the items using a string
2195 call assert_equal(-1, g:Xsetlist([], ' ', {'items' : 'Test'}))
2196
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002197 " Save and restore the quickfix stack
2198 call g:Xsetlist([], 'f')
2199 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2200 Xexpr "File1:10:Line1"
2201 Xexpr "File2:20:Line2"
2202 Xexpr "File3:30:Line3"
2203 let last_qf = g:Xgetlist({'nr':'$'}).nr
2204 call assert_equal(3, last_qf)
2205 let qstack = []
2206 for i in range(1, last_qf)
2207 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
2208 endfor
2209 call g:Xsetlist([], 'f')
2210 for i in range(len(qstack))
2211 call g:Xsetlist([], ' ', qstack[i])
2212 endfor
2213 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2214 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
2215 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
2216 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
2217 call g:Xsetlist([], 'f')
2218
2219 " Swap two quickfix lists
2220 Xexpr "File1:10:Line10"
2221 Xexpr "File2:20:Line20"
2222 Xexpr "File3:30:Line30"
2223 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
2224 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
2225 let l1=g:Xgetlist({'nr':1,'all':1})
2226 let l2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002227 let save_id = l1.id
2228 let l1.id=l2.id
2229 let l2.id=save_id
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002230 call g:Xsetlist([], 'r', l1)
2231 call g:Xsetlist([], 'r', l2)
2232 let newl1=g:Xgetlist({'nr':1,'all':1})
2233 let newl2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002234 call assert_equal('Fruits', newl1.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002235 call assert_equal(['Fruits'], newl1.context)
2236 call assert_equal('Line20', newl1.items[0].text)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002237 call assert_equal('Colors', newl2.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002238 call assert_equal(['Colors'], newl2.context)
2239 call assert_equal('Line10', newl2.items[0].text)
2240 call g:Xsetlist([], 'f')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002241endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002242
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002243func Test_qf_property()
Bram Moolenaard823fa92016-08-12 16:29:27 +02002244 call Xproperty_tests('c')
2245 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002246endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002247
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002248" Test for setting the current index in the location/quickfix list
2249func Xtest_setqfidx(cchar)
2250 call s:setup_commands(a:cchar)
2251
2252 Xgetexpr "F1:10:1:Line1\nF2:20:2:Line2\nF3:30:3:Line3"
2253 Xgetexpr "F4:10:1:Line1\nF5:20:2:Line2\nF6:30:3:Line3"
2254 Xgetexpr "F7:10:1:Line1\nF8:20:2:Line2\nF9:30:3:Line3"
2255
2256 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 2})
2257 call g:Xsetlist([], 'a', {'nr' : 2, 'idx' : 2})
2258 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 3})
2259 Xolder 2
2260 Xopen
2261 call assert_equal(3, line('.'))
2262 Xnewer
2263 call assert_equal(2, line('.'))
2264 Xnewer
2265 call assert_equal(2, line('.'))
2266 " Update the current index with the quickfix window open
2267 wincmd w
2268 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 3})
2269 Xopen
2270 call assert_equal(3, line('.'))
2271 Xclose
2272
2273 " Set the current index to the last entry
2274 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : '$'})
2275 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2276 " A large value should set the index to the last index
2277 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 1})
2278 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 999})
2279 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2280 " Invalid index values
2281 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : -1})
2282 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2283 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 0})
2284 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2285 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 'xx'})
2286 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2287 call assert_fails("call g:Xsetlist([], 'a', {'nr':1, 'idx':[]})", 'E745:')
2288
2289 call g:Xsetlist([], 'f')
2290 new | only
2291endfunc
2292
2293func Test_setqfidx()
2294 call Xtest_setqfidx('c')
2295 call Xtest_setqfidx('l')
2296endfunc
2297
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002298" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002299func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002300 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002301endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002302
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002303func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002304 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
2305 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
2306
2307 let g:acmds = []
2308 cexpr "F1:10:Line 10"
2309 caddexpr "F1:20:Line 20"
2310 cgetexpr "F1:30:Line 30"
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002311 cexpr ""
2312 caddexpr ""
2313 cgetexpr ""
2314 silent! cexpr non_existing_func()
2315 silent! caddexpr non_existing_func()
2316 silent! cgetexpr non_existing_func()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002317 let l = ['precexpr',
2318 \ 'postcexpr',
2319 \ 'precaddexpr',
2320 \ 'postcaddexpr',
2321 \ 'precgetexpr',
2322 \ 'postcgetexpr',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002323 \ 'precexpr',
2324 \ 'postcexpr',
2325 \ 'precaddexpr',
2326 \ 'postcaddexpr',
2327 \ 'precgetexpr',
2328 \ 'postcgetexpr',
2329 \ 'precexpr',
2330 \ 'precaddexpr',
2331 \ 'precgetexpr']
2332 call assert_equal(l, g:acmds)
2333
2334 let g:acmds = []
2335 enew! | call append(0, "F2:10:Line 10")
2336 cbuffer!
2337 enew! | call append(0, "F2:20:Line 20")
2338 cgetbuffer
2339 enew! | call append(0, "F2:30:Line 30")
2340 caddbuffer
2341 new
2342 let bnum = bufnr('%')
2343 bunload
2344 exe 'silent! cbuffer! ' . bnum
2345 exe 'silent! cgetbuffer ' . bnum
2346 exe 'silent! caddbuffer ' . bnum
2347 enew!
2348 let l = ['precbuffer',
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002349 \ 'postcbuffer',
2350 \ 'precgetbuffer',
2351 \ 'postcgetbuffer',
2352 \ 'precaddbuffer',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002353 \ 'postcaddbuffer',
2354 \ 'precbuffer',
2355 \ 'precgetbuffer',
2356 \ 'precaddbuffer']
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002357 call assert_equal(l, g:acmds)
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002358
2359 call writefile(['Xtest:1:Line1'], 'Xtest')
2360 call writefile([], 'Xempty')
2361 let g:acmds = []
2362 cfile Xtest
2363 caddfile Xtest
2364 cgetfile Xtest
2365 cfile Xempty
2366 caddfile Xempty
2367 cgetfile Xempty
2368 silent! cfile do_not_exist
2369 silent! caddfile do_not_exist
2370 silent! cgetfile do_not_exist
2371 let l = ['precfile',
2372 \ 'postcfile',
2373 \ 'precaddfile',
2374 \ 'postcaddfile',
2375 \ 'precgetfile',
2376 \ 'postcgetfile',
2377 \ 'precfile',
2378 \ 'postcfile',
2379 \ 'precaddfile',
2380 \ 'postcaddfile',
2381 \ 'precgetfile',
2382 \ 'postcgetfile',
2383 \ 'precfile',
2384 \ 'postcfile',
2385 \ 'precaddfile',
2386 \ 'postcaddfile',
2387 \ 'precgetfile',
2388 \ 'postcgetfile']
2389 call assert_equal(l, g:acmds)
2390
2391 let g:acmds = []
2392 helpgrep quickfix
2393 silent! helpgrep non_existing_help_topic
2394 vimgrep test Xtest
2395 vimgrepadd test Xtest
2396 silent! vimgrep non_existing_test Xtest
2397 silent! vimgrepadd non_existing_test Xtest
2398 set makeprg=
2399 silent! make
2400 set makeprg&
2401 let l = ['prehelpgrep',
2402 \ 'posthelpgrep',
2403 \ 'prehelpgrep',
2404 \ 'posthelpgrep',
2405 \ 'previmgrep',
2406 \ 'postvimgrep',
2407 \ 'previmgrepadd',
2408 \ 'postvimgrepadd',
2409 \ 'previmgrep',
2410 \ 'postvimgrep',
2411 \ 'previmgrepadd',
2412 \ 'postvimgrepadd',
2413 \ 'premake',
2414 \ 'postmake']
2415 call assert_equal(l, g:acmds)
2416
2417 if has('unix')
2418 " Run this test only on Unix-like systems. The grepprg may not be set on
2419 " non-Unix systems.
2420 " The following lines are used for the grep test. Don't remove.
2421 " Grep_Autocmd_Text: Match 1
2422 " GrepAdd_Autocmd_Text: Match 2
2423 let g:acmds = []
2424 silent grep Grep_Autocmd_Text test_quickfix.vim
2425 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2426 silent grep abc123def Xtest
2427 silent grepadd abc123def Xtest
2428 let l = ['pregrep',
2429 \ 'postgrep',
2430 \ 'pregrepadd',
2431 \ 'postgrepadd',
2432 \ 'pregrep',
2433 \ 'postgrep',
2434 \ 'pregrepadd',
2435 \ 'postgrepadd']
2436 call assert_equal(l, g:acmds)
2437 endif
2438
2439 call delete('Xtest')
2440 call delete('Xempty')
Bram Moolenaarb254af32017-12-18 19:48:58 +01002441 au! QuickFixCmdPre
2442 au! QuickFixCmdPost
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002443endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01002444
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002445func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01002446 set efm=%m
2447 lgetexpr '?'
2448
2449 try
2450 call DoesNotExit()
2451 catch
2452 lgetexpr '1'
2453 finally
2454 lgetexpr '1'
2455 endtry
2456
2457 call assert_equal('1', getloclist(0)[0].text)
2458
2459 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002460endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002461
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002462func Test_caddbuffer_wrong()
2463 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002464 let save_efm = &efm
2465 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
2466 cgetexpr ['WWWW', 'EEEE', 'CCCC']
2467 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002468 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002469 bwipe!
2470endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002471
2472func Test_caddexpr_wrong()
2473 " This used to cause a memory access in freed memory.
2474 cbuffer
2475 cbuffer
2476 copen
2477 let save_efm = &efm
2478 set efm=%
2479 call assert_fails('caddexpr ""', 'E376:')
2480 let &efm = save_efm
2481endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01002482
2483func Test_dirstack_cleanup()
2484 " This used to cause a memory access in freed memory.
2485 let save_efm = &efm
2486 lexpr '0'
2487 lopen
2488 fun X(c)
2489 let save_efm=&efm
2490 set efm=%D%f
2491 if a:c == 'c'
2492 caddexpr '::'
2493 else
2494 laddexpr ':0:0'
2495 endif
2496 let &efm=save_efm
2497 endfun
2498 call X('c')
2499 call X('l')
2500 call setqflist([], 'r')
2501 caddbuffer
2502 let &efm = save_efm
2503endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002504
2505" Tests for jumping to entries from the location list window and quickfix
2506" window
2507func Test_cwindow_jump()
2508 set efm=%f%%%l%%%m
2509 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2510 lopen | only
2511 lfirst
2512 call assert_true(winnr('$') == 2)
2513 call assert_true(winnr() == 1)
2514 " Location list for the new window should be set
2515 call assert_true(getloclist(0)[2].text == 'Line 30')
2516
2517 " Open a scratch buffer
2518 " Open a new window and create a location list
2519 " Open the location list window and close the other window
2520 " Jump to an entry.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002521 " Should create a new window and jump to the entry. The scratch buffer
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002522 " should not be used.
2523 enew | only
2524 set buftype=nofile
2525 below new
2526 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2527 lopen
2528 2wincmd c
2529 lnext
2530 call assert_true(winnr('$') == 3)
2531 call assert_true(winnr() == 2)
2532
2533 " Open two windows with two different location lists
2534 " Open the location list window and close the previous window
2535 " Jump to an entry in the location list window
2536 " Should open the file in the first window and not set the location list.
2537 enew | only
2538 lgetexpr ["F1%5%Line 5"]
2539 below new
2540 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2541 lopen
2542 2wincmd c
2543 lnext
2544 call assert_true(winnr() == 1)
2545 call assert_true(getloclist(0)[0].text == 'Line 5')
2546
2547 enew | only
2548 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2549 copen
2550 cnext
2551 call assert_true(winnr('$') == 2)
2552 call assert_true(winnr() == 1)
2553
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002554 " open the quickfix buffer in two windows and jump to an entry. Should open
2555 " the file in the first quickfix window.
2556 enew | only
2557 copen
2558 let bnum = bufnr('')
2559 exe 'sbuffer ' . bnum
2560 wincmd b
2561 cfirst
2562 call assert_equal(2, winnr())
2563 call assert_equal('F1', bufname(''))
2564 enew | only
2565 exe 'sb' bnum
2566 exe 'botright sb' bnum
2567 wincmd t
2568 clast
2569 call assert_equal(2, winnr())
2570 call assert_equal('quickfix', getwinvar(1, '&buftype'))
2571 call assert_equal('quickfix', getwinvar(3, '&buftype'))
2572
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002573 enew | only
2574 set efm&vim
2575endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01002576
Bram Moolenaare00fdf32019-09-15 19:09:42 +02002577func Test_cwindow_highlight()
2578 CheckScreendump
2579
2580 let lines =<< trim END
2581 set t_u7=
2582 call setline(1, ['some', 'text', 'with', 'matches'])
2583 write XCwindow
2584 vimgrep e XCwindow
2585 redraw
2586 cwindow 4
2587 END
2588 call writefile(lines, 'XtestCwindow')
2589 let buf = RunVimInTerminal('-S XtestCwindow', #{rows: 12})
2590 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_1', {})
2591
2592 call term_sendkeys(buf, ":cnext\<CR>")
2593 call VerifyScreenDump(buf, 'Test_quickfix_cwindow_2', {})
2594
2595 " clean up
2596 call StopVimInTerminal(buf)
2597 call delete('XtestCwindow')
2598 call delete('XCwindow')
2599endfunc
2600
Bram Moolenaaree85df32017-03-19 14:19:50 +01002601func XvimgrepTests(cchar)
2602 call s:setup_commands(a:cchar)
2603
2604 call writefile(['Editor:VIM vim',
2605 \ 'Editor:Emacs EmAcS',
2606 \ 'Editor:Notepad NOTEPAD'], 'Xtestfile1')
2607 call writefile(['Linux', 'MacOS', 'MS-Windows'], 'Xtestfile2')
2608
2609 " Error cases
2610 call assert_fails('Xvimgrep /abc *', 'E682:')
2611
2612 let @/=''
2613 call assert_fails('Xvimgrep // *', 'E35:')
2614
2615 call assert_fails('Xvimgrep abc', 'E683:')
2616 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
2617 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
2618
2619 Xexpr ""
2620 Xvimgrepadd Notepad Xtestfile1
2621 Xvimgrepadd MacOS Xtestfile2
2622 let l = g:Xgetlist()
2623 call assert_equal(2, len(l))
2624 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
2625
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01002626 10Xvimgrep #\cvim#g Xtestfile?
Bram Moolenaaree85df32017-03-19 14:19:50 +01002627 let l = g:Xgetlist()
2628 call assert_equal(2, len(l))
2629 call assert_equal(8, l[0].col)
2630 call assert_equal(12, l[1].col)
2631
2632 1Xvimgrep ?Editor? Xtestfile*
2633 let l = g:Xgetlist()
2634 call assert_equal(1, len(l))
2635 call assert_equal('Editor:VIM vim', l[0].text)
2636
2637 edit +3 Xtestfile2
2638 Xvimgrep +\cemacs+j Xtestfile1
2639 let l = g:Xgetlist()
2640 call assert_equal('Xtestfile2', bufname(''))
2641 call assert_equal('Editor:Emacs EmAcS', l[0].text)
2642
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02002643 " Test for unloading a buffer after vimgrep searched the buffer
2644 %bwipe
2645 Xvimgrep /Editor/j Xtestfile*
2646 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
2647 call assert_equal([], getbufinfo('Xtestfile2'))
2648
Bram Moolenaaree85df32017-03-19 14:19:50 +01002649 call delete('Xtestfile1')
2650 call delete('Xtestfile2')
2651endfunc
2652
2653" Tests for the :vimgrep command
2654func Test_vimgrep()
2655 call XvimgrepTests('c')
2656 call XvimgrepTests('l')
2657endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002658
Bram Moolenaar1c299432018-10-28 14:36:09 +01002659" Test for incsearch highlighting of the :vimgrep pattern
2660" This test used to cause "E315: ml_get: invalid lnum" errors.
2661func Test_vimgrep_incsearch()
2662 enew
2663 set incsearch
2664 call test_override("char_avail", 1)
2665
2666 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
2667 let l = getqflist()
2668 call assert_equal(2, len(l))
2669
2670 call test_override("ALL", 0)
2671 set noincsearch
2672endfunc
2673
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002674func XfreeTests(cchar)
2675 call s:setup_commands(a:cchar)
2676
2677 enew | only
2678
2679 " Deleting the quickfix stack should work even When the current list is
2680 " somewhere in the middle of the stack
2681 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2682 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2683 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2684 Xolder
2685 call g:Xsetlist([], 'f')
2686 call assert_equal(0, len(g:Xgetlist()))
2687
2688 " After deleting the stack, adding a new list should create a stack with a
2689 " single list.
2690 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2691 call assert_equal(1, g:Xgetlist({'all':1}).nr)
2692
2693 " Deleting the stack from a quickfix window should update/clear the
2694 " quickfix/location list window.
2695 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2696 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2697 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2698 Xolder
2699 Xwindow
2700 call g:Xsetlist([], 'f')
2701 call assert_equal(2, winnr('$'))
2702 call assert_equal(1, line('$'))
2703 Xclose
2704
2705 " Deleting the stack from a non-quickfix window should update/clear the
2706 " quickfix/location list window.
2707 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2708 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2709 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2710 Xolder
2711 Xwindow
2712 wincmd p
2713 call g:Xsetlist([], 'f')
2714 call assert_equal(0, len(g:Xgetlist()))
2715 wincmd p
2716 call assert_equal(2, winnr('$'))
2717 call assert_equal(1, line('$'))
2718
2719 " After deleting the location list stack, if the location list window is
2720 " opened, then a new location list should be created. So opening the
2721 " location list window again should not create a new window.
2722 if a:cchar == 'l'
2723 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2724 wincmd p
2725 lopen
2726 call assert_equal(2, winnr('$'))
2727 endif
2728 Xclose
2729endfunc
2730
Bram Moolenaar74240d32017-12-10 15:26:15 +01002731" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002732func Test_qf_free()
2733 call XfreeTests('c')
2734 call XfreeTests('l')
2735endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002736
2737" Test for buffer overflow when parsing lines and adding new entries to
2738" the quickfix list.
2739func Test_bufoverflow()
2740 set efm=%f:%l:%m
2741 cgetexpr ['File1:100:' . repeat('x', 1025)]
2742
2743 set efm=%+GCompiler:\ %.%#,%f:%l:%m
2744 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
2745
2746 set efm=%DEntering\ directory\ %f,%f:%l:%m
2747 cgetexpr ['Entering directory ' . repeat('a', 1006),
2748 \ 'File1:10:Hello World']
2749 set efm&vim
2750endfunc
2751
Bram Moolenaar875feea2017-06-11 16:07:51 +02002752" Tests for getting the quickfix stack size
2753func XsizeTests(cchar)
2754 call s:setup_commands(a:cchar)
2755
2756 call g:Xsetlist([], 'f')
2757 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01002758 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
2759 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02002760
2761 Xexpr "File1:10:Line1"
2762 Xexpr "File2:20:Line2"
2763 Xexpr "File3:30:Line3"
2764 Xolder | Xolder
2765 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2766 call g:Xsetlist([], 'f')
2767
2768 Xexpr "File1:10:Line1"
2769 Xexpr "File2:20:Line2"
2770 Xexpr "File3:30:Line3"
2771 Xolder | Xolder
2772 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
2773 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
2774endfunc
2775
2776func Test_Qf_Size()
2777 call XsizeTests('c')
2778 call XsizeTests('l')
2779endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02002780
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002781func Test_cclose_from_copen()
2782 augroup QF_Test
2783 au!
2784 au FileType qf :call assert_fails(':cclose', 'E788')
2785 augroup END
2786 copen
2787 augroup QF_Test
2788 au!
2789 augroup END
2790 augroup! QF_Test
2791endfunc
2792
Bram Moolenaar18141832017-06-25 21:17:25 +02002793func Test_cclose_in_autocmd()
2794 " Problem is only triggered if "starting" is zero, so that the OptionsSet
2795 " event will be triggered.
2796 call test_override('starting', 1)
2797 augroup QF_Test
2798 au!
2799 au FileType qf :call assert_fails(':cclose', 'E788')
2800 augroup END
2801 copen
2802 augroup QF_Test
2803 au!
2804 augroup END
2805 augroup! QF_Test
2806 call test_override('starting', 0)
2807endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002808
Bram Moolenaar379fb762018-08-30 15:58:28 +02002809" Check that ":file" without an argument is possible even when "curbuf_lock"
2810" is set.
2811func Test_file_from_copen()
2812 " Works without argument.
2813 augroup QF_Test
2814 au!
2815 au FileType qf file
2816 augroup END
2817 copen
2818
2819 augroup QF_Test
2820 au!
2821 augroup END
2822 cclose
2823
2824 " Fails with argument.
2825 augroup QF_Test
2826 au!
2827 au FileType qf call assert_fails(':file foo', 'E788')
2828 augroup END
2829 copen
2830 augroup QF_Test
2831 au!
2832 augroup END
2833 cclose
2834
2835 augroup! QF_Test
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02002836endfunc
Bram Moolenaar379fb762018-08-30 15:58:28 +02002837
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002838func Test_resize_from_copen()
2839 augroup QF_Test
2840 au!
2841 au FileType qf resize 5
2842 augroup END
2843 try
2844 " This should succeed without any exception. No other buffers are
2845 " involved in the autocmd.
2846 copen
2847 finally
2848 augroup QF_Test
2849 au!
2850 augroup END
2851 augroup! QF_Test
2852 endtry
2853endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02002854
2855" Tests for the quickfix buffer b:changedtick variable
2856func Xchangedtick_tests(cchar)
2857 call s:setup_commands(a:cchar)
2858
2859 new | only
2860
2861 Xexpr "" | Xexpr "" | Xexpr ""
2862
2863 Xopen
2864 Xolder
2865 Xolder
2866 Xaddexpr "F1:10:Line10"
2867 Xaddexpr "F2:20:Line20"
2868 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
2869 call g:Xsetlist([], 'f')
2870 call assert_equal(8, getbufvar('%', 'changedtick'))
2871 Xclose
2872endfunc
2873
2874func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02002875 call Xchangedtick_tests('c')
2876 call Xchangedtick_tests('l')
2877endfunc
2878
2879" Tests for parsing an expression using setqflist()
2880func Xsetexpr_tests(cchar)
2881 call s:setup_commands(a:cchar)
2882
2883 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002884 call g:Xsetlist([], ' ', {'lines' : t})
2885 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002886
2887 let l = g:Xgetlist()
2888 call assert_equal(3, len(l))
2889 call assert_equal(20, l[1].lnum)
2890 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002891 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002892 let l = g:Xgetlist()
2893 call assert_equal(1, len(l))
2894 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002895 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
2896 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02002897
2898 call g:Xsetlist([], 'f')
2899 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002900 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
2901 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
2902 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
2903 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002904 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
2905 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02002906
2907 " Adding entries using a custom efm
2908 set efm&
2909 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
2910 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
2911 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
2912 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
2913 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
2914 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
2915 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
2916 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02002917endfunc
2918
2919func Test_setexpr()
2920 call Xsetexpr_tests('c')
2921 call Xsetexpr_tests('l')
2922endfunc
2923
2924" Tests for per quickfix/location list directory stack
2925func Xmultidirstack_tests(cchar)
2926 call s:setup_commands(a:cchar)
2927
2928 call g:Xsetlist([], 'f')
2929 Xexpr "" | Xexpr ""
2930
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002931 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
2932 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
2933 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
2934 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002935
2936 let l1 = g:Xgetlist({'nr':1, 'items':1})
2937 let l2 = g:Xgetlist({'nr':2, 'items':1})
2938 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
2939 call assert_equal(3, l1.items[1].lnum)
2940 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
2941 call assert_equal(5, l2.items[1].lnum)
2942endfunc
2943
2944func Test_multidirstack()
2945 call mkdir('Xone/a', 'p')
2946 call mkdir('Xtwo/a', 'p')
2947 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2948 call writefile(lines, 'Xone/a/one.txt')
2949 call writefile(lines, 'Xtwo/a/two.txt')
2950 let save_efm = &efm
2951 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
2952
2953 call Xmultidirstack_tests('c')
2954 call Xmultidirstack_tests('l')
2955
2956 let &efm = save_efm
2957 call delete('Xone', 'rf')
2958 call delete('Xtwo', 'rf')
2959endfunc
2960
2961" Tests for per quickfix/location list file stack
2962func Xmultifilestack_tests(cchar)
2963 call s:setup_commands(a:cchar)
2964
2965 call g:Xsetlist([], 'f')
2966 Xexpr "" | Xexpr ""
2967
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002968 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
2969 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
2970 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
2971 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002972
2973 let l1 = g:Xgetlist({'nr':1, 'items':1})
2974 let l2 = g:Xgetlist({'nr':2, 'items':1})
2975 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
2976 call assert_equal(3, l1.items[1].lnum)
2977 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
2978 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02002979
2980 " Test for start of a new error line in the same line where a previous
2981 " error line ends with a file stack.
2982 let efm_val = 'Error\ l%l\ in\ %f,'
2983 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
2984 let l = g:Xgetlist({'lines' : [
2985 \ '(one.txt',
2986 \ 'Error l4 in one.txt',
2987 \ ') (two.txt',
2988 \ 'Error l6 in two.txt',
2989 \ ')',
2990 \ 'Error l8 in one.txt'
2991 \ ], 'efm' : efm_val})
2992 call assert_equal(3, len(l.items))
2993 call assert_equal('one.txt', bufname(l.items[0].bufnr))
2994 call assert_equal(4, l.items[0].lnum)
2995 call assert_equal('one.txt', l.items[0].text)
2996 call assert_equal('two.txt', bufname(l.items[1].bufnr))
2997 call assert_equal(6, l.items[1].lnum)
2998 call assert_equal('two.txt', l.items[1].text)
2999 call assert_equal('one.txt', bufname(l.items[2].bufnr))
3000 call assert_equal(8, l.items[2].lnum)
3001 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02003002endfunc
3003
3004func Test_multifilestack()
3005 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
3006 call writefile(lines, 'one.txt')
3007 call writefile(lines, 'two.txt')
3008 let save_efm = &efm
3009 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
3010
3011 call Xmultifilestack_tests('c')
3012 call Xmultifilestack_tests('l')
3013
3014 let &efm = save_efm
3015 call delete('one.txt')
3016 call delete('two.txt')
3017endfunc
3018
3019" Tests for per buffer 'efm' setting
3020func Test_perbuf_efm()
3021 call writefile(["File1-10-Line10"], 'one.txt')
3022 call writefile(["File2#20#Line20"], 'two.txt')
3023 set efm=%f#%l#%m
3024 new | only
3025 new
3026 setlocal efm=%f-%l-%m
3027 cfile one.txt
3028 wincmd w
3029 caddfile two.txt
3030
3031 let l = getqflist()
3032 call assert_equal(10, l[0].lnum)
3033 call assert_equal('Line20', l[1].text)
3034
3035 set efm&
3036 new | only
3037 call delete('one.txt')
3038 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02003039endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02003040
3041" Open multiple help windows using ":lhelpgrep
3042" This test used to crash Vim
3043func Test_Multi_LL_Help()
3044 new | only
3045 lhelpgrep window
3046 lopen
3047 e#
3048 lhelpgrep buffer
3049 call assert_equal(3, winnr('$'))
3050 call assert_true(len(getloclist(1)) != 0)
3051 call assert_true(len(getloclist(2)) != 0)
3052 new | only
3053endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02003054
3055" Tests for adding new quickfix lists using setqflist()
3056func XaddQf_tests(cchar)
3057 call s:setup_commands(a:cchar)
3058
3059 " Create a new list using ' ' for action
3060 call g:Xsetlist([], 'f')
3061 call g:Xsetlist([], ' ', {'title' : 'Test1'})
3062 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3063 call assert_equal(1, l.nr)
3064 call assert_equal('Test1', l.title)
3065
3066 " Create a new list using ' ' for action and '$' for 'nr'
3067 call g:Xsetlist([], 'f')
3068 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
3069 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3070 call assert_equal(1, l.nr)
3071 call assert_equal('Test2', l.title)
3072
3073 " Create a new list using 'a' for action
3074 call g:Xsetlist([], 'f')
3075 call g:Xsetlist([], 'a', {'title' : 'Test3'})
3076 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3077 call assert_equal(1, l.nr)
3078 call assert_equal('Test3', l.title)
3079
3080 " Create a new list using 'a' for action and '$' for 'nr'
3081 call g:Xsetlist([], 'f')
3082 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
3083 call g:Xsetlist([], 'a', {'title' : 'Test4'})
3084 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3085 call assert_equal(1, l.nr)
3086 call assert_equal('Test4', l.title)
3087
3088 " Adding a quickfix list should remove all the lists following the current
3089 " list.
3090 Xexpr "" | Xexpr "" | Xexpr ""
3091 silent! 10Xolder
3092 call g:Xsetlist([], ' ', {'title' : 'Test5'})
3093 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3094 call assert_equal(2, l.nr)
3095 call assert_equal('Test5', l.title)
3096
3097 " Add a quickfix list using '$' as the list number.
3098 let lastqf = g:Xgetlist({'nr':'$'}).nr
3099 silent! 99Xolder
3100 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
3101 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3102 call assert_equal(lastqf + 1, l.nr)
3103 call assert_equal('Test6', l.title)
3104
3105 " Add a quickfix list using 'nr' set to one more than the quickfix
3106 " list size.
3107 let lastqf = g:Xgetlist({'nr':'$'}).nr
3108 silent! 99Xolder
3109 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
3110 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3111 call assert_equal(lastqf + 1, l.nr)
3112 call assert_equal('Test7', l.title)
3113
3114 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
3115 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
3116 silent! 99Xolder
3117 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
3118 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
3119 call assert_equal(10, l.nr)
3120 call assert_equal('Test8', l.title)
3121
3122 " Add a quickfix list using 'nr' set to a value greater than 10
3123 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
3124
3125 " Try adding a quickfix list with 'nr' set to a value greater than the
3126 " quickfix list size but less than 10.
3127 call g:Xsetlist([], 'f')
3128 Xexpr "" | Xexpr "" | Xexpr ""
3129 silent! 99Xolder
3130 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
3131
3132 " Add a quickfix list using 'nr' set to a some string or list
3133 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
3134endfunc
3135
3136func Test_add_qf()
3137 call XaddQf_tests('c')
3138 call XaddQf_tests('l')
3139endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003140
3141" Test for getting the quickfix list items from some text without modifying
3142" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003143func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003144 call s:setup_commands(a:cchar)
3145 call g:Xsetlist([], 'f')
3146
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003147 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003148 call assert_equal(2, len(l))
3149 call assert_equal(30, l[1].lnum)
3150
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003151 call assert_equal({}, g:Xgetlist({'lines' : 10}))
3152 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
3153 call assert_equal([], g:Xgetlist({'lines' : []}).items)
3154 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003155
Bram Moolenaar36538222017-09-02 19:51:44 +02003156 " Parse text using a custom efm
3157 set efm&
3158 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
3159 call assert_equal('Line30', l[0].text)
3160 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
3161 call assert_equal('File3:30:Line30', l[0].text)
3162 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
3163 call assert_equal({}, l)
3164 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
3165 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
3166
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003167 " Make sure that the quickfix stack is not modified
3168 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
3169endfunc
3170
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003171func Test_get_list_from_lines()
3172 call XgetListFromLines('c')
3173 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02003174endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003175
3176" Tests for the quickfix list id
3177func Xqfid_tests(cchar)
3178 call s:setup_commands(a:cchar)
3179
3180 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003181 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003182 Xexpr ''
3183 let start_id = g:Xgetlist({'id' : 0}).id
3184 Xexpr '' | Xexpr ''
3185 Xolder
3186 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
3187 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
3188 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003189 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003190 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003191 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3192 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003193
3194 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3195 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003196 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003197 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3198 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3199 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3200
3201 let qfid = g:Xgetlist({'id':0, 'nr':0})
3202 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003203 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003204endfunc
3205
3206func Test_qf_id()
3207 call Xqfid_tests('c')
3208 call Xqfid_tests('l')
3209endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003210
3211func Xqfjump_tests(cchar)
3212 call s:setup_commands(a:cchar)
3213
3214 call writefile(["Line1\tFoo", "Line2"], 'F1')
3215 call writefile(["Line1\tBar", "Line2"], 'F2')
3216 call writefile(["Line1\tBaz", "Line2"], 'F3')
3217
3218 call g:Xsetlist([], 'f')
3219
3220 " Tests for
3221 " Jumping to a line using a pattern
3222 " Jumping to a column greater than the last column in a line
3223 " Jumping to a line greater than the last line in the file
3224 let l = []
3225 for i in range(1, 7)
3226 call add(l, {})
3227 endfor
3228 let l[0].filename='F1'
3229 let l[0].pattern='Line1'
3230 let l[1].filename='F2'
3231 let l[1].pattern='Line1'
3232 let l[2].filename='F3'
3233 let l[2].pattern='Line1'
3234 let l[3].filename='F3'
3235 let l[3].lnum=1
3236 let l[3].col=9
3237 let l[3].vcol=1
3238 let l[4].filename='F3'
3239 let l[4].lnum=99
3240 let l[5].filename='F3'
3241 let l[5].lnum=1
3242 let l[5].col=99
3243 let l[5].vcol=1
3244 let l[6].filename='F3'
3245 let l[6].pattern='abcxyz'
3246
3247 call g:Xsetlist([], ' ', {'items' : l})
3248 Xopen | only
3249 2Xnext
3250 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
3251 call assert_equal('F3', bufname('%'))
3252 Xnext
3253 call assert_equal(7, col('.'))
3254 Xnext
3255 call assert_equal(2, line('.'))
3256 Xnext
3257 call assert_equal(9, col('.'))
3258 2
3259 Xnext
3260 call assert_equal(2, line('.'))
3261
3262 if a:cchar == 'l'
3263 " When jumping to a location list entry in the location list window and
3264 " no usable windows are available, then a new window should be opened.
3265 enew! | new | only
3266 call g:Xsetlist([], 'f')
3267 setlocal buftype=nofile
3268 new
3269 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']})
3270 Xopen
3271 let winid = win_getid()
3272 wincmd p
3273 close
3274 call win_gotoid(winid)
3275 Xnext
3276 call assert_equal(3, winnr('$'))
3277 call assert_equal(1, winnr())
3278 call assert_equal(2, line('.'))
3279
3280 " When jumping to an entry in the location list window and the window
3281 " associated with the location list is not present and a window containing
3282 " the file is already present, then that window should be used.
3283 close
3284 belowright new
3285 call g:Xsetlist([], 'f')
3286 edit F3
3287 call win_gotoid(winid)
3288 Xlast
3289 call assert_equal(3, winnr())
3290 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3291 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3292 endif
3293
3294 " Cleanup
3295 enew!
3296 new | only
3297
3298 call delete('F1')
3299 call delete('F2')
3300 call delete('F3')
3301endfunc
3302
3303func Test_qfjump()
3304 call Xqfjump_tests('c')
3305 call Xqfjump_tests('l')
3306endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01003307
3308" Tests for the getqflist() and getloclist() functions when the list is not
3309" present or is empty
3310func Xgetlist_empty_tests(cchar)
3311 call s:setup_commands(a:cchar)
3312
3313 " Empty quickfix stack
3314 call g:Xsetlist([], 'f')
3315 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3316 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
3317 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3318 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3319 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
3320 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3321 call assert_equal('', g:Xgetlist({'title' : 0}).title)
3322 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003323 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003324 if a:cchar == 'c'
3325 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003326 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003327 \ 'title' : '', 'winid' : 0, 'changedtick': 0},
3328 \ g:Xgetlist({'all' : 0}))
3329 else
3330 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
3331 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003332 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
3333 \ 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003334 \ g:Xgetlist({'all' : 0}))
3335 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003336
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003337 " Quickfix window with empty stack
3338 silent! Xopen
3339 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003340 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003341 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
3342 Xclose
3343
Bram Moolenaara6d48492017-12-12 22:45:31 +01003344 " Empty quickfix list
3345 Xexpr ""
3346 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3347 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
3348 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3349 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3350 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
3351 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3352 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
3353 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003354 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003355
3356 let qfid = g:Xgetlist({'id' : 0}).id
3357 call g:Xsetlist([], 'f')
3358
3359 " Non-existing quickfix identifier
3360 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
3361 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
3362 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
3363 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
3364 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
3365 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
3366 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
3367 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003368 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003369 if a:cchar == 'c'
3370 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3371 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003372 \ 'qfbufnr' : qfbufnr,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003373 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
3374 else
3375 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3376 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003377 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003378 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
3379 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003380
3381 " Non-existing quickfix list number
3382 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
3383 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
3384 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
3385 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
3386 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
3387 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
3388 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
3389 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003390 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003391 if a:cchar == 'c'
3392 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3393 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003394 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr},
3395 \ g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003396 else
3397 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3398 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003399 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003400 \ g:Xgetlist({'nr' : 5, 'all' : 0}))
3401 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003402endfunc
3403
3404func Test_getqflist()
3405 call Xgetlist_empty_tests('c')
3406 call Xgetlist_empty_tests('l')
3407endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01003408
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01003409func Test_getqflist_invalid_nr()
3410 " The following commands used to crash Vim
3411 cexpr ""
3412 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
3413
3414 " Cleanup
3415 call setqflist([], 'r')
3416endfunc
3417
Bram Moolenaarb254af32017-12-18 19:48:58 +01003418" Tests for the quickfix/location list changedtick
3419func Xqftick_tests(cchar)
3420 call s:setup_commands(a:cchar)
3421
3422 call g:Xsetlist([], 'f')
3423
3424 Xexpr "F1:10:Line10"
3425 let qfid = g:Xgetlist({'id' : 0}).id
3426 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3427 Xaddexpr "F2:20:Line20\nF2:21:Line21"
3428 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3429 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
3430 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
3431 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
3432 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
3433 call g:Xsetlist([], 'a', {'title' : 'New Title'})
3434 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
3435
3436 enew!
3437 call append(0, ["F5:50:L50", "F6:60:L60"])
3438 Xaddbuffer
3439 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
3440 enew!
3441
3442 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
3443 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
3444 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3445 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
3446 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
3447 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3448 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
3449 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3450 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3451 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
3452 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3453
3454 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
3455 Xfile Xone
3456 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3457 Xaddfile Xone
3458 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3459
3460 " Test case for updating a non-current quickfix list
3461 call g:Xsetlist([], 'f')
3462 Xexpr "F1:1:L1"
3463 Xexpr "F2:2:L2"
3464 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
3465 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3466 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
3467
3468 call delete("Xone")
3469endfunc
3470
3471func Test_qf_tick()
3472 call Xqftick_tests('c')
3473 call Xqftick_tests('l')
3474endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01003475
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02003476" Test helpgrep with lang specifier
3477func Xtest_helpgrep_with_lang_specifier(cchar)
3478 call s:setup_commands(a:cchar)
3479 Xhelpgrep Vim@en
3480 call assert_equal('help', &filetype)
3481 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
3482 new | only
3483endfunc
3484
3485func Test_helpgrep_with_lang_specifier()
3486 call Xtest_helpgrep_with_lang_specifier('c')
3487 call Xtest_helpgrep_with_lang_specifier('l')
3488endfunc
3489
Bram Moolenaar12237442017-12-19 12:38:52 +01003490" The following test used to crash Vim.
3491" Open the location list window and close the regular window associated with
3492" the location list. When the garbage collection runs now, it incorrectly
3493" marks the location list context as not in use and frees the context.
3494func Test_ll_window_ctx()
3495 call setloclist(0, [], 'f')
3496 call setloclist(0, [], 'a', {'context' : []})
3497 lopen | only
3498 call test_garbagecollect_now()
3499 echo getloclist(0, {'context' : 1}).context
3500 enew | only
3501endfunc
3502
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01003503" The following test used to crash vim
3504func Test_lfile_crash()
3505 sp Xtest
3506 au QuickFixCmdPre * bw
3507 call assert_fails('lfile', 'E40')
3508 au! QuickFixCmdPre
3509endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01003510
3511" The following test used to crash vim
3512func Test_lbuffer_crash()
3513 sv Xtest
3514 augroup QF_Test
3515 au!
3516 au * * bw
3517 augroup END
3518 lbuffer
3519 augroup QF_Test
3520 au!
3521 augroup END
3522endfunc
3523
3524" The following test used to crash vim
3525func Test_lexpr_crash()
3526 augroup QF_Test
3527 au!
3528 au * * call setloclist(0, [], 'f')
3529 augroup END
3530 lexpr ""
3531 augroup QF_Test
3532 au!
3533 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003534
Bram Moolenaar3c097222017-12-21 20:54:49 +01003535 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003536 augroup QF_Test
3537 au!
3538 au BufNew * call setloclist(0, [], 'f')
3539 augroup END
3540 lexpr 'x:1:x'
3541 augroup QF_Test
3542 au!
3543 augroup END
3544
3545 enew | only
3546 lexpr ''
3547 lopen
3548 augroup QF_Test
3549 au!
3550 au FileType * call setloclist(0, [], 'f')
3551 augroup END
3552 lexpr ''
3553 augroup QF_Test
3554 au!
3555 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01003556endfunc
3557
3558" The following test used to crash Vim
3559func Test_lvimgrep_crash()
3560 sv Xtest
3561 augroup QF_Test
3562 au!
3563 au * * call setloclist(0, [], 'f')
3564 augroup END
3565 lvimgrep quickfix test_quickfix.vim
3566 augroup QF_Test
3567 au!
3568 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003569
3570 new | only
3571 augroup QF_Test
3572 au!
3573 au BufEnter * call setloclist(0, [], 'r')
3574 augroup END
3575 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
3576 augroup QF_Test
3577 au!
3578 augroup END
3579
Bram Moolenaar3c097222017-12-21 20:54:49 +01003580 enew | only
3581endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01003582
3583" Test for the position of the quickfix and location list window
3584func Test_qfwin_pos()
3585 " Open two windows
3586 new | only
3587 new
3588 cexpr ['F1:10:L10']
3589 copen
3590 " Quickfix window should be the bottom most window
3591 call assert_equal(3, winnr())
3592 close
3593 " Open at the very top
3594 wincmd t
3595 topleft copen
3596 call assert_equal(1, winnr())
3597 close
3598 " open left of the current window
3599 wincmd t
3600 below new
3601 leftabove copen
3602 call assert_equal(2, winnr())
3603 close
3604 " open right of the current window
3605 rightbelow copen
3606 call assert_equal(3, winnr())
3607 close
3608endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02003609
3610" Tests for quickfix/location lists changed by autocommands when
3611" :vimgrep/:lvimgrep commands are running.
3612func Test_vimgrep_autocmd()
3613 call setqflist([], 'f')
3614 call writefile(['stars'], 'Xtest1.txt')
3615 call writefile(['stars'], 'Xtest2.txt')
3616
3617 " Test 1:
3618 " When searching for a pattern using :vimgrep, if the quickfix list is
3619 " changed by an autocmd, the results should be added to the correct quickfix
3620 " list.
3621 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
3622 silent vimgrep stars Xtest*.txt
3623 call assert_equal(1, getqflist({'nr' : 0}).nr)
3624 call assert_equal(3, getqflist({'nr' : '$'}).nr)
3625 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
3626 au! BufRead Xtest2.txt
3627
3628 " Test 2:
3629 " When searching for a pattern using :vimgrep, if the quickfix list is
3630 " freed, then a error should be given.
3631 silent! %bwipe!
3632 call setqflist([], 'f')
3633 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
3634 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
3635 au! BufRead Xtest2.txt
3636
3637 " Test 3:
3638 " When searching for a pattern using :lvimgrep, if the location list is
3639 " freed, then the command should error out.
3640 silent! %bwipe!
3641 let g:save_winid = win_getid()
3642 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
3643 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
3644 au! BufRead Xtest2.txt
3645
3646 call delete('Xtest1.txt')
3647 call delete('Xtest2.txt')
3648 call setqflist([], 'f')
3649endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003650
Bram Moolenaarf9ae1542019-11-18 22:02:16 +01003651" Test for an autocmd changing the current directory when running vimgrep
3652func Xvimgrep_autocmd_cd(cchar)
3653 call s:setup_commands(a:cchar)
3654
3655 %bwipe
3656 let save_cwd = getcwd()
3657
3658 augroup QF_Test
3659 au!
3660 autocmd BufRead * silent cd %:p:h
3661 augroup END
3662
3663 10Xvimgrep /vim/ Xdir/**
3664 let l = g:Xgetlist()
3665 call assert_equal('f1.txt', bufname(l[0].bufnr))
3666 call assert_equal('f2.txt', fnamemodify(bufname(l[2].bufnr), ':t'))
3667
3668 augroup QF_Test
3669 au!
3670 augroup END
3671
3672 exe 'cd ' . save_cwd
3673endfunc
3674
3675func Test_vimgrep_autocmd_cd()
3676 call mkdir('Xdir/a', 'p')
3677 call mkdir('Xdir/b', 'p')
3678 call writefile(['a_L1_vim', 'a_L2_vim'], 'Xdir/a/f1.txt')
3679 call writefile(['b_L1_vim', 'b_L2_vim'], 'Xdir/b/f2.txt')
3680 call Xvimgrep_autocmd_cd('c')
3681 call Xvimgrep_autocmd_cd('l')
3682 %bwipe
3683 call delete('Xdir', 'rf')
3684endfunc
3685
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003686" The following test used to crash Vim
3687func Test_lhelpgrep_autocmd()
3688 lhelpgrep quickfix
3689 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
3690 lhelpgrep buffer
3691 call assert_equal('help', &filetype)
3692 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
3693 lhelpgrep tabpage
3694 call assert_equal('help', &filetype)
3695 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
3696 au! QuickFixCmdPost
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003697
3698 new | only
3699 augroup QF_Test
3700 au!
3701 au BufEnter * call setqflist([], 'f')
3702 augroup END
3703 call assert_fails('helpgrep quickfix', 'E925:')
3704 augroup QF_Test
3705 au! BufEnter
3706 augroup END
3707
3708 new | only
3709 augroup QF_Test
3710 au!
3711 au BufEnter * call setqflist([], 'r')
3712 augroup END
3713 call assert_fails('helpgrep quickfix', 'E925:')
3714 augroup QF_Test
3715 au! BufEnter
3716 augroup END
3717
3718 new | only
3719 augroup QF_Test
3720 au!
3721 au BufEnter * call setloclist(0, [], 'r')
3722 augroup END
3723 call assert_fails('lhelpgrep quickfix', 'E926:')
3724 augroup QF_Test
3725 au! BufEnter
3726 augroup END
3727
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003728 new | only
3729endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02003730
3731" Test for shortening/simplifying the file name when opening the
3732" quickfix window or when displaying the quickfix list
3733func Test_shorten_fname()
3734 if !has('unix')
3735 return
3736 endif
3737 %bwipe
3738 " Create a quickfix list with a absolute path filename
3739 let fname = getcwd() . '/test_quickfix.vim'
3740 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
3741 call assert_equal(fname, bufname('test_quickfix.vim'))
3742 " Opening the quickfix window should simplify the file path
3743 cwindow
3744 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
3745 cclose
3746 %bwipe
3747 " Create a quickfix list with a absolute path filename
3748 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
3749 call assert_equal(fname, bufname('test_quickfix.vim'))
3750 " Displaying the quickfix list should simplify the file path
3751 silent! clist
3752 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
3753endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02003754
3755" Quickfix title tests
3756" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
3757" Otherwise due to indentation, the title is set with spaces at the beginning
3758" of the command.
3759func Test_qftitle()
3760 call writefile(["F1:1:Line1"], 'Xerr')
3761
3762 " :cexpr
3763 exe "cexpr readfile('Xerr')"
3764 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
3765
3766 " :cgetexpr
3767 exe "cgetexpr readfile('Xerr')"
3768 call assert_equal(":cgetexpr readfile('Xerr')",
3769 \ getqflist({'title' : 1}).title)
3770
3771 " :caddexpr
3772 call setqflist([], 'f')
3773 exe "caddexpr readfile('Xerr')"
3774 call assert_equal(":caddexpr readfile('Xerr')",
3775 \ getqflist({'title' : 1}).title)
3776
3777 " :cbuffer
3778 new Xerr
3779 exe "cbuffer"
3780 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
3781
3782 " :cgetbuffer
3783 edit Xerr
3784 exe "cgetbuffer"
3785 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
3786
3787 " :caddbuffer
3788 call setqflist([], 'f')
3789 edit Xerr
3790 exe "caddbuffer"
3791 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
3792
3793 " :cfile
3794 exe "cfile Xerr"
3795 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
3796
3797 " :cgetfile
3798 exe "cgetfile Xerr"
3799 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
3800
3801 " :caddfile
3802 call setqflist([], 'f')
3803 exe "caddfile Xerr"
3804 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
3805
3806 " :grep
3807 set grepprg=internal
3808 exe "grep F1 Xerr"
3809 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
3810
3811 " :grepadd
3812 call setqflist([], 'f')
3813 exe "grepadd F1 Xerr"
3814 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
3815 set grepprg&vim
3816
3817 " :vimgrep
3818 exe "vimgrep F1 Xerr"
3819 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
3820
3821 " :vimgrepadd
3822 call setqflist([], 'f')
3823 exe "vimgrepadd F1 Xerr"
3824 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
3825
3826 call setqflist(['F1:10:L10'], ' ')
3827 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3828
3829 call setqflist([], 'f')
3830 call setqflist(['F1:10:L10'], 'a')
3831 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3832
3833 call setqflist([], 'f')
3834 call setqflist(['F1:10:L10'], 'r')
3835 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3836
3837 close
3838 call delete('Xerr')
3839
3840 call setqflist([], ' ', {'title' : 'Errors'})
3841 copen
3842 call assert_equal('Errors', w:quickfix_title)
3843 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
3844 call assert_equal('Errors', w:quickfix_title)
3845 cclose
3846endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02003847
3848func Test_lbuffer_with_bwipe()
3849 new
3850 new
3851 augroup nasty
3852 au * * bwipe
3853 augroup END
3854 lbuffer
3855 augroup nasty
3856 au!
3857 augroup END
3858endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02003859
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003860" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
3861" running
3862func Xexpr_acmd_freelist(cchar)
3863 call s:setup_commands(a:cchar)
3864
Bram Moolenaar0366c012018-06-18 20:52:13 +02003865 " This was using freed memory.
3866 augroup nasty
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003867 au * * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02003868 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003869 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02003870 augroup nasty
3871 au!
3872 augroup END
3873endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003874
3875func Test_cexpr_acmd_freelist()
3876 call Xexpr_acmd_freelist('c')
3877 call Xexpr_acmd_freelist('l')
3878endfunc
3879
3880" Test for commands that create a new quickfix/location list and jump to the
3881" first error automatically.
3882func Xjumpto_first_error_test(cchar)
3883 call s:setup_commands(a:cchar)
3884
3885 call s:create_test_file('Xtestfile1')
3886 call s:create_test_file('Xtestfile2')
3887 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
3888
3889 " Test for cexpr/lexpr
3890 enew
3891 Xexpr l
3892 call assert_equal('Xtestfile1', bufname(''))
3893 call assert_equal(2, line('.'))
3894
3895 " Test for cfile/lfile
3896 enew
3897 call writefile(l, 'Xerr')
3898 Xfile Xerr
3899 call assert_equal('Xtestfile1', bufname(''))
3900 call assert_equal(2, line('.'))
3901
3902 " Test for cbuffer/lbuffer
3903 edit Xerr
3904 Xbuffer
3905 call assert_equal('Xtestfile1', bufname(''))
3906 call assert_equal(2, line('.'))
3907
3908 call delete('Xerr')
3909 call delete('Xtestfile1')
3910 call delete('Xtestfile2')
3911endfunc
3912
3913func Test_jumpto_first_error()
3914 call Xjumpto_first_error_test('c')
3915 call Xjumpto_first_error_test('l')
3916endfunc
3917
3918" Test for a quickfix autocmd changing the quickfix/location list before
3919" jumping to the first error in the new list.
3920func Xautocmd_changelist(cchar)
3921 call s:setup_commands(a:cchar)
3922
3923 " Test for cfile/lfile
3924 call s:create_test_file('Xtestfile1')
3925 call s:create_test_file('Xtestfile2')
3926 Xexpr 'Xtestfile1:2:Line2'
3927 autocmd QuickFixCmdPost * Xolder
3928 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
3929 Xfile Xerr
3930 call assert_equal('Xtestfile2', bufname(''))
3931 call assert_equal(4, line('.'))
3932 autocmd! QuickFixCmdPost
3933
3934 " Test for cbuffer/lbuffer
3935 call g:Xsetlist([], 'f')
3936 Xexpr 'Xtestfile1:2:Line2'
3937 autocmd QuickFixCmdPost * Xolder
3938 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
3939 edit Xerr
3940 Xbuffer
3941 call assert_equal('Xtestfile2', bufname(''))
3942 call assert_equal(4, line('.'))
3943 autocmd! QuickFixCmdPost
3944
3945 " Test for cexpr/lexpr
3946 call g:Xsetlist([], 'f')
3947 Xexpr 'Xtestfile1:2:Line2'
3948 autocmd QuickFixCmdPost * Xolder
3949 Xexpr 'Xtestfile2:4:Line4'
3950 call assert_equal('Xtestfile2', bufname(''))
3951 call assert_equal(4, line('.'))
3952 autocmd! QuickFixCmdPost
3953
Bram Moolenaar851332e2018-07-03 19:16:00 +02003954 " The grepprg may not be set on non-Unix systems
3955 if has('unix')
3956 " Test for grep/lgrep
3957 call g:Xsetlist([], 'f')
3958 Xexpr 'Xtestfile1:2:Line2'
3959 autocmd QuickFixCmdPost * Xolder
3960 silent Xgrep Line5 Xtestfile2
3961 call assert_equal('Xtestfile2', bufname(''))
3962 call assert_equal(5, line('.'))
3963 autocmd! QuickFixCmdPost
3964 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003965
3966 " Test for vimgrep/lvimgrep
3967 call g:Xsetlist([], 'f')
3968 Xexpr 'Xtestfile1:2:Line2'
3969 autocmd QuickFixCmdPost * Xolder
3970 silent Xvimgrep Line5 Xtestfile2
3971 call assert_equal('Xtestfile2', bufname(''))
3972 call assert_equal(5, line('.'))
3973 autocmd! QuickFixCmdPost
3974
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003975 " Test for autocommands clearing the quickfix list before jumping to the
3976 " first error. This should not result in an error
3977 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
3978 let v:errmsg = ''
3979 " Test for cfile/lfile
3980 Xfile Xerr
3981 call assert_true(v:errmsg !~# 'E42:')
3982 " Test for cbuffer/lbuffer
3983 edit Xerr
3984 Xbuffer
3985 call assert_true(v:errmsg !~# 'E42:')
3986 " Test for cexpr/lexpr
3987 Xexpr 'Xtestfile2:4:Line4'
3988 call assert_true(v:errmsg !~# 'E42:')
3989 " Test for grep/lgrep
3990 " The grepprg may not be set on non-Unix systems
3991 if has('unix')
3992 silent Xgrep Line5 Xtestfile2
3993 call assert_true(v:errmsg !~# 'E42:')
3994 endif
3995 " Test for vimgrep/lvimgrep
3996 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
3997 autocmd! QuickFixCmdPost
3998
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003999 call delete('Xerr')
4000 call delete('Xtestfile1')
4001 call delete('Xtestfile2')
4002endfunc
4003
4004func Test_autocmd_changelist()
4005 call Xautocmd_changelist('c')
4006 call Xautocmd_changelist('l')
4007endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02004008
4009" Tests for the ':filter /pat/ clist' command
4010func Test_filter_clist()
4011 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
4012 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
4013 \ split(execute('filter /Line 15/ clist'), "\n"))
4014 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
4015 \ split(execute('filter /Xfile1/ clist'), "\n"))
4016 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
4017
4018 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
4019 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
4020 call assert_equal([' 2 pqr:pat2: '],
4021 \ split(execute('filter /pqr/ clist'), "\n"))
4022 call assert_equal([' 1 abc:pat1: '],
4023 \ split(execute('filter /pat1/ clist'), "\n"))
4024endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02004025
4026" Tests for the "CTRL-W <CR>" command.
4027func Xview_result_split_tests(cchar)
4028 call s:setup_commands(a:cchar)
4029
4030 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
4031 call g:Xsetlist([])
4032 Xopen
4033 let l:win_count = winnr('$')
4034 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42')
4035 call assert_equal(l:win_count, winnr('$'))
4036 Xclose
4037endfunc
4038
4039func Test_view_result_split()
4040 call Xview_result_split_tests('c')
4041 call Xview_result_split_tests('l')
4042endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02004043
4044" Test that :cc sets curswant
4045func Test_curswant()
4046 helpgrep quickfix
4047 normal! llll
4048 1cc
4049 call assert_equal(getcurpos()[4], virtcol('.'))
4050 cclose | helpclose
4051endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01004052
4053" Test for opening a file from the quickfix window using CTRL-W <Enter>
4054" doesn't leave an empty buffer around.
4055func Test_splitview()
4056 call s:create_test_file('Xtestfile1')
4057 call s:create_test_file('Xtestfile2')
4058 new | only
4059 let last_bufnr = bufnr('Test_sv_1', 1)
4060 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
4061 cgetexpr l
4062 copen
4063 let numbufs = len(getbufinfo())
4064 exe "normal \<C-W>\<CR>"
4065 copen
4066 exe "normal j\<C-W>\<CR>"
4067 " Make sure new empty buffers are not created
4068 call assert_equal(numbufs, len(getbufinfo()))
4069 " Creating a new buffer should use the next available buffer number
4070 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
4071 bwipe Test_sv_1
4072 bwipe Test_sv_2
4073 new | only
4074
4075 " When split opening files from location list window, make sure that two
4076 " windows doesn't refer to the same location list
4077 lgetexpr l
4078 let locid = getloclist(0, {'id' : 0}).id
4079 lopen
4080 exe "normal \<C-W>\<CR>"
4081 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4082 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4083 new | only
4084
4085 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004086 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01004087 lhelpgrep window
4088 let locid = getloclist(0, {'id' : 0}).id
4089 lwindow
4090 exe "normal j\<C-W>\<CR>"
4091 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
4092 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
4093 new | only
4094
4095 call delete('Xtestfile1')
4096 call delete('Xtestfile2')
4097endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01004098
4099" Test for parsing entries using visual screen column
4100func Test_viscol()
4101 enew
4102 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
4103 edit Xfile1
4104
4105 " Use byte offset for column number
4106 set efm&
4107 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
4108 call assert_equal([5, 8], [col('.'), virtcol('.')])
4109 cnext
4110 call assert_equal([9, 12], [col('.'), virtcol('.')])
4111 cnext
4112 call assert_equal([14, 20], [col('.'), virtcol('.')])
4113
4114 " Use screen column offset for column number
4115 set efm=%f:%l:%v:%m
4116 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
4117 call assert_equal([5, 8], [col('.'), virtcol('.')])
4118 cnext
4119 call assert_equal([9, 12], [col('.'), virtcol('.')])
4120 cnext
4121 call assert_equal([14, 20], [col('.'), virtcol('.')])
4122 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
4123 call assert_equal([5, 8], [col('.'), virtcol('.')])
4124 cnext
4125 call assert_equal([10, 16], [col('.'), virtcol('.')])
4126 cnext
4127 call assert_equal([14, 20], [col('.'), virtcol('.')])
4128
4129 enew
4130 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
4131
4132 " Use byte offset for column number
4133 set efm&
4134 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
4135 call assert_equal([8, 10], [col('.'), virtcol('.')])
4136 cnext
4137 call assert_equal([11, 17], [col('.'), virtcol('.')])
4138 cnext
4139 call assert_equal([16, 25], [col('.'), virtcol('.')])
4140
4141 " Use screen column offset for column number
4142 set efm=%f:%l:%v:%m
4143 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
4144 call assert_equal([8, 10], [col('.'), virtcol('.')])
4145 cnext
4146 call assert_equal([11, 17], [col('.'), virtcol('.')])
4147 cnext
4148 call assert_equal([16, 25], [col('.'), virtcol('.')])
4149
4150 enew | only
4151 set efm&
4152 call delete('Xfile1')
4153endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004154
4155" Test for the quickfix window buffer
4156func Xqfbuf_test(cchar)
4157 call s:setup_commands(a:cchar)
4158
4159 " Quickfix buffer should be reused across closing and opening a quickfix
4160 " window
4161 Xexpr "F1:10:Line10"
4162 Xopen
4163 let qfbnum = bufnr('')
4164 Xclose
4165 " Even after the quickfix window is closed, the buffer should be loaded
4166 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004167 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004168 Xopen
4169 " Buffer should be reused when opening the window again
4170 call assert_equal(qfbnum, bufnr(''))
4171 Xclose
4172
4173 if a:cchar == 'l'
4174 %bwipe
4175 " For a location list, when both the file window and the location list
4176 " window for the list are closed, then the buffer should be freed.
4177 new | only
4178 lexpr "F1:10:Line10"
4179 let wid = win_getid()
4180 lopen
4181 let qfbnum = bufnr('')
4182 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
4183 close
4184 " When the location list window is closed, the buffer name should not
4185 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004186 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004187 call assert_true(bufloaded(qfbnum))
4188
Bram Moolenaard82a81c2019-03-02 07:57:18 +01004189 " After deleting a location list buffer using ":bdelete", opening the
4190 " location list window should mark the buffer as a location list buffer.
4191 exe "bdelete " . qfbnum
4192 lopen
4193 call assert_equal("quickfix", &buftype)
4194 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
4195 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
4196 call assert_false(&swapfile)
4197 lclose
4198
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004199 " When the location list is cleared for the window, the buffer should be
4200 " removed
4201 call setloclist(0, [], 'f')
4202 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01004203 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004204
4205 " When the location list is freed with the location list window open, the
4206 " location list buffer should not be lost. It should be reused when the
4207 " location list is again populated.
4208 lexpr "F1:10:Line10"
4209 lopen
4210 let wid = win_getid()
4211 let qfbnum = bufnr('')
4212 wincmd p
4213 call setloclist(0, [], 'f')
4214 lexpr "F1:10:Line10"
4215 lopen
4216 call assert_equal(wid, win_getid())
4217 call assert_equal(qfbnum, bufnr(''))
4218 lclose
4219
4220 " When the window with the location list is closed, the buffer should be
4221 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004222 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004223 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01004224 endif
4225endfunc
4226
4227func Test_qfbuf()
4228 call Xqfbuf_test('c')
4229 call Xqfbuf_test('l')
4230endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004231
4232" If there is an autocmd to use only one window, then opening the location
4233" list window used to crash Vim.
4234func Test_winonly_autocmd()
4235 call s:create_test_file('Xtest1')
4236 " Autocmd to show only one Vim window at a time
4237 autocmd WinEnter * only
4238 new
4239 " Load the location list
4240 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
4241 let loclistid = getloclist(0, {'id' : 0}).id
4242 " Open the location list window. Only this window will be shown and the file
4243 " window is closed.
4244 lopen
4245 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4246 " Jump to an entry in the location list and make sure that the cursor is
4247 " positioned correctly.
4248 ll 3
4249 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4250 call assert_equal('Xtest1', bufname(''))
4251 call assert_equal(15, line('.'))
4252 " Cleanup
4253 autocmd! WinEnter
4254 new | only
4255 call delete('Xtest1')
4256endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02004257
4258" Test to make sure that an empty quickfix buffer is not reused for loading
4259" a normal buffer.
4260func Test_empty_qfbuf()
4261 enew | only
4262 call writefile(["Test"], 'Xfile1')
4263 call setqflist([], 'f')
4264 copen | only
4265 let qfbuf = bufnr('')
4266 edit Xfile1
4267 call assert_notequal(qfbuf, bufnr(''))
4268 enew
4269 call delete('Xfile1')
4270endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004271
4272" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004273" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004274func Xtest_below(cchar)
4275 call s:setup_commands(a:cchar)
4276
4277 " No quickfix/location list
4278 call assert_fails('Xbelow', 'E42:')
4279 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004280 call assert_fails('Xbefore', 'E42:')
4281 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004282
4283 " Empty quickfix/location list
4284 call g:Xsetlist([])
4285 call assert_fails('Xbelow', 'E42:')
4286 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004287 call assert_fails('Xbefore', 'E42:')
4288 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004289
4290 call s:create_test_file('X1')
4291 call s:create_test_file('X2')
4292 call s:create_test_file('X3')
4293 call s:create_test_file('X4')
4294
4295 " Invalid entries
4296 edit X1
4297 call g:Xsetlist(["E1", "E2"])
4298 call assert_fails('Xbelow', 'E42:')
4299 call assert_fails('Xabove', 'E42:')
4300 call assert_fails('3Xbelow', 'E42:')
4301 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004302 call assert_fails('Xbefore', 'E42:')
4303 call assert_fails('Xafter', 'E42:')
4304 call assert_fails('3Xbefore', 'E42:')
4305 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004306
4307 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004308 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 +02004309 edit +7 X2
4310 Xabove
4311 call assert_equal(['X2', 5], [bufname(''), line('.')])
4312 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004313 normal 7G
4314 Xbefore
4315 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4316 call assert_fails('Xbefore', 'E553:')
4317
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004318 normal 2j
4319 Xbelow
4320 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004321 normal 7G
4322 Xafter
4323 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4324
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004325 " Last error in this file
4326 Xbelow 99
4327 call assert_equal(['X2', 15], [bufname(''), line('.')])
4328 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004329 normal gg
4330 Xafter 99
4331 call assert_equal(['X2', 15, 4], [bufname(''), line('.'), col('.')])
4332 call assert_fails('Xafter', 'E553:')
4333
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004334 " First error in this file
4335 Xabove 99
4336 call assert_equal(['X2', 5], [bufname(''), line('.')])
4337 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004338 normal G
4339 Xbefore 99
4340 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4341 call assert_fails('Xbefore', 'E553:')
4342
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004343 normal gg
4344 Xbelow 2
4345 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004346 normal gg
4347 Xafter 2
4348 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4349
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004350 normal G
4351 Xabove 2
4352 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004353 normal G
4354 Xbefore 2
4355 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4356
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004357 edit X4
4358 call assert_fails('Xabove', 'E42:')
4359 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004360 call assert_fails('Xbefore', 'E42:')
4361 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004362 if a:cchar == 'l'
4363 " If a buffer has location list entries from some other window but not
4364 " from the current window, then the commands should fail.
4365 edit X1 | split | call setloclist(0, [], 'f')
4366 call assert_fails('Xabove', 'E776:')
4367 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004368 call assert_fails('Xbefore', 'E776:')
4369 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004370 close
4371 endif
4372
4373 " Test for lines with multiple quickfix entries
4374 Xexpr ["X1:5:L5", "X2:5:1:L5_1", "X2:5:2:L5_2", "X2:5:3:L5_3",
4375 \ "X2:10:1:L10_1", "X2:10:2:L10_2", "X2:10:3:L10_3",
4376 \ "X2:15:1:L15_1", "X2:15:2:L15_2", "X2:15:3:L15_3", "X3:3:L3"]
4377 edit +1 X2
4378 Xbelow 2
4379 call assert_equal(['X2', 10, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004380 normal 1G
4381 Xafter 2
4382 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4383
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004384 normal gg
4385 Xbelow 99
4386 call assert_equal(['X2', 15, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004387 normal gg
4388 Xafter 99
4389 call assert_equal(['X2', 15, 3], [bufname(''), line('.'), col('.')])
4390
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004391 normal G
4392 Xabove 2
4393 call assert_equal(['X2', 10, 1], [bufname(''), line('.'), col('.')])
4394 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004395 Xbefore 2
4396 call assert_equal(['X2', 15, 2], [bufname(''), line('.'), col('.')])
4397
4398 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004399 Xabove 99
4400 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004401 normal G
4402 Xbefore 99
4403 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
4404
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004405 normal 10G
4406 Xabove
4407 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004408 normal 10G$
4409 2Xbefore
4410 call assert_equal(['X2', 10, 2], [bufname(''), line('.'), col('.')])
4411
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004412 normal 10G
4413 Xbelow
4414 call assert_equal(['X2', 15, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004415 normal 9G
4416 5Xafter
4417 call assert_equal(['X2', 15, 2], [bufname(''), line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004418
4419 " Invalid range
4420 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02004421 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004422 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004423 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02004424 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004425 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004426 endif
4427
4428 call delete('X1')
4429 call delete('X2')
4430 call delete('X3')
4431 call delete('X4')
4432endfunc
4433
4434func Test_cbelow()
4435 call Xtest_below('c')
4436 call Xtest_below('l')
4437endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02004438
4439func Test_quickfix_count()
4440 let commands = [
4441 \ 'cNext',
4442 \ 'cNfile',
4443 \ 'cabove',
4444 \ 'cbelow',
4445 \ 'cfirst',
4446 \ 'clast',
4447 \ 'cnewer',
4448 \ 'cnext',
4449 \ 'cnfile',
4450 \ 'colder',
4451 \ 'cprevious',
4452 \ 'crewind',
4453 \
4454 \ 'lNext',
4455 \ 'lNfile',
4456 \ 'labove',
4457 \ 'lbelow',
4458 \ 'lfirst',
4459 \ 'llast',
4460 \ 'lnewer',
4461 \ 'lnext',
4462 \ 'lnfile',
4463 \ 'lolder',
4464 \ 'lprevious',
4465 \ 'lrewind',
4466 \ ]
4467 for cmd in commands
4468 call assert_fails('-1' .. cmd, 'E16:')
4469 call assert_fails('.' .. cmd, 'E16:')
4470 call assert_fails('%' .. cmd, 'E16:')
4471 call assert_fails('$' .. cmd, 'E16:')
4472 endfor
4473endfunc
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02004474
4475" Test for aborting quickfix commands using QuickFixCmdPre
4476func Xtest_qfcmd_abort(cchar)
4477 call s:setup_commands(a:cchar)
4478
4479 call g:Xsetlist([], 'f')
4480
4481 " cexpr/lexpr
4482 let e = ''
4483 try
4484 Xexpr ["F1:10:Line10", "F2:20:Line20"]
4485 catch /.*/
4486 let e = v:exception
4487 endtry
4488 call assert_equal('AbortCmd', e)
4489 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4490
4491 " cfile/lfile
4492 call writefile(["F1:10:Line10", "F2:20:Line20"], 'Xfile1')
4493 let e = ''
4494 try
4495 Xfile Xfile1
4496 catch /.*/
4497 let e = v:exception
4498 endtry
4499 call assert_equal('AbortCmd', e)
4500 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4501 call delete('Xfile1')
4502
4503 " cgetbuffer/lgetbuffer
4504 enew!
4505 call append(0, ["F1:10:Line10", "F2:20:Line20"])
4506 let e = ''
4507 try
4508 Xgetbuffer
4509 catch /.*/
4510 let e = v:exception
4511 endtry
4512 call assert_equal('AbortCmd', e)
4513 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4514 enew!
4515
4516 " vimgrep/lvimgrep
4517 let e = ''
4518 try
4519 Xvimgrep /func/ test_quickfix.vim
4520 catch /.*/
4521 let e = v:exception
4522 endtry
4523 call assert_equal('AbortCmd', e)
4524 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4525
4526 " helpgrep/lhelpgrep
4527 let e = ''
4528 try
4529 Xhelpgrep quickfix
4530 catch /.*/
4531 let e = v:exception
4532 endtry
4533 call assert_equal('AbortCmd', e)
4534 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4535
4536 " grep/lgrep
4537 if has('unix')
4538 let e = ''
4539 try
4540 silent Xgrep func test_quickfix.vim
4541 catch /.*/
4542 let e = v:exception
4543 endtry
4544 call assert_equal('AbortCmd', e)
4545 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
4546 endif
4547endfunc
4548
4549func Test_qfcmd_abort()
4550 augroup QF_Test
4551 au!
4552 autocmd QuickFixCmdPre * throw "AbortCmd"
4553 augroup END
4554
4555 call Xtest_qfcmd_abort('c')
4556 call Xtest_qfcmd_abort('l')
4557
4558 augroup QF_Test
4559 au!
4560 augroup END
4561endfunc
4562
Bram Moolenaard8a8c4c2019-11-16 21:04:57 +01004563" Test for using a file in one of the parent directories.
4564func Test_search_in_dirstack()
4565 call mkdir('Xtestdir/a/b/c', 'p')
4566 let save_cwd = getcwd()
4567 call writefile(["X1_L1", "X1_L2"], 'Xtestdir/Xfile1')
4568 call writefile(["X2_L1", "X2_L2"], 'Xtestdir/a/Xfile2')
4569 call writefile(["X3_L1", "X3_L2"], 'Xtestdir/a/b/Xfile3')
4570 call writefile(["X4_L1", "X4_L2"], 'Xtestdir/a/b/c/Xfile4')
4571
4572 let lines = "Entering dir Xtestdir\n" .
4573 \ "Entering dir a\n" .
4574 \ "Entering dir b\n" .
4575 \ "Xfile2:2:X2_L2\n" .
4576 \ "Leaving dir a\n" .
4577 \ "Xfile1:2:X1_L2\n" .
4578 \ "Xfile3:1:X3_L1\n" .
4579 \ "Entering dir c\n" .
4580 \ "Xfile4:2:X4_L2\n" .
4581 \ "Leaving dir c\n" .
4582 \ "Leaving dir Xtestdir\n"
4583 set efm=%DEntering\ dir\ %f,%XLeaving\ dir\ %f,%f:%l:%m
4584 cexpr lines
4585 call assert_equal(11, getqflist({'size' : 0}).size)
4586 call assert_equal(4, getqflist({'idx' : 0}).idx)
4587 call assert_equal('X2_L2', getline('.'))
4588 cnext
4589 call assert_equal(6, getqflist({'idx' : 0}).idx)
4590 call assert_equal('X1_L2', getline('.'))
4591 cnext
4592 call assert_equal(7, getqflist({'idx' : 0}).idx)
4593 call assert_equal(1, line('$'))
4594 call assert_equal('', getline(1))
4595 cnext
4596 call assert_equal(9, getqflist({'idx' : 0}).idx)
4597 call assert_equal(1, line('$'))
4598 call assert_equal('', getline(1))
4599
4600 set efm&
4601 exe 'cd ' . save_cwd
4602 call delete('Xtestdir', 'rf')
4603endfunc
4604
Bram Moolenaar6a0cc912019-10-26 16:48:44 +02004605" vim: shiftwidth=2 sts=2 expandtab