xref: /vim-8.2.3635/src/testdir/test_options.vim (revision 5f1920ad)
1" Test for options
2
3source check.vim
4
5func Test_whichwrap()
6  set whichwrap=b,s
7  call assert_equal('b,s', &whichwrap)
8
9  set whichwrap+=h,l
10  call assert_equal('b,s,h,l', &whichwrap)
11
12  set whichwrap+=h,l
13  call assert_equal('b,s,h,l', &whichwrap)
14
15  set whichwrap+=h,l
16  call assert_equal('b,s,h,l', &whichwrap)
17
18  set whichwrap=h,h
19  call assert_equal('h', &whichwrap)
20
21  set whichwrap=h,h,h
22  call assert_equal('h', &whichwrap)
23
24  set whichwrap&
25endfunc
26
27func Test_isfname()
28  " This used to cause Vim to access uninitialized memory.
29  set isfname=
30  call assert_equal("~X", expand("~X"))
31  set isfname&
32endfunc
33
34func Test_wildchar()
35  " Empty 'wildchar' used to access invalid memory.
36  call assert_fails('set wildchar=', 'E521:')
37  call assert_fails('set wildchar=abc', 'E521:')
38  set wildchar=<Esc>
39  let a=execute('set wildchar?')
40  call assert_equal("\n  wildchar=<Esc>", a)
41  set wildchar=27
42  let a=execute('set wildchar?')
43  call assert_equal("\n  wildchar=<Esc>", a)
44  set wildchar&
45endfunc
46
47func Test_options()
48  let caught = 'ok'
49  try
50    options
51  catch
52    let caught = v:throwpoint . "\n" . v:exception
53  endtry
54  call assert_equal('ok', caught)
55
56  " Check if the option-window is opened horizontally.
57  wincmd j
58  call assert_notequal('option-window', bufname(''))
59  wincmd k
60  call assert_equal('option-window', bufname(''))
61  " close option-window
62  close
63
64  " Open the option-window vertically.
65  vert options
66  " Check if the option-window is opened vertically.
67  wincmd l
68  call assert_notequal('option-window', bufname(''))
69  wincmd h
70  call assert_equal('option-window', bufname(''))
71  " close option-window
72  close
73
74  " Open the option-window in a new tab.
75  tab options
76  " Check if the option-window is opened in a tab.
77  normal gT
78  call assert_notequal('option-window', bufname(''))
79  normal gt
80  call assert_equal('option-window', bufname(''))
81
82  " close option-window
83  close
84endfunc
85
86func Test_path_keep_commas()
87  " Test that changing 'path' keeps two commas.
88  set path=foo,,bar
89  set path-=bar
90  set path+=bar
91  call assert_equal('foo,,bar', &path)
92
93  set path&
94endfunc
95
96func Test_signcolumn()
97  if has('signs')
98    call assert_equal("auto", &signcolumn)
99    set signcolumn=yes
100    set signcolumn=no
101    call assert_fails('set signcolumn=nope')
102  endif
103endfunc
104
105func Test_filetype_valid()
106  set ft=valid_name
107  call assert_equal("valid_name", &filetype)
108  set ft=valid-name
109  call assert_equal("valid-name", &filetype)
110
111  call assert_fails(":set ft=wrong;name", "E474:")
112  call assert_fails(":set ft=wrong\\\\name", "E474:")
113  call assert_fails(":set ft=wrong\\|name", "E474:")
114  call assert_fails(":set ft=wrong/name", "E474:")
115  call assert_fails(":set ft=wrong\\\nname", "E474:")
116  call assert_equal("valid-name", &filetype)
117
118  exe "set ft=trunc\x00name"
119  call assert_equal("trunc", &filetype)
120endfunc
121
122func Test_syntax_valid()
123  if !has('syntax')
124    return
125  endif
126  set syn=valid_name
127  call assert_equal("valid_name", &syntax)
128  set syn=valid-name
129  call assert_equal("valid-name", &syntax)
130
131  call assert_fails(":set syn=wrong;name", "E474:")
132  call assert_fails(":set syn=wrong\\\\name", "E474:")
133  call assert_fails(":set syn=wrong\\|name", "E474:")
134  call assert_fails(":set syn=wrong/name", "E474:")
135  call assert_fails(":set syn=wrong\\\nname", "E474:")
136  call assert_equal("valid-name", &syntax)
137
138  exe "set syn=trunc\x00name"
139  call assert_equal("trunc", &syntax)
140endfunc
141
142func Test_keymap_valid()
143  if !has('keymap')
144    return
145  endif
146  call assert_fails(":set kmp=valid_name", "E544:")
147  call assert_fails(":set kmp=valid_name", "valid_name")
148  call assert_fails(":set kmp=valid-name", "E544:")
149  call assert_fails(":set kmp=valid-name", "valid-name")
150
151  call assert_fails(":set kmp=wrong;name", "E474:")
152  call assert_fails(":set kmp=wrong\\\\name", "E474:")
153  call assert_fails(":set kmp=wrong\\|name", "E474:")
154  call assert_fails(":set kmp=wrong/name", "E474:")
155  call assert_fails(":set kmp=wrong\\\nname", "E474:")
156
157  call assert_fails(":set kmp=trunc\x00name", "E544:")
158  call assert_fails(":set kmp=trunc\x00name", "trunc")
159endfunc
160
161func Check_dir_option(name)
162  " Check that it's possible to set the option.
163  exe 'set ' . a:name . '=/usr/share/dict/words'
164  call assert_equal('/usr/share/dict/words', eval('&' . a:name))
165  exe 'set ' . a:name . '=/usr/share/dict/words,/and/there'
166  call assert_equal('/usr/share/dict/words,/and/there', eval('&' . a:name))
167  exe 'set ' . a:name . '=/usr/share/dict\ words'
168  call assert_equal('/usr/share/dict words', eval('&' . a:name))
169
170  " Check rejecting weird characters.
171  call assert_fails("set " . a:name . "=/not&there", "E474:")
172  call assert_fails("set " . a:name . "=/not>there", "E474:")
173  call assert_fails("set " . a:name . "=/not.*there", "E474:")
174endfunc
175
176func Test_cinkeys()
177  " This used to cause invalid memory access
178  set cindent cinkeys=0
179  norm a
180  set cindent& cinkeys&
181endfunc
182
183func Test_dictionary()
184  call Check_dir_option('dictionary')
185endfunc
186
187func Test_thesaurus()
188  call Check_dir_option('thesaurus')
189endfun
190
191func Test_complete()
192  " Trailing single backslash used to cause invalid memory access.
193  set complete=s\
194  new
195  call feedkeys("i\<C-N>\<Esc>", 'xt')
196  bwipe!
197  set complete&
198endfun
199
200func Test_set_completion()
201  call feedkeys(":set di\<C-A>\<C-B>\"\<CR>", 'tx')
202  call assert_equal('"set dictionary diff diffexpr diffopt digraph directory display', @:)
203
204  " Expand boolan options. When doing :set no<Tab>
205  " vim displays the options names without "no" but completion uses "no...".
206  call feedkeys(":set nodi\<C-A>\<C-B>\"\<CR>", 'tx')
207  call assert_equal('"set nodiff digraph', @:)
208
209  call feedkeys(":set invdi\<C-A>\<C-B>\"\<CR>", 'tx')
210  call assert_equal('"set invdiff digraph', @:)
211
212  " Expand abbreviation of options.
213  call feedkeys(":set ts\<C-A>\<C-B>\"\<CR>", 'tx')
214  call assert_equal('"set tabstop thesaurus ttyscroll', @:)
215
216  " Expand current value
217  call feedkeys(":set fileencodings=\<C-A>\<C-B>\"\<CR>", 'tx')
218  call assert_equal('"set fileencodings=ucs-bom,utf-8,default,latin1', @:)
219
220  call feedkeys(":set fileencodings:\<C-A>\<C-B>\"\<CR>", 'tx')
221  call assert_equal('"set fileencodings:ucs-bom,utf-8,default,latin1', @:)
222
223  " Expand key codes.
224  call feedkeys(":set <H\<C-A>\<C-B>\"\<CR>", 'tx')
225  call assert_equal('"set <Help> <Home>', @:)
226
227  " Expand terminal options.
228  call feedkeys(":set t_A\<C-A>\<C-B>\"\<CR>", 'tx')
229  call assert_equal('"set t_AB t_AF t_AL', @:)
230
231  " Expand directories.
232  call feedkeys(":set cdpath=./\<C-A>\<C-B>\"\<CR>", 'tx')
233  call assert_match(' ./samples/ ', @:)
234  call assert_notmatch(' ./small.vim ', @:)
235
236  " Expand files and directories.
237  call feedkeys(":set tags=./\<C-A>\<C-B>\"\<CR>", 'tx')
238  call assert_match(' ./samples/.* ./small.vim', @:)
239
240  call feedkeys(":set tags=./\\\\ dif\<C-A>\<C-B>\"\<CR>", 'tx')
241  call assert_equal('"set tags=./\\ diff diffexpr diffopt', @:)
242  set tags&
243endfunc
244
245func Test_set_errors()
246  call assert_fails('set scroll=-1', 'E49:')
247  call assert_fails('set backupcopy=', 'E474:')
248  call assert_fails('set regexpengine=3', 'E474:')
249  call assert_fails('set history=10001', 'E474:')
250  call assert_fails('set numberwidth=21', 'E474:')
251  call assert_fails('set colorcolumn=-a')
252  call assert_fails('set colorcolumn=a')
253  call assert_fails('set colorcolumn=1,')
254  call assert_fails('set cmdheight=-1', 'E487:')
255  call assert_fails('set cmdwinheight=-1', 'E487:')
256  if has('conceal')
257    call assert_fails('set conceallevel=-1', 'E487:')
258    call assert_fails('set conceallevel=4', 'E474:')
259  endif
260  call assert_fails('set helpheight=-1', 'E487:')
261  call assert_fails('set history=-1', 'E487:')
262  call assert_fails('set report=-1', 'E487:')
263  call assert_fails('set shiftwidth=-1', 'E487:')
264  call assert_fails('set sidescroll=-1', 'E487:')
265  call assert_fails('set tabstop=-1', 'E487:')
266  call assert_fails('set textwidth=-1', 'E487:')
267  call assert_fails('set timeoutlen=-1', 'E487:')
268  call assert_fails('set updatecount=-1', 'E487:')
269  call assert_fails('set updatetime=-1', 'E487:')
270  call assert_fails('set winheight=-1', 'E487:')
271  call assert_fails('set tabstop!', 'E488:')
272  call assert_fails('set xxx', 'E518:')
273  call assert_fails('set beautify?', 'E519:')
274  call assert_fails('set undolevels=x', 'E521:')
275  call assert_fails('set tabstop=', 'E521:')
276  call assert_fails('set comments=-', 'E524:')
277  call assert_fails('set comments=a', 'E525:')
278  call assert_fails('set foldmarker=x', 'E536:')
279  call assert_fails('set commentstring=x', 'E537:')
280  call assert_fails('set complete=x', 'E539:')
281  call assert_fails('set statusline=%{', 'E540:')
282  call assert_fails('set statusline=' . repeat("%p", 81), 'E541:')
283  call assert_fails('set statusline=%(', 'E542:')
284  if has('cursorshape')
285    " This invalid value for 'guicursor' used to cause Vim to crash.
286    call assert_fails('set guicursor=i-ci,r-cr:h', 'E545:')
287    call assert_fails('set guicursor=i-ci', 'E545:')
288    call assert_fails('set guicursor=x', 'E545:')
289    call assert_fails('set guicursor=r-cr:horx', 'E548:')
290    call assert_fails('set guicursor=r-cr:hor0', 'E549:')
291  endif
292  call assert_fails('set backupext=~ patchmode=~', 'E589:')
293  call assert_fails('set winminheight=10 winheight=9', 'E591:')
294  call assert_fails('set winminwidth=10 winwidth=9', 'E592:')
295  call assert_fails("set showbreak=\x01", 'E595:')
296  call assert_fails('set t_foo=', 'E846:')
297endfunc
298
299func CheckWasSet(name)
300  let verb_cm = execute('verbose set ' .. a:name .. '?')
301  call assert_match('Last set from.*test_options.vim', verb_cm)
302endfunc
303func CheckWasNotSet(name)
304  let verb_cm = execute('verbose set ' .. a:name .. '?')
305  call assert_notmatch('Last set from', verb_cm)
306endfunc
307
308" Must be executed before other tests that set 'term'.
309func Test_000_term_option_verbose()
310  CheckNotGui
311
312  call CheckWasNotSet('t_cm')
313
314  let term_save = &term
315  set term=ansi
316  call CheckWasSet('t_cm')
317  let &term = term_save
318endfunc
319
320func Test_copy_context()
321  setlocal list
322  call CheckWasSet('list')
323  split
324  call CheckWasSet('list')
325  quit
326  setlocal nolist
327
328  set ai
329  call CheckWasSet('ai')
330  set filetype=perl
331  call CheckWasSet('filetype')
332  set fo=tcroq
333  call CheckWasSet('fo')
334
335  split Xsomebuf
336  call CheckWasSet('ai')
337  call CheckWasNotSet('filetype')
338  call CheckWasSet('fo')
339endfunc
340
341func Test_set_ttytype()
342  CheckUnix
343  CheckNotGui
344
345  " Setting 'ttytype' used to cause a double-free when exiting vim and
346  " when vim is compiled with -DEXITFREE.
347  set ttytype=ansi
348  call assert_equal('ansi', &ttytype)
349  call assert_equal(&ttytype, &term)
350  set ttytype=xterm
351  call assert_equal('xterm', &ttytype)
352  call assert_equal(&ttytype, &term)
353  " "set ttytype=" gives E522 instead of E529
354  " in travis on some builds. Why?  Catch both for now
355  try
356    set ttytype=
357    call assert_report('set ttytype= did not fail')
358  catch /E529\|E522/
359  endtry
360
361  " Some systems accept any terminal name and return dumb settings,
362  " check for failure of finding the entry and for missing 'cm' entry.
363  try
364    set ttytype=xxx
365    call assert_report('set ttytype=xxx did not fail')
366  catch /E522\|E437/
367  endtry
368
369  set ttytype&
370  call assert_equal(&ttytype, &term)
371endfunc
372
373func Test_set_all()
374  set tw=75
375  set iskeyword=a-z,A-Z
376  set nosplitbelow
377  let out = execute('set all')
378  call assert_match('textwidth=75', out)
379  call assert_match('iskeyword=a-z,A-Z', out)
380  call assert_match('nosplitbelow', out)
381  set tw& iskeyword& splitbelow&
382endfunc
383
384func Test_set_values()
385  if filereadable('opt_test.vim')
386    source opt_test.vim
387  else
388    throw 'Skipped: opt_test.vim does not exist'
389  endif
390endfunc
391
392func ResetIndentexpr()
393  set indentexpr=
394endfunc
395
396func Test_set_indentexpr()
397  " this was causing usage of freed memory
398  set indentexpr=ResetIndentexpr()
399  new
400  call feedkeys("i\<c-f>", 'x')
401  call assert_equal('', &indentexpr)
402  bwipe!
403endfunc
404
405func Test_backupskip()
406  " Option 'backupskip' may contain several comma-separated path
407  " specifications if one or more of the environment variables TMPDIR, TMP,
408  " or TEMP is defined.  To simplify testing, convert the string value into a
409  " list.
410  let bsklist = split(&bsk, ',')
411
412  if has("mac")
413    let found = (index(bsklist, '/private/tmp/*') >= 0)
414    call assert_true(found, '/private/tmp not in option bsk: ' . &bsk)
415  elseif has("unix")
416    let found = (index(bsklist, '/tmp/*') >= 0)
417    call assert_true(found, '/tmp not in option bsk: ' . &bsk)
418  endif
419
420  " If our test platform is Windows, the path(s) in option bsk will use
421  " backslash for the path separator and the components could be in short
422  " (8.3) format.  As such, we need to replace the backslashes with forward
423  " slashes and convert the path components to long format.  The expand()
424  " function will do this but it cannot handle comma-separated paths.  This is
425  " why bsk was converted from a string into a list of strings above.
426  "
427  " One final complication is that the wildcard "/*" is at the end of each
428  " path and so expand() might return a list of matching files.  To prevent
429  " this, we need to remove the wildcard before calling expand() and then
430  " append it afterwards.
431  if has('win32')
432    let item_nbr = 0
433    while item_nbr < len(bsklist)
434      let path_spec = bsklist[item_nbr]
435      let path_spec = strcharpart(path_spec, 0, strlen(path_spec)-2)
436      let path_spec = substitute(expand(path_spec), '\\', '/', 'g')
437      let bsklist[item_nbr] = path_spec . '/*'
438      let item_nbr += 1
439    endwhile
440  endif
441
442  " Option bsk will also include these environment variables if defined.
443  " If they're defined, verify they appear in the option value.
444  for var in  ['$TMPDIR', '$TMP', '$TEMP']
445    if exists(var)
446      let varvalue = substitute(expand(var), '\\', '/', 'g')
447      let varvalue = substitute(varvalue, '/$', '', '')
448      let varvalue .= '/*'
449      let found = (index(bsklist, varvalue) >= 0)
450      call assert_true(found, var . ' (' . varvalue . ') not in option bsk: ' . &bsk)
451    endif
452  endfor
453
454  " Duplicates should be filtered out (option has P_NODUP)
455  let backupskip = &backupskip
456  set backupskip=
457  set backupskip+=/test/dir
458  set backupskip+=/other/dir
459  set backupskip+=/test/dir
460  call assert_equal('/test/dir,/other/dir', &backupskip)
461  let &backupskip = backupskip
462endfunc
463
464func Test_copy_winopt()
465  set hidden
466
467  " Test copy option from current buffer in window
468  split
469  enew
470  setlocal numberwidth=5
471  wincmd w
472  call assert_equal(4,&numberwidth)
473  bnext
474  call assert_equal(5,&numberwidth)
475  bw!
476  call assert_equal(4,&numberwidth)
477
478  " Test copy value from window that used to be display the buffer
479  split
480  enew
481  setlocal numberwidth=6
482  bnext
483  wincmd w
484  call assert_equal(4,&numberwidth)
485  bnext
486  call assert_equal(6,&numberwidth)
487  bw!
488
489  " Test that if buffer is current, don't use the stale cached value
490  " from the last time the buffer was displayed.
491  split
492  enew
493  setlocal numberwidth=7
494  bnext
495  bnext
496  setlocal numberwidth=8
497  wincmd w
498  call assert_equal(4,&numberwidth)
499  bnext
500  call assert_equal(8,&numberwidth)
501  bw!
502
503  " Test value is not copied if window already has seen the buffer
504  enew
505  split
506  setlocal numberwidth=9
507  bnext
508  setlocal numberwidth=10
509  wincmd w
510  call assert_equal(4,&numberwidth)
511  bnext
512  call assert_equal(4,&numberwidth)
513  bw!
514
515  set hidden&
516endfunc
517
518func Test_shortmess_F()
519  new
520  call assert_match('\[No Name\]', execute('file'))
521  set shortmess+=F
522  call assert_match('\[No Name\]', execute('file'))
523  call assert_match('^\s*$', execute('file foo'))
524  call assert_match('foo', execute('file'))
525  set shortmess-=F
526  call assert_match('bar', execute('file bar'))
527  call assert_match('bar', execute('file'))
528  set shortmess&
529  bwipe
530endfunc
531
532func Test_shortmess_F2()
533  e file1
534  e file2
535  call assert_match('file1', execute('bn', ''))
536  call assert_match('file2', execute('bn', ''))
537  set shortmess+=F
538  call assert_true(empty(execute('bn', '')))
539  call assert_false(test_getvalue('need_fileinfo'))
540  call assert_true(empty(execute('bn', '')))
541  call assert_false('need_fileinfo'->test_getvalue())
542  set hidden
543  call assert_true(empty(execute('bn', '')))
544  call assert_false(test_getvalue('need_fileinfo'))
545  call assert_true(empty(execute('bn', '')))
546  call assert_false(test_getvalue('need_fileinfo'))
547  set nohidden
548  call assert_true(empty(execute('bn', '')))
549  call assert_false(test_getvalue('need_fileinfo'))
550  call assert_true(empty(execute('bn', '')))
551  call assert_false(test_getvalue('need_fileinfo'))
552  set shortmess&
553  call assert_match('file1', execute('bn', ''))
554  call assert_match('file2', execute('bn', ''))
555  bwipe
556  bwipe
557endfunc
558
559func Test_local_scrolloff()
560  set so=5
561  set siso=7
562  split
563  call assert_equal(5, &so)
564  setlocal so=3
565  call assert_equal(3, &so)
566  wincmd w
567  call assert_equal(5, &so)
568  wincmd w
569  setlocal so<
570  call assert_equal(5, &so)
571  setlocal so=0
572  call assert_equal(0, &so)
573  setlocal so=-1
574  call assert_equal(5, &so)
575
576  call assert_equal(7, &siso)
577  setlocal siso=3
578  call assert_equal(3, &siso)
579  wincmd w
580  call assert_equal(7, &siso)
581  wincmd w
582  setlocal siso<
583  call assert_equal(7, &siso)
584  setlocal siso=0
585  call assert_equal(0, &siso)
586  setlocal siso=-1
587  call assert_equal(7, &siso)
588
589  close
590  set so&
591  set siso&
592endfunc
593
594func Test_writedelay()
595  if !has('reltime')
596    return
597  endif
598  new
599  call setline(1, 'empty')
600  redraw
601  set writedelay=10
602  let start = reltime()
603  call setline(1, repeat('x', 70))
604  redraw
605  let elapsed = reltimefloat(reltime(start))
606  set writedelay=0
607  " With 'writedelay' set should take at least 30 * 10 msec
608  call assert_inrange(30 * 0.01, 999.0, elapsed)
609
610  bwipe!
611endfunc
612
613func Test_visualbell()
614  set belloff=
615  set visualbell
616  call assert_beeps('normal 0h')
617  set novisualbell
618  set belloff=all
619endfunc
620