1" Test for the quickfix commands.
2
3if !has('quickfix')
4  finish
5endif
6
7" Tests for the :clist and :llist commands
8function XlistTests(cchar)
9  let Xlist = a:cchar . 'list'
10  let Xgetexpr = a:cchar . 'getexpr'
11
12  " With an empty list, command should return error
13  exe Xgetexpr . ' []'
14  exe 'silent! ' . Xlist
15  call assert_true(v:errmsg ==# 'E42: No Errors')
16
17  " Populate the list and then try
18  exe Xgetexpr . " ['non-error 1', 'Xtestfile1:1:3:Line1',
19		  \ 'non-error 2', 'Xtestfile2:2:2:Line2',
20		  \ 'non-error 3', 'Xtestfile3:3:1:Line3']"
21
22  " List only valid entries
23  redir => result
24  exe Xlist
25  redir END
26  let l = split(result, "\n")
27  call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
28		   \ ' 4 Xtestfile2:2 col 2: Line2',
29		   \ ' 6 Xtestfile3:3 col 1: Line3'], l)
30
31  " List all the entries
32  redir => result
33  exe Xlist . "!"
34  redir END
35  let l = split(result, "\n")
36  call assert_equal([' 1: non-error 1', ' 2 Xtestfile1:1 col 3: Line1',
37		   \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2',
38		   \ ' 5: non-error 3', ' 6 Xtestfile3:3 col 1: Line3'], l)
39
40  " List a range of errors
41  redir => result
42  exe Xlist . " 3,6"
43  redir END
44  let l = split(result, "\n")
45  call assert_equal([' 4 Xtestfile2:2 col 2: Line2',
46		   \ ' 6 Xtestfile3:3 col 1: Line3'], l)
47
48  redir => result
49  exe Xlist . "! 3,4"
50  redir END
51  let l = split(result, "\n")
52  call assert_equal([' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
53
54  redir => result
55  exe Xlist . " -6,-4"
56  redir END
57  let l = split(result, "\n")
58  call assert_equal([' 2 Xtestfile1:1 col 3: Line1'], l)
59
60  redir => result
61  exe Xlist . "! -5,-3"
62  redir END
63  let l = split(result, "\n")
64  call assert_equal([' 2 Xtestfile1:1 col 3: Line1',
65		   \ ' 3: non-error 2', ' 4 Xtestfile2:2 col 2: Line2'], l)
66endfunction
67
68function Test_clist()
69  call XlistTests('c')
70  call XlistTests('l')
71endfunction
72
73" Tests for the :colder, :cnewer, :lolder and :lnewer commands
74" Note that this test assumes that a quickfix/location list is
75" already set by previous tests
76function XageTests(cchar)
77  let Xolder = a:cchar . 'older'
78  let Xnewer = a:cchar . 'newer'
79  let Xgetexpr = a:cchar . 'getexpr'
80  if a:cchar == 'c'
81    let Xgetlist = 'getqflist()'
82  else
83    let Xgetlist = 'getloclist(0)'
84  endif
85
86  " Jumping to a non existent list should return error
87  exe 'silent! ' . Xolder . ' 99'
88  call assert_true(v:errmsg ==# 'E380: At bottom of quickfix stack')
89
90  exe 'silent! ' . Xnewer . ' 99'
91  call assert_true(v:errmsg ==# 'E381: At top of quickfix stack')
92
93  " Add three quickfix/location lists
94  exe Xgetexpr . " ['Xtestfile1:1:3:Line1']"
95  exe Xgetexpr . " ['Xtestfile2:2:2:Line2']"
96  exe Xgetexpr . " ['Xtestfile3:3:1:Line3']"
97
98  " Go back two lists
99  exe Xolder
100  exe 'let l = ' . Xgetlist
101  call assert_equal('Line2', l[0].text)
102
103  " Go forward two lists
104  exe Xnewer
105  exe 'let l = ' . Xgetlist
106  call assert_equal('Line3', l[0].text)
107
108  " Test for the optional count argument
109  exe Xolder . ' 2'
110  exe 'let l = ' . Xgetlist
111  call assert_equal('Line1', l[0].text)
112
113  exe Xnewer . ' 2'
114  exe 'let l = ' . Xgetlist
115  call assert_equal('Line3', l[0].text)
116endfunction
117
118function Test_cage()
119  call XageTests('c')
120  call XageTests('l')
121endfunction
122
123" Tests for the :cwindow, :lwindow :cclose, :lclose, :copen and :lopen
124" commands
125function XwindowTests(cchar)
126  let Xwindow = a:cchar . 'window'
127  let Xclose = a:cchar . 'close'
128  let Xopen = a:cchar . 'open'
129  let Xgetexpr = a:cchar . 'getexpr'
130
131  " Create a list with no valid entries
132  exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']"
133
134  " Quickfix/Location window should not open with no valid errors
135  exe Xwindow
136  call assert_true(winnr('$') == 1)
137
138  " Create a list with valid entries
139  exe Xgetexpr . " ['Xtestfile1:1:3:Line1', 'Xtestfile2:2:2:Line2',
140		  \ 'Xtestfile3:3:1:Line3']"
141
142  " Open the window
143  exe Xwindow
144  call assert_true(winnr('$') == 2 && winnr() == 2 &&
145	\ getline('.') ==# 'Xtestfile1|1 col 3| Line1')
146
147  " Close the window
148  exe Xclose
149  call assert_true(winnr('$') == 1)
150
151  " Create a list with no valid entries
152  exe Xgetexpr . " ['non-error 1', 'non-error 2', 'non-error 3']"
153
154  " Open the window
155  exe Xopen . ' 5'
156  call assert_true(winnr('$') == 2 && getline('.') ==# '|| non-error 1'
157		      \  && winheight('.') == 5)
158
159  " Opening the window again, should move the cursor to that window
160  wincmd t
161  exe Xopen . ' 7'
162  call assert_true(winnr('$') == 2 && winnr() == 2 &&
163	\ winheight('.') == 7 &&
164	\ getline('.') ==# '|| non-error 1')
165
166
167  " Calling cwindow should close the quickfix window with no valid errors
168  exe Xwindow
169  call assert_true(winnr('$') == 1)
170endfunction
171
172function Test_cwindow()
173  call XwindowTests('c')
174  call XwindowTests('l')
175endfunction
176
177" Tests for the :cfile, :lfile, :caddfile, :laddfile, :cgetfile and :lgetfile
178" commands.
179function XfileTests(cchar)
180  let Xfile = a:cchar . 'file'
181  let Xgetfile = a:cchar . 'getfile'
182  let Xaddfile = a:cchar . 'addfile'
183  if a:cchar == 'c'
184    let Xgetlist = 'getqflist()'
185  else
186    let Xgetlist = 'getloclist(0)'
187  endif
188
189  call writefile(['Xtestfile1:700:10:Line 700',
190	\ 'Xtestfile2:800:15:Line 800'], 'Xqftestfile1')
191
192  enew!
193  exe Xfile . ' Xqftestfile1'
194  exe 'let l = ' . Xgetlist
195  call assert_true(len(l) == 2 &&
196	\ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
197	\ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
198
199  " Run cfile/lfile from a modified buffer
200  enew!
201  silent! put ='Quickfix'
202  exe 'silent! ' . Xfile . ' Xqftestfile1'
203  call assert_true(v:errmsg ==# 'E37: No write since last change (add ! to override)')
204
205  call writefile(['Xtestfile3:900:30:Line 900'], 'Xqftestfile1')
206  exe Xaddfile . ' Xqftestfile1'
207  exe 'let l = ' . Xgetlist
208  call assert_true(len(l) == 3 &&
209	\ l[2].lnum == 900 && l[2].col == 30 && l[2].text ==# 'Line 900')
210
211  call writefile(['Xtestfile1:222:77:Line 222',
212	\ 'Xtestfile2:333:88:Line 333'], 'Xqftestfile1')
213
214  enew!
215  exe Xgetfile . ' Xqftestfile1'
216  exe 'let l = ' . Xgetlist
217  call assert_true(len(l) == 2 &&
218	\ l[0].lnum == 222 && l[0].col == 77 && l[0].text ==# 'Line 222' &&
219	\ l[1].lnum == 333 && l[1].col == 88 && l[1].text ==# 'Line 333')
220
221  call delete('Xqftestfile1')
222endfunction
223
224function Test_cfile()
225  call XfileTests('c')
226  call XfileTests('l')
227endfunction
228
229" Tests for the :cbuffer, :lbuffer, :caddbuffer, :laddbuffer, :cgetbuffer and
230" :lgetbuffer commands.
231function XbufferTests(cchar)
232  let Xbuffer = a:cchar . 'buffer'
233  let Xgetbuffer = a:cchar . 'getbuffer'
234  let Xaddbuffer = a:cchar . 'addbuffer'
235  if a:cchar == 'c'
236    let Xgetlist = 'getqflist()'
237  else
238    let Xgetlist = 'getloclist(0)'
239  endif
240
241  enew!
242  silent! call setline(1, ['Xtestfile7:700:10:Line 700',
243	\ 'Xtestfile8:800:15:Line 800'])
244  exe Xbuffer . "!"
245  exe 'let l = ' . Xgetlist
246  call assert_true(len(l) == 2 &&
247	\ l[0].lnum == 700 && l[0].col == 10 && l[0].text ==# 'Line 700' &&
248	\ l[1].lnum == 800 && l[1].col == 15 && l[1].text ==# 'Line 800')
249
250  enew!
251  silent! call setline(1, ['Xtestfile9:900:55:Line 900',
252	\ 'Xtestfile10:950:66:Line 950'])
253  exe Xgetbuffer
254  exe 'let l = ' . Xgetlist
255  call assert_true(len(l) == 2 &&
256	\ l[0].lnum == 900 && l[0].col == 55 && l[0].text ==# 'Line 900' &&
257	\ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950')
258
259  enew!
260  silent! call setline(1, ['Xtestfile11:700:20:Line 700',
261	\ 'Xtestfile12:750:25:Line 750'])
262  exe Xaddbuffer
263  exe 'let l = ' . Xgetlist
264  call assert_true(len(l) == 4 &&
265	\ l[1].lnum == 950 && l[1].col == 66 && l[1].text ==# 'Line 950' &&
266	\ l[2].lnum == 700 && l[2].col == 20 && l[2].text ==# 'Line 700' &&
267	\ l[3].lnum == 750 && l[3].col == 25 && l[3].text ==# 'Line 750')
268
269endfunction
270
271function Test_cbuffer()
272  call XbufferTests('c')
273  call XbufferTests('l')
274endfunction
275
276function Test_nomem()
277  call alloc_fail(1, 0, 0)
278  try
279    vimgrep vim runtest.vim
280  catch
281    call assert_true(v:exception =~ 'E342')
282  endtry
283
284  call alloc_fail(2, 0, 0)
285  try
286    vimgrep vim runtest.vim
287  catch
288    call assert_true(v:exception =~ 'E342')
289  endtry
290
291  call alloc_fail(3, 0, 0)
292  try
293    cfile runtest.vim
294  catch
295    call assert_true(v:exception =~ 'E342')
296  endtry
297
298  call alloc_fail(4, 0, 0)
299  try
300    cfile runtest.vim
301  catch
302    call assert_true(v:exception =~ 'E342')
303  endtry
304
305  call alloc_fail(5, 0, 0)
306  try
307    cfile runtest.vim
308  catch
309    call assert_true(v:exception =~ 'E342')
310  endtry
311
312endfunc
313
314
315