1" Test for :mksession, :mkview and :loadview in latin1 encoding
2
3set encoding=latin1
4scriptencoding latin1
5
6source check.vim
7CheckFeature mksession
8
9source shared.vim
10source term_util.vim
11
12" Test for storing global and local argument list in a session file
13" This one must be done first.
14func Test__mksession_arglocal()
15  enew | only
16  n a b c
17  new
18  arglocal
19  mksession! Xtest_mks.out
20
21  %bwipe!
22  %argdelete
23  argglobal
24  source Xtest_mks.out
25  call assert_equal(2, winnr('$'))
26  call assert_equal(2, arglistid(1))
27  call assert_equal(0, arglistid(2))
28
29  %bwipe!
30  %argdelete
31  argglobal
32  call delete('Xtest_mks.out')
33endfunc
34
35func Test_mksession()
36  tabnew
37  let wrap_save = &wrap
38  set sessionoptions=buffers splitbelow fileencoding=latin1
39  call setline(1, [
40    \   'start:',
41    \   'no multibyte chAracter',
42    \   '	one leaDing tab',
43    \   '    four leadinG spaces',
44    \   'two		consecutive tabs',
45    \   'two	tabs	in one line',
46    \   'one � multibyteCharacter',
47    \   'a� �  two multiByte characters',
48    \   'A���  three mulTibyte characters',
49    \   'short line',
50    \ ])
51  let tmpfile = 'Xtemp'
52  exec 'w! ' . tmpfile
53  /^start:
54  set wrap
55  vsplit
56  norm! j16|
57  split
58  norm! j16|
59  split
60  norm! j16|
61  split
62  norm! j8|
63  split
64  norm! j8|
65  split
66  norm! j16|
67  split
68  norm! j16|
69  split
70  norm! j16|
71  split
72  norm! j$
73  wincmd l
74
75  set nowrap
76  /^start:
77  norm! j16|3zl
78  split
79  norm! j016|3zl
80  split
81  norm! j016|3zl
82  split
83  norm! j08|3zl
84  split
85  norm! j08|3zl
86  split
87  norm! j016|3zl
88  split
89  norm! j016|3zl
90  split
91  norm! j016|3zl
92  split
93  call wincol()
94  mksession! Xtest_mks.out
95  let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
96  let expected = [
97    \   'normal! 016|',
98    \   'normal! 016|',
99    \   'normal! 016|',
100    \   'normal! 08|',
101    \   'normal! 08|',
102    \   'normal! 016|',
103    \   'normal! 016|',
104    \   'normal! 016|',
105    \   'normal! $',
106    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
107    \   "  normal! 016|",
108    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
109    \   "  normal! 016|",
110    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
111    \   "  normal! 016|",
112    \   "  exe 'normal! ' . s:c . '|zs' . 8 . '|'",
113    \   "  normal! 08|",
114    \   "  exe 'normal! ' . s:c . '|zs' . 8 . '|'",
115    \   "  normal! 08|",
116    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
117    \   "  normal! 016|",
118    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
119    \   "  normal! 016|",
120    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
121    \   "  normal! 016|",
122    \   "  exe 'normal! ' . s:c . '|zs' . 16 . '|'",
123    \   "  normal! 016|"
124    \ ]
125  call assert_equal(expected, li)
126  tabclose!
127
128  call delete('Xtest_mks.out')
129  call delete(tmpfile)
130  let &wrap = wrap_save
131  set sessionoptions&
132endfunc
133
134def Test_mksession_skiprtp()
135  mksession! Xtest_mks.out
136  var found_rtp = 0
137  var found_pp = 0
138  for line in readfile('Xtest_mks.out')
139    if line =~ 'set runtimepath'
140      found_rtp += 1
141    endif
142    if line =~ 'set packpath'
143      found_pp += 1
144    endif
145  endfor
146  assert_equal(1, found_rtp)
147  assert_equal(1, found_pp)
148  delete('Xtest_mks.out')
149
150  set sessionoptions+=skiprtp
151  mksession! Xtest_mks.out
152  var found = 0
153  for line in readfile('Xtest_mks.out')
154    if line =~ 'set \(runtimepath\|packpath\)'
155      found = 1
156      break
157    endif
158  endfor
159  assert_equal(0, found)
160  delete('Xtest_mks.out')
161  set sessionoptions&
162enddef
163
164func Test_mksession_winheight()
165  new
166  set winheight=10
167  set winminheight=2
168  mksession! Xtest_mks.out
169  source Xtest_mks.out
170
171  call delete('Xtest_mks.out')
172endfunc
173
174func Test_mksession_large_winheight()
175  set winheight=999
176  mksession! Xtest_mks_winheight.out
177  set winheight&
178  source Xtest_mks_winheight.out
179  call delete('Xtest_mks_winheight.out')
180endfunc
181
182func Test_mksession_zero_winheight()
183  set winminheight=0
184  edit SomeFile
185  split
186  wincmd _
187  mksession! Xtest_mks_zero
188  set winminheight&
189  let text = readfile('Xtest_mks_zero')->join()
190  call delete('Xtest_mks_zero')
191  close
192  " check there is no divide by zero
193  call assert_notmatch('/ 0[^0-9]', text)
194endfunc
195
196func Test_mksession_rtp()
197  " TODO: fix problem with backslashes on Win32
198  CheckNotMSWindows
199
200  new
201  let _rtp=&rtp
202  " Make a real long (invalid) runtimepath value,
203  " that should exceed PATH_MAX (hopefully)
204  let newrtp=&rtp.',~'.repeat('/foobar', 1000)
205  let newrtp.=",".expand("$HOME")."/.vim"
206  let &rtp=newrtp
207
208  " determine expected value
209  let expected=split(&rtp, ',')
210  let expected = map(expected, '"set runtimepath+=".v:val')
211  let expected = ['set runtimepath='] + expected
212  let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
213
214  mksession! Xtest_mks.out
215  let &rtp=_rtp
216  let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
217  call assert_equal(expected, li)
218
219  call delete('Xtest_mks.out')
220endfunc
221
222func Test_mksession_arglist()
223  %argdel
224  next file1 file2 file3 file4
225  new
226  next | next
227  mksession! Xtest_mks.out
228  source Xtest_mks.out
229  call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
230  call assert_equal(2, argidx())
231  wincmd w
232  call assert_equal(0, argidx())
233
234  call delete('Xtest_mks.out')
235  enew | only
236  argdel *
237endfunc
238
239func Test_mksession_one_buffer_two_windows()
240  edit Xtest1
241  new Xtest2
242  split
243  mksession! Xtest_mks.out
244  let lines = readfile('Xtest_mks.out')
245  let count1 = 0
246  let count2 = 0
247  let count2buf = 0
248  for line in lines
249    if line =~ 'edit \f*Xtest1$'
250      let count1 += 1
251    endif
252    if line =~ 'edit \f\{-}Xtest2'
253      let count2 += 1
254    endif
255    if line =~ 'buffer \f\{-}Xtest2'
256      let count2buf += 1
257    endif
258  endfor
259  call assert_equal(1, count1, 'Xtest1 count')
260  call assert_equal(2, count2, 'Xtest2 count')
261  call assert_equal(2, count2buf, 'Xtest2 buffer count')
262
263  close
264  bwipe!
265  call delete('Xtest_mks.out')
266endfunc
267
268func Test_mksession_lcd_multiple_tabs()
269  tabnew
270  tabnew
271  lcd .
272  tabfirst
273  lcd .
274  mksession! Xtest_mks.out
275  tabonly
276  source Xtest_mks.out
277  call assert_true(haslocaldir(), 'Tab 1 localdir')
278  tabnext 2
279  call assert_true(!haslocaldir(), 'Tab 2 localdir')
280  tabnext 3
281  call assert_true(haslocaldir(), 'Tab 3 localdir')
282  call delete('Xtest_mks.out')
283endfunc
284
285" Test for tabpage-local directory
286func Test_mksession_tcd_multiple_tabs()
287  let save_cwd = getcwd()
288  call mkdir('Xtopdir')
289  cd Xtopdir
290  call mkdir('Xtabdir1')
291  call mkdir('Xtabdir2')
292  call mkdir('Xtabdir3')
293  call mkdir('Xwindir1')
294  call mkdir('Xwindir2')
295  call mkdir('Xwindir3')
296  tcd Xtabdir1
297  botright new
298  wincmd t
299  lcd ../Xwindir1
300  tabnew
301  tcd ../Xtabdir2
302  botright new
303  lcd ../Xwindir2
304  tabnew
305  tcd ../Xtabdir3
306  botright new
307  lcd ../Xwindir3
308  tabfirst
309  1wincmd w
310  mksession! Xtest_mks.out
311  only | tabonly
312  source Xtest_mks.out
313  call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
314  call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
315  call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
316  call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
317  call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
318  call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
319  call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
320  call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
321  call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
322  %bwipe
323  call chdir(save_cwd)
324  call delete("Xtopdir", "rf")
325endfunc
326
327func Test_mksession_blank_tabs()
328  tabnew
329  tabnew
330  tabnew
331  tabnext 3
332  mksession! Xtest_mks.out
333  tabnew
334  tabnew
335  tabnext 2
336  source Xtest_mks.out
337  call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
338  call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
339  call delete('Xtest_mks.out')
340endfunc
341
342func Test_mksession_buffer_count()
343  set hidden
344
345  " Edit exactly three files in the current session.
346  %bwipe!
347  e Xfoo | tabe Xbar | tabe Xbaz
348  tabdo write
349  mksession! Xtest_mks.out
350
351  " Verify that loading the session does not create additional buffers.
352  %bwipe!
353  source Xtest_mks.out
354  call assert_equal(3, len(getbufinfo()))
355
356  " Clean up.
357  call delete('Xfoo')
358  call delete('Xbar')
359  call delete('Xbaz')
360  call delete('Xtest_mks.out')
361  %bwipe!
362  set hidden&
363endfunc
364
365if has('extra_search')
366
367func Test_mksession_hlsearch()
368  set hlsearch
369  mksession! Xtest_mks.out
370  nohlsearch
371  source Xtest_mks.out
372  call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
373  nohlsearch
374  mksession! Xtest_mks.out
375  source Xtest_mks.out
376  call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
377  call delete('Xtest_mks.out')
378endfunc
379
380endif
381
382
383func Test_mksession_blank_windows()
384  split
385  split
386  split
387  3 wincmd w
388  mksession! Xtest_mks.out
389  split
390  split
391  2 wincmd w
392  source Xtest_mks.out
393  call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
394  call assert_equal(3, winnr(), 'session restore should restore the active window')
395  call delete('Xtest_mks.out')
396endfunc
397
398func Test_mksession_terminal_shell()
399  CheckFeature terminal
400  CheckFeature quickfix
401
402  terminal
403  mksession! Xtest_mks.out
404  let lines = readfile('Xtest_mks.out')
405  let term_cmd = ''
406  for line in lines
407    if line =~ '^terminal'
408      let term_cmd = line
409    elseif line =~ 'badd.*' . &shell
410      call assert_report('unexpected shell line: ' . line)
411    endif
412  endfor
413  call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
414
415  call StopShellInTerminal(bufnr('%'))
416  call delete('Xtest_mks.out')
417endfunc
418
419func Test_mksession_terminal_no_restore_cmdarg()
420  CheckFeature terminal
421
422  terminal ++norestore
423  mksession! Xtest_mks.out
424  let lines = readfile('Xtest_mks.out')
425  let term_cmd = ''
426  for line in lines
427    if line =~ '^terminal'
428      call assert_report('session must not restore terminal')
429    endif
430  endfor
431
432  call StopShellInTerminal(bufnr('%'))
433  call delete('Xtest_mks.out')
434endfunc
435
436func Test_mksession_terminal_no_restore_funcarg()
437  CheckFeature terminal
438
439  let buf = Run_shell_in_terminal({'norestore': 1})
440  mksession! Xtest_mks.out
441  let lines = readfile('Xtest_mks.out')
442  let term_cmd = ''
443  for line in lines
444    if line =~ '^terminal'
445      call assert_report('session must not restore terminal')
446    endif
447  endfor
448
449  call StopShellInTerminal(buf)
450  call delete('Xtest_mks.out')
451endfunc
452
453func Test_mksession_terminal_no_restore_func()
454  CheckFeature terminal
455
456  terminal
457  call term_setrestore(bufnr('%'), 'NONE')
458  mksession! Xtest_mks.out
459  let lines = readfile('Xtest_mks.out')
460  let term_cmd = ''
461  for line in lines
462    if line =~ '^terminal'
463      call assert_report('session must not restore terminal')
464    endif
465  endfor
466
467  call StopShellInTerminal(bufnr('%'))
468  call delete('Xtest_mks.out')
469endfunc
470
471func Test_mksession_terminal_no_ssop()
472  CheckFeature terminal
473
474  terminal
475  set sessionoptions-=terminal
476  mksession! Xtest_mks.out
477  let lines = readfile('Xtest_mks.out')
478  let term_cmd = ''
479  for line in lines
480    if line =~ '^terminal'
481      call assert_report('session must not restore terminal')
482    endif
483  endfor
484
485  call StopShellInTerminal(bufnr('%'))
486  call delete('Xtest_mks.out')
487  set sessionoptions&
488endfunc
489
490func Test_mksession_terminal_restore_other()
491  CheckFeature terminal
492  CheckFeature quickfix
493
494  terminal
495  eval bufnr('%')->term_setrestore('other')
496  mksession! Xtest_mks.out
497  let lines = readfile('Xtest_mks.out')
498  let term_cmd = ''
499  for line in lines
500    if line =~ '^terminal'
501      let term_cmd = line
502    endif
503  endfor
504  call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
505
506  call StopShellInTerminal(bufnr('%'))
507  call delete('Xtest_mks.out')
508endfunc
509
510func Test_mksession_terminal_shared_windows()
511  CheckFeature terminal
512
513  terminal
514  let term_buf = bufnr()
515  new
516  execute "buffer" term_buf
517  mksession! Xtest_mks.out
518
519  let lines = readfile('Xtest_mks.out')
520  let found_creation = 0
521  let found_use = 0
522
523  for line in lines
524    if line =~ '^terminal'
525      let found_creation = 1
526      call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+', line)
527    elseif line =~ "^execute 'buffer ' . s:term_buf_" . term_buf . "$"
528      let found_use = 1
529    endif
530  endfor
531
532  call assert_true(found_creation && found_use)
533
534  call StopShellInTerminal(term_buf)
535  call delete('Xtest_mks.out')
536endfunc
537
538func Test_mkview_terminal_windows()
539  CheckFeature terminal
540
541  " create two window on the same terminal to check this is handled OK
542  terminal
543  let term_buf = bufnr()
544  exe 'sbuf ' .. term_buf
545  mkview! Xtestview
546
547  call StopShellInTerminal(term_buf)
548  call delete('Xtestview')
549endfunc
550
551func Test_mkview_open_folds()
552  enew!
553
554  call append(0, ['a', 'b', 'c'])
555  1,3fold
556  " zR affects 'foldlevel', make sure the option is applied after the folds
557  " have been recreated.
558  normal zR
559  write! Xtestfile
560
561  call assert_equal(-1, foldclosed(1))
562  call assert_equal(-1, foldclosed(2))
563  call assert_equal(-1, foldclosed(3))
564
565  mkview! Xtestview
566  source Xtestview
567
568  call assert_equal(-1, foldclosed(1))
569  call assert_equal(-1, foldclosed(2))
570  call assert_equal(-1, foldclosed(3))
571
572  call delete('Xtestview')
573  call delete('Xtestfile')
574  %bwipe
575endfunc
576
577func Test_mkview_no_balt()
578  edit Xtestfile1
579  edit Xtestfile2
580
581  mkview! Xtestview
582  bdelete Xtestfile1
583
584  source Xtestview
585  call assert_equal(0, buflisted('Xtestfile1'))
586
587  call delete('Xtestview')
588  %bwipe
589endfunc
590
591func Test_mksession_no_balt()
592  edit Xtestfile1
593  edit Xtestfile2
594
595  bdelete Xtestfile1
596  mksession! Xtestview
597
598  source Xtestview
599  call assert_equal(0, buflisted('Xtestfile1'))
600
601  call delete('Xtestview')
602  %bwipe
603endfunc
604
605" Test :mkview with a file argument.
606func Test_mkview_file()
607  " Create a view with line number and a fold.
608  help :mkview
609  set number
610  norm! V}zf0
611  let pos = getpos('.')
612  let linefoldclosed1 = foldclosed('.')
613  mkview! Xview
614  set nonumber
615  norm! zrj
616  " We can close the help window, as mkview with a file name should
617  " generate a command to edit the file.
618  helpclose
619
620  source Xview
621  call assert_equal(1, &number)
622  call assert_match('\*:mkview\*$', getline('.'))
623  call assert_equal(pos, getpos('.'))
624  call assert_equal(linefoldclosed1, foldclosed('.'))
625
626  " Creating a view again with the same file name should fail (file
627  " already exists). But with a !, the previous view should be
628  " overwritten without error.
629  help :loadview
630  call assert_fails('mkview Xview', 'E189:')
631  call assert_match('\*:loadview\*$', getline('.'))
632  mkview! Xview
633  call assert_match('\*:loadview\*$', getline('.'))
634
635  call delete('Xview')
636  bwipe
637endfunc
638
639" Test :mkview and :loadview with a custom 'viewdir'.
640func Test_mkview_loadview_with_viewdir()
641  set viewdir=Xviewdir
642
643  help :mkview
644  set number
645  norm! V}zf
646  let pos = getpos('.')
647  let linefoldclosed1 = foldclosed('.')
648  mkview 1
649  set nonumber
650  norm! zrj
651
652  loadview 1
653
654  " The directory Xviewdir/ should have been created and the view
655  " should be stored in that directory.
656  call assert_equal('Xviewdir/' .
657        \           substitute(
658        \             substitute(
659        \               expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
660        \           glob('Xviewdir/*'))
661  call assert_equal(1, &number)
662  call assert_match('\*:mkview\*$', getline('.'))
663  call assert_equal(pos, getpos('.'))
664  call assert_equal(linefoldclosed1, foldclosed('.'))
665
666  call delete('Xviewdir', 'rf')
667  set viewdir&
668  helpclose
669endfunc
670
671func Test_mkview_no_file_name()
672  new
673  " :mkview or :mkview {nr} should fail in a unnamed buffer.
674  call assert_fails('mkview', 'E32:')
675  call assert_fails('mkview 1', 'E32:')
676
677  " :mkview {file} should succeed in a unnamed buffer.
678  mkview Xview
679  help
680  source Xview
681  call assert_equal('', bufname('%'))
682
683  call delete('Xview')
684  %bwipe
685endfunc
686
687func Test_mkview_loadview_jumplist()
688  set viewdir=Xviewdir
689  au BufWinLeave * silent mkview
690  au BufWinEnter * silent loadview
691
692  edit Xfile1
693  call setline(1, ['a', 'bbbbbbb', 'c'])
694  normal j3l
695  call assert_equal([2, 4], getcurpos()[1:2])
696  write
697
698  edit Xfile2
699  call setline(1, ['d', 'eeeeeee', 'f'])
700  normal j5l
701  call assert_equal([2, 6], getcurpos()[1:2])
702  write
703
704  edit Xfile3
705  call setline(1, ['g', 'h', 'iiiii'])
706  normal jj3l
707  call assert_equal([3, 4], getcurpos()[1:2])
708  write
709
710  edit Xfile1
711  call assert_equal([2, 4], getcurpos()[1:2])
712  edit Xfile2
713  call assert_equal([2, 6], getcurpos()[1:2])
714  edit Xfile3
715  call assert_equal([3, 4], getcurpos()[1:2])
716
717  exe "normal \<C-O>"
718  call assert_equal('Xfile2', expand('%'))
719  call assert_equal([2, 6], getcurpos()[1:2])
720  exe "normal \<C-O>"
721  call assert_equal('Xfile1', expand('%'))
722  call assert_equal([2, 4], getcurpos()[1:2])
723
724  au! BufWinLeave
725  au! BufWinEnter
726  bwipe!
727  call delete('Xviewdir', 'rf')
728  call delete('Xfile1')
729  call delete('Xfile2')
730  call delete('Xfile3')
731  set viewdir&
732endfunc
733
734" A clean session (one empty buffer, one window, and one tab) should not
735" set any error messages when sourced because no commands should fail.
736func Test_mksession_no_errmsg()
737  let v:errmsg = ''
738  %bwipe!
739  mksession! Xtest_mks.out
740  source Xtest_mks.out
741  call assert_equal('', v:errmsg)
742  call delete('Xtest_mks.out')
743endfunc
744
745func Test_mksession_quote_in_filename()
746  " only Unix can handle this weird filename
747  CheckUnix
748
749  let v:errmsg = ''
750  %bwipe!
751  split another
752  split x'y\"z
753  mksession! Xtest_mks_quoted.out
754  %bwipe!
755  source Xtest_mks_quoted.out
756  call assert_true(bufexists("x'y\"z"))
757
758  %bwipe!
759  call delete('Xtest_mks_quoted.out')
760endfunc
761
762" Test for storing global variables in a session file
763func Test_mksession_globals()
764  set sessionoptions+=globals
765
766  " create different global variables
767  let g:Global_string = "Sun is shining\r\n"
768  let g:Global_count = 100
769  let g:Global_pi = 3.14
770  let g:Global_neg_float = -2.68
771
772  mksession! Xtest_mks.out
773
774  unlet g:Global_string
775  unlet g:Global_count
776  unlet g:Global_pi
777  unlet g:Global_neg_float
778
779  source Xtest_mks.out
780  call assert_equal("Sun is shining\r\n", g:Global_string)
781  call assert_equal(100, g:Global_count)
782  call assert_equal(3.14, g:Global_pi)
783  call assert_equal(-2.68, g:Global_neg_float)
784
785  unlet g:Global_string
786  unlet g:Global_count
787  unlet g:Global_pi
788  unlet g:Global_neg_float
789  call delete('Xtest_mks.out')
790  set sessionoptions&
791endfunc
792
793" Test for changing backslash to forward slash in filenames
794func Test_mksession_slash()
795  if exists('+shellslash')
796    throw 'Skipped: cannot use backslash in file name'
797  endif
798  enew
799  %bwipe!
800  e a\\b\\c
801  mksession! Xtest_mks1.out
802  set sessionoptions+=slash
803  mksession! Xtest_mks2.out
804
805  %bwipe!
806  source Xtest_mks1.out
807  call assert_equal('a\b\c', bufname(''))
808  %bwipe!
809  source Xtest_mks2.out
810  call assert_equal('a/b/c', bufname(''))
811
812  %bwipe!
813  call delete('Xtest_mks1.out')
814  call delete('Xtest_mks2.out')
815  set sessionoptions&
816endfunc
817
818" Test for changing directory to the session file directory
819func Test_mksession_sesdir()
820  let save_cwd = getcwd()
821  call mkdir('Xproj')
822  mksession! Xproj/Xtest_mks1.out
823  set sessionoptions-=curdir
824  set sessionoptions+=sesdir
825  mksession! Xproj/Xtest_mks2.out
826
827  source Xproj/Xtest_mks1.out
828  call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
829  source Xproj/Xtest_mks2.out
830  call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
831  call chdir(save_cwd)
832  %bwipe
833
834  set sessionoptions&
835  call delete('Xproj', 'rf')
836endfunc
837
838" Test for storing the 'lines' and 'columns' settings
839func Test_mksession_resize()
840  mksession! Xtest_mks1.out
841  set sessionoptions+=resize
842  mksession! Xtest_mks2.out
843
844  let lines = readfile('Xtest_mks1.out')
845  let found_resize = v:false
846  for line in lines
847    if line =~ '^set lines='
848      let found_resize = v:true
849      break
850    endif
851  endfor
852  call assert_false(found_resize)
853  let lines = readfile('Xtest_mks2.out')
854  let found_resize = v:false
855  for line in lines
856    if line =~ '^set lines='
857      let found_resize = v:true
858      break
859    endif
860  endfor
861  call assert_true(found_resize)
862
863  call delete('Xtest_mks1.out')
864  call delete('Xtest_mks2.out')
865  set sessionoptions&
866endfunc
867
868" Test for mksession with a named scratch buffer
869func Test_mksession_scratch()
870  enew | only
871  file Xscratch
872  set buftype=nofile
873  mksession! Xtest_mks.out
874  %bwipe
875  source Xtest_mks.out
876  call assert_equal('Xscratch', bufname(''))
877  call assert_equal('nofile', &buftype)
878  %bwipe
879  call delete('Xtest_mks.out')
880endfunc
881
882" Test for mksession with fold options
883func Test_mksession_foldopt()
884  set sessionoptions-=options
885  set sessionoptions+=folds
886  new
887  setlocal foldenable
888  setlocal foldmethod=expr
889  setlocal foldmarker=<<<,>>>
890  setlocal foldignore=%
891  setlocal foldlevel=2
892  setlocal foldminlines=10
893  setlocal foldnestmax=15
894  mksession! Xtest_mks.out
895  close
896  %bwipe
897
898  source Xtest_mks.out
899  call assert_true(&foldenable)
900  call assert_equal('expr', &foldmethod)
901  call assert_equal('<<<,>>>', &foldmarker)
902  call assert_equal('%', &foldignore)
903  call assert_equal(2, &foldlevel)
904  call assert_equal(10, &foldminlines)
905  call assert_equal(15, &foldnestmax)
906
907  close
908  %bwipe
909  set sessionoptions&
910endfunc
911
912" Test for mksession with window position
913func Test_mksession_winpos()
914  " Only applicable in GUI Vim
915  CheckGui
916
917  set sessionoptions+=winpos
918  mksession! Xtest_mks.out
919  let found_winpos = v:false
920  let lines = readfile('Xtest_mks.out')
921  for line in lines
922    if line =~ '^winpos '
923      let found_winpos = v:true
924      break
925    endif
926  endfor
927  call assert_true(found_winpos)
928  call delete('Xtest_mks.out')
929  set sessionoptions&
930endfunc
931
932" Test for mksession without options restores winminheight
933func Test_mksession_winminheight()
934  set sessionoptions-=options
935  split
936  mksession! Xtest_mks.out
937  let found_restore = 0
938  let lines = readfile('Xtest_mks.out')
939  for line in lines
940    if line =~ '= s:save_winmin\(width\|height\)'
941      let found_restore += 1
942    endif
943  endfor
944  call assert_equal(2, found_restore)
945  call delete('Xtest_mks.out')
946  close
947  set sessionoptions&
948endfunc
949
950" Test for mksession with 'compatible' option
951func Test_mksession_compatible()
952  mksession! Xtest_mks1.out
953  set compatible
954  mksession! Xtest_mks2.out
955  set nocp
956
957  let test_success = v:false
958  let lines = readfile('Xtest_mks1.out')
959  for line in lines
960    if line =~ '^if &cp | set nocp | endif'
961      let test_success = v:true
962      break
963    endif
964  endfor
965  call assert_true(test_success)
966
967  let test_success = v:false
968  let lines = readfile('Xtest_mks2.out')
969  for line in lines
970    if line =~ '^if !&cp | set cp | endif'
971      let test_success = v:true
972      break
973    endif
974  endfor
975  call assert_true(test_success)
976
977  call delete('Xtest_mks1.out')
978  call delete('Xtest_mks2.out')
979  set compatible&
980  set sessionoptions&
981endfunc
982
983func s:ClearMappings()
984  mapclear
985  omapclear
986  mapclear!
987  lmapclear
988  tmapclear
989endfunc
990
991func Test_mkvimrc()
992  let entries = [
993        \ ['', 'nothing', '<Nop>'],
994        \ ['n', 'normal', 'NORMAL'],
995        \ ['v', 'visual', 'VISUAL'],
996        \ ['s', 'select', 'SELECT'],
997        \ ['x', 'visualonly', 'VISUALONLY'],
998        \ ['o', 'operator', 'OPERATOR'],
999        \ ['i', 'insert', 'INSERT'],
1000        \ ['l', 'lang', 'LANG'],
1001        \ ['c', 'command', 'COMMAND'],
1002        \ ['t', 'terminal', 'TERMINAL'],
1003        \ ]
1004  for entry in entries
1005    exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
1006  endfor
1007
1008  mkvimrc Xtestvimrc
1009
1010  call s:ClearMappings()
1011  for entry in entries
1012    call assert_equal('', maparg(entry[1], entry[0]))
1013  endfor
1014
1015  source Xtestvimrc
1016
1017  for entry in entries
1018    call assert_equal(entry[2], maparg(entry[1], entry[0]))
1019  endfor
1020
1021  call s:ClearMappings()
1022
1023  " the 'pastetoggle', 'wildchar' and 'wildcharm' option values should be
1024  " stored as key names in the vimrc file
1025  set pastetoggle=<F5>
1026  set wildchar=<F6>
1027  set wildcharm=<F7>
1028  call assert_fails('mkvimrc Xtestvimrc')
1029  mkvimrc! Xtestvimrc
1030  call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set pastetoggle=<F5>'))
1031  call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildchar=<F6>'))
1032  call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildcharm=<F7>'))
1033  set pastetoggle& wildchar& wildcharm&
1034
1035  call delete('Xtestvimrc')
1036endfunc
1037
1038func Test_scrolloff()
1039  set sessionoptions+=localoptions
1040  setlocal so=1 siso=1
1041  mksession! Xtest_mks.out
1042  setlocal so=-1 siso=-1
1043  source Xtest_mks.out
1044  call assert_equal(1, &l:so)
1045  call assert_equal(1, &l:siso)
1046  call delete('Xtest_mks.out')
1047  setlocal so& siso&
1048  set sessionoptions&
1049endfunc
1050
1051func Test_altfile()
1052  edit Xone
1053  split Xtwo
1054  edit Xtwoalt
1055  edit #
1056  wincmd w
1057  edit Xonealt
1058  edit #
1059  mksession! Xtest_altfile
1060  only
1061  bwipe Xonealt
1062  bwipe Xtwoalt
1063  bwipe!
1064  source Xtest_altfile
1065  call assert_equal('Xone', bufname())
1066  call assert_equal('Xonealt', bufname('#'))
1067  wincmd w
1068  call assert_equal('Xtwo', bufname())
1069  call assert_equal('Xtwoalt', bufname('#'))
1070  only
1071  bwipe!
1072  call delete('Xtest_altfile')
1073endfunc
1074
1075" Test for creating views with manual folds
1076func Test_mkview_manual_fold()
1077  call writefile(range(1,10), 'Xfile')
1078  new Xfile
1079  " create recursive folds
1080  5,6fold
1081  4,7fold
1082  mkview Xview
1083  normal zE
1084  source Xview
1085  call assert_equal([-1, 4, 4, 4, 4, -1], [foldclosed(3), foldclosed(4),
1086        \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1087  " open one level of fold
1088  4foldopen
1089  mkview! Xview
1090  normal zE
1091  source Xview
1092  call assert_equal([-1, -1, 5, 5, -1, -1], [foldclosed(3), foldclosed(4),
1093        \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1094  " open all the folds
1095  %foldopen!
1096  mkview! Xview
1097  normal zE
1098  source Xview
1099  call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1100        \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1101  call delete('Xfile')
1102  call delete('Xview')
1103  bw!
1104endfunc
1105
1106" vim: shiftwidth=2 sts=2 expandtab
1107