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  close
105  call assert_equal({}, screenpos(999, 1, 1))
106  bwipe!
107
108  call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
109  nmenu WinBar.TEST :
110  call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
111  nunmenu WinBar.TEST
112endfunc
113
114func Test_screenpos_number()
115  rightbelow new
116  rightbelow 73vsplit
117  call setline (1, repeat('x', 66))
118  setlocal number
119  redraw
120  let winid = win_getid()
121  let [winrow, wincol] = win_screenpos(winid)
122  let pos = screenpos(winid, 1, 66)
123  call assert_equal(winrow, pos.row)
124  call assert_equal(wincol + 66 + 3, pos.col)
125  close
126  bwipe!
127endfunc
128
129" Save the visual start character position
130func SaveVisualStartCharPos()
131  call add(g:VisualStartPos, getcharpos('v'))
132  return ''
133endfunc
134
135" Save the current cursor character position in insert mode
136func SaveInsertCurrentCharPos()
137  call add(g:InsertCurrentPos, getcharpos('.'))
138  return ''
139endfunc
140
141" Test for the getcharpos() function
142func Test_getcharpos()
143  call assert_fails('call getcharpos({})', 'E731:')
144  call assert_equal([0, 0, 0, 0], getcharpos(0))
145  new
146  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
147
148  " Test for '.' and '$'
149  normal 1G
150  call assert_equal([0, 1, 1, 0], getcharpos('.'))
151  call assert_equal([0, 4, 1, 0], getcharpos('$'))
152  normal 2G6l
153  call assert_equal([0, 2, 7, 0], getcharpos('.'))
154  normal 3G$
155  call assert_equal([0, 3, 1, 0], getcharpos('.'))
156  normal 4G$
157  call assert_equal([0, 4, 9, 0], getcharpos('.'))
158
159  " Test for a mark
160  normal 2G7lmmgg
161  call assert_equal([0, 2, 8, 0], getcharpos("'m"))
162  delmarks m
163  call assert_equal([0, 0, 0, 0], getcharpos("'m"))
164
165  " Test for the visual start column
166  vnoremap <expr> <F3> SaveVisualStartCharPos()
167  let g:VisualStartPos = []
168  exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
169  call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
170  call assert_equal([0, 2, 9, 0], getcharpos('v'))
171  let g:VisualStartPos = []
172  exe "normal 3Gv$\<F3>o\<F3>"
173  call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
174  let g:VisualStartPos = []
175  exe "normal 1Gv$\<F3>o\<F3>"
176  call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
177  vunmap <F3>
178
179  " Test for getting the position in insert mode with the cursor after the
180  " last character in a line
181  inoremap <expr> <F3> SaveInsertCurrentCharPos()
182  let g:InsertCurrentPos = []
183  exe "normal 1GA\<F3>"
184  exe "normal 2GA\<F3>"
185  exe "normal 3GA\<F3>"
186  exe "normal 4GA\<F3>"
187  exe "normal 2G6li\<F3>"
188  call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
189                        \ [0, 2, 7, 0]], g:InsertCurrentPos)
190  iunmap <F3>
191
192  %bw!
193endfunc
194
195" Test for the setcharpos() function
196func Test_setcharpos()
197  call assert_equal(-1, setcharpos('.', test_null_list()))
198  new
199  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
200  call setcharpos('.', [0, 1, 1, 0])
201  call assert_equal([1, 1], [line('.'), col('.')])
202  call setcharpos('.', [0, 2, 7, 0])
203  call assert_equal([2, 9], [line('.'), col('.')])
204  call setcharpos('.', [0, 3, 4, 0])
205  call assert_equal([3, 1], [line('.'), col('.')])
206  call setcharpos('.', [0, 3, 1, 0])
207  call assert_equal([3, 1], [line('.'), col('.')])
208  call setcharpos('.', [0, 4, 0, 0])
209  call assert_equal([4, 1], [line('.'), col('.')])
210  call setcharpos('.', [0, 4, 20, 0])
211  call assert_equal([4, 9], [line('.'), col('.')])
212
213  " Test for mark
214  delmarks m
215  call setcharpos("'m", [0, 2, 9, 0])
216  normal `m
217  call assert_equal([2, 11], [line('.'), col('.')])
218  " unload the buffer and try to set the mark
219  let bnr = bufnr()
220  enew!
221  call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
222
223  %bw!
224  call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
225endfunc
226
227func SaveVisualStartCharCol()
228  call add(g:VisualStartCol, charcol('v'))
229  return ''
230endfunc
231
232func SaveInsertCurrentCharCol()
233  call add(g:InsertCurrentCol, charcol('.'))
234  return ''
235endfunc
236
237" Test for the charcol() function
238func Test_charcol()
239  call assert_fails('call charcol({})', 'E731:')
240  call assert_equal(0, charcol(0))
241  new
242  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
243
244  " Test for '.' and '$'
245  normal 1G
246  call assert_equal(1, charcol('.'))
247  call assert_equal(1, charcol('$'))
248  normal 2G6l
249  call assert_equal(7, charcol('.'))
250  call assert_equal(10, charcol('$'))
251  normal 3G$
252  call assert_equal(1, charcol('.'))
253  call assert_equal(2, charcol('$'))
254  normal 4G$
255  call assert_equal(9, charcol('.'))
256  call assert_equal(10, charcol('$'))
257
258  " Test for [lnum, '$']
259  call assert_equal(1, charcol([1, '$']))
260  call assert_equal(10, charcol([2, '$']))
261  call assert_equal(2, charcol([3, '$']))
262  call assert_equal(0, charcol([5, '$']))
263
264  " Test for a mark
265  normal 2G7lmmgg
266  call assert_equal(8, charcol("'m"))
267  delmarks m
268  call assert_equal(0, charcol("'m"))
269
270  " Test for the visual start column
271  vnoremap <expr> <F3> SaveVisualStartCharCol()
272  let g:VisualStartCol = []
273  exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
274  call assert_equal([7, 10, 5], g:VisualStartCol)
275  call assert_equal(9, charcol('v'))
276  let g:VisualStartCol = []
277  exe "normal 3Gv$\<F3>o\<F3>"
278  call assert_equal([1, 2], g:VisualStartCol)
279  let g:VisualStartCol = []
280  exe "normal 1Gv$\<F3>o\<F3>"
281  call assert_equal([1, 1], g:VisualStartCol)
282  vunmap <F3>
283
284  " Test for getting the column number in insert mode with the cursor after
285  " the last character in a line
286  inoremap <expr> <F3> SaveInsertCurrentCharCol()
287  let g:InsertCurrentCol = []
288  exe "normal 1GA\<F3>"
289  exe "normal 2GA\<F3>"
290  exe "normal 3GA\<F3>"
291  exe "normal 4GA\<F3>"
292  exe "normal 2G6li\<F3>"
293  call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
294  iunmap <F3>
295
296  %bw!
297endfunc
298
299func SaveInsertCursorCharPos()
300  call add(g:InsertCursorPos, getcursorcharpos('.'))
301  return ''
302endfunc
303
304" Test for getcursorcharpos()
305func Test_getcursorcharpos()
306  call assert_equal(getcursorcharpos(), getcursorcharpos(0))
307  call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
308  call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
309
310  new
311  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
312  normal 1G9l
313  call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
314  normal 2G9l
315  call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
316  normal 3G9l
317  call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
318  normal 4G9l
319  call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
320
321  " Test for getting the cursor position in insert mode with the cursor after
322  " the last character in a line
323  inoremap <expr> <F3> SaveInsertCursorCharPos()
324  let g:InsertCursorPos = []
325  exe "normal 1GA\<F3>"
326  exe "normal 2GA\<F3>"
327  exe "normal 3GA\<F3>"
328  exe "normal 4GA\<F3>"
329  exe "normal 2G6li\<F3>"
330  call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
331                    \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
332  iunmap <F3>
333
334  let winid = win_getid()
335  normal 2G5l
336  wincmd w
337  call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
338  %bw!
339endfunc
340
341" Test for setcursorcharpos()
342func Test_setcursorcharpos()
343  call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
344  call assert_fails('call setcursorcharpos([1])', 'E474:')
345  call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
346  new
347  call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
348  normal G
349  call setcursorcharpos([1, 1])
350  call assert_equal([1, 1], [line('.'), col('.')])
351  call setcursorcharpos([2, 7, 0])
352  call assert_equal([2, 9], [line('.'), col('.')])
353  call setcursorcharpos(3, 4)
354  call assert_equal([3, 1], [line('.'), col('.')])
355  call setcursorcharpos([3, 1])
356  call assert_equal([3, 1], [line('.'), col('.')])
357  call setcursorcharpos([4, 0, 0, 0])
358  call assert_equal([4, 1], [line('.'), col('.')])
359  call setcursorcharpos([4, 20])
360  call assert_equal([4, 9], [line('.'), col('.')])
361  normal 1G
362  call setcursorcharpos([100, 100, 100, 100])
363  call assert_equal([4, 9], [line('.'), col('.')])
364  normal 1G
365  call setcursorcharpos('$', 1)
366  call assert_equal([4, 1], [line('.'), col('.')])
367
368  %bw!
369endfunc
370
371" vim: shiftwidth=2 sts=2 expandtab
372