blob: 66cbca15a0d87776f8a2a4560345dbd968d3e56c [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 Moolenaar36ae89c2017-01-28 17:11:14 +0100134func Test_mksession_winheight()
135 new
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200136 set winheight=10
137 set winminheight=2
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100138 mksession! Xtest_mks.out
139 source Xtest_mks.out
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100140
Bram Moolenaarc9b56b22017-01-29 14:14:09 +0100141 call delete('Xtest_mks.out')
Bram Moolenaar36ae89c2017-01-28 17:11:14 +0100142endfunc
143
Bram Moolenaar1c3c1042018-06-12 16:49:30 +0200144func Test_mksession_large_winheight()
145 set winheight=999
146 mksession! Xtest_mks_winheight.out
147 set winheight&
148 source Xtest_mks_winheight.out
149 call delete('Xtest_mks_winheight.out')
150endfunc
151
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100152func Test_mksession_rtp()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200153 " TODO: fix problem with backslashes on Win32
154 CheckNotMSWindows
155
Bram Moolenaared18f2c2019-01-24 20:30:52 +0100156 new
157 let _rtp=&rtp
158 " Make a real long (invalid) runtimepath value,
159 " that should exceed PATH_MAX (hopefully)
160 let newrtp=&rtp.',~'.repeat('/foobar', 1000)
161 let newrtp.=",".expand("$HOME")."/.vim"
162 let &rtp=newrtp
163
164 " determine expected value
165 let expected=split(&rtp, ',')
166 let expected = map(expected, '"set runtimepath+=".v:val')
167 let expected = ['set runtimepath='] + expected
168 let expected = map(expected, {v,w -> substitute(w, $HOME, "~", "g")})
169
170 mksession! Xtest_mks.out
171 let &rtp=_rtp
172 let li = filter(readfile('Xtest_mks.out'), 'v:val =~# "runtimepath"')
173 call assert_equal(expected, li)
174
175 call delete('Xtest_mks.out')
176endfunc
177
Bram Moolenaar79da5632017-02-01 22:52:44 +0100178func Test_mksession_arglist()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200179 %argdel
Bram Moolenaar79da5632017-02-01 22:52:44 +0100180 next file1 file2 file3 file4
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200181 new
182 next | next
Bram Moolenaar79da5632017-02-01 22:52:44 +0100183 mksession! Xtest_mks.out
184 source Xtest_mks.out
185 call assert_equal(['file1', 'file2', 'file3', 'file4'], argv())
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200186 call assert_equal(2, argidx())
187 wincmd w
188 call assert_equal(0, argidx())
Bram Moolenaar79da5632017-02-01 22:52:44 +0100189
190 call delete('Xtest_mks.out')
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200191 enew | only
Bram Moolenaar79da5632017-02-01 22:52:44 +0100192 argdel *
193endfunc
194
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200195func Test_mksession_one_buffer_two_windows()
196 edit Xtest1
197 new Xtest2
198 split
199 mksession! Xtest_mks.out
200 let lines = readfile('Xtest_mks.out')
201 let count1 = 0
202 let count2 = 0
203 let count2buf = 0
204 for line in lines
205 if line =~ 'edit \f*Xtest1$'
206 let count1 += 1
207 endif
208 if line =~ 'edit \f\{-}Xtest2'
209 let count2 += 1
210 endif
211 if line =~ 'buffer \f\{-}Xtest2'
212 let count2buf += 1
213 endif
214 endfor
215 call assert_equal(1, count1, 'Xtest1 count')
216 call assert_equal(2, count2, 'Xtest2 count')
217 call assert_equal(2, count2buf, 'Xtest2 buffer count')
218
219 close
220 bwipe!
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200221 call delete('Xtest_mks.out')
222endfunc
223
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200224func Test_mksession_lcd_multiple_tabs()
225 tabnew
226 tabnew
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200227 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200228 tabfirst
Bram Moolenaar81e2ac72018-07-04 22:44:08 +0200229 lcd .
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200230 mksession! Xtest_mks.out
231 tabonly
232 source Xtest_mks.out
233 call assert_true(haslocaldir(), 'Tab 1 localdir')
234 tabnext 2
235 call assert_true(!haslocaldir(), 'Tab 2 localdir')
236 tabnext 3
237 call assert_true(haslocaldir(), 'Tab 3 localdir')
238 call delete('Xtest_mks.out')
239endfunc
240
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200241" Test for tabpage-local directory
242func Test_mksession_tcd_multiple_tabs()
243 let save_cwd = getcwd()
244 call mkdir('Xtopdir')
245 cd Xtopdir
246 call mkdir('Xtabdir1')
247 call mkdir('Xtabdir2')
248 call mkdir('Xtabdir3')
249 call mkdir('Xwindir1')
250 call mkdir('Xwindir2')
251 call mkdir('Xwindir3')
252 tcd Xtabdir1
253 botright new
254 wincmd t
255 lcd ../Xwindir1
256 tabnew
257 tcd ../Xtabdir2
258 botright new
259 lcd ../Xwindir2
260 tabnew
261 tcd ../Xtabdir3
262 botright new
263 lcd ../Xwindir3
264 tabfirst
265 1wincmd w
266 mksession! Xtest_mks.out
267 only | tabonly
268 source Xtest_mks.out
269 call assert_equal('Xtabdir1', fnamemodify(getcwd(-1, 1), ':t'))
270 call assert_equal('Xwindir1', fnamemodify(getcwd(1, 1), ':t'))
271 call assert_equal('Xtabdir1', fnamemodify(getcwd(2, 1), ':t'))
272 call assert_equal('Xtabdir2', fnamemodify(getcwd(-1, 2), ':t'))
273 call assert_equal('Xtabdir2', fnamemodify(getcwd(1, 2), ':t'))
274 call assert_equal('Xwindir2', fnamemodify(getcwd(2, 2), ':t'))
275 call assert_equal('Xtabdir3', fnamemodify(getcwd(-1, 3), ':t'))
276 call assert_equal('Xtabdir3', fnamemodify(getcwd(1, 3), ':t'))
277 call assert_equal('Xwindir3', fnamemodify(getcwd(2, 3), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100278 %bwipe
279 call chdir(save_cwd)
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200280 call delete("Xtopdir", "rf")
281endfunc
282
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200283func Test_mksession_blank_tabs()
284 tabnew
285 tabnew
286 tabnew
287 tabnext 3
288 mksession! Xtest_mks.out
289 tabnew
290 tabnew
291 tabnext 2
292 source Xtest_mks.out
293 call assert_equal(4, tabpagenr('$'), 'session restore should restore number of tabs')
294 call assert_equal(3, tabpagenr(), 'session restore should restore the active tab')
295 call delete('Xtest_mks.out')
296endfunc
297
Bram Moolenaard39e2752019-01-26 20:07:38 +0100298func Test_mksession_buffer_count()
299 set hidden
300
301 " Edit exactly three files in the current session.
302 %bwipe!
303 e Xfoo | tabe Xbar | tabe Xbaz
304 tabdo write
305 mksession! Xtest_mks.out
306
307 " Verify that loading the session does not create additional buffers.
308 %bwipe!
309 source Xtest_mks.out
310 call assert_equal(3, len(getbufinfo()))
311
312 " Clean up.
313 call delete('Xfoo')
314 call delete('Xbar')
315 call delete('Xbaz')
316 call delete('Xtest_mks.out')
317 %bwipe!
318 set hidden&
319endfunc
320
Bram Moolenaare3c74d22019-01-12 16:29:30 +0100321if has('extra_search')
322
323func Test_mksession_hlsearch()
324 set hlsearch
325 mksession! Xtest_mks.out
326 nohlsearch
327 source Xtest_mks.out
328 call assert_equal(1, v:hlsearch, 'session should restore search highlighting state')
329 nohlsearch
330 mksession! Xtest_mks.out
331 source Xtest_mks.out
332 call assert_equal(0, v:hlsearch, 'session should restore search highlighting state')
333 call delete('Xtest_mks.out')
334endfunc
335
336endif
337
338
Bram Moolenaar26d4b892018-07-04 22:26:28 +0200339func Test_mksession_blank_windows()
340 split
341 split
342 split
343 3 wincmd w
344 mksession! Xtest_mks.out
345 split
346 split
347 2 wincmd w
348 source Xtest_mks.out
349 call assert_equal(4, winnr('$'), 'session restore should restore number of windows')
350 call assert_equal(3, winnr(), 'session restore should restore the active window')
351 call delete('Xtest_mks.out')
352endfunc
353
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100354if has('terminal')
355
356func Test_mksession_terminal_shell()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100357 CheckFeature quickfix
358
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100359 terminal
360 mksession! Xtest_mks.out
361 let lines = readfile('Xtest_mks.out')
362 let term_cmd = ''
363 for line in lines
364 if line =~ '^terminal'
365 let term_cmd = line
366 elseif line =~ 'badd.*' . &shell
367 call assert_report('unexpected shell line: ' . line)
368 endif
369 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100370 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+\s*.*$', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100371
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200372 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100373 call delete('Xtest_mks.out')
374endfunc
375
376func Test_mksession_terminal_no_restore_cmdarg()
377 terminal ++norestore
378 mksession! Xtest_mks.out
379 let lines = readfile('Xtest_mks.out')
380 let term_cmd = ''
381 for line in lines
382 if line =~ '^terminal'
383 call assert_report('session must not restore teminal')
384 endif
385 endfor
386
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200387 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100388 call delete('Xtest_mks.out')
389endfunc
390
391func Test_mksession_terminal_no_restore_funcarg()
392 call term_start(&shell, {'norestore': 1})
393 mksession! Xtest_mks.out
394 let lines = readfile('Xtest_mks.out')
395 let term_cmd = ''
396 for line in lines
397 if line =~ '^terminal'
398 call assert_report('session must not restore teminal')
399 endif
400 endfor
401
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200402 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100403 call delete('Xtest_mks.out')
404endfunc
405
406func Test_mksession_terminal_no_restore_func()
407 terminal
408 call term_setrestore(bufnr('%'), 'NONE')
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'
414 call assert_report('session must not restore teminal')
415 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_ssop()
423 terminal
424 set sessionoptions-=terminal
425 mksession! Xtest_mks.out
426 let lines = readfile('Xtest_mks.out')
427 let term_cmd = ''
428 for line in lines
429 if line =~ '^terminal'
430 call assert_report('session must not restore teminal')
431 endif
432 endfor
433
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200434 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100435 call delete('Xtest_mks.out')
436 set sessionoptions&
437endfunc
438
439func Test_mksession_terminal_restore_other()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +0100440 CheckFeature quickfix
441
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100442 terminal
Bram Moolenaar7ee80f72019-09-08 20:55:06 +0200443 eval bufnr('%')->term_setrestore('other')
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100444 mksession! Xtest_mks.out
445 let lines = readfile('Xtest_mks.out')
446 let term_cmd = ''
447 for line in lines
448 if line =~ '^terminal'
449 let term_cmd = line
450 endif
451 endfor
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100452 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+.*other', term_cmd)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100453
Bram Moolenaar7a39dd72019-06-23 00:50:15 +0200454 call StopShellInTerminal(bufnr('%'))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100455 call delete('Xtest_mks.out')
456endfunc
457
Bram Moolenaar0e655112020-09-11 20:36:36 +0200458func Test_mksession_terminal_shared_windows()
459 terminal
460 let term_buf = bufnr()
461 new
462 execute "buffer" term_buf
463 mksession! Xtest_mks.out
464
465 let lines = readfile('Xtest_mks.out')
466 let found_creation = 0
467 let found_use = 0
468
469 for line in lines
470 if line =~ '^terminal'
471 let found_creation = 1
472 call assert_match('terminal ++curwin ++cols=\d\+ ++rows=\d\+', line)
473 elseif line =~ "^execute 'buffer ' . s:term_buf_" . term_buf . "$"
474 let found_use = 1
475 endif
476 endfor
477
478 call assert_true(found_creation && found_use)
479
480 call StopShellInTerminal(term_buf)
481 call delete('Xtest_mks.out')
482endfunc
483
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100484endif " has('terminal')
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +0200485
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200486" Test :mkview with a file argument.
487func Test_mkview_file()
488 " Create a view with line number and a fold.
489 help :mkview
490 set number
Bram Moolenaar2bf4fe02018-08-30 14:04:25 +0200491 norm! V}zf0
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200492 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200493 let linefoldclosed1 = foldclosed('.')
494 mkview! Xview
495 set nonumber
496 norm! zrj
497 " We can close the help window, as mkview with a file name should
498 " generate a command to edit the file.
499 helpclose
500
501 source Xview
502 call assert_equal(1, &number)
503 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200504 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200505 call assert_equal(linefoldclosed1, foldclosed('.'))
506
507 " Creating a view again with the same file name should fail (file
508 " already exists). But with a !, the previous view should be
509 " overwritten without error.
510 help :loadview
511 call assert_fails('mkview Xview', 'E189:')
512 call assert_match('\*:loadview\*$', getline('.'))
513 mkview! Xview
514 call assert_match('\*:loadview\*$', getline('.'))
515
516 call delete('Xview')
517 bwipe
518endfunc
519
520" Test :mkview and :loadview with a custom 'viewdir'.
521func Test_mkview_loadview_with_viewdir()
522 set viewdir=Xviewdir
523
524 help :mkview
525 set number
526 norm! V}zf
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200527 let pos = getpos('.')
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200528 let linefoldclosed1 = foldclosed('.')
529 mkview 1
530 set nonumber
531 norm! zrj
532
533 loadview 1
534
535 " The directory Xviewdir/ should have been created and the view
536 " should be stored in that directory.
537 call assert_equal('Xviewdir/' .
538 \ substitute(
539 \ substitute(
540 \ expand('%:p'), '/', '=+', 'g'), ':', '=-', 'g') . '=1.vim',
541 \ glob('Xviewdir/*'))
542 call assert_equal(1, &number)
543 call assert_match('\*:mkview\*$', getline('.'))
Bram Moolenaardd5d18e2018-08-30 14:16:06 +0200544 call assert_equal(pos, getpos('.'))
Bram Moolenaar627cb6a2018-08-28 22:19:31 +0200545 call assert_equal(linefoldclosed1, foldclosed('.'))
546
547 call delete('Xviewdir', 'rf')
548 set viewdir&
549 helpclose
550endfunc
551
552func Test_mkview_no_file_name()
553 new
554 " :mkview or :mkview {nr} should fail in a unnamed buffer.
555 call assert_fails('mkview', 'E32:')
556 call assert_fails('mkview 1', 'E32:')
557
558 " :mkview {file} should succeed in a unnamed buffer.
559 mkview Xview
560 help
561 source Xview
562 call assert_equal('', bufname('%'))
563
564 call delete('Xview')
565 %bwipe
566endfunc
Bram Moolenaar79da5632017-02-01 22:52:44 +0100567
Bram Moolenaar555de4e2019-01-21 23:03:49 +0100568" A clean session (one empty buffer, one window, and one tab) should not
569" set any error messages when sourced because no commands should fail.
570func Test_mksession_no_errmsg()
571 let v:errmsg = ''
572 %bwipe!
573 mksession! Xtest_mks.out
574 source Xtest_mks.out
575 call assert_equal('', v:errmsg)
576 call delete('Xtest_mks.out')
577endfunc
578
Bram Moolenaarad36a352019-01-24 13:34:42 +0100579func Test_mksession_quote_in_filename()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200580 " only Unix can handle this weird filename
581 CheckUnix
582
Bram Moolenaarad36a352019-01-24 13:34:42 +0100583 let v:errmsg = ''
584 %bwipe!
585 split another
586 split x'y\"z
587 mksession! Xtest_mks_quoted.out
588 %bwipe!
589 source Xtest_mks_quoted.out
590 call assert_true(bufexists("x'y\"z"))
591
592 %bwipe!
593 call delete('Xtest_mks_quoted.out')
594endfunc
595
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200596" Test for storing global variables in a session file
597func Test_mksession_globals()
598 set sessionoptions+=globals
599
600 " create different global variables
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200601 let g:Global_string = "Sun is shining\r\n"
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200602 let g:Global_count = 100
603 let g:Global_pi = 3.14
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200604 let g:Global_neg_float = -2.68
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200605
606 mksession! Xtest_mks.out
607
608 unlet g:Global_string
609 unlet g:Global_count
610 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200611 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200612
613 source Xtest_mks.out
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200614 call assert_equal("Sun is shining\r\n", g:Global_string)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200615 call assert_equal(100, g:Global_count)
616 call assert_equal(3.14, g:Global_pi)
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200617 call assert_equal(-2.68, g:Global_neg_float)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200618
619 unlet g:Global_string
620 unlet g:Global_count
621 unlet g:Global_pi
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200622 unlet g:Global_neg_float
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200623 call delete('Xtest_mks.out')
624 set sessionoptions&
625endfunc
626
627" Test for changing backslash to forward slash in filenames
628func Test_mksession_slash()
Bram Moolenaar37ac4a52019-09-28 19:29:04 +0200629 if exists('+shellslash')
630 throw 'Skipped: cannot use backslash in file name'
631 endif
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200632 enew
633 %bwipe!
634 e a\\b\\c
635 mksession! Xtest_mks1.out
636 set sessionoptions+=slash
637 mksession! Xtest_mks2.out
638
639 %bwipe!
640 source Xtest_mks1.out
641 call assert_equal('a\b\c', bufname(''))
642 %bwipe!
643 source Xtest_mks2.out
644 call assert_equal('a/b/c', bufname(''))
645
646 %bwipe!
647 call delete('Xtest_mks1.out')
648 call delete('Xtest_mks2.out')
649 set sessionoptions&
650endfunc
651
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200652" Test for changing directory to the session file directory
653func Test_mksession_sesdir()
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100654 let save_cwd = getcwd()
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200655 call mkdir('Xproj')
656 mksession! Xproj/Xtest_mks1.out
657 set sessionoptions-=curdir
658 set sessionoptions+=sesdir
659 mksession! Xproj/Xtest_mks2.out
660
661 source Xproj/Xtest_mks1.out
662 call assert_equal('testdir', fnamemodify(getcwd(), ':t'))
663 source Xproj/Xtest_mks2.out
664 call assert_equal('Xproj', fnamemodify(getcwd(), ':t'))
Bram Moolenaar5bf46e92019-11-09 18:06:04 +0100665 call chdir(save_cwd)
666 %bwipe
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200667
668 set sessionoptions&
669 call delete('Xproj', 'rf')
670endfunc
671
672" Test for storing the 'lines' and 'columns' settings
673func Test_mksession_resize()
674 mksession! Xtest_mks1.out
675 set sessionoptions+=resize
676 mksession! Xtest_mks2.out
677
678 let lines = readfile('Xtest_mks1.out')
679 let found_resize = v:false
680 for line in lines
681 if line =~ '^set lines='
682 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200683 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200684 endif
685 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200686 call assert_false(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200687 let lines = readfile('Xtest_mks2.out')
688 let found_resize = v:false
689 for line in lines
690 if line =~ '^set lines='
691 let found_resize = v:true
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200692 break
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200693 endif
694 endfor
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200695 call assert_true(found_resize)
Bram Moolenaar66f0e6c2019-09-28 19:08:45 +0200696
697 call delete('Xtest_mks1.out')
698 call delete('Xtest_mks2.out')
699 set sessionoptions&
700endfunc
701
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200702" Test for mksession with a named scratch buffer
703func Test_mksession_scratch()
704 enew | only
705 file Xscratch
706 set buftype=nofile
707 mksession! Xtest_mks.out
708 %bwipe
709 source Xtest_mks.out
710 call assert_equal('Xscratch', bufname(''))
711 call assert_equal('nofile', &buftype)
712 %bwipe
713 call delete('Xtest_mks.out')
714endfunc
715
716" Test for mksession with fold options
717func Test_mksession_foldopt()
718 set sessionoptions-=options
719 set sessionoptions+=folds
720 new
721 setlocal foldenable
722 setlocal foldmethod=expr
723 setlocal foldmarker=<<<,>>>
724 setlocal foldignore=%
725 setlocal foldlevel=2
726 setlocal foldminlines=10
727 setlocal foldnestmax=15
728 mksession! Xtest_mks.out
729 close
730 %bwipe
731
732 source Xtest_mks.out
733 call assert_true(&foldenable)
734 call assert_equal('expr', &foldmethod)
735 call assert_equal('<<<,>>>', &foldmarker)
736 call assert_equal('%', &foldignore)
737 call assert_equal(2, &foldlevel)
738 call assert_equal(10, &foldminlines)
739 call assert_equal(15, &foldnestmax)
740
741 close
742 %bwipe
743 set sessionoptions&
744endfunc
745
746" Test for mksession with window position
747func Test_mksession_winpos()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200748 " Only applicable in GUI Vim
749 CheckGui
750
Bram Moolenaaree1a2b52019-09-29 14:23:39 +0200751 set sessionoptions+=winpos
752 mksession! Xtest_mks.out
753 let found_winpos = v:false
754 let lines = readfile('Xtest_mks.out')
755 for line in lines
756 if line =~ '^winpos '
757 let found_winpos = v:true
758 break
759 endif
760 endfor
761 call assert_true(found_winpos)
762 call delete('Xtest_mks.out')
763 set sessionoptions&
764endfunc
765
766" Test for mksession with 'compatible' option
767func Test_mksession_compatible()
768 mksession! Xtest_mks1.out
769 set compatible
770 mksession! Xtest_mks2.out
771 set nocp
772
773 let test_success = v:false
774 let lines = readfile('Xtest_mks1.out')
775 for line in lines
776 if line =~ '^if &cp | set nocp | endif'
777 let test_success = v:true
778 break
779 endif
780 endfor
781 call assert_true(test_success)
782
783 let test_success = v:false
784 let lines = readfile('Xtest_mks2.out')
785 for line in lines
786 if line =~ '^if !&cp | set cp | endif'
787 let test_success = v:true
788 break
789 endif
790 endfor
791 call assert_true(test_success)
792
793 call delete('Xtest_mks1.out')
794 call delete('Xtest_mks2.out')
795 set compatible&
796 set sessionoptions&
797endfunc
798
Bram Moolenaar87500262019-08-01 23:05:49 +0200799func s:ClearMappings()
800 mapclear
801 omapclear
802 mapclear!
803 lmapclear
804 tmapclear
805endfunc
806
807func Test_mkvimrc()
808 let entries = [
809 \ ['', 'nothing', '<Nop>'],
810 \ ['n', 'normal', 'NORMAL'],
811 \ ['v', 'visual', 'VISUAL'],
812 \ ['s', 'select', 'SELECT'],
813 \ ['x', 'visualonly', 'VISUALONLY'],
814 \ ['o', 'operator', 'OPERATOR'],
815 \ ['i', 'insert', 'INSERT'],
816 \ ['l', 'lang', 'LANG'],
817 \ ['c', 'command', 'COMMAND'],
818 \ ['t', 'terminal', 'TERMINAL'],
819 \ ]
820 for entry in entries
821 exe entry[0] .. 'map ' .. entry[1] .. ' ' .. entry[2]
822 endfor
823
824 mkvimrc Xtestvimrc
825
826 call s:ClearMappings()
827 for entry in entries
828 call assert_equal('', maparg(entry[1], entry[0]))
829 endfor
830
831 source Xtestvimrc
832
833 for entry in entries
834 call assert_equal(entry[2], maparg(entry[1], entry[0]))
835 endfor
836
837 call s:ClearMappings()
838 call delete('Xtestvimrc')
839endfunc
840
Bram Moolenaareca626f2016-12-01 18:47:38 +0100841" vim: shiftwidth=2 sts=2 expandtab