test_number.vim 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. " Test for 'number' and 'relativenumber'
  2. source check.vim
  3. source view_util.vim
  4. source screendump.vim
  5. func s:screen_lines(start, end) abort
  6. return ScreenLines([a:start, a:end], 8)
  7. endfunc
  8. func s:compare_lines(expect, actual)
  9. call assert_equal(a:expect, a:actual)
  10. endfunc
  11. func s:test_windows(h, w) abort
  12. call NewWindow(a:h, a:w)
  13. endfunc
  14. func s:close_windows() abort
  15. call CloseWindow()
  16. endfunc
  17. func s:validate_cursor() abort
  18. " update skipcol.
  19. " wincol():
  20. " f_wincol
  21. " -> validate_cursor
  22. " -> curs_columns
  23. call wincol()
  24. endfunc
  25. func Test_set_options()
  26. set nu rnu
  27. call assert_equal(1, &nu)
  28. call assert_equal(1, &rnu)
  29. call s:test_windows(10, 20)
  30. call assert_equal(1, &nu)
  31. call assert_equal(1, &rnu)
  32. call s:close_windows()
  33. set nu& rnu&
  34. endfunc
  35. func Test_set_global_and_local()
  36. " setlocal must NOT reset the other global value
  37. set nonu nornu
  38. setglobal nu
  39. setlocal rnu
  40. call assert_equal(1, &g:nu)
  41. set nonu nornu
  42. setglobal rnu
  43. setlocal nu
  44. call assert_equal(1, &g:rnu)
  45. " setglobal MUST reset the other global value
  46. set nonu nornu
  47. setglobal nu
  48. setglobal rnu
  49. call assert_equal(1, &g:nu)
  50. set nonu nornu
  51. setglobal rnu
  52. setglobal nu
  53. call assert_equal(1, &g:rnu)
  54. " set MUST reset the other global value
  55. set nonu nornu
  56. set nu
  57. set rnu
  58. call assert_equal(1, &g:nu)
  59. set nonu nornu
  60. set rnu
  61. set nu
  62. call assert_equal(1, &g:rnu)
  63. set nu& rnu&
  64. endfunc
  65. func Test_number()
  66. call s:test_windows(10, 20)
  67. call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"])
  68. setl number
  69. let lines = s:screen_lines(1, 4)
  70. let expect = [
  71. \ " 1 abcd",
  72. \ " 2 klmn",
  73. \ " 3 uvwx",
  74. \ " 4 EFGH",
  75. \ ]
  76. call s:compare_lines(expect, lines)
  77. call s:close_windows()
  78. endfunc
  79. func Test_relativenumber()
  80. call s:test_windows(10, 20)
  81. call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"])
  82. 3
  83. setl relativenumber
  84. let lines = s:screen_lines(1, 6)
  85. let expect = [
  86. \ " 2 abcd",
  87. \ " 1 klmn",
  88. \ " 0 uvwx",
  89. \ " 1 EFGH",
  90. \ " 2 OPQR",
  91. \ " 3 YZ ",
  92. \ ]
  93. call s:compare_lines(expect, lines)
  94. call s:close_windows()
  95. endfunc
  96. func Test_number_with_relativenumber()
  97. call s:test_windows(10, 20)
  98. call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"])
  99. 4
  100. setl number relativenumber
  101. let lines = s:screen_lines(1, 6)
  102. let expect = [
  103. \ " 3 abcd",
  104. \ " 2 klmn",
  105. \ " 1 uvwx",
  106. \ "4 EFGH",
  107. \ " 1 OPQR",
  108. \ " 2 YZ ",
  109. \ ]
  110. call s:compare_lines(expect, lines)
  111. call s:close_windows()
  112. endfunc
  113. func Test_number_with_linewrap1()
  114. call s:test_windows(3, 20)
  115. normal! 61ia
  116. setl number wrap
  117. call s:validate_cursor()
  118. let lines = s:screen_lines(1, 3)
  119. let expect = [
  120. \ "<<< aaaa",
  121. \ " aaaa",
  122. \ " aaaa",
  123. \ ]
  124. call s:compare_lines(expect, lines)
  125. call s:close_windows()
  126. endfunc
  127. func Test_number_with_linewrap2()
  128. call s:test_windows(3, 20)
  129. normal! 61ia
  130. setl number wrap
  131. call s:validate_cursor()
  132. 0
  133. call s:validate_cursor()
  134. let lines = s:screen_lines(1, 3)
  135. let expect = [
  136. \ " 1 aaaa",
  137. \ " aaaa",
  138. \ " aaaa",
  139. \ ]
  140. call s:compare_lines(expect, lines)
  141. call s:close_windows()
  142. endfunc
  143. func Test_number_with_linewrap3()
  144. call s:test_windows(4, 20)
  145. normal! 81ia
  146. setl number wrap
  147. call s:validate_cursor()
  148. setl nonumber
  149. call s:validate_cursor()
  150. let lines = s:screen_lines(1, 4)
  151. let expect = [
  152. \ "<<<aaaaa",
  153. \ "aaaaaaaa",
  154. \ "aaaaaaaa",
  155. \ "a ",
  156. \ ]
  157. call s:compare_lines(expect, lines)
  158. call s:close_windows()
  159. endfunc
  160. func Test_numberwidth()
  161. call s:test_windows(10, 20)
  162. call setline(1, repeat(['aaaa'], 10))
  163. setl number numberwidth=6
  164. let lines = s:screen_lines(1, 3)
  165. let expect = [
  166. \ " 1 aa",
  167. \ " 2 aa",
  168. \ " 3 aa",
  169. \ ]
  170. call s:compare_lines(expect, lines)
  171. set relativenumber
  172. let lines = s:screen_lines(1, 3)
  173. let expect = [
  174. \ "1 aa",
  175. \ " 1 aa",
  176. \ " 2 aa",
  177. \ ]
  178. call s:compare_lines(expect, lines)
  179. set nonumber
  180. let lines = s:screen_lines(1, 3)
  181. let expect = [
  182. \ " 0 aa",
  183. \ " 1 aa",
  184. \ " 2 aa",
  185. \ ]
  186. call s:compare_lines(expect, lines)
  187. call s:close_windows()
  188. endfunc
  189. func Test_numberwidth_adjusted()
  190. call s:test_windows(10, 20)
  191. call setline(1, repeat(['aaaa'], 10000))
  192. setl number numberwidth=4
  193. let lines = s:screen_lines(1, 3)
  194. let expect = [
  195. \ " 1 aa",
  196. \ " 2 aa",
  197. \ " 3 aa",
  198. \ ]
  199. call s:compare_lines(expect, lines)
  200. $
  201. let lines = s:screen_lines(8, 10)
  202. let expect = [
  203. \ " 9998 aa",
  204. \ " 9999 aa",
  205. \ "10000 aa",
  206. \ ]
  207. call s:compare_lines(expect, lines)
  208. setl relativenumber
  209. let lines = s:screen_lines(8, 10)
  210. let expect = [
  211. \ " 2 aa",
  212. \ " 1 aa",
  213. \ "10000 aa",
  214. \ ]
  215. call s:compare_lines(expect, lines)
  216. setl nonumber
  217. let lines = s:screen_lines(8, 10)
  218. let expect = [
  219. \ " 2 aaaa",
  220. \ " 1 aaaa",
  221. \ " 0 aaaa",
  222. \ ]
  223. call s:compare_lines(expect, lines)
  224. call s:close_windows()
  225. endfunc
  226. " This was causing a memcheck error
  227. func Test_relativenumber_uninitialised()
  228. new
  229. set rnu
  230. call setline(1, ["a", "b"])
  231. redraw
  232. call feedkeys("j", 'xt')
  233. redraw
  234. bwipe!
  235. endfunc
  236. func Test_relativenumber_colors()
  237. CheckScreendump
  238. let lines =<< trim [CODE]
  239. call setline(1, range(200))
  240. 111
  241. set number relativenumber
  242. hi LineNr ctermfg=red
  243. [CODE]
  244. call writefile(lines, 'XTest_relnr', 'D')
  245. let buf = RunVimInTerminal('-S XTest_relnr', {'rows': 10, 'cols': 50})
  246. " Default colors
  247. call VerifyScreenDump(buf, 'Test_relnr_colors_1', {})
  248. call term_sendkeys(buf, ":hi LineNrAbove ctermfg=blue\<CR>:\<CR>")
  249. call VerifyScreenDump(buf, 'Test_relnr_colors_2', {})
  250. call term_sendkeys(buf, ":hi LineNrBelow ctermfg=green\<CR>:\<CR>")
  251. call VerifyScreenDump(buf, 'Test_relnr_colors_3', {})
  252. call term_sendkeys(buf, ":hi clear LineNrAbove\<CR>")
  253. call VerifyScreenDump(buf, 'Test_relnr_colors_4', {})
  254. " clean up
  255. call StopVimInTerminal(buf)
  256. endfunc
  257. func Test_relativenumber_colors_wrapped()
  258. CheckScreendump
  259. let lines =<< trim [CODE]
  260. set display=lastline scrolloff=0
  261. call setline(1, range(200)->map('v:val->string()->repeat(40)'))
  262. 111
  263. set number relativenumber
  264. hi LineNr ctermbg=red ctermfg=black
  265. hi LineNrAbove ctermbg=blue ctermfg=black
  266. hi LineNrBelow ctermbg=green ctermfg=black
  267. [CODE]
  268. call writefile(lines, 'XTest_relnr_wrap', 'D')
  269. let buf = RunVimInTerminal('-S XTest_relnr_wrap', {'rows': 20, 'cols': 50})
  270. call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_1', {})
  271. call term_sendkeys(buf, "k")
  272. call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_2', {})
  273. call term_sendkeys(buf, "2j")
  274. call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_3', {})
  275. call term_sendkeys(buf, "2j")
  276. call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_4', {})
  277. call term_sendkeys(buf, "k")
  278. call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_5', {})
  279. " clean up
  280. call StopVimInTerminal(buf)
  281. endfunc
  282. func Test_relativenumber_callback()
  283. CheckScreendump
  284. CheckFeature timers
  285. let lines =<< trim END
  286. call setline(1, ['aaaaa', 'bbbbb', 'ccccc', 'ddddd'])
  287. set relativenumber
  288. call cursor(4, 1)
  289. func Func(timer)
  290. call cursor(1, 1)
  291. endfunc
  292. call timer_start(300, 'Func')
  293. END
  294. call writefile(lines, 'Xrnu_timer', 'D')
  295. let buf = RunVimInTerminal('-S Xrnu_timer', #{rows: 8})
  296. call TermWait(buf, 310)
  297. call VerifyScreenDump(buf, 'Test_relativenumber_callback_1', {})
  298. call StopVimInTerminal(buf)
  299. endfunc
  300. " Test that line numbers below inserted/deleted lines are updated.
  301. func Test_number_insert_delete_lines()
  302. CheckScreendump
  303. let lines =<< trim END
  304. call setline(1, range(1, 7))
  305. set number
  306. call cursor(2, 1)
  307. END
  308. call writefile(lines, 'Xnumber_insert_delete_lines', 'D')
  309. let buf = RunVimInTerminal('-S Xnumber_insert_delete_lines', #{rows: 8})
  310. call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_1', {})
  311. call term_sendkeys(buf, "dd")
  312. call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_2', {})
  313. call term_sendkeys(buf, "P")
  314. call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_1', {})
  315. call term_sendkeys(buf, "2dd")
  316. call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_3', {})
  317. call term_sendkeys(buf, "P")
  318. call VerifyScreenDump(buf, 'Test_number_insert_delete_lines_1', {})
  319. call StopVimInTerminal(buf)
  320. endfunc
  321. " Test for displaying line numbers with 'rightleft'
  322. func Test_number_rightleft()
  323. CheckFeature rightleft
  324. new
  325. setlocal number
  326. setlocal rightleft
  327. call setline(1, range(1, 1000))
  328. normal! 9Gzt
  329. redraw!
  330. call assert_match('^\s\+9 9$', Screenline(1))
  331. normal! 10Gzt
  332. redraw!
  333. call assert_match('^\s\+01 10$', Screenline(1))
  334. normal! 100Gzt
  335. redraw!
  336. call assert_match('^\s\+001 100$', Screenline(1))
  337. normal! 1000Gzt
  338. redraw!
  339. call assert_match('^\s\+0001 1000$', Screenline(1))
  340. bw!
  341. endfunc
  342. " This used to cause a divide by zero
  343. func Test_number_no_text_virtual_edit()
  344. vnew
  345. call setline(1, ['line one', 'line two'])
  346. set number virtualedit=all
  347. normal w
  348. 4wincmd |
  349. normal j
  350. bwipe!
  351. endfunc
  352. " vim: shiftwidth=2 sts=2 expandtab