1" Tests for cursor() and other functions that get/set the cursor position
2
3func Test_wrong_arguments()
4  call assert_fails('call cursor(1. 3)', 'E474:')
5  call assert_fails('call cursor(test_null_list())', 'E474:')
6endfunc
7
8func Test_move_cursor()
9  new
10  call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
11
12  call cursor([1, 1, 0, 1])
13  call assert_equal([1, 1, 0, 1], getcurpos()[1:])
14  call cursor([4, 3, 0, 3])
15  call assert_equal([4, 3, 0, 3], getcurpos()[1:])
16
17  call cursor(2, 2)
18  call assert_equal([2, 2, 0, 2], getcurpos()[1:])
19  " line number zero keeps the line number
20  call cursor(0, 1)
21  call assert_equal([2, 1, 0, 1], getcurpos()[1:])
22  " col number zero keeps the column
23  call cursor(3, 0)
24  call assert_equal([3, 1, 0, 1], getcurpos()[1:])
25  " below last line goes to last line
26  eval [9, 1]->cursor()
27  call assert_equal([4, 1, 0, 1], getcurpos()[1:])
28  " pass string arguments
29  call cursor('3', '3')
30  call assert_equal([3, 3, 0, 3], getcurpos()[1:])
31
32  call setline(1, ["\<TAB>"])
33  call cursor(1, 1, 1)
34  call assert_equal([1, 1, 1], getcurpos()[1:3])
35
36  call assert_fails('call cursor(-1, -1)', 'E475:')
37
38  quit!
39endfunc
40
41" Very short version of what matchparen does.
42function s:Highlight_Matching_Pair()
43  let save_cursor = getcurpos()
44  eval save_cursor->setpos('.')
45endfunc
46
47func Test_curswant_with_autocommand()
48  new
49  call setline(1, ['func()', '{', '}', '----'])
50  autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
51  call test_override("char_avail", 1)
52  exe "normal! 3Ga\<Down>X\<Esc>"
53  call test_override("char_avail", 0)
54  call assert_equal('-X---', getline(4))
55  autocmd! CursorMovedI *
56  quit!
57endfunc
58
59" Tests for behavior of curswant with cursorcolumn/line
60func Test_curswant_with_cursorcolumn()
61  new
62  call setline(1, ['01234567', ''])
63  exe "normal! ggf6j"
64  call assert_equal(6, winsaveview().curswant)
65  set cursorcolumn
66  call assert_equal(6, winsaveview().curswant)
67  quit!
68endfunc
69
70func Test_curswant_with_cursorline()
71  new
72  call setline(1, ['01234567', ''])
73  exe "normal! ggf6j"
74  call assert_equal(6, winsaveview().curswant)
75  set cursorline
76  call assert_equal(6, winsaveview().curswant)
77  quit!
78endfunc
79
80func Test_screenpos()
81  rightbelow new
82  rightbelow 20vsplit
83  call setline(1, ["\tsome text", "long wrapping line here", "next line"])
84  redraw
85  let winid = win_getid()
86  let [winrow, wincol] = win_screenpos(winid)
87  call assert_equal({'row': winrow,
88	\ 'col': wincol + 0,
89	\ 'curscol': wincol + 7,
90	\ 'endcol': wincol + 7}, winid->screenpos(1, 1))
91  call assert_equal({'row': winrow,
92	\ 'col': wincol + 13,
93	\ 'curscol': wincol + 13,
94	\ 'endcol': wincol + 13}, winid->screenpos(1, 7))
95  call assert_equal({'row': winrow + 2,
96	\ 'col': wincol + 1,
97	\ 'curscol': wincol + 1,
98	\ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
99  setlocal number
100  call assert_equal({'row': winrow + 3,
101	\ 'col': wincol + 9,
102	\ 'curscol': wincol + 9,
103	\ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
104
105  let wininfo = getwininfo(winid)[0]
106  call setline(3, ['x']->repeat(wininfo.height))
107  call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3))
108  setlocal nonumber display=lastline so=0
109  exe "normal G\<C-Y>\<C-Y>"
110  redraw
111  call assert_equal({'row': winrow + wininfo.height - 1,
112	\ 'col': wincol + 7,
113	\ 'curscol': wincol + 7,
114	\ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8))
115  call assert_equal({'row': winrow - 1, 'col': 0, 'curscol': 0, 'endcol': 0},
116        \ winid->screenpos(line('$'), 22))
117
118  close
119  call assert_equal({}, screenpos(999, 1, 1))
120
121  bwipe!
122  set display&
123
124  call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
125  nmenu WinBar.TEST :
126  call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
127  nunmenu WinBar.TEST
128endfunc
129
130func Test_screenpos_number()
131  rightbelow new
132  rightbelow 73vsplit
133  call setline (1, repeat('x', 66))
134  setlocal number
135  redraw
136  let winid = win_getid()
137  let [winrow, wincol] = win_screenpos(winid)
138  let pos = screenpos(winid, 1, 66)
139  call assert_equal(winrow, pos.row)
140  call assert_equal(wincol + 66 + 3, pos.col)
141  close
142  bwipe!
143endfunc
144
145" Save the visual start character position
146func SaveVisualStartCharPos()
147  call add(g:VisualStartPos, getcharpos('v'))
148  return ''
149endfunc
150
151" Save the current cursor character position in insert mode
152func SaveInsertCurrentCharPos()
153  call add(g:InsertCurrentPos, getcharpos('.'))
154  return ''
155endfunc
156
157" Test for the getcharpos() function
158func Test_getcharpos()
159  call assert_fails('call getcharpos({})', 'E731:')
160  call assert_equal([0, 0, 0, 0], getcharpos(0))
161  new
162  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
163
164  " Test for '.' and '$'
165  normal 1G
166  call assert_equal([0, 1, 1, 0], getcharpos('.'))
167  call assert_equal([0, 4, 1, 0], getcharpos('$'))
168  normal 2G6l
169  call assert_equal([0, 2, 7, 0], getcharpos('.'))
170  normal 3G$
171  call assert_equal([0, 3, 1, 0], getcharpos('.'))
172  normal 4G$
173  call assert_equal([0, 4, 9, 0], getcharpos('.'))
174
175  " Test for a mark
176  normal 2G7lmmgg
177  call assert_equal([0, 2, 8, 0], getcharpos("'m"))
178  delmarks m
179  call assert_equal([0, 0, 0, 0], getcharpos("'m"))
180
181  " Test for the visual start column
182  vnoremap <expr> <F3> SaveVisualStartCharPos()
183  let g:VisualStartPos = []
184  exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
185  call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
186  call assert_equal([0, 2, 9, 0], getcharpos('v'))
187  let g:VisualStartPos = []
188  exe "normal 3Gv$\<F3>o\<F3>"
189  call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
190  let g:VisualStartPos = []
191  exe "normal 1Gv$\<F3>o\<F3>"
192  call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
193  vunmap <F3>
194
195  " Test for getting the position in insert mode with the cursor after the
196  " last character in a line
197  inoremap <expr> <F3> SaveInsertCurrentCharPos()
198  let g:InsertCurrentPos = []
199  exe "normal 1GA\<F3>"
200  exe "normal 2GA\<F3>"
201  exe "normal 3GA\<F3>"
202  exe "normal 4GA\<F3>"
203  exe "normal 2G6li\<F3>"
204  call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
205                        \ [0, 2, 7, 0]], g:InsertCurrentPos)
206  iunmap <F3>
207
208  %bw!
209endfunc
210
211" Test for the setcharpos() function
212func Test_setcharpos()
213  call assert_equal(-1, setcharpos('.', test_null_list()))
214  new
215  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
216  call setcharpos('.', [0, 1, 1, 0])
217  call assert_equal([1, 1], [line('.'), col('.')])
218  call setcharpos('.', [0, 2, 7, 0])
219  call assert_equal([2, 9], [line('.'), col('.')])
220  call setcharpos('.', [0, 3, 4, 0])
221  call assert_equal([3, 1], [line('.'), col('.')])
222  call setcharpos('.', [0, 3, 1, 0])
223  call assert_equal([3, 1], [line('.'), col('.')])
224  call setcharpos('.', [0, 4, 0, 0])
225  call assert_equal([4, 1], [line('.'), col('.')])
226  call setcharpos('.', [0, 4, 20, 0])
227  call assert_equal([4, 9], [line('.'), col('.')])
228
229  " Test for mark
230  delmarks m
231  call setcharpos("'m", [0, 2, 9, 0])
232  normal `m
233  call assert_equal([2, 11], [line('.'), col('.')])
234  " unload the buffer and try to set the mark
235  let bnr = bufnr()
236  enew!
237  call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
238
239  %bw!
240  call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
241endfunc
242
243func SaveVisualStartCharCol()
244  call add(g:VisualStartCol, charcol('v'))
245  return ''
246endfunc
247
248func SaveInsertCurrentCharCol()
249  call add(g:InsertCurrentCol, charcol('.'))
250  return ''
251endfunc
252
253" Test for the charcol() function
254func Test_charcol()
255  call assert_fails('call charcol({})', 'E731:')
256  call assert_equal(0, charcol(0))
257  new
258  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
259
260  " Test for '.' and '$'
261  normal 1G
262  call assert_equal(1, charcol('.'))
263  call assert_equal(1, charcol('$'))
264  normal 2G6l
265  call assert_equal(7, charcol('.'))
266  call assert_equal(10, charcol('$'))
267  normal 3G$
268  call assert_equal(1, charcol('.'))
269  call assert_equal(2, charcol('$'))
270  normal 4G$
271  call assert_equal(9, charcol('.'))
272  call assert_equal(10, charcol('$'))
273
274  " Test for [lnum, '$']
275  call assert_equal(1, charcol([1, '$']))
276  call assert_equal(10, charcol([2, '$']))
277  call assert_equal(2, charcol([3, '$']))
278  call assert_equal(0, charcol([5, '$']))
279
280  " Test for a mark
281  normal 2G7lmmgg
282  call assert_equal(8, charcol("'m"))
283  delmarks m
284  call assert_equal(0, charcol("'m"))
285
286  " Test for the visual start column
287  vnoremap <expr> <F3> SaveVisualStartCharCol()
288  let g:VisualStartCol = []
289  exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
290  call assert_equal([7, 10, 5], g:VisualStartCol)
291  call assert_equal(9, charcol('v'))
292  let g:VisualStartCol = []
293  exe "normal 3Gv$\<F3>o\<F3>"
294  call assert_equal([1, 2], g:VisualStartCol)
295  let g:VisualStartCol = []
296  exe "normal 1Gv$\<F3>o\<F3>"
297  call assert_equal([1, 1], g:VisualStartCol)
298  vunmap <F3>
299
300  " Test for getting the column number in insert mode with the cursor after
301  " the last character in a line
302  inoremap <expr> <F3> SaveInsertCurrentCharCol()
303  let g:InsertCurrentCol = []
304  exe "normal 1GA\<F3>"
305  exe "normal 2GA\<F3>"
306  exe "normal 3GA\<F3>"
307  exe "normal 4GA\<F3>"
308  exe "normal 2G6li\<F3>"
309  call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
310  iunmap <F3>
311
312  %bw!
313endfunc
314
315func SaveInsertCursorCharPos()
316  call add(g:InsertCursorPos, getcursorcharpos('.'))
317  return ''
318endfunc
319
320" Test for getcursorcharpos()
321func Test_getcursorcharpos()
322  call assert_equal(getcursorcharpos(), getcursorcharpos(0))
323  call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
324  call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
325
326  new
327  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
328  normal 1G9l
329  call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
330  normal 2G9l
331  call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
332  normal 3G9l
333  call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
334  normal 4G9l
335  call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
336
337  " Test for getting the cursor position in insert mode with the cursor after
338  " the last character in a line
339  inoremap <expr> <F3> SaveInsertCursorCharPos()
340  let g:InsertCursorPos = []
341  exe "normal 1GA\<F3>"
342  exe "normal 2GA\<F3>"
343  exe "normal 3GA\<F3>"
344  exe "normal 4GA\<F3>"
345  exe "normal 2G6li\<F3>"
346  call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
347                    \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
348  iunmap <F3>
349
350  let winid = win_getid()
351  normal 2G5l
352  wincmd w
353  call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
354  %bw!
355endfunc
356
357" Test for setcursorcharpos()
358func Test_setcursorcharpos()
359  call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
360  call assert_fails('call setcursorcharpos([1])', 'E474:')
361  call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
362  new
363  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
364  normal G
365  call setcursorcharpos([1, 1])
366  call assert_equal([1, 1], [line('.'), col('.')])
367  call setcursorcharpos([2, 7, 0])
368  call assert_equal([2, 9], [line('.'), col('.')])
369  call setcursorcharpos(3, 4)
370  call assert_equal([3, 1], [line('.'), col('.')])
371  call setcursorcharpos([3, 1])
372  call assert_equal([3, 1], [line('.'), col('.')])
373  call setcursorcharpos([4, 0, 0, 0])
374  call assert_equal([4, 1], [line('.'), col('.')])
375  call setcursorcharpos([4, 20])
376  call assert_equal([4, 9], [line('.'), col('.')])
377  normal 1G
378  call setcursorcharpos([100, 100, 100, 100])
379  call assert_equal([4, 9], [line('.'), col('.')])
380  normal 1G
381  call setcursorcharpos('$', 1)
382  call assert_equal([4, 1], [line('.'), col('.')])
383
384  %bw!
385endfunc
386
387" vim: shiftwidth=2 sts=2 expandtab
388