blob: 19b3416b797c37266a74c7d562009b639982b370 [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
3func Test_wrong_arguments()
Bram Moolenaar37175402017-03-18 20:18:45 +01004 call assert_fails('call cursor(1. 3)', 'E474:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005 call assert_fails('call cursor(test_null_list())', 'E474:')
Bram Moolenaar5a46a582016-01-15 15:56:58 +01006endfunc
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)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010018 call assert_equal([2, 2, 0, 2], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010019 " line number zero keeps the line number
20 call cursor(0, 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010021 call assert_equal([2, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010022 " col number zero keeps the column
23 call cursor(3, 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010024 call assert_equal([3, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010025 " below last line goes to last line
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020026 eval [9, 1]->cursor()
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010027 call assert_equal([4, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010028
Bram Moolenaar17aca702019-05-16 22:24:55 +020029 call setline(1, ["\<TAB>"])
30 call cursor(1, 1, 1)
31 call assert_equal([1, 1, 1], getcurpos()[1:3])
32
Bram Moolenaar9a963372020-12-21 21:58:46 +010033 call assert_fails('call cursor(-1, -1)', 'E475:')
Bram Moolenaar17aca702019-05-16 22:24:55 +020034
Bram Moolenaar5a46a582016-01-15 15:56:58 +010035 quit!
36endfunc
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010037
38" Very short version of what matchparen does.
39function s:Highlight_Matching_Pair()
40 let save_cursor = getcurpos()
Bram Moolenaaraad222c2019-09-06 22:46:09 +020041 eval save_cursor->setpos('.')
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010042endfunc
43
44func Test_curswant_with_autocommand()
45 new
46 call setline(1, ['func()', '{', '}', '----'])
47 autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010048 call test_override("char_avail", 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010049 exe "normal! 3Ga\<Down>X\<Esc>"
Bram Moolenaareb992cb2017-03-09 18:20:16 +010050 call test_override("char_avail", 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010051 call assert_equal('-X---', getline(4))
52 autocmd! CursorMovedI *
53 quit!
54endfunc
55
Bram Moolenaar177ab9e2019-01-15 21:12:57 +010056" Tests for behavior of curswant with cursorcolumn/line
57func Test_curswant_with_cursorcolumn()
58 new
59 call setline(1, ['01234567', ''])
60 exe "normal! ggf6j"
61 call assert_equal(6, winsaveview().curswant)
62 set cursorcolumn
63 call assert_equal(6, winsaveview().curswant)
64 quit!
65endfunc
66
67func Test_curswant_with_cursorline()
68 new
69 call setline(1, ['01234567', ''])
70 exe "normal! ggf6j"
71 call assert_equal(6, winsaveview().curswant)
72 set cursorline
73 call assert_equal(6, winsaveview().curswant)
74 quit!
75endfunc
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020076
77func Test_screenpos()
78 rightbelow new
79 rightbelow 20vsplit
80 call setline(1, ["\tsome text", "long wrapping line here", "next line"])
81 redraw
82 let winid = win_getid()
83 let [winrow, wincol] = win_screenpos(winid)
84 call assert_equal({'row': winrow,
85 \ 'col': wincol + 0,
86 \ 'curscol': wincol + 7,
Bram Moolenaarf92e58c2019-09-08 21:51:41 +020087 \ 'endcol': wincol + 7}, winid->screenpos(1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020088 call assert_equal({'row': winrow,
89 \ 'col': wincol + 13,
90 \ 'curscol': wincol + 13,
Bram Moolenaar196b4662019-09-06 21:34:30 +020091 \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020092 call assert_equal({'row': winrow + 2,
93 \ 'col': wincol + 1,
94 \ 'curscol': wincol + 1,
95 \ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
96 setlocal number
97 call assert_equal({'row': winrow + 3,
98 \ 'col': wincol + 9,
99 \ 'curscol': wincol + 9,
100 \ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
101 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200102 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200103 bwipe!
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100104
105 call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
106 nmenu WinBar.TEST :
107 call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
108 nunmenu WinBar.TEST
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200109endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100110
111func Test_screenpos_number()
112 rightbelow new
113 rightbelow 73vsplit
114 call setline (1, repeat('x', 66))
115 setlocal number
116 redraw
117 let winid = win_getid()
118 let [winrow, wincol] = win_screenpos(winid)
119 let pos = screenpos(winid, 1, 66)
120 call assert_equal(winrow, pos.row)
121 call assert_equal(wincol + 66 + 3, pos.col)
122 close
123 bwipe!
124endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200125
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100126func SaveVisualStartCharPos()
127 call add(g:VisualStartPos, getcharpos('v'))
128 return ''
129endfunc
130
131" Test for the getcharpos() function
132func Test_getcharpos()
133 call assert_fails('call getcharpos({})', 'E731:')
134 call assert_equal([0, 0, 0, 0], getcharpos(0))
135 new
136 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
137
138 " Test for '.' and '$'
139 normal 1G
140 call assert_equal([0, 1, 1, 0], getcharpos('.'))
141 call assert_equal([0, 4, 1, 0], getcharpos('$'))
142 normal 2G6l
143 call assert_equal([0, 2, 7, 0], getcharpos('.'))
144 normal 3G$
145 call assert_equal([0, 3, 1, 0], getcharpos('.'))
146 normal 4G$
147 call assert_equal([0, 4, 9, 0], getcharpos('.'))
148
149 " Test for a mark
150 normal 2G7lmmgg
151 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
152 delmarks m
153 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
154
155 " Test for the visual start column
156 vnoremap <expr> <F3> SaveVisualStartCharPos()
157 let g:VisualStartPos = []
158 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
159 call assert_equal([[0, 2, 7, 0], [0, 2, 9, 0], [0, 2, 5, 0]], g:VisualStartPos)
160 call assert_equal([0, 2, 9, 0], getcharpos('v'))
161 let g:VisualStartPos = []
162 exe "normal 3Gv$\<F3>o\<F3>"
163 call assert_equal([[0, 3, 1, 0], [0, 3, 1, 0]], g:VisualStartPos)
164 let g:VisualStartPos = []
165 exe "normal 1Gv$\<F3>o\<F3>"
166 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
167 vunmap <F3>
168
169 %bw!
170endfunc
171
172" Test for the setcharpos() function
173func Test_setcharpos()
174 call assert_equal(-1, setcharpos('.', test_null_list()))
175 new
176 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
177 call setcharpos('.', [0, 1, 1, 0])
178 call assert_equal([1, 1], [line('.'), col('.')])
179 call setcharpos('.', [0, 2, 7, 0])
180 call assert_equal([2, 9], [line('.'), col('.')])
181 call setcharpos('.', [0, 3, 4, 0])
182 call assert_equal([3, 1], [line('.'), col('.')])
183 call setcharpos('.', [0, 3, 1, 0])
184 call assert_equal([3, 1], [line('.'), col('.')])
185 call setcharpos('.', [0, 4, 0, 0])
186 call assert_equal([4, 1], [line('.'), col('.')])
187 call setcharpos('.', [0, 4, 20, 0])
188 call assert_equal([4, 9], [line('.'), col('.')])
189
190 " Test for mark
191 delmarks m
192 call setcharpos("'m", [0, 2, 9, 0])
193 normal `m
194 call assert_equal([2, 11], [line('.'), col('.')])
195
196 %bw!
197 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
198endfunc
199
200func SaveVisualStartCharCol()
201 call add(g:VisualStartCol, charcol('v'))
202 return ''
203endfunc
204
205" Test for the charcol() function
206func Test_charcol()
207 call assert_fails('call charcol({})', 'E731:')
208 call assert_equal(0, charcol(0))
209 new
210 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
211
212 " Test for '.' and '$'
213 normal 1G
214 call assert_equal(1, charcol('.'))
215 call assert_equal(1, charcol('$'))
216 normal 2G6l
217 call assert_equal(7, charcol('.'))
218 call assert_equal(10, charcol('$'))
219 normal 3G$
220 call assert_equal(1, charcol('.'))
221 call assert_equal(2, charcol('$'))
222 normal 4G$
223 call assert_equal(9, charcol('.'))
224 call assert_equal(10, charcol('$'))
225
226 " Test for [lnum, '$']
227 call assert_equal(1, charcol([1, '$']))
228 call assert_equal(10, charcol([2, '$']))
229 call assert_equal(2, charcol([3, '$']))
230 call assert_equal(0, charcol([5, '$']))
231
232 " Test for a mark
233 normal 2G7lmmgg
234 call assert_equal(8, charcol("'m"))
235 delmarks m
236 call assert_equal(0, charcol("'m"))
237
238 " Test for the visual start column
239 vnoremap <expr> <F3> SaveVisualStartCharCol()
240 let g:VisualStartCol = []
241 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
242 call assert_equal([7, 9, 5], g:VisualStartCol)
243 call assert_equal(9, charcol('v'))
244 let g:VisualStartCol = []
245 exe "normal 3Gv$\<F3>o\<F3>"
246 call assert_equal([1, 1], g:VisualStartCol)
247 let g:VisualStartCol = []
248 exe "normal 1Gv$\<F3>o\<F3>"
249 call assert_equal([1, 1], g:VisualStartCol)
250 vunmap <F3>
251
252 %bw!
253endfunc
254
255" Test for getcursorcharpos()
256func Test_getcursorcharpos()
257 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
258 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
259 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
260
261 new
262 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
263 normal 1G9l
264 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
265 normal 2G9l
266 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
267 normal 3G9l
268 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
269 normal 4G9l
270 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
271
272 let winid = win_getid()
273 normal 2G5l
274 wincmd w
275 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
276 %bw!
277endfunc
278
279" Test for setcursorcharpos()
280func Test_setcursorcharpos()
281 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
282 call assert_fails('call setcursorcharpos([1])', 'E474:')
283 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
284 new
285 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
286 normal G
287 call setcursorcharpos([1, 1])
288 call assert_equal([1, 1], [line('.'), col('.')])
289 call setcursorcharpos([2, 7, 0])
290 call assert_equal([2, 9], [line('.'), col('.')])
291 call setcursorcharpos(3, 4)
292 call assert_equal([3, 1], [line('.'), col('.')])
293 call setcursorcharpos([3, 1])
294 call assert_equal([3, 1], [line('.'), col('.')])
295 call setcursorcharpos([4, 0, 0, 0])
296 call assert_equal([4, 1], [line('.'), col('.')])
297 call setcursorcharpos([4, 20])
298 call assert_equal([4, 9], [line('.'), col('.')])
299 normal 1G
300 call setcursorcharpos([100, 100, 100, 100])
301 call assert_equal([4, 9], [line('.'), col('.')])
302 normal 1G
303 call setcursorcharpos('$', 1)
304 call assert_equal([4, 1], [line('.'), col('.')])
305
306 %bw!
307endfunc
308
Bram Moolenaar08f41572020-04-20 16:50:00 +0200309" vim: shiftwidth=2 sts=2 expandtab