xref: /vim-8.2.3635/src/testdir/test_vartabs.vim (revision 59585497)
1" Test for variable tabstops
2
3source check.vim
4CheckFeature vartabs
5
6source view_util.vim
7
8func s:compare_lines(expect, actual)
9  call assert_equal(join(a:expect, "\n"), join(a:actual, "\n"))
10endfunc
11
12func Test_vartabs()
13  new
14  %d
15
16  " Test normal operation of tabstops ...
17  set ts=4
18  call setline(1, join(split('aaaaa', '\zs'), "\t"))
19  retab 8
20  let expect = "a   a\<tab>a   a\<tab>a"
21  call assert_equal(expect, getline(1))
22
23  " ... and softtabstops
24  set ts=8 sts=6
25  exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
26  let expect = "b     b\<tab>    b\<tab>  b\<tab>b"
27  call assert_equal(expect, getline(1))
28
29  " Test variable tabstops.
30  set sts=0 vts=4,8,4,8
31  exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
32  retab 8
33  let expect = "c   c\<tab>    c\<tab>c\<tab>c\<tab>c"
34  call assert_equal(expect, getline(1))
35
36  set et vts=4,8,4,8
37  exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
38  let expect = "d   d       d   d       d       d"
39  call assert_equal(expect, getline(1))
40
41  " Changing ts should have no effect if vts is in use.
42  call cursor(1, 1)
43  set ts=6
44  exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
45  let expect = "e   e       e   e       e       e"
46  call assert_equal(expect, getline(1))
47
48  " Clearing vts should revert to using ts.
49  set vts=
50  exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
51  let expect = "f     f     f     f     f     f"
52  call assert_equal(expect, getline(1))
53
54  " Test variable softtabstops.
55  set noet ts=8 vsts=12,2,6
56  exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
57  let expect = "g\<tab>    g g\<tab>    g\<tab>  g\<tab>g"
58  call assert_equal(expect, getline(1))
59
60  " Variable tabstops and softtabstops combined.
61  set vsts=6,12,8 vts=4,6,8
62  exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
63  let expect = "h\<tab>  h\<tab>\<tab>h\<tab>h\<tab>h"
64  call assert_equal(expect, getline(1))
65
66  " Retab with a single value, not using vts.
67  set ts=8 sts=0 vts= vsts=
68  exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
69  retab 4
70  let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
71  call assert_equal(expect, getline(1))
72
73  " Retab with a single value, using vts.
74  set ts=8 sts=0 vts=6 vsts=
75  exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
76  retab 4
77  let expect = "j\<tab>  j\<tab>\<tab>j\<tab>  j\<tab>\<tab>j"
78  call assert_equal(expect, getline(1))
79
80  " Retab with multiple values, not using vts.
81  set ts=6 sts=0 vts= vsts=
82  exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
83  retab 4,8
84  let expect = "k\<tab>  k\<tab>k     k\<tab>    k\<tab>  k"
85  call assert_equal(expect, getline(1))
86
87  " Retab with multiple values, using vts.
88  set ts=8 sts=0 vts=6 vsts=
89  exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
90  retab 4,8
91  let expect = "l\<tab>  l\<tab>l     l\<tab>    l\<tab>  l"
92  call assert_equal(expect, getline(1))
93
94  " Test for 'retab' with vts
95  set ts=8 sts=0 vts=5,3,6,2 vsts=
96  exe "norm! S                l"
97  .retab!
98  call assert_equal("\t\t\t\tl", getline(1))
99
100  " Test for 'retab' with same values as vts
101  set ts=8 sts=0 vts=5,3,6,2 vsts=
102  exe "norm! S                l"
103  .retab! 5,3,6,2
104  call assert_equal("\t\t\t\tl", getline(1))
105
106  " Check that global and local values are set.
107  set ts=4 vts=6 sts=8 vsts=10
108  call assert_equal(&ts, 4)
109  call assert_equal(&vts, '6')
110  call assert_equal(&sts, 8)
111  call assert_equal(&vsts, '10')
112  new
113  call assert_equal(&ts, 4)
114  call assert_equal(&vts, '6')
115  call assert_equal(&sts, 8)
116  call assert_equal(&vsts, '10')
117  bwipeout!
118
119  " Check that local values only are set.
120  setlocal ts=5 vts=7 sts=9 vsts=11
121  call assert_equal(&ts, 5)
122  call assert_equal(&vts, '7')
123  call assert_equal(&sts, 9)
124  call assert_equal(&vsts, '11')
125  new
126  call assert_equal(&ts, 4)
127  call assert_equal(&vts, '6')
128  call assert_equal(&sts, 8)
129  call assert_equal(&vsts, '10')
130  bwipeout!
131
132  " Check that global values only are set.
133  setglobal ts=6 vts=8 sts=10 vsts=12
134  call assert_equal(&ts, 5)
135  call assert_equal(&vts, '7')
136  call assert_equal(&sts, 9)
137  call assert_equal(&vsts, '11')
138  new
139  call assert_equal(&ts, 6)
140  call assert_equal(&vts, '8')
141  call assert_equal(&sts, 10)
142  call assert_equal(&vsts, '12')
143  bwipeout!
144
145  set ts& vts& sts& vsts& et&
146  bwipeout!
147endfunc
148
149func Test_vartabs_breakindent()
150  CheckOption breakindent
151  new
152  %d
153
154  " Test normal operation of tabstops ...
155  set ts=4
156  call setline(1, join(split('aaaaa', '\zs'), "\t"))
157  retab 8
158  let expect = "a   a\<tab>a   a\<tab>a"
159  call assert_equal(expect, getline(1))
160
161  " ... and softtabstops
162  set ts=8 sts=6
163  exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
164  let expect = "b     b\<tab>    b\<tab>  b\<tab>b"
165  call assert_equal(expect, getline(1))
166
167  " Test variable tabstops.
168  set sts=0 vts=4,8,4,8
169  exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
170  retab 8
171  let expect = "c   c\<tab>    c\<tab>c\<tab>c\<tab>c"
172  call assert_equal(expect, getline(1))
173
174  set et vts=4,8,4,8
175  exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
176  let expect = "d   d       d   d       d       d"
177  call assert_equal(expect, getline(1))
178
179  " Changing ts should have no effect if vts is in use.
180  call cursor(1, 1)
181  set ts=6
182  exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
183  let expect = "e   e       e   e       e       e"
184  call assert_equal(expect, getline(1))
185
186  " Clearing vts should revert to using ts.
187  set vts=
188  exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
189  let expect = "f     f     f     f     f     f"
190  call assert_equal(expect, getline(1))
191
192  " Test variable softtabstops.
193  set noet ts=8 vsts=12,2,6
194  exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
195  let expect = "g\<tab>    g g\<tab>    g\<tab>  g\<tab>g"
196  call assert_equal(expect, getline(1))
197
198  " Variable tabstops and softtabstops combined.
199  set vsts=6,12,8 vts=4,6,8
200  exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
201  let expect = "h\<tab>  h\<tab>\<tab>h\<tab>h\<tab>h"
202  call assert_equal(expect, getline(1))
203
204  " Retab with a single value, not using vts.
205  set ts=8 sts=0 vts= vsts=
206  exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
207  retab 4
208  let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
209  call assert_equal(expect, getline(1))
210
211  " Retab with a single value, using vts.
212  set ts=8 sts=0 vts=6 vsts=
213  exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
214  retab 4
215  let expect = "j\<tab>  j\<tab>\<tab>j\<tab>  j\<tab>\<tab>j"
216  call assert_equal(expect, getline(1))
217
218  " Retab with multiple values, not using vts.
219  set ts=6 sts=0 vts= vsts=
220  exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
221  retab 4,8
222  let expect = "k\<tab>  k\<tab>k     k\<tab>    k\<tab>  k"
223  call assert_equal(expect, getline(1))
224
225  " Retab with multiple values, using vts.
226  set ts=8 sts=0 vts=6 vsts=
227  exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
228  retab 4,8
229  let expect = "l\<tab>  l\<tab>l     l\<tab>    l\<tab>  l"
230  call assert_equal(expect, getline(1))
231
232  " Check that global and local values are set.
233  set ts=4 vts=6 sts=8 vsts=10
234  call assert_equal(&ts, 4)
235  call assert_equal(&vts, '6')
236  call assert_equal(&sts, 8)
237  call assert_equal(&vsts, '10')
238  new
239  call assert_equal(&ts, 4)
240  call assert_equal(&vts, '6')
241  call assert_equal(&sts, 8)
242  call assert_equal(&vsts, '10')
243  bwipeout!
244
245  " Check that local values only are set.
246  setlocal ts=5 vts=7 sts=9 vsts=11
247  call assert_equal(&ts, 5)
248  call assert_equal(&vts, '7')
249  call assert_equal(&sts, 9)
250  call assert_equal(&vsts, '11')
251  new
252  call assert_equal(&ts, 4)
253  call assert_equal(&vts, '6')
254  call assert_equal(&sts, 8)
255  call assert_equal(&vsts, '10')
256  bwipeout!
257
258  " Check that global values only are set.
259  setglobal ts=6 vts=8 sts=10 vsts=12
260  call assert_equal(&ts, 5)
261  call assert_equal(&vts, '7')
262  call assert_equal(&sts, 9)
263  call assert_equal(&vsts, '11')
264  new
265  call assert_equal(&ts, 6)
266  call assert_equal(&vts, '8')
267  call assert_equal(&sts, 10)
268  call assert_equal(&vsts, '12')
269  bwipeout!
270
271  bwipeout!
272endfunc
273
274func Test_vartabs_linebreak()
275  if winwidth(0) < 40
276    return
277  endif
278  new
279  40vnew
280  %d
281  setl linebreak vartabstop=10,20,30,40
282  call setline(1, "\tx\tx\tx\tx")
283
284  let expect = ['          x                             ',
285        \       'x                   x                   ',
286        \       'x                                       ']
287  let lines = ScreenLines([1, 3], winwidth(0))
288  call s:compare_lines(expect, lines)
289  setl list listchars=tab:>-
290  let expect = ['>---------x>------------------          ',
291        \       'x>------------------x>------------------',
292        \       'x                                       ']
293  let lines = ScreenLines([1, 3], winwidth(0))
294  call s:compare_lines(expect, lines)
295  setl linebreak vartabstop=40
296  let expect = ['>---------------------------------------',
297        \       'x>--------------------------------------',
298        \       'x>--------------------------------------',
299        \       'x>--------------------------------------',
300        \       'x                                       ']
301  let lines = ScreenLines([1, 5], winwidth(0))
302  call s:compare_lines(expect, lines)
303
304  " cleanup
305  bw!
306  bw!
307  set nolist listchars&vim
308endfunc
309
310func Test_vartabs_shiftwidth()
311  "return
312  if winwidth(0) < 40
313    return
314  endif
315  new
316  40vnew
317  %d
318"  setl varsofttabstop=10,20,30,40
319  setl shiftwidth=0 vartabstop=10,20,30,40
320  call setline(1, "x")
321
322  " Check without any change.
323  let expect = ['x                                       ']
324  let lines = ScreenLines(1, winwidth(0))
325  call s:compare_lines(expect, lines)
326  " Test 1:
327  " shiftwidth depends on the indent, first check with cursor at the end of the
328  " line (which is the same as the start of the line, since there is only one
329  " character).
330  norm! $>>
331  let expect1 = ['          x                             ']
332  let lines = ScreenLines(1, winwidth(0))
333  call s:compare_lines(expect1, lines)
334  call assert_equal(10, shiftwidth())
335  call assert_equal(10, shiftwidth(1))
336  call assert_equal(20, shiftwidth(virtcol('.')))
337  norm! $>>
338  let expect2 = ['                              x         ', '~                                       ']
339  let lines = ScreenLines([1, 2], winwidth(0))
340  call s:compare_lines(expect2, lines)
341  call assert_equal(20, shiftwidth(virtcol('.')-2))
342  call assert_equal(30, virtcol('.')->shiftwidth())
343  norm! $>>
344  let expect3 = ['                                        ', '                    x                   ', '~                                       ']
345  let lines = ScreenLines([1, 3], winwidth(0))
346  call s:compare_lines(expect3, lines)
347  call assert_equal(30, shiftwidth(virtcol('.')-2))
348  call assert_equal(40, shiftwidth(virtcol('.')))
349  norm! $>>
350  let expect4 = ['                                        ', '                                        ', '                    x                   ']
351  let lines = ScreenLines([1, 3], winwidth(0))
352  call assert_equal(40, shiftwidth(virtcol('.')))
353  call s:compare_lines(expect4, lines)
354
355  " Test 2: Put the cursor at the first column, result should be the same
356  call setline(1, "x")
357  norm! 0>>
358  let lines = ScreenLines(1, winwidth(0))
359  call s:compare_lines(expect1, lines)
360  norm! 0>>
361  let lines = ScreenLines([1, 2], winwidth(0))
362  call s:compare_lines(expect2, lines)
363  norm! 0>>
364  let lines = ScreenLines([1, 3], winwidth(0))
365  call s:compare_lines(expect3, lines)
366  norm! 0>>
367  let lines = ScreenLines([1, 3], winwidth(0))
368  call s:compare_lines(expect4, lines)
369
370  call assert_fails('call shiftwidth([])', 'E745:')
371
372  " cleanup
373  bw!
374  bw!
375endfunc
376
377func Test_vartabs_failures()
378  call assert_fails('set vts=8,')
379  call assert_fails('set vsts=8,')
380  call assert_fails('set vts=8,,8')
381  call assert_fails('set vsts=8,,8')
382  call assert_fails('set vts=8,,8,')
383  call assert_fails('set vsts=8,,8,')
384  call assert_fails('set vts=,8')
385  call assert_fails('set vsts=,8')
386endfunc
387
388func Test_vartabs_reset()
389  set vts=8
390  set all&
391  call assert_equal('', &vts)
392endfunc
393
394func s:SaveCol(l)
395  call add(a:l, [col('.'), virtcol('.')])
396  return ''
397endfunc
398
399" Test for 'varsofttabstop'
400func Test_varsofttabstop()
401  new
402  inoremap <expr> <F2>  s:SaveCol(g:cols)
403
404  set backspace=indent,eol,start
405  set varsofttabstop=6,2,5,3
406  let g:cols = []
407  call feedkeys("a\t\<F2>\t\<F2>\t\<F2>\t\<F2> ", 'xt')
408  call assert_equal("\t\t ", getline(1))
409  call assert_equal([[7, 7], [2, 9], [7, 14], [3, 17]], g:cols)
410
411  let g:cols = []
412  call feedkeys("a\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>\<bs>\<F2>", 'xt')
413  call assert_equal('', getline(1))
414  call assert_equal([[3, 17], [7, 14], [2, 9], [7, 7], [1, 1]], g:cols)
415
416  set varsofttabstop&
417  set backspace&
418  iunmap <F2>
419  close!
420endfunc
421
422" Setting 'shiftwidth' to a negative value, should set it to either the value
423" of 'tabstop' (if 'vartabstop' is not set) or to the first value in
424" 'vartabstop'
425func Test_shiftwidth_vartabstop()
426  setlocal tabstop=7 vartabstop=
427  call assert_fails('set shiftwidth=-1', 'E487:')
428  call assert_equal(7, &shiftwidth)
429  setlocal tabstop=7 vartabstop=5,7,10
430  call assert_fails('set shiftwidth=-1', 'E487:')
431  call assert_equal(5, &shiftwidth)
432  setlocal shiftwidth& vartabstop& tabstop&
433endfunc
434
435" vim: shiftwidth=2 sts=2 expandtab
436