blob: d74255e19bd62dd91dd074ec33e1faef12d669c3 [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))
zeertzjqba2d1912022-12-18 12:28:59 +0000153 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 4, 1))
154 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 5, 1))
155 setlocal number
156 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 3, 1))
157 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 4, 1))
158 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 5, 1))
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000159 call assert_equal(4, screenpos(1, 6, 1).row)
160 bwipe!
161endfunc
162
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000163func Test_screenpos_diff()
164 CheckFeature diff
165
166 enew!
167 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
168 vnew
169 call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
170 windo diffthis
171 wincmd w
172 call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
173
174 windo diffoff
175 bwipe!
176 bwipe!
177endfunc
178
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100179func Test_screenpos_number()
180 rightbelow new
181 rightbelow 73vsplit
182 call setline (1, repeat('x', 66))
183 setlocal number
184 redraw
185 let winid = win_getid()
186 let [winrow, wincol] = win_screenpos(winid)
187 let pos = screenpos(winid, 1, 66)
188 call assert_equal(winrow, pos.row)
189 call assert_equal(wincol + 66 + 3, pos.col)
Bram Moolenaar99d19432022-12-05 16:23:24 +0000190
191 call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
192
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100193 close
194 bwipe!
195endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200196
Bram Moolenaar91458462021-01-13 20:08:38 +0100197" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198func SaveVisualStartCharPos()
199 call add(g:VisualStartPos, getcharpos('v'))
200 return ''
201endfunc
202
Bram Moolenaar91458462021-01-13 20:08:38 +0100203" Save the current cursor character position in insert mode
204func SaveInsertCurrentCharPos()
205 call add(g:InsertCurrentPos, getcharpos('.'))
206 return ''
207endfunc
208
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100209" Test for the getcharpos() function
210func Test_getcharpos()
211 call assert_fails('call getcharpos({})', 'E731:')
212 call assert_equal([0, 0, 0, 0], getcharpos(0))
213 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100214 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100215
216 " Test for '.' and '$'
217 normal 1G
218 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100219 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100220 normal 2G6l
221 call assert_equal([0, 2, 7, 0], getcharpos('.'))
222 normal 3G$
223 call assert_equal([0, 3, 1, 0], getcharpos('.'))
224 normal 4G$
225 call assert_equal([0, 4, 9, 0], getcharpos('.'))
226
227 " Test for a mark
228 normal 2G7lmmgg
229 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
230 delmarks m
231 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
232
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100233 " Check mark does not move
234 normal 5Gfxma
235 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
236 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
237 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
238
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100239 " Test for the visual start column
240 vnoremap <expr> <F3> SaveVisualStartCharPos()
241 let g:VisualStartPos = []
242 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100243 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100244 call assert_equal([0, 2, 9, 0], getcharpos('v'))
245 let g:VisualStartPos = []
246 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100247 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100248 let g:VisualStartPos = []
249 exe "normal 1Gv$\<F3>o\<F3>"
250 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
251 vunmap <F3>
252
Bram Moolenaar91458462021-01-13 20:08:38 +0100253 " Test for getting the position in insert mode with the cursor after the
254 " last character in a line
255 inoremap <expr> <F3> SaveInsertCurrentCharPos()
256 let g:InsertCurrentPos = []
257 exe "normal 1GA\<F3>"
258 exe "normal 2GA\<F3>"
259 exe "normal 3GA\<F3>"
260 exe "normal 4GA\<F3>"
261 exe "normal 2G6li\<F3>"
262 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
263 \ [0, 2, 7, 0]], g:InsertCurrentPos)
264 iunmap <F3>
265
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100266 %bw!
267endfunc
268
269" Test for the setcharpos() function
270func Test_setcharpos()
271 call assert_equal(-1, setcharpos('.', test_null_list()))
272 new
273 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
274 call setcharpos('.', [0, 1, 1, 0])
275 call assert_equal([1, 1], [line('.'), col('.')])
276 call setcharpos('.', [0, 2, 7, 0])
277 call assert_equal([2, 9], [line('.'), col('.')])
278 call setcharpos('.', [0, 3, 4, 0])
279 call assert_equal([3, 1], [line('.'), col('.')])
280 call setcharpos('.', [0, 3, 1, 0])
281 call assert_equal([3, 1], [line('.'), col('.')])
282 call setcharpos('.', [0, 4, 0, 0])
283 call assert_equal([4, 1], [line('.'), col('.')])
284 call setcharpos('.', [0, 4, 20, 0])
285 call assert_equal([4, 9], [line('.'), col('.')])
286
287 " Test for mark
288 delmarks m
289 call setcharpos("'m", [0, 2, 9, 0])
290 normal `m
291 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100292 " unload the buffer and try to set the mark
293 let bnr = bufnr()
294 enew!
295 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100296
297 %bw!
298 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
299endfunc
300
301func SaveVisualStartCharCol()
302 call add(g:VisualStartCol, charcol('v'))
303 return ''
304endfunc
305
Bram Moolenaar91458462021-01-13 20:08:38 +0100306func SaveInsertCurrentCharCol()
307 call add(g:InsertCurrentCol, charcol('.'))
308 return ''
309endfunc
310
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100311" Test for the charcol() function
312func Test_charcol()
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000313 call assert_fails('call charcol({})', 'E1222:')
314 call assert_fails('call charcol(".", [])', 'E1210:')
315 call assert_fails('call charcol(0)', 'E1222:')
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100316 new
317 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
318
319 " Test for '.' and '$'
320 normal 1G
321 call assert_equal(1, charcol('.'))
322 call assert_equal(1, charcol('$'))
323 normal 2G6l
324 call assert_equal(7, charcol('.'))
325 call assert_equal(10, charcol('$'))
326 normal 3G$
327 call assert_equal(1, charcol('.'))
328 call assert_equal(2, charcol('$'))
329 normal 4G$
330 call assert_equal(9, charcol('.'))
331 call assert_equal(10, charcol('$'))
332
333 " Test for [lnum, '$']
334 call assert_equal(1, charcol([1, '$']))
335 call assert_equal(10, charcol([2, '$']))
336 call assert_equal(2, charcol([3, '$']))
337 call assert_equal(0, charcol([5, '$']))
338
339 " Test for a mark
340 normal 2G7lmmgg
341 call assert_equal(8, charcol("'m"))
342 delmarks m
343 call assert_equal(0, charcol("'m"))
344
345 " Test for the visual start column
346 vnoremap <expr> <F3> SaveVisualStartCharCol()
347 let g:VisualStartCol = []
348 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100349 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100350 call assert_equal(9, charcol('v'))
351 let g:VisualStartCol = []
352 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100353 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100354 let g:VisualStartCol = []
355 exe "normal 1Gv$\<F3>o\<F3>"
356 call assert_equal([1, 1], g:VisualStartCol)
357 vunmap <F3>
358
Bram Moolenaar91458462021-01-13 20:08:38 +0100359 " Test for getting the column number in insert mode with the cursor after
360 " the last character in a line
361 inoremap <expr> <F3> SaveInsertCurrentCharCol()
362 let g:InsertCurrentCol = []
363 exe "normal 1GA\<F3>"
364 exe "normal 2GA\<F3>"
365 exe "normal 3GA\<F3>"
366 exe "normal 4GA\<F3>"
367 exe "normal 2G6li\<F3>"
368 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
369 iunmap <F3>
370
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000371 " Test for getting the column number in another window.
372 let winid = win_getid()
373 new
374 call win_execute(winid, 'normal 1G')
375 call assert_equal(1, charcol('.', winid))
376 call assert_equal(1, charcol('$', winid))
377 call win_execute(winid, 'normal 2G6l')
378 call assert_equal(7, charcol('.', winid))
379 call assert_equal(10, charcol('$', winid))
380
381 " calling from another tab page also works
382 tabnew
383 call assert_equal(7, charcol('.', winid))
384 call assert_equal(10, charcol('$', winid))
385 tabclose
386
387 " unknown window ID
388 call assert_equal(0, charcol('.', 10001))
389
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100390 %bw!
391endfunc
392
Bram Moolenaar91458462021-01-13 20:08:38 +0100393func SaveInsertCursorCharPos()
394 call add(g:InsertCursorPos, getcursorcharpos('.'))
395 return ''
396endfunc
397
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100398" Test for getcursorcharpos()
399func Test_getcursorcharpos()
400 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
401 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
402 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
403
404 new
405 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
406 normal 1G9l
407 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
408 normal 2G9l
409 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
410 normal 3G9l
411 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
412 normal 4G9l
413 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
414
Bram Moolenaar91458462021-01-13 20:08:38 +0100415 " Test for getting the cursor position in insert mode with the cursor after
416 " the last character in a line
417 inoremap <expr> <F3> SaveInsertCursorCharPos()
418 let g:InsertCursorPos = []
419 exe "normal 1GA\<F3>"
420 exe "normal 2GA\<F3>"
421 exe "normal 3GA\<F3>"
422 exe "normal 4GA\<F3>"
423 exe "normal 2G6li\<F3>"
424 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
425 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
426 iunmap <F3>
427
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100428 let winid = win_getid()
429 normal 2G5l
430 wincmd w
431 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
432 %bw!
433endfunc
434
435" Test for setcursorcharpos()
436func Test_setcursorcharpos()
437 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
438 call assert_fails('call setcursorcharpos([1])', 'E474:')
439 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
440 new
441 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
442 normal G
443 call setcursorcharpos([1, 1])
444 call assert_equal([1, 1], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100445
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100446 call setcursorcharpos([2, 7, 0])
447 call assert_equal([2, 9], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100448 call setcursorcharpos([0, 7, 0])
449 call assert_equal([2, 9], [line('.'), col('.')])
450 call setcursorcharpos(0, 7, 0)
451 call assert_equal([2, 9], [line('.'), col('.')])
452
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100453 call setcursorcharpos(3, 4)
454 call assert_equal([3, 1], [line('.'), col('.')])
455 call setcursorcharpos([3, 1])
456 call assert_equal([3, 1], [line('.'), col('.')])
457 call setcursorcharpos([4, 0, 0, 0])
458 call assert_equal([4, 1], [line('.'), col('.')])
459 call setcursorcharpos([4, 20])
460 call assert_equal([4, 9], [line('.'), col('.')])
461 normal 1G
462 call setcursorcharpos([100, 100, 100, 100])
463 call assert_equal([4, 9], [line('.'), col('.')])
464 normal 1G
465 call setcursorcharpos('$', 1)
466 call assert_equal([4, 1], [line('.'), col('.')])
467
468 %bw!
469endfunc
470
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100471" Test for virtcol2col()
472func Test_virtcol2col()
473 new
474 call setline(1, ["a\tb\tc"])
475 call assert_equal(1, virtcol2col(0, 1, 1))
476 call assert_equal(2, virtcol2col(0, 1, 2))
477 call assert_equal(2, virtcol2col(0, 1, 8))
478 call assert_equal(3, virtcol2col(0, 1, 9))
479 call assert_equal(4, virtcol2col(0, 1, 10))
480 call assert_equal(4, virtcol2col(0, 1, 16))
481 call assert_equal(5, virtcol2col(0, 1, 17))
482 call assert_equal(-1, virtcol2col(10, 1, 1))
483 call assert_equal(-1, virtcol2col(0, 10, 1))
484 call assert_equal(-1, virtcol2col(0, -1, 1))
485 call assert_equal(-1, virtcol2col(0, 1, -1))
486 call assert_equal(5, virtcol2col(0, 1, 20))
487 call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
488 call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
489 call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
490 bw!
491endfunc
492
Bram Moolenaar08f41572020-04-20 16:50:00 +0200493" vim: shiftwidth=2 sts=2 expandtab