blob: ae3aec5222340e26ebccc5edb25eacaf761da1d4 [file] [log] [blame]
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001" Test for the quickfix feature.
Bram Moolenaarda59dd52016-01-05 21:59:58 +01002
3if !has('quickfix')
4 finish
5endif
6
Bram Moolenaar7eba3d22016-03-19 22:54:09 +01007set encoding=utf-8
8
Bram Moolenaar2b946c92016-11-12 18:14:44 +01009func s:setup_commands(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020010 if a:cchar == 'c'
11 command! -nargs=* -bang Xlist <mods>clist<bang> <args>
12 command! -nargs=* Xgetexpr <mods>cgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020013 command! -nargs=* Xaddexpr <mods>caddexpr <args>
Bram Moolenaar55b69262017-08-13 13:42:01 +020014 command! -nargs=* -count Xolder <mods><count>colder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020015 command! -nargs=* Xnewer <mods>cnewer <args>
16 command! -nargs=* Xopen <mods>copen <args>
17 command! -nargs=* Xwindow <mods>cwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020018 command! -nargs=* Xbottom <mods>cbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020019 command! -nargs=* Xclose <mods>cclose <args>
20 command! -nargs=* -bang Xfile <mods>cfile<bang> <args>
21 command! -nargs=* Xgetfile <mods>cgetfile <args>
22 command! -nargs=* Xaddfile <mods>caddfile <args>
23 command! -nargs=* -bang Xbuffer <mods>cbuffer<bang> <args>
24 command! -nargs=* Xgetbuffer <mods>cgetbuffer <args>
25 command! -nargs=* Xaddbuffer <mods>caddbuffer <args>
26 command! -nargs=* Xrewind <mods>crewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020027 command! -count -nargs=* -bang Xnext <mods><count>cnext<bang> <args>
28 command! -count -nargs=* -bang Xprev <mods><count>cprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020029 command! -nargs=* -bang Xfirst <mods>cfirst<bang> <args>
30 command! -nargs=* -bang Xlast <mods>clast<bang> <args>
Bram Moolenaar25190db2019-05-04 15:05:28 +020031 command! -count -nargs=* -bang Xnfile <mods><count>cnfile<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020032 command! -nargs=* -bang Xpfile <mods>cpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020033 command! -nargs=* Xexpr <mods>cexpr <args>
Bram Moolenaaree85df32017-03-19 14:19:50 +010034 command! -range -nargs=* Xvimgrep <mods><count>vimgrep <args>
35 command! -nargs=* Xvimgrepadd <mods>vimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020036 command! -nargs=* Xgrep <mods> grep <args>
37 command! -nargs=* Xgrepadd <mods> grepadd <args>
38 command! -nargs=* Xhelpgrep helpgrep <args>
Bram Moolenaar74240d32017-12-10 15:26:15 +010039 command! -nargs=0 -count Xcc <count>cc
Bram Moolenaar3ff33112019-05-03 21:56:35 +020040 command! -count=1 -nargs=0 Xbelow <mods><count>cbelow
41 command! -count=1 -nargs=0 Xabove <mods><count>cabove
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +020042 command! -count=1 -nargs=0 Xbefore <mods><count>cbefore
43 command! -count=1 -nargs=0 Xafter <mods><count>cafter
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020044 let g:Xgetlist = function('getqflist')
45 let g:Xsetlist = function('setqflist')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020046 call setqflist([], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020047 else
48 command! -nargs=* -bang Xlist <mods>llist<bang> <args>
49 command! -nargs=* Xgetexpr <mods>lgetexpr <args>
Bram Moolenaar361c8f02016-07-02 15:41:47 +020050 command! -nargs=* Xaddexpr <mods>laddexpr <args>
Bram Moolenaar55b69262017-08-13 13:42:01 +020051 command! -nargs=* -count Xolder <mods><count>lolder <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020052 command! -nargs=* Xnewer <mods>lnewer <args>
53 command! -nargs=* Xopen <mods>lopen <args>
54 command! -nargs=* Xwindow <mods>lwindow <args>
Bram Moolenaar537ef082016-07-09 17:56:19 +020055 command! -nargs=* Xbottom <mods>lbottom <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020056 command! -nargs=* Xclose <mods>lclose <args>
57 command! -nargs=* -bang Xfile <mods>lfile<bang> <args>
58 command! -nargs=* Xgetfile <mods>lgetfile <args>
59 command! -nargs=* Xaddfile <mods>laddfile <args>
60 command! -nargs=* -bang Xbuffer <mods>lbuffer<bang> <args>
61 command! -nargs=* Xgetbuffer <mods>lgetbuffer <args>
62 command! -nargs=* Xaddbuffer <mods>laddbuffer <args>
63 command! -nargs=* Xrewind <mods>lrewind <args>
Bram Moolenaar99895ea2017-04-20 22:44:47 +020064 command! -count -nargs=* -bang Xnext <mods><count>lnext<bang> <args>
65 command! -count -nargs=* -bang Xprev <mods><count>lprev<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020066 command! -nargs=* -bang Xfirst <mods>lfirst<bang> <args>
67 command! -nargs=* -bang Xlast <mods>llast<bang> <args>
Bram Moolenaar25190db2019-05-04 15:05:28 +020068 command! -count -nargs=* -bang Xnfile <mods><count>lnfile<bang> <args>
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +020069 command! -nargs=* -bang Xpfile <mods>lpfile<bang> <args>
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020070 command! -nargs=* Xexpr <mods>lexpr <args>
Bram Moolenaaree85df32017-03-19 14:19:50 +010071 command! -range -nargs=* Xvimgrep <mods><count>lvimgrep <args>
72 command! -nargs=* Xvimgrepadd <mods>lvimgrepadd <args>
Bram Moolenaar049cba92016-06-26 14:38:04 +020073 command! -nargs=* Xgrep <mods> lgrep <args>
74 command! -nargs=* Xgrepadd <mods> lgrepadd <args>
75 command! -nargs=* Xhelpgrep lhelpgrep <args>
Bram Moolenaar74240d32017-12-10 15:26:15 +010076 command! -nargs=0 -count Xcc <count>ll
Bram Moolenaar3ff33112019-05-03 21:56:35 +020077 command! -count=1 -nargs=0 Xbelow <mods><count>lbelow
78 command! -count=1 -nargs=0 Xabove <mods><count>labove
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +020079 command! -count=1 -nargs=0 Xbefore <mods><count>lbefore
80 command! -count=1 -nargs=0 Xafter <mods><count>lafter
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020081 let g:Xgetlist = function('getloclist', [0])
82 let g:Xsetlist = function('setloclist', [0])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020083 call setloclist(0, [], 'f')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020084 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +010085endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020086
Bram Moolenaarda59dd52016-01-05 21:59:58 +010087" Tests for the :clist and :llist commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +010088func XlistTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020089 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +010090
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +020091 if a:cchar == 'l'
92 call assert_fails('llist', 'E776:')
93 endif
Bram Moolenaarda59dd52016-01-05 21:59:58 +010094 " With an empty list, command should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +020095 Xgetexpr []
96 silent! Xlist
Bram Moolenaarda59dd52016-01-05 21:59:58 +010097 call assert_true(v:errmsg ==# 'E42: No Errors')
98
99 " Populate the list and then try
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200100 Xgetexpr ['non-error 1', 'Xtestfile1:1:3:Line1',
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100101 \ 'non-error 2', 'Xtestfile2:2:2:Line2',
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200102 \ 'non-error 3', 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100103
104 " List only valid entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100105 let l = split(execute('Xlist', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100106 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
107 \ ' 4 Xtestfile2:2 col 2: Line2',
108 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
109
110 " List all the entries
Bram Moolenaaree85df32017-03-19 14:19:50 +0100111 let l = split(execute('Xlist!', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100112 call assert_equal([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1',
113 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2',
114 \ ' 5: non-error 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
115
116 " List a range of errors
Bram Moolenaaree85df32017-03-19 14:19:50 +0100117 let l = split(execute('Xlist 3,6', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100118 call assert_equal([' 4 Xtestfile2:2 col 2: Line2',
119 \ ' 6 Xtestfile3:3 col 1: Line3'], l)
120
Bram Moolenaaree85df32017-03-19 14:19:50 +0100121 let l = split(execute('Xlist! 3,4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100122 call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
123
Bram Moolenaaree85df32017-03-19 14:19:50 +0100124 let l = split(execute('Xlist -6,-4', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100125 call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l)
126
Bram Moolenaaree85df32017-03-19 14:19:50 +0100127 let l = split(execute('Xlist! -5,-3', ''), "\n")
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100128 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
129 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100130
131 " Test for '+'
Bram Moolenaaree85df32017-03-19 14:19:50 +0100132 let l = split(execute('Xlist! +2', ''), "\n")
Bram Moolenaar391b1dd2017-03-04 13:47:11 +0100133 call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
134 \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
Bram Moolenaaree85df32017-03-19 14:19:50 +0100135
136 " Different types of errors
137 call g:Xsetlist([{'lnum':10,'col':5,'type':'W', 'text':'Warning','nr':11},
138 \ {'lnum':20,'col':10,'type':'e','text':'Error','nr':22},
139 \ {'lnum':30,'col':15,'type':'i','text':'Info','nr':33},
140 \ {'lnum':40,'col':20,'type':'x', 'text':'Other','nr':44},
141 \ {'lnum':50,'col':25,'type':"\<C-A>",'text':'one','nr':55}])
142 let l = split(execute('Xlist', ""), "\n")
143 call assert_equal([' 1:10 col 5 warning 11: Warning',
144 \ ' 2:20 col 10 error 22: Error',
145 \ ' 3:30 col 15 info 33: Info',
146 \ ' 4:40 col 20 x 44: Other',
147 \ ' 5:50 col 25 55: one'], l)
148
Bram Moolenaard76ce852018-05-01 15:02:04 +0200149 " Test for module names, one needs to explicitly set `'valid':v:true` so
150 call g:Xsetlist([
151 \ {'lnum':10,'col':5,'type':'W','module':'Data.Text','text':'ModuleWarning','nr':11,'valid':v:true},
152 \ {'lnum':20,'col':10,'type':'W','module':'Data.Text','filename':'Data/Text.hs','text':'ModuleWarning','nr':22,'valid':v:true},
153 \ {'lnum':30,'col':15,'type':'W','filename':'Data/Text.hs','text':'FileWarning','nr':33,'valid':v:true}])
154 let l = split(execute('Xlist', ""), "\n")
155 call assert_equal([' 1 Data.Text:10 col 5 warning 11: ModuleWarning',
156 \ ' 2 Data.Text:20 col 10 warning 22: ModuleWarning',
157 \ ' 3 Data/Text.hs:30 col 15 warning 33: FileWarning'], l)
158
Bram Moolenaaree85df32017-03-19 14:19:50 +0100159 " Error cases
160 call assert_fails('Xlist abc', 'E488:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100161endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100162
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100163func Test_clist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100164 call XlistTests('c')
165 call XlistTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100166endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100167
168" Tests for the :colder, :cnewer, :lolder and :lnewer commands
169" Note that this test assumes that a quickfix/location list is
Bram Moolenaarcfc0a352016-01-09 20:23:00 +0100170" already set by the caller.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100171func XageTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200172 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100173
Bram Moolenaar87f59b02019-04-04 14:04:11 +0200174 if a:cchar == 'l'
175 " No location list for the current window
176 call assert_fails('lolder', 'E776:')
177 call assert_fails('lnewer', 'E776:')
178 endif
179
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200180 let list = [{'bufnr': bufnr('%'), 'lnum': 1}]
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200181 call g:Xsetlist(list)
182
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100183 " Jumping to a non existent list should return error
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200184 silent! Xolder 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100185 call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
186
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200187 silent! Xnewer 99
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100188 call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
189
190 " Add three quickfix/location lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200191 Xgetexpr ['Xtestfile1:1:3:Line1']
192 Xgetexpr ['Xtestfile2:2:2:Line2']
193 Xgetexpr ['Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100194
195 " Go back two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200196 Xolder
197 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100198 call assert_equal('Line2', l[0].text)
199
200 " Go forward two lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200201 Xnewer
202 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100203 call assert_equal('Line3', l[0].text)
204
205 " Test for the optional count argument
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200206 Xolder 2
207 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100208 call assert_equal('Line1', l[0].text)
209
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200210 Xnewer 2
211 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100212 call assert_equal('Line3', l[0].text)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100213endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100214
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100215func Test_cage()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100216 call XageTests('c')
217 call XageTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100218endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100219
220" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
221" commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100222func XwindowTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200223 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100224
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200225 " Opening the location list window without any errors should fail
226 if a:cchar == 'l'
227 call assert_fails('lopen', 'E776:')
228 endif
229
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100230 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200231 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100232
233 " Quickfix/Location window should not open with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200234 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100235 call assert_true(winnr('$') == 1)
236
237 " Create a list with valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200238 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
239 \ 'Xtestfile3:3:1:Line3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100240
241 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200242 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100243 call assert_true(winnr('$') == 2 && winnr() == 2 &&
244 \ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
Bram Moolenaar537ef082016-07-09 17:56:19 +0200245 redraw!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100246
247 " Close the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200248 Xclose
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100249 call assert_true(winnr('$') == 1)
250
251 " Create a list with no valid entries
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200252 Xgetexpr ['non-error 1', 'non-error 2', 'non-error 3']
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100253
254 " Open the window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200255 Xopen 5
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100256 call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
257 \ && winheight('.') == 5)
258
259 " Opening the window again, should move the cursor to that window
260 wincmd t
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200261 Xopen 7
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100262 call assert_true(winnr('$') == 2 && winnr() == 2 &&
263 \ winheight('.') == 7 &&
264 \ getline('.') ==# '|| non-error 1')
265
266
267 " Calling cwindow should close the quickfix window with no valid errors
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200268 Xwindow
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100269 call assert_true(winnr('$') == 1)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200270
271 if a:cchar == 'c'
272 " Opening the quickfix window in multiple tab pages should reuse the
273 " quickfix buffer
274 Xgetexpr ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
275 \ 'Xtestfile3:3:1:Line3']
276 Xopen
277 let qfbufnum = bufnr('%')
278 tabnew
279 Xopen
280 call assert_equal(qfbufnum, bufnr('%'))
281 new | only | tabonly
282 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100283endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100284
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100285func Test_cwindow()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100286 call XwindowTests('c')
287 call XwindowTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100288endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100289
Bram Moolenaar36d50222019-05-02 20:17:40 +0200290func Test_copenHeight()
291 copen
292 wincmd H
293 let height = winheight(0)
294 copen 10
295 call assert_equal(height, winheight(0))
296 quit
297endfunc
298
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100299" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
300" commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100301func XfileTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200302 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100303
304 call writefile(['Xtestfile1:700:10:Line 700',
305 \ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
306
307 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200308 Xfile Xqftestfile1
309 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100310 call assert_true(len(l) == 2 &&
311 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
312 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
313
Bram Moolenaar049cba92016-06-26 14:38:04 +0200314 " Test with a non existent file
315 call assert_fails('Xfile non_existent_file', 'E40')
316
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100317 " Run cfile/lfile from a modified buffer
318 enew!
319 silent! put ='Quickfix'
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200320 silent! Xfile Xqftestfile1
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100321 call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
322
323 call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200324 Xaddfile Xqftestfile1
325 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100326 call assert_true(len(l) == 3 &&
327 \ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
328
329 call writefile(['Xtestfile1:222:77:Line 222',
330 \ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
331
332 enew!
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200333 Xgetfile Xqftestfile1
334 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100335 call assert_true(len(l) == 2 &&
336 \ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
337 \ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
338
339 call delete('Xqftestfile1')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100340endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100341
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100342func Test_cfile()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100343 call XfileTests('c')
344 call XfileTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100345endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100346
347" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
348" :lgetbuffer commands.
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100349func XbufferTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200350 call s:setup_commands(a:cchar)
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100351
352 enew!
353 silent! call setline(1, ['Xtestfile7:700:10:Line 700',
354 \ 'Xtestfile8:800:15:Line 800'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200355 Xbuffer!
356 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100357 call assert_true(len(l) == 2 &&
358 \ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
359 \ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
360
361 enew!
362 silent! call setline(1, ['Xtestfile9:900:55:Line 900',
363 \ 'Xtestfile10:950:66:Line 950'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200364 Xgetbuffer
365 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100366 call assert_true(len(l) == 2 &&
367 \ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
368 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
369
370 enew!
371 silent! call setline(1, ['Xtestfile11:700:20:Line 700',
372 \ 'Xtestfile12:750:25:Line 750'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200373 Xaddbuffer
374 let l = g:Xgetlist()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100375 call assert_true(len(l) == 4 &&
376 \ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
377 \ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
378 \ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
Bram Moolenaarab47c612016-06-14 22:02:26 +0200379 enew!
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100380
Bram Moolenaaree85df32017-03-19 14:19:50 +0100381 " Check for invalid buffer
382 call assert_fails('Xbuffer 199', 'E474:')
383
384 " Check for unloaded buffer
385 edit Xtestfile1
386 let bnr = bufnr('%')
387 enew!
388 call assert_fails('Xbuffer ' . bnr, 'E681:')
389
390 " Check for invalid range
391 " Using Xbuffer will not run the range check in the cbuffer/lbuffer
392 " commands. So directly call the commands.
393 if (a:cchar == 'c')
394 call assert_fails('900,999cbuffer', 'E16:')
395 else
396 call assert_fails('900,999lbuffer', 'E16:')
397 endif
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100398endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100399
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100400func Test_cbuffer()
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100401 call XbufferTests('c')
402 call XbufferTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100403endfunc
Bram Moolenaarda59dd52016-01-05 21:59:58 +0100404
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100405func XexprTests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200406 call s:setup_commands(a:cchar)
407
408 call assert_fails('Xexpr 10', 'E777:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100409endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200410
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100411func Test_cexpr()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200412 call XexprTests('c')
413 call XexprTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100414endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200415
416" Tests for :cnext, :cprev, :cfirst, :clast commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100417func Xtest_browse(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200418 call s:setup_commands(a:cchar)
419
Bram Moolenaar74240d32017-12-10 15:26:15 +0100420 call g:Xsetlist([], 'f')
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200421 " Jumping to first or next location list entry without any error should
422 " result in failure
Bram Moolenaar74240d32017-12-10 15:26:15 +0100423 if a:cchar == 'c'
424 let err = 'E42:'
425 else
426 let err = 'E776:'
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200427 endif
Bram Moolenaar74240d32017-12-10 15:26:15 +0100428 call assert_fails('Xnext', err)
429 call assert_fails('Xprev', err)
430 call assert_fails('Xnfile', err)
431 call assert_fails('Xpfile', err)
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +0200432
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200433 call s:create_test_file('Xqftestfile1')
434 call s:create_test_file('Xqftestfile2')
435
436 Xgetexpr ['Xqftestfile1:5:Line5',
437 \ 'Xqftestfile1:6:Line6',
438 \ 'Xqftestfile2:10:Line10',
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200439 \ 'Xqftestfile2:11:Line11',
440 \ 'RegularLine1',
441 \ 'RegularLine2']
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200442
443 Xfirst
444 call assert_fails('Xprev', 'E553')
445 call assert_fails('Xpfile', 'E553')
446 Xnfile
447 call assert_equal('Xqftestfile2', bufname('%'))
448 call assert_equal(10, line('.'))
449 Xpfile
450 call assert_equal('Xqftestfile1', bufname('%'))
451 call assert_equal(6, line('.'))
Bram Moolenaar74240d32017-12-10 15:26:15 +0100452 5Xcc
453 call assert_equal(5, g:Xgetlist({'idx':0}).idx)
454 2Xcc
455 call assert_equal(2, g:Xgetlist({'idx':0}).idx)
456 10Xcc
457 call assert_equal(6, g:Xgetlist({'idx':0}).idx)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200458 Xlast
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200459 Xprev
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200460 call assert_equal('Xqftestfile2', bufname('%'))
461 call assert_equal(11, line('.'))
462 call assert_fails('Xnext', 'E553')
463 call assert_fails('Xnfile', 'E553')
464 Xrewind
465 call assert_equal('Xqftestfile1', bufname('%'))
466 call assert_equal(5, line('.'))
467
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200468 10Xnext
469 call assert_equal('Xqftestfile2', bufname('%'))
470 call assert_equal(11, line('.'))
471 10Xprev
472 call assert_equal('Xqftestfile1', bufname('%'))
473 call assert_equal(5, line('.'))
474
Bram Moolenaar74240d32017-12-10 15:26:15 +0100475 " Jumping to an error from the error window using cc command
476 Xgetexpr ['Xqftestfile1:5:Line5',
477 \ 'Xqftestfile1:6:Line6',
478 \ 'Xqftestfile2:10:Line10',
479 \ 'Xqftestfile2:11:Line11']
480 Xopen
481 10Xcc
482 call assert_equal(11, line('.'))
483 call assert_equal('Xqftestfile2', bufname('%'))
484
485 " Jumping to an error from the error window (when only the error window is
486 " present)
487 Xopen | only
488 Xlast 1
489 call assert_equal(5, line('.'))
490 call assert_equal('Xqftestfile1', bufname('%'))
491
Bram Moolenaaree85df32017-03-19 14:19:50 +0100492 Xexpr ""
493 call assert_fails('Xnext', 'E42:')
494
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200495 call delete('Xqftestfile1')
496 call delete('Xqftestfile2')
Bram Moolenaarfc2b2702017-09-15 22:43:07 +0200497
498 " Should be able to use next/prev with invalid entries
499 Xexpr ""
500 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
501 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
502 Xaddexpr ['foo', 'bar', 'baz', 'quux', 'shmoo']
503 call assert_equal(5, g:Xgetlist({'size' : 0}).size)
504 Xlast
505 call assert_equal(5, g:Xgetlist({'idx' : 0}).idx)
506 Xfirst
507 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
508 2Xnext
509 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100510endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200511
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100512func Test_browse()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200513 call Xtest_browse('c')
514 call Xtest_browse('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100515endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +0200516
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100517func Test_nomem()
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200518 call test_alloc_fail(GetAllocId('qf_dirname_start'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100519 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100520
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200521 call test_alloc_fail(GetAllocId('qf_dirname_now'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100522 call assert_fails('vimgrep vim runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100523
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200524 call test_alloc_fail(GetAllocId('qf_namebuf'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100525 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100526
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200527 call test_alloc_fail(GetAllocId('qf_errmsg'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100528 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100529
Bram Moolenaar8e8df252016-05-25 21:23:21 +0200530 call test_alloc_fail(GetAllocId('qf_pattern'), 0, 0)
Bram Moolenaara260b872016-01-15 20:48:22 +0100531 call assert_fails('cfile runtest.vim', 'E342:')
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100532
533endfunc
534
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100535func s:test_xhelpgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +0200536 call s:setup_commands(a:cchar)
537 Xhelpgrep quickfix
538 Xopen
539 if a:cchar == 'c'
540 let title_text = ':helpgrep quickfix'
541 else
542 let title_text = ':lhelpgrep quickfix'
543 endif
544 call assert_true(w:quickfix_title =~ title_text, w:quickfix_title)
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200545
546 " Jumping to a help topic should open the help window
547 only
548 Xnext
549 call assert_true(&buftype == 'help')
550 call assert_true(winnr('$') == 2)
551 " Jumping to the next match should reuse the help window
552 Xnext
553 call assert_true(&buftype == 'help')
554 call assert_true(winnr() == 1)
555 call assert_true(winnr('$') == 2)
556 " Jumping to the next match from the quickfix window should reuse the help
557 " window
558 Xopen
559 Xnext
560 call assert_true(&buftype == 'help')
561 call assert_true(winnr() == 1)
562 call assert_true(winnr('$') == 2)
563
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100564 " This wipes out the buffer, make sure that doesn't cause trouble.
Bram Moolenaar049cba92016-06-26 14:38:04 +0200565 Xclose
Bram Moolenaaree85df32017-03-19 14:19:50 +0100566
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +0200567 if a:cchar == 'l'
568 " When a help window is present, running :lhelpgrep should reuse the
569 " help window and not the current window
570 new | only
571 call g:Xsetlist([], 'f')
572 help index.txt
573 wincmd w
574 lhelpgrep quickfix
575 call assert_equal(1, winnr())
576 call assert_notequal([], getloclist(1))
577 call assert_equal([], getloclist(2))
578 endif
579
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200580 new | only
581
Bram Moolenaaree85df32017-03-19 14:19:50 +0100582 " Search for non existing help string
583 call assert_fails('Xhelpgrep a1b2c3', 'E480:')
Bram Moolenaar108e7b42018-10-11 17:39:12 +0200584 " Invalid regular expression
585 call assert_fails('Xhelpgrep \@<!', 'E480:')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100586endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +0200587
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100588func Test_helpgrep()
Bram Moolenaar049cba92016-06-26 14:38:04 +0200589 call s:test_xhelpgrep('c')
Bram Moolenaarcf25fdb2016-08-03 21:04:53 +0200590 helpclose
Bram Moolenaar049cba92016-06-26 14:38:04 +0200591 call s:test_xhelpgrep('l')
Bram Moolenaar62ef7972016-01-19 14:51:54 +0100592endfunc
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +0100593
Bram Moolenaar6920c722016-01-22 22:44:10 +0100594func Test_errortitle()
595 augroup QfBufWinEnter
596 au!
597 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
598 augroup END
599 copen
600 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'')'}]
601 call setqflist(a)
602 call assert_equal(':setqflist()', g:a)
603 augroup QfBufWinEnter
604 au!
605 augroup END
606 augroup! QfBufWinEnter
607endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100608
Bram Moolenaar5584df62016-03-18 21:00:51 +0100609func Test_vimgreptitle()
610 augroup QfBufWinEnter
611 au!
612 au BufWinEnter * :let g:a=get(w:, 'quickfix_title', 'NONE')
613 augroup END
614 try
615 vimgrep /pattern/j file
616 catch /E480/
617 endtry
618 copen
619 call assert_equal(': vimgrep /pattern/j file', g:a)
620 augroup QfBufWinEnter
621 au!
622 augroup END
623 augroup! QfBufWinEnter
624endfunc
625
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100626func XqfTitleTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200627 call s:setup_commands(a:cchar)
Bram Moolenaare27dba42016-03-15 14:11:10 +0100628
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200629 Xgetexpr ['file:1:1:message']
630 let l = g:Xgetlist()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100631 if a:cchar == 'c'
632 call setqflist(l, 'r')
633 else
634 call setloclist(0, l, 'r')
635 endif
636
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200637 Xopen
Bram Moolenaare27dba42016-03-15 14:11:10 +0100638 if a:cchar == 'c'
639 let title = ':setqflist()'
640 else
641 let title = ':setloclist()'
642 endif
643 call assert_equal(title, w:quickfix_title)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200644 Xclose
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100645endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100646
647" Tests for quickfix window's title
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100648func Test_qf_title()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200649 call XqfTitleTests('c')
650 call XqfTitleTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100651endfunc
Bram Moolenaare27dba42016-03-15 14:11:10 +0100652
653" Tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100654func Test_efm()
Bram Moolenaare27dba42016-03-15 14:11:10 +0100655 let save_efm = &efm
656 set efm=%EEEE%m,%WWWW%m,%+CCCC%.%#,%-GGGG%.%#
657 cgetexpr ['WWWW', 'EEEE', 'CCCC']
658 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
659 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
660 cgetexpr ['WWWW', 'GGGG', 'EEEE', 'CCCC']
661 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
662 call assert_equal("[['W', 1], ['E^@CCCC', 1]]", l)
663 cgetexpr ['WWWW', 'GGGG', 'ZZZZ', 'EEEE', 'CCCC', 'YYYY']
664 let l = strtrans(string(map(getqflist(), '[v:val.text, v:val.valid]')))
665 call assert_equal("[['W', 1], ['ZZZZ', 0], ['E^@CCCC', 1], ['YYYY', 0]]", l)
666 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100667endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100668
669" This will test for problems in quickfix:
670" A. incorrectly copying location lists which caused the location list to show
671" a different name than the file that was actually being displayed.
672" B. not reusing the window for which the location list window is opened but
673" instead creating new windows.
674" C. make sure that the location list window is not reused instead of the
675" window it belongs to.
676"
677" Set up the test environment:
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100678func ReadTestProtocol(name)
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100679 let base = substitute(a:name, '\v^test://(.*)%(\.[^.]+)?', '\1', '')
680 let word = substitute(base, '\v(.*)\..*', '\1', '')
681
682 setl modifiable
683 setl noreadonly
684 setl noswapfile
685 setl bufhidden=delete
686 %del _
687 " For problem 2:
688 " 'buftype' has to be set to reproduce the constant opening of new windows
689 setl buftype=nofile
690
691 call setline(1, word)
692
693 setl nomodified
694 setl nomodifiable
695 setl readonly
696 exe 'doautocmd BufRead ' . substitute(a:name, '\v^test://(.*)', '\1', '')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100697endfunc
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100698
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100699func Test_locationlist()
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100700 enew
701
702 augroup testgroup
703 au!
704 autocmd BufReadCmd test://* call ReadTestProtocol(expand("<amatch>"))
705 augroup END
706
707 let words = [ "foo", "bar", "baz", "quux", "shmoo", "spam", "eggs" ]
708
709 let qflist = []
710 for word in words
711 call add(qflist, {'filename': 'test://' . word . '.txt', 'text': 'file ' . word . '.txt', })
712 " NOTE: problem 1:
713 " intentionally not setting 'lnum' so that the quickfix entries are not
714 " valid
715 call setloclist(0, qflist, ' ')
716 endfor
717
718 " Test A
719 lrewind
720 enew
721 lopen
Bram Moolenaar99895ea2017-04-20 22:44:47 +0200722 4lnext
Bram Moolenaar1ff2b642016-03-17 22:07:02 +0100723 vert split
724 wincmd L
725 lopen
726 wincmd p
727 lnext
728 let fileName = expand("%")
729 wincmd p
730 let locationListFileName = substitute(getline(line('.')), '\([^|]*\)|.*', '\1', '')
731 let fileName = substitute(fileName, '\\', '/', 'g')
732 let locationListFileName = substitute(locationListFileName, '\\', '/', 'g')
733 call assert_equal("test://bar.txt", fileName)
734 call assert_equal("test://bar.txt", locationListFileName)
735
736 wincmd n | only
737
738 " Test B:
739 lrewind
740 lopen
741 2
742 exe "normal \<CR>"
743 wincmd p
744 3
745 exe "normal \<CR>"
746 wincmd p
747 4
748 exe "normal \<CR>"
749 call assert_equal(2, winnr('$'))
750 wincmd n | only
751
752 " Test C:
753 lrewind
754 lopen
755 " Let's move the location list window to the top to check whether it (the
756 " first window found) will be reused when we try to open new windows:
757 wincmd K
758 2
759 exe "normal \<CR>"
760 wincmd p
761 3
762 exe "normal \<CR>"
763 wincmd p
764 4
765 exe "normal \<CR>"
766 1wincmd w
767 call assert_equal('quickfix', &buftype)
768 2wincmd w
769 let bufferName = expand("%")
770 let bufferName = substitute(bufferName, '\\', '/', 'g')
771 call assert_equal('test://quux.txt', bufferName)
772
773 wincmd n | only
774
775 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100776endfunc
Bram Moolenaar0899d692016-03-19 13:35:03 +0100777
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100778func Test_locationlist_curwin_was_closed()
Bram Moolenaar0899d692016-03-19 13:35:03 +0100779 augroup testgroup
780 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200781 autocmd BufReadCmd test_curwin.txt call R(expand("<amatch>"))
Bram Moolenaar0899d692016-03-19 13:35:03 +0100782 augroup END
783
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100784 func! R(n)
Bram Moolenaar0899d692016-03-19 13:35:03 +0100785 quit
786 endfunc
787
788 new
789 let q = []
Bram Moolenaard106e5b2016-04-21 19:38:07 +0200790 call add(q, {'filename': 'test_curwin.txt' })
Bram Moolenaar0899d692016-03-19 13:35:03 +0100791 call setloclist(0, q)
792 call assert_fails('lrewind', 'E924:')
793
794 augroup! testgroup
Bram Moolenaaree85df32017-03-19 14:19:50 +0100795endfunc
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100796
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100797func Test_locationlist_cross_tab_jump()
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200798 call writefile(['loclistfoo'], 'loclistfoo')
799 call writefile(['loclistbar'], 'loclistbar')
800 set switchbuf=usetab
801
802 edit loclistfoo
803 tabedit loclistbar
804 silent lgrep loclistfoo loclist*
805 call assert_equal(1, tabpagenr())
806
807 enew | only | tabonly
808 set switchbuf&vim
809 call delete('loclistfoo')
810 call delete('loclistbar')
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100811endfunc
Bram Moolenaar0a9046f2016-10-15 19:28:13 +0200812
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100813" More tests for 'errorformat'
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100814func Test_efm1()
Bram Moolenaar7eba3d22016-03-19 22:54:09 +0100815 if !has('unix')
816 " The 'errorformat' setting is different on non-Unix systems.
817 " This test works only on Unix-like systems.
818 return
819 endif
820
821 let l = [
822 \ '"Xtestfile", line 4.12: 1506-045 (S) Undeclared identifier fd_set.',
823 \ '"Xtestfile", line 6 col 19; this is an error',
824 \ 'gcc -c -DHAVE_CONFIsing-prototypes -I/usr/X11R6/include version.c',
825 \ 'Xtestfile:9: parse error before `asd''',
826 \ 'make: *** [vim] Error 1',
827 \ 'in file "Xtestfile" linenr 10: there is an error',
828 \ '',
829 \ '2 returned',
830 \ '"Xtestfile", line 11 col 1; this is an error',
831 \ '"Xtestfile", line 12 col 2; this is another error',
832 \ '"Xtestfile", line 14:10; this is an error in column 10',
833 \ '=Xtestfile=, line 15:10; this is another error, but in vcol 10 this time',
834 \ '"Xtestfile", linenr 16: yet another problem',
835 \ 'Error in "Xtestfile" at line 17:',
836 \ 'x should be a dot',
837 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17',
838 \ ' ^',
839 \ 'Error in "Xtestfile" at line 18:',
840 \ 'x should be a dot',
841 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18',
842 \ '.............^',
843 \ 'Error in "Xtestfile" at line 19:',
844 \ 'x should be a dot',
845 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19',
846 \ '--------------^',
847 \ 'Error in "Xtestfile" at line 20:',
848 \ 'x should be a dot',
849 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20',
850 \ ' ^',
851 \ '',
852 \ 'Does anyone know what is the problem and how to correction it?',
853 \ '"Xtestfile", line 21 col 9: What is the title of the quickfix window?',
854 \ '"Xtestfile", line 22 col 9: What is the title of the quickfix window?'
855 \ ]
856
857 call writefile(l, 'Xerrorfile1')
858 call writefile(l[:-2], 'Xerrorfile2')
859
860 let m = [
861 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 2',
862 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 3',
863 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 4',
864 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 5',
865 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 6',
866 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 7',
867 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 8',
868 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 9',
869 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 10',
870 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 11',
871 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 12',
872 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 13',
873 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 14',
874 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 15',
875 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 16',
876 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 17',
877 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 18',
878 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 19',
879 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 20',
880 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 21',
881 \ ' xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx line 22'
882 \ ]
883 call writefile(m, 'Xtestfile')
884
885 let save_efm = &efm
886 set efm+==%f=\\,\ line\ %l%*\\D%v%*[^\ ]\ %m
887 set efm^=%AError\ in\ \"%f\"\ at\ line\ %l:,%Z%p^,%C%m
888
889 exe 'cf Xerrorfile2'
890 clast
891 copen
892 call assert_equal(':cf Xerrorfile2', w:quickfix_title)
893 wincmd p
894
895 exe 'cf Xerrorfile1'
896 call assert_equal([4, 12], [line('.'), col('.')])
897 cn
898 call assert_equal([6, 19], [line('.'), col('.')])
899 cn
900 call assert_equal([9, 2], [line('.'), col('.')])
901 cn
902 call assert_equal([10, 2], [line('.'), col('.')])
903 cn
904 call assert_equal([11, 1], [line('.'), col('.')])
905 cn
906 call assert_equal([12, 2], [line('.'), col('.')])
907 cn
908 call assert_equal([14, 10], [line('.'), col('.')])
909 cn
910 call assert_equal([15, 3, 10], [line('.'), col('.'), virtcol('.')])
911 cn
912 call assert_equal([16, 2], [line('.'), col('.')])
913 cn
914 call assert_equal([17, 6], [line('.'), col('.')])
915 cn
916 call assert_equal([18, 7], [line('.'), col('.')])
917 cn
918 call assert_equal([19, 8], [line('.'), col('.')])
919 cn
920 call assert_equal([20, 9], [line('.'), col('.')])
921 clast
922 cprev
923 cprev
924 wincmd w
925 call assert_equal(':cf Xerrorfile1', w:quickfix_title)
926 wincmd p
927
928 let &efm = save_efm
929 call delete('Xerrorfile1')
930 call delete('Xerrorfile2')
931 call delete('Xtestfile')
Bram Moolenaaree85df32017-03-19 14:19:50 +0100932endfunc
Bram Moolenaarffec3c52016-03-23 20:55:42 +0100933
Bram Moolenaarab47c612016-06-14 22:02:26 +0200934" Test for quickfix directory stack support
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100935func s:dir_stack_tests(cchar)
Bram Moolenaar38df43b2016-06-20 21:41:12 +0200936 call s:setup_commands(a:cchar)
937
Bram Moolenaarab47c612016-06-14 22:02:26 +0200938 let save_efm=&efm
939 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
940
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200941 let lines = ["Entering dir 'dir1/a'",
942 \ 'habits2.txt:1:Nine Healthy Habits',
943 \ "Entering dir 'b'",
944 \ 'habits3.txt:2:0 Hours of television',
945 \ 'habits2.txt:7:5 Small meals',
946 \ "Entering dir 'dir1/c'",
947 \ 'habits4.txt:3:1 Hour of exercise',
948 \ "Leaving dir 'dir1/c'",
949 \ "Leaving dir 'dir1/a'",
950 \ 'habits1.txt:4:2 Liters of water',
951 \ "Entering dir 'dir2'",
952 \ 'habits5.txt:5:3 Cups of hot green tea',
953 \ "Leaving dir 'dir2'"
954 \]
Bram Moolenaarab47c612016-06-14 22:02:26 +0200955
Bram Moolenaar361c8f02016-07-02 15:41:47 +0200956 Xexpr ""
957 for l in lines
958 Xaddexpr l
959 endfor
Bram Moolenaarab47c612016-06-14 22:02:26 +0200960
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +0200961 let qf = g:Xgetlist()
Bram Moolenaarab47c612016-06-14 22:02:26 +0200962
963 call assert_equal('dir1/a/habits2.txt', bufname(qf[1].bufnr))
964 call assert_equal(1, qf[1].lnum)
965 call assert_equal('dir1/a/b/habits3.txt', bufname(qf[3].bufnr))
966 call assert_equal(2, qf[3].lnum)
967 call assert_equal('dir1/a/habits2.txt', bufname(qf[4].bufnr))
968 call assert_equal(7, qf[4].lnum)
969 call assert_equal('dir1/c/habits4.txt', bufname(qf[6].bufnr))
970 call assert_equal(3, qf[6].lnum)
971 call assert_equal('habits1.txt', bufname(qf[9].bufnr))
972 call assert_equal(4, qf[9].lnum)
973 call assert_equal('dir2/habits5.txt', bufname(qf[11].bufnr))
974 call assert_equal(5, qf[11].lnum)
975
976 let &efm=save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100977endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +0200978
979" Tests for %D and %X errorformat options
Bram Moolenaar2b946c92016-11-12 18:14:44 +0100980func Test_efm_dirstack()
Bram Moolenaarab47c612016-06-14 22:02:26 +0200981 " Create the directory stack and files
982 call mkdir('dir1')
983 call mkdir('dir1/a')
984 call mkdir('dir1/a/b')
985 call mkdir('dir1/c')
986 call mkdir('dir2')
987
988 let lines = ["Nine Healthy Habits",
989 \ "0 Hours of television",
990 \ "1 Hour of exercise",
991 \ "2 Liters of water",
992 \ "3 Cups of hot green tea",
993 \ "4 Short mental breaks",
994 \ "5 Small meals",
995 \ "6 AM wake up time",
996 \ "7 Minutes of laughter",
997 \ "8 Hours of sleep (at least)",
998 \ "9 PM end of the day and off to bed"
999 \ ]
1000 call writefile(lines, 'habits1.txt')
1001 call writefile(lines, 'dir1/a/habits2.txt')
1002 call writefile(lines, 'dir1/a/b/habits3.txt')
1003 call writefile(lines, 'dir1/c/habits4.txt')
1004 call writefile(lines, 'dir2/habits5.txt')
1005
1006 call s:dir_stack_tests('c')
1007 call s:dir_stack_tests('l')
1008
1009 call delete('dir1', 'rf')
1010 call delete('dir2', 'rf')
1011 call delete('habits1.txt')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001012endfunc
Bram Moolenaarab47c612016-06-14 22:02:26 +02001013
Bram Moolenaar9b457942016-10-09 16:10:05 +02001014" Test for resync after continuing an ignored message
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001015func Xefm_ignore_continuations(cchar)
Bram Moolenaar9b457942016-10-09 16:10:05 +02001016 call s:setup_commands(a:cchar)
1017
1018 let save_efm = &efm
1019
1020 let &efm =
1021 \ '%Eerror %m %l,' .
1022 \ '%-Wignored %m %l,' .
1023 \ '%+Cmore ignored %m %l,' .
1024 \ '%Zignored end'
1025 Xgetexpr ['ignored warning 1', 'more ignored continuation 2', 'ignored end', 'error resync 4']
1026 let l = map(g:Xgetlist(), '[v:val.text, v:val.valid, v:val.lnum, v:val.type]')
1027 call assert_equal([['resync', 1, 4, 'E']], l)
1028
1029 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001030endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001031
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001032func Test_efm_ignore_continuations()
Bram Moolenaar9b457942016-10-09 16:10:05 +02001033 call Xefm_ignore_continuations('c')
1034 call Xefm_ignore_continuations('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001035endfunc
Bram Moolenaar9b457942016-10-09 16:10:05 +02001036
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001037" Tests for invalid error format specifies
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001038func Xinvalid_efm_Tests(cchar)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001039 call s:setup_commands(a:cchar)
1040
Bram Moolenaar049cba92016-06-26 14:38:04 +02001041 let save_efm = &efm
1042
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001043 set efm=%f:%l:%m,%f:%f:%l:%m
1044 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E372:')
1045
1046 set efm=%f:%l:%m,%f:%l:%r:%m
1047 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1048
1049 set efm=%f:%l:%m,%O:%f:%l:%m
1050 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E373:')
1051
1052 set efm=%f:%l:%m,%f:%l:%*[^a-z
1053 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E374:')
1054
1055 set efm=%f:%l:%m,%f:%l:%*c
1056 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E375:')
1057
1058 set efm=%f:%l:%m,%L%M%N
1059 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E376:')
1060
1061 set efm=%f:%l:%m,%f:%l:%m:%R
1062 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E377:')
1063
1064 set efm=
1065 call assert_fails('Xexpr "abc.txt:1:Hello world"', 'E378:')
1066
1067 set efm=%DEntering\ dir\ abc,%f:%l:%m
1068 call assert_fails('Xexpr ["Entering dir abc", "abc.txt:1:Hello world"]', 'E379:')
1069
1070 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001071endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001072
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001073func Test_invalid_efm()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001074 call Xinvalid_efm_Tests('c')
1075 call Xinvalid_efm_Tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001076endfunc
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001077
1078" TODO:
1079" Add tests for the following formats in 'errorformat'
1080" %r %O
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001081func Test_efm2()
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001082 let save_efm = &efm
Bram Moolenaar049cba92016-06-26 14:38:04 +02001083
1084 " Test for %s format in efm
1085 set efm=%f:%s
1086 cexpr 'Xtestfile:Line search text'
Bram Moolenaar049cba92016-06-26 14:38:04 +02001087 let l = getqflist()
1088 call assert_equal(l[0].pattern, '^\VLine search text\$')
1089 call assert_equal(l[0].lnum, 0)
1090
Bram Moolenaaree85df32017-03-19 14:19:50 +01001091 let l = split(execute('clist', ''), "\n")
1092 call assert_equal([' 1 Xtestfile:^\VLine search text\$: '], l)
1093
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001094 " Test for %P, %Q and %t format specifiers
Bram Moolenaar049cba92016-06-26 14:38:04 +02001095 let lines=["[Xtestfile1]",
1096 \ "(1,17) error: ';' missing",
1097 \ "(21,2) warning: variable 'z' not defined",
1098 \ "(67,3) error: end of file found before string ended",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001099 \ "--",
Bram Moolenaar049cba92016-06-26 14:38:04 +02001100 \ "",
1101 \ "[Xtestfile2]",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001102 \ "--",
Bram Moolenaar049cba92016-06-26 14:38:04 +02001103 \ "",
1104 \ "[Xtestfile3]",
1105 \ "NEW compiler v1.1",
1106 \ "(2,2) warning: variable 'x' not defined",
Bram Moolenaaree85df32017-03-19 14:19:50 +01001107 \ "(67,3) warning: 's' already defined",
1108 \ "--"
Bram Moolenaar049cba92016-06-26 14:38:04 +02001109 \]
Bram Moolenaaree85df32017-03-19 14:19:50 +01001110 set efm=%+P[%f]%r,(%l\\,%c)%*[\ ]%t%*[^:]:\ %m,%+Q--%r
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001111 " To exercise the push/pop file functionality in quickfix, the test files
1112 " need to be created.
1113 call writefile(['Line1'], 'Xtestfile1')
1114 call writefile(['Line2'], 'Xtestfile2')
1115 call writefile(['Line3'], 'Xtestfile3')
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001116 cexpr ""
1117 for l in lines
1118 caddexpr l
1119 endfor
Bram Moolenaar049cba92016-06-26 14:38:04 +02001120 let l = getqflist()
Bram Moolenaaree85df32017-03-19 14:19:50 +01001121 call assert_equal(12, len(l))
Bram Moolenaar049cba92016-06-26 14:38:04 +02001122 call assert_equal(21, l[2].lnum)
1123 call assert_equal(2, l[2].col)
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001124 call assert_equal('w', l[2].type)
1125 call assert_equal('e', l[3].type)
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001126 call delete('Xtestfile1')
1127 call delete('Xtestfile2')
1128 call delete('Xtestfile3')
Bram Moolenaar0fcc7c62016-07-02 21:22:52 +02001129
1130 " Tests for %E, %C and %Z format specifiers
1131 let lines = ["Error 275",
1132 \ "line 42",
1133 \ "column 3",
1134 \ "' ' expected after '--'"
1135 \]
1136 set efm=%EError\ %n,%Cline\ %l,%Ccolumn\ %c,%Z%m
1137 cgetexpr lines
1138 let l = getqflist()
1139 call assert_equal(275, l[0].nr)
1140 call assert_equal(42, l[0].lnum)
1141 call assert_equal(3, l[0].col)
1142 call assert_equal('E', l[0].type)
1143 call assert_equal("\n' ' expected after '--'", l[0].text)
1144
1145 " Test for %>
1146 let lines = ["Error in line 147 of foo.c:",
1147 \"unknown variable 'i'"
1148 \]
1149 set efm=unknown\ variable\ %m,%E%>Error\ in\ line\ %l\ of\ %f:,%Z%m
1150 cgetexpr lines
1151 let l = getqflist()
1152 call assert_equal(147, l[0].lnum)
1153 call assert_equal('E', l[0].type)
1154 call assert_equal("\nunknown variable 'i'", l[0].text)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001155
Bram Moolenaare87e6dd2016-07-17 19:25:04 +02001156 " Test for %A, %C and other formats
1157 let lines = [
1158 \"==============================================================",
1159 \"FAIL: testGetTypeIdCachesResult (dbfacadeTest.DjsDBFacadeTest)",
1160 \"--------------------------------------------------------------",
1161 \"Traceback (most recent call last):",
1162 \' File "unittests/dbfacadeTest.py", line 89, in testFoo',
1163 \" self.assertEquals(34, dtid)",
1164 \' File "/usr/lib/python2.2/unittest.py", line 286, in',
1165 \" failUnlessEqual",
1166 \" raise self.failureException, \\",
1167 \"AssertionError: 34 != 33",
1168 \"",
1169 \"--------------------------------------------------------------",
1170 \"Ran 27 tests in 0.063s"
1171 \]
1172 set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
1173 cgetexpr lines
1174 let l = getqflist()
1175 call assert_equal(8, len(l))
1176 call assert_equal(89, l[4].lnum)
1177 call assert_equal(1, l[4].valid)
1178 call assert_equal('unittests/dbfacadeTest.py', bufname(l[4].bufnr))
1179
Bram Moolenaard76ce852018-05-01 15:02:04 +02001180 " Test for %o
1181 set efm=%f(%o):%l\ %m
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001182 cgetexpr ['Xotestfile(Language.PureScript.Types):20 Error']
1183 call writefile(['Line1'], 'Xotestfile')
Bram Moolenaard76ce852018-05-01 15:02:04 +02001184 let l = getqflist()
1185 call assert_equal(1, len(l), string(l))
1186 call assert_equal('Language.PureScript.Types', l[0].module)
1187 copen
1188 call assert_equal('Language.PureScript.Types|20| Error', getline(1))
1189 call feedkeys("\<CR>", 'xn')
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001190 call assert_equal('Xotestfile', expand('%:t'))
Bram Moolenaard76ce852018-05-01 15:02:04 +02001191 cclose
1192 bd
Bram Moolenaarbc7845d2018-05-01 16:26:48 +02001193 call delete("Xotestfile")
Bram Moolenaard76ce852018-05-01 15:02:04 +02001194
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001195 " The following sequence of commands used to crash Vim
1196 set efm=%W%m
1197 cgetexpr ['msg1']
1198 let l = getqflist()
1199 call assert_equal(1, len(l), string(l))
1200 call assert_equal('msg1', l[0].text)
1201 set efm=%C%m
1202 lexpr 'msg2'
1203 let l = getloclist(0)
1204 call assert_equal(1, len(l), string(l))
1205 call assert_equal('msg2', l[0].text)
1206 lopen
1207 call setqflist([], 'r')
1208 caddbuf
1209 let l = getqflist()
1210 call assert_equal(1, len(l), string(l))
1211 call assert_equal('|| msg2', l[0].text)
1212
Bram Moolenaar78ddc062018-05-15 21:56:34 +02001213 " When matching error lines, case should be ignored. Test for this.
1214 set noignorecase
1215 let l=getqflist({'lines' : ['Xtest:FOO10:Line 20'], 'efm':'%f:foo%l:%m'})
1216 call assert_equal(10, l.items[0].lnum)
1217 call assert_equal('Line 20', l.items[0].text)
1218 set ignorecase&
1219
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001220 new | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001221 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001222endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001223
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001224func XquickfixChangedByAutocmd(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001225 call s:setup_commands(a:cchar)
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001226 if a:cchar == 'c'
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001227 let ErrorNr = 'E925'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001228 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001229 colder
1230 cgetexpr []
1231 endfunc
1232 else
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001233 let ErrorNr = 'E926'
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001234 func! ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001235 lolder
1236 lgetexpr []
1237 endfunc
1238 endif
1239
1240 augroup testgroup
1241 au!
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001242 autocmd BufReadCmd test_changed.txt call ReadFunc()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001243 augroup END
1244
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001245 new | only
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001246 let words = [ "a", "b" ]
1247 let qflist = []
1248 for word in words
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001249 call add(qflist, {'filename': 'test_changed.txt'})
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001250 call g:Xsetlist(qflist, ' ')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001251 endfor
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001252 call assert_fails('Xrewind', ErrorNr . ':')
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001253
1254 augroup! testgroup
1255endfunc
1256
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001257func Test_quickfix_was_changed_by_autocmd()
Bram Moolenaarffec3c52016-03-23 20:55:42 +01001258 call XquickfixChangedByAutocmd('c')
1259 call XquickfixChangedByAutocmd('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001260endfunc
Bram Moolenaar8b201792016-03-25 15:01:10 +01001261
1262func Test_caddbuffer_to_empty()
1263 helpgr quickfix
1264 call setqflist([], 'r')
1265 cad
Bram Moolenaarf68f1d72016-03-25 17:14:06 +01001266 try
1267 cn
1268 catch
1269 " number of matches is unknown
1270 call assert_true(v:exception =~ 'E553:')
1271 endtry
Bram Moolenaar8b201792016-03-25 15:01:10 +01001272 quit!
1273endfunc
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001274
1275func Test_cgetexpr_works()
1276 " this must not crash Vim
1277 cgetexpr [$x]
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001278 lgetexpr [$x]
Bram Moolenaar89c64d52016-03-27 18:44:40 +02001279endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001280
1281" Tests for the setqflist() and setloclist() functions
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001282func SetXlistTests(cchar, bnum)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001283 call s:setup_commands(a:cchar)
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001284
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001285 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 1},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001286 \ {'bufnr': a:bnum, 'lnum': 2}])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001287 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001288 call assert_equal(2, len(l))
1289 call assert_equal(2, l[1].lnum)
1290
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001291 Xnext
1292 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3}], 'a')
1293 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001294 call assert_equal(3, len(l))
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001295 Xnext
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001296 call assert_equal(3, line('.'))
1297
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001298 " Appending entries to the list should not change the cursor position
1299 " in the quickfix window
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001300 Xwindow
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001301 1
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001302 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 4},
Bram Moolenaar2b2b8ae2016-05-24 19:59:51 +02001303 \ {'bufnr': a:bnum, 'lnum': 5}], 'a')
1304 call assert_equal(1, line('.'))
1305 close
1306
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001307 call g:Xsetlist([{'bufnr': a:bnum, 'lnum': 3},
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001308 \ {'bufnr': a:bnum, 'lnum': 4},
1309 \ {'bufnr': a:bnum, 'lnum': 5}], 'r')
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001310 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001311 call assert_equal(3, len(l))
1312 call assert_equal(5, l[2].lnum)
1313
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001314 call g:Xsetlist([])
1315 let l = g:Xgetlist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001316 call assert_equal(0, len(l))
Bram Moolenaaree85df32017-03-19 14:19:50 +01001317
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001318 " Tests for setting the 'valid' flag
1319 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':4, 'valid':0}])
1320 Xwindow
1321 call assert_equal(1, winnr('$'))
1322 let l = g:Xgetlist()
1323 call g:Xsetlist(l)
1324 call assert_equal(0, g:Xgetlist()[0].valid)
Bram Moolenaar9752c722018-12-22 16:49:34 +01001325 " Adding a non-valid entry should not mark the list as having valid entries
1326 call g:Xsetlist([{'bufnr':a:bnum, 'lnum':5, 'valid':0}], 'a')
1327 Xwindow
1328 call assert_equal(1, winnr('$'))
1329
1330 " :cnext/:cprev should still work even with invalid entries in the list
1331 let l = [{'bufnr' : a:bnum, 'lnum' : 1, 'text' : '1', 'valid' : 0},
1332 \ {'bufnr' : a:bnum, 'lnum' : 2, 'text' : '2', 'valid' : 0}]
1333 call g:Xsetlist(l)
1334 Xnext
1335 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1336 Xprev
1337 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1338 " :cnext/:cprev should still work after appending invalid entries to an
1339 " empty list
1340 call g:Xsetlist([])
1341 call g:Xsetlist(l, 'a')
1342 Xnext
1343 call assert_equal(2, g:Xgetlist({'idx' : 0}).idx)
1344 Xprev
1345 call assert_equal(1, g:Xgetlist({'idx' : 0}).idx)
1346
Bram Moolenaarf1d21c82017-04-22 21:20:46 +02001347 call g:Xsetlist([{'text':'Text1', 'valid':1}])
1348 Xwindow
1349 call assert_equal(2, winnr('$'))
1350 Xclose
1351 let save_efm = &efm
1352 set efm=%m
1353 Xgetexpr 'TestMessage'
1354 let l = g:Xgetlist()
1355 call g:Xsetlist(l)
1356 call assert_equal(1, g:Xgetlist()[0].valid)
1357 let &efm = save_efm
1358
Bram Moolenaaree85df32017-03-19 14:19:50 +01001359 " Error cases:
1360 " Refer to a non-existing buffer and pass a non-dictionary type
1361 call assert_fails("call g:Xsetlist([{'bufnr':998, 'lnum':4}," .
1362 \ " {'bufnr':999, 'lnum':5}])", 'E92:')
1363 call g:Xsetlist([[1, 2,3]])
1364 call assert_equal(0, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001365endfunc
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001366
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001367func Test_setqflist()
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001368 new Xtestfile | only
1369 let bnum = bufnr('%')
1370 call setline(1, range(1,5))
1371
1372 call SetXlistTests('c', bnum)
1373 call SetXlistTests('l', bnum)
1374
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001375 enew!
Bram Moolenaarc1808d52016-04-18 20:04:00 +02001376 call delete('Xtestfile')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001377endfunc
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001378
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001379func Xlist_empty_middle(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001380 call s:setup_commands(a:cchar)
1381
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001382 " create three quickfix lists
Bram Moolenaaree85df32017-03-19 14:19:50 +01001383 let @/ = 'Test_'
1384 Xvimgrep // test_quickfix.vim
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001385 let testlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001386 call assert_true(testlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001387 Xvimgrep empty test_quickfix.vim
1388 call assert_true(len(g:Xgetlist()) > 0)
1389 Xvimgrep matches test_quickfix.vim
1390 let matchlen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001391 call assert_true(matchlen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001392 Xolder
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001393 " make the middle list empty
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001394 call g:Xsetlist([], 'r')
1395 call assert_true(len(g:Xgetlist()) == 0)
1396 Xolder
1397 call assert_equal(testlen, len(g:Xgetlist()))
1398 Xnewer
1399 Xnewer
1400 call assert_equal(matchlen, len(g:Xgetlist()))
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001401endfunc
1402
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001403func Test_setqflist_empty_middle()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001404 call Xlist_empty_middle('c')
1405 call Xlist_empty_middle('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001406endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001407
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001408func Xlist_empty_older(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001409 call s:setup_commands(a:cchar)
1410
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001411 " create three quickfix lists
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001412 Xvimgrep one test_quickfix.vim
1413 let onelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001414 call assert_true(onelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001415 Xvimgrep two test_quickfix.vim
1416 let twolen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001417 call assert_true(twolen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001418 Xvimgrep three test_quickfix.vim
1419 let threelen = len(g:Xgetlist())
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001420 call assert_true(threelen > 0)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001421 Xolder 2
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001422 " make the first list empty, check the others didn't change
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001423 call g:Xsetlist([], 'r')
1424 call assert_true(len(g:Xgetlist()) == 0)
1425 Xnewer
1426 call assert_equal(twolen, len(g:Xgetlist()))
1427 Xnewer
1428 call assert_equal(threelen, len(g:Xgetlist()))
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001429endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001430
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001431func Test_setqflist_empty_older()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001432 call Xlist_empty_older('c')
1433 call Xlist_empty_older('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001434endfunc
Bram Moolenaar1cee6932016-06-09 22:17:22 +02001435
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001436func XquickfixSetListWithAct(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001437 call s:setup_commands(a:cchar)
1438
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001439 let list1 = [{'filename': 'fnameA', 'text': 'A'},
1440 \ {'filename': 'fnameB', 'text': 'B'}]
1441 let list2 = [{'filename': 'fnameC', 'text': 'C'},
1442 \ {'filename': 'fnameD', 'text': 'D'},
1443 \ {'filename': 'fnameE', 'text': 'E'}]
1444
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01001445 " {action} is unspecified. Same as specifying ' '.
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001446 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001447 silent! Xnewer 99
1448 call g:Xsetlist(list1)
1449 call g:Xsetlist(list2)
1450 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001451 call assert_equal(3, len(li))
1452 call assert_equal('C', li[0]['text'])
1453 call assert_equal('D', li[1]['text'])
1454 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001455 silent! Xolder
1456 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001457 call assert_equal(2, len(li))
1458 call assert_equal('A', li[0]['text'])
1459 call assert_equal('B', li[1]['text'])
1460
1461 " {action} is specified ' '.
1462 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001463 silent! Xnewer 99
1464 call g:Xsetlist(list1)
1465 call g:Xsetlist(list2, ' ')
1466 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001467 call assert_equal(3, len(li))
1468 call assert_equal('C', li[0]['text'])
1469 call assert_equal('D', li[1]['text'])
1470 call assert_equal('E', li[2]['text'])
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001471 silent! Xolder
1472 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001473 call assert_equal(2, len(li))
1474 call assert_equal('A', li[0]['text'])
1475 call assert_equal('B', li[1]['text'])
1476
1477 " {action} is specified 'a'.
1478 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001479 silent! Xnewer 99
1480 call g:Xsetlist(list1)
1481 call g:Xsetlist(list2, 'a')
1482 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001483 call assert_equal(5, len(li))
1484 call assert_equal('A', li[0]['text'])
1485 call assert_equal('B', li[1]['text'])
1486 call assert_equal('C', li[2]['text'])
1487 call assert_equal('D', li[3]['text'])
1488 call assert_equal('E', li[4]['text'])
1489
1490 " {action} is specified 'r'.
1491 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001492 silent! Xnewer 99
1493 call g:Xsetlist(list1)
1494 call g:Xsetlist(list2, 'r')
1495 let li = g:Xgetlist()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001496 call assert_equal(3, len(li))
1497 call assert_equal('C', li[0]['text'])
1498 call assert_equal('D', li[1]['text'])
1499 call assert_equal('E', li[2]['text'])
1500
1501 " Test for wrong value.
1502 new | only
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001503 call assert_fails("call g:Xsetlist(0)", 'E714:')
1504 call assert_fails("call g:Xsetlist(list1, '')", 'E927:')
1505 call assert_fails("call g:Xsetlist(list1, 'aa')", 'E927:')
1506 call assert_fails("call g:Xsetlist(list1, ' a')", 'E927:')
1507 call assert_fails("call g:Xsetlist(list1, 0)", 'E928:')
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001508endfunc
1509
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01001510func Test_setqflist_invalid_nr()
1511 " The following command used to crash Vim
1512 call setqflist([], ' ', {'nr' : $XXX_DOES_NOT_EXIST})
1513endfunc
1514
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001515func Test_quickfix_set_list_with_act()
Bram Moolenaard106e5b2016-04-21 19:38:07 +02001516 call XquickfixSetListWithAct('c')
1517 call XquickfixSetListWithAct('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001518endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001519
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001520func XLongLinesTests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001521 let l = g:Xgetlist()
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001522
Bram Moolenaar049cba92016-06-26 14:38:04 +02001523 call assert_equal(4, len(l))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001524 call assert_equal(1, l[0].lnum)
1525 call assert_equal(1, l[0].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001526 call assert_equal(1975, len(l[0].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001527 call assert_equal(2, l[1].lnum)
1528 call assert_equal(1, l[1].col)
1529 call assert_equal(4070, len(l[1].text))
1530 call assert_equal(3, l[2].lnum)
1531 call assert_equal(1, l[2].col)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001532 call assert_equal(4070, len(l[2].text))
1533 call assert_equal(4, l[3].lnum)
1534 call assert_equal(1, l[3].col)
1535 call assert_equal(10, len(l[3].text))
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001536
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001537 call g:Xsetlist([], 'r')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001538endfunc
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001539
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001540func s:long_lines_tests(cchar)
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001541 call s:setup_commands(a:cchar)
1542
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001543 let testfile = 'samples/quickfix.txt'
1544
1545 " file
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001546 exe 'Xgetfile' testfile
1547 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001548
1549 " list
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001550 Xexpr readfile(testfile)
1551 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001552
1553 " string
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001554 Xexpr join(readfile(testfile), "\n")
1555 call XLongLinesTests(a:cchar)
Bram Moolenaar6be8c8e2016-04-30 13:17:09 +02001556
1557 " buffer
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001558 exe 'edit' testfile
1559 exe 'Xbuffer' bufnr('%')
Bram Moolenaarf50df392016-06-21 21:33:34 +02001560 call XLongLinesTests(a:cchar)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001561endfunc
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001562
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001563func Test_long_lines()
Bram Moolenaar3ef5bf72016-06-15 22:41:31 +02001564 call s:long_lines_tests('c')
1565 call s:long_lines_tests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001566endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001567
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001568func s:create_test_file(filename)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001569 let l = []
1570 for i in range(1, 20)
1571 call add(l, 'Line' . i)
1572 endfor
1573 call writefile(l, a:filename)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001574endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001575
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001576func Test_switchbuf()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001577 call s:create_test_file('Xqftestfile1')
1578 call s:create_test_file('Xqftestfile2')
1579 call s:create_test_file('Xqftestfile3')
1580
1581 new | only
1582 edit Xqftestfile1
1583 let file1_winid = win_getid()
1584 new Xqftestfile2
1585 let file2_winid = win_getid()
1586 cgetexpr ['Xqftestfile1:5:Line5',
1587 \ 'Xqftestfile1:6:Line6',
1588 \ 'Xqftestfile2:10:Line10',
1589 \ 'Xqftestfile2:11:Line11',
1590 \ 'Xqftestfile3:15:Line15',
1591 \ 'Xqftestfile3:16:Line16']
1592
1593 new
1594 let winid = win_getid()
1595 cfirst | cnext
1596 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001597 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001598 call assert_equal(winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001599 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001600 call assert_equal(winid, win_getid())
1601 enew
1602
1603 set switchbuf=useopen
1604 cfirst | cnext
1605 call assert_equal(file1_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001606 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001607 call assert_equal(file2_winid, win_getid())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001608 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001609 call assert_equal(file2_winid, win_getid())
1610
1611 enew | only
1612 set switchbuf=usetab
1613 tabedit Xqftestfile1
1614 tabedit Xqftestfile2
Bram Moolenaar74240d32017-12-10 15:26:15 +01001615 tabedit Xqftestfile3
Bram Moolenaar049cba92016-06-26 14:38:04 +02001616 tabfirst
1617 cfirst | cnext
1618 call assert_equal(2, tabpagenr())
Bram Moolenaar99895ea2017-04-20 22:44:47 +02001619 2cnext
Bram Moolenaar049cba92016-06-26 14:38:04 +02001620 call assert_equal(3, tabpagenr())
Bram Moolenaar74240d32017-12-10 15:26:15 +01001621 6cnext
1622 call assert_equal(4, tabpagenr())
1623 2cpfile
1624 call assert_equal(2, tabpagenr())
1625 2cnfile
1626 call assert_equal(4, tabpagenr())
Bram Moolenaar049cba92016-06-26 14:38:04 +02001627 tabfirst | tabonly | enew
1628
1629 set switchbuf=split
1630 cfirst | cnext
1631 call assert_equal(1, winnr('$'))
1632 cnext | cnext
1633 call assert_equal(2, winnr('$'))
1634 cnext | cnext
1635 call assert_equal(3, winnr('$'))
1636 enew | only
1637
1638 set switchbuf=newtab
1639 cfirst | cnext
1640 call assert_equal(1, tabpagenr('$'))
1641 cnext | cnext
1642 call assert_equal(2, tabpagenr('$'))
1643 cnext | cnext
1644 call assert_equal(3, tabpagenr('$'))
1645 tabfirst | enew | tabonly | only
1646
1647 set switchbuf=
1648 edit Xqftestfile1
1649 let file1_winid = win_getid()
1650 new Xqftestfile2
1651 let file2_winid = win_getid()
1652 copen
1653 exe "normal 1G\<CR>"
1654 call assert_equal(file1_winid, win_getid())
1655 copen
1656 exe "normal 3G\<CR>"
1657 call assert_equal(file2_winid, win_getid())
1658 copen | only
1659 exe "normal 5G\<CR>"
1660 call assert_equal(2, winnr('$'))
1661 call assert_equal(1, bufwinnr('Xqftestfile3'))
1662
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001663 " If only quickfix window is open in the current tabpage, jumping to an
1664 " entry with 'switchubf' set to 'usetab' should search in other tabpages.
Bram Moolenaar049cba92016-06-26 14:38:04 +02001665 enew | only
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001666 set switchbuf=usetab
1667 tabedit Xqftestfile1
1668 tabedit Xqftestfile2
1669 tabedit Xqftestfile3
1670 tabfirst
1671 copen | only
1672 clast
1673 call assert_equal(4, tabpagenr())
1674 tabfirst | tabonly | enew | only
Bram Moolenaar049cba92016-06-26 14:38:04 +02001675
1676 call delete('Xqftestfile1')
1677 call delete('Xqftestfile2')
1678 call delete('Xqftestfile3')
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01001679 set switchbuf&vim
1680
1681 enew | only
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001682endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001683
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001684func Xadjust_qflnum(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001685 call s:setup_commands(a:cchar)
1686
1687 enew | only
1688
Bram Moolenaarc1542742016-07-20 21:44:37 +02001689 let fname = 'Xqftestfile' . a:cchar
1690 call s:create_test_file(fname)
1691 exe 'edit ' . fname
Bram Moolenaar049cba92016-06-26 14:38:04 +02001692
Bram Moolenaarc1542742016-07-20 21:44:37 +02001693 Xgetexpr [fname . ':5:Line5',
1694 \ fname . ':10:Line10',
1695 \ fname . ':15:Line15',
1696 \ fname . ':20:Line20']
Bram Moolenaar049cba92016-06-26 14:38:04 +02001697
1698 6,14delete
1699 call append(6, ['Buffer', 'Window'])
1700
1701 let l = g:Xgetlist()
1702
1703 call assert_equal(5, l[0].lnum)
1704 call assert_equal(6, l[2].lnum)
1705 call assert_equal(13, l[3].lnum)
1706
1707 enew!
Bram Moolenaarc1542742016-07-20 21:44:37 +02001708 call delete(fname)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001709endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001710
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001711func Test_adjust_lnum()
Bram Moolenaarc1542742016-07-20 21:44:37 +02001712 call setloclist(0, [])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001713 call Xadjust_qflnum('c')
Bram Moolenaarc1542742016-07-20 21:44:37 +02001714 call setqflist([])
Bram Moolenaar049cba92016-06-26 14:38:04 +02001715 call Xadjust_qflnum('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001716endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001717
1718" Tests for the :grep/:lgrep and :grepadd/:lgrepadd commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001719func s:test_xgrep(cchar)
Bram Moolenaar049cba92016-06-26 14:38:04 +02001720 call s:setup_commands(a:cchar)
1721
1722 " The following lines are used for the grep test. Don't remove.
1723 " Grep_Test_Text: Match 1
1724 " Grep_Test_Text: Match 2
1725 " GrepAdd_Test_Text: Match 1
1726 " GrepAdd_Test_Text: Match 2
1727 enew! | only
1728 set makeef&vim
1729 silent Xgrep Grep_Test_Text: test_quickfix.vim
1730 call assert_true(len(g:Xgetlist()) == 3)
1731 Xopen
1732 call assert_true(w:quickfix_title =~ '^:grep')
1733 Xclose
1734 enew
1735 set makeef=Temp_File_##
1736 silent Xgrepadd GrepAdd_Test_Text: test_quickfix.vim
1737 call assert_true(len(g:Xgetlist()) == 6)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001738endfunc
Bram Moolenaar049cba92016-06-26 14:38:04 +02001739
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001740func Test_grep()
Bram Moolenaar049cba92016-06-26 14:38:04 +02001741 if !has('unix')
1742 " The grepprg may not be set on non-Unix systems
1743 return
1744 endif
1745
1746 call s:test_xgrep('c')
1747 call s:test_xgrep('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001748endfunc
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001749
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001750func Test_two_windows()
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001751 " Use one 'errorformat' for two windows. Add an expression to each of them,
1752 " make sure they each keep their own state.
1753 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
1754 call mkdir('Xone/a', 'p')
1755 call mkdir('Xtwo/a', 'p')
1756 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
1757 call writefile(lines, 'Xone/a/one.txt')
1758 call writefile(lines, 'Xtwo/a/two.txt')
1759
1760 new one
1761 let one_id = win_getid()
1762 lexpr ""
1763 new two
1764 let two_id = win_getid()
1765 lexpr ""
1766
1767 laddexpr "Entering dir 'Xtwo/a'"
1768 call win_gotoid(one_id)
1769 laddexpr "Entering dir 'Xone/a'"
1770 call win_gotoid(two_id)
1771 laddexpr 'two.txt:5:two two two'
1772 call win_gotoid(one_id)
1773 laddexpr 'one.txt:3:one one one'
1774
1775 let loc_one = getloclist(one_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001776 call assert_equal('Xone/a/one.txt', bufname(loc_one[1].bufnr))
1777 call assert_equal(3, loc_one[1].lnum)
1778
1779 let loc_two = getloclist(two_id)
Bram Moolenaar361c8f02016-07-02 15:41:47 +02001780 call assert_equal('Xtwo/a/two.txt', bufname(loc_two[1].bufnr))
1781 call assert_equal(5, loc_two[1].lnum)
1782
1783 call win_gotoid(one_id)
1784 bwipe!
1785 call win_gotoid(two_id)
1786 bwipe!
1787 call delete('Xone', 'rf')
1788 call delete('Xtwo', 'rf')
1789endfunc
Bram Moolenaardcb17002016-07-07 18:58:59 +02001790
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001791func XbottomTests(cchar)
Bram Moolenaar537ef082016-07-09 17:56:19 +02001792 call s:setup_commands(a:cchar)
1793
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001794 " Calling lbottom without any errors should fail
1795 if a:cchar == 'l'
1796 call assert_fails('lbottom', 'E776:')
1797 endif
1798
Bram Moolenaar875feea2017-06-11 16:07:51 +02001799 call g:Xsetlist([{'filename': 'foo', 'lnum': 42}])
Bram Moolenaar537ef082016-07-09 17:56:19 +02001800 Xopen
Bram Moolenaardcb17002016-07-07 18:58:59 +02001801 let wid = win_getid()
1802 call assert_equal(1, line('.'))
1803 wincmd w
Bram Moolenaar875feea2017-06-11 16:07:51 +02001804 call g:Xsetlist([{'filename': 'var', 'lnum': 24}], 'a')
Bram Moolenaar537ef082016-07-09 17:56:19 +02001805 Xbottom
Bram Moolenaardcb17002016-07-07 18:58:59 +02001806 call win_gotoid(wid)
1807 call assert_equal(2, line('.'))
Bram Moolenaar537ef082016-07-09 17:56:19 +02001808 Xclose
Bram Moolenaardcb17002016-07-07 18:58:59 +02001809endfunc
Bram Moolenaar537ef082016-07-09 17:56:19 +02001810
1811" Tests for the :cbottom and :lbottom commands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001812func Test_cbottom()
Bram Moolenaar537ef082016-07-09 17:56:19 +02001813 call XbottomTests('c')
1814 call XbottomTests('l')
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001815endfunc
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001816
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001817func HistoryTest(cchar)
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001818 call s:setup_commands(a:cchar)
1819
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001820 " clear all lists after the first one, then replace the first one.
1821 call g:Xsetlist([])
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001822 call assert_fails('Xolder 99', 'E380:')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001823 let entry = {'filename': 'foo', 'lnum': 42}
1824 call g:Xsetlist([entry], 'r')
1825 call g:Xsetlist([entry, entry])
1826 call g:Xsetlist([entry, entry, entry])
1827 let res = split(execute(a:cchar . 'hist'), "\n")
1828 call assert_equal(3, len(res))
1829 let common = 'errors :set' . (a:cchar == 'c' ? 'qf' : 'loc') . 'list()'
1830 call assert_equal(' error list 1 of 3; 1 ' . common, res[0])
1831 call assert_equal(' error list 2 of 3; 2 ' . common, res[1])
1832 call assert_equal('> error list 3 of 3; 3 ' . common, res[2])
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02001833
1834 call g:Xsetlist([], 'f')
1835 let l = split(execute(a:cchar . 'hist'), "\n")
1836 call assert_equal('No entries', l[0])
Bram Moolenaar5b69c222019-01-11 14:50:06 +01001837
1838 " An empty list should still show the stack history
1839 call g:Xsetlist([])
1840 let res = split(execute(a:cchar . 'hist'), "\n")
1841 call assert_equal('> error list 1 of 1; 0 ' . common, res[0])
1842
1843 call g:Xsetlist([], 'f')
Bram Moolenaarf6acffb2016-07-16 16:54:24 +02001844endfunc
1845
1846func Test_history()
1847 call HistoryTest('c')
1848 call HistoryTest('l')
1849endfunc
Bram Moolenaar015102e2016-07-16 18:24:56 +02001850
1851func Test_duplicate_buf()
1852 " make sure we can get the highest buffer number
1853 edit DoesNotExist
1854 edit DoesNotExist2
1855 let last_buffer = bufnr("$")
1856
1857 " make sure only one buffer is created
1858 call writefile(['this one', 'that one'], 'Xgrepthis')
1859 vimgrep one Xgrepthis
1860 vimgrep one Xgrepthis
1861 call assert_equal(last_buffer + 1, bufnr("$"))
1862
1863 call delete('Xgrepthis')
1864endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02001865
1866" Quickfix/Location list set/get properties tests
Bram Moolenaar2b946c92016-11-12 18:14:44 +01001867func Xproperty_tests(cchar)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001868 call s:setup_commands(a:cchar)
1869
1870 " Error cases
1871 call assert_fails('call g:Xgetlist(99)', 'E715:')
1872 call assert_fails('call g:Xsetlist(99)', 'E714:')
1873 call assert_fails('call g:Xsetlist([], "a", [])', 'E715:')
1874
1875 " Set and get the title
Bram Moolenaarb6fa30c2017-03-29 14:19:25 +02001876 call g:Xsetlist([])
Bram Moolenaard823fa92016-08-12 16:29:27 +02001877 Xopen
1878 wincmd p
1879 call g:Xsetlist([{'filename':'foo', 'lnum':27}])
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001880 let s = g:Xsetlist([], 'a', {'title' : 'Sample'})
1881 call assert_equal(0, s)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001882 let d = g:Xgetlist({"title":1})
1883 call assert_equal('Sample', d.title)
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02001884 " Try setting title to a non-string value
1885 call assert_equal(-1, g:Xsetlist([], 'a', {'title' : ['Test']}))
1886 call assert_equal('Sample', g:Xgetlist({"title":1}).title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001887
1888 Xopen
1889 call assert_equal('Sample', w:quickfix_title)
1890 Xclose
1891
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02001892 " Tests for action argument
1893 silent! Xolder 999
1894 let qfnr = g:Xgetlist({'all':1}).nr
1895 call g:Xsetlist([], 'r', {'title' : 'N1'})
1896 call assert_equal('N1', g:Xgetlist({'all':1}).title)
1897 call g:Xsetlist([], ' ', {'title' : 'N2'})
1898 call assert_equal(qfnr + 1, g:Xgetlist({'all':1}).nr)
Bram Moolenaar890680c2016-09-27 21:28:56 +02001899
1900 let res = g:Xgetlist({'nr': 0})
1901 call assert_equal(qfnr + 1, res.nr)
1902 call assert_equal(['nr'], keys(res))
1903
Bram Moolenaar2b529bb2016-08-27 13:35:35 +02001904 call g:Xsetlist([], ' ', {'title' : 'N3'})
1905 call assert_equal('N2', g:Xgetlist({'nr':2, 'title':1}).title)
1906
Bram Moolenaaree85df32017-03-19 14:19:50 +01001907 " Changing the title of an earlier quickfix list
Bram Moolenaar55b69262017-08-13 13:42:01 +02001908 call g:Xsetlist([], 'r', {'title' : 'NewTitle', 'nr' : 2})
Bram Moolenaaree85df32017-03-19 14:19:50 +01001909 call assert_equal('NewTitle', g:Xgetlist({'nr':2, 'title':1}).title)
1910
1911 " Changing the title of an invalid quickfix list
1912 call assert_equal(-1, g:Xsetlist([], ' ',
1913 \ {'title' : 'SomeTitle', 'nr' : 99}))
1914 call assert_equal(-1, g:Xsetlist([], ' ',
1915 \ {'title' : 'SomeTitle', 'nr' : 'abc'}))
1916
1917 if a:cchar == 'c'
1918 copen
1919 call assert_equal({'winid':win_getid()}, getqflist({'winid':1}))
1920 cclose
1921 endif
1922
Bram Moolenaard823fa92016-08-12 16:29:27 +02001923 " Invalid arguments
1924 call assert_fails('call g:Xgetlist([])', 'E715')
1925 call assert_fails('call g:Xsetlist([], "a", [])', 'E715')
1926 let s = g:Xsetlist([], 'a', {'abc':1})
1927 call assert_equal(-1, s)
1928
1929 call assert_equal({}, g:Xgetlist({'abc':1}))
Bram Moolenaara6d48492017-12-12 22:45:31 +01001930 call assert_equal('', g:Xgetlist({'nr':99, 'title':1}).title)
1931 call assert_equal('', g:Xgetlist({'nr':[], 'title':1}).title)
Bram Moolenaard823fa92016-08-12 16:29:27 +02001932
1933 if a:cchar == 'l'
Bram Moolenaar890680c2016-09-27 21:28:56 +02001934 call assert_equal({}, getloclist(99, {'title': 1}))
Bram Moolenaard823fa92016-08-12 16:29:27 +02001935 endif
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001936
1937 " Context related tests
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02001938 let s = g:Xsetlist([], 'a', {'context':[1,2,3]})
1939 call assert_equal(0, s)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001940 call test_garbagecollect_now()
1941 let d = g:Xgetlist({'context':1})
1942 call assert_equal([1,2,3], d.context)
1943 call g:Xsetlist([], 'a', {'context':{'color':'green'}})
1944 let d = g:Xgetlist({'context':1})
1945 call assert_equal({'color':'green'}, d.context)
1946 call g:Xsetlist([], 'a', {'context':"Context info"})
1947 let d = g:Xgetlist({'context':1})
1948 call assert_equal("Context info", d.context)
1949 call g:Xsetlist([], 'a', {'context':246})
1950 let d = g:Xgetlist({'context':1})
1951 call assert_equal(246, d.context)
1952 if a:cchar == 'l'
1953 " Test for copying context across two different location lists
1954 new | only
1955 let w1_id = win_getid()
1956 let l = [1]
1957 call setloclist(0, [], 'a', {'context':l})
1958 new
1959 let w2_id = win_getid()
1960 call add(l, 2)
1961 call assert_equal([1, 2], getloclist(w1_id, {'context':1}).context)
1962 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
1963 unlet! l
1964 call assert_equal([1, 2], getloclist(w2_id, {'context':1}).context)
1965 only
1966 call setloclist(0, [], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01001967 call assert_equal('', getloclist(0, {'context':1}).context)
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02001968 endif
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001969
1970 " Test for changing the context of previous quickfix lists
1971 call g:Xsetlist([], 'f')
1972 Xexpr "One"
1973 Xexpr "Two"
1974 Xexpr "Three"
Bram Moolenaar55b69262017-08-13 13:42:01 +02001975 call g:Xsetlist([], 'r', {'context' : [1], 'nr' : 1})
1976 call g:Xsetlist([], 'a', {'context' : [2], 'nr' : 2})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001977 " Also, check for setting the context using quickfix list number zero.
Bram Moolenaar55b69262017-08-13 13:42:01 +02001978 call g:Xsetlist([], 'r', {'context' : [3], 'nr' : 0})
Bram Moolenaar6e62da32017-05-28 08:16:25 +02001979 call test_garbagecollect_now()
1980 let l = g:Xgetlist({'nr' : 1, 'context' : 1})
1981 call assert_equal([1], l.context)
1982 let l = g:Xgetlist({'nr' : 2, 'context' : 1})
1983 call assert_equal([2], l.context)
1984 let l = g:Xgetlist({'nr' : 3, 'context' : 1})
1985 call assert_equal([3], l.context)
1986
1987 " Test for changing the context through reference and for garbage
1988 " collection of quickfix context
1989 let l = ["red"]
1990 call g:Xsetlist([], ' ', {'context' : l})
1991 call add(l, "blue")
1992 let x = g:Xgetlist({'context' : 1})
1993 call add(x.context, "green")
1994 call assert_equal(["red", "blue", "green"], l)
1995 call assert_equal(["red", "blue", "green"], x.context)
1996 unlet l
1997 call test_garbagecollect_now()
1998 let m = g:Xgetlist({'context' : 1})
1999 call assert_equal(["red", "blue", "green"], m.context)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002000
2001 " Test for setting/getting items
2002 Xexpr ""
2003 let qfprev = g:Xgetlist({'nr':0})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002004 let s = g:Xsetlist([], ' ', {'title':'Green',
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002005 \ 'items' : [{'filename':'F1', 'lnum':10}]})
Bram Moolenaar86f100dc2017-06-28 21:26:27 +02002006 call assert_equal(0, s)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002007 let qfcur = g:Xgetlist({'nr':0})
2008 call assert_true(qfcur.nr == qfprev.nr + 1)
2009 let l = g:Xgetlist({'items':1})
2010 call assert_equal('F1', bufname(l.items[0].bufnr))
2011 call assert_equal(10, l.items[0].lnum)
2012 call g:Xsetlist([], 'a', {'items' : [{'filename':'F2', 'lnum':20},
2013 \ {'filename':'F2', 'lnum':30}]})
2014 let l = g:Xgetlist({'items':1})
2015 call assert_equal('F2', bufname(l.items[2].bufnr))
2016 call assert_equal(30, l.items[2].lnum)
2017 call g:Xsetlist([], 'r', {'items' : [{'filename':'F3', 'lnum':40}]})
2018 let l = g:Xgetlist({'items':1})
2019 call assert_equal('F3', bufname(l.items[0].bufnr))
2020 call assert_equal(40, l.items[0].lnum)
2021 call g:Xsetlist([], 'r', {'items' : []})
2022 let l = g:Xgetlist({'items':1})
2023 call assert_equal(0, len(l.items))
2024
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002025 call g:Xsetlist([], 'r', {'title' : 'TestTitle'})
2026 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2027 call g:Xsetlist([], 'r', {'items' : [{'filename' : 'F1', 'lnum' : 10, 'text' : 'L10'}]})
2028 call assert_equal('TestTitle', g:Xgetlist({'title' : 1}).title)
2029
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02002030 " Test for getting id of window associated with a location list window
2031 if a:cchar == 'l'
2032 only
2033 call assert_equal(0, g:Xgetlist({'all' : 1}).filewinid)
2034 let wid = win_getid()
2035 Xopen
2036 call assert_equal(wid, g:Xgetlist({'filewinid' : 1}).filewinid)
2037 wincmd w
2038 call assert_equal(0, g:Xgetlist({'filewinid' : 1}).filewinid)
2039 only
2040 endif
2041
Bram Moolenaarae338332017-08-11 20:25:26 +02002042 " The following used to crash Vim with address sanitizer
2043 call g:Xsetlist([], 'f')
2044 call g:Xsetlist([], 'a', {'items' : [{'filename':'F1', 'lnum':10}]})
2045 call assert_equal(10, g:Xgetlist({'items':1}).items[0].lnum)
2046
Bram Moolenaara2aa8a22018-04-24 13:55:00 +02002047 " Try setting the items using a string
2048 call assert_equal(-1, g:Xsetlist([], ' ', {'items' : 'Test'}))
2049
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002050 " Save and restore the quickfix stack
2051 call g:Xsetlist([], 'f')
2052 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
2053 Xexpr "File1:10:Line1"
2054 Xexpr "File2:20:Line2"
2055 Xexpr "File3:30:Line3"
2056 let last_qf = g:Xgetlist({'nr':'$'}).nr
2057 call assert_equal(3, last_qf)
2058 let qstack = []
2059 for i in range(1, last_qf)
2060 let qstack = add(qstack, g:Xgetlist({'nr':i, 'all':1}))
2061 endfor
2062 call g:Xsetlist([], 'f')
2063 for i in range(len(qstack))
2064 call g:Xsetlist([], ' ', qstack[i])
2065 endfor
2066 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2067 call assert_equal(10, g:Xgetlist({'nr':1, 'items':1}).items[0].lnum)
2068 call assert_equal(20, g:Xgetlist({'nr':2, 'items':1}).items[0].lnum)
2069 call assert_equal(30, g:Xgetlist({'nr':3, 'items':1}).items[0].lnum)
2070 call g:Xsetlist([], 'f')
2071
2072 " Swap two quickfix lists
2073 Xexpr "File1:10:Line10"
2074 Xexpr "File2:20:Line20"
2075 Xexpr "File3:30:Line30"
2076 call g:Xsetlist([], 'r', {'nr':1,'title':'Colors','context':['Colors']})
2077 call g:Xsetlist([], 'r', {'nr':2,'title':'Fruits','context':['Fruits']})
2078 let l1=g:Xgetlist({'nr':1,'all':1})
2079 let l2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002080 let save_id = l1.id
2081 let l1.id=l2.id
2082 let l2.id=save_id
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002083 call g:Xsetlist([], 'r', l1)
2084 call g:Xsetlist([], 'r', l2)
2085 let newl1=g:Xgetlist({'nr':1,'all':1})
2086 let newl2=g:Xgetlist({'nr':2,'all':1})
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002087 call assert_equal('Fruits', newl1.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002088 call assert_equal(['Fruits'], newl1.context)
2089 call assert_equal('Line20', newl1.items[0].text)
Bram Moolenaarb4d5fba2017-09-11 19:31:28 +02002090 call assert_equal('Colors', newl2.title)
Bram Moolenaar6a8958d2017-06-22 21:33:20 +02002091 call assert_equal(['Colors'], newl2.context)
2092 call assert_equal('Line10', newl2.items[0].text)
2093 call g:Xsetlist([], 'f')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002094endfunc
Bram Moolenaard823fa92016-08-12 16:29:27 +02002095
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002096func Test_qf_property()
Bram Moolenaard823fa92016-08-12 16:29:27 +02002097 call Xproperty_tests('c')
2098 call Xproperty_tests('l')
Bram Moolenaaree85df32017-03-19 14:19:50 +01002099endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002100
Bram Moolenaar5b69c222019-01-11 14:50:06 +01002101" Test for setting the current index in the location/quickfix list
2102func Xtest_setqfidx(cchar)
2103 call s:setup_commands(a:cchar)
2104
2105 Xgetexpr "F1:10:1:Line1\nF2:20:2:Line2\nF3:30:3:Line3"
2106 Xgetexpr "F4:10:1:Line1\nF5:20:2:Line2\nF6:30:3:Line3"
2107 Xgetexpr "F7:10:1:Line1\nF8:20:2:Line2\nF9:30:3:Line3"
2108
2109 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 2})
2110 call g:Xsetlist([], 'a', {'nr' : 2, 'idx' : 2})
2111 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 3})
2112 Xolder 2
2113 Xopen
2114 call assert_equal(3, line('.'))
2115 Xnewer
2116 call assert_equal(2, line('.'))
2117 Xnewer
2118 call assert_equal(2, line('.'))
2119 " Update the current index with the quickfix window open
2120 wincmd w
2121 call g:Xsetlist([], 'a', {'nr' : 3, 'idx' : 3})
2122 Xopen
2123 call assert_equal(3, line('.'))
2124 Xclose
2125
2126 " Set the current index to the last entry
2127 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : '$'})
2128 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2129 " A large value should set the index to the last index
2130 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 1})
2131 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 999})
2132 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2133 " Invalid index values
2134 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : -1})
2135 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2136 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 0})
2137 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2138 call g:Xsetlist([], 'a', {'nr' : 1, 'idx' : 'xx'})
2139 call assert_equal(3, g:Xgetlist({'nr' : 1, 'idx' : 0}).idx)
2140 call assert_fails("call g:Xsetlist([], 'a', {'nr':1, 'idx':[]})", 'E745:')
2141
2142 call g:Xsetlist([], 'f')
2143 new | only
2144endfunc
2145
2146func Test_setqfidx()
2147 call Xtest_setqfidx('c')
2148 call Xtest_setqfidx('l')
2149endfunc
2150
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002151" Tests for the QuickFixCmdPre/QuickFixCmdPost autocommands
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002152func QfAutoCmdHandler(loc, cmd)
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002153 call add(g:acmds, a:loc . a:cmd)
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002154endfunc
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002155
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002156func Test_Autocmd()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002157 autocmd QuickFixCmdPre * call QfAutoCmdHandler('pre', expand('<amatch>'))
2158 autocmd QuickFixCmdPost * call QfAutoCmdHandler('post', expand('<amatch>'))
2159
2160 let g:acmds = []
2161 cexpr "F1:10:Line 10"
2162 caddexpr "F1:20:Line 20"
2163 cgetexpr "F1:30:Line 30"
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002164 cexpr ""
2165 caddexpr ""
2166 cgetexpr ""
2167 silent! cexpr non_existing_func()
2168 silent! caddexpr non_existing_func()
2169 silent! cgetexpr non_existing_func()
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002170 let l = ['precexpr',
2171 \ 'postcexpr',
2172 \ 'precaddexpr',
2173 \ 'postcaddexpr',
2174 \ 'precgetexpr',
2175 \ 'postcgetexpr',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002176 \ 'precexpr',
2177 \ 'postcexpr',
2178 \ 'precaddexpr',
2179 \ 'postcaddexpr',
2180 \ 'precgetexpr',
2181 \ 'postcgetexpr',
2182 \ 'precexpr',
2183 \ 'precaddexpr',
2184 \ 'precgetexpr']
2185 call assert_equal(l, g:acmds)
2186
2187 let g:acmds = []
2188 enew! | call append(0, "F2:10:Line 10")
2189 cbuffer!
2190 enew! | call append(0, "F2:20:Line 20")
2191 cgetbuffer
2192 enew! | call append(0, "F2:30:Line 30")
2193 caddbuffer
2194 new
2195 let bnum = bufnr('%')
2196 bunload
2197 exe 'silent! cbuffer! ' . bnum
2198 exe 'silent! cgetbuffer ' . bnum
2199 exe 'silent! caddbuffer ' . bnum
2200 enew!
2201 let l = ['precbuffer',
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002202 \ 'postcbuffer',
2203 \ 'precgetbuffer',
2204 \ 'postcgetbuffer',
2205 \ 'precaddbuffer',
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002206 \ 'postcaddbuffer',
2207 \ 'precbuffer',
2208 \ 'precgetbuffer',
2209 \ 'precaddbuffer']
Bram Moolenaar04c4ce62016-09-01 15:45:58 +02002210 call assert_equal(l, g:acmds)
Bram Moolenaar1ed22762017-11-28 18:03:44 +01002211
2212 call writefile(['Xtest:1:Line1'], 'Xtest')
2213 call writefile([], 'Xempty')
2214 let g:acmds = []
2215 cfile Xtest
2216 caddfile Xtest
2217 cgetfile Xtest
2218 cfile Xempty
2219 caddfile Xempty
2220 cgetfile Xempty
2221 silent! cfile do_not_exist
2222 silent! caddfile do_not_exist
2223 silent! cgetfile do_not_exist
2224 let l = ['precfile',
2225 \ 'postcfile',
2226 \ 'precaddfile',
2227 \ 'postcaddfile',
2228 \ 'precgetfile',
2229 \ 'postcgetfile',
2230 \ 'precfile',
2231 \ 'postcfile',
2232 \ 'precaddfile',
2233 \ 'postcaddfile',
2234 \ 'precgetfile',
2235 \ 'postcgetfile',
2236 \ 'precfile',
2237 \ 'postcfile',
2238 \ 'precaddfile',
2239 \ 'postcaddfile',
2240 \ 'precgetfile',
2241 \ 'postcgetfile']
2242 call assert_equal(l, g:acmds)
2243
2244 let g:acmds = []
2245 helpgrep quickfix
2246 silent! helpgrep non_existing_help_topic
2247 vimgrep test Xtest
2248 vimgrepadd test Xtest
2249 silent! vimgrep non_existing_test Xtest
2250 silent! vimgrepadd non_existing_test Xtest
2251 set makeprg=
2252 silent! make
2253 set makeprg&
2254 let l = ['prehelpgrep',
2255 \ 'posthelpgrep',
2256 \ 'prehelpgrep',
2257 \ 'posthelpgrep',
2258 \ 'previmgrep',
2259 \ 'postvimgrep',
2260 \ 'previmgrepadd',
2261 \ 'postvimgrepadd',
2262 \ 'previmgrep',
2263 \ 'postvimgrep',
2264 \ 'previmgrepadd',
2265 \ 'postvimgrepadd',
2266 \ 'premake',
2267 \ 'postmake']
2268 call assert_equal(l, g:acmds)
2269
2270 if has('unix')
2271 " Run this test only on Unix-like systems. The grepprg may not be set on
2272 " non-Unix systems.
2273 " The following lines are used for the grep test. Don't remove.
2274 " Grep_Autocmd_Text: Match 1
2275 " GrepAdd_Autocmd_Text: Match 2
2276 let g:acmds = []
2277 silent grep Grep_Autocmd_Text test_quickfix.vim
2278 silent grepadd GrepAdd_Autocmd_Text test_quickfix.vim
2279 silent grep abc123def Xtest
2280 silent grepadd abc123def Xtest
2281 let l = ['pregrep',
2282 \ 'postgrep',
2283 \ 'pregrepadd',
2284 \ 'postgrepadd',
2285 \ 'pregrep',
2286 \ 'postgrep',
2287 \ 'pregrepadd',
2288 \ 'postgrepadd']
2289 call assert_equal(l, g:acmds)
2290 endif
2291
2292 call delete('Xtest')
2293 call delete('Xempty')
Bram Moolenaarb254af32017-12-18 19:48:58 +01002294 au! QuickFixCmdPre
2295 au! QuickFixCmdPost
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002296endfunc
Bram Moolenaar21662be2016-11-06 14:46:44 +01002297
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002298func Test_Autocmd_Exception()
Bram Moolenaar21662be2016-11-06 14:46:44 +01002299 set efm=%m
2300 lgetexpr '?'
2301
2302 try
2303 call DoesNotExit()
2304 catch
2305 lgetexpr '1'
2306 finally
2307 lgetexpr '1'
2308 endtry
2309
2310 call assert_equal('1', getloclist(0)[0].text)
2311
2312 set efm&vim
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002313endfunc
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002314
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002315func Test_caddbuffer_wrong()
2316 " This used to cause a memory access in freed memory.
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002317 let save_efm = &efm
2318 set efm=%EEEE%m,%WWWW,%+CCCC%>%#,%GGGG%.#
2319 cgetexpr ['WWWW', 'EEEE', 'CCCC']
2320 let &efm = save_efm
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002321 caddbuffer
Bram Moolenaar63bed3d2016-11-12 15:36:54 +01002322 bwipe!
2323endfunc
Bram Moolenaar2b946c92016-11-12 18:14:44 +01002324
2325func Test_caddexpr_wrong()
2326 " This used to cause a memory access in freed memory.
2327 cbuffer
2328 cbuffer
2329 copen
2330 let save_efm = &efm
2331 set efm=%
2332 call assert_fails('caddexpr ""', 'E376:')
2333 let &efm = save_efm
2334endfunc
Bram Moolenaar7618e002016-11-13 15:09:26 +01002335
2336func Test_dirstack_cleanup()
2337 " This used to cause a memory access in freed memory.
2338 let save_efm = &efm
2339 lexpr '0'
2340 lopen
2341 fun X(c)
2342 let save_efm=&efm
2343 set efm=%D%f
2344 if a:c == 'c'
2345 caddexpr '::'
2346 else
2347 laddexpr ':0:0'
2348 endif
2349 let &efm=save_efm
2350 endfun
2351 call X('c')
2352 call X('l')
2353 call setqflist([], 'r')
2354 caddbuffer
2355 let &efm = save_efm
2356endfunc
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002357
2358" Tests for jumping to entries from the location list window and quickfix
2359" window
2360func Test_cwindow_jump()
2361 set efm=%f%%%l%%%m
2362 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2363 lopen | only
2364 lfirst
2365 call assert_true(winnr('$') == 2)
2366 call assert_true(winnr() == 1)
2367 " Location list for the new window should be set
2368 call assert_true(getloclist(0)[2].text == 'Line 30')
2369
2370 " Open a scratch buffer
2371 " Open a new window and create a location list
2372 " Open the location list window and close the other window
2373 " Jump to an entry.
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01002374 " Should create a new window and jump to the entry. The scratch buffer
Bram Moolenaar391b1dd2017-03-04 13:47:11 +01002375 " should not be used.
2376 enew | only
2377 set buftype=nofile
2378 below new
2379 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2380 lopen
2381 2wincmd c
2382 lnext
2383 call assert_true(winnr('$') == 3)
2384 call assert_true(winnr() == 2)
2385
2386 " Open two windows with two different location lists
2387 " Open the location list window and close the previous window
2388 " Jump to an entry in the location list window
2389 " Should open the file in the first window and not set the location list.
2390 enew | only
2391 lgetexpr ["F1%5%Line 5"]
2392 below new
2393 lgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2394 lopen
2395 2wincmd c
2396 lnext
2397 call assert_true(winnr() == 1)
2398 call assert_true(getloclist(0)[0].text == 'Line 5')
2399
2400 enew | only
2401 cgetexpr ["F1%10%Line 10", "F2%20%Line 20", "F3%30%Line 30"]
2402 copen
2403 cnext
2404 call assert_true(winnr('$') == 2)
2405 call assert_true(winnr() == 1)
2406
2407 enew | only
2408 set efm&vim
2409endfunc
Bram Moolenaaree85df32017-03-19 14:19:50 +01002410
2411func XvimgrepTests(cchar)
2412 call s:setup_commands(a:cchar)
2413
2414 call writefile(['Editor:VIM vim',
2415 \ 'Editor:Emacs EmAcS',
2416 \ 'Editor:Notepad NOTEPAD'], 'Xtestfile1')
2417 call writefile(['Linux', 'MacOS', 'MS-Windows'], 'Xtestfile2')
2418
2419 " Error cases
2420 call assert_fails('Xvimgrep /abc *', 'E682:')
2421
2422 let @/=''
2423 call assert_fails('Xvimgrep // *', 'E35:')
2424
2425 call assert_fails('Xvimgrep abc', 'E683:')
2426 call assert_fails('Xvimgrep a1b2c3 Xtestfile1', 'E480:')
2427 call assert_fails('Xvimgrep pat Xa1b2c3', 'E480:')
2428
2429 Xexpr ""
2430 Xvimgrepadd Notepad Xtestfile1
2431 Xvimgrepadd MacOS Xtestfile2
2432 let l = g:Xgetlist()
2433 call assert_equal(2, len(l))
2434 call assert_equal('Editor:Notepad NOTEPAD', l[0].text)
2435
2436 Xvimgrep #\cvim#g Xtestfile?
2437 let l = g:Xgetlist()
2438 call assert_equal(2, len(l))
2439 call assert_equal(8, l[0].col)
2440 call assert_equal(12, l[1].col)
2441
2442 1Xvimgrep ?Editor? Xtestfile*
2443 let l = g:Xgetlist()
2444 call assert_equal(1, len(l))
2445 call assert_equal('Editor:VIM vim', l[0].text)
2446
2447 edit +3 Xtestfile2
2448 Xvimgrep +\cemacs+j Xtestfile1
2449 let l = g:Xgetlist()
2450 call assert_equal('Xtestfile2', bufname(''))
2451 call assert_equal('Editor:Emacs EmAcS', l[0].text)
2452
Bram Moolenaar2225ebb2018-04-24 15:48:11 +02002453 " Test for unloading a buffer after vimgrep searched the buffer
2454 %bwipe
2455 Xvimgrep /Editor/j Xtestfile*
2456 call assert_equal(0, getbufinfo('Xtestfile1')[0].loaded)
2457 call assert_equal([], getbufinfo('Xtestfile2'))
2458
Bram Moolenaaree85df32017-03-19 14:19:50 +01002459 call delete('Xtestfile1')
2460 call delete('Xtestfile2')
2461endfunc
2462
2463" Tests for the :vimgrep command
2464func Test_vimgrep()
2465 call XvimgrepTests('c')
2466 call XvimgrepTests('l')
2467endfunc
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002468
Bram Moolenaar1c299432018-10-28 14:36:09 +01002469" Test for incsearch highlighting of the :vimgrep pattern
2470" This test used to cause "E315: ml_get: invalid lnum" errors.
2471func Test_vimgrep_incsearch()
2472 enew
2473 set incsearch
2474 call test_override("char_avail", 1)
2475
2476 call feedkeys(":2vimgrep assert test_quickfix.vim test_cdo.vim\<CR>", "ntx")
2477 let l = getqflist()
2478 call assert_equal(2, len(l))
2479
2480 call test_override("ALL", 0)
2481 set noincsearch
2482endfunc
2483
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002484func XfreeTests(cchar)
2485 call s:setup_commands(a:cchar)
2486
2487 enew | only
2488
2489 " Deleting the quickfix stack should work even When the current list is
2490 " somewhere in the middle of the stack
2491 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2492 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2493 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2494 Xolder
2495 call g:Xsetlist([], 'f')
2496 call assert_equal(0, len(g:Xgetlist()))
2497
2498 " After deleting the stack, adding a new list should create a stack with a
2499 " single list.
2500 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2501 call assert_equal(1, g:Xgetlist({'all':1}).nr)
2502
2503 " Deleting the stack from a quickfix window should update/clear the
2504 " quickfix/location list window.
2505 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2506 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2507 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2508 Xolder
2509 Xwindow
2510 call g:Xsetlist([], 'f')
2511 call assert_equal(2, winnr('$'))
2512 call assert_equal(1, line('$'))
2513 Xclose
2514
2515 " Deleting the stack from a non-quickfix window should update/clear the
2516 " quickfix/location list window.
2517 Xexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2518 Xexpr ['Xfile2:20:20:Line 20', 'Xfile2:25:25:Line 25']
2519 Xexpr ['Xfile3:30:30:Line 30', 'Xfile3:35:35:Line 35']
2520 Xolder
2521 Xwindow
2522 wincmd p
2523 call g:Xsetlist([], 'f')
2524 call assert_equal(0, len(g:Xgetlist()))
2525 wincmd p
2526 call assert_equal(2, winnr('$'))
2527 call assert_equal(1, line('$'))
2528
2529 " After deleting the location list stack, if the location list window is
2530 " opened, then a new location list should be created. So opening the
2531 " location list window again should not create a new window.
2532 if a:cchar == 'l'
2533 lexpr ['Xfile1:10:10:Line 10', 'Xfile1:15:15:Line 15']
2534 wincmd p
2535 lopen
2536 call assert_equal(2, winnr('$'))
2537 endif
2538 Xclose
2539endfunc
2540
Bram Moolenaar74240d32017-12-10 15:26:15 +01002541" Tests for the quickfix free functionality
Bram Moolenaar69f40be2017-04-02 15:15:49 +02002542func Test_qf_free()
2543 call XfreeTests('c')
2544 call XfreeTests('l')
2545endfunc
Bram Moolenaar6e62da32017-05-28 08:16:25 +02002546
2547" Test for buffer overflow when parsing lines and adding new entries to
2548" the quickfix list.
2549func Test_bufoverflow()
2550 set efm=%f:%l:%m
2551 cgetexpr ['File1:100:' . repeat('x', 1025)]
2552
2553 set efm=%+GCompiler:\ %.%#,%f:%l:%m
2554 cgetexpr ['Compiler: ' . repeat('a', 1015), 'File1:10:Hello World']
2555
2556 set efm=%DEntering\ directory\ %f,%f:%l:%m
2557 cgetexpr ['Entering directory ' . repeat('a', 1006),
2558 \ 'File1:10:Hello World']
2559 set efm&vim
2560endfunc
2561
Bram Moolenaar875feea2017-06-11 16:07:51 +02002562" Tests for getting the quickfix stack size
2563func XsizeTests(cchar)
2564 call s:setup_commands(a:cchar)
2565
2566 call g:Xsetlist([], 'f')
2567 call assert_equal(0, g:Xgetlist({'nr':'$'}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01002568 call assert_equal('', g:Xgetlist({'nr':'$', 'all':1}).title)
2569 call assert_equal(0, g:Xgetlist({'nr':0}).nr)
Bram Moolenaar875feea2017-06-11 16:07:51 +02002570
2571 Xexpr "File1:10:Line1"
2572 Xexpr "File2:20:Line2"
2573 Xexpr "File3:30:Line3"
2574 Xolder | Xolder
2575 call assert_equal(3, g:Xgetlist({'nr':'$'}).nr)
2576 call g:Xsetlist([], 'f')
2577
2578 Xexpr "File1:10:Line1"
2579 Xexpr "File2:20:Line2"
2580 Xexpr "File3:30:Line3"
2581 Xolder | Xolder
2582 call g:Xsetlist([], 'a', {'nr':'$', 'title':'Compiler'})
2583 call assert_equal('Compiler', g:Xgetlist({'nr':3, 'all':1}).title)
2584endfunc
2585
2586func Test_Qf_Size()
2587 call XsizeTests('c')
2588 call XsizeTests('l')
2589endfunc
Bram Moolenaar18141832017-06-25 21:17:25 +02002590
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002591func Test_cclose_from_copen()
2592 augroup QF_Test
2593 au!
2594 au FileType qf :call assert_fails(':cclose', 'E788')
2595 augroup END
2596 copen
2597 augroup QF_Test
2598 au!
2599 augroup END
2600 augroup! QF_Test
2601endfunc
2602
Bram Moolenaar18141832017-06-25 21:17:25 +02002603func Test_cclose_in_autocmd()
2604 " Problem is only triggered if "starting" is zero, so that the OptionsSet
2605 " event will be triggered.
2606 call test_override('starting', 1)
2607 augroup QF_Test
2608 au!
2609 au FileType qf :call assert_fails(':cclose', 'E788')
2610 augroup END
2611 copen
2612 augroup QF_Test
2613 au!
2614 augroup END
2615 augroup! QF_Test
2616 call test_override('starting', 0)
2617endfunc
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002618
Bram Moolenaar379fb762018-08-30 15:58:28 +02002619" Check that ":file" without an argument is possible even when "curbuf_lock"
2620" is set.
2621func Test_file_from_copen()
2622 " Works without argument.
2623 augroup QF_Test
2624 au!
2625 au FileType qf file
2626 augroup END
2627 copen
2628
2629 augroup QF_Test
2630 au!
2631 augroup END
2632 cclose
2633
2634 " Fails with argument.
2635 augroup QF_Test
2636 au!
2637 au FileType qf call assert_fails(':file foo', 'E788')
2638 augroup END
2639 copen
2640 augroup QF_Test
2641 au!
2642 augroup END
2643 cclose
2644
2645 augroup! QF_Test
2646endfunction
2647
Bram Moolenaar9c4feff2017-06-28 22:26:54 +02002648func Test_resize_from_copen()
2649 augroup QF_Test
2650 au!
2651 au FileType qf resize 5
2652 augroup END
2653 try
2654 " This should succeed without any exception. No other buffers are
2655 " involved in the autocmd.
2656 copen
2657 finally
2658 augroup QF_Test
2659 au!
2660 augroup END
2661 augroup! QF_Test
2662 endtry
2663endfunc
Bram Moolenaara8788f42017-07-19 17:06:20 +02002664
2665" Tests for the quickfix buffer b:changedtick variable
2666func Xchangedtick_tests(cchar)
2667 call s:setup_commands(a:cchar)
2668
2669 new | only
2670
2671 Xexpr "" | Xexpr "" | Xexpr ""
2672
2673 Xopen
2674 Xolder
2675 Xolder
2676 Xaddexpr "F1:10:Line10"
2677 Xaddexpr "F2:20:Line20"
2678 call g:Xsetlist([{"filename":"F3", "lnum":30, "text":"Line30"}], 'a')
2679 call g:Xsetlist([], 'f')
2680 call assert_equal(8, getbufvar('%', 'changedtick'))
2681 Xclose
2682endfunc
2683
2684func Test_changedtick()
Bram Moolenaarae338332017-08-11 20:25:26 +02002685 call Xchangedtick_tests('c')
2686 call Xchangedtick_tests('l')
2687endfunc
2688
2689" Tests for parsing an expression using setqflist()
2690func Xsetexpr_tests(cchar)
2691 call s:setup_commands(a:cchar)
2692
2693 let t = ["File1:10:Line10", "File1:20:Line20"]
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002694 call g:Xsetlist([], ' ', {'lines' : t})
2695 call g:Xsetlist([], 'a', {'lines' : ["File1:30:Line30"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002696
2697 let l = g:Xgetlist()
2698 call assert_equal(3, len(l))
2699 call assert_equal(20, l[1].lnum)
2700 call assert_equal('Line30', l[2].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002701 call g:Xsetlist([], 'r', {'lines' : ["File2:5:Line5"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002702 let l = g:Xgetlist()
2703 call assert_equal(1, len(l))
2704 call assert_equal('Line5', l[0].text)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002705 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : 10}))
2706 call assert_equal(-1, g:Xsetlist([], 'a', {'lines' : "F1:10:L10"}))
Bram Moolenaarae338332017-08-11 20:25:26 +02002707
2708 call g:Xsetlist([], 'f')
2709 " Add entries to multiple lists
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002710 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:10:Line10"]})
2711 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:20:Line20"]})
2712 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["File1:15:Line15"]})
2713 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["File2:25:Line25"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002714 call assert_equal('Line15', g:Xgetlist({'nr':1, 'items':1}).items[1].text)
2715 call assert_equal('Line25', g:Xgetlist({'nr':2, 'items':1}).items[1].text)
Bram Moolenaar36538222017-09-02 19:51:44 +02002716
2717 " Adding entries using a custom efm
2718 set efm&
2719 call g:Xsetlist([], ' ', {'efm' : '%f#%l#%m',
2720 \ 'lines' : ["F1#10#L10", "F2#20#L20"]})
2721 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
2722 call g:Xsetlist([], 'a', {'efm' : '%f#%l#%m', 'lines' : ["F3:30:L30"]})
2723 call assert_equal('F3:30:L30', g:Xgetlist({'items':1}).items[2].text)
2724 call assert_equal(20, g:Xgetlist({'items':1}).items[1].lnum)
2725 call assert_equal(-1, g:Xsetlist([], 'a', {'efm' : [],
2726 \ 'lines' : ['F1:10:L10']}))
Bram Moolenaarae338332017-08-11 20:25:26 +02002727endfunc
2728
2729func Test_setexpr()
2730 call Xsetexpr_tests('c')
2731 call Xsetexpr_tests('l')
2732endfunc
2733
2734" Tests for per quickfix/location list directory stack
2735func Xmultidirstack_tests(cchar)
2736 call s:setup_commands(a:cchar)
2737
2738 call g:Xsetlist([], 'f')
2739 Xexpr "" | Xexpr ""
2740
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002741 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["Entering dir 'Xone/a'"]})
2742 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["Entering dir 'Xtwo/a'"]})
2743 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["one.txt:3:one one one"]})
2744 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["two.txt:5:two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002745
2746 let l1 = g:Xgetlist({'nr':1, 'items':1})
2747 let l2 = g:Xgetlist({'nr':2, 'items':1})
2748 call assert_equal('Xone/a/one.txt', bufname(l1.items[1].bufnr))
2749 call assert_equal(3, l1.items[1].lnum)
2750 call assert_equal('Xtwo/a/two.txt', bufname(l2.items[1].bufnr))
2751 call assert_equal(5, l2.items[1].lnum)
2752endfunc
2753
2754func Test_multidirstack()
2755 call mkdir('Xone/a', 'p')
2756 call mkdir('Xtwo/a', 'p')
2757 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2758 call writefile(lines, 'Xone/a/one.txt')
2759 call writefile(lines, 'Xtwo/a/two.txt')
2760 let save_efm = &efm
2761 set efm=%DEntering\ dir\ '%f',%f:%l:%m,%XLeaving\ dir\ '%f'
2762
2763 call Xmultidirstack_tests('c')
2764 call Xmultidirstack_tests('l')
2765
2766 let &efm = save_efm
2767 call delete('Xone', 'rf')
2768 call delete('Xtwo', 'rf')
2769endfunc
2770
2771" Tests for per quickfix/location list file stack
2772func Xmultifilestack_tests(cchar)
2773 call s:setup_commands(a:cchar)
2774
2775 call g:Xsetlist([], 'f')
2776 Xexpr "" | Xexpr ""
2777
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002778 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["[one.txt]"]})
2779 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["[two.txt]"]})
2780 call g:Xsetlist([], 'a', {'nr' : 1, 'lines' : ["(3,5) one one one"]})
2781 call g:Xsetlist([], 'a', {'nr' : 2, 'lines' : ["(5,9) two two two"]})
Bram Moolenaarae338332017-08-11 20:25:26 +02002782
2783 let l1 = g:Xgetlist({'nr':1, 'items':1})
2784 let l2 = g:Xgetlist({'nr':2, 'items':1})
2785 call assert_equal('one.txt', bufname(l1.items[1].bufnr))
2786 call assert_equal(3, l1.items[1].lnum)
2787 call assert_equal('two.txt', bufname(l2.items[1].bufnr))
2788 call assert_equal(5, l2.items[1].lnum)
Bram Moolenaare333e792018-04-08 13:27:39 +02002789
2790 " Test for start of a new error line in the same line where a previous
2791 " error line ends with a file stack.
2792 let efm_val = 'Error\ l%l\ in\ %f,'
2793 let efm_val .= '%-P%>(%f%r,Error\ l%l\ in\ %m,%-Q)%r'
2794 let l = g:Xgetlist({'lines' : [
2795 \ '(one.txt',
2796 \ 'Error l4 in one.txt',
2797 \ ') (two.txt',
2798 \ 'Error l6 in two.txt',
2799 \ ')',
2800 \ 'Error l8 in one.txt'
2801 \ ], 'efm' : efm_val})
2802 call assert_equal(3, len(l.items))
2803 call assert_equal('one.txt', bufname(l.items[0].bufnr))
2804 call assert_equal(4, l.items[0].lnum)
2805 call assert_equal('one.txt', l.items[0].text)
2806 call assert_equal('two.txt', bufname(l.items[1].bufnr))
2807 call assert_equal(6, l.items[1].lnum)
2808 call assert_equal('two.txt', l.items[1].text)
2809 call assert_equal('one.txt', bufname(l.items[2].bufnr))
2810 call assert_equal(8, l.items[2].lnum)
2811 call assert_equal('', l.items[2].text)
Bram Moolenaarae338332017-08-11 20:25:26 +02002812endfunc
2813
2814func Test_multifilestack()
2815 let lines = ['1', '2', 'one one one', '4', 'two two two', '6', '7']
2816 call writefile(lines, 'one.txt')
2817 call writefile(lines, 'two.txt')
2818 let save_efm = &efm
2819 set efm=%+P[%f],(%l\\,%c)\ %m,%-Q
2820
2821 call Xmultifilestack_tests('c')
2822 call Xmultifilestack_tests('l')
2823
2824 let &efm = save_efm
2825 call delete('one.txt')
2826 call delete('two.txt')
2827endfunc
2828
2829" Tests for per buffer 'efm' setting
2830func Test_perbuf_efm()
2831 call writefile(["File1-10-Line10"], 'one.txt')
2832 call writefile(["File2#20#Line20"], 'two.txt')
2833 set efm=%f#%l#%m
2834 new | only
2835 new
2836 setlocal efm=%f-%l-%m
2837 cfile one.txt
2838 wincmd w
2839 caddfile two.txt
2840
2841 let l = getqflist()
2842 call assert_equal(10, l[0].lnum)
2843 call assert_equal('Line20', l[1].text)
2844
2845 set efm&
2846 new | only
2847 call delete('one.txt')
2848 call delete('two.txt')
Bram Moolenaara8788f42017-07-19 17:06:20 +02002849endfunc
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02002850
2851" Open multiple help windows using ":lhelpgrep
2852" This test used to crash Vim
2853func Test_Multi_LL_Help()
2854 new | only
2855 lhelpgrep window
2856 lopen
2857 e#
2858 lhelpgrep buffer
2859 call assert_equal(3, winnr('$'))
2860 call assert_true(len(getloclist(1)) != 0)
2861 call assert_true(len(getloclist(2)) != 0)
2862 new | only
2863endfunc
Bram Moolenaar55b69262017-08-13 13:42:01 +02002864
2865" Tests for adding new quickfix lists using setqflist()
2866func XaddQf_tests(cchar)
2867 call s:setup_commands(a:cchar)
2868
2869 " Create a new list using ' ' for action
2870 call g:Xsetlist([], 'f')
2871 call g:Xsetlist([], ' ', {'title' : 'Test1'})
2872 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2873 call assert_equal(1, l.nr)
2874 call assert_equal('Test1', l.title)
2875
2876 " Create a new list using ' ' for action and '$' for 'nr'
2877 call g:Xsetlist([], 'f')
2878 call g:Xsetlist([], ' ', {'title' : 'Test2', 'nr' : '$'})
2879 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2880 call assert_equal(1, l.nr)
2881 call assert_equal('Test2', l.title)
2882
2883 " Create a new list using 'a' for action
2884 call g:Xsetlist([], 'f')
2885 call g:Xsetlist([], 'a', {'title' : 'Test3'})
2886 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2887 call assert_equal(1, l.nr)
2888 call assert_equal('Test3', l.title)
2889
2890 " Create a new list using 'a' for action and '$' for 'nr'
2891 call g:Xsetlist([], 'f')
2892 call g:Xsetlist([], 'a', {'title' : 'Test3', 'nr' : '$'})
2893 call g:Xsetlist([], 'a', {'title' : 'Test4'})
2894 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2895 call assert_equal(1, l.nr)
2896 call assert_equal('Test4', l.title)
2897
2898 " Adding a quickfix list should remove all the lists following the current
2899 " list.
2900 Xexpr "" | Xexpr "" | Xexpr ""
2901 silent! 10Xolder
2902 call g:Xsetlist([], ' ', {'title' : 'Test5'})
2903 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2904 call assert_equal(2, l.nr)
2905 call assert_equal('Test5', l.title)
2906
2907 " Add a quickfix list using '$' as the list number.
2908 let lastqf = g:Xgetlist({'nr':'$'}).nr
2909 silent! 99Xolder
2910 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test6'})
2911 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2912 call assert_equal(lastqf + 1, l.nr)
2913 call assert_equal('Test6', l.title)
2914
2915 " Add a quickfix list using 'nr' set to one more than the quickfix
2916 " list size.
2917 let lastqf = g:Xgetlist({'nr':'$'}).nr
2918 silent! 99Xolder
2919 call g:Xsetlist([], ' ', {'nr' : lastqf + 1, 'title' : 'Test7'})
2920 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2921 call assert_equal(lastqf + 1, l.nr)
2922 call assert_equal('Test7', l.title)
2923
2924 " Add a quickfix list to a stack with 10 lists using 'nr' set to '$'
2925 exe repeat('Xexpr "" |', 9) . 'Xexpr ""'
2926 silent! 99Xolder
2927 call g:Xsetlist([], ' ', {'nr' : '$', 'title' : 'Test8'})
2928 let l = g:Xgetlist({'nr' : '$', 'all' : 1})
2929 call assert_equal(10, l.nr)
2930 call assert_equal('Test8', l.title)
2931
2932 " Add a quickfix list using 'nr' set to a value greater than 10
2933 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 12, 'title' : 'Test9'}))
2934
2935 " Try adding a quickfix list with 'nr' set to a value greater than the
2936 " quickfix list size but less than 10.
2937 call g:Xsetlist([], 'f')
2938 Xexpr "" | Xexpr "" | Xexpr ""
2939 silent! 99Xolder
2940 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : 8, 'title' : 'Test10'}))
2941
2942 " Add a quickfix list using 'nr' set to a some string or list
2943 call assert_equal(-1, g:Xsetlist([], ' ', {'nr' : [1,2], 'title' : 'Test11'}))
2944endfunc
2945
2946func Test_add_qf()
2947 call XaddQf_tests('c')
2948 call XaddQf_tests('l')
2949endfunc
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002950
2951" Test for getting the quickfix list items from some text without modifying
2952" the quickfix stack
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002953func XgetListFromLines(cchar)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002954 call s:setup_commands(a:cchar)
2955 call g:Xsetlist([], 'f')
2956
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002957 let l = g:Xgetlist({'lines' : ["File2:20:Line20", "File2:30:Line30"]}).items
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002958 call assert_equal(2, len(l))
2959 call assert_equal(30, l[1].lnum)
2960
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002961 call assert_equal({}, g:Xgetlist({'lines' : 10}))
2962 call assert_equal({}, g:Xgetlist({'lines' : 'File1:10:Line10'}))
2963 call assert_equal([], g:Xgetlist({'lines' : []}).items)
2964 call assert_equal([], g:Xgetlist({'lines' : [10, 20]}).items)
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002965
Bram Moolenaar36538222017-09-02 19:51:44 +02002966 " Parse text using a custom efm
2967 set efm&
2968 let l = g:Xgetlist({'lines':['File3#30#Line30'], 'efm' : '%f#%l#%m'}).items
2969 call assert_equal('Line30', l[0].text)
2970 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : '%f-%l-%m'}).items
2971 call assert_equal('File3:30:Line30', l[0].text)
2972 let l = g:Xgetlist({'lines':['File3:30:Line30'], 'efm' : [1,2]})
2973 call assert_equal({}, l)
2974 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':'%2'})", 'E376:')
2975 call assert_fails("call g:Xgetlist({'lines':['abc'], 'efm':''})", 'E378:')
2976
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002977 " Make sure that the quickfix stack is not modified
2978 call assert_equal(0, g:Xgetlist({'nr' : '$'}).nr)
2979endfunc
2980
Bram Moolenaar2c809b72017-09-01 18:34:02 +02002981func Test_get_list_from_lines()
2982 call XgetListFromLines('c')
2983 call XgetListFromLines('l')
Bram Moolenaar7adf06f2017-08-27 15:23:41 +02002984endfunc
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002985
2986" Tests for the quickfix list id
2987func Xqfid_tests(cchar)
2988 call s:setup_commands(a:cchar)
2989
2990 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01002991 call assert_equal(0, g:Xgetlist({'id':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02002992 Xexpr ''
2993 let start_id = g:Xgetlist({'id' : 0}).id
2994 Xexpr '' | Xexpr ''
2995 Xolder
2996 call assert_equal(start_id, g:Xgetlist({'id':0, 'nr':1}).id)
2997 call assert_equal(start_id + 1, g:Xgetlist({'id':0, 'nr':0}).id)
2998 call assert_equal(start_id + 2, g:Xgetlist({'id':0, 'nr':'$'}).id)
Bram Moolenaara6d48492017-12-12 22:45:31 +01002999 call assert_equal(0, g:Xgetlist({'id':0, 'nr':99}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003000 call assert_equal(2, g:Xgetlist({'id':start_id + 1, 'nr':0}).nr)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003001 call assert_equal(0, g:Xgetlist({'id':99, 'nr':0}).id)
3002 call assert_equal(0, g:Xgetlist({'id':"abc", 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003003
3004 call g:Xsetlist([], 'a', {'id':start_id, 'context':[1,2]})
3005 call assert_equal([1,2], g:Xgetlist({'nr':1, 'context':1}).context)
Bram Moolenaar2c809b72017-09-01 18:34:02 +02003006 call g:Xsetlist([], 'a', {'id':start_id+1, 'lines':['F1:10:L10']})
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003007 call assert_equal('L10', g:Xgetlist({'nr':2, 'items':1}).items[0].text)
3008 call assert_equal(-1, g:Xsetlist([], 'a', {'id':999, 'title':'Vim'}))
3009 call assert_equal(-1, g:Xsetlist([], 'a', {'id':'abc', 'title':'Vim'}))
3010
3011 let qfid = g:Xgetlist({'id':0, 'nr':0})
3012 call g:Xsetlist([], 'f')
Bram Moolenaara6d48492017-12-12 22:45:31 +01003013 call assert_equal(0, g:Xgetlist({'id':qfid, 'nr':0}).id)
Bram Moolenaara539f4f2017-08-30 20:33:55 +02003014endfunc
3015
3016func Test_qf_id()
3017 call Xqfid_tests('c')
3018 call Xqfid_tests('l')
3019endfunc
Bram Moolenaar74240d32017-12-10 15:26:15 +01003020
3021func Xqfjump_tests(cchar)
3022 call s:setup_commands(a:cchar)
3023
3024 call writefile(["Line1\tFoo", "Line2"], 'F1')
3025 call writefile(["Line1\tBar", "Line2"], 'F2')
3026 call writefile(["Line1\tBaz", "Line2"], 'F3')
3027
3028 call g:Xsetlist([], 'f')
3029
3030 " Tests for
3031 " Jumping to a line using a pattern
3032 " Jumping to a column greater than the last column in a line
3033 " Jumping to a line greater than the last line in the file
3034 let l = []
3035 for i in range(1, 7)
3036 call add(l, {})
3037 endfor
3038 let l[0].filename='F1'
3039 let l[0].pattern='Line1'
3040 let l[1].filename='F2'
3041 let l[1].pattern='Line1'
3042 let l[2].filename='F3'
3043 let l[2].pattern='Line1'
3044 let l[3].filename='F3'
3045 let l[3].lnum=1
3046 let l[3].col=9
3047 let l[3].vcol=1
3048 let l[4].filename='F3'
3049 let l[4].lnum=99
3050 let l[5].filename='F3'
3051 let l[5].lnum=1
3052 let l[5].col=99
3053 let l[5].vcol=1
3054 let l[6].filename='F3'
3055 let l[6].pattern='abcxyz'
3056
3057 call g:Xsetlist([], ' ', {'items' : l})
3058 Xopen | only
3059 2Xnext
3060 call assert_equal(3, g:Xgetlist({'idx' : 0}).idx)
3061 call assert_equal('F3', bufname('%'))
3062 Xnext
3063 call assert_equal(7, col('.'))
3064 Xnext
3065 call assert_equal(2, line('.'))
3066 Xnext
3067 call assert_equal(9, col('.'))
3068 2
3069 Xnext
3070 call assert_equal(2, line('.'))
3071
3072 if a:cchar == 'l'
3073 " When jumping to a location list entry in the location list window and
3074 " no usable windows are available, then a new window should be opened.
3075 enew! | new | only
3076 call g:Xsetlist([], 'f')
3077 setlocal buftype=nofile
3078 new
3079 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']})
3080 Xopen
3081 let winid = win_getid()
3082 wincmd p
3083 close
3084 call win_gotoid(winid)
3085 Xnext
3086 call assert_equal(3, winnr('$'))
3087 call assert_equal(1, winnr())
3088 call assert_equal(2, line('.'))
3089
3090 " When jumping to an entry in the location list window and the window
3091 " associated with the location list is not present and a window containing
3092 " the file is already present, then that window should be used.
3093 close
3094 belowright new
3095 call g:Xsetlist([], 'f')
3096 edit F3
3097 call win_gotoid(winid)
3098 Xlast
3099 call assert_equal(3, winnr())
3100 call assert_equal(6, g:Xgetlist({'size' : 1}).size)
3101 call assert_equal(winid, g:Xgetlist({'winid' : 1}).winid)
3102 endif
3103
3104 " Cleanup
3105 enew!
3106 new | only
3107
3108 call delete('F1')
3109 call delete('F2')
3110 call delete('F3')
3111endfunc
3112
3113func Test_qfjump()
3114 call Xqfjump_tests('c')
3115 call Xqfjump_tests('l')
3116endfunc
Bram Moolenaara6d48492017-12-12 22:45:31 +01003117
3118" Tests for the getqflist() and getloclist() functions when the list is not
3119" present or is empty
3120func Xgetlist_empty_tests(cchar)
3121 call s:setup_commands(a:cchar)
3122
3123 " Empty quickfix stack
3124 call g:Xsetlist([], 'f')
3125 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3126 call assert_equal(0, g:Xgetlist({'id' : 0}).id)
3127 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3128 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3129 call assert_equal(0, g:Xgetlist({'nr' : 0}).nr)
3130 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3131 call assert_equal('', g:Xgetlist({'title' : 0}).title)
3132 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003133 call assert_equal(0, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003134 if a:cchar == 'c'
3135 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003136 \ 'items' : [], 'nr' : 0, 'size' : 0, 'qfbufnr' : 0,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003137 \ 'title' : '', 'winid' : 0, 'changedtick': 0},
3138 \ g:Xgetlist({'all' : 0}))
3139 else
3140 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0,
3141 \ 'items' : [], 'nr' : 0, 'size' : 0, 'title' : '',
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003142 \ 'winid' : 0, 'changedtick': 0, 'filewinid' : 0,
3143 \ 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003144 \ g:Xgetlist({'all' : 0}))
3145 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003146
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003147 " Quickfix window with empty stack
3148 silent! Xopen
3149 let qfwinid = (a:cchar == 'c') ? win_getid() : 0
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003150 let qfbufnr = (a:cchar == 'c') ? bufnr('') : 0
Bram Moolenaar2ec364e2018-01-27 11:52:13 +01003151 call assert_equal(qfwinid, g:Xgetlist({'winid' : 0}).winid)
3152 Xclose
3153
Bram Moolenaara6d48492017-12-12 22:45:31 +01003154 " Empty quickfix list
3155 Xexpr ""
3156 call assert_equal('', g:Xgetlist({'context' : 0}).context)
3157 call assert_notequal(0, g:Xgetlist({'id' : 0}).id)
3158 call assert_equal(0, g:Xgetlist({'idx' : 0}).idx)
3159 call assert_equal([], g:Xgetlist({'items' : 0}).items)
3160 call assert_notequal(0, g:Xgetlist({'nr' : 0}).nr)
3161 call assert_equal(0, g:Xgetlist({'size' : 0}).size)
3162 call assert_notequal('', g:Xgetlist({'title' : 0}).title)
3163 call assert_equal(0, g:Xgetlist({'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003164 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
Bram Moolenaara6d48492017-12-12 22:45:31 +01003165
3166 let qfid = g:Xgetlist({'id' : 0}).id
3167 call g:Xsetlist([], 'f')
3168
3169 " Non-existing quickfix identifier
3170 call assert_equal('', g:Xgetlist({'id' : qfid, 'context' : 0}).context)
3171 call assert_equal(0, g:Xgetlist({'id' : qfid}).id)
3172 call assert_equal(0, g:Xgetlist({'id' : qfid, 'idx' : 0}).idx)
3173 call assert_equal([], g:Xgetlist({'id' : qfid, 'items' : 0}).items)
3174 call assert_equal(0, g:Xgetlist({'id' : qfid, 'nr' : 0}).nr)
3175 call assert_equal(0, g:Xgetlist({'id' : qfid, 'size' : 0}).size)
3176 call assert_equal('', g:Xgetlist({'id' : qfid, 'title' : 0}).title)
3177 call assert_equal(0, g:Xgetlist({'id' : qfid, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003178 call assert_equal(0, g:Xgetlist({'id' : qfid, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003179 if a:cchar == 'c'
3180 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3181 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003182 \ 'qfbufnr' : qfbufnr,
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003183 \ 'changedtick' : 0}, g:Xgetlist({'id' : qfid, 'all' : 0}))
3184 else
3185 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3186 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003187 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003188 \ g:Xgetlist({'id' : qfid, 'all' : 0}))
3189 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003190
3191 " Non-existing quickfix list number
3192 call assert_equal('', g:Xgetlist({'nr' : 5, 'context' : 0}).context)
3193 call assert_equal(0, g:Xgetlist({'nr' : 5}).nr)
3194 call assert_equal(0, g:Xgetlist({'nr' : 5, 'idx' : 0}).idx)
3195 call assert_equal([], g:Xgetlist({'nr' : 5, 'items' : 0}).items)
3196 call assert_equal(0, g:Xgetlist({'nr' : 5, 'id' : 0}).id)
3197 call assert_equal(0, g:Xgetlist({'nr' : 5, 'size' : 0}).size)
3198 call assert_equal('', g:Xgetlist({'nr' : 5, 'title' : 0}).title)
3199 call assert_equal(0, g:Xgetlist({'nr' : 5, 'winid' : 0}).winid)
Bram Moolenaarb254af32017-12-18 19:48:58 +01003200 call assert_equal(0, g:Xgetlist({'nr' : 5, 'changedtick' : 0}).changedtick)
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003201 if a:cchar == 'c'
3202 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3203 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003204 \ 'changedtick' : 0, 'qfbufnr' : qfbufnr},
3205 \ g:Xgetlist({'nr' : 5, 'all' : 0}))
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003206 else
3207 call assert_equal({'context' : '', 'id' : 0, 'idx' : 0, 'items' : [],
3208 \ 'nr' : 0, 'size' : 0, 'title' : '', 'winid' : 0,
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003209 \ 'changedtick' : 0, 'filewinid' : 0, 'qfbufnr' : 0},
Bram Moolenaarc9cc9c72018-09-02 15:18:42 +02003210 \ g:Xgetlist({'nr' : 5, 'all' : 0}))
3211 endif
Bram Moolenaara6d48492017-12-12 22:45:31 +01003212endfunc
3213
3214func Test_getqflist()
3215 call Xgetlist_empty_tests('c')
3216 call Xgetlist_empty_tests('l')
3217endfunc
Bram Moolenaarb254af32017-12-18 19:48:58 +01003218
Bram Moolenaara0ca7d02017-12-19 10:22:19 +01003219func Test_getqflist_invalid_nr()
3220 " The following commands used to crash Vim
3221 cexpr ""
3222 call getqflist({'nr' : $XXX_DOES_NOT_EXIST_XXX})
3223
3224 " Cleanup
3225 call setqflist([], 'r')
3226endfunc
3227
Bram Moolenaarb254af32017-12-18 19:48:58 +01003228" Tests for the quickfix/location list changedtick
3229func Xqftick_tests(cchar)
3230 call s:setup_commands(a:cchar)
3231
3232 call g:Xsetlist([], 'f')
3233
3234 Xexpr "F1:10:Line10"
3235 let qfid = g:Xgetlist({'id' : 0}).id
3236 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3237 Xaddexpr "F2:20:Line20\nF2:21:Line21"
3238 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3239 call g:Xsetlist([], 'a', {'lines' : ["F3:30:Line30", "F3:31:Line31"]})
3240 call assert_equal(3, g:Xgetlist({'changedtick' : 0}).changedtick)
3241 call g:Xsetlist([], 'r', {'lines' : ["F4:40:Line40"]})
3242 call assert_equal(4, g:Xgetlist({'changedtick' : 0}).changedtick)
3243 call g:Xsetlist([], 'a', {'title' : 'New Title'})
3244 call assert_equal(5, g:Xgetlist({'changedtick' : 0}).changedtick)
3245
3246 enew!
3247 call append(0, ["F5:50:L50", "F6:60:L60"])
3248 Xaddbuffer
3249 call assert_equal(6, g:Xgetlist({'changedtick' : 0}).changedtick)
3250 enew!
3251
3252 call g:Xsetlist([], 'a', {'context' : {'bus' : 'pci'}})
3253 call assert_equal(7, g:Xgetlist({'changedtick' : 0}).changedtick)
3254 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3255 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'a')
3256 call assert_equal(8, g:Xgetlist({'changedtick' : 0}).changedtick)
3257 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3258 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], ' ')
3259 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3260 call g:Xsetlist([{'filename' : 'F7', 'lnum' : 10, 'text' : 'L7'},
3261 \ {'filename' : 'F7', 'lnum' : 11, 'text' : 'L11'}], 'r')
3262 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3263
3264 call writefile(["F8:80:L80", "F8:81:L81"], "Xone")
3265 Xfile Xone
3266 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3267 Xaddfile Xone
3268 call assert_equal(2, g:Xgetlist({'changedtick' : 0}).changedtick)
3269
3270 " Test case for updating a non-current quickfix list
3271 call g:Xsetlist([], 'f')
3272 Xexpr "F1:1:L1"
3273 Xexpr "F2:2:L2"
3274 call g:Xsetlist([], 'a', {'nr' : 1, "lines" : ["F10:10:L10"]})
3275 call assert_equal(1, g:Xgetlist({'changedtick' : 0}).changedtick)
3276 call assert_equal(2, g:Xgetlist({'nr' : 1, 'changedtick' : 0}).changedtick)
3277
3278 call delete("Xone")
3279endfunc
3280
3281func Test_qf_tick()
3282 call Xqftick_tests('c')
3283 call Xqftick_tests('l')
3284endfunc
Bram Moolenaar12237442017-12-19 12:38:52 +01003285
Bram Moolenaarc631f2d2018-08-21 21:58:13 +02003286" Test helpgrep with lang specifier
3287func Xtest_helpgrep_with_lang_specifier(cchar)
3288 call s:setup_commands(a:cchar)
3289 Xhelpgrep Vim@en
3290 call assert_equal('help', &filetype)
3291 call assert_notequal(0, g:Xgetlist({'nr' : '$'}).nr)
3292 new | only
3293endfunc
3294
3295func Test_helpgrep_with_lang_specifier()
3296 call Xtest_helpgrep_with_lang_specifier('c')
3297 call Xtest_helpgrep_with_lang_specifier('l')
3298endfunc
3299
Bram Moolenaar12237442017-12-19 12:38:52 +01003300" The following test used to crash Vim.
3301" Open the location list window and close the regular window associated with
3302" the location list. When the garbage collection runs now, it incorrectly
3303" marks the location list context as not in use and frees the context.
3304func Test_ll_window_ctx()
3305 call setloclist(0, [], 'f')
3306 call setloclist(0, [], 'a', {'context' : []})
3307 lopen | only
3308 call test_garbagecollect_now()
3309 echo getloclist(0, {'context' : 1}).context
3310 enew | only
3311endfunc
3312
Bram Moolenaar14a4deb2017-12-19 16:48:55 +01003313" The following test used to crash vim
3314func Test_lfile_crash()
3315 sp Xtest
3316 au QuickFixCmdPre * bw
3317 call assert_fails('lfile', 'E40')
3318 au! QuickFixCmdPre
3319endfunc
Bram Moolenaar3c097222017-12-21 20:54:49 +01003320
3321" The following test used to crash vim
3322func Test_lbuffer_crash()
3323 sv Xtest
3324 augroup QF_Test
3325 au!
3326 au * * bw
3327 augroup END
3328 lbuffer
3329 augroup QF_Test
3330 au!
3331 augroup END
3332endfunc
3333
3334" The following test used to crash vim
3335func Test_lexpr_crash()
3336 augroup QF_Test
3337 au!
3338 au * * call setloclist(0, [], 'f')
3339 augroup END
3340 lexpr ""
3341 augroup QF_Test
3342 au!
3343 augroup END
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003344
Bram Moolenaar3c097222017-12-21 20:54:49 +01003345 enew | only
Bram Moolenaar9f84ded2018-10-20 20:54:02 +02003346 augroup QF_Test
3347 au!
3348 au BufNew * call setloclist(0, [], 'f')
3349 augroup END
3350 lexpr 'x:1:x'
3351 augroup QF_Test
3352 au!
3353 augroup END
3354
3355 enew | only
3356 lexpr ''
3357 lopen
3358 augroup QF_Test
3359 au!
3360 au FileType * call setloclist(0, [], 'f')
3361 augroup END
3362 lexpr ''
3363 augroup QF_Test
3364 au!
3365 augroup END
Bram Moolenaar3c097222017-12-21 20:54:49 +01003366endfunc
3367
3368" The following test used to crash Vim
3369func Test_lvimgrep_crash()
3370 sv Xtest
3371 augroup QF_Test
3372 au!
3373 au * * call setloclist(0, [], 'f')
3374 augroup END
3375 lvimgrep quickfix test_quickfix.vim
3376 augroup QF_Test
3377 au!
3378 augroup END
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003379
3380 new | only
3381 augroup QF_Test
3382 au!
3383 au BufEnter * call setloclist(0, [], 'r')
3384 augroup END
3385 call assert_fails('lvimgrep Test_lvimgrep_crash *', 'E926:')
3386 augroup QF_Test
3387 au!
3388 augroup END
3389
Bram Moolenaar3c097222017-12-21 20:54:49 +01003390 enew | only
3391endfunc
Bram Moolenaarde046542017-12-26 13:53:11 +01003392
3393" Test for the position of the quickfix and location list window
3394func Test_qfwin_pos()
3395 " Open two windows
3396 new | only
3397 new
3398 cexpr ['F1:10:L10']
3399 copen
3400 " Quickfix window should be the bottom most window
3401 call assert_equal(3, winnr())
3402 close
3403 " Open at the very top
3404 wincmd t
3405 topleft copen
3406 call assert_equal(1, winnr())
3407 close
3408 " open left of the current window
3409 wincmd t
3410 below new
3411 leftabove copen
3412 call assert_equal(2, winnr())
3413 close
3414 " open right of the current window
3415 rightbelow copen
3416 call assert_equal(3, winnr())
3417 close
3418endfunc
Bram Moolenaare1bb8792018-04-06 22:58:23 +02003419
3420" Tests for quickfix/location lists changed by autocommands when
3421" :vimgrep/:lvimgrep commands are running.
3422func Test_vimgrep_autocmd()
3423 call setqflist([], 'f')
3424 call writefile(['stars'], 'Xtest1.txt')
3425 call writefile(['stars'], 'Xtest2.txt')
3426
3427 " Test 1:
3428 " When searching for a pattern using :vimgrep, if the quickfix list is
3429 " changed by an autocmd, the results should be added to the correct quickfix
3430 " list.
3431 autocmd BufRead Xtest2.txt cexpr '' | cexpr ''
3432 silent vimgrep stars Xtest*.txt
3433 call assert_equal(1, getqflist({'nr' : 0}).nr)
3434 call assert_equal(3, getqflist({'nr' : '$'}).nr)
3435 call assert_equal('Xtest2.txt', bufname(getqflist()[1].bufnr))
3436 au! BufRead Xtest2.txt
3437
3438 " Test 2:
3439 " When searching for a pattern using :vimgrep, if the quickfix list is
3440 " freed, then a error should be given.
3441 silent! %bwipe!
3442 call setqflist([], 'f')
3443 autocmd BufRead Xtest2.txt for i in range(10) | cexpr '' | endfor
3444 call assert_fails('vimgrep stars Xtest*.txt', 'E925:')
3445 au! BufRead Xtest2.txt
3446
3447 " Test 3:
3448 " When searching for a pattern using :lvimgrep, if the location list is
3449 " freed, then the command should error out.
3450 silent! %bwipe!
3451 let g:save_winid = win_getid()
3452 autocmd BufRead Xtest2.txt call setloclist(g:save_winid, [], 'f')
3453 call assert_fails('lvimgrep stars Xtest*.txt', 'E926:')
3454 au! BufRead Xtest2.txt
3455
3456 call delete('Xtest1.txt')
3457 call delete('Xtest2.txt')
3458 call setqflist([], 'f')
3459endfunc
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003460
3461" The following test used to crash Vim
3462func Test_lhelpgrep_autocmd()
3463 lhelpgrep quickfix
3464 autocmd QuickFixCmdPost * call setloclist(0, [], 'f')
3465 lhelpgrep buffer
3466 call assert_equal('help', &filetype)
3467 call assert_equal(0, getloclist(0, {'nr' : '$'}).nr)
3468 lhelpgrep tabpage
3469 call assert_equal('help', &filetype)
3470 call assert_equal(1, getloclist(0, {'nr' : '$'}).nr)
3471 au! QuickFixCmdPost
Bram Moolenaarb6f14802018-10-21 18:47:43 +02003472
3473 new | only
3474 augroup QF_Test
3475 au!
3476 au BufEnter * call setqflist([], 'f')
3477 augroup END
3478 call assert_fails('helpgrep quickfix', 'E925:')
3479 augroup QF_Test
3480 au! BufEnter
3481 augroup END
3482
3483 new | only
3484 augroup QF_Test
3485 au!
3486 au BufEnter * call setqflist([], 'r')
3487 augroup END
3488 call assert_fails('helpgrep quickfix', 'E925:')
3489 augroup QF_Test
3490 au! BufEnter
3491 augroup END
3492
3493 new | only
3494 augroup QF_Test
3495 au!
3496 au BufEnter * call setloclist(0, [], 'r')
3497 augroup END
3498 call assert_fails('lhelpgrep quickfix', 'E926:')
3499 augroup QF_Test
3500 au! BufEnter
3501 augroup END
3502
Bram Moolenaar3b9474b2018-04-23 21:29:48 +02003503 new | only
3504endfunc
Bram Moolenaara796d462018-05-01 14:30:36 +02003505
3506" Test for shortening/simplifying the file name when opening the
3507" quickfix window or when displaying the quickfix list
3508func Test_shorten_fname()
3509 if !has('unix')
3510 return
3511 endif
3512 %bwipe
3513 " Create a quickfix list with a absolute path filename
3514 let fname = getcwd() . '/test_quickfix.vim'
3515 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
3516 call assert_equal(fname, bufname('test_quickfix.vim'))
3517 " Opening the quickfix window should simplify the file path
3518 cwindow
3519 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
3520 cclose
3521 %bwipe
3522 " Create a quickfix list with a absolute path filename
3523 call setqflist([], ' ', {'lines':[fname . ":20:Line20"], 'efm':'%f:%l:%m'})
3524 call assert_equal(fname, bufname('test_quickfix.vim'))
3525 " Displaying the quickfix list should simplify the file path
3526 silent! clist
3527 call assert_equal('test_quickfix.vim', bufname('test_quickfix.vim'))
3528endfunc
Bram Moolenaar8b62e312018-05-13 15:29:04 +02003529
3530" Quickfix title tests
3531" In the below tests, 'exe "cmd"' is used to invoke the quickfix commands.
3532" Otherwise due to indentation, the title is set with spaces at the beginning
3533" of the command.
3534func Test_qftitle()
3535 call writefile(["F1:1:Line1"], 'Xerr')
3536
3537 " :cexpr
3538 exe "cexpr readfile('Xerr')"
3539 call assert_equal(":cexpr readfile('Xerr')", getqflist({'title' : 1}).title)
3540
3541 " :cgetexpr
3542 exe "cgetexpr readfile('Xerr')"
3543 call assert_equal(":cgetexpr readfile('Xerr')",
3544 \ getqflist({'title' : 1}).title)
3545
3546 " :caddexpr
3547 call setqflist([], 'f')
3548 exe "caddexpr readfile('Xerr')"
3549 call assert_equal(":caddexpr readfile('Xerr')",
3550 \ getqflist({'title' : 1}).title)
3551
3552 " :cbuffer
3553 new Xerr
3554 exe "cbuffer"
3555 call assert_equal(':cbuffer (Xerr)', getqflist({'title' : 1}).title)
3556
3557 " :cgetbuffer
3558 edit Xerr
3559 exe "cgetbuffer"
3560 call assert_equal(':cgetbuffer (Xerr)', getqflist({'title' : 1}).title)
3561
3562 " :caddbuffer
3563 call setqflist([], 'f')
3564 edit Xerr
3565 exe "caddbuffer"
3566 call assert_equal(':caddbuffer (Xerr)', getqflist({'title' : 1}).title)
3567
3568 " :cfile
3569 exe "cfile Xerr"
3570 call assert_equal(':cfile Xerr', getqflist({'title' : 1}).title)
3571
3572 " :cgetfile
3573 exe "cgetfile Xerr"
3574 call assert_equal(':cgetfile Xerr', getqflist({'title' : 1}).title)
3575
3576 " :caddfile
3577 call setqflist([], 'f')
3578 exe "caddfile Xerr"
3579 call assert_equal(':caddfile Xerr', getqflist({'title' : 1}).title)
3580
3581 " :grep
3582 set grepprg=internal
3583 exe "grep F1 Xerr"
3584 call assert_equal(':grep F1 Xerr', getqflist({'title' : 1}).title)
3585
3586 " :grepadd
3587 call setqflist([], 'f')
3588 exe "grepadd F1 Xerr"
3589 call assert_equal(':grepadd F1 Xerr', getqflist({'title' : 1}).title)
3590 set grepprg&vim
3591
3592 " :vimgrep
3593 exe "vimgrep F1 Xerr"
3594 call assert_equal(':vimgrep F1 Xerr', getqflist({'title' : 1}).title)
3595
3596 " :vimgrepadd
3597 call setqflist([], 'f')
3598 exe "vimgrepadd F1 Xerr"
3599 call assert_equal(':vimgrepadd F1 Xerr', getqflist({'title' : 1}).title)
3600
3601 call setqflist(['F1:10:L10'], ' ')
3602 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3603
3604 call setqflist([], 'f')
3605 call setqflist(['F1:10:L10'], 'a')
3606 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3607
3608 call setqflist([], 'f')
3609 call setqflist(['F1:10:L10'], 'r')
3610 call assert_equal(':setqflist()', getqflist({'title' : 1}).title)
3611
3612 close
3613 call delete('Xerr')
3614
3615 call setqflist([], ' ', {'title' : 'Errors'})
3616 copen
3617 call assert_equal('Errors', w:quickfix_title)
3618 call setqflist([], 'r', {'items' : [{'filename' : 'a.c', 'lnum' : 10}]})
3619 call assert_equal('Errors', w:quickfix_title)
3620 cclose
3621endfunc
Bram Moolenaar600323b2018-06-16 22:16:47 +02003622
3623func Test_lbuffer_with_bwipe()
3624 new
3625 new
3626 augroup nasty
3627 au * * bwipe
3628 augroup END
3629 lbuffer
3630 augroup nasty
3631 au!
3632 augroup END
3633endfunc
Bram Moolenaar0366c012018-06-18 20:52:13 +02003634
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003635" Test for an autocmd freeing the quickfix/location list when cexpr/lexpr is
3636" running
3637func Xexpr_acmd_freelist(cchar)
3638 call s:setup_commands(a:cchar)
3639
Bram Moolenaar0366c012018-06-18 20:52:13 +02003640 " This was using freed memory.
3641 augroup nasty
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003642 au * * call g:Xsetlist([], 'f')
Bram Moolenaar0366c012018-06-18 20:52:13 +02003643 augroup END
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003644 Xexpr "x"
Bram Moolenaar0366c012018-06-18 20:52:13 +02003645 augroup nasty
3646 au!
3647 augroup END
3648endfunc
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003649
3650func Test_cexpr_acmd_freelist()
3651 call Xexpr_acmd_freelist('c')
3652 call Xexpr_acmd_freelist('l')
3653endfunc
3654
3655" Test for commands that create a new quickfix/location list and jump to the
3656" first error automatically.
3657func Xjumpto_first_error_test(cchar)
3658 call s:setup_commands(a:cchar)
3659
3660 call s:create_test_file('Xtestfile1')
3661 call s:create_test_file('Xtestfile2')
3662 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
3663
3664 " Test for cexpr/lexpr
3665 enew
3666 Xexpr l
3667 call assert_equal('Xtestfile1', bufname(''))
3668 call assert_equal(2, line('.'))
3669
3670 " Test for cfile/lfile
3671 enew
3672 call writefile(l, 'Xerr')
3673 Xfile Xerr
3674 call assert_equal('Xtestfile1', bufname(''))
3675 call assert_equal(2, line('.'))
3676
3677 " Test for cbuffer/lbuffer
3678 edit Xerr
3679 Xbuffer
3680 call assert_equal('Xtestfile1', bufname(''))
3681 call assert_equal(2, line('.'))
3682
3683 call delete('Xerr')
3684 call delete('Xtestfile1')
3685 call delete('Xtestfile2')
3686endfunc
3687
3688func Test_jumpto_first_error()
3689 call Xjumpto_first_error_test('c')
3690 call Xjumpto_first_error_test('l')
3691endfunc
3692
3693" Test for a quickfix autocmd changing the quickfix/location list before
3694" jumping to the first error in the new list.
3695func Xautocmd_changelist(cchar)
3696 call s:setup_commands(a:cchar)
3697
3698 " Test for cfile/lfile
3699 call s:create_test_file('Xtestfile1')
3700 call s:create_test_file('Xtestfile2')
3701 Xexpr 'Xtestfile1:2:Line2'
3702 autocmd QuickFixCmdPost * Xolder
3703 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
3704 Xfile Xerr
3705 call assert_equal('Xtestfile2', bufname(''))
3706 call assert_equal(4, line('.'))
3707 autocmd! QuickFixCmdPost
3708
3709 " Test for cbuffer/lbuffer
3710 call g:Xsetlist([], 'f')
3711 Xexpr 'Xtestfile1:2:Line2'
3712 autocmd QuickFixCmdPost * Xolder
3713 call writefile(['Xtestfile2:4:Line4'], 'Xerr')
3714 edit Xerr
3715 Xbuffer
3716 call assert_equal('Xtestfile2', bufname(''))
3717 call assert_equal(4, line('.'))
3718 autocmd! QuickFixCmdPost
3719
3720 " Test for cexpr/lexpr
3721 call g:Xsetlist([], 'f')
3722 Xexpr 'Xtestfile1:2:Line2'
3723 autocmd QuickFixCmdPost * Xolder
3724 Xexpr 'Xtestfile2:4:Line4'
3725 call assert_equal('Xtestfile2', bufname(''))
3726 call assert_equal(4, line('.'))
3727 autocmd! QuickFixCmdPost
3728
Bram Moolenaar851332e2018-07-03 19:16:00 +02003729 " The grepprg may not be set on non-Unix systems
3730 if has('unix')
3731 " Test for grep/lgrep
3732 call g:Xsetlist([], 'f')
3733 Xexpr 'Xtestfile1:2:Line2'
3734 autocmd QuickFixCmdPost * Xolder
3735 silent Xgrep Line5 Xtestfile2
3736 call assert_equal('Xtestfile2', bufname(''))
3737 call assert_equal(5, line('.'))
3738 autocmd! QuickFixCmdPost
3739 endif
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003740
3741 " Test for vimgrep/lvimgrep
3742 call g:Xsetlist([], 'f')
3743 Xexpr 'Xtestfile1:2:Line2'
3744 autocmd QuickFixCmdPost * Xolder
3745 silent Xvimgrep Line5 Xtestfile2
3746 call assert_equal('Xtestfile2', bufname(''))
3747 call assert_equal(5, line('.'))
3748 autocmd! QuickFixCmdPost
3749
Bram Moolenaar3f347e42018-08-09 21:19:20 +02003750 " Test for autocommands clearing the quickfix list before jumping to the
3751 " first error. This should not result in an error
3752 autocmd QuickFixCmdPost * call g:Xsetlist([], 'r')
3753 let v:errmsg = ''
3754 " Test for cfile/lfile
3755 Xfile Xerr
3756 call assert_true(v:errmsg !~# 'E42:')
3757 " Test for cbuffer/lbuffer
3758 edit Xerr
3759 Xbuffer
3760 call assert_true(v:errmsg !~# 'E42:')
3761 " Test for cexpr/lexpr
3762 Xexpr 'Xtestfile2:4:Line4'
3763 call assert_true(v:errmsg !~# 'E42:')
3764 " Test for grep/lgrep
3765 " The grepprg may not be set on non-Unix systems
3766 if has('unix')
3767 silent Xgrep Line5 Xtestfile2
3768 call assert_true(v:errmsg !~# 'E42:')
3769 endif
3770 " Test for vimgrep/lvimgrep
3771 call assert_fails('silent Xvimgrep Line5 Xtestfile2', 'E480:')
3772 autocmd! QuickFixCmdPost
3773
Bram Moolenaar531b9a32018-07-03 16:54:23 +02003774 call delete('Xerr')
3775 call delete('Xtestfile1')
3776 call delete('Xtestfile2')
3777endfunc
3778
3779func Test_autocmd_changelist()
3780 call Xautocmd_changelist('c')
3781 call Xautocmd_changelist('l')
3782endfunc
Bram Moolenaar4cde86c2018-07-08 16:01:08 +02003783
3784" Tests for the ':filter /pat/ clist' command
3785func Test_filter_clist()
3786 cexpr ['Xfile1:10:10:Line 10', 'Xfile2:15:15:Line 15']
3787 call assert_equal([' 2 Xfile2:15 col 15: Line 15'],
3788 \ split(execute('filter /Line 15/ clist'), "\n"))
3789 call assert_equal([' 1 Xfile1:10 col 10: Line 10'],
3790 \ split(execute('filter /Xfile1/ clist'), "\n"))
3791 call assert_equal([], split(execute('filter /abc/ clist'), "\n"))
3792
3793 call setqflist([{'module' : 'abc', 'pattern' : 'pat1'},
3794 \ {'module' : 'pqr', 'pattern' : 'pat2'}], ' ')
3795 call assert_equal([' 2 pqr:pat2: '],
3796 \ split(execute('filter /pqr/ clist'), "\n"))
3797 call assert_equal([' 1 abc:pat1: '],
3798 \ split(execute('filter /pat1/ clist'), "\n"))
3799endfunc
Bram Moolenaar0a08c632018-07-25 22:36:52 +02003800
3801" Tests for the "CTRL-W <CR>" command.
3802func Xview_result_split_tests(cchar)
3803 call s:setup_commands(a:cchar)
3804
3805 " Test that "CTRL-W <CR>" in a qf/ll window fails with empty list.
3806 call g:Xsetlist([])
3807 Xopen
3808 let l:win_count = winnr('$')
3809 call assert_fails('execute "normal! \<C-W>\<CR>"', 'E42')
3810 call assert_equal(l:win_count, winnr('$'))
3811 Xclose
3812endfunc
3813
3814func Test_view_result_split()
3815 call Xview_result_split_tests('c')
3816 call Xview_result_split_tests('l')
3817endfunc
Bram Moolenaar2dfcef42018-08-15 22:29:51 +02003818
3819" Test that :cc sets curswant
3820func Test_curswant()
3821 helpgrep quickfix
3822 normal! llll
3823 1cc
3824 call assert_equal(getcurpos()[4], virtcol('.'))
3825 cclose | helpclose
3826endfunc
Bram Moolenaarb2443732018-11-11 22:50:27 +01003827
3828" Test for opening a file from the quickfix window using CTRL-W <Enter>
3829" doesn't leave an empty buffer around.
3830func Test_splitview()
3831 call s:create_test_file('Xtestfile1')
3832 call s:create_test_file('Xtestfile2')
3833 new | only
3834 let last_bufnr = bufnr('Test_sv_1', 1)
3835 let l = ['Xtestfile1:2:Line2', 'Xtestfile2:4:Line4']
3836 cgetexpr l
3837 copen
3838 let numbufs = len(getbufinfo())
3839 exe "normal \<C-W>\<CR>"
3840 copen
3841 exe "normal j\<C-W>\<CR>"
3842 " Make sure new empty buffers are not created
3843 call assert_equal(numbufs, len(getbufinfo()))
3844 " Creating a new buffer should use the next available buffer number
3845 call assert_equal(last_bufnr + 4, bufnr("Test_sv_2", 1))
3846 bwipe Test_sv_1
3847 bwipe Test_sv_2
3848 new | only
3849
3850 " When split opening files from location list window, make sure that two
3851 " windows doesn't refer to the same location list
3852 lgetexpr l
3853 let locid = getloclist(0, {'id' : 0}).id
3854 lopen
3855 exe "normal \<C-W>\<CR>"
3856 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
3857 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
3858 new | only
3859
3860 " When split opening files from a helpgrep location list window, a new help
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003861 " window should be opened with a copy of the location list.
Bram Moolenaarb2443732018-11-11 22:50:27 +01003862 lhelpgrep window
3863 let locid = getloclist(0, {'id' : 0}).id
3864 lwindow
3865 exe "normal j\<C-W>\<CR>"
3866 call assert_notequal(locid, getloclist(0, {'id' : 0}).id)
3867 call assert_equal(0, getloclist(0, {'winid' : 0}).winid)
3868 new | only
3869
3870 call delete('Xtestfile1')
3871 call delete('Xtestfile2')
3872endfunc
Bram Moolenaarc45eb772019-01-31 14:27:04 +01003873
3874" Test for parsing entries using visual screen column
3875func Test_viscol()
3876 enew
3877 call writefile(["Col1\tCol2\tCol3"], 'Xfile1')
3878 edit Xfile1
3879
3880 " Use byte offset for column number
3881 set efm&
3882 cexpr "Xfile1:1:5:XX\nXfile1:1:9:YY\nXfile1:1:20:ZZ"
3883 call assert_equal([5, 8], [col('.'), virtcol('.')])
3884 cnext
3885 call assert_equal([9, 12], [col('.'), virtcol('.')])
3886 cnext
3887 call assert_equal([14, 20], [col('.'), virtcol('.')])
3888
3889 " Use screen column offset for column number
3890 set efm=%f:%l:%v:%m
3891 cexpr "Xfile1:1:8:XX\nXfile1:1:12:YY\nXfile1:1:20:ZZ"
3892 call assert_equal([5, 8], [col('.'), virtcol('.')])
3893 cnext
3894 call assert_equal([9, 12], [col('.'), virtcol('.')])
3895 cnext
3896 call assert_equal([14, 20], [col('.'), virtcol('.')])
3897 cexpr "Xfile1:1:6:XX\nXfile1:1:15:YY\nXfile1:1:24:ZZ"
3898 call assert_equal([5, 8], [col('.'), virtcol('.')])
3899 cnext
3900 call assert_equal([10, 16], [col('.'), virtcol('.')])
3901 cnext
3902 call assert_equal([14, 20], [col('.'), virtcol('.')])
3903
3904 enew
3905 call writefile(["Col1\täü\töß\tCol4"], 'Xfile1')
3906
3907 " Use byte offset for column number
3908 set efm&
3909 cexpr "Xfile1:1:8:XX\nXfile1:1:11:YY\nXfile1:1:16:ZZ"
3910 call assert_equal([8, 10], [col('.'), virtcol('.')])
3911 cnext
3912 call assert_equal([11, 17], [col('.'), virtcol('.')])
3913 cnext
3914 call assert_equal([16, 25], [col('.'), virtcol('.')])
3915
3916 " Use screen column offset for column number
3917 set efm=%f:%l:%v:%m
3918 cexpr "Xfile1:1:10:XX\nXfile1:1:17:YY\nXfile1:1:25:ZZ"
3919 call assert_equal([8, 10], [col('.'), virtcol('.')])
3920 cnext
3921 call assert_equal([11, 17], [col('.'), virtcol('.')])
3922 cnext
3923 call assert_equal([16, 25], [col('.'), virtcol('.')])
3924
3925 enew | only
3926 set efm&
3927 call delete('Xfile1')
3928endfunc
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003929
3930" Test for the quickfix window buffer
3931func Xqfbuf_test(cchar)
3932 call s:setup_commands(a:cchar)
3933
3934 " Quickfix buffer should be reused across closing and opening a quickfix
3935 " window
3936 Xexpr "F1:10:Line10"
3937 Xopen
3938 let qfbnum = bufnr('')
3939 Xclose
3940 " Even after the quickfix window is closed, the buffer should be loaded
3941 call assert_true(bufloaded(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003942 call assert_true(qfbnum, g:Xgetlist({'qfbufnr' : 0}).qfbufnr)
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003943 Xopen
3944 " Buffer should be reused when opening the window again
3945 call assert_equal(qfbnum, bufnr(''))
3946 Xclose
3947
3948 if a:cchar == 'l'
3949 %bwipe
3950 " For a location list, when both the file window and the location list
3951 " window for the list are closed, then the buffer should be freed.
3952 new | only
3953 lexpr "F1:10:Line10"
3954 let wid = win_getid()
3955 lopen
3956 let qfbnum = bufnr('')
3957 call assert_match(qfbnum . ' %a- "\[Location List]"', execute('ls'))
3958 close
3959 " When the location list window is closed, the buffer name should not
3960 " change to 'Quickfix List'
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003961 call assert_match(qfbnum . 'u h- "\[Location List]"', execute('ls!'))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003962 call assert_true(bufloaded(qfbnum))
3963
Bram Moolenaard82a81c2019-03-02 07:57:18 +01003964 " After deleting a location list buffer using ":bdelete", opening the
3965 " location list window should mark the buffer as a location list buffer.
3966 exe "bdelete " . qfbnum
3967 lopen
3968 call assert_equal("quickfix", &buftype)
3969 call assert_equal(1, getwininfo(win_getid(winnr()))[0].loclist)
3970 call assert_equal(wid, getloclist(0, {'filewinid' : 0}).filewinid)
3971 call assert_false(&swapfile)
3972 lclose
3973
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003974 " When the location list is cleared for the window, the buffer should be
3975 " removed
3976 call setloclist(0, [], 'f')
3977 call assert_false(bufexists(qfbnum))
Bram Moolenaar647e24b2019-03-17 16:39:46 +01003978 call assert_equal(0, getloclist(0, {'qfbufnr' : 0}).qfbufnr)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003979
3980 " When the location list is freed with the location list window open, the
3981 " location list buffer should not be lost. It should be reused when the
3982 " location list is again populated.
3983 lexpr "F1:10:Line10"
3984 lopen
3985 let wid = win_getid()
3986 let qfbnum = bufnr('')
3987 wincmd p
3988 call setloclist(0, [], 'f')
3989 lexpr "F1:10:Line10"
3990 lopen
3991 call assert_equal(wid, win_getid())
3992 call assert_equal(qfbnum, bufnr(''))
3993 lclose
3994
3995 " When the window with the location list is closed, the buffer should be
3996 " removed
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003997 new | only
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01003998 call assert_false(bufexists(qfbnum))
Bram Moolenaaree8188f2019-02-05 21:23:04 +01003999 endif
4000endfunc
4001
4002func Test_qfbuf()
4003 call Xqfbuf_test('c')
4004 call Xqfbuf_test('l')
4005endfunc
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01004006
4007" If there is an autocmd to use only one window, then opening the location
4008" list window used to crash Vim.
4009func Test_winonly_autocmd()
4010 call s:create_test_file('Xtest1')
4011 " Autocmd to show only one Vim window at a time
4012 autocmd WinEnter * only
4013 new
4014 " Load the location list
4015 lexpr "Xtest1:5:Line5\nXtest1:10:Line10\nXtest1:15:Line15"
4016 let loclistid = getloclist(0, {'id' : 0}).id
4017 " Open the location list window. Only this window will be shown and the file
4018 " window is closed.
4019 lopen
4020 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4021 " Jump to an entry in the location list and make sure that the cursor is
4022 " positioned correctly.
4023 ll 3
4024 call assert_equal(loclistid, getloclist(0, {'id' : 0}).id)
4025 call assert_equal('Xtest1', bufname(''))
4026 call assert_equal(15, line('.'))
4027 " Cleanup
4028 autocmd! WinEnter
4029 new | only
4030 call delete('Xtest1')
4031endfunc
Bram Moolenaar39803d82019-04-07 12:04:51 +02004032
4033" Test to make sure that an empty quickfix buffer is not reused for loading
4034" a normal buffer.
4035func Test_empty_qfbuf()
4036 enew | only
4037 call writefile(["Test"], 'Xfile1')
4038 call setqflist([], 'f')
4039 copen | only
4040 let qfbuf = bufnr('')
4041 edit Xfile1
4042 call assert_notequal(qfbuf, bufnr(''))
4043 enew
4044 call delete('Xfile1')
4045endfunc
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004046
4047" Test for the :cbelow, :cabove, :lbelow and :labove commands.
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004048" And for the :cafter, :cbefore, :lafter and :lbefore commands.
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004049func Xtest_below(cchar)
4050 call s:setup_commands(a:cchar)
4051
4052 " No quickfix/location list
4053 call assert_fails('Xbelow', 'E42:')
4054 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004055 call assert_fails('Xbefore', 'E42:')
4056 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004057
4058 " Empty quickfix/location list
4059 call g:Xsetlist([])
4060 call assert_fails('Xbelow', 'E42:')
4061 call assert_fails('Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004062 call assert_fails('Xbefore', 'E42:')
4063 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004064
4065 call s:create_test_file('X1')
4066 call s:create_test_file('X2')
4067 call s:create_test_file('X3')
4068 call s:create_test_file('X4')
4069
4070 " Invalid entries
4071 edit X1
4072 call g:Xsetlist(["E1", "E2"])
4073 call assert_fails('Xbelow', 'E42:')
4074 call assert_fails('Xabove', 'E42:')
4075 call assert_fails('3Xbelow', 'E42:')
4076 call assert_fails('4Xabove', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004077 call assert_fails('Xbefore', 'E42:')
4078 call assert_fails('Xafter', 'E42:')
4079 call assert_fails('3Xbefore', 'E42:')
4080 call assert_fails('4Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004081
4082 " Test the commands with various arguments
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004083 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 +02004084 edit +7 X2
4085 Xabove
4086 call assert_equal(['X2', 5], [bufname(''), line('.')])
4087 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004088 normal 7G
4089 Xbefore
4090 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4091 call assert_fails('Xbefore', 'E553:')
4092
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004093 normal 2j
4094 Xbelow
4095 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004096 normal 7G
4097 Xafter
4098 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4099
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004100 " Last error in this file
4101 Xbelow 99
4102 call assert_equal(['X2', 15], [bufname(''), line('.')])
4103 call assert_fails('Xbelow', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004104 normal gg
4105 Xafter 99
4106 call assert_equal(['X2', 15, 4], [bufname(''), line('.'), col('.')])
4107 call assert_fails('Xafter', 'E553:')
4108
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004109 " First error in this file
4110 Xabove 99
4111 call assert_equal(['X2', 5], [bufname(''), line('.')])
4112 call assert_fails('Xabove', 'E553:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004113 normal G
4114 Xbefore 99
4115 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4116 call assert_fails('Xbefore', 'E553:')
4117
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004118 normal gg
4119 Xbelow 2
4120 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004121 normal gg
4122 Xafter 2
4123 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4124
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004125 normal G
4126 Xabove 2
4127 call assert_equal(['X2', 10], [bufname(''), line('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004128 normal G
4129 Xbefore 2
4130 call assert_equal(['X2', 10, 3], [bufname(''), line('.'), col('.')])
4131
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004132 edit X4
4133 call assert_fails('Xabove', 'E42:')
4134 call assert_fails('Xbelow', 'E42:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004135 call assert_fails('Xbefore', 'E42:')
4136 call assert_fails('Xafter', 'E42:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004137 if a:cchar == 'l'
4138 " If a buffer has location list entries from some other window but not
4139 " from the current window, then the commands should fail.
4140 edit X1 | split | call setloclist(0, [], 'f')
4141 call assert_fails('Xabove', 'E776:')
4142 call assert_fails('Xbelow', 'E776:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004143 call assert_fails('Xbefore', 'E776:')
4144 call assert_fails('Xafter', 'E776:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004145 close
4146 endif
4147
4148 " Test for lines with multiple quickfix entries
4149 Xexpr ["X1:5:L5", "X2:5:1:L5_1", "X2:5:2:L5_2", "X2:5:3:L5_3",
4150 \ "X2:10:1:L10_1", "X2:10:2:L10_2", "X2:10:3:L10_3",
4151 \ "X2:15:1:L15_1", "X2:15:2:L15_2", "X2:15:3:L15_3", "X3:3:L3"]
4152 edit +1 X2
4153 Xbelow 2
4154 call assert_equal(['X2', 10, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004155 normal 1G
4156 Xafter 2
4157 call assert_equal(['X2', 5, 2], [bufname(''), line('.'), col('.')])
4158
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004159 normal gg
4160 Xbelow 99
4161 call assert_equal(['X2', 15, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004162 normal gg
4163 Xafter 99
4164 call assert_equal(['X2', 15, 3], [bufname(''), line('.'), col('.')])
4165
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004166 normal G
4167 Xabove 2
4168 call assert_equal(['X2', 10, 1], [bufname(''), line('.'), col('.')])
4169 normal G
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004170 Xbefore 2
4171 call assert_equal(['X2', 15, 2], [bufname(''), line('.'), col('.')])
4172
4173 normal G
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004174 Xabove 99
4175 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004176 normal G
4177 Xbefore 99
4178 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
4179
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004180 normal 10G
4181 Xabove
4182 call assert_equal(['X2', 5, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004183 normal 10G$
4184 2Xbefore
4185 call assert_equal(['X2', 10, 2], [bufname(''), line('.'), col('.')])
4186
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004187 normal 10G
4188 Xbelow
4189 call assert_equal(['X2', 15, 1], [bufname(''), line('.'), col('.')])
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004190 normal 9G
4191 5Xafter
4192 call assert_equal(['X2', 15, 2], [bufname(''), line('.'), col('.')])
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004193
4194 " Invalid range
4195 if a:cchar == 'c'
Bram Moolenaar25190db2019-05-04 15:05:28 +02004196 call assert_fails('-2cbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004197 call assert_fails('-2cafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004198 else
Bram Moolenaar25190db2019-05-04 15:05:28 +02004199 call assert_fails('-2lbelow', 'E16:')
Bram Moolenaarcf6a55c2019-05-05 15:02:30 +02004200 call assert_fails('-2lafter', 'E16:')
Bram Moolenaar3ff33112019-05-03 21:56:35 +02004201 endif
4202
4203 call delete('X1')
4204 call delete('X2')
4205 call delete('X3')
4206 call delete('X4')
4207endfunc
4208
4209func Test_cbelow()
4210 call Xtest_below('c')
4211 call Xtest_below('l')
4212endfunc
Bram Moolenaar25190db2019-05-04 15:05:28 +02004213
4214func Test_quickfix_count()
4215 let commands = [
4216 \ 'cNext',
4217 \ 'cNfile',
4218 \ 'cabove',
4219 \ 'cbelow',
4220 \ 'cfirst',
4221 \ 'clast',
4222 \ 'cnewer',
4223 \ 'cnext',
4224 \ 'cnfile',
4225 \ 'colder',
4226 \ 'cprevious',
4227 \ 'crewind',
4228 \
4229 \ 'lNext',
4230 \ 'lNfile',
4231 \ 'labove',
4232 \ 'lbelow',
4233 \ 'lfirst',
4234 \ 'llast',
4235 \ 'lnewer',
4236 \ 'lnext',
4237 \ 'lnfile',
4238 \ 'lolder',
4239 \ 'lprevious',
4240 \ 'lrewind',
4241 \ ]
4242 for cmd in commands
4243 call assert_fails('-1' .. cmd, 'E16:')
4244 call assert_fails('.' .. cmd, 'E16:')
4245 call assert_fails('%' .. cmd, 'E16:')
4246 call assert_fails('$' .. cmd, 'E16:')
4247 endfor
4248endfunc