robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

glepnirvim-patch:9.2.0639: gq with 'formatprg' fails on an empty buffer (#40226)ff0352fd4f

master
9.0 KiB353 linesraw
1
2" Test that a deleted mark is restored after delete-undo-redo-undo.
3func Test_Restore_DelMark()
4  enew!
5  call append(0, ["	textline A", "	textline B", "	textline C"])
6  normal! 2gg
7  set nocp viminfo+=nviminfo
8  exe "normal! i\<C-G>u\<Esc>"
9  exe "normal! maddu\<C-R>u"
10  let pos = getpos("'a")
11  call assert_equal(2, pos[1])
12  call assert_equal(1, pos[2])
13  enew!
14endfunc
15
16" Test that CTRL-A and CTRL-X updates last changed mark '[, '].
17func Test_Incr_Marks()
18  enew!
19  call append(0, ["123 123 123", "123 123 123", "123 123 123"])
20  normal! gg
21  execute "normal! \<C-A>`[v`]rAjwvjw\<C-X>`[v`]rX"
22  call assert_equal("AAA 123 123", getline(1))
23  call assert_equal("123 XXXXXXX", getline(2))
24  call assert_equal("XXX 123 123", getline(3))
25  enew!
26endfunc
27
28func Test_previous_jump_mark()
29  new
30  call setline(1, ['']->repeat(6))
31  normal Ggg
32  call assert_equal(6, getpos("''")[1])
33  normal jjjjj
34  call assert_equal(6, getpos("''")[1])
35  bwipe!
36endfunc
37
38func Test_setpos()
39  new Xone
40  let onebuf = bufnr('%')
41  let onewin = win_getid()
42  call setline(1, ['aaa', 'bbb', 'ccc'])
43  new Xtwo
44  let twobuf = bufnr('%')
45  let twowin = win_getid()
46  call setline(1, ['aaa', 'bbb', 'ccc'])
47
48  " for the cursor the buffer number is ignored
49  call setpos(".", [0, 2, 1, 0])
50  call assert_equal([0, 2, 1, 0], getpos("."))
51  call setpos(".", [onebuf, 3, 3, 0])
52  call assert_equal([0, 3, 3, 0], getpos("."))
53
54  call setpos("''", [0, 1, 3, 0])
55  call assert_equal([0, 1, 3, 0], getpos("''"))
56  call setpos("''", [onebuf, 2, 2, 0])
57  call assert_equal([0, 2, 2, 0], getpos("''"))
58
59  " buffer-local marks
60  for mark in ["'a", "'\"", "'[", "']", "'<", "'>"]
61    call win_gotoid(twowin)
62    call setpos(mark, [0, 2, 1, 0])
63    call assert_equal([0, 2, 1, 0], getpos(mark), "for mark " . mark)
64    call setpos(mark, [onebuf, 1, 3, 0])
65    call win_gotoid(onewin)
66    call assert_equal([0, 1, 3, 0], getpos(mark), "for mark " . mark)
67  endfor
68
69  " global marks
70  call win_gotoid(twowin)
71  call setpos("'N", [0, 2, 1, 0])
72  call assert_equal([twobuf, 2, 1, 0], getpos("'N"))
73  call setpos("'N", [onebuf, 1, 3, 0])
74  call assert_equal([onebuf, 1, 3, 0], getpos("'N"))
75
76  " try invalid column and check virtcol()
77  call win_gotoid(onewin)
78  call setpos("'a", [0, 1, 2, 0])
79  call assert_equal([0, 1, 2, 0], getpos("'a"))
80  call setpos("'a", [0, 1, -5, 0])
81  call assert_equal([0, 1, 2, 0], getpos("'a"))
82  call setpos("'a", [0, 1, 0, 0])
83  call assert_equal([0, 1, 1, 0], getpos("'a"))
84  call setpos("'a", [0, 1, 4, 0])
85  call assert_equal([0, 1, 4, 0], getpos("'a"))
86  call assert_equal(4, virtcol("'a"))
87  call setpos("'a", [0, 1, 5, 0])
88  call assert_equal([0, 1, 5, 0], getpos("'a"))
89  call assert_equal(4, virtcol("'a"))
90  call setpos("'a", [0, 1, 21341234, 0])
91  call assert_equal([0, 1, 21341234, 0], getpos("'a"))
92  call assert_equal(4, virtcol("'a"))
93
94  " Test with invalid buffer number, line number and column number
95  call cursor(2, 2)
96  call setpos('.', [-1, 1, 1, 0])
97  call assert_equal([2, 2], [line('.'), col('.')])
98  call setpos('.', [0, -1, 1, 0])
99  call assert_equal([2, 2], [line('.'), col('.')])
100  call setpos('.', [0, 1, -1, 0])
101  call assert_equal([2, 2], [line('.'), col('.')])
102
103  call assert_fails("call setpos('ab', [0, 1, 1, 0])", 'E474:')
104
105  bwipe!
106  call win_gotoid(twowin)
107  bwipe!
108endfunc
109
110func Test_marks_cmd()
111  new Xone
112  call setline(1, ['aaa', 'bbb'])
113  norm! maG$mB
114  w!
115  new Xtwo
116  call setline(1, ['ccc', 'ddd'])
117  norm! $mcGmD
118  exe "norm! GVgg\<Esc>G"
119  w!
120
121  b Xone
122  let a = split(execute('marks'), "\n")
123  call assert_equal(9, len(a))
124  call assert_equal(['mark line  col file/text',
125        \ " '      2    0 bbb",
126        \ ' a      1    0 aaa',
127        \ ' B      2    2 bbb',
128        \ ' D      2    0 Xtwo',
129        \ ' "      1    0 aaa',
130        \ ' [      1    0 aaa',
131        \ ' ]      2    0 bbb',
132        \ ' .      2    0 bbb'], a)
133
134  b Xtwo
135  let a = split(execute('marks'), "\n")
136  call assert_equal(11, len(a))
137  call assert_equal(['mark line  col file/text',
138        \ " '      1    0 ccc",
139        \ ' c      1    2 ccc',
140        \ ' B      2    2 Xone',
141        \ ' D      2    0 ddd',
142        \ ' "      2    0 ddd',
143        \ ' [      1    0 ccc',
144        \ ' ]      2    0 ddd',
145        \ ' .      2    0 ddd',
146        \ ' <      1    0 ccc',
147        \ ' >      2    0 ddd'], a)
148  norm! Gdd
149  w!
150  let a = split(execute('marks <>'), "\n")
151  call assert_equal(3, len(a))
152  call assert_equal(['mark line  col file/text',
153        \ ' <      1    0 ccc',
154        \ ' >      2    0 -invalid-'], a)
155
156  b Xone
157  delmarks aB
158  let a = split(execute('marks aBcD'), "\n")
159  call assert_equal(2, len(a))
160  call assert_equal('mark line  col file/text', a[0])
161  call assert_equal(' D      2    0 Xtwo', a[1])
162
163  b Xtwo
164  delmarks cD
165  call assert_fails('marks aBcD', 'E283:')
166
167  call delete('Xone')
168  call delete('Xtwo')
169  %bwipe
170endfunc
171
172func Test_marks_cmd_multibyte()
173  new Xone
174  call setline(1, [repeat('á', &columns)])
175  norm! ma
176
177  let a = split(execute('marks a'), "\n")
178  call assert_equal(2, len(a))
179  let expected = ' a      1    0 ' . repeat('á', &columns - 16)
180  call assert_equal(expected, a[1])
181
182  bwipe!
183endfunc
184
185func Test_delmarks()
186  new
187  norm mx
188  norm `x
189  delmarks x
190  call assert_fails('norm `x', 'E20:')
191
192  " Deleting an already deleted mark should not fail.
193  delmarks x
194
195  " getpos() should return all zeros after deleting a filemark.
196  norm mA
197  delmarks A
198  call assert_equal([0, 0, 0, 0], getpos("'A"))
199
200  " Test deleting a range of marks.
201  norm ma
202  norm mb
203  norm mc
204  norm mz
205  delmarks b-z
206  norm `a
207  call assert_fails('norm `b', 'E20:')
208  call assert_fails('norm `c', 'E20:')
209  call assert_fails('norm `z', 'E20:')
210  call assert_fails('delmarks z-b', 'E475:')
211
212  call assert_fails('delmarks', 'E471:')
213  call assert_fails('delmarks /', 'E475:')
214
215  " Test delmarks!
216  norm mx
217  norm `x
218  delmarks!
219  call assert_fails('norm `x', 'E20:')
220  call assert_fails('delmarks! x', 'E474:')
221
222  bwipe!
223endfunc
224
225func Test_mark_error()
226  call assert_fails('mark', 'E471:')
227  call assert_fails('mark xx', 'E488:')
228  call assert_fails('mark _', 'E191:')
229  call assert_beeps('normal! m~')
230
231  call setpos("'k", [0, 100, 1, 0])
232  call assert_fails("normal 'k", 'E19:')
233endfunc
234
235" Test for :lockmarks when pasting content
236func Test_lockmarks_with_put()
237  new
238  call append(0, repeat(['sky is blue'], 4))
239  normal gg
240  1,2yank r
241  put r
242  normal G
243  lockmarks put r
244  call assert_equal(2, line("'["))
245  call assert_equal(3, line("']"))
246
247  bwipe!
248endfunc
249
250" Test for :k command to set a mark
251func Test_marks_k_cmd()
252  new
253  call setline(1, ['foo', 'bar', 'baz', 'qux'])
254  1,3kr
255  call assert_equal([0, 3, 1, 0], getpos("'r"))
256  " whitespace before mark
257  4k f
258  call assert_equal([0, 4, 1, 0], getpos("'f"))
259  :2     k	 g
260  call assert_equal([0, 2, 1, 0], getpos("'g"))
261  bw!
262  call assert_fails(':kz7', 'E488: Trailing characters: z7')
263  call assert_fails(':execute ":k^"', 'E191: Argument must be a letter or forward/backward quote')
264endfunc
265
266" Test for file marks (A-Z)
267func Test_file_mark()
268  new Xone
269  call setline(1, ['aaa', 'bbb'])
270  norm! G$mB
271  w!
272  new Xtwo
273  call setline(1, ['ccc', 'ddd'])
274  norm! GmD
275  w!
276
277  enew
278  normal! `B
279  call assert_equal('Xone', bufname())
280  call assert_equal([2, 3], [line('.'), col('.')])
281  normal! 'D
282  call assert_equal('Xtwo', bufname())
283  call assert_equal([2, 1], [line('.'), col('.')])
284
285  call delete('Xone')
286  call delete('Xtwo')
287endfunc
288
289" Test for the getmarklist() function
290func Test_getmarklist()
291  new
292  " global marks
293  delmarks A-Z 0-9 \" ^.[]
294  call assert_equal([], getmarklist())
295  call setline(1, ['one', 'two', 'three'])
296  mark A
297  call cursor(3, 5)
298  normal mN
299  call assert_equal([{'file' : '', 'mark' : "'A", 'pos' : [bufnr(), 1, 1, 0]},
300        \ {'file' : '', 'mark' : "'N", 'pos' : [bufnr(), 3, 5, 0]}],
301        \ getmarklist())
302  " buffer local marks
303  delmarks!
304  call assert_equal([{'mark' : "''", 'pos' : [bufnr(), 1, 1, 0]},
305        \ {'mark' : "'\"", 'pos' : [bufnr(), 1, 1, 0]}], getmarklist(bufnr()))
306  call cursor(2, 2)
307  normal mr
308  call assert_equal({'mark' : "'r", 'pos' : [bufnr(), 2, 2, 0]},
309        \ bufnr()->getmarklist()[0])
310  call assert_equal([], {}->getmarklist())
311  normal! yy
312  call assert_equal([
313        \ {'mark': "'[", 'pos': [bufnr(), 2, 1, 0]},
314        \ {'mark': "']", 'pos': [bufnr(), 2, v:maxcol, 0]},
315        \ ], getmarklist(bufnr())[-2:])
316  bw!
317endfunc
318
319" This was using freed memory
320func Test_jump_mark_autocmd()
321  next 00
322  edit 0
323  sargument
324  au BufEnter 0 all
325  sil norm 
326
327  au! BufEnter
328  bwipe!
329endfunc
330
331func Test_mark_formatprg_on_empty()
332  new
333  if has('win32')
334    setl formatprg=more
335  else
336    setl formatprg=cat
337  endif
338  call assert_equal([0, 0], [line("'["), col("'[")])
339  call assert_equal([0, 0], [line("']"), col("']")])
340  let v:errmsg = ''
341  try
342    norm! gqG
343  catch
344    call assert_report('gqG on empty buffer should not fail: ' .. v:exception)
345  endtry
346  call assert_true(empty(v:errmsg))
347  " col() is 1-based, so 1 == first column (:marks shows it as 0)
348  call assert_equal([1, 1], [line("'["), col("'[")])
349  call assert_equal([1, 1], [line("']"), col("']")])
350  bwipe!
351endfunc
352
353" vim: shiftwidth=2 sts=2 expandtab