blob: 045aa2de0a5c03e07e5a2277fb100e486e998f34 [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 Moolenaared18f2c2019-01-24 20:30:52 +0100182func Test_mksession_rtp()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200183 " TODO: fix problem with backslashes on Win32
184 CheckNotMSWindows
185
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100186 new
187 let _rtp=&rtp
188 " Make a real long (invalid) runtimepath value,
189 " that should exceed PATH_MAX (hopefully)
190 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
191 let newrtp.=",".expand("$HOME")."/.vim"
192 let &rtp=newrtp
193
194 " determine expected value
195 let expected=split(&rtp, ',')
196 let expected = map(expected, '"set runtimepath+=".v:val')
197 let expected = ['set runtimepath='] + expected
198 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
199
200 mksession! Xtest_mks.out
201 let &rtp=_rtp
202 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
203 call assert_equal(expected, li)
204
205 call delete('Xtest_mks.out')
206endfunc
207
Bram Moolenaar79da5632017-02-01 22:52:44 +0100208func Test_mksession_arglist()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200209 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100210 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200211 new
212 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100213 mksession! Xtest_mks.out
214 source Xtest_mks.out
215 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200216 call assert_equal(2, argidx())
217 wincmd w
218 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100219
220 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200221 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100222 argdel *
223endfunc
224
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200225func Test_mksession_one_buffer_two_windows()
226 edit Xtest1
227 new Xtest2
228 split
229 mksession! Xtest_mks.out
230 let lines = readfile('Xtest_mks.out')
231 let count1 = 0
232 let count2 = 0
233 let count2buf = 0
234 for line in lines
235 if line =~ 'edit \f*Xtest1$'
236 let count1 += 1
237 endif
238 if line =~ 'edit \f\{-}Xtest2'
239 let count2 += 1
240 endif
241 if line =~ 'buffer \f\{-}Xtest2'
242 let count2buf += 1
243 endif
244 endfor
245 call assert_equal(1, count1, 'Xtest1 count')
246 call assert_equal(2, count2, 'Xtest2 count')
247 call assert_equal(2, count2buf, 'Xtest2 buffer count')
248
249 close
250 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200251 call delete('Xtest_mks.out')
252endfunc
253
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200254func Test_mksession_lcd_multiple_tabs()
255 tabnew
256 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200257 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200258 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200259 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200260 mksession! Xtest_mks.out
261 tabonly
262 source Xtest_mks.out
263 call assert_true(haslocaldir(), 'Tab 1 localdir')
264 tabnext 2
265 call assert_true(!haslocaldir(), 'Tab 2 localdir')
266 tabnext 3
267 call assert_true(haslocaldir(), 'Tab 3 localdir')
268 call delete('Xtest_mks.out')
269endfunc
270
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200271" Test for tabpage-local directory
272func Test_mksession_tcd_multiple_tabs()
273 let save_cwd = getcwd()
274 call mkdir('Xtopdir')
275 cd Xtopdir
276 call mkdir('Xtabdir1')
277 call mkdir('Xtabdir2')
278 call mkdir('Xtabdir3')
279 call mkdir('Xwindir1')
280 call mkdir('Xwindir2')
281 call mkdir('Xwindir3')
282 tcd Xtabdir1
283 botright new
284 wincmd t
285 lcd ../Xwindir1
286 tabnew
287 tcd ../Xtabdir2
288 botright new
289 lcd ../Xwindir2
290 tabnew
291 tcd ../Xtabdir3
292 botright new
293 lcd ../Xwindir3
294 tabfirst
295 1wincmd w
296 mksession! Xtest_mks.out
297 only | tabonly
298 source Xtest_mks.out
299 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
300 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
301 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
302 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
303 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
304 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
305 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
306 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
307 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100308 %bwipe
309 call chdir(save_cwd)
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200310 call delete("Xtopdir", "rf")
311endfunc
312
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200313func Test_mksession_blank_tabs()
314 tabnew
315 tabnew
316 tabnew
317 tabnext 3
318 mksession! Xtest_mks.out
319 tabnew
320 tabnew
321 tabnext 2
322 source Xtest_mks.out
323 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
324 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
325 call delete('Xtest_mks.out')
326endfunc
327
Bram Moolenaard39e2752019-01-26 20:07:38 +0100328func Test_mksession_buffer_count()
329 set hidden
330
331 " Edit exactly three files in the current session.
332 %bwipe!
333 e Xfoo | tabe Xbar | tabe Xbaz
334 tabdo write
335 mksession! Xtest_mks.out
336
337 " Verify that loading the session does not create additional buffers.
338 %bwipe!
339 source Xtest_mks.out
340 call assert_equal(3, len(getbufinfo()))
341
342 " Clean up.
343 call delete('Xfoo')
344 call delete('Xbar')
345 call delete('Xbaz')
346 call delete('Xtest_mks.out')
347 %bwipe!
348 set hidden&
349endfunc
350
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100351if has('extra_search')
352
353func Test_mksession_hlsearch()
354 set hlsearch
355 mksession! Xtest_mks.out
356 nohlsearch
357 source Xtest_mks.out
358 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
359 nohlsearch
360 mksession! Xtest_mks.out
361 source Xtest_mks.out
362 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
363 call delete('Xtest_mks.out')
364endfunc
365
366endif
367
368
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200369func Test_mksession_blank_windows()
370 split
371 split
372 split
373 3 wincmd w
374 mksession! Xtest_mks.out
375 split
376 split
377 2 wincmd w
378 source Xtest_mks.out
379 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
380 call assert_equal(3, winnr(), 'session restore should restore the active window')
381 call delete('Xtest_mks.out')
382endfunc
383
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100384func Test_mksession_terminal_shell()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200385 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100386 CheckFeature quickfix
387
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100388 terminal
389 mksession! Xtest_mks.out
390 let lines = readfile('Xtest_mks.out')
391 let term_cmd = ''
392 for line in lines
393 if line =~ '^terminal'
394 let term_cmd = line
395 elseif line =~ 'badd.*' . &shell
396 call assert_report('unexpected shell line: ' . line)
397 endif
398 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100399 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100400
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200401 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100402 call delete('Xtest_mks.out')
403endfunc
404
405func Test_mksession_terminal_no_restore_cmdarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200406 CheckFeature terminal
407
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100408 terminal ++norestore
409 mksession! Xtest_mks.out
410 let lines = readfile('Xtest_mks.out')
411 let term_cmd = ''
412 for line in lines
413 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100414 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100415 endif
416 endfor
417
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200418 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100419 call delete('Xtest_mks.out')
420endfunc
421
422func Test_mksession_terminal_no_restore_funcarg()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200423 CheckFeature terminal
424
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200425 let buf = Run_shell_in_terminal({'norestore': 1})
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100426 mksession! Xtest_mks.out
427 let lines = readfile('Xtest_mks.out')
428 let term_cmd = ''
429 for line in lines
430 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100431 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100432 endif
433 endfor
434
Bram Moolenaar3a6aadb2021-04-04 15:28:59 +0200435 call StopShellInTerminal(buf)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100436 call delete('Xtest_mks.out')
437endfunc
438
439func Test_mksession_terminal_no_restore_func()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200440 CheckFeature terminal
441
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100442 terminal
443 call term_setrestore(bufnr('%'), 'NONE')
444 mksession! Xtest_mks.out
445 let lines = readfile('Xtest_mks.out')
446 let term_cmd = ''
447 for line in lines
448 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100449 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100450 endif
451 endfor
452
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200453 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100454 call delete('Xtest_mks.out')
455endfunc
456
457func Test_mksession_terminal_no_ssop()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200458 CheckFeature terminal
459
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100460 terminal
461 set sessionoptions-=terminal
462 mksession! Xtest_mks.out
463 let lines = readfile('Xtest_mks.out')
464 let term_cmd = ''
465 for line in lines
466 if line =~ '^terminal'
Bram Moolenaarac665c22020-12-08 20:39:15 +0100467 call assert_report('session must not restore terminal')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100468 endif
469 endfor
470
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200471 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100472 call delete('Xtest_mks.out')
473 set sessionoptions&
474endfunc
475
476func Test_mksession_terminal_restore_other()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200477 CheckFeature terminal
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100478 CheckFeature quickfix
479
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100480 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200481 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100482 mksession! Xtest_mks.out
483 let lines = readfile('Xtest_mks.out')
484 let term_cmd = ''
485 for line in lines
486 if line =~ '^terminal'
487 let term_cmd = line
488 endif
489 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100490 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100491
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200492 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100493 call delete('Xtest_mks.out')
494endfunc
495
Bram Moolenaar0e655112020-09-11 20:36:36 +0200496func Test_mksession_terminal_shared_windows()
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200497 CheckFeature terminal
498
Bram Moolenaar0e655112020-09-11 20:36:36 +0200499 terminal
500 let term_buf = bufnr()
501 new
502 execute "buffer" term_buf
503 mksession! Xtest_mks.out
504
505 let lines = readfile('Xtest_mks.out')
506 let found_creation = 0
507 let found_use = 0
508
509 for line in lines
510 if line =~ '^terminal'
511 let found_creation = 1
512 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+', line)
513 elseif line =~ "^execute 'buffer ' . s:term_buf_" . term_buf . "$"
514 let found_use = 1
515 endif
516 endfor
517
518 call assert_true(found_creation && found_use)
519
520 call StopShellInTerminal(term_buf)
521 call delete('Xtest_mks.out')
522endfunc
523
Bram Moolenaarc2c82052020-09-11 22:10:22 +0200524func Test_mkview_terminal_windows()
525 CheckFeature terminal
526
527 " create two window on the same terminal to check this is handled OK
528 terminal
529 let term_buf = bufnr()
530 exe 'sbuf ' .. term_buf
531 mkview! Xtestview
532
533 call StopShellInTerminal(term_buf)
534 call delete('Xtestview')
535endfunc
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200536
Bram Moolenaarf9547eb2021-02-01 19:24:55 +0100537func Test_mkview_open_folds()
538 enew!
539
540 call append(0, ['a', 'b', 'c'])
541 1,3fold
542 " zR affects 'foldlevel', make sure the option is applied after the folds
543 " have been recreated.
544 normal zR
545 write! Xtestfile
546
547 call assert_equal(-1, foldclosed(1))
548 call assert_equal(-1, foldclosed(2))
549 call assert_equal(-1, foldclosed(3))
550
551 mkview! Xtestview
552 source Xtestview
553
554 call assert_equal(-1, foldclosed(1))
555 call assert_equal(-1, foldclosed(2))
556 call assert_equal(-1, foldclosed(3))
557
558 call delete('Xtestview')
559 call delete('Xtestfile')
560 %bwipe
561endfunc
562
Bram Moolenaar139348f2021-02-05 21:55:53 +0100563func Test_mkview_no_balt()
564 edit Xtestfile1
565 edit Xtestfile2
566
567 mkview! Xtestview
568 bdelete Xtestfile1
569
570 source Xtestview
571 call assert_equal(0, buflisted('Xtestfile1'))
572
573 call delete('Xtestview')
574 %bwipe
575endfunc
576
Bram Moolenaar0756f752021-03-13 13:52:33 +0100577func Test_mksession_no_balt()
578 edit Xtestfile1
579 edit Xtestfile2
580
581 bdelete Xtestfile1
582 mksession! Xtestview
583
584 source Xtestview
585 call assert_equal(0, buflisted('Xtestfile1'))
586
587 call delete('Xtestview')
588 %bwipe
589endfunc
590
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200591" Test :mkview with a file argument.
592func Test_mkview_file()
593 " Create a view with line number and a fold.
594 help :mkview
595 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200596 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200597 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200598 let linefoldclosed1 = foldclosed('.')
599 mkview! Xview
600 set nonumber
601 norm! zrj
602 " We can close the help window, as mkview with a file name should
603 " generate a command to edit the file.
604 helpclose
605
606 source Xview
607 call assert_equal(1, &number)
608 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200609 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200610 call assert_equal(linefoldclosed1, foldclosed('.'))
611
612 " Creating a view again with the same file name should fail (file
613 " already exists). But with a !, the previous view should be
614 " overwritten without error.
615 help :loadview
616 call assert_fails('mkview Xview', 'E189:')
617 call assert_match('\*:loadview\*$', getline('.'))
618 mkview! Xview
619 call assert_match('\*:loadview\*$', getline('.'))
620
621 call delete('Xview')
622 bwipe
623endfunc
624
625" Test :mkview and :loadview with a custom 'viewdir'.
626func Test_mkview_loadview_with_viewdir()
627 set viewdir=Xviewdir
628
629 help :mkview
630 set number
631 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200632 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200633 let linefoldclosed1 = foldclosed('.')
634 mkview 1
635 set nonumber
636 norm! zrj
637
638 loadview 1
639
640 " The directory Xviewdir/ should have been created and the view
641 " should be stored in that directory.
642 call assert_equal('Xviewdir/' .
643 \ substitute(
644 \ substitute(
645 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
646 \ glob('Xviewdir/*'))
647 call assert_equal(1, &number)
648 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200649 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200650 call assert_equal(linefoldclosed1, foldclosed('.'))
651
652 call delete('Xviewdir', 'rf')
653 set viewdir&
654 helpclose
655endfunc
656
657func Test_mkview_no_file_name()
658 new
659 " :mkview or :mkview {nr} should fail in a unnamed buffer.
660 call assert_fails('mkview', 'E32:')
661 call assert_fails('mkview 1', 'E32:')
662
663 " :mkview {file} should succeed in a unnamed buffer.
664 mkview Xview
665 help
666 source Xview
667 call assert_equal('', bufname('%'))
668
669 call delete('Xview')
670 %bwipe
671endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100672
Bram Moolenaar3482be62020-11-27 11:00:38 +0100673func Test_mkview_loadview_jumplist()
674 set viewdir=Xviewdir
675 au BufWinLeave * silent mkview
676 au BufWinEnter * silent loadview
677
678 edit Xfile1
679 call setline(1, ['a', 'bbbbbbb', 'c'])
680 normal j3l
681 call assert_equal([2, 4], getcurpos()[1:2])
682 write
683
684 edit Xfile2
685 call setline(1, ['d', 'eeeeeee', 'f'])
686 normal j5l
687 call assert_equal([2, 6], getcurpos()[1:2])
688 write
689
690 edit Xfile3
691 call setline(1, ['g', 'h', 'iiiii'])
692 normal jj3l
693 call assert_equal([3, 4], getcurpos()[1:2])
694 write
695
696 edit Xfile1
697 call assert_equal([2, 4], getcurpos()[1:2])
698 edit Xfile2
699 call assert_equal([2, 6], getcurpos()[1:2])
700 edit Xfile3
701 call assert_equal([3, 4], getcurpos()[1:2])
702
703 exe "normal \<C-O>"
704 call assert_equal('Xfile2', expand('%'))
705 call assert_equal([2, 6], getcurpos()[1:2])
706 exe "normal \<C-O>"
707 call assert_equal('Xfile1', expand('%'))
708 call assert_equal([2, 4], getcurpos()[1:2])
709
710 au! BufWinLeave
711 au! BufWinEnter
712 bwipe!
713 call delete('Xviewdir', 'rf')
714 call delete('Xfile1')
715 call delete('Xfile2')
716 call delete('Xfile3')
717 set viewdir&
718endfunc
719
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100720" A clean session (one empty buffer, one window, and one tab) should not
721" set any error messages when sourced because no commands should fail.
722func Test_mksession_no_errmsg()
723 let v:errmsg = ''
724 %bwipe!
725 mksession! Xtest_mks.out
726 source Xtest_mks.out
727 call assert_equal('', v:errmsg)
728 call delete('Xtest_mks.out')
729endfunc
730
Bram Moolenaarad36a352019-01-24 13:34:42 +0100731func Test_mksession_quote_in_filename()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200732 " only Unix can handle this weird filename
733 CheckUnix
734
Bram Moolenaarad36a352019-01-24 13:34:42 +0100735 let v:errmsg = ''
736 %bwipe!
737 split another
738 split x'y\"z
739 mksession! Xtest_mks_quoted.out
740 %bwipe!
741 source Xtest_mks_quoted.out
742 call assert_true(bufexists("x'y\"z"))
743
744 %bwipe!
745 call delete('Xtest_mks_quoted.out')
746endfunc
747
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200748" Test for storing global variables in a session file
749func Test_mksession_globals()
750 set sessionoptions+=globals
751
752 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200753 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200754 let g:Global_count = 100
755 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200756 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200757
758 mksession! Xtest_mks.out
759
760 unlet g:Global_string
761 unlet g:Global_count
762 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200763 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200764
765 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200766 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200767 call assert_equal(100, g:Global_count)
768 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200769 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200770
771 unlet g:Global_string
772 unlet g:Global_count
773 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200774 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200775 call delete('Xtest_mks.out')
776 set sessionoptions&
777endfunc
778
779" Test for changing backslash to forward slash in filenames
780func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200781 if exists('+shellslash')
782 throw 'Skipped: cannot use backslash in file name'
783 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200784 enew
785 %bwipe!
786 e a\\b\\c
787 mksession! Xtest_mks1.out
788 set sessionoptions+=slash
789 mksession! Xtest_mks2.out
790
791 %bwipe!
792 source Xtest_mks1.out
793 call assert_equal('a\b\c', bufname(''))
794 %bwipe!
795 source Xtest_mks2.out
796 call assert_equal('a/b/c', bufname(''))
797
798 %bwipe!
799 call delete('Xtest_mks1.out')
800 call delete('Xtest_mks2.out')
801 set sessionoptions&
802endfunc
803
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200804" Test for changing directory to the session file directory
805func Test_mksession_sesdir()
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100806 let save_cwd = getcwd()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200807 call mkdir('Xproj')
808 mksession! Xproj/Xtest_mks1.out
809 set sessionoptions-=curdir
810 set sessionoptions+=sesdir
811 mksession! Xproj/Xtest_mks2.out
812
813 source Xproj/Xtest_mks1.out
814 call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
815 source Xproj/Xtest_mks2.out
816 call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100817 call chdir(save_cwd)
818 %bwipe
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200819
820 set sessionoptions&
821 call delete('Xproj', 'rf')
822endfunc
823
824" Test for storing the 'lines' and 'columns' settings
825func Test_mksession_resize()
826 mksession! Xtest_mks1.out
827 set sessionoptions+=resize
828 mksession! Xtest_mks2.out
829
830 let lines = readfile('Xtest_mks1.out')
831 let found_resize = v:false
832 for line in lines
833 if line =~ '^set lines='
834 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200835 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200836 endif
837 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200838 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200839 let lines = readfile('Xtest_mks2.out')
840 let found_resize = v:false
841 for line in lines
842 if line =~ '^set lines='
843 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200844 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200845 endif
846 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200847 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200848
849 call delete('Xtest_mks1.out')
850 call delete('Xtest_mks2.out')
851 set sessionoptions&
852endfunc
853
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200854" Test for mksession with a named scratch buffer
855func Test_mksession_scratch()
856 enew | only
857 file Xscratch
858 set buftype=nofile
859 mksession! Xtest_mks.out
860 %bwipe
861 source Xtest_mks.out
862 call assert_equal('Xscratch', bufname(''))
863 call assert_equal('nofile', &buftype)
864 %bwipe
865 call delete('Xtest_mks.out')
866endfunc
867
868" Test for mksession with fold options
869func Test_mksession_foldopt()
870 set sessionoptions-=options
871 set sessionoptions+=folds
872 new
873 setlocal foldenable
874 setlocal foldmethod=expr
875 setlocal foldmarker=<<<,>>>
876 setlocal foldignore=%
877 setlocal foldlevel=2
878 setlocal foldminlines=10
879 setlocal foldnestmax=15
880 mksession! Xtest_mks.out
881 close
882 %bwipe
883
884 source Xtest_mks.out
885 call assert_true(&foldenable)
886 call assert_equal('expr', &foldmethod)
887 call assert_equal('<<<,>>>', &foldmarker)
888 call assert_equal('%', &foldignore)
889 call assert_equal(2, &foldlevel)
890 call assert_equal(10, &foldminlines)
891 call assert_equal(15, &foldnestmax)
892
893 close
894 %bwipe
895 set sessionoptions&
896endfunc
897
898" Test for mksession with window position
899func Test_mksession_winpos()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200900 " Only applicable in GUI Vim
901 CheckGui
902
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200903 set sessionoptions+=winpos
904 mksession! Xtest_mks.out
905 let found_winpos = v:false
906 let lines = readfile('Xtest_mks.out')
907 for line in lines
908 if line =~ '^winpos '
909 let found_winpos = v:true
910 break
911 endif
912 endfor
913 call assert_true(found_winpos)
914 call delete('Xtest_mks.out')
915 set sessionoptions&
916endfunc
917
Bram Moolenaar0995c812021-04-17 18:38:54 +0200918" Test for mksession without options restores winminheight
919func Test_mksession_winminheight()
920 set sessionoptions-=options
921 split
922 mksession! Xtest_mks.out
923 let found_restore = 0
924 let lines = readfile('Xtest_mks.out')
925 for line in lines
926 if line =~ '= s:save_winmin\(width\|height\)'
927 let found_restore += 1
928 endif
929 endfor
930 call assert_equal(2, found_restore)
931 call delete('Xtest_mks.out')
932 close
933 set sessionoptions&
934endfunc
935
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200936" Test for mksession with 'compatible' option
937func Test_mksession_compatible()
938 mksession! Xtest_mks1.out
939 set compatible
940 mksession! Xtest_mks2.out
941 set nocp
942
943 let test_success = v:false
944 let lines = readfile('Xtest_mks1.out')
945 for line in lines
946 if line =~ '^if &cp | set nocp | endif'
947 let test_success = v:true
948 break
949 endif
950 endfor
951 call assert_true(test_success)
952
953 let test_success = v:false
954 let lines = readfile('Xtest_mks2.out')
955 for line in lines
956 if line =~ '^if !&cp | set cp | endif'
957 let test_success = v:true
958 break
959 endif
960 endfor
961 call assert_true(test_success)
962
963 call delete('Xtest_mks1.out')
964 call delete('Xtest_mks2.out')
965 set compatible&
966 set sessionoptions&
967endfunc
968
Bram Moolenaar87500262019-08-01 23:05:49 +0200969func s:ClearMappings()
970 mapclear
971 omapclear
972 mapclear!
973 lmapclear
974 tmapclear
975endfunc
976
977func Test_mkvimrc()
978 let entries = [
979 \ ['', 'nothing', '<Nop>'],
980 \ ['n', 'normal', 'NORMAL'],
981 \ ['v', 'visual', 'VISUAL'],
982 \ ['s', 'select', 'SELECT'],
983 \ ['x', 'visualonly', 'VISUALONLY'],
984 \ ['o', 'operator', 'OPERATOR'],
985 \ ['i', 'insert', 'INSERT'],
986 \ ['l', 'lang', 'LANG'],
987 \ ['c', 'command', 'COMMAND'],
988 \ ['t', 'terminal', 'TERMINAL'],
989 \ ]
990 for entry in entries
991 exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
992 endfor
993
994 mkvimrc Xtestvimrc
995
996 call s:ClearMappings()
997 for entry in entries
998 call assert_equal('', maparg(entry[1], entry[0]))
999 endfor
1000
1001 source Xtestvimrc
1002
1003 for entry in entries
1004 call assert_equal(entry[2], maparg(entry[1], entry[0]))
1005 endfor
1006
1007 call s:ClearMappings()
1008 call delete('Xtestvimrc')
1009endfunc
1010
Bram Moolenaar38890832020-11-01 17:40:54 +01001011func Test_scrolloff()
1012 set sessionoptions+=localoptions
1013 setlocal so=1 siso=1
1014 mksession! Xtest_mks.out
1015 setlocal so=-1 siso=-1
1016 source Xtest_mks.out
1017 call assert_equal(1, &l:so)
1018 call assert_equal(1, &l:siso)
1019 call delete('Xtest_mks.out')
1020 setlocal so& siso&
1021 set sessionoptions&
1022endfunc
1023
Bram Moolenaar3482be62020-11-27 11:00:38 +01001024func Test_altfile()
1025 edit Xone
1026 split Xtwo
1027 edit Xtwoalt
1028 edit #
1029 wincmd w
1030 edit Xonealt
1031 edit #
1032 mksession! Xtest_altfile
1033 only
1034 bwipe Xonealt
1035 bwipe Xtwoalt
1036 bwipe!
1037 source Xtest_altfile
1038 call assert_equal('Xone', bufname())
1039 call assert_equal('Xonealt', bufname('#'))
1040 wincmd w
1041 call assert_equal('Xtwo', bufname())
1042 call assert_equal('Xtwoalt', bufname('#'))
1043 only
1044 bwipe!
Bram Moolenaarac665c22020-12-08 20:39:15 +01001045 call delete('Xtest_altfile')
Bram Moolenaar3482be62020-11-27 11:00:38 +01001046endfunc
1047
Bram Moolenaar5c504f62021-04-01 13:39:51 +02001048" Test for creating views with manual folds
1049func Test_mkview_manual_fold()
1050 call writefile(range(1,10), 'Xfile')
1051 new Xfile
1052 " create recursive folds
1053 5,6fold
1054 4,7fold
1055 mkview Xview
1056 normal zE
1057 source Xview
1058 call assert_equal([-1, 4, 4, 4, 4, -1], [foldclosed(3), foldclosed(4),
1059 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1060 " open one level of fold
1061 4foldopen
1062 mkview! Xview
1063 normal zE
1064 source Xview
1065 call assert_equal([-1, -1, 5, 5, -1, -1], [foldclosed(3), foldclosed(4),
1066 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1067 " open all the folds
1068 %foldopen!
1069 mkview! Xview
1070 normal zE
1071 source Xview
1072 call assert_equal([-1, -1, -1, -1, -1, -1], [foldclosed(3), foldclosed(4),
1073 \ foldclosed(5), foldclosed(6), foldclosed(7), foldclosed(8)])
1074 call delete('Xfile')
1075 call delete('Xview')
1076 bw!
1077endfunc
1078
Bram Moolenaareca626f2016-12-01 18:47:38 +01001079" vim: shiftwidth=2 sts=2 expandtab