xref: /vim-8.2.3635/src/testdir/test_vartabs.vim (revision fcfe1a9b)
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  " Check that global and local values are set.
95  set ts=4 vts=6 sts=8 vsts=10
96  call assert_equal(&ts, 4)
97  call assert_equal(&vts, '6')
98  call assert_equal(&sts, 8)
99  call assert_equal(&vsts, '10')
100  new
101  call assert_equal(&ts, 4)
102  call assert_equal(&vts, '6')
103  call assert_equal(&sts, 8)
104  call assert_equal(&vsts, '10')
105  bwipeout!
106
107  " Check that local values only are set.
108  setlocal ts=5 vts=7 sts=9 vsts=11
109  call assert_equal(&ts, 5)
110  call assert_equal(&vts, '7')
111  call assert_equal(&sts, 9)
112  call assert_equal(&vsts, '11')
113  new
114  call assert_equal(&ts, 4)
115  call assert_equal(&vts, '6')
116  call assert_equal(&sts, 8)
117  call assert_equal(&vsts, '10')
118  bwipeout!
119
120  " Check that global values only are set.
121  setglobal ts=6 vts=8 sts=10 vsts=12
122  call assert_equal(&ts, 5)
123  call assert_equal(&vts, '7')
124  call assert_equal(&sts, 9)
125  call assert_equal(&vsts, '11')
126  new
127  call assert_equal(&ts, 6)
128  call assert_equal(&vts, '8')
129  call assert_equal(&sts, 10)
130  call assert_equal(&vsts, '12')
131  bwipeout!
132
133  set ts& vts& sts& vsts& et&
134  bwipeout!
135endfunc
136
137func Test_vartabs_breakindent()
138  if !exists("+breakindent")
139    return
140  endif
141  new
142  %d
143
144  " Test normal operation of tabstops ...
145  set ts=4
146  call setline(1, join(split('aaaaa', '\zs'), "\t"))
147  retab 8
148  let expect = "a   a\<tab>a   a\<tab>a"
149  call assert_equal(expect, getline(1))
150
151  " ... and softtabstops
152  set ts=8 sts=6
153  exe "norm! Sb\<tab>b\<tab>b\<tab>b\<tab>b"
154  let expect = "b     b\<tab>    b\<tab>  b\<tab>b"
155  call assert_equal(expect, getline(1))
156
157  " Test variable tabstops.
158  set sts=0 vts=4,8,4,8
159  exe "norm! Sc\<tab>c\<tab>c\<tab>c\<tab>c\<tab>c"
160  retab 8
161  let expect = "c   c\<tab>    c\<tab>c\<tab>c\<tab>c"
162  call assert_equal(expect, getline(1))
163
164  set et vts=4,8,4,8
165  exe "norm! Sd\<tab>d\<tab>d\<tab>d\<tab>d\<tab>d"
166  let expect = "d   d       d   d       d       d"
167  call assert_equal(expect, getline(1))
168
169  " Changing ts should have no effect if vts is in use.
170  call cursor(1, 1)
171  set ts=6
172  exe "norm! Se\<tab>e\<tab>e\<tab>e\<tab>e\<tab>e"
173  let expect = "e   e       e   e       e       e"
174  call assert_equal(expect, getline(1))
175
176  " Clearing vts should revert to using ts.
177  set vts=
178  exe "norm! Sf\<tab>f\<tab>f\<tab>f\<tab>f\<tab>f"
179  let expect = "f     f     f     f     f     f"
180  call assert_equal(expect, getline(1))
181
182  " Test variable softtabstops.
183  set noet ts=8 vsts=12,2,6
184  exe "norm! Sg\<tab>g\<tab>g\<tab>g\<tab>g\<tab>g"
185  let expect = "g\<tab>    g g\<tab>    g\<tab>  g\<tab>g"
186  call assert_equal(expect, getline(1))
187
188  " Variable tabstops and softtabstops combined.
189  set vsts=6,12,8 vts=4,6,8
190  exe "norm! Sh\<tab>h\<tab>h\<tab>h\<tab>h"
191  let expect = "h\<tab>  h\<tab>\<tab>h\<tab>h\<tab>h"
192  call assert_equal(expect, getline(1))
193
194  " Retab with a single value, not using vts.
195  set ts=8 sts=0 vts= vsts=
196  exe "norm! Si\<tab>i\<tab>i\<tab>i\<tab>i"
197  retab 4
198  let expect = "i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i\<tab>\<tab>i"
199  call assert_equal(expect, getline(1))
200
201  " Retab with a single value, using vts.
202  set ts=8 sts=0 vts=6 vsts=
203  exe "norm! Sj\<tab>j\<tab>j\<tab>j\<tab>j"
204  retab 4
205  let expect = "j\<tab>  j\<tab>\<tab>j\<tab>  j\<tab>\<tab>j"
206  call assert_equal(expect, getline(1))
207
208  " Retab with multiple values, not using vts.
209  set ts=6 sts=0 vts= vsts=
210  exe "norm! Sk\<tab>k\<tab>k\<tab>k\<tab>k\<tab>k"
211  retab 4,8
212  let expect = "k\<tab>  k\<tab>k     k\<tab>    k\<tab>  k"
213  call assert_equal(expect, getline(1))
214
215  " Retab with multiple values, using vts.
216  set ts=8 sts=0 vts=6 vsts=
217  exe "norm! Sl\<tab>l\<tab>l\<tab>l\<tab>l\<tab>l"
218  retab 4,8
219  let expect = "l\<tab>  l\<tab>l     l\<tab>    l\<tab>  l"
220  call assert_equal(expect, getline(1))
221
222  " Check that global and local values are set.
223  set ts=4 vts=6 sts=8 vsts=10
224  call assert_equal(&ts, 4)
225  call assert_equal(&vts, '6')
226  call assert_equal(&sts, 8)
227  call assert_equal(&vsts, '10')
228  new
229  call assert_equal(&ts, 4)
230  call assert_equal(&vts, '6')
231  call assert_equal(&sts, 8)
232  call assert_equal(&vsts, '10')
233  bwipeout!
234
235  " Check that local values only are set.
236  setlocal ts=5 vts=7 sts=9 vsts=11
237  call assert_equal(&ts, 5)
238  call assert_equal(&vts, '7')
239  call assert_equal(&sts, 9)
240  call assert_equal(&vsts, '11')
241  new
242  call assert_equal(&ts, 4)
243  call assert_equal(&vts, '6')
244  call assert_equal(&sts, 8)
245  call assert_equal(&vsts, '10')
246  bwipeout!
247
248  " Check that global values only are set.
249  setglobal ts=6 vts=8 sts=10 vsts=12
250  call assert_equal(&ts, 5)
251  call assert_equal(&vts, '7')
252  call assert_equal(&sts, 9)
253  call assert_equal(&vsts, '11')
254  new
255  call assert_equal(&ts, 6)
256  call assert_equal(&vts, '8')
257  call assert_equal(&sts, 10)
258  call assert_equal(&vsts, '12')
259  bwipeout!
260
261  bwipeout!
262endfunc
263
264func Test_vartabs_linebreak()
265  if winwidth(0) < 40
266    return
267  endif
268  new
269  40vnew
270  %d
271  setl linebreak vartabstop=10,20,30,40
272  call setline(1, "\tx\tx\tx\tx")
273
274  let expect = ['          x                             ',
275        \       'x                   x                   ',
276        \       'x                                       ']
277  let lines = ScreenLines([1, 3], winwidth(0))
278  call s:compare_lines(expect, lines)
279  setl list listchars=tab:>-
280  let expect = ['>---------x>------------------          ',
281        \       'x>------------------x>------------------',
282        \       'x                                       ']
283  let lines = ScreenLines([1, 3], winwidth(0))
284  call s:compare_lines(expect, lines)
285  setl linebreak vartabstop=40
286  let expect = ['>---------------------------------------',
287        \       'x>--------------------------------------',
288        \       'x>--------------------------------------',
289        \       'x>--------------------------------------',
290        \       'x                                       ']
291  let lines = ScreenLines([1, 5], winwidth(0))
292  call s:compare_lines(expect, lines)
293
294  " cleanup
295  bw!
296  bw!
297  set nolist listchars&vim
298endfunc
299
300func Test_vartabs_shiftwidth()
301  "return
302  if winwidth(0) < 40
303    return
304  endif
305  new
306  40vnew
307  %d
308"  setl varsofttabstop=10,20,30,40
309  setl shiftwidth=0 vartabstop=10,20,30,40
310  call setline(1, "x")
311
312  " Check without any change.
313  let expect = ['x                                       ']
314  let lines = ScreenLines(1, winwidth(0))
315  call s:compare_lines(expect, lines)
316  " Test 1:
317  " shiftwidth depends on the indent, first check with cursor at the end of the
318  " line (which is the same as the start of the line, since there is only one
319  " character).
320  norm! $>>
321  let expect1 = ['          x                             ']
322  let lines = ScreenLines(1, winwidth(0))
323  call s:compare_lines(expect1, lines)
324  call assert_equal(10, shiftwidth())
325  call assert_equal(10, shiftwidth(1))
326  call assert_equal(20, shiftwidth(virtcol('.')))
327  norm! $>>
328  let expect2 = ['                              x         ', '~                                       ']
329  let lines = ScreenLines([1, 2], winwidth(0))
330  call s:compare_lines(expect2, lines)
331  call assert_equal(20, shiftwidth(virtcol('.')-2))
332  call assert_equal(30, shiftwidth(virtcol('.')))
333  norm! $>>
334  let expect3 = ['                                        ', '                    x                   ', '~                                       ']
335  let lines = ScreenLines([1, 3], winwidth(0))
336  call s:compare_lines(expect3, lines)
337  call assert_equal(30, shiftwidth(virtcol('.')-2))
338  call assert_equal(40, shiftwidth(virtcol('.')))
339  norm! $>>
340  let expect4 = ['                                        ', '                                        ', '                    x                   ']
341  let lines = ScreenLines([1, 3], winwidth(0))
342  call assert_equal(40, shiftwidth(virtcol('.')))
343  call s:compare_lines(expect4, lines)
344
345  " Test 2: Put the cursor at the first column, result should be the same
346  call setline(1, "x")
347  norm! 0>>
348  let lines = ScreenLines(1, winwidth(0))
349  call s:compare_lines(expect1, lines)
350  norm! 0>>
351  let lines = ScreenLines([1, 2], winwidth(0))
352  call s:compare_lines(expect2, lines)
353  norm! 0>>
354  let lines = ScreenLines([1, 3], winwidth(0))
355  call s:compare_lines(expect3, lines)
356  norm! 0>>
357  let lines = ScreenLines([1, 3], winwidth(0))
358  call s:compare_lines(expect4, lines)
359
360  " cleanup
361  bw!
362  bw!
363endfunc
364
365func Test_vartabs_failures()
366  call assert_fails('set vts=8,')
367  call assert_fails('set vsts=8,')
368  call assert_fails('set vts=8,,8')
369  call assert_fails('set vsts=8,,8')
370  call assert_fails('set vts=8,,8,')
371  call assert_fails('set vsts=8,,8,')
372  call assert_fails('set vts=,8')
373  call assert_fails('set vsts=,8')
374endfunc
375
376func Test_vartabs_reset()
377  set vts=8
378  set all&
379  call assert_equal('', &vts)
380endfunc
381