blob: 66e6e292ac9d2340a6681b9801799386dcf7d424 [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
Bram Moolenaareca626f2016-12-01 18:47:38 +010035func 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',
Bram Moolenaar92c1b692018-08-29 21:42:42 +020048 \ 'Aäöü three mulTibyte characters',
49 \ 'short line',
Bram Moolenaareca626f2016-12-01 18:47:38 +010050 \ ])
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010051 let tmpfile = 'Xtemp'
Bram Moolenaareca626f2016-12-01 18:47:38 +010052 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|
Bram Moolenaar92c1b692018-08-29 21:42:42 +020071 split
72 norm! j$
Bram Moolenaareca626f2016-12-01 18:47:38 +010073 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()
Bram Moolenaarc9b56b22017-01-29 14:14:09 +010094 mksession! Xtest_mks.out
Bram Moolenaar92c1b692018-08-29 21:42:42 +020095 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "\\(^ *normal! [0$]\\|^ *exe ''normal!\\)"')
Bram Moolenaareca626f2016-12-01 18:47:38 +010096 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|',
Bram Moolenaar92c1b692018-08-29 21:42:42 +0200105 \ 'normal! $',
Bram Moolenaareca626f2016-12-01 18:47:38 +0100106 \ " 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
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100128 call delete('Xtest_mks.out')
Bram Moolenaareca626f2016-12-01 18:47:38 +0100129 call delete(tmpfile)
130 let &wrap = wrap_save
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100131 set sessionoptions&
Bram Moolenaareca626f2016-12-01 18:47:38 +0100132endfunc
133
Bram Moolenaar635bd602021-04-16 19:58:22 +0200134def Test_mksession_skiprtp()
135 mksession! Xtest_mks.out
Bram Moolenaard23b7142021-04-17 21:04:34 +0200136 var found_rtp = 0
137 var found_pp = 0
Bram Moolenaar635bd602021-04-16 19:58:22 +0200138 for line in readfile('Xtest_mks.out')
139 if line =~ 'set runtimepath'
Bram Moolenaard23b7142021-04-17 21:04:34 +0200140 found_rtp += 1
141 endif
142 if line =~ 'set packpath'
143 found_pp += 1
Bram Moolenaar635bd602021-04-16 19:58:22 +0200144 endif
145 endfor
Bram Moolenaard23b7142021-04-17 21:04:34 +0200146 assert_equal(1, found_rtp)
147 assert_equal(1, found_pp)
Bram Moolenaar635bd602021-04-16 19:58:22 +0200148 delete('Xtest_mks.out')
149
150 set sessionoptions+=skiprtp
151 mksession! Xtest_mks.out
Bram Moolenaard23b7142021-04-17 21:04:34 +0200152 var found = 0
Bram Moolenaar635bd602021-04-16 19:58:22 +0200153 for line in readfile('Xtest_mks.out')
Bram Moolenaard23b7142021-04-17 21:04:34 +0200154 if line =~ 'set \(runtimepath\|packpath\)'
Bram Moolenaar635bd602021-04-16 19:58:22 +0200155 found = 1
156 break
157 endif
158 endfor
159 assert_equal(0, found)
160 delete('Xtest_mks.out')
161 set sessionoptions&
162enddef
163
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100164func Test_mksession_winheight()
165 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200166 set winheight=10
167 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100168 mksession! Xtest_mks.out
169 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100170
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100171 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100172endfunc
173
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200174func 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
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200182func 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()
Dominique Pelle5f8ed742021-05-03 19:08:37 +0200190 call delete('Xtest_mks_zero')
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200191 close
Dominique Pelle5f8ed742021-05-03 19:08:37 +0200192 " check there is no divide by zero
Bram Moolenaarb6c2e9a2021-04-30 21:37:51 +0200193 call assert_notmatch('/ 0[^0-9]', text)
194endfunc
195
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100196func Test_mksession_rtp()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200197 " TODO: fix problem with backslashes on Win32
198 CheckNotMSWindows
199
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100200 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
Bram Moolenaar79da5632017-02-01 22:52:44 +0100222func Test_mksession_arglist()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200223 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100224 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200225 new
226 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100227 mksession! Xtest_mks.out
228 source Xtest_mks.out
229 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200230 call assert_equal(2, argidx())
231 wincmd w
232 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100233
234 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200235 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100236 argdel *
237endfunc
238
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200239func 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!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200265 call delete('Xtest_mks.out')
266endfunc
267
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200268func Test_mksession_lcd_multiple_tabs()
269 tabnew
270 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200271 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200272 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200273 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200274 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
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200285" 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'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100322 %bwipe
323 call chdir(save_cwd)
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200324 call delete("Xtopdir", "rf")
325endfunc
326
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200327func 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
Bram Moolenaard39e2752019-01-26 20:07:38 +0100342func 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
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100365if 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
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200383func 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
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100398func Test_mksession_terminal_shell()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200399 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100400 CheckFeature quickfix
401
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100402 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
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100413 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100414
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200415 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100416 call delete('Xtest_mks.out')
417endfunc
418
419func Test_mksession_terminal_no_restore_cmdarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200420 CheckFeature terminal
421
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100422 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'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100428 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100429 endif
430 endfor
431
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200432 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100433 call delete('Xtest_mks.out')
434endfunc
435
436func Test_mksession_terminal_no_restore_funcarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200437 CheckFeature terminal
438
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200439 let buf = Run_shell_in_terminal({'norestore': 1})
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100440 mksession! Xtest_mks.out
441 let lines = readfile('Xtest_mks.out')
442 let term_cmd = ''
443 for line in lines
444 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100445 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100446 endif
447 endfor
448
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200449 call StopShellInTerminal(buf)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100450 call delete('Xtest_mks.out')
451endfunc
452
453func Test_mksession_terminal_no_restore_func()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200454 CheckFeature terminal
455
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100456 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'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100463 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100464 endif
465 endfor
466
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200467 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100468 call delete('Xtest_mks.out')
469endfunc
470
471func Test_mksession_terminal_no_ssop()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200472 CheckFeature terminal
473
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100474 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'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100481 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100482 endif
483 endfor
484
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200485 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100486 call delete('Xtest_mks.out')
487 set sessionoptions&
488endfunc
489
490func Test_mksession_terminal_restore_other()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200491 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100492 CheckFeature quickfix
493
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100494 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200495 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100496 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
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100504 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100505
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200506 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100507 call delete('Xtest_mks.out')
508endfunc
509
Bram Moolenaar0e655112020-09-11 20:36:36 +0200510func Test_mksession_terminal_shared_windows()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200511 CheckFeature terminal
512
Bram Moolenaar0e655112020-09-11 20:36:36 +0200513 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
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200538func 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
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200550
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100551func 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
Bram Moolenaar139348f2021-02-05 21:55:53 +0100577func 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
Bram Moolenaar0756f752021-03-13 13:52:33 +0100591func 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
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200605" 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
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200610 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200611 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200612 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('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200623 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200624 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
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200646 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200647 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('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200663 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200664 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
Bram Moolenaar79da5632017-02-01 22:52:44 +0100686
Bram Moolenaar3482be62020-11-27 11:00:38 +0100687func 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
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100734" 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
Bram Moolenaarad36a352019-01-24 13:34:42 +0100745func Test_mksession_quote_in_filename()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200746 " only Unix can handle this weird filename
747 CheckUnix
748
Bram Moolenaarad36a352019-01-24 13:34:42 +0100749 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
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200762" Test for storing global variables in a session file
763func Test_mksession_globals()
764 set sessionoptions+=globals
765
766 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200767 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200768 let g:Global_count = 100
769 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200770 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200771
772 mksession! Xtest_mks.out
773
774 unlet g:Global_string
775 unlet g:Global_count
776 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200777 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200778
779 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200780 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200781 call assert_equal(100, g:Global_count)
782 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200783 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200784
785 unlet g:Global_string
786 unlet g:Global_count
787 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200788 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200789 call delete('Xtest_mks.out')
790 set sessionoptions&
791endfunc
792
793" Test for changing backslash to forward slash in filenames
794func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200795 if exists('+shellslash')
796 throw 'Skipped: cannot use backslash in file name'
797 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200798 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
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200818" Test for changing directory to the session file directory
819func Test_mksession_sesdir()
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100820 let save_cwd = getcwd()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200821 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'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100831 call chdir(save_cwd)
832 %bwipe
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200833
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
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200849 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200850 endif
851 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200852 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200853 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
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200858 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200859 endif
860 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200861 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200862
863 call delete('Xtest_mks1.out')
864 call delete('Xtest_mks2.out')
865 set sessionoptions&
866endfunc
867
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200868" 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()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200914 " Only applicable in GUI Vim
915 CheckGui
916
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200917 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
Bram Moolenaar0995c812021-04-17 18:38:54 +0200932" 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
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200950" 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
Bram Moolenaar87500262019-08-01 23:05:49 +0200983func 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 call delete('Xtestvimrc')
1023endfunc
1024
Bram Moolenaar38890832020-11-01 17:40:54 +01001025func Test_scrolloff()
1026 set sessionoptions+=localoptions
1027 setlocal so=1 siso=1
1028 mksession! Xtest_mks.out
1029 setlocal so=-1 siso=-1
1030 source Xtest_mks.out
1031 call assert_equal(1, &l:so)
1032 call assert_equal(1, &l:siso)
1033 call delete('Xtest_mks.out')
1034 setlocal so& siso&
1035 set sessionoptions&
1036endfunc
1037
Bram Moolenaar3482be62020-11-27 11:00:38 +01001038func Test_altfile()
1039 edit Xone
1040 split Xtwo
1041 edit Xtwoalt
1042 edit #
1043 wincmd w
1044 edit Xonealt
1045 edit #
1046 mksession! Xtest_altfile
1047 only
1048 bwipe Xonealt
1049 bwipe Xtwoalt
1050 bwipe!
1051 source Xtest_altfile
1052 call assert_equal('Xone', bufname())
1053 call assert_equal('Xonealt', bufname('#'))
1054 wincmd w
1055 call assert_equal('Xtwo', bufname())
1056 call assert_equal('Xtwoalt', bufname('#'))
1057 only
1058 bwipe!
Bram Moolenaarac665c22020-12-08 20:39:15 +01001059 call delete('Xtest_altfile')
Bram Moolenaar3482be62020-11-27 11:00:38 +01001060endfunc
1061
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001062" Test for creating views with manual folds
1063func Test_mkview_manual_fold()
1064 call writefile(range(1,10), 'Xfile')
1065 new Xfile
1066 " create recursive folds
1067 5,6fold
1068 4,7fold
1069 mkview Xview
1070 normal zE
1071 source Xview
1072 call assert_equal([-1, 4, 4, 4, 4, -1], [foldclosed(3), foldclosed(4),
1073 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1074 " open one level of fold
1075 4foldopen
1076 mkview! Xview
1077 normal zE
1078 source Xview
1079 call assert_equal([-1, -1, 5, 5, -1, -1], [foldclosed(3), foldclosed(4),
1080 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1081 " open all the folds
1082 %foldopen!
1083 mkview! Xview
1084 normal zE
1085 source Xview
1086 call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1087 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1088 call delete('Xfile')
1089 call delete('Xview')
1090 bw!
1091endfunc
1092
Bram Moolenaareca626f2016-12-01 18:47:38 +01001093" vim: shiftwidth=2 sts=2 expandtab