robin/laufey

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

zeertzjqvim-patch:9.2.0605: tests: Test_screenpos() is flaky in GUI (#40156)51d59b2244

master
19.2 KiB631 linesraw
1" Tests for cursor() and other functions that get/set the cursor position
2
3source check.vim
4
5func Test_wrong_arguments()
6  call assert_fails('call cursor(1. 3)', 'E474:')
7  call assert_fails('call cursor(v:_null_list)', 'E474:')
8endfunc
9
10func Test_move_cursor()
11  new
12  call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
13
14  call cursor([1, 1, 0, 1])
15  call assert_equal([1, 1, 0, 1], getcurpos()[1:])
16  call cursor([4, 3, 0, 3])
17  call assert_equal([4, 3, 0, 3], getcurpos()[1:])
18
19  call cursor(2, 2)
20  call assert_equal([2, 2, 0, 2], getcurpos()[1:])
21  " line number zero keeps the line number
22  call cursor(0, 1)
23  call assert_equal([2, 1, 0, 1], getcurpos()[1:])
24  " col number zero keeps the column
25  call cursor(3, 0)
26  call assert_equal([3, 1, 0, 1], getcurpos()[1:])
27  " below last line goes to last line
28  eval [9, 1]->cursor()
29  call assert_equal([4, 1, 0, 1], getcurpos()[1:])
30  " pass string arguments
31  call cursor('3', '3')
32  call assert_equal([3, 3, 0, 3], getcurpos()[1:])
33
34  call setline(1, ["\<TAB>"])
35  call cursor(1, 1, 1)
36  call assert_equal([1, 1, 1], getcurpos()[1:3])
37
38  call assert_fails('call cursor(-1, -1)', 'E475:')
39
40  quit!
41endfunc
42
43func Test_curswant_maxcol()
44  new
45  call setline(1, 'foo')
46
47  " Test that after "$" command curswant is set to the same value as v:maxcol.
48  normal! 1G$
49  call assert_equal(v:maxcol, getcurpos()[4])
50  call assert_equal(v:maxcol, winsaveview().curswant)
51
52  quit!
53endfunc
54
55" Very short version of what matchparen does.
56function s:Highlight_Matching_Pair()
57  let save_cursor = getcurpos()
58  eval save_cursor->setpos('.')
59endfunc
60
61func Test_curswant_with_autocommand()
62  new
63  call setline(1, ['func()', '{', '}', '----'])
64  autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
65  exe "normal! 3Ga\<Down>X\<Esc>"
66  call assert_equal('-X---', getline(4))
67  autocmd! CursorMovedI *
68  quit!
69endfunc
70
71" Tests for behavior of curswant with cursorcolumn/line
72func Test_curswant_with_cursorcolumn()
73  new
74  call setline(1, ['01234567', ''])
75  exe "normal! ggf6j"
76  call assert_equal(6, winsaveview().curswant)
77  set cursorcolumn
78  call assert_equal(6, winsaveview().curswant)
79  quit!
80endfunc
81
82func Test_curswant_with_cursorline()
83  new
84  call setline(1, ['01234567', ''])
85  exe "normal! ggf6j"
86  call assert_equal(6, winsaveview().curswant)
87  set cursorline
88  call assert_equal(6, winsaveview().curswant)
89  quit!
90endfunc
91
92func Test_screenpos()
93  if has('gui_running')
94    set lines=25
95    set columns=78
96  endif
97  rightbelow new
98  rightbelow 20vsplit
99  call setline(1, ["\tsome text", "long wrapping line here", "next line"])
100  redraw
101  let winid = win_getid()
102  let [winrow, wincol] = win_screenpos(winid)
103  call assert_equal({'row': winrow,
104	\ 'col': wincol + 0,
105	\ 'curscol': wincol + 7,
106	\ 'endcol': wincol + 7}, winid->screenpos(1, 1))
107  call assert_equal({'row': winrow,
108	\ 'col': wincol + 13,
109	\ 'curscol': wincol + 13,
110	\ 'endcol': wincol + 13}, winid->screenpos(1, 7))
111  call assert_equal({'row': winrow + 2,
112	\ 'col': wincol + 1,
113	\ 'curscol': wincol + 1,
114	\ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
115  setlocal number
116  call assert_equal({'row': winrow + 3,
117	\ 'col': wincol + 9,
118	\ 'curscol': wincol + 9,
119	\ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
120
121  let wininfo = getwininfo(winid)[0]
122  call setline(3, ['x']->repeat(wininfo.height))
123  call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3))
124  setlocal nonumber display=lastline so=0
125  exe "normal G\<C-Y>\<C-Y>"
126  redraw
127  let winbar_height = get(wininfo, 'winbar', 0)
128  call assert_equal({'row': winrow + winheight(winid) - 1 + winbar_height,
129	\ 'col': wincol + 7,
130	\ 'curscol': wincol + 7,
131	\ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8))
132  call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
133	\ winid->screenpos(line('$'), 22))
134
135  1split
136
137  " w_leftcol should be subtracted
138  setlocal nowrap
139  normal G050zl$
140  redraw
141  call assert_equal({'row': winrow + 0,
142	\ 'col': wincol + 10 - 1,
143	\ 'curscol': wincol + 10 - 1,
144	\ 'endcol': wincol + 10 - 1},
145	\ screenpos(win_getid(), line('.'), col('.')))
146
147  " w_skipcol should be taken into account
148  setlocal wrap
149  normal $
150  redraw
151  call assert_equal({'row': winrow + 0,
152	\ 'col': wincol + 20 - 1,
153	\ 'curscol': wincol + 20 - 1,
154	\ 'endcol': wincol + 20 - 1},
155	\ screenpos(win_getid(), line('.'), col('.')))
156  call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
157	\ screenpos(win_getid(), line('.'), col('.') - 20))
158  setlocal number
159  redraw
160  call assert_equal({'row': winrow + 0,
161	\ 'col': wincol + 16 - 1,
162	\ 'curscol': wincol + 16 - 1,
163	\ 'endcol': wincol + 16 - 1},
164	\ screenpos(win_getid(), line('.'), col('.')))
165  call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
166	\ screenpos(win_getid(), line('.'), col('.') - 16))
167  set cpoptions+=n
168  redraw
169  call assert_equal({'row': winrow + 0,
170	\ 'col': wincol + 4 - 1,
171	\ 'curscol': wincol + 4 - 1,
172	\ 'endcol': wincol + 4 - 1},
173	\ screenpos(win_getid(), line('.'), col('.')))
174  call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
175	\ screenpos(win_getid(), line('.'), col('.') - 4))
176
177  wincmd +
178  call setline(line('$') + 1, 'last line')
179  setlocal smoothscroll
180  normal G$
181  redraw
182  call assert_equal({'row': winrow + 1,
183	\ 'col': wincol + 4 + 9 - 1,
184	\ 'curscol': wincol + 4 + 9 - 1,
185	\ 'endcol': wincol + 4 + 9 - 1},
186	\ screenpos(win_getid(), line('.'), col('.')))
187  set cpoptions-=n
188  redraw
189  call assert_equal({'row': winrow + 1,
190	\ 'col': wincol + 4 + 9 - 1,
191	\ 'curscol': wincol + 4 + 9 - 1,
192	\ 'endcol': wincol + 4 + 9 - 1},
193	\ screenpos(win_getid(), line('.'), col('.')))
194  setlocal nonumber
195  redraw
196  call assert_equal({'row': winrow + 1,
197	\ 'col': wincol + 9 - 1,
198	\ 'curscol': wincol + 9 - 1,
199	\ 'endcol': wincol + 9 - 1},
200	\ screenpos(win_getid(), line('.'), col('.')))
201
202  close
203  call assert_equal({}, screenpos(999, 1, 1))
204
205  bwipe!
206  set display&
207
208  call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
209  " nmenu WinBar.TEST :
210  setlocal winbar=TEST
211  call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
212  " nunmenu WinBar.TEST
213  setlocal winbar&
214  call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
215
216  call assert_equal(#{col: 0, row: 0, endcol: 0, curscol: 0}, screenpos(0, 0, 1))
217  call assert_equal(#{col: 0, row: 0, endcol: 0, curscol: 0}, screenpos(0, -1, 1))
218  call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(0, 1, -v:maxcol))
219endfunc
220
221func Test_screenpos_fold()
222  CheckFeature folding
223
224  enew!
225  call setline(1, range(10))
226  3,5fold
227  redraw
228  call assert_equal(2, screenpos(1, 2, 1).row)
229  call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
230  call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 4, 1))
231  call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 5, 1))
232  setlocal number
233  call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 3, 1))
234  call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 4, 1))
235  call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 5, 1))
236  call assert_equal(4, screenpos(1, 6, 1).row)
237  bwipe!
238endfunc
239
240func Test_screenpos_diff()
241  CheckFeature diff
242
243  enew!
244  call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
245  vnew
246  call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
247  windo diffthis
248  wincmd w
249  call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
250  call assert_equal(#{col: 3, row: 8, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
251  exe "normal! 3\<C-E>"
252  call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
253  call assert_equal(#{col: 3, row: 5, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
254  exe "normal! \<C-E>"
255  call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
256  call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
257  exe "normal! \<C-E>"
258  call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
259  call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
260  exe "normal! \<C-E>"
261  call assert_equal(#{col: 3, row: 1, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
262  call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
263
264  windo diffoff
265  bwipe!
266  bwipe!
267endfunc
268
269func Test_screenpos_number()
270  rightbelow new
271  rightbelow 73vsplit
272  call setline (1, repeat('x', 66))
273  setlocal number
274  redraw
275  let winid = win_getid()
276  let [winrow, wincol] = win_screenpos(winid)
277  let pos = screenpos(winid, 1, 66)
278  call assert_equal(winrow, pos.row)
279  call assert_equal(wincol + 66 + 3, pos.col)
280
281  call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
282
283  close
284  bwipe!
285endfunc
286
287func Test_screenpos_edit_newfile()
288  new
289  20vsp
290  setl nowrap
291  call setline(1, 'abcdefghijklmnopqrstuvwxyz')
292  call cursor(1, 10)
293  norm! 5zl
294  call assert_equal(#{col: 5, row: 1, endcol: 5, curscol: 5}, screenpos(win_getid(), 1, 10))
295  enew!
296  call assert_equal(1, &l:wrap)
297  call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
298
299  bwipe!
300endfunc
301
302" Save the visual start character position
303func SaveVisualStartCharPos()
304  call add(g:VisualStartPos, getcharpos('v'))
305  return ''
306endfunc
307
308" Save the current cursor character position in insert mode
309func SaveInsertCurrentCharPos()
310  call add(g:InsertCurrentPos, getcharpos('.'))
311  return ''
312endfunc
313
314" Test for the getcharpos() function
315func Test_getcharpos()
316  call assert_fails('call getcharpos({})', 'E731:')
317  call assert_equal([0, 0, 0, 0], getcharpos(0))
318  new
319  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │  x'])
320
321  " Test for '.' and '$'
322  normal 1G
323  call assert_equal([0, 1, 1, 0], getcharpos('.'))
324  call assert_equal([0, 5, 1, 0], getcharpos('$'))
325  normal 2G6l
326  call assert_equal([0, 2, 7, 0], getcharpos('.'))
327  normal 3G$
328  call assert_equal([0, 3, 1, 0], getcharpos('.'))
329  normal 4G$
330  call assert_equal([0, 4, 9, 0], getcharpos('.'))
331
332  " Test for a mark
333  normal 2G7lmmgg
334  call assert_equal([0, 2, 8, 0], getcharpos("'m"))
335  delmarks m
336  call assert_equal([0, 0, 0, 0], getcharpos("'m"))
337
338  " Check mark does not move
339  normal 5Gfxma
340  call assert_equal([0, 5, 5, 0], getcharpos("'a"))
341  call assert_equal([0, 5, 5, 0], getcharpos("'a"))
342  call assert_equal([0, 5, 5, 0], getcharpos("'a"))
343
344  " Test for the visual start column
345  vnoremap <expr> <F3> SaveVisualStartCharPos()
346  let g:VisualStartPos = []
347  exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
348  call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
349  call assert_equal([0, 2, 9, 0], getcharpos('v'))
350  let g:VisualStartPos = []
351  exe "normal 3Gv$\<F3>o\<F3>"
352  call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
353  let g:VisualStartPos = []
354  exe "normal 1Gv$\<F3>o\<F3>"
355  call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
356  vunmap <F3>
357
358  " Test for getting the position in insert mode with the cursor after the
359  " last character in a line
360  inoremap <expr> <F3> SaveInsertCurrentCharPos()
361  let g:InsertCurrentPos = []
362  exe "normal 1GA\<F3>"
363  exe "normal 2GA\<F3>"
364  exe "normal 3GA\<F3>"
365  exe "normal 4GA\<F3>"
366  exe "normal 2G6li\<F3>"
367  call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
368                        \ [0, 2, 7, 0]], g:InsertCurrentPos)
369  iunmap <F3>
370
371  %bw!
372endfunc
373
374" Test for the setcharpos() function
375func Test_setcharpos()
376  call assert_equal(-1, setcharpos('.', v:_null_list))
377  new
378  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
379  call setcharpos('.', [0, 1, 1, 0])
380  call assert_equal([1, 1], [line('.'), col('.')])
381  call setcharpos('.', [0, 2, 7, 0])
382  call assert_equal([2, 9], [line('.'), col('.')])
383  call setcharpos('.', [0, 3, 4, 0])
384  call assert_equal([3, 1], [line('.'), col('.')])
385  call setcharpos('.', [0, 3, 1, 0])
386  call assert_equal([3, 1], [line('.'), col('.')])
387  call setcharpos('.', [0, 4, 0, 0])
388  call assert_equal([4, 1], [line('.'), col('.')])
389  call setcharpos('.', [0, 4, 20, 0])
390  call assert_equal([4, 9], [line('.'), col('.')])
391
392  " Test for mark
393  delmarks m
394  call setcharpos("'m", [0, 2, 9, 0])
395  normal `m
396  call assert_equal([2, 11], [line('.'), col('.')])
397  " unload the buffer and try to set the mark
398  let bnr = bufnr()
399  enew!
400  call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
401
402  %bw!
403  call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
404endfunc
405
406func SaveVisualStartCharCol()
407  call add(g:VisualStartCol, charcol('v'))
408  return ''
409endfunc
410
411func SaveInsertCurrentCharCol()
412  call add(g:InsertCurrentCol, charcol('.'))
413  return ''
414endfunc
415
416" Test for the charcol() function
417func Test_charcol()
418  call assert_fails('call charcol({})', 'E1222:')
419  call assert_fails('call charcol(".", [])', 'E1210:')
420  call assert_fails('call charcol(0)', 'E1222:')
421  new
422  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
423
424  " Test for '.' and '$'
425  normal 1G
426  call assert_equal(1, charcol('.'))
427  call assert_equal(1, charcol('$'))
428  normal 2G6l
429  call assert_equal(7, charcol('.'))
430  call assert_equal(10, charcol('$'))
431  normal 3G$
432  call assert_equal(1, charcol('.'))
433  call assert_equal(2, charcol('$'))
434  normal 4G$
435  call assert_equal(9, charcol('.'))
436  call assert_equal(10, charcol('$'))
437
438  " Test for [lnum, '$']
439  call assert_equal(1, charcol([1, '$']))
440  call assert_equal(10, charcol([2, '$']))
441  call assert_equal(2, charcol([3, '$']))
442  call assert_equal(0, charcol([5, '$']))
443
444  " Test for a mark
445  normal 2G7lmmgg
446  call assert_equal(8, charcol("'m"))
447  delmarks m
448  call assert_equal(0, charcol("'m"))
449
450  " Test for the visual start column
451  vnoremap <expr> <F3> SaveVisualStartCharCol()
452  let g:VisualStartCol = []
453  exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
454  call assert_equal([7, 10, 5], g:VisualStartCol)
455  call assert_equal(9, charcol('v'))
456  let g:VisualStartCol = []
457  exe "normal 3Gv$\<F3>o\<F3>"
458  call assert_equal([1, 2], g:VisualStartCol)
459  let g:VisualStartCol = []
460  exe "normal 1Gv$\<F3>o\<F3>"
461  call assert_equal([1, 1], g:VisualStartCol)
462  vunmap <F3>
463
464  " Test for getting the column number in insert mode with the cursor after
465  " the last character in a line
466  inoremap <expr> <F3> SaveInsertCurrentCharCol()
467  let g:InsertCurrentCol = []
468  exe "normal 1GA\<F3>"
469  exe "normal 2GA\<F3>"
470  exe "normal 3GA\<F3>"
471  exe "normal 4GA\<F3>"
472  exe "normal 2G6li\<F3>"
473  call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
474  iunmap <F3>
475
476  " Test for getting the column number in another window.
477  let winid = win_getid()
478  new
479  call win_execute(winid, 'normal 1G')
480  call assert_equal(1, charcol('.', winid))
481  call assert_equal(1, charcol('$', winid))
482  call win_execute(winid, 'normal 2G6l')
483  call assert_equal(7, charcol('.', winid))
484  call assert_equal(10, charcol('$', winid))
485
486  " calling from another tab page also works
487  tabnew
488  call assert_equal(7, charcol('.', winid))
489  call assert_equal(10, charcol('$', winid))
490  tabclose
491
492  " unknown window ID
493  call assert_equal(0, charcol('.', 10001))
494
495  %bw!
496endfunc
497
498func SaveInsertCursorCharPos()
499  call add(g:InsertCursorPos, getcursorcharpos('.'))
500  return ''
501endfunc
502
503" Test for getcursorcharpos()
504func Test_getcursorcharpos()
505  call assert_equal(getcursorcharpos(), getcursorcharpos(0))
506  call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
507  call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
508
509  new
510  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
511  normal 1G9l
512  call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
513  normal 2G9l
514  call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
515  normal 3G9l
516  call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
517  normal 4G9l
518  call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
519
520  " Test for getting the cursor position in insert mode with the cursor after
521  " the last character in a line
522  inoremap <expr> <F3> SaveInsertCursorCharPos()
523  let g:InsertCursorPos = []
524  exe "normal 1GA\<F3>"
525  exe "normal 2GA\<F3>"
526  exe "normal 3GA\<F3>"
527  exe "normal 4GA\<F3>"
528  exe "normal 2G6li\<F3>"
529  call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
530                    \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
531  iunmap <F3>
532
533  let winid = win_getid()
534  normal 2G5l
535  wincmd w
536  call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
537  %bw!
538endfunc
539
540" Test for setcursorcharpos()
541func Test_setcursorcharpos()
542  call assert_fails('call setcursorcharpos(v:_null_list)', 'E474:')
543  call assert_fails('call setcursorcharpos([1])', 'E474:')
544  call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
545  new
546  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
547  normal G
548  call setcursorcharpos([1, 1])
549  call assert_equal([1, 1], [line('.'), col('.')])
550
551  call setcursorcharpos([2, 7, 0])
552  call assert_equal([2, 9], [line('.'), col('.')])
553  call setcursorcharpos([0, 7, 0])
554  call assert_equal([2, 9], [line('.'), col('.')])
555  call setcursorcharpos(0, 7, 0)
556  call assert_equal([2, 9], [line('.'), col('.')])
557
558  call setcursorcharpos(3, 4)
559  call assert_equal([3, 1], [line('.'), col('.')])
560  call setcursorcharpos([3, 1])
561  call assert_equal([3, 1], [line('.'), col('.')])
562  call setcursorcharpos([4, 0, 0, 0])
563  call assert_equal([4, 1], [line('.'), col('.')])
564  call setcursorcharpos([4, 20])
565  call assert_equal([4, 9], [line('.'), col('.')])
566  normal 1G
567  call setcursorcharpos([100, 100, 100, 100])
568  call assert_equal([4, 9], [line('.'), col('.')])
569  normal 1G
570  call setcursorcharpos('$', 1)
571  call assert_equal([4, 1], [line('.'), col('.')])
572
573  %bw!
574endfunc
575
576" Test for virtcol2col()
577func Test_virtcol2col()
578  new
579  call setline(1, ["a\tb\tc"])
580  call assert_equal(1, virtcol2col(0, 1, 1))
581  call assert_equal(2, virtcol2col(0, 1, 2))
582  call assert_equal(2, virtcol2col(0, 1, 8))
583  call assert_equal(3, virtcol2col(0, 1, 9))
584  call assert_equal(4, virtcol2col(0, 1, 10))
585  call assert_equal(4, virtcol2col(0, 1, 16))
586  call assert_equal(5, virtcol2col(0, 1, 17))
587  call assert_equal(-1, virtcol2col(10, 1, 1))
588  call assert_equal(-1, virtcol2col(0, 10, 1))
589  call assert_equal(-1, virtcol2col(0, -1, 1))
590  call assert_equal(-1, virtcol2col(0, 1, -1))
591  call assert_equal(5, virtcol2col(0, 1, 20))
592
593  " Multibyte character
594  call setline(1, ['a✅✅✅'])
595  call assert_equal(1, virtcol2col(0, 1, 1))
596  call assert_equal(2, virtcol2col(0, 1, 3))
597  call assert_equal(5, virtcol2col(0, 1, 5))
598  call assert_equal(8, virtcol2col(0, 1, 7))
599  call assert_equal(8, virtcol2col(0, 1, 8))
600
601  " These used to cause invalid memory access
602  call setline(1, '')
603  call assert_equal(0, virtcol2col(0, 1, 1))
604  call assert_equal(0, virtcol2col(0, 1, 2))
605
606  let w = winwidth(0)
607  call setline(2, repeat('a', w + 2))
608  let win_nosbr = win_getid()
609  split
610  setlocal showbreak=!!
611  let win_sbr = win_getid()
612  call assert_equal(w, virtcol2col(win_nosbr, 2, w))
613  call assert_equal(w + 1, virtcol2col(win_nosbr, 2, w + 1))
614  call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 2))
615  call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 3))
616  call assert_equal(w, virtcol2col(win_sbr, 2, w))
617  call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 1))
618  call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 2))
619  call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 3))
620  call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 4))
621  call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 5))
622  close
623
624  call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
625  call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
626  call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
627
628  bw!
629endfunc
630
631" vim: shiftwidth=2 sts=2 expandtab