robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.2.0329: tests: test_indent.vim leaves swapfiles behind (#38921)10e385cf30

master
10.9 KiB392 linesraw
1" Test for various indent options
2
3source shared.vim
4source check.vim
5
6func Test_preserveindent()
7  new
8  " Test for autoindent copying indent from the previous line
9  setlocal autoindent
10  call setline(1, [repeat(' ', 16) .. 'line1'])
11  call feedkeys("A\nline2", 'xt')
12  call assert_equal("\t\tline2", getline(2))
13  setlocal autoindent&
14
15  " Test for using CTRL-T with and without 'preserveindent'
16  set shiftwidth=4
17  call cursor(1, 1)
18  call setline(1, "    \t    ")
19  call feedkeys("Al\<C-T>", 'xt')
20  call assert_equal("\t\tl", getline(1))
21  set preserveindent
22  call setline(1, "    \t    ")
23  call feedkeys("Al\<C-T>", 'xt')
24  call assert_equal("    \t    \tl", getline(1))
25  set pi& sw&
26
27  " Test for using CTRL-T with 'expandtab' and 'preserveindent'
28  call cursor(1, 1)
29  call setline(1, "\t    \t")
30  set shiftwidth=4 expandtab preserveindent
31  call feedkeys("Al\<C-T>", 'xt')
32  call assert_equal("\t    \t    l", getline(1))
33  set sw& et& pi&
34
35  bw!
36endfunc
37
38" Test for indent()
39func Test_indent_func()
40  call assert_equal(-1, indent(-1))
41  new
42  call setline(1, "\tabc")
43  call assert_equal(8, indent(1))
44  call setline(1, "    abc")
45  call assert_equal(4, indent(1))
46  call setline(1, "    \t    abc")
47  call assert_equal(12, indent(1))
48  bw!
49endfunc
50
51" Test for reindenting a line using the '=' operator
52func Test_reindent()
53  new
54  call setline(1, 'abc')
55  set nomodifiable
56  call assert_fails('normal ==', 'E21:')
57  set modifiable
58
59  call setline(1, ['foo', 'bar'])
60  call feedkeys('ggVG=', 'xt')
61  call assert_equal(['foo', 'bar'], getline(1, 2))
62  bw!
63endfunc
64
65" Test indent operator creating one undo entry
66func Test_indent_operator_undo()
67  enew
68  call setline(1, range(12)->map('"\t" .. v:val'))
69  func FoldExpr()
70    let g:foldcount += 1
71    return '='
72  endfunc
73  set foldmethod=expr foldexpr=FoldExpr()
74  let g:foldcount = 0
75  redraw
76  call assert_equal(12, g:foldcount)
77  normal gg=G
78  call assert_equal(24, g:foldcount)
79  undo
80  call assert_equal(38, g:foldcount)
81
82  bwipe!
83  set foldmethod& foldexpr=
84  delfunc FoldExpr
85  unlet g:foldcount
86endfunc
87
88" Test for shifting a line with a preprocessor directive ('#')
89func Test_preproc_indent()
90  new
91  set sw=4
92  call setline(1, '#define FOO 1')
93  normal >>
94  call assert_equal('    #define FOO 1', getline(1))
95
96  " with 'smartindent'
97  call setline(1, '#define FOO 1')
98  set smartindent
99  normal >>
100  call assert_equal('#define FOO 1', getline(1))
101  set smartindent&
102
103  " with 'cindent'
104  set cindent
105  normal >>
106  call assert_equal('#define FOO 1', getline(1))
107  set cindent&
108
109  bw!
110endfunc
111
112func Test_userlabel_indent()
113  new
114  call setline(1, ['{', 'label:'])
115  normal GV=
116  call assert_equal('label:', getline(2))
117
118  call setline(2, 'läbél:')
119  normal GV=
120  call assert_equal('läbél:', getline(2))
121
122  bw!
123endfunc
124
125" Test that struct members are aligned
126func Test_struct_indent()
127  new
128  call setline(1, ['struct a a = {', '1,', '1,'])
129  normal gg=G
130  call assert_equal(getline(2), getline(3))
131
132  call setline(1, 'a = (struct a) {')
133  normal gg=G
134  call assert_equal(getline(2), getline(3))
135
136  call setline(1, 'void *ptr = &(static struct a) {{')
137  normal gg=G
138  call assert_equal(getline(2), getline(3))
139
140  call setline(1, 'a = (macro(arg1, "str)))")) {')
141  normal gg=G
142  call assert_equal(getline(2), getline(3))
143
144  call setline(1, 'return (struct a) {')
145  normal gg=G
146  call assert_equal(getline(2), getline(3))
147  bw!
148endfunc
149
150" Test for 'copyindent'
151func Test_copyindent()
152  new
153  set shiftwidth=4 autoindent expandtab copyindent
154  call setline(1, "    \t    abc")
155  call feedkeys("ol", 'xt')
156  call assert_equal("    \t    l", getline(2))
157  set noexpandtab
158  call setline(1, "    \t    abc")
159  call feedkeys("ol", 'xt')
160  call assert_equal("    \t    l", getline(2))
161  set sw& ai& et& ci&
162  bw!
163endfunc
164
165" Test for changing multiple lines with lisp indent
166func Test_lisp_indent_change_multiline()
167  new
168  setlocal lisp autoindent
169  call setline(1, ['(if a', '  (if b', '    (return 5)))'])
170  normal! jc2j(return 4))
171  call assert_equal('  (return 4))', getline(2))
172  bw!
173endfunc
174
175func Test_lisp_indent()
176  new
177  setlocal lisp autoindent
178  call setline(1, ['(if a', '  ;; comment', '  \ abc', '', '  " str1\', '  " st\b', '  (return 5)'])
179  normal! jo;; comment
180  normal! jo\ abc
181  normal! jo;; ret
182  normal! jostr1"
183  normal! jostr2"
184  call assert_equal(['  ;; comment', '  ;; comment', '  \ abc', '  \ abc', '', '  ;; ret', '  " str1\', '  str1"', '  " st\b', '  str2"'], getline(2, 11))
185  bw!
186endfunc
187
188func Test_lisp_indent_quoted()
189  " This was going past the end of the line
190  new
191  setlocal lisp autoindent
192  call setline(1, ['"[', '='])
193  normal Gvk=
194
195  bwipe!
196endfunc
197
198" Test for setting the 'indentexpr' from a modeline
199func Test_modeline_indent_expr()
200  let modeline = &modeline
201  set modeline
202  func GetIndent()
203    return line('.') * 2
204  endfunc
205  call writefile(['# vim: indentexpr=GetIndent()'], 'Xmlfile.txt', 'D')
206  set modelineexpr
207  new Xmlfile.txt
208  call assert_equal('GetIndent()', &indentexpr)
209  exe "normal Oa\nb\n"
210  call assert_equal(['  a', '    b'], getline(1, 2))
211
212  set modelineexpr&
213  delfunc GetIndent
214  let &modeline = modeline
215  bw!
216endfunc
217
218func Test_indent_func_with_gq()
219
220  function GetTeXIndent()
221    " Sample indent expression for TeX files
222    let lnum = prevnonblank(v:lnum - 1)
223    " At the start of the file use zero indent.
224    if lnum == 0
225      return 0
226    endif
227    let line = getline(lnum)
228    let ind = indent(lnum)
229    " Add a 'shiftwidth' after beginning of environments.
230    if line =~ '\\begin{center}'
231      let ind = ind + shiftwidth()
232    endif
233    return ind
234  endfunction
235
236  new
237  setl et sw=2 sts=2 ts=2 tw=50 indentexpr=GetTeXIndent()
238  put =[  '\documentclass{article}', '', '\begin{document}', '',
239        \ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce ut enim non',
240        \ 'libero efficitur aliquet. Maecenas metus justo, facilisis convallis blandit',
241        \ 'non, semper eu urna. Suspendisse diam diam, iaculis faucibus lorem eu,',
242        \ 'fringilla condimentum lectus. Quisque euismod diam at convallis vulputate.',
243        \ 'Pellentesque laoreet tortor sit amet mauris euismod ornare. Sed varius',
244        \ 'bibendum orci vel vehicula. Pellentesque tempor, ipsum et auctor accumsan,',
245        \ 'metus lectus ultrices odio, sed elementum mi ante at arcu.', '', '\begin{center}', '',
246        \ 'Proin nec risus consequat nunc dapibus consectetur. Mauris lacinia est a augue',
247        \ 'tristique accumsan. Morbi pretium, felis molestie eleifend condimentum, arcu',
248        \ 'ipsum congue nisl, quis euismod purus libero in ante.', '',
249        \ 'Donec id semper purus.',
250        \ 'Suspendisse eget aliquam nunc. Maecenas fringilla mauris vitae maximus',
251        \ 'condimentum. Cras a quam in mi dictum eleifend at a lorem. Sed convallis',
252        \ 'ante a commodo facilisis. Nam suscipit vulputate odio, vel dapibus nisl',
253        \ 'dignissim facilisis. Vestibulum ante ipsum primis in faucibus orci luctus et',
254        \ 'ultrices posuere cubilia curae;', '', '']
255  1d_
256  call cursor(5, 1)
257  ka
258  call cursor(14, 1)
259  kb
260  norm! 'agqap
261  norm! 'bgqG
262  let expected = [ '\documentclass{article}', '', '\begin{document}', '',
263        \ 'Lorem ipsum dolor sit amet, consectetur adipiscing',
264        \ 'elit. Fusce ut enim non libero efficitur aliquet.',
265        \ 'Maecenas metus justo, facilisis convallis blandit',
266        \ 'non, semper eu urna. Suspendisse diam diam,',
267        \ 'iaculis faucibus lorem eu, fringilla condimentum',
268        \ 'lectus. Quisque euismod diam at convallis',
269        \ 'vulputate.  Pellentesque laoreet tortor sit amet',
270        \ 'mauris euismod ornare. Sed varius bibendum orci',
271        \ 'vel vehicula. Pellentesque tempor, ipsum et auctor',
272        \ 'accumsan, metus lectus ultrices odio, sed',
273        \ 'elementum mi ante at arcu.', '', '\begin{center}', '',
274        \ '  Proin nec risus consequat nunc dapibus',
275        \ '  consectetur. Mauris lacinia est a augue',
276        \ '  tristique accumsan. Morbi pretium, felis',
277        \ '  molestie eleifend condimentum, arcu ipsum congue',
278        \ '  nisl, quis euismod purus libero in ante.',
279        \ '',
280        \ '  Donec id semper purus.  Suspendisse eget aliquam',
281        \ '  nunc. Maecenas fringilla mauris vitae maximus',
282        \ '  condimentum. Cras a quam in mi dictum eleifend',
283        \ '  at a lorem. Sed convallis ante a commodo',
284        \ '  facilisis. Nam suscipit vulputate odio, vel',
285        \ '  dapibus nisl dignissim facilisis. Vestibulum',
286        \ '  ante ipsum primis in faucibus orci luctus et',
287        \ '  ultrices posuere cubilia curae;', '', '']
288  call assert_equal(expected, getline(1, '$'))
289
290  bwipe!
291  delmark ab
292  delfunction GetTeXIndent
293endfu
294
295func Test_formatting_keeps_first_line_indent()
296  let lines =<< trim END
297      foo()
298      {
299          int x;         // manually positioned
300                         // more text that will be formatted
301                         // but not reindented
302  END
303  new
304  call setline(1, lines)
305  setlocal sw=4 cindent tw=45 et
306  normal! 4Ggqj
307  let expected =<< trim END
308      foo()
309      {
310          int x;         // manually positioned
311                         // more text that will be
312                         // formatted but not
313                         // reindented
314  END
315  call assert_equal(expected, getline(1, '$'))
316  bwipe!
317endfunc
318
319" Test for indenting with large amount, causes overflow
320func Test_indent_overflow_count()
321  throw 'skipped: TODO: '
322  new
323  setl sw=8
324  call setline(1, "abc")
325  norm! V2147483647>
326  " indents by INT_MAX
327  call assert_equal(2147483647, indent(1))
328  bw!
329endfunc
330
331func Test_indent_overflow_count2()
332  throw 'skipped: Nvim does not support 64-bit number options'
333  new
334  " this only works, when long is 64bits
335  try
336    setl sw=0x180000000
337  catch /^Vim\%((\a\+)\)\=:E487:/
338  throw 'Skipped: value negative on this platform'
339  endtry
340  call setline(1, "\tabc")
341  norm! <<
342  call assert_equal(0, indent(1))
343  close!
344endfunc
345
346" Test that mouse shape is restored to Normal mode after using "gq" when
347" 'indentexpr' executes :normal.
348func Test_mouse_shape_indent_norm_with_gq()
349  CheckFeature mouseshape
350  CheckCanRunGui
351
352  let lines =<< trim END
353    func Indent()
354      exe "normal! \<Ignore>"
355      return 0
356    endfunc
357
358    setlocal indentexpr=Indent()
359  END
360  call writefile(lines, 'Xindentexpr.vim', 'D')
361
362  let lines =<< trim END
363    vim9script
364    var mouse_shapes = []
365
366    setline(1, [repeat('a', 80), repeat('b', 80)])
367
368    feedkeys('ggVG')
369    timer_start(50, (_) => {
370      mouse_shapes += [getmouseshape()]
371      timer_start(50, (_) => {
372        feedkeys('gq')
373        timer_start(50, (_) => {
374          mouse_shapes += [getmouseshape()]
375          timer_start(50, (_) => {
376            writefile(mouse_shapes, 'Xmouseshapes')
377            quit!
378          })
379        })
380      })
381    })
382  END
383  call writefile(lines, 'Xmouseshape.vim', 'D')
384
385  call RunVim([], [], "-g -S Xindentexpr.vim -S Xmouseshape.vim")
386  call WaitForAssert({-> assert_equal(['rightup-arrow', 'arrow'],
387        \ readfile('Xmouseshapes'))}, 300)
388
389  call delete('Xmouseshapes')
390endfunc
391
392" vim: shiftwidth=2 sts=2 expandtab