blob: fe64be0e71f51be84bcb1824d522edfe7d9c0f65 [file] [log] [blame]
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001" Tests for cursor() and other functions that get/set the cursor position
Bram Moolenaar5a46a582016-01-15 15:56:58 +01002
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00003source check.vim
4
Bram Moolenaar5a46a582016-01-15 15:56:58 +01005func Test_wrong_arguments()
Bram Moolenaar37175402017-03-18 20:18:45 +01006 call assert_fails('call cursor(1. 3)', 'E474:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02007 call assert_fails('call cursor(test_null_list())', 'E474:')
Bram Moolenaar5a46a582016-01-15 15:56:58 +01008endfunc
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)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010020 call assert_equal([2, 2, 0, 2], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010021 " line number zero keeps the line number
22 call cursor(0, 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010023 call assert_equal([2, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010024 " col number zero keeps the column
25 call cursor(3, 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010026 call assert_equal([3, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010027 " below last line goes to last line
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020028 eval [9, 1]->cursor()
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010029 call assert_equal([4, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar9ebcf232021-01-16 16:52:49 +010030 " pass string arguments
31 call cursor('3', '3')
32 call assert_equal([3, 3, 0, 3], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010033
Bram Moolenaar17aca702019-05-16 22:24:55 +020034 call setline(1, ["\<TAB>"])
35 call cursor(1, 1, 1)
36 call assert_equal([1, 1, 1], getcurpos()[1:3])
37
Bram Moolenaar9a963372020-12-21 21:58:46 +010038 call assert_fails('call cursor(-1, -1)', 'E475:')
Bram Moolenaar17aca702019-05-16 22:24:55 +020039
Bram Moolenaar5a46a582016-01-15 15:56:58 +010040 quit!
41endfunc
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010042
naohiro ono56200ee2022-01-01 14:59:44 +000043func 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
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010055" Very short version of what matchparen does.
56function s:Highlight_Matching_Pair()
57 let save_cursor = getcurpos()
Bram Moolenaaraad222c2019-09-06 22:46:09 +020058 eval save_cursor->setpos('.')
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010059endfunc
60
61func Test_curswant_with_autocommand()
62 new
63 call setline(1, ['func()', '{', '}', '----'])
64 autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010065 call test_override("char_avail", 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010066 exe "normal! 3Ga\<Down>X\<Esc>"
Bram Moolenaareb992cb2017-03-09 18:20:16 +010067 call test_override("char_avail", 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010068 call assert_equal('-X---', getline(4))
69 autocmd! CursorMovedI *
70 quit!
71endfunc
72
Bram Moolenaar177ab9e2019-01-15 21:12:57 +010073" Tests for behavior of curswant with cursorcolumn/line
74func Test_curswant_with_cursorcolumn()
75 new
76 call setline(1, ['01234567', ''])
77 exe "normal! ggf6j"
78 call assert_equal(6, winsaveview().curswant)
79 set cursorcolumn
80 call assert_equal(6, winsaveview().curswant)
81 quit!
82endfunc
83
84func Test_curswant_with_cursorline()
85 new
86 call setline(1, ['01234567', ''])
87 exe "normal! ggf6j"
88 call assert_equal(6, winsaveview().curswant)
89 set cursorline
90 call assert_equal(6, winsaveview().curswant)
91 quit!
92endfunc
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020093
94func Test_screenpos()
95 rightbelow new
96 rightbelow 20vsplit
97 call setline(1, ["\tsome text", "long wrapping line here", "next line"])
98 redraw
99 let winid = win_getid()
100 let [winrow, wincol] = win_screenpos(winid)
101 call assert_equal({'row': winrow,
102 \ 'col': wincol + 0,
103 \ 'curscol': wincol + 7,
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200104 \ 'endcol': wincol + 7}, winid->screenpos(1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200105 call assert_equal({'row': winrow,
106 \ 'col': wincol + 13,
107 \ 'curscol': wincol + 13,
Bram Moolenaar196b4662019-09-06 21:34:30 +0200108 \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200109 call assert_equal({'row': winrow + 2,
110 \ 'col': wincol + 1,
111 \ 'curscol': wincol + 1,
112 \ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
113 setlocal number
114 call assert_equal({'row': winrow + 3,
115 \ 'col': wincol + 9,
116 \ 'curscol': wincol + 9,
117 \ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200118
119 let wininfo = getwininfo(winid)[0]
120 call setline(3, ['x']->repeat(wininfo.height))
121 call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3))
122 setlocal nonumber display=lastline so=0
123 exe "normal G\<C-Y>\<C-Y>"
124 redraw
125 call assert_equal({'row': winrow + wininfo.height - 1,
126 \ 'col': wincol + 7,
127 \ 'curscol': wincol + 7,
128 \ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8))
Bram Moolenaar7924a172022-01-24 16:15:15 +0000129 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
Bram Moolenaar189663b2021-07-21 18:04:56 +0200130 \ winid->screenpos(line('$'), 22))
131
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200132 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200133 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200134
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200135 bwipe!
Bram Moolenaar189663b2021-07-21 18:04:56 +0200136 set display&
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100137
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000138 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100139 nmenu WinBar.TEST :
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000140 call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100141 nunmenu WinBar.TEST
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200142endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100143
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000144func Test_screenpos_fold()
145 CheckFeature folding
146
147 enew!
148 call setline(1, range(10))
149 3,5fold
150 redraw
151 call assert_equal(2, screenpos(1, 2, 1).row)
152 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
153 call assert_equal(3, screenpos(1, 4, 1).row)
154 call assert_equal(3, screenpos(1, 5, 1).row)
155 call assert_equal(4, screenpos(1, 6, 1).row)
156 bwipe!
157endfunc
158
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000159func Test_screenpos_diff()
160 CheckFeature diff
161
162 enew!
163 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
164 vnew
165 call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
166 windo diffthis
167 wincmd w
168 call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
169
170 windo diffoff
171 bwipe!
172 bwipe!
173endfunc
174
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100175func Test_screenpos_number()
176 rightbelow new
177 rightbelow 73vsplit
178 call setline (1, repeat('x', 66))
179 setlocal number
180 redraw
181 let winid = win_getid()
182 let [winrow, wincol] = win_screenpos(winid)
183 let pos = screenpos(winid, 1, 66)
184 call assert_equal(winrow, pos.row)
185 call assert_equal(wincol + 66 + 3, pos.col)
Bram Moolenaar99d19432022-12-05 16:23:24 +0000186
187 call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
188
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100189 close
190 bwipe!
191endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200192
Bram Moolenaar91458462021-01-13 20:08:38 +0100193" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100194func SaveVisualStartCharPos()
195 call add(g:VisualStartPos, getcharpos('v'))
196 return ''
197endfunc
198
Bram Moolenaar91458462021-01-13 20:08:38 +0100199" Save the current cursor character position in insert mode
200func SaveInsertCurrentCharPos()
201 call add(g:InsertCurrentPos, getcharpos('.'))
202 return ''
203endfunc
204
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100205" Test for the getcharpos() function
206func Test_getcharpos()
207 call assert_fails('call getcharpos({})', 'E731:')
208 call assert_equal([0, 0, 0, 0], getcharpos(0))
209 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100210 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100211
212 " Test for '.' and '$'
213 normal 1G
214 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100215 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100216 normal 2G6l
217 call assert_equal([0, 2, 7, 0], getcharpos('.'))
218 normal 3G$
219 call assert_equal([0, 3, 1, 0], getcharpos('.'))
220 normal 4G$
221 call assert_equal([0, 4, 9, 0], getcharpos('.'))
222
223 " Test for a mark
224 normal 2G7lmmgg
225 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
226 delmarks m
227 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
228
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100229 " Check mark does not move
230 normal 5Gfxma
231 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
232 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
233 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
234
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100235 " Test for the visual start column
236 vnoremap <expr> <F3> SaveVisualStartCharPos()
237 let g:VisualStartPos = []
238 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100239 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100240 call assert_equal([0, 2, 9, 0], getcharpos('v'))
241 let g:VisualStartPos = []
242 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100243 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100244 let g:VisualStartPos = []
245 exe "normal 1Gv$\<F3>o\<F3>"
246 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
247 vunmap <F3>
248
Bram Moolenaar91458462021-01-13 20:08:38 +0100249 " Test for getting the position in insert mode with the cursor after the
250 " last character in a line
251 inoremap <expr> <F3> SaveInsertCurrentCharPos()
252 let g:InsertCurrentPos = []
253 exe "normal 1GA\<F3>"
254 exe "normal 2GA\<F3>"
255 exe "normal 3GA\<F3>"
256 exe "normal 4GA\<F3>"
257 exe "normal 2G6li\<F3>"
258 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
259 \ [0, 2, 7, 0]], g:InsertCurrentPos)
260 iunmap <F3>
261
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100262 %bw!
263endfunc
264
265" Test for the setcharpos() function
266func Test_setcharpos()
267 call assert_equal(-1, setcharpos('.', test_null_list()))
268 new
269 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
270 call setcharpos('.', [0, 1, 1, 0])
271 call assert_equal([1, 1], [line('.'), col('.')])
272 call setcharpos('.', [0, 2, 7, 0])
273 call assert_equal([2, 9], [line('.'), col('.')])
274 call setcharpos('.', [0, 3, 4, 0])
275 call assert_equal([3, 1], [line('.'), col('.')])
276 call setcharpos('.', [0, 3, 1, 0])
277 call assert_equal([3, 1], [line('.'), col('.')])
278 call setcharpos('.', [0, 4, 0, 0])
279 call assert_equal([4, 1], [line('.'), col('.')])
280 call setcharpos('.', [0, 4, 20, 0])
281 call assert_equal([4, 9], [line('.'), col('.')])
282
283 " Test for mark
284 delmarks m
285 call setcharpos("'m", [0, 2, 9, 0])
286 normal `m
287 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100288 " unload the buffer and try to set the mark
289 let bnr = bufnr()
290 enew!
291 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100292
293 %bw!
294 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
295endfunc
296
297func SaveVisualStartCharCol()
298 call add(g:VisualStartCol, charcol('v'))
299 return ''
300endfunc
301
Bram Moolenaar91458462021-01-13 20:08:38 +0100302func SaveInsertCurrentCharCol()
303 call add(g:InsertCurrentCol, charcol('.'))
304 return ''
305endfunc
306
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100307" Test for the charcol() function
308func Test_charcol()
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000309 call assert_fails('call charcol({})', 'E1222:')
310 call assert_fails('call charcol(".", [])', 'E1210:')
311 call assert_fails('call charcol(0)', 'E1222:')
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100312 new
313 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
314
315 " Test for '.' and '$'
316 normal 1G
317 call assert_equal(1, charcol('.'))
318 call assert_equal(1, charcol('$'))
319 normal 2G6l
320 call assert_equal(7, charcol('.'))
321 call assert_equal(10, charcol('$'))
322 normal 3G$
323 call assert_equal(1, charcol('.'))
324 call assert_equal(2, charcol('$'))
325 normal 4G$
326 call assert_equal(9, charcol('.'))
327 call assert_equal(10, charcol('$'))
328
329 " Test for [lnum, '$']
330 call assert_equal(1, charcol([1, '$']))
331 call assert_equal(10, charcol([2, '$']))
332 call assert_equal(2, charcol([3, '$']))
333 call assert_equal(0, charcol([5, '$']))
334
335 " Test for a mark
336 normal 2G7lmmgg
337 call assert_equal(8, charcol("'m"))
338 delmarks m
339 call assert_equal(0, charcol("'m"))
340
341 " Test for the visual start column
342 vnoremap <expr> <F3> SaveVisualStartCharCol()
343 let g:VisualStartCol = []
344 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100345 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100346 call assert_equal(9, charcol('v'))
347 let g:VisualStartCol = []
348 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100349 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100350 let g:VisualStartCol = []
351 exe "normal 1Gv$\<F3>o\<F3>"
352 call assert_equal([1, 1], g:VisualStartCol)
353 vunmap <F3>
354
Bram Moolenaar91458462021-01-13 20:08:38 +0100355 " Test for getting the column number in insert mode with the cursor after
356 " the last character in a line
357 inoremap <expr> <F3> SaveInsertCurrentCharCol()
358 let g:InsertCurrentCol = []
359 exe "normal 1GA\<F3>"
360 exe "normal 2GA\<F3>"
361 exe "normal 3GA\<F3>"
362 exe "normal 4GA\<F3>"
363 exe "normal 2G6li\<F3>"
364 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
365 iunmap <F3>
366
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000367 " Test for getting the column number in another window.
368 let winid = win_getid()
369 new
370 call win_execute(winid, 'normal 1G')
371 call assert_equal(1, charcol('.', winid))
372 call assert_equal(1, charcol('$', winid))
373 call win_execute(winid, 'normal 2G6l')
374 call assert_equal(7, charcol('.', winid))
375 call assert_equal(10, charcol('$', winid))
376
377 " calling from another tab page also works
378 tabnew
379 call assert_equal(7, charcol('.', winid))
380 call assert_equal(10, charcol('$', winid))
381 tabclose
382
383 " unknown window ID
384 call assert_equal(0, charcol('.', 10001))
385
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100386 %bw!
387endfunc
388
Bram Moolenaar91458462021-01-13 20:08:38 +0100389func SaveInsertCursorCharPos()
390 call add(g:InsertCursorPos, getcursorcharpos('.'))
391 return ''
392endfunc
393
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100394" Test for getcursorcharpos()
395func Test_getcursorcharpos()
396 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
397 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
398 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
399
400 new
401 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
402 normal 1G9l
403 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
404 normal 2G9l
405 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
406 normal 3G9l
407 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
408 normal 4G9l
409 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
410
Bram Moolenaar91458462021-01-13 20:08:38 +0100411 " Test for getting the cursor position in insert mode with the cursor after
412 " the last character in a line
413 inoremap <expr> <F3> SaveInsertCursorCharPos()
414 let g:InsertCursorPos = []
415 exe "normal 1GA\<F3>"
416 exe "normal 2GA\<F3>"
417 exe "normal 3GA\<F3>"
418 exe "normal 4GA\<F3>"
419 exe "normal 2G6li\<F3>"
420 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
421 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
422 iunmap <F3>
423
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100424 let winid = win_getid()
425 normal 2G5l
426 wincmd w
427 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
428 %bw!
429endfunc
430
431" Test for setcursorcharpos()
432func Test_setcursorcharpos()
433 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
434 call assert_fails('call setcursorcharpos([1])', 'E474:')
435 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
436 new
437 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
438 normal G
439 call setcursorcharpos([1, 1])
440 call assert_equal([1, 1], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100441
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100442 call setcursorcharpos([2, 7, 0])
443 call assert_equal([2, 9], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100444 call setcursorcharpos([0, 7, 0])
445 call assert_equal([2, 9], [line('.'), col('.')])
446 call setcursorcharpos(0, 7, 0)
447 call assert_equal([2, 9], [line('.'), col('.')])
448
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100449 call setcursorcharpos(3, 4)
450 call assert_equal([3, 1], [line('.'), col('.')])
451 call setcursorcharpos([3, 1])
452 call assert_equal([3, 1], [line('.'), col('.')])
453 call setcursorcharpos([4, 0, 0, 0])
454 call assert_equal([4, 1], [line('.'), col('.')])
455 call setcursorcharpos([4, 20])
456 call assert_equal([4, 9], [line('.'), col('.')])
457 normal 1G
458 call setcursorcharpos([100, 100, 100, 100])
459 call assert_equal([4, 9], [line('.'), col('.')])
460 normal 1G
461 call setcursorcharpos('$', 1)
462 call assert_equal([4, 1], [line('.'), col('.')])
463
464 %bw!
465endfunc
466
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100467" Test for virtcol2col()
468func Test_virtcol2col()
469 new
470 call setline(1, ["a\tb\tc"])
471 call assert_equal(1, virtcol2col(0, 1, 1))
472 call assert_equal(2, virtcol2col(0, 1, 2))
473 call assert_equal(2, virtcol2col(0, 1, 8))
474 call assert_equal(3, virtcol2col(0, 1, 9))
475 call assert_equal(4, virtcol2col(0, 1, 10))
476 call assert_equal(4, virtcol2col(0, 1, 16))
477 call assert_equal(5, virtcol2col(0, 1, 17))
478 call assert_equal(-1, virtcol2col(10, 1, 1))
479 call assert_equal(-1, virtcol2col(0, 10, 1))
480 call assert_equal(-1, virtcol2col(0, -1, 1))
481 call assert_equal(-1, virtcol2col(0, 1, -1))
482 call assert_equal(5, virtcol2col(0, 1, 20))
483 call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
484 call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
485 call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
486 bw!
487endfunc
488
Bram Moolenaar08f41572020-04-20 16:50:00 +0200489" vim: shiftwidth=2 sts=2 expandtab