blob: 6ac3c4cfe4adcb68fac6600f21b572edfc3a68f4 [file] [log] [blame]
Bram Moolenaardc9a0812017-02-23 18:46:50 +01001" Test for 'number' and 'relativenumber'
2
Bram Moolenaarefae76a2019-10-27 22:54:58 +01003source check.vim
Bram Moolenaardc9a0812017-02-23 18:46:50 +01004source view_util.vim
5
Bram Moolenaarefae76a2019-10-27 22:54:58 +01006source screendump.vim
7
Bram Moolenaar1e115362019-01-09 23:01:02 +01008func s:screen_lines(start, end) abort
Bram Moolenaardc9a0812017-02-23 18:46:50 +01009 return ScreenLines([a:start, a:end], 8)
10endfunc
11
Bram Moolenaar1e115362019-01-09 23:01:02 +010012func s:compare_lines(expect, actual)
Bram Moolenaardc9a0812017-02-23 18:46:50 +010013 call assert_equal(a:expect, a:actual)
14endfunc
15
Bram Moolenaar1e115362019-01-09 23:01:02 +010016func s:test_windows(h, w) abort
Bram Moolenaardc9a0812017-02-23 18:46:50 +010017 call NewWindow(a:h, a:w)
18endfunc
19
Bram Moolenaar1e115362019-01-09 23:01:02 +010020func s:close_windows() abort
Bram Moolenaardc9a0812017-02-23 18:46:50 +010021 call CloseWindow()
22endfunc
23
Bram Moolenaar1e115362019-01-09 23:01:02 +010024func s:validate_cursor() abort
Bram Moolenaardc9a0812017-02-23 18:46:50 +010025 " update skipcol.
26 " wincol():
27 " f_wincol
28 " -> validate_cursor
29 " -> curs_columns
30 call wincol()
31endfunc
32
33func Test_set_options()
34 set nu rnu
35 call assert_equal(1, &nu)
36 call assert_equal(1, &rnu)
37
38 call s:test_windows(10, 20)
39 call assert_equal(1, &nu)
40 call assert_equal(1, &rnu)
41 call s:close_windows()
42
43 set nu& rnu&
44endfunc
45
46func Test_set_global_and_local()
47 " setlocal must NOT reset the other global value
48 set nonu nornu
49 setglobal nu
50 setlocal rnu
51 call assert_equal(1, &g:nu)
52
53 set nonu nornu
54 setglobal rnu
55 setlocal nu
56 call assert_equal(1, &g:rnu)
57
58 " setglobal MUST reset the other global value
59 set nonu nornu
60 setglobal nu
61 setglobal rnu
62 call assert_equal(1, &g:nu)
63
64 set nonu nornu
65 setglobal rnu
66 setglobal nu
67 call assert_equal(1, &g:rnu)
68
69 " set MUST reset the other global value
70 set nonu nornu
71 set nu
72 set rnu
73 call assert_equal(1, &g:nu)
74
75 set nonu nornu
76 set rnu
77 set nu
78 call assert_equal(1, &g:rnu)
79
80 set nu& rnu&
81endfunc
82
83func Test_number()
84 call s:test_windows(10, 20)
85 call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"])
86 setl number
87 let lines = s:screen_lines(1, 4)
88 let expect = [
89\ " 1 abcd",
90\ " 2 klmn",
91\ " 3 uvwx",
92\ " 4 EFGH",
93\ ]
94 call s:compare_lines(expect, lines)
95 call s:close_windows()
96endfunc
97
98func Test_relativenumber()
99 call s:test_windows(10, 20)
100 call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"])
101 3
102 setl relativenumber
103 let lines = s:screen_lines(1, 6)
104 let expect = [
105\ " 2 abcd",
106\ " 1 klmn",
107\ " 0 uvwx",
108\ " 1 EFGH",
109\ " 2 OPQR",
110\ " 3 YZ ",
111\ ]
112 call s:compare_lines(expect, lines)
113 call s:close_windows()
114endfunc
115
116func Test_number_with_relativenumber()
117 call s:test_windows(10, 20)
118 call setline(1, ["abcdefghij", "klmnopqrst", "uvwxyzABCD", "EFGHIJKLMN", "OPQRSTUVWX", "YZ"])
119 4
120 setl number relativenumber
121 let lines = s:screen_lines(1, 6)
122 let expect = [
123\ " 3 abcd",
124\ " 2 klmn",
125\ " 1 uvwx",
126\ "4 EFGH",
127\ " 1 OPQR",
128\ " 2 YZ ",
129\ ]
130 call s:compare_lines(expect, lines)
131 call s:close_windows()
132endfunc
133
134func Test_number_with_linewrap1()
135 call s:test_windows(3, 20)
136 normal! 61ia
137 setl number wrap
138 call s:validate_cursor()
139 let lines = s:screen_lines(1, 3)
140 let expect = [
Bram Moolenaar0466d392022-10-03 17:07:34 +0100141\ "<<< aaaa",
Bram Moolenaardc9a0812017-02-23 18:46:50 +0100142\ " aaaa",
143\ " aaaa",
144\ ]
145 call s:compare_lines(expect, lines)
146 call s:close_windows()
147endfunc
148
149" Pending: https://groups.google.com/forum/#!topic/vim_dev/tzNKP7EDWYI
150func XTest_number_with_linewrap2()
151 call s:test_windows(3, 20)
152 normal! 61ia
153 setl number wrap
154 call s:validate_cursor()
155 0
156 call s:validate_cursor()
157 let lines = s:screen_lines(1, 3)
158 let expect = [
159\ " 1 aaaa",
160\ " aaaa",
161\ " aaaa",
162\ ]
163 call s:compare_lines(expect, lines)
164 call s:close_windows()
165endfunc
166
167" Pending: https://groups.google.com/forum/#!topic/vim_dev/tzNKP7EDWYI
168func XTest_number_with_linewrap3()
169 call s:test_windows(4, 20)
170 normal! 81ia
171 setl number wrap
172 call s:validate_cursor()
173 setl nonumber
174 call s:validate_cursor()
175 let lines = s:screen_lines(1, 4)
176 let expect = [
177\ "aaaaaaaa",
178\ "aaaaaaaa",
179\ "aaaaaaaa",
180\ "a ",
181\ ]
182 call s:compare_lines(expect, lines)
183 call s:close_windows()
184endfunc
185
186func Test_numberwidth()
187 call s:test_windows(10, 20)
188 call setline(1, repeat(['aaaa'], 10))
189 setl number numberwidth=6
190 let lines = s:screen_lines(1, 3)
191 let expect = [
192\ " 1 aa",
193\ " 2 aa",
194\ " 3 aa",
195\ ]
196 call s:compare_lines(expect, lines)
197
198 set relativenumber
199 let lines = s:screen_lines(1, 3)
200 let expect = [
201\ "1 aa",
202\ " 1 aa",
203\ " 2 aa",
204\ ]
205 call s:compare_lines(expect, lines)
206
207 set nonumber
208 let lines = s:screen_lines(1, 3)
209 let expect = [
210\ " 0 aa",
211\ " 1 aa",
212\ " 2 aa",
213\ ]
214 call s:compare_lines(expect, lines)
215 call s:close_windows()
216endfunc
217
218func Test_numberwidth_adjusted()
219 call s:test_windows(10, 20)
220 call setline(1, repeat(['aaaa'], 10000))
221 setl number numberwidth=4
222 let lines = s:screen_lines(1, 3)
223 let expect = [
224\ " 1 aa",
225\ " 2 aa",
226\ " 3 aa",
227\ ]
228 call s:compare_lines(expect, lines)
229
230 $
231 let lines = s:screen_lines(8, 10)
232 let expect = [
233\ " 9998 aa",
234\ " 9999 aa",
235\ "10000 aa",
236\ ]
237 call s:compare_lines(expect, lines)
238
239 setl relativenumber
240 let lines = s:screen_lines(8, 10)
241 let expect = [
242\ " 2 aa",
243\ " 1 aa",
244\ "10000 aa",
245\ ]
246 call s:compare_lines(expect, lines)
247
248 setl nonumber
249 let lines = s:screen_lines(8, 10)
250 let expect = [
251\ " 2 aaaa",
252\ " 1 aaaa",
253\ " 0 aaaa",
254\ ]
255 call s:compare_lines(expect, lines)
256 call s:close_windows()
257endfunc
Bram Moolenaarec572ad2019-07-07 14:26:59 +0200258
259" This was causing a memcheck error
260func Test_relativenumber_uninitialised()
261 new
262 set rnu
263 call setline(1, ["a", "b"])
264 redraw
265 call feedkeys("j", 'xt')
266 redraw
267 bwipe!
268endfunc
Bram Moolenaarefae76a2019-10-27 22:54:58 +0100269
270func Test_relativenumber_colors()
271 CheckScreendump
272
273 let lines =<< trim [CODE]
274 call setline(1, range(200))
275 111
276 set number relativenumber
277 hi LineNr ctermfg=red
278 [CODE]
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100279 call writefile(lines, 'XTest_relnr', 'D')
Bram Moolenaarefae76a2019-10-27 22:54:58 +0100280
Bram Moolenaarefae76a2019-10-27 22:54:58 +0100281 let buf = RunVimInTerminal('-S XTest_relnr', {'rows': 10, 'cols': 50})
Bram Moolenaarefae76a2019-10-27 22:54:58 +0100282 " Default colors
283 call VerifyScreenDump(buf, 'Test_relnr_colors_1', {})
284
Bram Moolenaar8040a712020-02-23 13:38:08 +0100285 call term_sendkeys(buf, ":hi LineNrAbove ctermfg=blue\<CR>:\<CR>")
Bram Moolenaarefae76a2019-10-27 22:54:58 +0100286 call VerifyScreenDump(buf, 'Test_relnr_colors_2', {})
287
Bram Moolenaar8040a712020-02-23 13:38:08 +0100288 call term_sendkeys(buf, ":hi LineNrBelow ctermfg=green\<CR>:\<CR>")
Bram Moolenaarefae76a2019-10-27 22:54:58 +0100289 call VerifyScreenDump(buf, 'Test_relnr_colors_3', {})
290
291 call term_sendkeys(buf, ":hi clear LineNrAbove\<CR>")
292 call VerifyScreenDump(buf, 'Test_relnr_colors_4', {})
293
294 " clean up
295 call StopVimInTerminal(buf)
Bram Moolenaarefae76a2019-10-27 22:54:58 +0100296endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200297
zeertzjqebfd8562024-02-06 10:59:03 +0100298func Test_relativenumber_colors_wrapped()
299 CheckScreendump
300
301 let lines =<< trim [CODE]
302 set display=lastline scrolloff=0
303 call setline(1, range(200)->map('v:val->string()->repeat(40)'))
304 111
305 set number relativenumber
306 hi LineNr ctermbg=red ctermfg=black
307 hi LineNrAbove ctermbg=blue ctermfg=black
308 hi LineNrBelow ctermbg=green ctermfg=black
309 [CODE]
310 call writefile(lines, 'XTest_relnr_wrap', 'D')
311
312 let buf = RunVimInTerminal('-S XTest_relnr_wrap', {'rows': 20, 'cols': 50})
313
314 call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_1', {})
315 call term_sendkeys(buf, "k")
316 call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_2', {})
317 call term_sendkeys(buf, "2j")
318 call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_3', {})
319 call term_sendkeys(buf, "2j")
320 call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_4', {})
321 call term_sendkeys(buf, "k")
322 call VerifyScreenDump(buf, 'Test_relnr_colors_wrapped_5', {})
323
324 " clean up
325 call StopVimInTerminal(buf)
326endfunc
327
zeertzjq3e559cd2022-03-27 19:26:55 +0100328func Test_relativenumber_callback()
329 CheckScreendump
330 CheckFeature timers
331
332 let lines =<< trim END
333 call setline(1, ['aaaaa', 'bbbbb', 'ccccc', 'ddddd'])
334 set relativenumber
335 call cursor(4, 1)
336
337 func Func(timer)
338 call cursor(1, 1)
339 endfunc
340
341 call timer_start(300, 'Func')
342 END
Bram Moolenaarb152b6a2022-09-29 21:37:33 +0100343 call writefile(lines, 'Xrnu_timer', 'D')
zeertzjq3e559cd2022-03-27 19:26:55 +0100344
345 let buf = RunVimInTerminal('-S Xrnu_timer', #{rows: 8})
346 call TermWait(buf, 310)
347 call VerifyScreenDump(buf, 'Test_relativenumber_callback_1', {})
348
349 call StopVimInTerminal(buf)
zeertzjq3e559cd2022-03-27 19:26:55 +0100350endfunc
351
Christian Brabandt29f0dc32021-06-16 19:28:34 +0200352" Test for displaying line numbers with 'rightleft'
353func Test_number_rightleft()
354 CheckFeature rightleft
355 new
356 setlocal number
357 setlocal rightleft
358 call setline(1, range(1, 1000))
359 normal! 9Gzt
360 redraw!
361 call assert_match('^\s\+9 9$', Screenline(1))
362 normal! 10Gzt
363 redraw!
364 call assert_match('^\s\+01 10$', Screenline(1))
365 normal! 100Gzt
366 redraw!
367 call assert_match('^\s\+001 100$', Screenline(1))
368 normal! 1000Gzt
369 redraw!
370 call assert_match('^\s\+0001 1000$', Screenline(1))
371 bw!
372endfunc
373
Bram Moolenaar02f86942021-08-17 22:14:29 +0200374" This used to cause a divide by zero
375func Test_number_no_text_virtual_edit()
376 vnew
377 call setline(1, ['line one', 'line two'])
378 set number virtualedit=all
379 normal w
380 4wincmd |
381 normal j
382 bwipe!
383endfunc
384
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200385" vim: shiftwidth=2 sts=2 expandtab