test_cursor_func.vim 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. " Tests for cursor() and other functions that get/set the cursor position
  2. source check.vim
  3. func Test_wrong_arguments()
  4. call assert_fails('call cursor(1. 3)', 'E474:')
  5. call assert_fails('call cursor(v:_null_list)', 'E474:')
  6. endfunc
  7. func Test_move_cursor()
  8. new
  9. call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
  10. call cursor([1, 1, 0, 1])
  11. call assert_equal([1, 1, 0, 1], getcurpos()[1:])
  12. call cursor([4, 3, 0, 3])
  13. call assert_equal([4, 3, 0, 3], getcurpos()[1:])
  14. call cursor(2, 2)
  15. call assert_equal([2, 2, 0, 2], getcurpos()[1:])
  16. " line number zero keeps the line number
  17. call cursor(0, 1)
  18. call assert_equal([2, 1, 0, 1], getcurpos()[1:])
  19. " col number zero keeps the column
  20. call cursor(3, 0)
  21. call assert_equal([3, 1, 0, 1], getcurpos()[1:])
  22. " below last line goes to last line
  23. eval [9, 1]->cursor()
  24. call assert_equal([4, 1, 0, 1], getcurpos()[1:])
  25. " pass string arguments
  26. call cursor('3', '3')
  27. call assert_equal([3, 3, 0, 3], getcurpos()[1:])
  28. call setline(1, ["\<TAB>"])
  29. call cursor(1, 1, 1)
  30. call assert_equal([1, 1, 1], getcurpos()[1:3])
  31. call assert_fails('call cursor(-1, -1)', 'E475:')
  32. quit!
  33. endfunc
  34. func Test_curswant_maxcol()
  35. new
  36. call setline(1, 'foo')
  37. " Test that after "$" command curswant is set to the same value as v:maxcol.
  38. normal! 1G$
  39. call assert_equal(v:maxcol, getcurpos()[4])
  40. call assert_equal(v:maxcol, winsaveview().curswant)
  41. quit!
  42. endfunc
  43. " Very short version of what matchparen does.
  44. function s:Highlight_Matching_Pair()
  45. let save_cursor = getcurpos()
  46. eval save_cursor->setpos('.')
  47. endfunc
  48. func Test_curswant_with_autocommand()
  49. new
  50. call setline(1, ['func()', '{', '}', '----'])
  51. autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
  52. exe "normal! 3Ga\<Down>X\<Esc>"
  53. call assert_equal('-X---', getline(4))
  54. autocmd! CursorMovedI *
  55. quit!
  56. endfunc
  57. " Tests for behavior of curswant with cursorcolumn/line
  58. func Test_curswant_with_cursorcolumn()
  59. new
  60. call setline(1, ['01234567', ''])
  61. exe "normal! ggf6j"
  62. call assert_equal(6, winsaveview().curswant)
  63. set cursorcolumn
  64. call assert_equal(6, winsaveview().curswant)
  65. quit!
  66. endfunc
  67. func 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!
  75. endfunc
  76. func Test_screenpos()
  77. rightbelow new
  78. rightbelow 20vsplit
  79. call setline(1, ["\tsome text", "long wrapping line here", "next line"])
  80. redraw
  81. let winid = win_getid()
  82. let [winrow, wincol] = win_screenpos(winid)
  83. call assert_equal({'row': winrow,
  84. \ 'col': wincol + 0,
  85. \ 'curscol': wincol + 7,
  86. \ 'endcol': wincol + 7}, winid->screenpos(1, 1))
  87. call assert_equal({'row': winrow,
  88. \ 'col': wincol + 13,
  89. \ 'curscol': wincol + 13,
  90. \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
  91. call assert_equal({'row': winrow + 2,
  92. \ 'col': wincol + 1,
  93. \ 'curscol': wincol + 1,
  94. \ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
  95. setlocal number
  96. call assert_equal({'row': winrow + 3,
  97. \ 'col': wincol + 9,
  98. \ 'curscol': wincol + 9,
  99. \ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
  100. let wininfo = getwininfo(winid)[0]
  101. call setline(3, ['x']->repeat(wininfo.height))
  102. call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3))
  103. setlocal nonumber display=lastline so=0
  104. exe "normal G\<C-Y>\<C-Y>"
  105. redraw
  106. call assert_equal({'row': winrow + wininfo.height - 1,
  107. \ 'col': wincol + 7,
  108. \ 'curscol': wincol + 7,
  109. \ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8))
  110. call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
  111. \ winid->screenpos(line('$'), 22))
  112. 1split
  113. " w_leftcol should be subtracted
  114. setlocal nowrap
  115. normal G050zl$
  116. redraw
  117. call assert_equal({'row': winrow + 0,
  118. \ 'col': wincol + 10 - 1,
  119. \ 'curscol': wincol + 10 - 1,
  120. \ 'endcol': wincol + 10 - 1},
  121. \ screenpos(win_getid(), line('.'), col('.')))
  122. " w_skipcol should be taken into account
  123. setlocal wrap
  124. normal $
  125. redraw
  126. call assert_equal({'row': winrow + 0,
  127. \ 'col': wincol + 20 - 1,
  128. \ 'curscol': wincol + 20 - 1,
  129. \ 'endcol': wincol + 20 - 1},
  130. \ screenpos(win_getid(), line('.'), col('.')))
  131. call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
  132. \ screenpos(win_getid(), line('.'), col('.') - 20))
  133. setlocal number
  134. redraw
  135. call assert_equal({'row': winrow + 0,
  136. \ 'col': wincol + 16 - 1,
  137. \ 'curscol': wincol + 16 - 1,
  138. \ 'endcol': wincol + 16 - 1},
  139. \ screenpos(win_getid(), line('.'), col('.')))
  140. call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
  141. \ screenpos(win_getid(), line('.'), col('.') - 16))
  142. set cpoptions+=n
  143. redraw
  144. call assert_equal({'row': winrow + 0,
  145. \ 'col': wincol + 4 - 1,
  146. \ 'curscol': wincol + 4 - 1,
  147. \ 'endcol': wincol + 4 - 1},
  148. \ screenpos(win_getid(), line('.'), col('.')))
  149. call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
  150. \ screenpos(win_getid(), line('.'), col('.') - 4))
  151. wincmd +
  152. call setline(line('$') + 1, 'last line')
  153. setlocal smoothscroll
  154. normal G$
  155. redraw
  156. call assert_equal({'row': winrow + 1,
  157. \ 'col': wincol + 4 + 9 - 1,
  158. \ 'curscol': wincol + 4 + 9 - 1,
  159. \ 'endcol': wincol + 4 + 9 - 1},
  160. \ screenpos(win_getid(), line('.'), col('.')))
  161. set cpoptions-=n
  162. redraw
  163. call assert_equal({'row': winrow + 1,
  164. \ 'col': wincol + 4 + 9 - 1,
  165. \ 'curscol': wincol + 4 + 9 - 1,
  166. \ 'endcol': wincol + 4 + 9 - 1},
  167. \ screenpos(win_getid(), line('.'), col('.')))
  168. setlocal nonumber
  169. redraw
  170. call assert_equal({'row': winrow + 1,
  171. \ 'col': wincol + 9 - 1,
  172. \ 'curscol': wincol + 9 - 1,
  173. \ 'endcol': wincol + 9 - 1},
  174. \ screenpos(win_getid(), line('.'), col('.')))
  175. close
  176. call assert_equal({}, screenpos(999, 1, 1))
  177. bwipe!
  178. set display&
  179. call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
  180. " nmenu WinBar.TEST :
  181. setlocal winbar=TEST
  182. call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
  183. " nunmenu WinBar.TEST
  184. setlocal winbar&
  185. call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
  186. call assert_equal(#{col: 0, row: 0, endcol: 0, curscol: 0}, screenpos(0, 0, 1))
  187. call assert_equal(#{col: 0, row: 0, endcol: 0, curscol: 0}, screenpos(0, -1, 1))
  188. call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(0, 1, -v:maxcol))
  189. endfunc
  190. func Test_screenpos_fold()
  191. CheckFeature folding
  192. enew!
  193. call setline(1, range(10))
  194. 3,5fold
  195. redraw
  196. call assert_equal(2, screenpos(1, 2, 1).row)
  197. call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
  198. call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 4, 1))
  199. call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 5, 1))
  200. setlocal number
  201. call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 3, 1))
  202. call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 4, 1))
  203. call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 5, 1))
  204. call assert_equal(4, screenpos(1, 6, 1).row)
  205. bwipe!
  206. endfunc
  207. func Test_screenpos_diff()
  208. CheckFeature diff
  209. enew!
  210. call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
  211. vnew
  212. call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
  213. windo diffthis
  214. wincmd w
  215. call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
  216. call assert_equal(#{col: 3, row: 8, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
  217. exe "normal! 3\<C-E>"
  218. call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
  219. call assert_equal(#{col: 3, row: 5, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
  220. exe "normal! \<C-E>"
  221. call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
  222. call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
  223. exe "normal! \<C-E>"
  224. call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
  225. call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
  226. exe "normal! \<C-E>"
  227. call assert_equal(#{col: 3, row: 1, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
  228. call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
  229. windo diffoff
  230. bwipe!
  231. bwipe!
  232. endfunc
  233. func Test_screenpos_number()
  234. rightbelow new
  235. rightbelow 73vsplit
  236. call setline (1, repeat('x', 66))
  237. setlocal number
  238. redraw
  239. let winid = win_getid()
  240. let [winrow, wincol] = win_screenpos(winid)
  241. let pos = screenpos(winid, 1, 66)
  242. call assert_equal(winrow, pos.row)
  243. call assert_equal(wincol + 66 + 3, pos.col)
  244. call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
  245. close
  246. bwipe!
  247. endfunc
  248. func Test_screenpos_edit_newfile()
  249. new
  250. 20vsp
  251. setl nowrap
  252. call setline(1, 'abcdefghijklmnopqrstuvwxyz')
  253. call cursor(1, 10)
  254. norm! 5zl
  255. call assert_equal(#{col: 5, row: 1, endcol: 5, curscol: 5}, screenpos(win_getid(), 1, 10))
  256. enew!
  257. call assert_equal(1, &l:wrap)
  258. call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
  259. bwipe!
  260. endfunc
  261. " Save the visual start character position
  262. func SaveVisualStartCharPos()
  263. call add(g:VisualStartPos, getcharpos('v'))
  264. return ''
  265. endfunc
  266. " Save the current cursor character position in insert mode
  267. func SaveInsertCurrentCharPos()
  268. call add(g:InsertCurrentPos, getcharpos('.'))
  269. return ''
  270. endfunc
  271. " Test for the getcharpos() function
  272. func Test_getcharpos()
  273. call assert_fails('call getcharpos({})', 'E731:')
  274. call assert_equal([0, 0, 0, 0], getcharpos(0))
  275. new
  276. call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
  277. " Test for '.' and '$'
  278. normal 1G
  279. call assert_equal([0, 1, 1, 0], getcharpos('.'))
  280. call assert_equal([0, 5, 1, 0], getcharpos('$'))
  281. normal 2G6l
  282. call assert_equal([0, 2, 7, 0], getcharpos('.'))
  283. normal 3G$
  284. call assert_equal([0, 3, 1, 0], getcharpos('.'))
  285. normal 4G$
  286. call assert_equal([0, 4, 9, 0], getcharpos('.'))
  287. " Test for a mark
  288. normal 2G7lmmgg
  289. call assert_equal([0, 2, 8, 0], getcharpos("'m"))
  290. delmarks m
  291. call assert_equal([0, 0, 0, 0], getcharpos("'m"))
  292. " Check mark does not move
  293. normal 5Gfxma
  294. call assert_equal([0, 5, 5, 0], getcharpos("'a"))
  295. call assert_equal([0, 5, 5, 0], getcharpos("'a"))
  296. call assert_equal([0, 5, 5, 0], getcharpos("'a"))
  297. " Test for the visual start column
  298. vnoremap <expr> <F3> SaveVisualStartCharPos()
  299. let g:VisualStartPos = []
  300. exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
  301. call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
  302. call assert_equal([0, 2, 9, 0], getcharpos('v'))
  303. let g:VisualStartPos = []
  304. exe "normal 3Gv$\<F3>o\<F3>"
  305. call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
  306. let g:VisualStartPos = []
  307. exe "normal 1Gv$\<F3>o\<F3>"
  308. call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
  309. vunmap <F3>
  310. " Test for getting the position in insert mode with the cursor after the
  311. " last character in a line
  312. inoremap <expr> <F3> SaveInsertCurrentCharPos()
  313. let g:InsertCurrentPos = []
  314. exe "normal 1GA\<F3>"
  315. exe "normal 2GA\<F3>"
  316. exe "normal 3GA\<F3>"
  317. exe "normal 4GA\<F3>"
  318. exe "normal 2G6li\<F3>"
  319. call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
  320. \ [0, 2, 7, 0]], g:InsertCurrentPos)
  321. iunmap <F3>
  322. %bw!
  323. endfunc
  324. " Test for the setcharpos() function
  325. func Test_setcharpos()
  326. call assert_equal(-1, setcharpos('.', v:_null_list))
  327. new
  328. call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
  329. call setcharpos('.', [0, 1, 1, 0])
  330. call assert_equal([1, 1], [line('.'), col('.')])
  331. call setcharpos('.', [0, 2, 7, 0])
  332. call assert_equal([2, 9], [line('.'), col('.')])
  333. call setcharpos('.', [0, 3, 4, 0])
  334. call assert_equal([3, 1], [line('.'), col('.')])
  335. call setcharpos('.', [0, 3, 1, 0])
  336. call assert_equal([3, 1], [line('.'), col('.')])
  337. call setcharpos('.', [0, 4, 0, 0])
  338. call assert_equal([4, 1], [line('.'), col('.')])
  339. call setcharpos('.', [0, 4, 20, 0])
  340. call assert_equal([4, 9], [line('.'), col('.')])
  341. " Test for mark
  342. delmarks m
  343. call setcharpos("'m", [0, 2, 9, 0])
  344. normal `m
  345. call assert_equal([2, 11], [line('.'), col('.')])
  346. " unload the buffer and try to set the mark
  347. let bnr = bufnr()
  348. enew!
  349. call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
  350. %bw!
  351. call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
  352. endfunc
  353. func SaveVisualStartCharCol()
  354. call add(g:VisualStartCol, charcol('v'))
  355. return ''
  356. endfunc
  357. func SaveInsertCurrentCharCol()
  358. call add(g:InsertCurrentCol, charcol('.'))
  359. return ''
  360. endfunc
  361. " Test for the charcol() function
  362. func Test_charcol()
  363. call assert_fails('call charcol({})', 'E1222:')
  364. call assert_fails('call charcol(".", [])', 'E1210:')
  365. call assert_fails('call charcol(0)', 'E1222:')
  366. new
  367. call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
  368. " Test for '.' and '$'
  369. normal 1G
  370. call assert_equal(1, charcol('.'))
  371. call assert_equal(1, charcol('$'))
  372. normal 2G6l
  373. call assert_equal(7, charcol('.'))
  374. call assert_equal(10, charcol('$'))
  375. normal 3G$
  376. call assert_equal(1, charcol('.'))
  377. call assert_equal(2, charcol('$'))
  378. normal 4G$
  379. call assert_equal(9, charcol('.'))
  380. call assert_equal(10, charcol('$'))
  381. " Test for [lnum, '$']
  382. call assert_equal(1, charcol([1, '$']))
  383. call assert_equal(10, charcol([2, '$']))
  384. call assert_equal(2, charcol([3, '$']))
  385. call assert_equal(0, charcol([5, '$']))
  386. " Test for a mark
  387. normal 2G7lmmgg
  388. call assert_equal(8, charcol("'m"))
  389. delmarks m
  390. call assert_equal(0, charcol("'m"))
  391. " Test for the visual start column
  392. vnoremap <expr> <F3> SaveVisualStartCharCol()
  393. let g:VisualStartCol = []
  394. exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
  395. call assert_equal([7, 10, 5], g:VisualStartCol)
  396. call assert_equal(9, charcol('v'))
  397. let g:VisualStartCol = []
  398. exe "normal 3Gv$\<F3>o\<F3>"
  399. call assert_equal([1, 2], g:VisualStartCol)
  400. let g:VisualStartCol = []
  401. exe "normal 1Gv$\<F3>o\<F3>"
  402. call assert_equal([1, 1], g:VisualStartCol)
  403. vunmap <F3>
  404. " Test for getting the column number in insert mode with the cursor after
  405. " the last character in a line
  406. inoremap <expr> <F3> SaveInsertCurrentCharCol()
  407. let g:InsertCurrentCol = []
  408. exe "normal 1GA\<F3>"
  409. exe "normal 2GA\<F3>"
  410. exe "normal 3GA\<F3>"
  411. exe "normal 4GA\<F3>"
  412. exe "normal 2G6li\<F3>"
  413. call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
  414. iunmap <F3>
  415. " Test for getting the column number in another window.
  416. let winid = win_getid()
  417. new
  418. call win_execute(winid, 'normal 1G')
  419. call assert_equal(1, charcol('.', winid))
  420. call assert_equal(1, charcol('$', winid))
  421. call win_execute(winid, 'normal 2G6l')
  422. call assert_equal(7, charcol('.', winid))
  423. call assert_equal(10, charcol('$', winid))
  424. " calling from another tab page also works
  425. tabnew
  426. call assert_equal(7, charcol('.', winid))
  427. call assert_equal(10, charcol('$', winid))
  428. tabclose
  429. " unknown window ID
  430. call assert_equal(0, charcol('.', 10001))
  431. %bw!
  432. endfunc
  433. func SaveInsertCursorCharPos()
  434. call add(g:InsertCursorPos, getcursorcharpos('.'))
  435. return ''
  436. endfunc
  437. " Test for getcursorcharpos()
  438. func Test_getcursorcharpos()
  439. call assert_equal(getcursorcharpos(), getcursorcharpos(0))
  440. call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
  441. call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
  442. new
  443. call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
  444. normal 1G9l
  445. call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
  446. normal 2G9l
  447. call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
  448. normal 3G9l
  449. call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
  450. normal 4G9l
  451. call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
  452. " Test for getting the cursor position in insert mode with the cursor after
  453. " the last character in a line
  454. inoremap <expr> <F3> SaveInsertCursorCharPos()
  455. let g:InsertCursorPos = []
  456. exe "normal 1GA\<F3>"
  457. exe "normal 2GA\<F3>"
  458. exe "normal 3GA\<F3>"
  459. exe "normal 4GA\<F3>"
  460. exe "normal 2G6li\<F3>"
  461. call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
  462. \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
  463. iunmap <F3>
  464. let winid = win_getid()
  465. normal 2G5l
  466. wincmd w
  467. call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
  468. %bw!
  469. endfunc
  470. " Test for setcursorcharpos()
  471. func Test_setcursorcharpos()
  472. call assert_fails('call setcursorcharpos(v:_null_list)', 'E474:')
  473. call assert_fails('call setcursorcharpos([1])', 'E474:')
  474. call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
  475. new
  476. call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
  477. normal G
  478. call setcursorcharpos([1, 1])
  479. call assert_equal([1, 1], [line('.'), col('.')])
  480. call setcursorcharpos([2, 7, 0])
  481. call assert_equal([2, 9], [line('.'), col('.')])
  482. call setcursorcharpos([0, 7, 0])
  483. call assert_equal([2, 9], [line('.'), col('.')])
  484. call setcursorcharpos(0, 7, 0)
  485. call assert_equal([2, 9], [line('.'), col('.')])
  486. call setcursorcharpos(3, 4)
  487. call assert_equal([3, 1], [line('.'), col('.')])
  488. call setcursorcharpos([3, 1])
  489. call assert_equal([3, 1], [line('.'), col('.')])
  490. call setcursorcharpos([4, 0, 0, 0])
  491. call assert_equal([4, 1], [line('.'), col('.')])
  492. call setcursorcharpos([4, 20])
  493. call assert_equal([4, 9], [line('.'), col('.')])
  494. normal 1G
  495. call setcursorcharpos([100, 100, 100, 100])
  496. call assert_equal([4, 9], [line('.'), col('.')])
  497. normal 1G
  498. call setcursorcharpos('$', 1)
  499. call assert_equal([4, 1], [line('.'), col('.')])
  500. %bw!
  501. endfunc
  502. " Test for virtcol2col()
  503. func Test_virtcol2col()
  504. new
  505. call setline(1, ["a\tb\tc"])
  506. call assert_equal(1, virtcol2col(0, 1, 1))
  507. call assert_equal(2, virtcol2col(0, 1, 2))
  508. call assert_equal(2, virtcol2col(0, 1, 8))
  509. call assert_equal(3, virtcol2col(0, 1, 9))
  510. call assert_equal(4, virtcol2col(0, 1, 10))
  511. call assert_equal(4, virtcol2col(0, 1, 16))
  512. call assert_equal(5, virtcol2col(0, 1, 17))
  513. call assert_equal(-1, virtcol2col(10, 1, 1))
  514. call assert_equal(-1, virtcol2col(0, 10, 1))
  515. call assert_equal(-1, virtcol2col(0, -1, 1))
  516. call assert_equal(-1, virtcol2col(0, 1, -1))
  517. call assert_equal(5, virtcol2col(0, 1, 20))
  518. " Multibyte character
  519. call setline(1, ['a✅✅✅'])
  520. call assert_equal(1, virtcol2col(0, 1, 1))
  521. call assert_equal(2, virtcol2col(0, 1, 3))
  522. call assert_equal(5, virtcol2col(0, 1, 5))
  523. call assert_equal(8, virtcol2col(0, 1, 7))
  524. call assert_equal(8, virtcol2col(0, 1, 8))
  525. " These used to cause invalid memory access
  526. call setline(1, '')
  527. call assert_equal(0, virtcol2col(0, 1, 1))
  528. call assert_equal(0, virtcol2col(0, 1, 2))
  529. let w = winwidth(0)
  530. call setline(2, repeat('a', w + 2))
  531. let win_nosbr = win_getid()
  532. split
  533. setlocal showbreak=!!
  534. let win_sbr = win_getid()
  535. call assert_equal(w, virtcol2col(win_nosbr, 2, w))
  536. call assert_equal(w + 1, virtcol2col(win_nosbr, 2, w + 1))
  537. call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 2))
  538. call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 3))
  539. call assert_equal(w, virtcol2col(win_sbr, 2, w))
  540. call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 1))
  541. call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 2))
  542. call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 3))
  543. call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 4))
  544. call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 5))
  545. close
  546. call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
  547. call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
  548. call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
  549. bw!
  550. endfunc
  551. " vim: shiftwidth=2 sts=2 expandtab