xref: /vim-8.2.3635/src/testdir/test_options.vim (revision 98be7fec)
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_command()
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  call feedkeys(":setlocal di\<C-A>\<C-B>\"\<CR>", 'tx')
205  call assert_equal('"setlocal dictionary diff diffexpr diffopt digraph directory display', @:)
206
207  call feedkeys(":setglobal di\<C-A>\<C-B>\"\<CR>", 'tx')
208  call assert_equal('"setglobal dictionary diff diffexpr diffopt digraph directory display', @:)
209
210  " Expand boolan options. When doing :set no<Tab>
211  " vim displays the options names without "no" but completion uses "no...".
212  call feedkeys(":set nodi\<C-A>\<C-B>\"\<CR>", 'tx')
213  call assert_equal('"set nodiff digraph', @:)
214
215  call feedkeys(":set invdi\<C-A>\<C-B>\"\<CR>", 'tx')
216  call assert_equal('"set invdiff digraph', @:)
217
218  " Expand abbreviation of options.
219  call feedkeys(":set ts\<C-A>\<C-B>\"\<CR>", 'tx')
220  call assert_equal('"set tabstop thesaurus ttyscroll', @:)
221
222  " Expand current value
223  call feedkeys(":set fileencodings=\<C-A>\<C-B>\"\<CR>", 'tx')
224  call assert_equal('"set fileencodings=ucs-bom,utf-8,default,latin1', @:)
225
226  call feedkeys(":set fileencodings:\<C-A>\<C-B>\"\<CR>", 'tx')
227  call assert_equal('"set fileencodings:ucs-bom,utf-8,default,latin1', @:)
228
229  " Expand key codes.
230  call feedkeys(":set <H\<C-A>\<C-B>\"\<CR>", 'tx')
231  call assert_equal('"set <Help> <Home>', @:)
232
233  " Expand terminal options.
234  call feedkeys(":set t_A\<C-A>\<C-B>\"\<CR>", 'tx')
235  call assert_equal('"set t_AB t_AF t_AL', @:)
236
237  " Expand directories.
238  call feedkeys(":set cdpath=./\<C-A>\<C-B>\"\<CR>", 'tx')
239  call assert_match(' ./samples/ ', @:)
240  call assert_notmatch(' ./small.vim ', @:)
241
242  " Expand files and directories.
243  call feedkeys(":set tags=./\<C-A>\<C-B>\"\<CR>", 'tx')
244  call assert_match(' ./samples/.* ./small.vim', @:)
245
246  call feedkeys(":set tags=./\\\\ dif\<C-A>\<C-B>\"\<CR>", 'tx')
247  call assert_equal('"set tags=./\\ diff diffexpr diffopt', @:)
248
249  set tags&
250endfunc
251
252func Test_set_errors()
253  call assert_fails('set scroll=-1', 'E49:')
254  call assert_fails('set backupcopy=', 'E474:')
255  call assert_fails('set regexpengine=3', 'E474:')
256  call assert_fails('set history=10001', 'E474:')
257  call assert_fails('set numberwidth=21', 'E474:')
258  call assert_fails('set colorcolumn=-a')
259  call assert_fails('set colorcolumn=a')
260  call assert_fails('set colorcolumn=1,')
261  call assert_fails('set cmdheight=-1', 'E487:')
262  call assert_fails('set cmdwinheight=-1', 'E487:')
263  if has('conceal')
264    call assert_fails('set conceallevel=-1', 'E487:')
265    call assert_fails('set conceallevel=4', 'E474:')
266  endif
267  call assert_fails('set helpheight=-1', 'E487:')
268  call assert_fails('set history=-1', 'E487:')
269  call assert_fails('set report=-1', 'E487:')
270  call assert_fails('set shiftwidth=-1', 'E487:')
271  call assert_fails('set sidescroll=-1', 'E487:')
272  call assert_fails('set tabstop=-1', 'E487:')
273  call assert_fails('set textwidth=-1', 'E487:')
274  call assert_fails('set timeoutlen=-1', 'E487:')
275  call assert_fails('set updatecount=-1', 'E487:')
276  call assert_fails('set updatetime=-1', 'E487:')
277  call assert_fails('set winheight=-1', 'E487:')
278  call assert_fails('set tabstop!', 'E488:')
279  call assert_fails('set xxx', 'E518:')
280  call assert_fails('set beautify?', 'E519:')
281  call assert_fails('set undolevels=x', 'E521:')
282  call assert_fails('set tabstop=', 'E521:')
283  call assert_fails('set comments=-', 'E524:')
284  call assert_fails('set comments=a', 'E525:')
285  call assert_fails('set foldmarker=x', 'E536:')
286  call assert_fails('set commentstring=x', 'E537:')
287  call assert_fails('set complete=x', 'E539:')
288  call assert_fails('set statusline=%{', 'E540:')
289  call assert_fails('set statusline=' . repeat("%p", 81), 'E541:')
290  call assert_fails('set statusline=%(', 'E542:')
291  if has('cursorshape')
292    " This invalid value for 'guicursor' used to cause Vim to crash.
293    call assert_fails('set guicursor=i-ci,r-cr:h', 'E545:')
294    call assert_fails('set guicursor=i-ci', 'E545:')
295    call assert_fails('set guicursor=x', 'E545:')
296    call assert_fails('set guicursor=r-cr:horx', 'E548:')
297    call assert_fails('set guicursor=r-cr:hor0', 'E549:')
298  endif
299  call assert_fails('set backupext=~ patchmode=~', 'E589:')
300  call assert_fails('set winminheight=10 winheight=9', 'E591:')
301  call assert_fails('set winminwidth=10 winwidth=9', 'E592:')
302  call assert_fails("set showbreak=\x01", 'E595:')
303  call assert_fails('set t_foo=', 'E846:')
304endfunc
305
306func CheckWasSet(name)
307  let verb_cm = execute('verbose set ' .. a:name .. '?')
308  call assert_match('Last set from.*test_options.vim', verb_cm)
309endfunc
310func CheckWasNotSet(name)
311  let verb_cm = execute('verbose set ' .. a:name .. '?')
312  call assert_notmatch('Last set from', verb_cm)
313endfunc
314
315" Must be executed before other tests that set 'term'.
316func Test_000_term_option_verbose()
317  CheckNotGui
318
319  call CheckWasNotSet('t_cm')
320
321  let term_save = &term
322  set term=ansi
323  call CheckWasSet('t_cm')
324  let &term = term_save
325endfunc
326
327func Test_copy_context()
328  setlocal list
329  call CheckWasSet('list')
330  split
331  call CheckWasSet('list')
332  quit
333  setlocal nolist
334
335  set ai
336  call CheckWasSet('ai')
337  set filetype=perl
338  call CheckWasSet('filetype')
339  set fo=tcroq
340  call CheckWasSet('fo')
341
342  split Xsomebuf
343  call CheckWasSet('ai')
344  call CheckWasNotSet('filetype')
345  call CheckWasSet('fo')
346endfunc
347
348func Test_set_ttytype()
349  CheckUnix
350  CheckNotGui
351
352  " Setting 'ttytype' used to cause a double-free when exiting vim and
353  " when vim is compiled with -DEXITFREE.
354  set ttytype=ansi
355  call assert_equal('ansi', &ttytype)
356  call assert_equal(&ttytype, &term)
357  set ttytype=xterm
358  call assert_equal('xterm', &ttytype)
359  call assert_equal(&ttytype, &term)
360  " "set ttytype=" gives E522 instead of E529
361  " in travis on some builds. Why?  Catch both for now
362  try
363    set ttytype=
364    call assert_report('set ttytype= did not fail')
365  catch /E529\|E522/
366  endtry
367
368  " Some systems accept any terminal name and return dumb settings,
369  " check for failure of finding the entry and for missing 'cm' entry.
370  try
371    set ttytype=xxx
372    call assert_report('set ttytype=xxx did not fail')
373  catch /E522\|E437/
374  endtry
375
376  set ttytype&
377  call assert_equal(&ttytype, &term)
378endfunc
379
380func Test_set_all()
381  set tw=75
382  set iskeyword=a-z,A-Z
383  set nosplitbelow
384  let out = execute('set all')
385  call assert_match('textwidth=75', out)
386  call assert_match('iskeyword=a-z,A-Z', out)
387  call assert_match('nosplitbelow', out)
388  set tw& iskeyword& splitbelow&
389endfunc
390
391func Test_set_one_column()
392  let out_mult = execute('set all')->split("\n")
393  let out_one = execute('set! all')->split("\n")
394  " one column should be two to four times as many lines
395  call assert_inrange(len(out_mult) * 2, len(out_mult) * 4, len(out_one))
396endfunc
397
398func Test_set_values()
399  if filereadable('opt_test.vim')
400    source opt_test.vim
401  else
402    throw 'Skipped: opt_test.vim does not exist'
403  endif
404endfunc
405
406func Test_renderoptions()
407  " Only do this for Windows Vista and later, fails on Windows XP and earlier.
408  " Doesn't hurt to do this on a non-Windows system.
409  if windowsversion() !~ '^[345]\.'
410    set renderoptions=type:directx
411    set rop=type:directx
412  endif
413endfunc
414
415func ResetIndentexpr()
416  set indentexpr=
417endfunc
418
419func Test_set_indentexpr()
420  " this was causing usage of freed memory
421  set indentexpr=ResetIndentexpr()
422  new
423  call feedkeys("i\<c-f>", 'x')
424  call assert_equal('', &indentexpr)
425  bwipe!
426endfunc
427
428func Test_backupskip()
429  " Option 'backupskip' may contain several comma-separated path
430  " specifications if one or more of the environment variables TMPDIR, TMP,
431  " or TEMP is defined.  To simplify testing, convert the string value into a
432  " list.
433  let bsklist = split(&bsk, ',')
434
435  if has("mac")
436    let found = (index(bsklist, '/private/tmp/*') >= 0)
437    call assert_true(found, '/private/tmp not in option bsk: ' . &bsk)
438  elseif has("unix")
439    let found = (index(bsklist, '/tmp/*') >= 0)
440    call assert_true(found, '/tmp not in option bsk: ' . &bsk)
441  endif
442
443  " If our test platform is Windows, the path(s) in option bsk will use
444  " backslash for the path separator and the components could be in short
445  " (8.3) format.  As such, we need to replace the backslashes with forward
446  " slashes and convert the path components to long format.  The expand()
447  " function will do this but it cannot handle comma-separated paths.  This is
448  " why bsk was converted from a string into a list of strings above.
449  "
450  " One final complication is that the wildcard "/*" is at the end of each
451  " path and so expand() might return a list of matching files.  To prevent
452  " this, we need to remove the wildcard before calling expand() and then
453  " append it afterwards.
454  if has('win32')
455    let item_nbr = 0
456    while item_nbr < len(bsklist)
457      let path_spec = bsklist[item_nbr]
458      let path_spec = strcharpart(path_spec, 0, strlen(path_spec)-2)
459      let path_spec = substitute(expand(path_spec), '\\', '/', 'g')
460      let bsklist[item_nbr] = path_spec . '/*'
461      let item_nbr += 1
462    endwhile
463  endif
464
465  " Option bsk will also include these environment variables if defined.
466  " If they're defined, verify they appear in the option value.
467  for var in  ['$TMPDIR', '$TMP', '$TEMP']
468    if exists(var)
469      let varvalue = substitute(expand(var), '\\', '/', 'g')
470      let varvalue = substitute(varvalue, '/$', '', '')
471      let varvalue .= '/*'
472      let found = (index(bsklist, varvalue) >= 0)
473      call assert_true(found, var . ' (' . varvalue . ') not in option bsk: ' . &bsk)
474    endif
475  endfor
476
477  " Duplicates should be filtered out (option has P_NODUP)
478  let backupskip = &backupskip
479  set backupskip=
480  set backupskip+=/test/dir
481  set backupskip+=/other/dir
482  set backupskip+=/test/dir
483  call assert_equal('/test/dir,/other/dir', &backupskip)
484  let &backupskip = backupskip
485endfunc
486
487func Test_copy_winopt()
488  set hidden
489
490  " Test copy option from current buffer in window
491  split
492  enew
493  setlocal numberwidth=5
494  wincmd w
495  call assert_equal(4,&numberwidth)
496  bnext
497  call assert_equal(5,&numberwidth)
498  bw!
499  call assert_equal(4,&numberwidth)
500
501  " Test copy value from window that used to be display the buffer
502  split
503  enew
504  setlocal numberwidth=6
505  bnext
506  wincmd w
507  call assert_equal(4,&numberwidth)
508  bnext
509  call assert_equal(6,&numberwidth)
510  bw!
511
512  " Test that if buffer is current, don't use the stale cached value
513  " from the last time the buffer was displayed.
514  split
515  enew
516  setlocal numberwidth=7
517  bnext
518  bnext
519  setlocal numberwidth=8
520  wincmd w
521  call assert_equal(4,&numberwidth)
522  bnext
523  call assert_equal(8,&numberwidth)
524  bw!
525
526  " Test value is not copied if window already has seen the buffer
527  enew
528  split
529  setlocal numberwidth=9
530  bnext
531  setlocal numberwidth=10
532  wincmd w
533  call assert_equal(4,&numberwidth)
534  bnext
535  call assert_equal(4,&numberwidth)
536  bw!
537
538  set hidden&
539endfunc
540
541func Test_shortmess_F()
542  new
543  call assert_match('\[No Name\]', execute('file'))
544  set shortmess+=F
545  call assert_match('\[No Name\]', execute('file'))
546  call assert_match('^\s*$', execute('file foo'))
547  call assert_match('foo', execute('file'))
548  set shortmess-=F
549  call assert_match('bar', execute('file bar'))
550  call assert_match('bar', execute('file'))
551  set shortmess&
552  bwipe
553endfunc
554
555func Test_shortmess_F2()
556  e file1
557  e file2
558  call assert_match('file1', execute('bn', ''))
559  call assert_match('file2', execute('bn', ''))
560  set shortmess+=F
561  call assert_true(empty(execute('bn', '')))
562  call assert_false(test_getvalue('need_fileinfo'))
563  call assert_true(empty(execute('bn', '')))
564  call assert_false('need_fileinfo'->test_getvalue())
565  set hidden
566  call assert_true(empty(execute('bn', '')))
567  call assert_false(test_getvalue('need_fileinfo'))
568  call assert_true(empty(execute('bn', '')))
569  call assert_false(test_getvalue('need_fileinfo'))
570  set nohidden
571  call assert_true(empty(execute('bn', '')))
572  call assert_false(test_getvalue('need_fileinfo'))
573  call assert_true(empty(execute('bn', '')))
574  call assert_false(test_getvalue('need_fileinfo'))
575  set shortmess&
576  call assert_match('file1', execute('bn', ''))
577  call assert_match('file2', execute('bn', ''))
578  bwipe
579  bwipe
580endfunc
581
582func Test_local_scrolloff()
583  set so=5
584  set siso=7
585  split
586  call assert_equal(5, &so)
587  setlocal so=3
588  call assert_equal(3, &so)
589  wincmd w
590  call assert_equal(5, &so)
591  wincmd w
592  setlocal so<
593  call assert_equal(5, &so)
594  setlocal so=0
595  call assert_equal(0, &so)
596  setlocal so=-1
597  call assert_equal(5, &so)
598
599  call assert_equal(7, &siso)
600  setlocal siso=3
601  call assert_equal(3, &siso)
602  wincmd w
603  call assert_equal(7, &siso)
604  wincmd w
605  setlocal siso<
606  call assert_equal(7, &siso)
607  setlocal siso=0
608  call assert_equal(0, &siso)
609  setlocal siso=-1
610  call assert_equal(7, &siso)
611
612  close
613  set so&
614  set siso&
615endfunc
616
617func Test_writedelay()
618  CheckFunction reltimefloat
619
620  new
621  call setline(1, 'empty')
622  redraw
623  set writedelay=10
624  let start = reltime()
625  call setline(1, repeat('x', 70))
626  redraw
627  let elapsed = reltimefloat(reltime(start))
628  set writedelay=0
629  " With 'writedelay' set should take at least 30 * 10 msec
630  call assert_inrange(30 * 0.01, 999.0, elapsed)
631
632  bwipe!
633endfunc
634
635func Test_visualbell()
636  set belloff=
637  set visualbell
638  call assert_beeps('normal 0h')
639  set novisualbell
640  set belloff=all
641endfunc
642
643" Test for the 'write' option
644func Test_write()
645  new
646  call setline(1, ['L1'])
647  set nowrite
648  call assert_fails('write Xfile', 'E142:')
649  set write
650  close!
651endfunc
652
653" Test for 'buftype' option
654func Test_buftype()
655  new
656  call setline(1, ['L1'])
657  set buftype=nowrite
658  call assert_fails('write', 'E382:')
659
660  for val in ['', 'nofile', 'nowrite', 'acwrite', 'quickfix', 'help', 'terminal', 'prompt', 'popup']
661    exe 'set buftype=' .. val
662    call writefile(['something'], 'XBuftype')
663    call assert_fails('write XBuftype', 'E13:', 'with buftype=' .. val)
664  endfor
665
666  call delete('XBuftype')
667  bwipe!
668endfunc
669
670" Test for the 'shellquote' option
671func Test_shellquote()
672  CheckUnix
673  set shellquote=#
674  set verbose=20
675  redir => v
676  silent! !echo Hello
677  redir END
678  set verbose&
679  set shellquote&
680  call assert_match(': "#echo Hello#"', v)
681endfunc
682
683" Test for the 'rightleftcmd' option
684func Test_rightleftcmd()
685  CheckFeature rightleft
686  set rightleft
687  set rightleftcmd
688
689  let g:l = []
690  func AddPos()
691    call add(g:l, screencol())
692    return ''
693  endfunc
694  cmap <expr> <F2> AddPos()
695
696  call feedkeys("/\<F2>abc\<Left>\<F2>\<Right>\<Right>\<F2>" ..
697        \ "\<Left>\<F2>\<Esc>", 'xt')
698  call assert_equal([&co - 1, &co - 4, &co - 2, &co - 3], g:l)
699
700  cunmap <F2>
701  unlet g:l
702  set rightleftcmd&
703  set rightleft&
704endfunc
705
706" vim: shiftwidth=2 sts=2 expandtab
707