blob: 8217d91b8ca7b2680e1b27f28a74998678b15ec8 [file] [log] [blame]
Bram Moolenaareca626f2016-12-01 18:47:38 +01001" Test for :mksession, :mkview and :loadview in latin1 encoding
2
3set encoding=latin1
4scriptencoding latin1
5
Bram Moolenaarb46fecd2019-06-15 17:58:09 +02006source check.vim
7CheckFeature mksession
Bram Moolenaareca626f2016-12-01 18:47:38 +01008
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01009source shared.vim
Bram Moolenaar7a39dd72019-06-23 00:50:15 +020010source term_util.vim
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010011
Bram Moolenaar1e15e612019-11-09 17:18:52 +010012" 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
zeertzjqa304e492025-06-10 20:31:44 +020035func Test_mksession_arglocal_localdir()
36 call mkdir('Xa', 'R')
37 call writefile(['This is Xb'], 'Xa/Xb.txt', 'D')
38 let olddir = getcwd()
39 let oldargs = argv()
40
41 for tabpage in [v:false, v:true]
42 let msg = tabpage ? 'tabpage-local' : 'window-local'
43
44 exe tabpage ? 'tabnew' : 'botright new'
45 exe tabpage ? 'tcd Xa' : 'lcd Xa'
46 let localdir = getcwd()
47 arglocal
48 $argadd Xb.txt
49 let localargs = argv()
50 exe tabpage ? 'tabprev' : 'wincmd p'
51 call assert_equal(olddir, getcwd(), msg)
52 call assert_equal(oldargs, argv(), msg)
53 mksession! Xtest_mks_localdir.out
54 exe tabpage ? '+tabclose' : '$close'
55 bwipe! Xa/Xb.txt
56
57 source Xtest_mks_localdir.out
58 exe tabpage ? 'tabnext' : 'wincmd b'
59 call assert_equal(localdir, getcwd(), msg)
60 call assert_equal(localargs, argv(), msg)
61 $argument
62 call assert_equal('This is Xb', getline(1), msg)
63
64 bwipe!
65 call assert_equal(olddir, getcwd(), msg)
66 call assert_equal(oldargs, argv(), msg)
67 call delete('Xtest_mks_localdir.out')
68 endfor
69endfunc
70
Bram Moolenaareca626f2016-12-01 18:47:38 +010071func Test_mksession()
72 tabnew
73 let wrap_save = &wrap
74 set sessionoptions=buffers splitbelow fileencoding=latin1
75 call setline(1, [
76 \ 'start:',
77 \ 'no multibyte chAracter',
78 \ ' one leaDing tab',
79 \ ' four leadinG spaces',
80 \ 'two consecutive tabs',
81 \ 'two tabs in one line',
82 \ 'one ä multibyteCharacter',
83 \ 'aä Ä two multiByte characters',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020084 \ 'Aäöü three mulTibyte characters',
85 \ 'short line',
Bram Moolenaareca626f2016-12-01 18:47:38 +010086 \ ])
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010087 let tmpfile = 'Xtemp'
Bram Moolenaareca626f2016-12-01 18:47:38 +010088 exec 'w! ' . tmpfile
89 /^start:
90 set wrap
91 vsplit
92 norm! j16|
93 split
94 norm! j16|
95 split
96 norm! j16|
97 split
98 norm! j8|
99 split
100 norm! j8|
101 split
102 norm! j16|
103 split
104 norm! j16|
105 split
106 norm! j16|
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200107 split
108 norm! j$
Bram Moolenaareca626f2016-12-01 18:47:38 +0100109 wincmd l
110
111 set nowrap
112 /^start:
113 norm! j16|3zl
114 split
115 norm! j016|3zl
116 split
117 norm! j016|3zl
118 split
119 norm! j08|3zl
120 split
121 norm! j08|3zl
122 split
123 norm! j016|3zl
124 split
125 norm! j016|3zl
126 split
127 norm! j016|3zl
128 split
129 call wincol()
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100130 mksession! Xtest_mks.out
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200131 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100132 let expected = [
133 \ 'normal! 016|',
134 \ 'normal! 016|',
135 \ 'normal! 016|',
136 \ 'normal! 08|',
137 \ 'normal! 08|',
138 \ 'normal! 016|',
139 \ 'normal! 016|',
140 \ 'normal! 016|',
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200141 \ 'normal! $',
Bram Moolenaareca626f2016-12-01 18:47:38 +0100142 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
143 \ " normal! 016|",
144 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
145 \ " normal! 016|",
146 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
147 \ " normal! 016|",
148 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
149 \ " normal! 08|",
150 \ " exe 'normal! ' . s:c . '|zs' . 8 . '|'",
151 \ " normal! 08|",
152 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
153 \ " normal! 016|",
154 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
155 \ " normal! 016|",
156 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
157 \ " normal! 016|",
158 \ " exe 'normal! ' . s:c . '|zs' . 16 . '|'",
159 \ " normal! 016|"
160 \ ]
161 call assert_equal(expected, li)
162 tabclose!
163
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100164 call delete('Xtest_mks.out')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100165 call delete(tmpfile)
166 let &wrap = wrap_save
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100167 set sessionoptions&
Bram Moolenaareca626f2016-12-01 18:47:38 +0100168endfunc
169
Bram Moolenaar635bd602021-04-16 19:58:22 +0200170def Test_mksession_skiprtp()
171 mksession! Xtest_mks.out
Bram Moolenaard23b7142021-04-17 21:04:34 +0200172 var found_rtp = 0
173 var found_pp = 0
Bram Moolenaar635bd602021-04-16 19:58:22 +0200174 for line in readfile('Xtest_mks.out')
175 if line =~ 'set runtimepath'
Bram Moolenaard23b7142021-04-17 21:04:34 +0200176 found_rtp += 1
177 endif
178 if line =~ 'set packpath'
179 found_pp += 1
Bram Moolenaar635bd602021-04-16 19:58:22 +0200180 endif
181 endfor
Bram Moolenaard23b7142021-04-17 21:04:34 +0200182 assert_equal(1, found_rtp)
183 assert_equal(1, found_pp)
Bram Moolenaar635bd602021-04-16 19:58:22 +0200184 delete('Xtest_mks.out')
185
186 set sessionoptions+=skiprtp
187 mksession! Xtest_mks.out
Bram Moolenaard23b7142021-04-17 21:04:34 +0200188 var found = 0
Bram Moolenaar635bd602021-04-16 19:58:22 +0200189 for line in readfile('Xtest_mks.out')
Bram Moolenaard23b7142021-04-17 21:04:34 +0200190 if line =~ 'set \(runtimepath\|packpath\)'
Bram Moolenaar635bd602021-04-16 19:58:22 +0200191 found = 1
192 break
193 endif
194 endfor
195 assert_equal(0, found)
196 delete('Xtest_mks.out')
197 set sessionoptions&
198enddef
199
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100200func Test_mksession_winheight()
201 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200202 set winheight=10
203 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100204 mksession! Xtest_mks.out
205 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100206
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100207 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100208endfunc
209
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200210func Test_mksession_large_winheight()
211 set winheight=999
212 mksession! Xtest_mks_winheight.out
213 set winheight&
214 source Xtest_mks_winheight.out
215 call delete('Xtest_mks_winheight.out')
216endfunc
217
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200218func Test_mksession_zero_winheight()
219 set winminheight=0
220 edit SomeFile
221 split
222 wincmd _
223 mksession! Xtest_mks_zero
224 set winminheight&
225 let text = readfile('Xtest_mks_zero')->join()
Dominique Pelle5f8ed742021-05-03 19:08:37 +0200226 call delete('Xtest_mks_zero')
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200227 close
Dominique Pelle5f8ed742021-05-03 19:08:37 +0200228 " check there is no divide by zero
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200229 call assert_notmatch('/ 0[^0-9]', text)
230endfunc
231
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100232func Test_mksession_rtp()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200233 " TODO: fix problem with backslashes on Win32
234 CheckNotMSWindows
235
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100236 new
237 let _rtp=&rtp
238 " Make a real long (invalid) runtimepath value,
239 " that should exceed PATH_MAX (hopefully)
240 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
241 let newrtp.=",".expand("$HOME")."/.vim"
242 let &rtp=newrtp
243
244 " determine expected value
245 let expected=split(&rtp, ',')
246 let expected = map(expected, '"set runtimepath+=".v:val')
247 let expected = ['set runtimepath='] + expected
248 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
249
250 mksession! Xtest_mks.out
251 let &rtp=_rtp
252 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
253 call assert_equal(expected, li)
254
255 call delete('Xtest_mks.out')
256endfunc
257
Bram Moolenaar79da5632017-02-01 22:52:44 +0100258func Test_mksession_arglist()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200259 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100260 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200261 new
262 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100263 mksession! Xtest_mks.out
264 source Xtest_mks.out
265 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200266 call assert_equal(2, argidx())
267 wincmd w
268 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100269
270 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200271 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100272 argdel *
273endfunc
274
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200275func Test_mksession_one_buffer_two_windows()
276 edit Xtest1
277 new Xtest2
278 split
279 mksession! Xtest_mks.out
280 let lines = readfile('Xtest_mks.out')
281 let count1 = 0
282 let count2 = 0
283 let count2buf = 0
James Cherti7d428402022-03-14 20:24:51 +0000284 let bufexists = 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200285 for line in lines
286 if line =~ 'edit \f*Xtest1$'
287 let count1 += 1
288 endif
289 if line =~ 'edit \f\{-}Xtest2'
290 let count2 += 1
291 endif
292 if line =~ 'buffer \f\{-}Xtest2'
293 let count2buf += 1
294 endif
James Cherti7d428402022-03-14 20:24:51 +0000295 if line =~ 'bufexists(fnamemodify(.*, ":p")'
296 let bufexists += 1
297 endif
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200298 endfor
299 call assert_equal(1, count1, 'Xtest1 count')
300 call assert_equal(2, count2, 'Xtest2 count')
301 call assert_equal(2, count2buf, 'Xtest2 buffer count')
James Cherti7d428402022-03-14 20:24:51 +0000302 call assert_equal(2, bufexists)
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200303
304 close
305 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200306 call delete('Xtest_mks.out')
307endfunc
308
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200309func Test_mksession_lcd_multiple_tabs()
310 tabnew
311 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200312 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200313 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200314 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200315 mksession! Xtest_mks.out
316 tabonly
317 source Xtest_mks.out
318 call assert_true(haslocaldir(), 'Tab 1 localdir')
319 tabnext 2
320 call assert_true(!haslocaldir(), 'Tab 2 localdir')
321 tabnext 3
322 call assert_true(haslocaldir(), 'Tab 3 localdir')
323 call delete('Xtest_mks.out')
324endfunc
325
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200326" Test for tabpage-local directory
327func Test_mksession_tcd_multiple_tabs()
328 let save_cwd = getcwd()
329 call mkdir('Xtopdir')
330 cd Xtopdir
331 call mkdir('Xtabdir1')
332 call mkdir('Xtabdir2')
333 call mkdir('Xtabdir3')
334 call mkdir('Xwindir1')
335 call mkdir('Xwindir2')
336 call mkdir('Xwindir3')
337 tcd Xtabdir1
338 botright new
339 wincmd t
340 lcd ../Xwindir1
341 tabnew
342 tcd ../Xtabdir2
343 botright new
344 lcd ../Xwindir2
345 tabnew
346 tcd ../Xtabdir3
347 botright new
348 lcd ../Xwindir3
349 tabfirst
350 1wincmd w
351 mksession! Xtest_mks.out
352 only | tabonly
353 source Xtest_mks.out
354 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
355 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
356 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
357 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
358 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
359 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
360 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
361 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
362 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100363 %bwipe
364 call chdir(save_cwd)
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200365 call delete("Xtopdir", "rf")
366endfunc
367
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200368func Test_mksession_blank_tabs()
369 tabnew
370 tabnew
371 tabnew
372 tabnext 3
373 mksession! Xtest_mks.out
374 tabnew
375 tabnew
376 tabnext 2
377 source Xtest_mks.out
378 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
379 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
380 call delete('Xtest_mks.out')
381endfunc
382
Bram Moolenaard39e2752019-01-26 20:07:38 +0100383func Test_mksession_buffer_count()
384 set hidden
385
386 " Edit exactly three files in the current session.
387 %bwipe!
388 e Xfoo | tabe Xbar | tabe Xbaz
389 tabdo write
390 mksession! Xtest_mks.out
391
392 " Verify that loading the session does not create additional buffers.
393 %bwipe!
394 source Xtest_mks.out
395 call assert_equal(3, len(getbufinfo()))
396
397 " Clean up.
398 call delete('Xfoo')
399 call delete('Xbar')
400 call delete('Xbaz')
401 call delete('Xtest_mks.out')
402 %bwipe!
403 set hidden&
404endfunc
405
Evgeni Chasnovski26ebf1f2022-01-14 13:19:43 +0000406func Test_mksession_buffer_order()
407 %bwipe!
408 e Xfoo | e Xbar | e Xbaz | e Xqux
409 bufdo write
410 mksession! Xtest_mks.out
411
412 " Verify that loading the session preserves order of buffers
413 %bwipe!
414 source Xtest_mks.out
415
416 let s:buf_info = getbufinfo()
417 call assert_true(s:buf_info[0]['name'] =~# 'Xfoo$')
418 call assert_true(s:buf_info[1]['name'] =~# 'Xbar$')
419 call assert_true(s:buf_info[2]['name'] =~# 'Xbaz$')
420 call assert_true(s:buf_info[3]['name'] =~# 'Xqux$')
421
422 " Clean up.
423 call delete('Xfoo')
424 call delete('Xbar')
425 call delete('Xbaz')
426 call delete('Xqux')
427 call delete('Xtest_mks.out')
428 %bwipe!
429endfunc
430
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100431if has('extra_search')
432
433func Test_mksession_hlsearch()
434 set hlsearch
435 mksession! Xtest_mks.out
436 nohlsearch
437 source Xtest_mks.out
438 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
439 nohlsearch
440 mksession! Xtest_mks.out
441 source Xtest_mks.out
442 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
443 call delete('Xtest_mks.out')
444endfunc
445
446endif
447
448
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200449func Test_mksession_blank_windows()
450 split
451 split
452 split
453 3 wincmd w
454 mksession! Xtest_mks.out
455 split
456 split
457 2 wincmd w
458 source Xtest_mks.out
459 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
460 call assert_equal(3, winnr(), 'session restore should restore the active window')
461 call delete('Xtest_mks.out')
462endfunc
463
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100464func Test_mksession_terminal_shell()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200465 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100466 CheckFeature quickfix
467
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100468 terminal
469 mksession! Xtest_mks.out
470 let lines = readfile('Xtest_mks.out')
471 let term_cmd = ''
472 for line in lines
473 if line =~ '^terminal'
474 let term_cmd = line
475 elseif line =~ 'badd.*' . &shell
476 call assert_report('unexpected shell line: ' . line)
477 endif
478 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100479 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100480
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200481 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100482 call delete('Xtest_mks.out')
483endfunc
484
485func Test_mksession_terminal_no_restore_cmdarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200486 CheckFeature terminal
487
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100488 terminal ++norestore
489 mksession! Xtest_mks.out
490 let lines = readfile('Xtest_mks.out')
491 let term_cmd = ''
492 for line in lines
493 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100494 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100495 endif
496 endfor
497
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200498 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100499 call delete('Xtest_mks.out')
500endfunc
501
502func Test_mksession_terminal_no_restore_funcarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200503 CheckFeature terminal
504
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200505 let buf = Run_shell_in_terminal({'norestore': 1})
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100506 mksession! Xtest_mks.out
507 let lines = readfile('Xtest_mks.out')
508 let term_cmd = ''
509 for line in lines
510 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100511 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100512 endif
513 endfor
514
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200515 call StopShellInTerminal(buf)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100516 call delete('Xtest_mks.out')
517endfunc
518
519func Test_mksession_terminal_no_restore_func()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200520 CheckFeature terminal
521
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100522 terminal
523 call term_setrestore(bufnr('%'), 'NONE')
524 mksession! Xtest_mks.out
525 let lines = readfile('Xtest_mks.out')
526 let term_cmd = ''
527 for line in lines
528 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100529 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100530 endif
531 endfor
532
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200533 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100534 call delete('Xtest_mks.out')
535endfunc
536
537func Test_mksession_terminal_no_ssop()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200538 CheckFeature terminal
539
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100540 terminal
541 set sessionoptions-=terminal
542 mksession! Xtest_mks.out
543 let lines = readfile('Xtest_mks.out')
544 let term_cmd = ''
545 for line in lines
546 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100547 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100548 endif
549 endfor
550
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200551 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100552 call delete('Xtest_mks.out')
553 set sessionoptions&
554endfunc
555
556func Test_mksession_terminal_restore_other()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200557 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100558 CheckFeature quickfix
559
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100560 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200561 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100562 mksession! Xtest_mks.out
563 let lines = readfile('Xtest_mks.out')
564 let term_cmd = ''
565 for line in lines
566 if line =~ '^terminal'
567 let term_cmd = line
568 endif
569 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100570 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100571
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200572 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100573 call delete('Xtest_mks.out')
574endfunc
575
Bram Moolenaar0e655112020-09-11 20:36:36 +0200576func Test_mksession_terminal_shared_windows()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200577 CheckFeature terminal
578
Bram Moolenaar0e655112020-09-11 20:36:36 +0200579 terminal
580 let term_buf = bufnr()
581 new
582 execute "buffer" term_buf
583 mksession! Xtest_mks.out
584
585 let lines = readfile('Xtest_mks.out')
586 let found_creation = 0
587 let found_use = 0
588
589 for line in lines
590 if line =~ '^terminal'
591 let found_creation = 1
592 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+', line)
593 elseif line =~ "^execute 'buffer ' . s:term_buf_" . term_buf . "$"
594 let found_use = 1
595 endif
596 endfor
597
598 call assert_true(found_creation && found_use)
599
600 call StopShellInTerminal(term_buf)
601 call delete('Xtest_mks.out')
602endfunc
603
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200604func Test_mkview_terminal_windows()
605 CheckFeature terminal
606
Christian Brabandtee17b6f2023-09-09 11:23:50 +0200607 " create two windows on the same terminal to check this is handled OK
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200608 terminal
609 let term_buf = bufnr()
610 exe 'sbuf ' .. term_buf
611 mkview! Xtestview
612
613 call StopShellInTerminal(term_buf)
614 call delete('Xtestview')
615endfunc
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200616
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100617func Test_mkview_open_folds()
618 enew!
619
620 call append(0, ['a', 'b', 'c'])
621 1,3fold
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100622 write! Xtestfile
623
James McCoyc829faa2022-06-19 17:16:18 +0100624 call assert_notequal(-1, foldclosed(1))
625 call assert_notequal(-1, foldclosed(2))
626 call assert_notequal(-1, foldclosed(3))
627
628 " Save the view with folds closed
629 mkview! Xtestview
630
631 " zR affects 'foldlevel', make sure the option is applied after the folds
632 " have been recreated.
633 " Open folds to ensure they get closed when restoring the view
634 normal zR
635
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100636 call assert_equal(-1, foldclosed(1))
637 call assert_equal(-1, foldclosed(2))
638 call assert_equal(-1, foldclosed(3))
639
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100640 source Xtestview
641
James McCoyc829faa2022-06-19 17:16:18 +0100642 call assert_notequal(-1, foldclosed(1))
643 call assert_notequal(-1, foldclosed(2))
644 call assert_notequal(-1, foldclosed(3))
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100645
646 call delete('Xtestview')
647 call delete('Xtestfile')
648 %bwipe
649endfunc
650
Bram Moolenaar139348f2021-02-05 21:55:53 +0100651func Test_mkview_no_balt()
652 edit Xtestfile1
653 edit Xtestfile2
654
655 mkview! Xtestview
656 bdelete Xtestfile1
657
658 source Xtestview
659 call assert_equal(0, buflisted('Xtestfile1'))
660
661 call delete('Xtestview')
662 %bwipe
663endfunc
664
Bram Moolenaar0756f752021-03-13 13:52:33 +0100665func Test_mksession_no_balt()
666 edit Xtestfile1
667 edit Xtestfile2
668
669 bdelete Xtestfile1
670 mksession! Xtestview
671
672 source Xtestview
673 call assert_equal(0, buflisted('Xtestfile1'))
674
675 call delete('Xtestview')
676 %bwipe
677endfunc
678
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200679" Test :mkview with a file argument.
680func Test_mkview_file()
681 " Create a view with line number and a fold.
682 help :mkview
683 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200684 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200685 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200686 let linefoldclosed1 = foldclosed('.')
687 mkview! Xview
688 set nonumber
689 norm! zrj
690 " We can close the help window, as mkview with a file name should
691 " generate a command to edit the file.
692 helpclose
693
694 source Xview
695 call assert_equal(1, &number)
696 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200697 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200698 call assert_equal(linefoldclosed1, foldclosed('.'))
699
700 " Creating a view again with the same file name should fail (file
701 " already exists). But with a !, the previous view should be
702 " overwritten without error.
703 help :loadview
704 call assert_fails('mkview Xview', 'E189:')
705 call assert_match('\*:loadview\*$', getline('.'))
706 mkview! Xview
707 call assert_match('\*:loadview\*$', getline('.'))
708
709 call delete('Xview')
710 bwipe
711endfunc
712
713" Test :mkview and :loadview with a custom 'viewdir'.
714func Test_mkview_loadview_with_viewdir()
715 set viewdir=Xviewdir
716
717 help :mkview
718 set number
719 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200720 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200721 let linefoldclosed1 = foldclosed('.')
722 mkview 1
723 set nonumber
724 norm! zrj
725
726 loadview 1
727
728 " The directory Xviewdir/ should have been created and the view
729 " should be stored in that directory.
730 call assert_equal('Xviewdir/' .
731 \ substitute(
732 \ substitute(
733 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
734 \ glob('Xviewdir/*'))
735 call assert_equal(1, &number)
736 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200737 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200738 call assert_equal(linefoldclosed1, foldclosed('.'))
739
740 call delete('Xviewdir', 'rf')
741 set viewdir&
742 helpclose
743endfunc
744
745func Test_mkview_no_file_name()
746 new
zeertzjqc029c132024-03-28 11:37:26 +0100747 " :mkview or :mkview {nr} should fail in an unnamed buffer.
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200748 call assert_fails('mkview', 'E32:')
749 call assert_fails('mkview 1', 'E32:')
750
zeertzjqc029c132024-03-28 11:37:26 +0100751 " :mkview {file} should succeed in an unnamed buffer.
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200752 mkview Xview
753 help
754 source Xview
755 call assert_equal('', bufname('%'))
756
757 call delete('Xview')
758 %bwipe
759endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100760
Bram Moolenaar3482be62020-11-27 11:00:38 +0100761func Test_mkview_loadview_jumplist()
762 set viewdir=Xviewdir
763 au BufWinLeave * silent mkview
764 au BufWinEnter * silent loadview
765
766 edit Xfile1
767 call setline(1, ['a', 'bbbbbbb', 'c'])
768 normal j3l
769 call assert_equal([2, 4], getcurpos()[1:2])
770 write
771
772 edit Xfile2
773 call setline(1, ['d', 'eeeeeee', 'f'])
774 normal j5l
775 call assert_equal([2, 6], getcurpos()[1:2])
776 write
777
778 edit Xfile3
779 call setline(1, ['g', 'h', 'iiiii'])
780 normal jj3l
781 call assert_equal([3, 4], getcurpos()[1:2])
782 write
783
784 edit Xfile1
785 call assert_equal([2, 4], getcurpos()[1:2])
786 edit Xfile2
787 call assert_equal([2, 6], getcurpos()[1:2])
788 edit Xfile3
789 call assert_equal([3, 4], getcurpos()[1:2])
790
791 exe "normal \<C-O>"
792 call assert_equal('Xfile2', expand('%'))
793 call assert_equal([2, 6], getcurpos()[1:2])
794 exe "normal \<C-O>"
795 call assert_equal('Xfile1', expand('%'))
796 call assert_equal([2, 4], getcurpos()[1:2])
797
798 au! BufWinLeave
799 au! BufWinEnter
800 bwipe!
801 call delete('Xviewdir', 'rf')
802 call delete('Xfile1')
803 call delete('Xfile2')
804 call delete('Xfile3')
805 set viewdir&
806endfunc
807
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100808" A clean session (one empty buffer, one window, and one tab) should not
809" set any error messages when sourced because no commands should fail.
810func Test_mksession_no_errmsg()
811 let v:errmsg = ''
812 %bwipe!
813 mksession! Xtest_mks.out
814 source Xtest_mks.out
815 call assert_equal('', v:errmsg)
816 call delete('Xtest_mks.out')
817endfunc
818
Bram Moolenaarad36a352019-01-24 13:34:42 +0100819func Test_mksession_quote_in_filename()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200820 " only Unix can handle this weird filename
821 CheckUnix
822
Bram Moolenaarad36a352019-01-24 13:34:42 +0100823 let v:errmsg = ''
824 %bwipe!
825 split another
826 split x'y\"z
827 mksession! Xtest_mks_quoted.out
828 %bwipe!
829 source Xtest_mks_quoted.out
830 call assert_true(bufexists("x'y\"z"))
831
832 %bwipe!
833 call delete('Xtest_mks_quoted.out')
834endfunc
835
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200836" Test for storing global variables in a session file
837func Test_mksession_globals()
838 set sessionoptions+=globals
839
840 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200841 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200842 let g:Global_count = 100
843 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200844 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200845
846 mksession! Xtest_mks.out
847
848 unlet g:Global_string
849 unlet g:Global_count
850 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200851 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200852
853 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200854 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200855 call assert_equal(100, g:Global_count)
856 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200857 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200858
859 unlet g:Global_string
860 unlet g:Global_count
861 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200862 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200863 call delete('Xtest_mks.out')
864 set sessionoptions&
865endfunc
866
867" Test for changing backslash to forward slash in filenames
868func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200869 if exists('+shellslash')
870 throw 'Skipped: cannot use backslash in file name'
871 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200872 enew
873 %bwipe!
874 e a\\b\\c
875 mksession! Xtest_mks1.out
876 set sessionoptions+=slash
877 mksession! Xtest_mks2.out
878
879 %bwipe!
880 source Xtest_mks1.out
881 call assert_equal('a\b\c', bufname(''))
882 %bwipe!
883 source Xtest_mks2.out
884 call assert_equal('a/b/c', bufname(''))
885
886 %bwipe!
887 call delete('Xtest_mks1.out')
888 call delete('Xtest_mks2.out')
889 set sessionoptions&
890endfunc
891
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200892" Test for changing directory to the session file directory
893func Test_mksession_sesdir()
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100894 let save_cwd = getcwd()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200895 call mkdir('Xproj')
896 mksession! Xproj/Xtest_mks1.out
897 set sessionoptions-=curdir
898 set sessionoptions+=sesdir
899 mksession! Xproj/Xtest_mks2.out
900
901 source Xproj/Xtest_mks1.out
902 call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
903 source Xproj/Xtest_mks2.out
904 call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100905 call chdir(save_cwd)
906 %bwipe
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200907
908 set sessionoptions&
909 call delete('Xproj', 'rf')
910endfunc
911
LemonBoyd7c95642022-04-30 16:10:27 +0100912" Test for saving and restoring the tab-local working directory when there is
913" only a single tab and 'tabpages' is not in 'sessionoptions'.
914func Test_mksession_tcd_single_tabs()
915 only | tabonly
916
917 let save_cwd = getcwd()
918 set sessionoptions-=tabpages
919 set sessionoptions+=curdir
920 call mkdir('Xtopdir1')
921 call mkdir('Xtopdir2')
922
923 " There are two tab pages, the current one has local cwd set to 'Xtopdir2'.
924 exec 'tcd ' .. save_cwd .. '/Xtopdir1'
925 tabnew
926 exec 'tcd ' .. save_cwd .. '/Xtopdir2'
927 mksession! Xtest_tcd_single
928
929 source Xtest_tcd_single
930 call assert_equal(2, haslocaldir())
931 call assert_equal('Xtopdir2', fnamemodify(getcwd(-1, 0), ':t'))
932 %bwipe
933
934 set sessionoptions&
935 call chdir(save_cwd)
936 call delete('Xtopdir1', 'rf')
937 call delete('Xtopdir2', 'rf')
938endfunc
939
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200940" Test for storing the 'lines' and 'columns' settings
941func Test_mksession_resize()
942 mksession! Xtest_mks1.out
943 set sessionoptions+=resize
944 mksession! Xtest_mks2.out
945
946 let lines = readfile('Xtest_mks1.out')
947 let found_resize = v:false
948 for line in lines
949 if line =~ '^set lines='
950 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200951 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200952 endif
953 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200954 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200955 let lines = readfile('Xtest_mks2.out')
956 let found_resize = v:false
957 for line in lines
958 if line =~ '^set lines='
959 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200960 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200961 endif
962 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200963 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200964
965 call delete('Xtest_mks1.out')
966 call delete('Xtest_mks2.out')
967 set sessionoptions&
968endfunc
969
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200970" Test for mksession with a named scratch buffer
971func Test_mksession_scratch()
972 enew | only
973 file Xscratch
974 set buftype=nofile
975 mksession! Xtest_mks.out
976 %bwipe
977 source Xtest_mks.out
978 call assert_equal('Xscratch', bufname(''))
979 call assert_equal('nofile', &buftype)
980 %bwipe
981 call delete('Xtest_mks.out')
982endfunc
983
984" Test for mksession with fold options
985func Test_mksession_foldopt()
986 set sessionoptions-=options
987 set sessionoptions+=folds
988 new
989 setlocal foldenable
990 setlocal foldmethod=expr
991 setlocal foldmarker=<<<,>>>
992 setlocal foldignore=%
993 setlocal foldlevel=2
994 setlocal foldminlines=10
995 setlocal foldnestmax=15
996 mksession! Xtest_mks.out
997 close
998 %bwipe
999
1000 source Xtest_mks.out
1001 call assert_true(&foldenable)
1002 call assert_equal('expr', &foldmethod)
1003 call assert_equal('<<<,>>>', &foldmarker)
1004 call assert_equal('%', &foldignore)
1005 call assert_equal(2, &foldlevel)
1006 call assert_equal(10, &foldminlines)
1007 call assert_equal(15, &foldnestmax)
1008
1009 close
1010 %bwipe
1011 set sessionoptions&
Christian Brabandt84bc00e2023-07-13 11:45:54 +02001012 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001013endfunc
1014
matveyt8e7d9db2022-01-05 14:01:30 +00001015" Test for mksession with "help" but not "options" in 'sessionoptions'
1016func Test_mksession_help_noopt()
1017 set sessionoptions-=options
1018 set sessionoptions+=help
1019 help
1020 let fname = expand('%')
1021 mksession! Xtest_mks.out
1022 bwipe
1023
1024 source Xtest_mks.out
1025 call assert_equal('help', &buftype)
1026 call assert_equal('help', &filetype)
1027 call assert_equal(fname, expand('%'))
1028 call assert_false(&modifiable)
1029 call assert_true(&readonly)
1030
1031 helpclose
1032 help index
1033 let fname = expand('%')
1034 mksession! Xtest_mks.out
1035 bwipe
1036
1037 source Xtest_mks.out
1038 call assert_equal('help', &buftype)
1039 call assert_equal(fname, expand('%'))
1040
1041 call delete('Xtest_mks.out')
1042 set sessionoptions&
1043endfunc
1044
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001045" Test for mksession with window position
1046func Test_mksession_winpos()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001047 " Only applicable in GUI Vim
1048 CheckGui
1049
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001050 set sessionoptions+=winpos
1051 mksession! Xtest_mks.out
1052 let found_winpos = v:false
1053 let lines = readfile('Xtest_mks.out')
1054 for line in lines
1055 if line =~ '^winpos '
1056 let found_winpos = v:true
1057 break
1058 endif
1059 endfor
1060 call assert_true(found_winpos)
1061 call delete('Xtest_mks.out')
1062 set sessionoptions&
1063endfunc
1064
Bram Moolenaar0995c812021-04-17 18:38:54 +02001065" Test for mksession without options restores winminheight
1066func Test_mksession_winminheight()
1067 set sessionoptions-=options
1068 split
1069 mksession! Xtest_mks.out
1070 let found_restore = 0
1071 let lines = readfile('Xtest_mks.out')
1072 for line in lines
1073 if line =~ '= s:save_winmin\(width\|height\)'
1074 let found_restore += 1
1075 endif
1076 endfor
1077 call assert_equal(2, found_restore)
1078 call delete('Xtest_mks.out')
1079 close
1080 set sessionoptions&
1081endfunc
1082
James Chertifd012802022-03-29 12:02:57 +01001083" Test for mksession with and without options restores shortmess
1084func Test_mksession_shortmess()
1085 " Without options
1086 set sessionoptions-=options
1087 split
1088 mksession! Xtest_mks.out
1089 let found_save = 0
1090 let found_restore = 0
1091 let lines = readfile('Xtest_mks.out')
1092 for line in lines
1093 let line = trim(line)
1094
1095 if line ==# 'let s:shortmess_save = &shortmess'
1096 let found_save += 1
1097 endif
1098
1099 if found_save !=# 0 && line ==# 'let &shortmess = s:shortmess_save'
1100 let found_restore += 1
1101 endif
1102 endfor
1103 call assert_equal(1, found_save)
1104 call assert_equal(1, found_restore)
1105 call delete('Xtest_mks.out')
1106 close
1107 set sessionoptions&
1108
1109 " With options
1110 set sessionoptions+=options
1111 split
1112 mksession! Xtest_mks.out
1113 let found_restore = 0
1114 let lines = readfile('Xtest_mks.out')
1115 for line in lines
1116 if line =~# 's:shortmess_save'
1117 let found_restore += 1
1118 endif
1119 endfor
1120 call assert_equal(0, found_restore)
1121 call delete('Xtest_mks.out')
1122 close
1123 set sessionoptions&
1124endfunc
1125
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +01001126" Test that when Vim loading session has 'A' in 'shortmess' it does not
1127" complain about an existing swapfile.
1128func Test_mksession_shortmess_with_A()
1129 edit Xtestfile
1130 write
1131 let fname = swapname('%')
1132 let cont = readblob(fname)
1133 set sessionoptions-=options
1134 mksession Xtestsession
1135 bwipe!
1136
1137 " Recreate the swap file to pretend the file is being edited
Bram Moolenaarb152b6a2022-09-29 21:37:33 +01001138 call writefile(cont, fname, 'D')
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +01001139 set shortmess+=A
1140 source Xtestsession
1141
1142 set shortmess&
1143 set sessionoptions&
1144 call delete('Xtestsession')
Christian Brabandt84bc00e2023-07-13 11:45:54 +02001145 call delete('Xtestfile')
Bram Moolenaaraaadb5b2022-05-18 22:07:47 +01001146endfunc
1147
Bram Moolenaaree1a2b52019-09-29 14:23:39 +02001148" Test for mksession with 'compatible' option
1149func Test_mksession_compatible()
1150 mksession! Xtest_mks1.out
1151 set compatible
1152 mksession! Xtest_mks2.out
1153 set nocp
1154
1155 let test_success = v:false
1156 let lines = readfile('Xtest_mks1.out')
1157 for line in lines
1158 if line =~ '^if &cp | set nocp | endif'
1159 let test_success = v:true
1160 break
1161 endif
1162 endfor
1163 call assert_true(test_success)
1164
1165 let test_success = v:false
1166 let lines = readfile('Xtest_mks2.out')
1167 for line in lines
1168 if line =~ '^if !&cp | set cp | endif'
1169 let test_success = v:true
1170 break
1171 endif
1172 endfor
1173 call assert_true(test_success)
1174
1175 call delete('Xtest_mks1.out')
1176 call delete('Xtest_mks2.out')
1177 set compatible&
1178 set sessionoptions&
1179endfunc
1180
Bram Moolenaar87500262019-08-01 23:05:49 +02001181func s:ClearMappings()
1182 mapclear
1183 omapclear
1184 mapclear!
1185 lmapclear
1186 tmapclear
1187endfunc
1188
1189func Test_mkvimrc()
1190 let entries = [
1191 \ ['', 'nothing', '<Nop>'],
zeertzjq9ff1e592025-05-23 17:16:17 +02001192 \ ['n', 'normal', 'NORMAL<Up>'],
1193 \ ['v', 'visual', 'VISUAL<S-Down>'],
1194 \ ['s', 'select', 'SELECT<C-Left>'],
1195 \ ['x', 'visualonly', 'VISUALONLY<M-Right>'],
Bram Moolenaar87500262019-08-01 23:05:49 +02001196 \ ['o', 'operator', 'OPERATOR'],
1197 \ ['i', 'insert', 'INSERT'],
1198 \ ['l', 'lang', 'LANG'],
1199 \ ['c', 'command', 'COMMAND'],
1200 \ ['t', 'terminal', 'TERMINAL'],
1201 \ ]
1202 for entry in entries
1203 exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
1204 endfor
1205
1206 mkvimrc Xtestvimrc
1207
1208 call s:ClearMappings()
1209 for entry in entries
1210 call assert_equal('', maparg(entry[1], entry[0]))
1211 endfor
1212
1213 source Xtestvimrc
1214
1215 for entry in entries
1216 call assert_equal(entry[2], maparg(entry[1], entry[0]))
1217 endfor
1218
1219 call s:ClearMappings()
Yegappan Lakshmanan59585492021-06-12 13:46:41 +02001220
1221 " the 'pastetoggle', 'wildchar' and 'wildcharm' option values should be
1222 " stored as key names in the vimrc file
1223 set pastetoggle=<F5>
1224 set wildchar=<F6>
1225 set wildcharm=<F7>
1226 call assert_fails('mkvimrc Xtestvimrc')
1227 mkvimrc! Xtestvimrc
1228 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set pastetoggle=<F5>'))
1229 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildchar=<F6>'))
1230 call assert_notequal(-1, index(readfile('Xtestvimrc'), 'set wildcharm=<F7>'))
1231 set pastetoggle& wildchar& wildcharm&
1232
Bram Moolenaar87500262019-08-01 23:05:49 +02001233 call delete('Xtestvimrc')
1234endfunc
1235
Bram Moolenaar38890832020-11-01 17:40:54 +01001236func Test_scrolloff()
1237 set sessionoptions+=localoptions
1238 setlocal so=1 siso=1
1239 mksession! Xtest_mks.out
1240 setlocal so=-1 siso=-1
1241 source Xtest_mks.out
1242 call assert_equal(1, &l:so)
1243 call assert_equal(1, &l:siso)
1244 call delete('Xtest_mks.out')
1245 setlocal so& siso&
1246 set sessionoptions&
1247endfunc
1248
Bram Moolenaar3482be62020-11-27 11:00:38 +01001249func Test_altfile()
1250 edit Xone
1251 split Xtwo
1252 edit Xtwoalt
1253 edit #
1254 wincmd w
1255 edit Xonealt
1256 edit #
1257 mksession! Xtest_altfile
1258 only
1259 bwipe Xonealt
1260 bwipe Xtwoalt
1261 bwipe!
1262 source Xtest_altfile
1263 call assert_equal('Xone', bufname())
1264 call assert_equal('Xonealt', bufname('#'))
1265 wincmd w
1266 call assert_equal('Xtwo', bufname())
1267 call assert_equal('Xtwoalt', bufname('#'))
1268 only
1269 bwipe!
Bram Moolenaarac665c22020-12-08 20:39:15 +01001270 call delete('Xtest_altfile')
Bram Moolenaar3482be62020-11-27 11:00:38 +01001271endfunc
1272
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001273" Test for creating views with manual folds
1274func Test_mkview_manual_fold()
Bram Moolenaarb152b6a2022-09-29 21:37:33 +01001275 call writefile(range(1,10), 'Xmkvfile', 'D')
Bram Moolenaarb18b4962022-09-02 21:55:50 +01001276 new Xmkvfile
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001277 " create recursive folds
1278 5,6fold
1279 4,7fold
1280 mkview Xview
1281 normal zE
1282 source Xview
1283 call assert_equal([-1, 4, 4, 4, 4, -1], [foldclosed(3), foldclosed(4),
1284 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1285 " open one level of fold
1286 4foldopen
1287 mkview! Xview
1288 normal zE
1289 source Xview
1290 call assert_equal([-1, -1, 5, 5, -1, -1], [foldclosed(3), foldclosed(4),
1291 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1292 " open all the folds
1293 %foldopen!
1294 mkview! Xview
1295 normal zE
1296 source Xview
1297 call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1298 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001299 call delete('Xview')
1300 bw!
1301endfunc
1302
Igor Lacerdacca5dea2025-04-17 20:41:31 +02001303" Test for handling invalid folds within views
1304func Test_mkview_ignore_invalid_folds()
1305 call writefile(range(1,10), 'Xmkvfile', 'D')
1306 new Xmkvfile
1307 " create some folds
1308 5,6fold
1309 4,7fold
1310 mkview Xview
1311 normal zE
1312 " delete lines to make folds invalid
1313 call deletebufline('', 6, '$')
1314 source Xview
1315 call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1316 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1317 call delete('Xview')
1318 bw!
1319endfunc
1320
Christian Brabandtb8b1c8e2023-06-23 22:23:01 +01001321" Test default 'viewdir' value
1322func Test_mkview_default_home()
1323 if has('win32')
Bram Moolenaar813b7a82023-06-23 22:56:47 +01001324 " use escape() to handle backslash path separators
1325 call assert_match('^' .. escape($ORIGHOME, '\') .. '/vimfiles', &viewdir)
Christian Brabandtb8b1c8e2023-06-23 22:23:01 +01001326 elseif has('unix')
John M Devin5b9237c2024-09-08 20:00:38 +02001327 call assert_match(
1328 \ '^' .. $ORIGHOME .. '/.vim\|' ..
1329 \ '^' .. $XDG_CONFIG_HOME .. '/vim'
1330 \ , &viewdir)
Christian Brabandtb8b1c8e2023-06-23 22:23:01 +01001331 elseif has('amiga')
1332 call assert_match('^home:vimfiles', &viewdir)
1333 elseif has('mac')
1334 call assert_match('^' .. $VIM .. '/vimfiles', &viewdir)
1335 endif
1336endfunc
1337
Bram Moolenaareca626f2016-12-01 18:47:38 +01001338" vim: shiftwidth=2 sts=2 expandtab