]> granicus.if.org Git - vim/blob - src/testdir/test_options.vim
af84b428bd2ee7c3d77946b2d271e8701307a5c4
[vim] / src / testdir / test_options.vim
1 " Test for options
2
3 source shared.vim
4 source check.vim
5 source view_util.vim
6
7 func Test_whichwrap()
8   set whichwrap=b,s
9   call assert_equal('b,s', &whichwrap)
10
11   set whichwrap+=h,l
12   call assert_equal('b,s,h,l', &whichwrap)
13
14   set whichwrap+=h,l
15   call assert_equal('b,s,h,l', &whichwrap)
16
17   set whichwrap+=h,l
18   call assert_equal('b,s,h,l', &whichwrap)
19
20   set whichwrap=h,h
21   call assert_equal('h', &whichwrap)
22
23   set whichwrap=h,h,h
24   call assert_equal('h', &whichwrap)
25
26   " For compatibility with Vim 3.0 and before, number values are also
27   " supported for 'whichwrap'
28   set whichwrap=1
29   call assert_equal('b', &whichwrap)
30   set whichwrap=2
31   call assert_equal('s', &whichwrap)
32   set whichwrap=4
33   call assert_equal('h,l', &whichwrap)
34   set whichwrap=8
35   call assert_equal('<,>', &whichwrap)
36   set whichwrap=16
37   call assert_equal('[,]', &whichwrap)
38   set whichwrap=31
39   call assert_equal('b,s,h,l,<,>,[,]', &whichwrap)
40
41   set whichwrap&
42 endfunc
43
44 func Test_isfname()
45   " This used to cause Vim to access uninitialized memory.
46   set isfname=
47   call assert_equal("~X", expand("~X"))
48   set isfname&
49 endfunc
50
51 " Test for getting the value of 'pastetoggle'
52 func Test_pastetoggle()
53   " character with K_SPECIAL byte
54   let &pastetoggle = '…'
55   call assert_equal('…', &pastetoggle)
56   call assert_equal("\n  pastetoggle=…", execute('set pastetoggle?'))
57
58   " modified character with K_SPECIAL byte
59   let &pastetoggle = '<M-…>'
60   call assert_equal('<M-…>', &pastetoggle)
61   call assert_equal("\n  pastetoggle=<M-…>", execute('set pastetoggle?'))
62
63   " illegal bytes
64   let str = ":\x7f:\x80:\x90:\xd0:"
65   let &pastetoggle = str
66   call assert_equal(str, &pastetoggle)
67   call assert_equal("\n  pastetoggle=" .. strtrans(str), execute('set pastetoggle?'))
68
69   unlet str
70   set pastetoggle&
71 endfunc
72
73 func Test_wildchar()
74   " Empty 'wildchar' used to access invalid memory.
75   call assert_fails('set wildchar=', 'E521:')
76   call assert_fails('set wildchar=abc', 'E521:')
77   set wildchar=<Esc>
78   let a=execute('set wildchar?')
79   call assert_equal("\n  wildchar=<Esc>", a)
80   set wildchar=27
81   let a=execute('set wildchar?')
82   call assert_equal("\n  wildchar=<Esc>", a)
83   set wildchar&
84 endfunc
85
86 func Test_wildoptions()
87   set wildoptions=
88   set wildoptions+=tagfile
89   set wildoptions+=tagfile
90   call assert_equal('tagfile', &wildoptions)
91 endfunc
92
93 func Test_options_command()
94   let caught = 'ok'
95   try
96     options
97   catch
98     let caught = v:throwpoint . "\n" . v:exception
99   endtry
100   call assert_equal('ok', caught)
101
102   " Check if the option-window is opened horizontally.
103   wincmd j
104   call assert_notequal('option-window', bufname(''))
105   wincmd k
106   call assert_equal('option-window', bufname(''))
107   " close option-window
108   close
109
110   " Open the option-window vertically.
111   vert options
112   " Check if the option-window is opened vertically.
113   wincmd l
114   call assert_notequal('option-window', bufname(''))
115   wincmd h
116   call assert_equal('option-window', bufname(''))
117   " close option-window
118   close
119
120   " Open the option-window at the top.
121   set splitbelow
122   topleft options
123   call assert_equal(1, winnr())
124   close
125
126   " Open the option-window at the bottom.
127   set nosplitbelow
128   botright options
129   call assert_equal(winnr('$'), winnr())
130   close
131   set splitbelow&
132
133   " Open the option-window in a new tab.
134   tab options
135   " Check if the option-window is opened in a tab.
136   normal gT
137   call assert_notequal('option-window', bufname(''))
138   normal gt
139   call assert_equal('option-window', bufname(''))
140   " close option-window
141   close
142
143   " Open the options window browse
144   if has('browse')
145     browse set
146     call assert_equal('option-window', bufname(''))
147     close
148   endif
149 endfunc
150
151 func Test_path_keep_commas()
152   " Test that changing 'path' keeps two commas.
153   set path=foo,,bar
154   set path-=bar
155   set path+=bar
156   call assert_equal('foo,,bar', &path)
157
158   set path&
159 endfunc
160
161 func Test_path_too_long()
162   exe 'set path=' .. repeat('x', 10000)
163   call assert_fails('find x', 'E854:')
164   set path&
165 endfunc
166
167 func Test_signcolumn()
168   CheckFeature signs
169   call assert_equal("auto", &signcolumn)
170   set signcolumn=yes
171   set signcolumn=no
172   call assert_fails('set signcolumn=nope')
173 endfunc
174
175 func Test_filetype_valid()
176   set ft=valid_name
177   call assert_equal("valid_name", &filetype)
178   set ft=valid-name
179   call assert_equal("valid-name", &filetype)
180
181   call assert_fails(":set ft=wrong;name", "E474:")
182   call assert_fails(":set ft=wrong\\\\name", "E474:")
183   call assert_fails(":set ft=wrong\\|name", "E474:")
184   call assert_fails(":set ft=wrong/name", "E474:")
185   call assert_fails(":set ft=wrong\\\nname", "E474:")
186   call assert_equal("valid-name", &filetype)
187
188   exe "set ft=trunc\x00name"
189   call assert_equal("trunc", &filetype)
190 endfunc
191
192 func Test_syntax_valid()
193   CheckFeature syntax
194   set syn=valid_name
195   call assert_equal("valid_name", &syntax)
196   set syn=valid-name
197   call assert_equal("valid-name", &syntax)
198
199   call assert_fails(":set syn=wrong;name", "E474:")
200   call assert_fails(":set syn=wrong\\\\name", "E474:")
201   call assert_fails(":set syn=wrong\\|name", "E474:")
202   call assert_fails(":set syn=wrong/name", "E474:")
203   call assert_fails(":set syn=wrong\\\nname", "E474:")
204   call assert_equal("valid-name", &syntax)
205
206   exe "set syn=trunc\x00name"
207   call assert_equal("trunc", &syntax)
208 endfunc
209
210 func Test_keymap_valid()
211   CheckFeature keymap
212   call assert_fails(":set kmp=valid_name", "E544:")
213   call assert_fails(":set kmp=valid_name", "valid_name")
214   call assert_fails(":set kmp=valid-name", "E544:")
215   call assert_fails(":set kmp=valid-name", "valid-name")
216
217   call assert_fails(":set kmp=wrong;name", "E474:")
218   call assert_fails(":set kmp=wrong\\\\name", "E474:")
219   call assert_fails(":set kmp=wrong\\|name", "E474:")
220   call assert_fails(":set kmp=wrong/name", "E474:")
221   call assert_fails(":set kmp=wrong\\\nname", "E474:")
222
223   call assert_fails(":set kmp=trunc\x00name", "E544:")
224   call assert_fails(":set kmp=trunc\x00name", "trunc")
225 endfunc
226
227 func Check_dir_option(name)
228   " Check that it's possible to set the option.
229   exe 'set ' . a:name . '=/usr/share/dict/words'
230   call assert_equal('/usr/share/dict/words', eval('&' . a:name))
231   exe 'set ' . a:name . '=/usr/share/dict/words,/and/there'
232   call assert_equal('/usr/share/dict/words,/and/there', eval('&' . a:name))
233   exe 'set ' . a:name . '=/usr/share/dict\ words'
234   call assert_equal('/usr/share/dict words', eval('&' . a:name))
235
236   " Check rejecting weird characters.
237   call assert_fails("set " . a:name . "=/not&there", "E474:")
238   call assert_fails("set " . a:name . "=/not>there", "E474:")
239   call assert_fails("set " . a:name . "=/not.*there", "E474:")
240 endfunc
241
242 func Test_cinkeys()
243   " This used to cause invalid memory access
244   set cindent cinkeys=0
245   norm a
246   set cindent& cinkeys&
247 endfunc
248
249 func Test_dictionary()
250   call Check_dir_option('dictionary')
251 endfunc
252
253 func Test_thesaurus()
254   call Check_dir_option('thesaurus')
255 endfun
256
257 func Test_complete()
258   " Trailing single backslash used to cause invalid memory access.
259   set complete=s\
260   new
261   call feedkeys("i\<C-N>\<Esc>", 'xt')
262   bwipe!
263   call assert_fails('set complete=ix', 'E535:')
264   set complete&
265 endfun
266
267 func Test_set_completion()
268   call feedkeys(":set di\<C-A>\<C-B>\"\<CR>", 'tx')
269   call assert_equal('"set dictionary diff diffexpr diffopt digraph directory display', @:)
270
271   call feedkeys(":setlocal di\<C-A>\<C-B>\"\<CR>", 'tx')
272   call assert_equal('"setlocal dictionary diff diffexpr diffopt digraph directory display', @:)
273
274   call feedkeys(":setglobal di\<C-A>\<C-B>\"\<CR>", 'tx')
275   call assert_equal('"setglobal dictionary diff diffexpr diffopt digraph directory display', @:)
276
277   " Expand boolean options. When doing :set no<Tab>
278   " vim displays the options names without "no" but completion uses "no...".
279   call feedkeys(":set nodi\<C-A>\<C-B>\"\<CR>", 'tx')
280   call assert_equal('"set nodiff digraph', @:)
281
282   call feedkeys(":set invdi\<C-A>\<C-B>\"\<CR>", 'tx')
283   call assert_equal('"set invdiff digraph', @:)
284
285   " Expand abbreviation of options.
286   call feedkeys(":set ts\<C-A>\<C-B>\"\<CR>", 'tx')
287   call assert_equal('"set tabstop thesaurus thesaurusfunc ttyscroll', @:)
288
289   " Expand current value
290   call feedkeys(":set fileencodings=\<C-A>\<C-B>\"\<CR>", 'tx')
291   call assert_equal('"set fileencodings=ucs-bom,utf-8,default,latin1', @:)
292
293   call feedkeys(":set fileencodings:\<C-A>\<C-B>\"\<CR>", 'tx')
294   call assert_equal('"set fileencodings:ucs-bom,utf-8,default,latin1', @:)
295
296   " Expand key codes.
297   call feedkeys(":set <H\<C-A>\<C-B>\"\<CR>", 'tx')
298   call assert_equal('"set <Help> <Home>', @:)
299
300   " Expand terminal options.
301   call feedkeys(":set t_A\<C-A>\<C-B>\"\<CR>", 'tx')
302   call assert_equal('"set t_AB t_AF t_AU t_AL', @:)
303   call assert_fails('call feedkeys(":set <t_afoo>=\<C-A>\<CR>", "xt")', 'E474:')
304
305   " Expand directories.
306   call feedkeys(":set cdpath=./\<C-A>\<C-B>\"\<CR>", 'tx')
307   call assert_match(' ./samples/ ', @:)
308   call assert_notmatch(' ./summarize.vim ', @:)
309
310   " Expand files and directories.
311   call feedkeys(":set tags=./\<C-A>\<C-B>\"\<CR>", 'tx')
312   call assert_match(' ./samples/.* ./summarize.vim', @:)
313
314   call feedkeys(":set tags=./\\\\ dif\<C-A>\<C-B>\"\<CR>", 'tx')
315   call assert_equal('"set tags=./\\ diff diffexpr diffopt', @:)
316   set tags&
317
318   " Expanding the option names
319   call feedkeys(":set \<Tab>\<C-B>\"\<CR>", 'xt')
320   call assert_equal('"set all', @:)
321
322   " Expanding a second set of option names
323   call feedkeys(":set wrapscan \<Tab>\<C-B>\"\<CR>", 'xt')
324   call assert_equal('"set wrapscan all', @:)
325
326   " Expanding a special keycode
327   call feedkeys(":set <Home>\<Tab>\<C-B>\"\<CR>", 'xt')
328   call assert_equal('"set <Home>', @:)
329
330   " Expanding an invalid special keycode
331   call feedkeys(":set <abcd>\<Tab>\<C-B>\"\<CR>", 'xt')
332   call assert_equal("\"set <abcd>\<Tab>", @:)
333
334   " Expanding a terminal keycode
335   call feedkeys(":set t_AB\<Tab>\<C-B>\"\<CR>", 'xt')
336   call assert_equal("\"set t_AB", @:)
337
338   " Expanding an invalid option name
339   call feedkeys(":set abcde=\<Tab>\<C-B>\"\<CR>", 'xt')
340   call assert_equal("\"set abcde=\<Tab>", @:)
341
342   " Expanding after a = for a boolean option
343   call feedkeys(":set wrapscan=\<Tab>\<C-B>\"\<CR>", 'xt')
344   call assert_equal("\"set wrapscan=\<Tab>", @:)
345
346   " Expanding a numeric option
347   call feedkeys(":set tabstop+=\<Tab>\<C-B>\"\<CR>", 'xt')
348   call assert_equal("\"set tabstop+=" .. &tabstop, @:)
349
350   " Expanding a non-boolean option
351   call feedkeys(":set invtabstop=\<Tab>\<C-B>\"\<CR>", 'xt')
352   call assert_equal("\"set invtabstop=", @:)
353
354   " Expand options for 'spellsuggest'
355   call feedkeys(":set spellsuggest=best,file:xyz\<Tab>\<C-B>\"\<CR>", 'xt')
356   call assert_equal("\"set spellsuggest=best,file:xyz", @:)
357
358   " Expand value for 'key'
359   set key=abcd
360   call feedkeys(":set key=\<Tab>\<C-B>\"\<CR>", 'xt')
361   call assert_equal('"set key=*****', @:)
362   set key=
363
364   " Expand values for 'filetype'
365   call feedkeys(":set filetype=sshdconfi\<Tab>\<C-B>\"\<CR>", 'xt')
366   call assert_equal('"set filetype=sshdconfig', @:)
367   call feedkeys(":set filetype=a\<C-A>\<C-B>\"\<CR>", 'xt')
368   call assert_equal('"set filetype=' .. getcompletion('a*', 'filetype')->join(), @:)
369 endfunc
370
371 func Test_set_errors()
372   call assert_fails('set scroll=-1', 'E49:')
373   call assert_fails('set backupcopy=', 'E474:')
374   call assert_fails('set regexpengine=3', 'E474:')
375   call assert_fails('set history=10001', 'E474:')
376   call assert_fails('set numberwidth=21', 'E474:')
377   call assert_fails('set colorcolumn=-a', 'E474:')
378   call assert_fails('set colorcolumn=a', 'E474:')
379   call assert_fails('set colorcolumn=1,', 'E474:')
380   call assert_fails('set colorcolumn=1;', 'E474:')
381   call assert_fails('set cmdheight=-1', 'E487:')
382   call assert_fails('set cmdwinheight=-1', 'E487:')
383   if has('conceal')
384     call assert_fails('set conceallevel=-1', 'E487:')
385     call assert_fails('set conceallevel=4', 'E474:')
386   endif
387   call assert_fails('set helpheight=-1', 'E487:')
388   call assert_fails('set history=-1', 'E487:')
389   call assert_fails('set report=-1', 'E487:')
390   call assert_fails('set shiftwidth=-1', 'E487:')
391   call assert_fails('set sidescroll=-1', 'E487:')
392   call assert_fails('set tabstop=-1', 'E487:')
393   call assert_fails('set tabstop=10000', 'E474:')
394   call assert_fails('let &tabstop = 10000', 'E474:')
395   call assert_fails('set tabstop=5500000000', 'E474:')
396   call assert_fails('set textwidth=-1', 'E487:')
397   call assert_fails('set timeoutlen=-1', 'E487:')
398   call assert_fails('set updatecount=-1', 'E487:')
399   call assert_fails('set updatetime=-1', 'E487:')
400   call assert_fails('set winheight=-1', 'E487:')
401   call assert_fails('set tabstop!', 'E488:')
402   call assert_fails('set xxx', 'E518:')
403   call assert_fails('set beautify?', 'E519:')
404   call assert_fails('set undolevels=x', 'E521:')
405   call assert_fails('set tabstop=', 'E521:')
406   call assert_fails('set comments=-', 'E524:')
407   call assert_fails('set comments=a', 'E525:')
408   call assert_fails('set foldmarker=x', 'E536:')
409   call assert_fails('set commentstring=x', 'E537:')
410   call assert_fails('let &commentstring = "x"', 'E537:')
411   call assert_fails('set complete=x', 'E539:')
412   call assert_fails('set rulerformat=%-', 'E539:')
413   call assert_fails('set rulerformat=%(', 'E542:')
414   call assert_fails('set rulerformat=%15(%%', 'E542:')
415   call assert_fails('set statusline=%$', 'E539:')
416   call assert_fails('set statusline=%{', 'E540:')
417   call assert_fails('set statusline=%{%', 'E540:')
418   call assert_fails('set statusline=%{%}', 'E539:')
419   call assert_fails('set statusline=%(', 'E542:')
420   call assert_fails('set statusline=%)', 'E542:')
421   call assert_fails('set tabline=%$', 'E539:')
422   call assert_fails('set tabline=%{', 'E540:')
423   call assert_fails('set tabline=%{%', 'E540:')
424   call assert_fails('set tabline=%{%}', 'E539:')
425   call assert_fails('set tabline=%(', 'E542:')
426   call assert_fails('set tabline=%)', 'E542:')
427
428   if has('cursorshape')
429     " This invalid value for 'guicursor' used to cause Vim to crash.
430     call assert_fails('set guicursor=i-ci,r-cr:h', 'E545:')
431     call assert_fails('set guicursor=i-ci', 'E545:')
432     call assert_fails('set guicursor=x', 'E545:')
433     call assert_fails('set guicursor=x:', 'E546:')
434     call assert_fails('set guicursor=r-cr:horx', 'E548:')
435     call assert_fails('set guicursor=r-cr:hor0', 'E549:')
436   endif
437   if has('mouseshape')
438     call assert_fails('se mouseshape=i-r:x', 'E547:')
439   endif
440
441   " Test for 'backupext' and 'patchmode' set to the same value
442   set backupext=.bak
443   set patchmode=.patch
444   call assert_fails('set patchmode=.bak', 'E589:')
445   call assert_equal('.patch', &patchmode)
446   call assert_fails('set backupext=.patch', 'E589:')
447   call assert_equal('.bak', &backupext)
448   set backupext& patchmode&
449
450   call assert_fails('set winminheight=10 winheight=9', 'E591:')
451   set winminheight& winheight&
452   set winheight=10 winminheight=10
453   call assert_fails('set winheight=9', 'E591:')
454   set winminheight& winheight&
455   call assert_fails('set winminwidth=10 winwidth=9', 'E592:')
456   set winminwidth& winwidth&
457   call assert_fails('set winwidth=9 winminwidth=10', 'E592:')
458   set winwidth& winminwidth&
459   call assert_fails("set showbreak=\x01", 'E595:')
460   call assert_fails('set t_foo=', 'E846:')
461   call assert_fails('set tabstop??', 'E488:')
462   call assert_fails('set wrapscan!!', 'E488:')
463   call assert_fails('set tabstop&&', 'E488:')
464   call assert_fails('set wrapscan<<', 'E488:')
465   call assert_fails('set wrapscan=1', 'E474:')
466   call assert_fails('set autoindent@', 'E488:')
467   call assert_fails('set wildchar=<abc>', 'E474:')
468   call assert_fails('set cmdheight=1a', 'E521:')
469   call assert_fails('set invcmdheight', 'E474:')
470   if has('python') || has('python3')
471     call assert_fails('set pyxversion=6', 'E474:')
472   endif
473   call assert_fails("let &tabstop='ab'", 'E521:')
474   call assert_fails('set spellcapcheck=%\\(', 'E54:')
475   call assert_fails('set sessionoptions=curdir,sesdir', 'E474:')
476   call assert_fails('set foldmarker={{{,', 'E474:')
477   call assert_fails('set sessionoptions=sesdir,curdir', 'E474:')
478   setlocal listchars=trail:·
479   call assert_fails('set ambiwidth=double', 'E834:')
480   setlocal listchars=trail:-
481   setglobal listchars=trail:·
482   call assert_fails('set ambiwidth=double', 'E834:')
483   set listchars&
484   setlocal fillchars=stl:·
485   call assert_fails('set ambiwidth=double', 'E835:')
486   setlocal fillchars=stl:-
487   setglobal fillchars=stl:·
488   call assert_fails('set ambiwidth=double', 'E835:')
489   set fillchars&
490   call assert_fails('set fileencoding=latin1,utf-8', 'E474:')
491   set nomodifiable
492   call assert_fails('set fileencoding=latin1', 'E21:')
493   set modifiable&
494   call assert_fails('set t_#-&', 'E522:')
495   call assert_fails('let &formatoptions = "?"', 'E539:')
496   call assert_fails('call setbufvar("", "&formatoptions", "?")', 'E539:')
497 endfunc
498
499 func Test_set_encoding()
500   let save_encoding = &encoding
501
502   set enc=iso8859-1
503   call assert_equal('latin1', &enc)
504   set enc=iso8859_1
505   call assert_equal('latin1', &enc)
506   set enc=iso-8859-1
507   call assert_equal('latin1', &enc)
508   set enc=iso_8859_1
509   call assert_equal('latin1', &enc)
510   set enc=iso88591
511   call assert_equal('latin1', &enc)
512   set enc=iso8859
513   call assert_equal('latin1', &enc)
514   set enc=iso-8859
515   call assert_equal('latin1', &enc)
516   set enc=iso_8859
517   call assert_equal('latin1', &enc)
518   call assert_fails('set enc=iso8858', 'E474:')
519   call assert_equal('latin1', &enc)
520
521   let &encoding = save_encoding
522 endfunc
523
524 func CheckWasSet(name)
525   let verb_cm = execute('verbose set ' .. a:name .. '?')
526   call assert_match('Last set from.*test_options.vim', verb_cm)
527 endfunc
528 func CheckWasNotSet(name)
529   let verb_cm = execute('verbose set ' .. a:name .. '?')
530   call assert_notmatch('Last set from', verb_cm)
531 endfunc
532
533 " Must be executed before other tests that set 'term'.
534 func Test_000_term_option_verbose()
535   CheckNotGui
536
537   call CheckWasNotSet('t_cm')
538
539   let term_save = &term
540   set term=ansi
541   call CheckWasSet('t_cm')
542   let &term = term_save
543 endfunc
544
545 func Test_copy_context()
546   setlocal list
547   call CheckWasSet('list')
548   split
549   call CheckWasSet('list')
550   quit
551   setlocal nolist
552
553   set ai
554   call CheckWasSet('ai')
555   set filetype=perl
556   call CheckWasSet('filetype')
557   set fo=tcroq
558   call CheckWasSet('fo')
559
560   split Xsomebuf
561   call CheckWasSet('ai')
562   call CheckWasNotSet('filetype')
563   call CheckWasSet('fo')
564 endfunc
565
566 func Test_set_ttytype()
567   CheckUnix
568   CheckNotGui
569
570   " Setting 'ttytype' used to cause a double-free when exiting vim and
571   " when vim is compiled with -DEXITFREE.
572   set ttytype=ansi
573   call assert_equal('ansi', &ttytype)
574   call assert_equal(&ttytype, &term)
575   set ttytype=xterm
576   call assert_equal('xterm', &ttytype)
577   call assert_equal(&ttytype, &term)
578   try
579     set ttytype=
580     call assert_report('set ttytype= did not fail')
581   catch /E529/
582   endtry
583
584   " Some systems accept any terminal name and return dumb settings,
585   " check for failure of finding the entry and for missing 'cm' entry.
586   try
587     set ttytype=xxx
588     call assert_report('set ttytype=xxx did not fail')
589   catch /E522\|E437/
590   endtry
591
592   set ttytype&
593   call assert_equal(&ttytype, &term)
594
595   if has('gui') && !has('gui_running')
596     call assert_fails('set term=gui', 'E531:')
597   endif
598 endfunc
599
600 func Test_set_all()
601   set tw=75
602   set iskeyword=a-z,A-Z
603   set nosplitbelow
604   let out = execute('set all')
605   call assert_match('textwidth=75', out)
606   call assert_match('iskeyword=a-z,A-Z', out)
607   call assert_match('nosplitbelow', out)
608   set tw& iskeyword& splitbelow&
609 endfunc
610
611 func Test_set_one_column()
612   let out_mult = execute('set all')->split("\n")
613   let out_one = execute('set! all')->split("\n")
614   call assert_true(len(out_mult) < len(out_one))
615 endfunc
616
617 func Test_set_values()
618   " opt_test.vim is generated from ../optiondefs.h using gen_opt_test.vim
619   if filereadable('opt_test.vim')
620     source opt_test.vim
621   else
622     throw 'Skipped: opt_test.vim does not exist'
623   endif
624 endfunc
625
626 func Test_renderoptions()
627   " Only do this for Windows Vista and later, fails on Windows XP and earlier.
628   " Doesn't hurt to do this on a non-Windows system.
629   if windowsversion() !~ '^[345]\.'
630     set renderoptions=type:directx
631     set rop=type:directx
632   endif
633 endfunc
634
635 func ResetIndentexpr()
636   set indentexpr=
637 endfunc
638
639 func Test_set_indentexpr()
640   " this was causing usage of freed memory
641   set indentexpr=ResetIndentexpr()
642   new
643   call feedkeys("i\<c-f>", 'x')
644   call assert_equal('', &indentexpr)
645   bwipe!
646 endfunc
647
648 func Test_backupskip()
649   " Option 'backupskip' may contain several comma-separated path
650   " specifications if one or more of the environment variables TMPDIR, TMP,
651   " or TEMP is defined.  To simplify testing, convert the string value into a
652   " list.
653   let bsklist = split(&bsk, ',')
654
655   if has("mac")
656     let found = (index(bsklist, '/private/tmp/*') >= 0)
657     call assert_true(found, '/private/tmp not in option bsk: ' . &bsk)
658   elseif has("unix")
659     let found = (index(bsklist, '/tmp/*') >= 0)
660     call assert_true(found, '/tmp not in option bsk: ' . &bsk)
661   endif
662
663   " If our test platform is Windows, the path(s) in option bsk will use
664   " backslash for the path separator and the components could be in short
665   " (8.3) format.  As such, we need to replace the backslashes with forward
666   " slashes and convert the path components to long format.  The expand()
667   " function will do this but it cannot handle comma-separated paths.  This is
668   " why bsk was converted from a string into a list of strings above.
669   "
670   " One final complication is that the wildcard "/*" is at the end of each
671   " path and so expand() might return a list of matching files.  To prevent
672   " this, we need to remove the wildcard before calling expand() and then
673   " append it afterwards.
674   if has('win32')
675     let item_nbr = 0
676     while item_nbr < len(bsklist)
677       let path_spec = bsklist[item_nbr]
678       let path_spec = strcharpart(path_spec, 0, strlen(path_spec)-2)
679       let path_spec = substitute(expand(path_spec), '\\', '/', 'g')
680       let bsklist[item_nbr] = path_spec . '/*'
681       let item_nbr += 1
682     endwhile
683   endif
684
685   " Option bsk will also include these environment variables if defined.
686   " If they're defined, verify they appear in the option value.
687   for var in  ['$TMPDIR', '$TMP', '$TEMP']
688     if exists(var)
689       let varvalue = substitute(expand(var), '\\', '/', 'g')
690       let varvalue = substitute(varvalue, '/$', '', '')
691       let varvalue .= '/*'
692       let found = (index(bsklist, varvalue) >= 0)
693       call assert_true(found, var . ' (' . varvalue . ') not in option bsk: ' . &bsk)
694     endif
695   endfor
696
697   " Duplicates from environment variables should be filtered out (option has
698   " P_NODUP).  Run this in a separate instance and write v:errors in a file,
699   " so that we see what happens on startup.
700   let after =<< trim [CODE]
701       let bsklist = split(&backupskip, ',')
702       call assert_equal(uniq(copy(bsklist)), bsklist)
703       call writefile(['errors:'] + v:errors, 'Xtestout')
704       qall
705   [CODE]
706   call writefile(after, 'Xafter', 'D')
707   let cmd = GetVimProg() . ' --not-a-term -S Xafter --cmd "set enc=utf8"'
708
709   let saveenv = {}
710   for var in ['TMPDIR', 'TMP', 'TEMP']
711     let saveenv[var] = getenv(var)
712     call setenv(var, '/duplicate/path')
713   endfor
714
715   exe 'silent !' . cmd
716   call assert_equal(['errors:'], readfile('Xtestout'))
717
718   " restore environment variables
719   for var in ['TMPDIR', 'TMP', 'TEMP']
720     call setenv(var, saveenv[var])
721   endfor
722
723   call delete('Xtestout')
724
725   " Duplicates should be filtered out (option has P_NODUP)
726   let backupskip = &backupskip
727   set backupskip=
728   set backupskip+=/test/dir
729   set backupskip+=/other/dir
730   set backupskip+=/test/dir
731   call assert_equal('/test/dir,/other/dir', &backupskip)
732   let &backupskip = backupskip
733 endfunc
734
735 func Test_buf_copy_winopt()
736   set hidden
737
738   " Test copy option from current buffer in window
739   split
740   enew
741   setlocal numberwidth=5
742   wincmd w
743   call assert_equal(4,&numberwidth)
744   bnext
745   call assert_equal(5,&numberwidth)
746   bw!
747   call assert_equal(4,&numberwidth)
748
749   " Test copy value from window that used to be display the buffer
750   split
751   enew
752   setlocal numberwidth=6
753   bnext
754   wincmd w
755   call assert_equal(4,&numberwidth)
756   bnext
757   call assert_equal(6,&numberwidth)
758   bw!
759
760   " Test that if buffer is current, don't use the stale cached value
761   " from the last time the buffer was displayed.
762   split
763   enew
764   setlocal numberwidth=7
765   bnext
766   bnext
767   setlocal numberwidth=8
768   wincmd w
769   call assert_equal(4,&numberwidth)
770   bnext
771   call assert_equal(8,&numberwidth)
772   bw!
773
774   " Test value is not copied if window already has seen the buffer
775   enew
776   split
777   setlocal numberwidth=9
778   bnext
779   setlocal numberwidth=10
780   wincmd w
781   call assert_equal(4,&numberwidth)
782   bnext
783   call assert_equal(4,&numberwidth)
784   bw!
785
786   set hidden&
787 endfunc
788
789 def Test_split_copy_options()
790   var values = [
791     ['cursorbind', true, false],
792     ['fillchars', '"vert:-"', '"' .. &fillchars .. '"'],
793     ['list', true, 0],
794     ['listchars', '"space:-"', '"' .. &listchars .. '"'],
795     ['number', true, 0],
796     ['relativenumber', true, false],
797     ['scrollbind', true, false],
798     ['smoothscroll', true, false],
799     ['virtualedit', '"block"', '"' .. &virtualedit .. '"'],
800     ['wincolor', '"Search"', '"' .. &wincolor .. '"'],
801     ['wrap', false, true],
802   ]
803   if has('linebreak')
804     values += [
805       ['breakindent', true, false],
806       ['breakindentopt', '"min:5"', '"' .. &breakindentopt .. '"'],
807       ['linebreak', true, false],
808       ['numberwidth', 7, 4],
809       ['showbreak', '"++"', '"' .. &showbreak .. '"'],
810     ]
811   endif
812   if has('rightleft')
813     values += [
814       ['rightleft', true, false],
815       ['rightleftcmd', '"search"', '"' .. &rightleftcmd .. '"'],
816     ]
817   endif
818   if has('statusline')
819     values += [
820       ['statusline', '"---%f---"', '"' .. &statusline .. '"'],
821     ]
822   endif
823   if has('spell')
824     values += [
825       ['spell', true, false],
826     ]
827   endif
828   if has('syntax')
829     values += [
830       ['cursorcolumn', true, false],
831       ['cursorline', true, false],
832       ['cursorlineopt', '"screenline"', '"' .. &cursorlineopt .. '"'],
833       ['colorcolumn', '"+1"', '"' .. &colorcolumn .. '"'],
834     ]
835   endif
836   if has('diff')
837     values += [
838       ['diff', true, false],
839     ]
840   endif
841   if has('conceal')
842     values += [
843       ['concealcursor', '"nv"', '"' .. &concealcursor .. '"'],
844       ['conceallevel', '3', &conceallevel],
845     ]
846   endif
847   if has('terminal')
848     values += [
849       ['termwinkey', '"<C-X>"', '"' .. &termwinkey .. '"'],
850       ['termwinsize', '"10x20"', '"' .. &termwinsize .. '"'],
851     ]
852   endif
853   if has('folding')
854     values += [
855       ['foldcolumn', 5,  &foldcolumn],
856       ['foldenable', false, true],
857       ['foldexpr', '"2 + 3"', '"' .. &foldexpr .. '"'],
858       ['foldignore', '"+="', '"' .. &foldignore .. '"'],
859       ['foldlevel', 4,  &foldlevel],
860       ['foldmarker', '">>,<<"', '"' .. &foldmarker .. '"'],
861       ['foldmethod', '"marker"', '"' .. &foldmethod .. '"'],
862       ['foldminlines', 3,  &foldminlines],
863       ['foldnestmax', 17,  &foldnestmax],
864       ['foldtext', '"closed"', '"' .. &foldtext .. '"'],
865     ]
866   endif
867   if has('signs')
868     values += [
869       ['signcolumn', '"number"', '"' .. &signcolumn .. '"'],
870     ]
871   endif
872
873   # set options to non-default value
874   for item in values
875     exe $'&l:{item[0]} = {item[1]}'
876   endfor
877
878   # check values are set in new window
879   split
880   for item in values
881     exe $'assert_equal({item[1]}, &{item[0]}, "{item[0]}")'
882   endfor
883
884   # restore
885   close
886   for item in values
887     exe $'&l:{item[0]} = {item[2]}'
888   endfor
889 enddef
890
891 func Test_shortmess_F()
892   new
893   call assert_match('\[No Name\]', execute('file'))
894   set shortmess+=F
895   call assert_match('\[No Name\]', execute('file'))
896   call assert_match('^\s*$', execute('file foo'))
897   call assert_match('foo', execute('file'))
898   set shortmess-=F
899   call assert_match('bar', execute('file bar'))
900   call assert_match('bar', execute('file'))
901   set shortmess&
902   bwipe
903 endfunc
904
905 func Test_shortmess_F2()
906   e file1
907   e file2
908   call assert_match('file1', execute('bn', ''))
909   call assert_match('file2', execute('bn', ''))
910   set shortmess+=F
911   call assert_true(empty(execute('bn', '')))
912   call assert_false(test_getvalue('need_fileinfo'))
913   call assert_true(empty(execute('bn', '')))
914   call assert_false('need_fileinfo'->test_getvalue())
915   set hidden
916   call assert_true(empty(execute('bn', '')))
917   call assert_false(test_getvalue('need_fileinfo'))
918   call assert_true(empty(execute('bn', '')))
919   call assert_false(test_getvalue('need_fileinfo'))
920   set nohidden
921   call assert_true(empty(execute('bn', '')))
922   call assert_false(test_getvalue('need_fileinfo'))
923   call assert_true(empty(execute('bn', '')))
924   call assert_false(test_getvalue('need_fileinfo'))
925   set shortmess&
926   call assert_match('file1', execute('bn', ''))
927   call assert_match('file2', execute('bn', ''))
928   bwipe
929   bwipe
930   call assert_fails('call test_getvalue("abc")', 'E475:')
931 endfunc
932
933 func Test_local_scrolloff()
934   set so=5
935   set siso=7
936   split
937   call assert_equal(5, &so)
938   setlocal so=3
939   call assert_equal(3, &so)
940   wincmd w
941   call assert_equal(5, &so)
942   wincmd w
943   setlocal so<
944   call assert_equal(5, &so)
945   setlocal so=0
946   call assert_equal(0, &so)
947   setlocal so=-1
948   call assert_equal(5, &so)
949
950   call assert_equal(7, &siso)
951   setlocal siso=3
952   call assert_equal(3, &siso)
953   wincmd w
954   call assert_equal(7, &siso)
955   wincmd w
956   setlocal siso<
957   call assert_equal(7, &siso)
958   setlocal siso=0
959   call assert_equal(0, &siso)
960   setlocal siso=-1
961   call assert_equal(7, &siso)
962
963   close
964   set so&
965   set siso&
966 endfunc
967
968 func Test_writedelay()
969   CheckFunction reltimefloat
970
971   new
972   call setline(1, 'empty')
973   redraw
974   set writedelay=10
975   let start = reltime()
976   call setline(1, repeat('x', 70))
977   redraw
978   let elapsed = reltimefloat(reltime(start))
979   set writedelay=0
980   " With 'writedelay' set should take at least 30 * 10 msec
981   call assert_inrange(30 * 0.01, 999.0, elapsed)
982
983   bwipe!
984 endfunc
985
986 func Test_visualbell()
987   set belloff=
988   set visualbell
989   call assert_beeps('normal 0h')
990   set novisualbell
991   set belloff=all
992 endfunc
993
994 " Test for the 'write' option
995 func Test_write()
996   new
997   call setline(1, ['L1'])
998   set nowrite
999   call assert_fails('write Xwrfile', 'E142:')
1000   set write
1001   close!
1002 endfunc
1003
1004 " Test for 'buftype' option
1005 func Test_buftype()
1006   new
1007   call setline(1, ['L1'])
1008   set buftype=nowrite
1009   call assert_fails('write', 'E382:')
1010
1011   for val in ['', 'nofile', 'nowrite', 'acwrite', 'quickfix', 'help', 'terminal', 'prompt', 'popup']
1012     exe 'set buftype=' .. val
1013     call writefile(['something'], 'XBuftype', 'D')
1014     call assert_fails('write XBuftype', 'E13:', 'with buftype=' .. val)
1015   endfor
1016
1017   bwipe!
1018 endfunc
1019
1020 " Test for the 'rightleftcmd' option
1021 func Test_rightleftcmd()
1022   CheckFeature rightleft
1023   set rightleft
1024
1025   let g:l = []
1026   func AddPos()
1027     call add(g:l, screencol())
1028     return ''
1029   endfunc
1030   cmap <expr> <F2> AddPos()
1031
1032   set rightleftcmd=
1033   call feedkeys("/\<F2>abc\<Right>\<F2>\<Left>\<Left>\<F2>" ..
1034         \ "\<Right>\<F2>\<Esc>", 'xt')
1035   call assert_equal([2, 5, 3, 4], g:l)
1036
1037   let g:l = []
1038   set rightleftcmd=search
1039   call feedkeys("/\<F2>abc\<Left>\<F2>\<Right>\<Right>\<F2>" ..
1040         \ "\<Left>\<F2>\<Esc>", 'xt')
1041   call assert_equal([&co - 1, &co - 4, &co - 2, &co - 3], g:l)
1042
1043   cunmap <F2>
1044   unlet g:l
1045   set rightleftcmd&
1046   set rightleft&
1047 endfunc
1048
1049 " Test for the 'debug' option
1050 func Test_debug_option()
1051   " redraw to avoid matching previous messages
1052   redraw
1053   set debug=beep
1054   exe "normal \<C-c>"
1055   call assert_equal('Beep!', Screenline(&lines))
1056   call assert_equal('line    4:', Screenline(&lines - 1))
1057   " only match the final colon in the line that shows the source
1058   call assert_match(':$', Screenline(&lines - 2))
1059   set debug&
1060 endfunc
1061
1062 " Test for the default CDPATH option
1063 func Test_opt_default_cdpath()
1064   let after =<< trim [CODE]
1065     call assert_equal(',/path/to/dir1,/path/to/dir2', &cdpath)
1066     call writefile(v:errors, 'Xtestout')
1067     qall
1068   [CODE]
1069   if has('unix')
1070     let $CDPATH='/path/to/dir1:/path/to/dir2'
1071   else
1072     let $CDPATH='/path/to/dir1;/path/to/dir2'
1073   endif
1074   if RunVim([], after, '')
1075     call assert_equal([], readfile('Xtestout'))
1076     call delete('Xtestout')
1077   endif
1078 endfunc
1079
1080 " Test for setting keycodes using set
1081 func Test_opt_set_keycode()
1082   call assert_fails('set <t_k1=l', 'E474:')
1083   call assert_fails('set <Home=l', 'E474:')
1084   set <t_k9>=abcd
1085   call assert_equal('abcd', &t_k9)
1086   set <t_k9>&
1087   set <F9>=xyz
1088   call assert_equal('xyz', &t_k9)
1089   set <t_k9>&
1090
1091   " should we test all of them?
1092   set t_Ce=testCe
1093   set t_Cs=testCs
1094   set t_Us=testUs
1095   set t_ds=testds
1096   set t_Ds=testDs
1097   call assert_equal('testCe', &t_Ce)
1098   call assert_equal('testCs', &t_Cs)
1099   call assert_equal('testUs', &t_Us)
1100   call assert_equal('testds', &t_ds)
1101   call assert_equal('testDs', &t_Ds)
1102 endfunc
1103
1104 " Test for changing options in a sandbox
1105 func Test_opt_sandbox()
1106   for opt in ['backupdir', 'cdpath', 'exrc']
1107     call assert_fails('sandbox set ' .. opt .. '?', 'E48:')
1108     call assert_fails('sandbox let &' .. opt .. ' = 1', 'E48:')
1109   endfor
1110   call assert_fails('sandbox let &modelineexpr = 1', 'E48:')
1111 endfunc
1112
1113 " Test for setting an option with local value to global value
1114 func Test_opt_local_to_global()
1115   setglobal equalprg=gprg
1116   setlocal equalprg=lprg
1117   call assert_equal('gprg', &g:equalprg)
1118   call assert_equal('lprg', &l:equalprg)
1119   call assert_equal('lprg', &equalprg)
1120   set equalprg<
1121   call assert_equal('', &l:equalprg)
1122   call assert_equal('gprg', &equalprg)
1123   setglobal equalprg=gnewprg
1124   setlocal equalprg=lnewprg
1125   setlocal equalprg<
1126   call assert_equal('gnewprg', &l:equalprg)
1127   call assert_equal('gnewprg', &equalprg)
1128   set equalprg&
1129
1130   " Test for setting the global/local value of a boolean option
1131   setglobal autoread
1132   setlocal noautoread
1133   call assert_false(&autoread)
1134   set autoread<
1135   call assert_true(&autoread)
1136   setglobal noautoread
1137   setlocal autoread
1138   setlocal autoread<
1139   call assert_false(&autoread)
1140   set autoread&
1141 endfunc
1142
1143 func Test_set_in_sandbox()
1144   " Some boolean options cannot be set in sandbox, some can.
1145   call assert_fails('sandbox set modelineexpr', 'E48:')
1146   sandbox set number
1147   call assert_true(&number)
1148   set number&
1149
1150   " Some boolean options cannot be set in sandbox, some can.
1151   if has('python') || has('python3')
1152     call assert_fails('sandbox set pyxversion=3', 'E48:')
1153   endif
1154   sandbox set tabstop=4
1155   call assert_equal(4, &tabstop)
1156   set tabstop&
1157
1158   " Some string options cannot be set in sandbox, some can.
1159   call assert_fails('sandbox set backupdir=/tmp', 'E48:')
1160   sandbox set filetype=perl
1161   call assert_equal('perl', &filetype)
1162   set filetype&
1163 endfunc
1164
1165 " Test for incrementing, decrementing and multiplying a number option value
1166 func Test_opt_num_op()
1167   set shiftwidth=4
1168   set sw+=2
1169   call assert_equal(6, &sw)
1170   set sw-=2
1171   call assert_equal(4, &sw)
1172   set sw^=2
1173   call assert_equal(8, &sw)
1174   set shiftwidth&
1175 endfunc
1176
1177 " Test for setting option values using v:false and v:true
1178 func Test_opt_boolean()
1179   set number&
1180   set number
1181   call assert_equal(1, &nu)
1182   set nonu
1183   call assert_equal(0, &nu)
1184   let &nu = v:true
1185   call assert_equal(1, &nu)
1186   let &nu = v:false
1187   call assert_equal(0, &nu)
1188   set number&
1189 endfunc
1190
1191 " Test for the 'window' option
1192 func Test_window_opt()
1193   " Needs only one open widow
1194   %bw!
1195   call setline(1, range(1, 8))
1196   set window=5
1197   exe "normal \<C-F>"
1198   call assert_equal(4, line('w0'))
1199   exe "normal \<C-F>"
1200   call assert_equal(7, line('w0'))
1201   exe "normal \<C-F>"
1202   call assert_equal(8, line('w0'))
1203   exe "normal \<C-B>"
1204   call assert_equal(5, line('w0'))
1205   exe "normal \<C-B>"
1206   call assert_equal(2, line('w0'))
1207   exe "normal \<C-B>"
1208   call assert_equal(1, line('w0'))
1209   set window=1
1210   exe "normal gg\<C-F>"
1211   call assert_equal(2, line('w0'))
1212   exe "normal \<C-F>"
1213   call assert_equal(3, line('w0'))
1214   exe "normal \<C-B>"
1215   call assert_equal(2, line('w0'))
1216   exe "normal \<C-B>"
1217   call assert_equal(1, line('w0'))
1218   enew!
1219   set window&
1220 endfunc
1221
1222 " Test for the 'winminheight' option
1223 func Test_opt_winminheight()
1224   only!
1225   let &winheight = &lines + 4
1226   call assert_fails('let &winminheight = &lines + 2', 'E36:')
1227   call assert_true(&winminheight <= &lines)
1228   set winminheight&
1229   set winheight&
1230 endfunc
1231
1232 func Test_opt_winminheight_term()
1233   CheckRunVimInTerminal
1234
1235   " The tabline should be taken into account.
1236   let lines =<< trim END
1237     set wmh=0 stal=2
1238     below sp | wincmd _
1239     below sp | wincmd _
1240     below sp | wincmd _
1241     below sp
1242   END
1243   call writefile(lines, 'Xwinminheight', 'D')
1244   let buf = RunVimInTerminal('-S Xwinminheight', #{rows: 11})
1245   call term_sendkeys(buf, ":set wmh=1\n")
1246   call WaitForAssert({-> assert_match('E36: Not enough room', term_getline(buf, 11))})
1247
1248   call StopVimInTerminal(buf)
1249 endfunc
1250
1251 func Test_opt_winminheight_term_tabs()
1252   CheckRunVimInTerminal
1253
1254   " The tabline should be taken into account.
1255   let lines =<< trim END
1256     set wmh=0 stal=2
1257     split
1258     split
1259     split
1260     split
1261     tabnew
1262   END
1263   call writefile(lines, 'Xwinminheight', 'D')
1264   let buf = RunVimInTerminal('-S Xwinminheight', #{rows: 11})
1265   call term_sendkeys(buf, ":set wmh=1\n")
1266   call WaitForAssert({-> assert_match('E36: Not enough room', term_getline(buf, 11))})
1267
1268   call StopVimInTerminal(buf)
1269 endfunc
1270
1271 " Test for the 'winminwidth' option
1272 func Test_opt_winminwidth()
1273   only!
1274   let &winwidth = &columns + 4
1275   call assert_fails('let &winminwidth = &columns + 2', 'E36:')
1276   call assert_true(&winminwidth <= &columns)
1277   set winminwidth&
1278   set winwidth&
1279 endfunc
1280
1281 " Test for setting option value containing spaces with isfname+=32
1282 func Test_isfname_with_options()
1283   set isfname+=32
1284   setlocal keywordprg=:term\ help.exe
1285   call assert_equal(':term help.exe', &keywordprg)
1286   set isfname&
1287   setlocal keywordprg&
1288 endfunc
1289
1290 " Test that resetting laststatus does change scroll option
1291 func Test_opt_reset_scroll()
1292   CheckRunVimInTerminal
1293   let vimrc =<< trim [CODE]
1294     set scroll=2
1295     set laststatus=2
1296   [CODE]
1297   call writefile(vimrc, 'Xscroll', 'D')
1298   let buf = RunVimInTerminal('-S Xscroll', {'rows': 16, 'cols': 45})
1299   call term_sendkeys(buf, ":verbose set scroll?\n")
1300   call WaitForAssert({-> assert_match('Last set.*window size', term_getline(buf, 15))})
1301   call assert_match('^\s*scroll=7$', term_getline(buf, 14))
1302
1303   " clean up
1304   call StopVimInTerminal(buf)
1305 endfunc
1306
1307 " Check that VIM_POSIX env variable influences default value of 'cpo' and 'shm'
1308 func Test_VIM_POSIX()
1309   let saved_VIM_POSIX = getenv("VIM_POSIX")
1310
1311   call setenv('VIM_POSIX', "1")
1312   let after =<< trim [CODE]
1313     call writefile([&cpo, &shm], 'X_VIM_POSIX')
1314     qall
1315   [CODE]
1316   if RunVim([], after, '')
1317     call assert_equal(['aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZ$!%*-+<>#{|&/\.;',
1318           \            'AS'], readfile('X_VIM_POSIX'))
1319   endif
1320
1321   call setenv('VIM_POSIX', v:null)
1322   let after =<< trim [CODE]
1323     call writefile([&cpo, &shm], 'X_VIM_POSIX')
1324     qall
1325   [CODE]
1326   if RunVim([], after, '')
1327     call assert_equal(['aAbBcCdDeEfFgHiIjJkKlLmMnoOpPqrRsStuvwWxXyZ$!%*-+<>;',
1328           \            'S'], readfile('X_VIM_POSIX'))
1329   endif
1330
1331   call delete('X_VIM_POSIX')
1332   call setenv('VIM_POSIX', saved_VIM_POSIX)
1333 endfunc
1334
1335 " Test for setting an option to a Vi or Vim default
1336 func Test_opt_default()
1337   set formatoptions&vi
1338   call assert_equal('vt', &formatoptions)
1339   set formatoptions&vim
1340   call assert_equal('tcq', &formatoptions)
1341
1342   call assert_equal('ucs-bom,utf-8,default,latin1', &fencs)
1343   set fencs=latin1
1344   set fencs&
1345   call assert_equal('ucs-bom,utf-8,default,latin1', &fencs)
1346   set fencs=latin1
1347   set all&
1348   call assert_equal('ucs-bom,utf-8,default,latin1', &fencs)
1349 endfunc
1350
1351 " Test for the 'cmdheight' option
1352 func Test_cmdheight()
1353   %bw!
1354   let ht = &lines
1355   set cmdheight=9999
1356   call assert_equal(1, winheight(0))
1357   call assert_equal(ht - 1, &cmdheight)
1358   set cmdheight&
1359 endfunc
1360
1361 " To specify a control character as an option value, '^' can be used
1362 func Test_opt_control_char()
1363   set wildchar=^v
1364   call assert_equal("\<C-V>", nr2char(&wildchar))
1365   set wildcharm=^r
1366   call assert_equal("\<C-R>", nr2char(&wildcharm))
1367   " Bug: This doesn't work for the 'cedit' and 'termwinkey' options
1368   set wildchar& wildcharm&
1369 endfunc
1370
1371 " Test for the 'errorbells' option
1372 func Test_opt_errorbells()
1373   set errorbells
1374   call assert_beeps('s/a1b2/x1y2/')
1375   set noerrorbells
1376 endfunc
1377
1378 func Test_opt_scrolljump()
1379   help
1380   resize 10
1381
1382   " Test with positive 'scrolljump'.
1383   set scrolljump=2
1384   norm! Lj
1385   call assert_equal({'lnum':11, 'leftcol':0, 'col':0, 'topfill':0,
1386         \            'topline':3, 'coladd':0, 'skipcol':0, 'curswant':0},
1387         \           winsaveview())
1388
1389   " Test with negative 'scrolljump' (percentage of window height).
1390   set scrolljump=-40
1391   norm! ggLj
1392   call assert_equal({'lnum':11, 'leftcol':0, 'col':0, 'topfill':0,
1393          \            'topline':5, 'coladd':0, 'skipcol':0, 'curswant':0},
1394          \           winsaveview())
1395
1396   set scrolljump&
1397   bw
1398 endfunc
1399
1400 " Test for the 'cdhome' option
1401 func Test_opt_cdhome()
1402   if has('unix') || has('vms')
1403     throw 'Skipped: only works on non-Unix'
1404   endif
1405
1406   set cdhome&
1407   call assert_equal(0, &cdhome)
1408   set cdhome
1409
1410   " This paragraph is copied from Test_cd_no_arg().
1411   let path = getcwd()
1412   cd
1413   call assert_equal($HOME, getcwd())
1414   call assert_notequal(path, getcwd())
1415   exe 'cd ' .. fnameescape(path)
1416   call assert_equal(path, getcwd())
1417
1418   set cdhome&
1419 endfunc
1420
1421 func Test_set_completion_2()
1422   CheckOption termguicolors
1423
1424   " Test default option completion
1425   set wildoptions=
1426   call feedkeys(":set termg\<C-A>\<C-B>\"\<CR>", 'tx')
1427   call assert_equal('"set termguicolors', @:)
1428
1429   call feedkeys(":set notermg\<C-A>\<C-B>\"\<CR>", 'tx')
1430   call assert_equal('"set notermguicolors', @:)
1431
1432   " Test fuzzy option completion
1433   set wildoptions=fuzzy
1434   call feedkeys(":set termg\<C-A>\<C-B>\"\<CR>", 'tx')
1435   call assert_equal('"set termguicolors termencoding', @:)
1436
1437   call feedkeys(":set notermg\<C-A>\<C-B>\"\<CR>", 'tx')
1438   call assert_equal('"set notermguicolors', @:)
1439
1440   set wildoptions=
1441 endfunc
1442
1443 func Test_switchbuf_reset()
1444   set switchbuf=useopen
1445   sblast
1446   call assert_equal(1, winnr('$'))
1447   set all&
1448   call assert_equal('', &switchbuf)
1449   sblast
1450   call assert_equal(2, winnr('$'))
1451   only!
1452 endfunc
1453
1454 " :set empty string for global 'keywordprg' falls back to ":help"
1455 func Test_keywordprg_empty()
1456   let k = &keywordprg
1457   set keywordprg=man
1458   call assert_equal('man', &keywordprg)
1459   set keywordprg=
1460   call assert_equal(':help', &keywordprg)
1461   set keywordprg=man
1462   call assert_equal('man', &keywordprg)
1463   call assert_equal("\n  keywordprg=:help", execute('set kp= kp?'))
1464   let &keywordprg = k
1465 endfunc
1466
1467 " check that the very first buffer created does not have 'endoffile' set
1468 func Test_endoffile_default()
1469   let after =<< trim [CODE]
1470     call writefile([execute('set eof?')], 'Xtestout')
1471     qall!
1472   [CODE]
1473   if RunVim([], after, '')
1474     call assert_equal(["\nnoendoffile"], readfile('Xtestout'))
1475   endif
1476   call delete('Xtestout')
1477 endfunc
1478
1479 " Test for setting the 'lines' and 'columns' options to a minimum value
1480 func Test_set_min_lines_columns()
1481   let save_lines = &lines
1482   let save_columns = &columns
1483
1484   let after =<< trim END
1485     set nomore
1486     let msg = []
1487     let v:errmsg = ''
1488     silent! let &columns=0
1489     call add(msg, v:errmsg)
1490     silent! set columns=0
1491     call add(msg, v:errmsg)
1492     silent! call setbufvar('', '&columns', 0)
1493     call add(msg, v:errmsg)
1494     "call writefile(msg, 'XResultsetminlines')
1495     silent! let &lines=0
1496     call add(msg, v:errmsg)
1497     silent! set lines=0
1498     call add(msg, v:errmsg)
1499     silent! call setbufvar('', '&lines', 0)
1500     call add(msg, v:errmsg)
1501     call writefile(msg, 'XResultsetminlines')
1502     qall!
1503   END
1504   if RunVim([], after, '')
1505     call assert_equal(['E594: Need at least 12 columns',
1506           \ 'E594: Need at least 12 columns: columns=0',
1507           \ 'E594: Need at least 12 columns',
1508           \ 'E593: Need at least 2 lines',
1509           \ 'E593: Need at least 2 lines: lines=0',
1510           \ 'E593: Need at least 2 lines',], readfile('XResultsetminlines'))
1511   endif
1512
1513   call delete('XResultsetminlines')
1514   let &lines = save_lines
1515   let &columns = save_columns
1516 endfunc
1517
1518 " Test for reverting a string option value if the new value is invalid.
1519 func Test_string_option_revert_on_failure()
1520   new
1521   let optlist = [
1522         \ ['ambiwidth', 'double', 'a123'],
1523         \ ['background', 'dark', 'a123'],
1524         \ ['backspace', 'eol', 'a123'],
1525         \ ['backupcopy', 'no', 'a123'],
1526         \ ['belloff', 'showmatch', 'a123'],
1527         \ ['breakindentopt', 'min:10', 'list'],
1528         \ ['bufhidden', 'wipe', 'a123'],
1529         \ ['buftype', 'nowrite', 'a123'],
1530         \ ['casemap', 'keepascii', 'a123'],
1531         \ ['cedit', "\<C-Y>", 'z'],
1532         \ ['colorcolumn', '10', 'z'],
1533         \ ['commentstring', '#%s', 'a123'],
1534         \ ['complete', '.,t', 'a'],
1535         \ ['completefunc', 'MyCmplFunc', '1a-'],
1536         \ ['completeopt', 'popup', 'a123'],
1537         \ ['completepopup', 'width:20', 'border'],
1538         \ ['concealcursor', 'v', 'xyz'],
1539         \ ['cpoptions', 'HJ', '~'],
1540         \ ['cryptmethod', 'zip', 'a123'],
1541         \ ['cursorlineopt', 'screenline', 'a123'],
1542         \ ['debug', 'throw', 'a123'],
1543         \ ['diffopt', 'iwhite', 'a123'],
1544         \ ['display', 'uhex', 'a123'],
1545         \ ['eadirection', 'hor', 'a123'],
1546         \ ['encoding', 'utf-8', 'a123'],
1547         \ ['eventignore', 'TextYankPost', 'a123'],
1548         \ ['fileencoding', 'utf-8', 'a123,'],
1549         \ ['fileformat', 'mac', 'a123'],
1550         \ ['fileformats', 'mac', 'a123'],
1551         \ ['fillchars', 'diff:~', 'a123'],
1552         \ ['foldclose', 'all', 'a123'],
1553         \ ['foldmarker', '[[[,]]]', '[[['],
1554         \ ['foldmethod', 'marker', 'a123'],
1555         \ ['foldopen', 'percent', 'a123'],
1556         \ ['formatoptions', 'an', '*'],
1557         \ ['guicursor', 'n-v-c:block-Cursor/lCursor', 'n-v-c'],
1558         \ ['helplang', 'en', 'a'],
1559         \ ['highlight', '!:CursorColumn', '8:']
1560         \ ]
1561   if has('gui')
1562     call add(optlist, ['browsedir', 'buffer', 'a123'])
1563   endif
1564   if has('clipboard_working')
1565     call add(optlist, ['clipboard', 'unnamed', 'a123'])
1566   endif
1567   if has('win32')
1568     call add(optlist, ['completeslash', 'slash', 'a123'])
1569   endif
1570   if has('cscope')
1571     call add(optlist, ['cscopequickfix', 't-', 'z-'])
1572   endif
1573   if !has('win32')
1574     call add(optlist, ['imactivatefunc', 'MyCmplFunc', '1a-'])
1575   endif
1576   for opt in optlist
1577     exe $"let save_opt = &{opt[0]}"
1578     exe $"let &{opt[0]} = '{opt[1]}'"
1579     call assert_fails($"let &{opt[0]} = '{opt[2]}'", '', opt[0])
1580     call assert_equal(opt[1], eval($"&{opt[0]}"), opt[0])
1581     exe $"let &{opt[0]} = save_opt"
1582   endfor
1583   bw!
1584 endfunc
1585
1586 " vim: shiftwidth=2 sts=2 expandtab